CImageCompresser *pThisObject=(CImageCompresser *)pArgue;
BYTE *pData=NULL;
BYTE *pDataJPGIn = new BYTE[10000000];
int nWidth,nHeight,nBytesPerPix,nIndex,nMirrorImage;
int nAdjust;
int i,j;
long count;
int nLost = 0;
while ((pThisObject->m_nCompress==1)||(pData!=NULL))
{
pData = pThisObject->BufferOut(&nWidth,&nHeight,&nBytesPerPix,&nIndex);
if (pData==NULL)
{
Sleep(100);
continue;
}
nAdjust = nWidth*nBytesPerPix%4;
if (nAdjust>0) nAdjust = 4-nAdjust;
if (nBytesPerPix==3){
for (j=0;j<nHeight;j++){
for (i = 0;i<nWidth;i++)
{
pDataJPGIn[j*(nWidth*3+nAdjust)+i*3] = pData[j*(nWidth*3+nAdjust)+i*3+2];
pDataJPGIn[j*(nWidth*3+nAdjust)+i*3+1] = pData[j*(nWidth*3+nAdjust)+i*3+1];
pDataJPGIn[j*(nWidth*3+nAdjust)+i*3+2] = pData[j*(nWidth*3+nAdjust)+i*3];
}
}
}else{
for (j=0;j<nHeight;j++){
for (i = 0;i<nWidth;i++)
{
pDataJPGIn[j*(nWidth+nAdjust)+i] = pData[j*(nWidth+nAdjust)+i];
}
}
}
delete [] pData;
pData = NULL;
FILE *f;
char strPathName[255];//路径名
sprintf_s(strPathName, "%s\\", theApp.m_strPicFullPath);
char strDestFileName[255];//带路径图片名
sprintf_s(strDestFileName,"%s%s%d.jpg",strPathName,theApp.m_Channel,nIndex);
fopen_s(&f,strDestFileName,"wb");
if (f==NULL)
{
delete [] pDataJPGIn;
return;
}
struct jpeg_compress_struct jcs;
jpeg_create_compress(&jcs);
struct jpeg_error_mgr jem;
jcs.err = jpeg_std_error(&jem);
jpeg_stdio_dest(&jcs, f);
jcs.image_width = nWidth;
jcs.image_height = nHeight;
jcs.input_components = nBytesPerPix;
if (nBytesPerPix==1)
jcs.in_color_space = JCS_GRAYSCALE;
else
jcs.in_color_space = JCS_RGB;
jpeg_set_defaults(&jcs);
jpeg_set_quality (&jcs, 80, true);
jpeg_start_compress(&jcs, TRUE);
JSAMPROW row_pointer[1];
int row_stride;
row_stride = jcs.image_width*nBytesPerPix;
while (jcs.next_scanline < jcs.image_height) {
row_pointer[0] = & pDataJPGIn[jcs.next_scanline * (row_stride+nAdjust)];
jpeg_write_scanlines(&jcs, row_pointer, 1);
}
jpeg_finish_compress(&jcs);
jpeg_destroy_compress(&jcs);
fclose(f);
theApp.m_iSaveImageCount++;
jpeg压缩
最新推荐文章于 2024-04-12 20:15:00 发布