对bmp文件内存压缩 与 解压缩

对24位bmp位图文件进行jpg的压缩

传出的参数:outdata 压缩后数据内存指针 nSize 压缩后数据的大小

传入参数:bmp文件的内存数据 以及文件信息

需要 jconfig.h jmorecfg.h jpeglib.h libjpeg.lib 文件支持

void BmptoJpg(BYTE* &outdata,int &nSize,BITMAP m_bmpBit1,BYTE* m_pBmpData1,BITMAPINFO BitmapInfo)  //返回压缩后jpg数据,数据的大小
{
  
 int nAdjust24;
 UINT dwRead=0;
  BYTE* pData24;
 nAdjust24 = BitmapInfo.bmiHeader.biWidth*3%4;
 if (nAdjust24) nAdjust24 = 4-nAdjust24;
  pData24 = new BYTE[(BitmapInfo.bmiHeader.biWidth*3+nAdjust24)*BitmapInfo.bmiHeader.biHeight];
  for (int j=0;j   for (int i = 0;i   {
    BYTE red = m_pBmpData1[j*(BitmapInfo.bmiHeader.biWidth*3+nAdjust24)+i*3];
    m_pBmpData1[j*(BitmapInfo.bmiHeader.biWidth*3+nAdjust24)+i*3] =m_pBmpData1[j*(BitmapInfo.bmiHeader.biWidth*3+nAdjust24)+i*3+2];
    m_pBmpData1[j*(BitmapInfo.bmiHeader.biWidth*3+nAdjust24)+i*3+2] = red;
   }
  }
 struct jpeg_compress_struct jcs;
 struct jpeg_error_mgr jem;
 jcs.err = jpeg_std_error(&jem);

 jpeg_create_compress(&jcs);

 jpeg_stdio_dest(&jcs,(char*)outdata,&nSize);
 jcs.image_width = BitmapInfo.bmiHeader.biWidth;    // 为图的宽和高,单位为像素
 jcs.image_height = BitmapInfo.bmiHeader.biHeight;
 jcs.input_components = 3;   // 1,表示灰度图, 如果是彩色位图,则为3
 jcs.in_color_space = JCS_RGB;

 jpeg_set_defaults(&jcs); 
 jpeg_set_quality (&jcs, 60, true);

 jpeg_start_compress(&jcs, TRUE);

 JSAMPROW row_pointer[1];   // 一行位图
 int row_stride;      // 每一行的字节数

 row_stride = jcs.image_width*3;  // 如果不是索引图,此处需要乘以3

 // 对每一行进行压缩
 while (jcs.next_scanline < jcs.image_height) {
     row_pointer[0] = & m_pBmpData1[(jcs.image_height-jcs.next_scanline-1) * (row_stride+nAdjust24)];
     jpeg_write_scanlines(&jcs, row_pointer, 1);
 }

 jpeg_finish_compress(&jcs);

 jpeg_destroy_compress(&jcs);

 delete [] pData24;

}

 

void JpgtoBmp(BYTE *indata,int nSize,int size,BYTE* &data)  //size为生成bmp文件分配的内存大小,后面已更正了RGB序列,但还存在RGB逆序的错误,使生成的文件                                                                                     //颜色不对,还需改进
{
    BITMAPFILEHEADER bfh;  // bmp文件头
 BITMAPINFOHEADER bih;  // bmp头信息
 RGBQUAD rq[256];   // 调色板
 int nAdjust; // 用于字节对齐
 int nComponent = 0;

 // 声明解压缩对象及错误信息管理器
 struct jpeg_decompress_struct cinfo;
 struct jpeg_error_mgr jerr;

 cinfo.err = jpeg_std_error(&jerr);
 jpeg_create_decompress(&cinfo);
 jpeg_stdio_src(&cinfo,(char*) indata,nSize);
 jpeg_read_header(&cinfo, TRUE);
 nAdjust = cinfo.image_width*cinfo.num_components%4;
 if (nAdjust) nAdjust = 4-nAdjust;
 data = new BYTE[size]; 
 jpeg_start_decompress(&cinfo);
 JSAMPROW row_pointer[1];
 while (cinfo.output_scanline < cinfo.output_height)
 {
  row_pointer[0] = &data[(cinfo.output_height - cinfo.output_scanline-1)*(cinfo.image_width*cinfo.num_components+nAdjust)];
  jpeg_read_scanlines(&cinfo,row_pointer ,
     1);
 }
 jpeg_finish_decompress(&cinfo);
 jpeg_destroy_decompress(&cinfo);

 for (int j=0;j  for (int i = 0;i  {
   BYTE red = data[j*(cinfo.image_width*cinfo.num_components+nAdjust)+i*3];
   data[j*(cinfo.image_width*cinfo.num_components+nAdjust)+i*3] = data[j*(cinfo.image_width*cinfo.num_components+nAdjust)+i*3+2];
   data[j*(cinfo.image_width*cinfo.num_components+nAdjust)+i*3+2] = red;
  }
 
}

转载于:https://www.cnblogs.com/xiaoluyiqing/archive/2012/03/14/2396206.html

你可能感兴趣的:(对bmp文件内存压缩 与 解压缩)