使用jpeglib,实现jpg和bmp互转

使用jpeglib,实现jpg和bmp互转
2011年10月08日
  到http://www.ijg.org下载库文件
  1、头文件
  #include "afx.h"
  #include
  extern "C" {
  #include "jpeglib.h"
  }
  #pragma comment( lib , "libjpeg.lib" )
  int ByteAlign( int nBits )
  {
  int nAlignBytes = ( nBits + 31 ) / 32 * 4;
  return nAlignBytes;
  }
  extern "C" int __declspec(dllexport) __stdcall JpegFromBMP24(CString &strBmpName,CString &strJpgName,int iCompressed=80); //int nWidth, int nHeight, int nLineBytes, LPBYTE &pJpgOut);
  extern "C" bool __declspec(dllexport) __stdcall JpegToBMP(char *input, char *output);
  2、cpp文件
  #include "JpgFile.h"
  int setBmpHeader(BITMAPINFOHEADER &BMIH, BITMAPFILEHEADER &bmfh,struct jpeg_decompress_struct *info)
  {
  memset(&BMIH, 0, sizeof(BITMAPINFOHEADER)) ;
  BMIH.biSize = sizeof(BITMAPINFOHEADER) ;
  BMIH.biBitCount = 24 ;
  BMIH.biPlanes = 1 ;
  BMIH.biCompression = BI_RGB;
  BMIH.biWidth =info->output_width;
  BMIH.biHeight = 0-info->output_height;//高度是负数。表示bmp的反转
  BMIH.biSizeImage = ((((BMIH.biWidth * BMIH.biBitCount) + 31) & ~31) >> 3) * info->output_height;//BMIH.biWidth*cinfo.output_height*3;//((((BMIH.biWidth * BMIH.biBitCount) + 31) & ~31) >> 3) * BMIH.biHeight;
  int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize;
  LONG lImageSize = BMIH.biSizeImage ;
  LONG lFileSize = nBitsOffset + lImageSize ;
  bmfh.bfType = 'B'+('M'bmWidth;//位图宽度
  int nHeight=pBitMap->bmHeight;//位图高度
  int nSrcColorBits = (int)(pBitMap->bmBitsPixel);//每个颜色多少位表示
  int nSrcBitsPiexel = 0;
  switch( nSrcColorBits)
  {
  case 16:
  nSrcBitsPiexel = 2;
  break;
  case 24:
  nSrcBitsPiexel = 3; //每个像素3个字节
  break;
  case 32:
  nSrcBitsPiexel = 4;
  break;
  default:
  nSrcBitsPiexel = 3;
  break;
  }
  int nLineBytes = ByteAlign( nWidth * nSrcBitsPiexel * 8 );//字节对齐 每行多少字节
  struct jpeg_compress_struct jcs;//申请压缩结构
  struct jpeg_error_mgr jsrcerr;
  BYTE *pRGB24In =(BYTE*)pBitMap->bmBits;//位图的数据内容
  if (nWidth == 0) return 0;
  if (nHeight == 0) return 0;
  /* initialize the JPEG compression object. */
  jcs.err = jpeg_std_error(&jsrcerr);
  jpeg_create_compress(&jcs);//建立压缩
  unsigned long lsize = 0;
  BYTE* lpJpgData = NULL;
  BYTE* pJpgOut=NULL;
  jpeg_mem_dest(&jcs, &lpJpgData, &lsize); //初始化jcs
  jcs.image_width = nWidth; /* image widthPix and height, in pixels */
  jcs.image_height = nHeight;
  jcs.input_components = 3; /* # of color components per pixel */
  jcs.in_color_space = JCS_RGB; /* colorspace of input image */
  jpeg_set_defaults(&jcs);
  int nCompressed = iCompressed; /*[0-100], if 0, quality is best, but size is biggest*/
  jpeg_set_quality(&jcs, nCompressed, TRUE /* limit to baseline-JPEG values */);
  jpeg_start_compress(&jcs, TRUE);//开始压缩
  while (jcs.next_scanline BMP header */
  fwrite(&bmfh, 1, sizeof(BITMAPFILEHEADER), out);
  fwrite(&BMIH, 1, sizeof(BITMAPINFOHEADER), out);
  /* SAMPLEs per row in output buffer */
  row_width = cinfo.output_width * cinfo.output_components;
  /* calculate number of padding */
  num_padding = cinfo.output_width%4;
  /* set buffer */
  buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_width, 1);
  while (cinfo.output_scanline < cinfo.output_height)
  {
  jpeg_read_scanlines(&cinfo, buffer, 1);
  convertColor((char *)buffer[0], cinfo.output_width);
  fwrite(buffer[0], 1, row_width, out);
  fwrite("0", 1, num_padding, out);
  }
  jpeg_finish_decompress(&cinfo);
  jpeg_destroy_decompress(&cinfo);
  fclose(in);
  fclose(out);
  return TRUE;
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值