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;
}