使用intel ijl庫 RGB轉Jpg

smilestone322發表於2016-03-01

BOOL CRawXXX:: EncodeJpegUseIJL(char *dstfile,int *len, int width, int height, char* srcdata, int imageChannels, int quality)

{

     BOOL res = FALSE;

     JPEG_CORE_PROPERTIES image;

     IJLERR err;

     ZeroMemory( &image, sizeof( JPEG_CORE_PROPERTIES ) );

     IJLIOTYPE iotype;

 

     try

     {

         if( ijlInit( &image ) != IJL_OK )

         {

              OutputDebugString( "Can't initialize Intel(R) JPEG library" );

              return FALSE;

         }

         image.DIBWidth  = width;

         image.DIBHeight = height;

         image.DIBBytes  = (unsigned char*)srcdata;

         if (!len || *len == 0)

         {

              image.JPGFile = dstfile;

              iotype = IJL_JFILE_WRITEWHOLEIMAGE;

         }

         else

         {

              image.JPGBytes    = (unsigned char*)dstfile;

              image.JPGSizeBytes = *len;

              iotype = IJL_JBUFF_WRITEWHOLEIMAGE;

         }

         image.JPGWidth  = width;

         image.JPGHeight = abs(height);

         image.jquality = quality;

         switch(imageChannels)

         {

         case 1:

              image.DIBColor       = IJL_G;

              image.DIBChannels    = 1;

              image.DIBPadBytes    = IJL_DIB_PAD_BYTES(image.DIBWidth,1);

              image.JPGColor       = IJL_G;

              image.JPGChannels    = 1;

              image.JPGSubsampling = IJL_NONE;

              break;

         case 3:

              image.DIBColor       = IJL_BGR;

              image.DIBChannels    = 3;

              image.DIBPadBytes    = IJL_DIB_PAD_BYTES(image.DIBWidth,3);

              image.JPGColor       = IJL_YCBCR;

              image.JPGChannels    = 3;

              image.JPGSubsampling = IJL_411;

              break;           

         case 4:

              image.DIBColor       = IJL_RGBA_FPX;

              image.DIBChannels    = 4;

              image.DIBPadBytes    = IJL_DIB_PAD_BYTES(image.DIBWidth,4);

              image.JPGColor       = IJL_YCBCRA_FPX;

              image.JPGChannels    = 4;

              image.JPGSubsampling = IJL_4114;           

              break;

 

         default:

              break;

         }

 

         if( (err = ijlWrite( &image, iotype )) != IJL_OK )

         {

              OutputDebugString( "Can't write image(%d)");

         }

         if (len)

              *len = image.JPGSizeBytes;

         if( ijlFree( &image ) != IJL_OK )

         {

              OutputDebugString( "Can't free Intel(R) JPEG library" );

         }

         res = TRUE;

     }

     catch (...)

     {

         OutputDebugString( "EncodeJpegUseIJL unknown error" );

     }

     return res;

}

引數說明:

引數分別是:[輸出引數]壓縮後的緩衝區、大小,[輸入]bmp寬、高,bmp緩衝區,影象位(一般寫 3),壓縮質量1-99

 

呼叫方式:

       char *bjpgData=new char[pRgb888.width*pRgb888.height*3];

     int lSize=1628*1236*3;

     if(EncodeJpegUseIJL((char*)bjpgData,&lSize,pRgb888.width,pRgb888.height*(-1),(char*)pRgb888.buffer,3,80))

     {

         //壓縮成功了

         TRACE("rgb to jpg success\n");

     }

 

     //儲存影象

     SYSTEMTIME t;

     GetLocalTime(&t);

     char filename[128];

     sprintf(filename, "%04d-%02d-%02d-%02d-%02d-%02d-%03d.jpg",

         t.wYear, t.wMonth, t.wDay,

         t.wHour, t.wMinute, t.wSecond, t.wMilliseconds);

     FILE *fp = fopen(filename, "wb");

     fwrite(bjpgData, lSize, 1, fp);

         fclose(fp);                    

 

封裝後的函式如下:

//ijl bmp to jpg 其中ijlInit等函式的定義

int CImgXXX:rgb24_to_jpeg_ijl(unsigned char *data,int image_width, int image_height, char *filename, int quality )

{

 

     JPEG_CORE_PROPERTIES jcprops;

     IJLERR jerr;

     jerr = ijlInit(&jcprops);

     if(IJL_OK != jerr)

     {

         /*MessageBox(ijlErrorStr(jerr),"ijlInit function called failed!",MB_OK);*/

         ijlFree(&jcprops);

         return -1;

     }

 

     long LineSize = (image_width *3+3)/4*4;

 

     //initialize the compression parameters describing the kind of Bitmap we are encoding from

 

     jcprops.DIBChannels = 3;

     jcprops.DIBColor = IJL_BGR;//Color space of uncompressed data.

     jcprops.DIBHeight = image_height;

     jcprops.DIBWidth = image_width;

     jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jcprops.DIBWidth,jcprops.JPGChannels);

 

     //

     jcprops.JPGWidth = image_width;

     jcprops.JPGHeight =image_height;

 

     jcprops.JPGFile = filename;

 

     int w,h;

     w = image_width;

     h = image_height;

     BYTE * JPEGimgdata = new BYTE[image_width*image_height*3];

 

     for(int i=0;i<h;i++)

     {

         memcpy(JPEGimgdata+i*LineSize,data+(h-i-1)*LineSize,LineSize);

     }

 

     jcprops.DIBBytes = JPEGimgdata;

 

     //write the entire JPEG image. this create a JFIF file.

     jerr = ijlWrite(&jcprops,IJL_JFILE_WRITEWHOLEIMAGE);

     if(IJL_OK != jerr)

     {

         //MessageBox(ijlErrorStr(jerr),"ijlWrite function called failed!",MB_OK);

 

         //release the IJL

         ijlFree(&jcprops);

         //release file

         delete[] JPEGimgdata;

         JPEGimgdata = NULL;

         return -1;

     }

 

     //release the IJL

     ijlFree(&jcprops);

 

     delete[] JPEGimgdata;

     JPEGimgdata = NULL;

 

     return 0;

}

相關文章