19,468
社区成员
发帖
与我相关
我的任务
分享
gettimeofday(&tp1, NULL);
//Convert BMP to JPG
cinfo.err = jpeg_std_error(&jerr);
//* Now we can initialize the JPEG compression object.
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, output_file);
cinfo.image_width = bih.biWidth; //* image width and height, in pixels
cinfo.image_height = bih.biHeight;
cinfo.input_components = depth; //* # of color components per pixel
if(depth==3)
{cinfo.in_color_space = JCS_RGB;} //* colorspace of input image
else if(depth==1)
{cinfo.in_color_space = JCS_GRAYSCALE;}
jpeg_set_defaults(&cinfo);
//Now you can set any non-default parameters you wish to.
//Here we just illustrate the use of quality (quantization table) scaling:
jpeg_set_quality(&cinfo, JPEG_QUALITY, TRUE ); //* limit to baseline-JPEG values
jpeg_start_compress(&cinfo, TRUE);
point=dst_buffer; //获取BMP像素数据
while (cinfo.next_scanline<bih.biHeight)
{
row_pointer[0]=&point[0];
jpeg_write_scanlines(&cinfo,row_pointer,1);
point+=bih.biWidth*depth;
}
gettimeofday(&tp2, NULL);
jpeg_finish_compress(&cinfo);
printf("cost time=%ld us\n", (tp2.tv_sec - tp1.tv_sec)*1000000 + tp2.tv_usec - tp1.tv_usec) ;
int ConvertToJpg(BYTE *pBitmap, const RECT& rect, int nQuality, float fScale, BYTE *pJpgBuf, int &nJpgSize)
{
int nRet = 0, nWidth = rect.right - rect.left, nHeight = rect.bottom - rect.top;
JPEG_CORE_PROPERTIES jcprops;
IJLERR jerr = ijlInit(&jcprops);
if (IJL_OK != jerr)
{
("ijlInit failed! jerr=%d\n", jerr);
return -1;
}
do
{
//保证缓冲区够大
if (nWidth * nHeight * 2 > nJpgSize)
{
("buffer too small\n");
nRet = -1;
break;
}
jcprops.DIBWidth = nWidth;
jcprops.DIBHeight = nHeight;
jcprops.DIBBytes = pBitmap;
jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(nWidth, 3);
jcprops.DIBChannels = 3;
jcprops.DIBColor = IJL_BGR;
jcprops.JPGWidth = (int)(nWidth * fScale);
jcprops.JPGHeight = (int)(nHeight * fScale);
jcprops.JPGFile = NULL;
jcprops.JPGBytes = pJpgBuf;
jcprops.JPGSizeBytes = nJpgSize;
jcprops.JPGChannels = 3;
jcprops.JPGColor = IJL_YCBCR;
jcprops.JPGSubsampling = IJL_411; //4:1:1 subsampling.
jcprops.jquality = nQuality; //Write the actual JPEG image from the pixel buffer.
jerr = ijlWrite(&jcprops, IJL_JBUFF_WRITEWHOLEIMAGE);
if (IJL_OK != jerr)
{
("ijlWrite failed! jerr=%d\n", jerr);
nRet = -1;
break;
}
//JPG图像大小
nJpgSize = jcprops.JPGSizeBytes;
("JPG Image Size=%d\n", nJpgSize);
}while(FALSE);
ijlFree(&jcprops);
return nRet;
}