jpeglib的使用

  1. BOOL BmpToJpg( int nWidth, int nHeight, int nPixelBytes, BYTE* byBmpData, BYTE** byJpgData, unsigned long* nSize )  
  2. {  
  3.     BOOL bResult = FALSE;  
  4.       
  5.     jpeg_compress_struct jCompress;  
  6.     jpeg_error_mgr       jErrorMgr;  
  7.       
  8.     jCompress.err = jpeg_std_error( &jErrorMgr );  
  9.       
  10.     jpeg_create_compress( &jCompress );  
  11.       
  12.     jpeg_mem_dest( &jCompress, byJpgData, nSize );  
  13.       
  14.     jCompress.image_width      = nWidth;  
  15.     jCompress.image_height     = nHeight;  
  16.     jCompress.input_components = nPixelBytes;  
  17.     jCompress.in_color_space   = JCS_UNKNOWN;  
  18.       
  19.     jpeg_set_defaults( &jCompress );  
  20.     jpeg_set_quality ( &jCompress, 70, true );  
  21.       
  22.     jpeg_start_compress( &jCompress, true );  
  23.       
  24.     int nLineWidth = nWidth * nPixelBytes;  
  25.       
  26.     for ( int i = 0; i < nHeight; ++i )  
  27.     {  
  28.         BYTE* lpJpgBits = byBmpData + ( nHeight - i - 1 ) * nLineWidth;  
  29.           
  30.         JSAMPROW row_pointer = lpJpgBits;  
  31.           
  32.         jpeg_write_scanlines( &jCompress, &row_pointer, 1 );  
  33.     }  
  34.       
  35.     jpeg_finish_compress( &jCompress );  
  36.     jpeg_destroy_compress( &jCompress );  
  37.       
  38.     return bResult;  
  39. }  
  40.   
  41. BOOL JpgToBmp( BYTE* byJpgData, BYTE* byBmpData, unsigned long* nSize )  
  42. {  
  43.     BOOL bResult = FALSE;  
  44.       
  45.     jpeg_decompress_struct jDecompress;  
  46.     jpeg_error_mgr         jErrorMgr;  
  47.       
  48.     jDecompress.err = jpeg_std_error( &jErrorMgr );  
  49.       
  50.     jpeg_create_decompress( &jDecompress );  
  51.       
  52.     jpeg_mem_src( &jDecompress, byJpgData, *nSize );  
  53.       
  54.     jpeg_read_header( &jDecompress, true );  
  55.       
  56.     jpeg_start_decompress( &jDecompress );  
  57.       
  58.     int nHeight    = jDecompress.output_height;  
  59.     int nLineWidth = jDecompress.output_components * jDecompress.output_width;  
  60.       
  61.     for ( int i = 0; i < nHeight; ++i )  
  62.     {  
  63.         BYTE* lpBmBits = byBmpData + ( nHeight - i - 1 ) * nLineWidth;  
  64.           
  65.         jpeg_read_scanlines( &jDecompress, &lpBmBits, 1 );  
  66.     }  
  67.       
  68.     jpeg_finish_decompress( &jDecompress );  
  69.     jpeg_destroy_decompress( &jDecompress );  
  70.   
  71.     *nSize = nLineWidth * nHeight;  
  72.   
  73.     return bResult;  
  74. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值