jpeglib.h
提供给我们各种图形转换的功能
但现在我想将将打开的BMP图像放入内存,对jpeglib进行了学习,但太复杂了,在如下一段代码中...
BOOL JpegFile::RGBToJpegFile(CString fileName,
BYTE *dataBuf,
UINT widthPix,
UINT height,
BOOL color,
int quality)
{
if (dataBuf==NULL)
return FALSE;
if (widthPix==0)
return FALSE;
if (height==0)
return FALSE; LPBYTE tmp;
if (!color)
{
tmp = (BYTE*)new BYTE[widthPix*height];
if (tmp==NULL)
{
AfxMessageBox("Memory error");
return FALSE;
} UINT row,col;
for (row=0;row<height;row++)
{
for (col=0;col<widthPix;col++)
{
LPBYTE pRed, pGrn, pBlu;
pRed = dataBuf + row * widthPix * 3 + col * 3;
pGrn = dataBuf + row * widthPix * 3 + col * 3 + 1;
pBlu = dataBuf + row * widthPix * 3 + col * 3 + 2; // luminance
int lum = (int)(.299 * (double)(*pRed) + .587 * (double)(*pGrn) + .114 * (double)(*pBlu));
LPBYTE pGray;
pGray = tmp + row * widthPix + col;
*pGray = (BYTE)lum;
}
}
} struct jpeg_compress_struct cinfo;
struct my_error_mgr jerr;
if (setjmp(jerr.setjmp_buffer))
{ jpeg_destroy_compress(&cinfo); if (outfile!=NULL)
fclose(outfile); if (!color)
{
delete [] tmp;
}
return FALSE;
}
/* Now we can initialize the JPEG compression object. */
jpeg_create_compress(&cinfo); /* Step 2: specify data destination (eg, a file) */ if ((outfile = fopen(fileName, "wb")) == NULL)
{
char buf[250];
sprintf(buf, "JpegFile :\nCan't open %s\n", fileName);
AfxMessageBox(buf);
return FALSE;
} jpeg_stdio_dest(&cinfo, outfile); cinfo.image_width = widthPix;
cinfo.image_height = height;
if (color)
{
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
}
else
{
cinfo.input_components = 1;
cinfo.in_color_space = JCS_GRAYSCALE; } jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); jpeg_start_compress(&cinfo, TRUE); row_stride = widthPix * 3;
while (cinfo.next_scanline < cinfo.image_height)
{
LPBYTE outRow;
if (color)
{
outRow = dataBuf + (cinfo.next_scanline * widthPix * 3);
}
else
{
outRow = tmp + (cinfo.next_scanline * widthPix);
} (void) jpeg_write_scanlines(&cinfo, &outRow, 1);
}
jpeg_finish_compress(&cinfo); fclose(outfile); jpeg_destroy_compress(&cinfo); if (!color)
delete [] tmp;
return TRUE;
}跟进去看了各函数,发觉很难改进,大家有没有好的办法呢?
提供给我们各种图形转换的功能
但现在我想将将打开的BMP图像放入内存,对jpeglib进行了学习,但太复杂了,在如下一段代码中...
BOOL JpegFile::RGBToJpegFile(CString fileName,
BYTE *dataBuf,
UINT widthPix,
UINT height,
BOOL color,
int quality)
{
if (dataBuf==NULL)
return FALSE;
if (widthPix==0)
return FALSE;
if (height==0)
return FALSE; LPBYTE tmp;
if (!color)
{
tmp = (BYTE*)new BYTE[widthPix*height];
if (tmp==NULL)
{
AfxMessageBox("Memory error");
return FALSE;
} UINT row,col;
for (row=0;row<height;row++)
{
for (col=0;col<widthPix;col++)
{
LPBYTE pRed, pGrn, pBlu;
pRed = dataBuf + row * widthPix * 3 + col * 3;
pGrn = dataBuf + row * widthPix * 3 + col * 3 + 1;
pBlu = dataBuf + row * widthPix * 3 + col * 3 + 2; // luminance
int lum = (int)(.299 * (double)(*pRed) + .587 * (double)(*pGrn) + .114 * (double)(*pBlu));
LPBYTE pGray;
pGray = tmp + row * widthPix + col;
*pGray = (BYTE)lum;
}
}
} struct jpeg_compress_struct cinfo;
struct my_error_mgr jerr;
if (setjmp(jerr.setjmp_buffer))
{ jpeg_destroy_compress(&cinfo); if (outfile!=NULL)
fclose(outfile); if (!color)
{
delete [] tmp;
}
return FALSE;
}
/* Now we can initialize the JPEG compression object. */
jpeg_create_compress(&cinfo); /* Step 2: specify data destination (eg, a file) */ if ((outfile = fopen(fileName, "wb")) == NULL)
{
char buf[250];
sprintf(buf, "JpegFile :\nCan't open %s\n", fileName);
AfxMessageBox(buf);
return FALSE;
} jpeg_stdio_dest(&cinfo, outfile); cinfo.image_width = widthPix;
cinfo.image_height = height;
if (color)
{
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
}
else
{
cinfo.input_components = 1;
cinfo.in_color_space = JCS_GRAYSCALE; } jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); jpeg_start_compress(&cinfo, TRUE); row_stride = widthPix * 3;
while (cinfo.next_scanline < cinfo.image_height)
{
LPBYTE outRow;
if (color)
{
outRow = dataBuf + (cinfo.next_scanline * widthPix * 3);
}
else
{
outRow = tmp + (cinfo.next_scanline * widthPix);
} (void) jpeg_write_scanlines(&cinfo, &outRow, 1);
}
jpeg_finish_compress(&cinfo); fclose(outfile); jpeg_destroy_compress(&cinfo); if (!color)
delete [] tmp;
return TRUE;
}跟进去看了各函数,发觉很难改进,大家有没有好的办法呢?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货