freeimage 图像转char*

来源:互联网 发布:华讯网络笔试题 编辑:程序博客网 时间:2024/06/05 00:25
#include "stdio.h"#include "stdlib.h"#include "unistd.h"#include "FreeImage.h"#include "OpenJPEG.h"#include "wrappers.hpp"#include "gradientMex.hpp"#define  PI  3.141592645// 自定义一个4字节的结构体class byte4{public:    BYTE r; // 用于存放 red    BYTE g; // 用于存放 green    BYTE b; // 用于存放 blue    BYTE a; // 用于存放 alpha};///////////////////////////////////缩放图像static void _ieInterpImageBilinear8UC1_Ver3_RowFilter(unsigned char* src, long* dst, int len, int* leftIdx, int* rightIdx, long* weight, int shift){    int i;    for(i = 0; i < len - 4; i+=4) {        *dst++ = ((1<<shift) - weight[i])*src[leftIdx[i]] + weight[i]*src[rightIdx[i]];        *dst++ = ((1<<shift) - weight[i+1])*src[leftIdx[i+1]] + weight[i+1]*src[rightIdx[i+1]];        *dst++ = ((1<<shift) - weight[i+2])*src[leftIdx[i+2]] + weight[i+2]*src[rightIdx[i+2]];        *dst++ = ((1<<shift) - weight[i+3])*src[leftIdx[i+3]] + weight[i+3]*src[rightIdx[i+3]];    }    for( ; i < len; ++i) {        *dst++ = ((1<<shift) - weight[i])*src[leftIdx[i]] + weight[i]*src[rightIdx[i]];    }}#define IET_MAX(x,y) (x)>(y)?(x):(y)#define IET_MIN(x,y) (x)>(y)?(y):(x)#define IET_SWAP(x,y,tmp) (tmp)=(x);(x)=(y);(y)=(tmp);static void ResizeImage(unsigned char* pSrc,int src_w,int src_h,                        unsigned char* pDst,int dst_w, int dst_h){    int i, j;    int sw, sh, sstep;    int dw, dh, dstep;    unsigned char *sdata, *ddata;    float horScaleRatio, verScaleRatio;    long *rowBuf1, *rowBuf2;    long *upLinePtr, *downLinePtr, *tempPtr;    long *horWeight;    int *horLeftIdx, *horRightIdx;    int preVerUpIdx, preVerDownIdx;    int shift = 8;    sw=src_w;    sh=src_h;    sstep=24;    sdata=pSrc;    dw=dst_w;    dh=dst_h;    dstep=24;    ddata=pDst;    horScaleRatio = sw / (float)(dw);    verScaleRatio = sh / (float)(dh);    rowBuf1 = new long[dw];    rowBuf2 = new long[dw];    horWeight = new long[dw];    horLeftIdx = new int[dw];    horRightIdx = new int[dw];    //col interpolation    //计算目标图像像素横向的左右邻居序号,和权重。    for(i = 0; i < dw; i++) {        float pos = (i + 0.5f) * horScaleRatio;        horLeftIdx[i] = (int)(IET_MAX(pos - 0.5f, 0));        horRightIdx[i] = (int)(IET_MIN(pos + 0.5f, sw-1));        horWeight[i] = (long) (fabs(pos - 0.5f - horLeftIdx[i]) * (1<<shift));    }    preVerUpIdx = -1;    preVerDownIdx = -1;    upLinePtr = rowBuf1;    downLinePtr = rowBuf2;    for(j = 0; j < dh; j++) {        float pos = (j + 0.5f) * verScaleRatio;        int verUpIdx = (int)(IET_MAX(pos - 0.5f, 0));        int verDownIdx = (int)(IET_MIN(pos + 0.5f, sh-1));        long verWeight = (long) (fabs(pos - 0.5f - verUpIdx) * (1<<shift));        if(verUpIdx == preVerUpIdx && verDownIdx == preVerDownIdx) {            ;            //do nothing        } else if(verUpIdx == preVerDownIdx) {            IET_SWAP(upLinePtr, downLinePtr, tempPtr);            _ieInterpImageBilinear8UC1_Ver3_RowFilter(sdata + sstep*verDownIdx, downLinePtr, dw, horLeftIdx, horRightIdx, horWeight, shift);        } else {            _ieInterpImageBilinear8UC1_Ver3_RowFilter(sdata + sstep*verUpIdx,   upLinePtr, dw, horLeftIdx, horRightIdx, horWeight, shift);            _ieInterpImageBilinear8UC1_Ver3_RowFilter(sdata + sstep*verDownIdx, downLinePtr, dw, horLeftIdx, horRightIdx, horWeight, shift);        }        unsigned char* _ptr = ddata + dstep*j;        for(i = 0; i < dw-4; i+=4) {            *_ptr++ = (unsigned char) ( (((1<<shift) - verWeight)*upLinePtr[i] + verWeight*downLinePtr[i]) >> (2*shift) );            *_ptr++ = (unsigned char) ( (((1<<shift) - verWeight)*upLinePtr[i+1] + verWeight*downLinePtr[i+1]) >> (2*shift) );            *_ptr++ = (unsigned char) ( (((1<<shift) - verWeight)*upLinePtr[i+2] + verWeight*downLinePtr[i+2]) >> (2*shift) );            *_ptr++ = (unsigned char) ( (((1<<shift) - verWeight)*upLinePtr[i+3] + verWeight*downLinePtr[i+3]) >> (2*shift) );        }        for(; i < dw; i++) {            *_ptr++ = (unsigned char) ( (((1<<shift) - verWeight)*upLinePtr[i] + verWeight*downLinePtr[i]) >> (2*shift) );        }        preVerUpIdx = verUpIdx;        preVerDownIdx = verDownIdx;    }    delete []rowBuf1;    delete []rowBuf2;    delete []horWeight;    delete []horLeftIdx;    delete []horRightIdx;}void chog(){    char *src="1.jpg";       char *dst="2.jpg"; //    RescalePic(src,600,400,dst); #if defined(FREEIMAGE_LIB) || !defined(WIN32)             FreeImage_Initialise(); #endif    FIBITMAP *c_bitmap = NULL ;    OpenJPEG *openJPEG=new OpenJPEG();         FIBITMAP *bmp =openJPEG->LoadDIB(src,0);         if( bmp ) {         printf("aaaaaaaaaaaaaaaaaaa");     int bpp;         bpp = FreeImage_GetBPP(bmp);         switch (bpp)         {         case 32:             break;         default:             FIBITMAP *bmpTemp = FreeImage_ConvertTo32Bits(bmp);             if (bmp != NULL) FreeImage_Unload(bmp);             bmp = bmpTemp;             bpp = FreeImage_GetBPP(bmp);             break;         }         const int orientations = 9;         // ensure array is continuous //        const cv::Mat& image = (img.isContinuous() ? img : img.clone());         int binSize=4;         int channels =3;// image.channels();         int computeChannels = 32;        int width = FreeImage_GetWidth(bmp);        int height = FreeImage_GetHeight(bmp);        bpp = FreeImage_GetBPP(bmp);        switch (bpp)        {        case 32:            break;        default:            FIBITMAP *bmpTemp = FreeImage_ConvertTo32Bits(bmp);            if (bmp != NULL) FreeImage_Unload(bmp);            bmp = bmpTemp;            bpp = FreeImage_GetBPP(bmp);            break;        }        unsigned char* pixels = (unsigned char*) malloc(sizeof(unsigned char) * 4 * width * height);          FreeImage_ConvertToRawBits(pixels, bmp, width * 4, bpp, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, true);            unsigned char* dstpixels = (unsigned char*) malloc(sizeof(unsigned char) * 4 *4* width * height); ResizeImage(pixels,width,height,dstpixels,width*2,height*2);//  FIBITMAP* bitmap = FreeImage_ConvertFromRawBits(dstpixels,width*2,height*2,width*2*3,32,8,8,8,FALSE);  FIBITMAP* image = FreeImage_ConvertFromRawBits(pixels, width,height,width*3, 24, 0x0000FF, 0xFF0000, 0x00FF00, false);  FreeImage_Save(FIF_BMP, image, "aaaaa.jpg", 0);//  openJPEG->Savejpg(bitmap,"aaaaa.jpg"); //DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertFromRawBits(BYTE *bits, int width, int height, int pitch, unsigned bpp, unsigned red_mask, unsigned green_mask, unsigned blue_mask, BOOL topdown FI_DEFAULT(FALSE));// FreeImage_ConvertFromRawBits(dstpixels,width*2,height*2,width*2*3,bpp); //         width =bitmap->data;// image.cols; //         height = image.rows;         int widthBin = width / binSize;         int heightBin = height / binSize;         float* const I = (float*)wrCalloc(static_cast<size_t>(width * height * channels), sizeof(float));         float* const H = (float*)wrCalloc(static_cast<size_t>(widthBin * heightBin * computeChannels), sizeof(float));         float* const M = (float*)wrCalloc(static_cast<size_t>(width * height), sizeof(float));         float* const O = (float*)wrCalloc(static_cast<size_t>(width * height), sizeof(float));         // row major (interleaved) to col major (non interleaved;clustered) //        float* imageData = reinterpret_cast<float*>(image.data);         float* const redChannel = I;         float* const greenChannel = I + width * height;         float* const blueChannel = I + 2 * width * height;         int colMajorPos = 0, rowMajorPos = 0;         for (int row = 0; row < height; ++row)         {             for (int col = 0; col < width; ++col)             {                 colMajorPos = col * height + row;                 rowMajorPos = row * channels * width + col * channels;                 blueChannel[colMajorPos] = pixels[rowMajorPos];                 greenChannel[colMajorPos] = pixels[rowMajorPos + 1];                 redChannel[colMajorPos] = pixels[rowMajorPos + 2];             }         }         // calc fhog in col major         piotr::gradMag(I, M, O, height, width, channels, true); //        if (fhogChannelsToCopy == 27) //            fhog(M, O, H, height, width, binSize, orientations, -1, 0.2f, false); //        else           piotr::fhog_o(M, O, H, height, width, binSize, orientations, -1, 0.2f);           int fhogChannelsToCopy = 31;           wrFree(M);           wrFree(O);           wrFree(I);           wrFree(H);         }}int main(int argc, char *argv[]){chog();}



0 0
原创粉丝点击