jpeglib的使用

来源:互联网 发布:吉他包淘宝 编辑:程序博客网 时间:2024/05/20 09:06
  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 0
原创粉丝点击