织梦CMS - 轻松建站从此开始!

罗索

图片在jpg 格式与bmp24格式之间的转换

jackyhwei 发布于 2011-05-17 16:10 点击:次 
首先jpeg.lib必须有,还要包含jconfig.h,jmorecfg.h,jpeglib.h。提示:转JPG时,数据要求4字节对齐,数据流用如下函数补齐。
TAG:

首先jpeg.lib必须有,还要包含jconfig.h,jmorecfg.h,jpeglib.h。

#include "jpeglib.h"

==================

提示:

转JPG时,数据要求4字节对齐,数据流用如下函数补齐。

  1. //in: nBits = Width * nDepthBits 
  2. int ByteAlign( int nBits ) 
  3.     int nAlignBytes = ( nBits + 31 ) / 32 * 4; 
  4.     return nAlignBytes; 

/*encode BMP24 into JPEG


    use jpeg-lib 8.0
    parameter:
    BYTE *pRGB24In    [in]        RGB24 的BMP 数据,不包含头信息;
    int nWidth             [in]        BMP 图像的宽度(像素);
    int nHight              [in]        BMP 图像的高度(像素);
    int nLineBytes       [in]        BMP 图像的每行的字节数(必须4字节对齐,如没有对齐需要补齐);
    BYTE *pJpgOut      [out]     转换成JPG的数据

    return:                     压缩为JPG后的数据大小

    Note:                        pJpgOut 会在函数内部进行内存分配,使用此函数后需要释放内存
                                     nCompressed   0---100之间,表示图像压缩率,建议75
*/

  1. int JpegFromBMP24(BYTE *pRGB24In, int nWidth, int nHeight, int nLineBytes, LPBYTE &pJpgOut); 
  2.     struct jpeg_compress_struct jcs; 
  3.     struct jpeg_error_mgr jsrcerr; 
  4.     
  5.     if (pRGB24In == NULL) 
  6.         return 0; 
  7.     if (nWidth == 0) 
  8.         return 0; 
  9.     if (nHeight == 0) 
  10.         return 0; 
  11.     
  12.     /*  initialize the JPEG compression object. */nCompressed 
  13.     jcs.err = jpeg_std_error(&jsrcerr); 
  14.     jpeg_create_compress(&jcs); 
  15.     
  16.     unsigned long  lsize = 0; 
  17.     BYTE*  lpJpgData = NULL; 
  18.     jpeg_mem_dest(&jcs, &lpJpgData, &lsize); 
  19.     
  20.     jcs.image_width = nWidth;     /* image widthPix and height, in pixels */ 
  21.     jcs.image_height = nHeight; 
  22.     jcs.input_components = 3;        /* # of color components per pixel */ 
  23.     jcs.in_color_space = JCS_RGB;     /* colorspace of input image */ 
  24.     
  25.     jpeg_set_defaults(&jcs); 
  26.  
  27.     int nCompressed = 71; /*[0-100], if 0, quality is best, but size is biggest*/ 
  28.  
  29.     jpeg_set_quality(&jcs, nCompressed, TRUE /* limit to baseline-JPEG values */); 
  30.     jpeg_start_compress(&jcs, TRUE); 
  31.     
  32.     while (jcs.next_scanline < jcs.image_height) 
  33.     { 
  34.         LPBYTE outRow; 
  35.         int nRow = jcs.image_height - jcs.next_scanline - 1;//last to header 
  36.         int offset = nRow * nLineBytes; 
  37.         outRow = pRGB24In + offset; 
  38.         for(int index=0; index < nLineBytes/3; index++) 
  39.         { 
  40.             int nRGBbit = index * 3; 
  41.             
  42.             BYTE tmp; 
  43.             tmp = *(outRow + nRGBbit + 0); 
  44.             *(outRow + nRGBbit + 0) = *(outRow + nRGBbit + 2);//r = g 
  45.             *(outRow + nRGBbit + 2) = tmp;//g = r 
  46.         } 
  47.         (void) jpeg_write_scanlines(&jcs, &outRow, 1); 
  48.     } 
  49.     
  50.     jpeg_finish_compress(&jcs); 
  51.     jpeg_destroy_compress(&jcs); 
  52.     
  53.     if(pJpgOut == NULL) 
  54.     { 
  55.         pJpgOut = (LPBYTE)malloc(lsize); 
  56.         memset(pJpgOut, 0, lsize); 
  57.     } 
  58.     memcpy(pJpgOut, lpJpgData, lsize); 
  59.     
  60.     if( lpJpgData != NULL ) 
  61.         free( lpJpgData ); 
  62.  
  63.     return lsize; 
  64.  
  65.  
  66. BOOL  JpegToBMP24( BYTE *pJpgIn, int nJPGDataSize, LPBYTE& pRGB24Out
  67. int&  nWidth, int& nHight, int& nLineBytes ) 
  68.     if (pJpgIn ==NULL) 
  69.         return FALSE; 
  70.     
  71.     struct jpeg_decompress_struct jds; 
  72.     struct jpeg_error_mgr jdsterr; 
  73.  
  74.     jds.err = jpeg_std_error(&jdsterr); 
  75.     jpeg_create_decompress(&jds); 
  76.     jpeg_mem_src(&jds, pJpgIn, nJPGDataSize); 
  77.  
  78.     (void) jpeg_read_header(&jds, TRUE); 
  79.     jpeg_start_decompress(&jds); 
  80.  
  81.     nWidth = jds.output_width; 
  82.     nHight = jds.output_height; 
  83.     nLineBytes = ByteAlign( nWidth * 24 ); 
  84.  
  85.     JSAMPARRAY buffer;        /* Output row buffer */ 
  86.     int row_width; 
  87.  
  88.     /* SAMPLEs per row in output buffer */ 
  89.     row_width = jds.output_width * jds.output_components; 
  90.  
  91.     buffer = (*jds.mem->alloc_sarray) 
  92.         ((j_common_ptr) &jds, JPOOL_IMAGE, nLineBytes, 1); 
  93.  
  94.     if(pRGB24Out == NULL) 
  95.     { 
  96.         int dataSize = (nLineBytes) * (nHight); 
  97.         pRGB24Out = (LPBYTE)malloc(dataSize); 
  98.         memset(pRGB24Out, 0, dataSize); 
  99.     } 
  100.  
  101.     while (jds.output_scanline < jds.output_height) 
  102.     { 
  103.         (void) jpeg_read_scanlines(&jds, buffer, 1); 
  104.         if (jds.out_color_components==3) 
  105.         {    
  106.             JPGputRGBScanline(buffer[0], nWidth, pRGB24Out
  107. , jds.output_height-jds.output_scanline); 
  108.         } 
  109.     } 
  110.  
  111.     jpeg_finish_decompress(&jds); 
  112.     jpeg_destroy_decompress(&jds); 
  113.  
  114.     return TRUE; 

exsample:

  1. void func(CString   strFileName) 
  2. /*BMP data to JPG */ 
  3.     HBITMAP hBitmap = NULL; 
  4.     hBitmap = ( HBITMAP )LoadImage( NULL, strFileName, IMAGE_BITMAP, 0, 0, 
  5. LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE | LR_SHARED ); 
  6.     CBitmap pRGB16Bmp; 
  7.  
  8.     pRGB16Bmp.Detach(); 
  9.     pRGB16Bmp.Attach( hBitmap ); 
  10.  
  11.     BITMAP *pBitMap = new BITMAP; 
  12.     pRGB16Bmp.GetBitmap( pBitMap ); 
  13.  
  14.     int nWidth  = pBitMap->bmWidth; 
  15.     int nSrcColorBits = (int)(pBitMap->bmBitsPixel); 
  16.     int nSrcBitsPiexel = 0; 
  17.     switch( nSrcColorBits) 
  18.     { 
  19.     case 16: 
  20.         nSrcBitsPiexel = 2; 
  21.         break
  22.     case 24: 
  23.         nSrcBitsPiexel = 3; 
  24.         break;    
  25.     case 32: 
  26.         nSrcBitsPiexel = 4; 
  27.         break
  28.     default 
  29.         nSrcBitsPiexel = 3; 
  30.         break
  31.    } 
  32.     int nSrcLineBytes = ByteAlign( nWidth * nSrcBitsPiexel * 8 );//字节对齐 
  33.     BYTE *pJpgOut = NULL; 
  34.     int nSize = JpegFromBMP24( pBitMap->bmBits, nWidth, nHeight, nSrcLineBytes, pJpgOut); 
  35.     FILE *hFile = fopen("C:\\test.jpg","wb"); 
  36.     fwrite(pJpgOut,sizeof(unsigned char),nSize,hFile); 
  37.     fclose(hFile); 
  38.  
  39. /*JPG data to BMP*/ 
  40.     if( nSrcColorBits == 16 || nSrcColorBits == 32 ) 
  41.     { 
  42.         nDstBitsPiexel = 3; 
  43.     } 
  44.     else if( nSrcColorBits == 24 ) 
  45.     { 
  46.         nDstBitsPiexel = 2; 
  47.     } 
  48.     else //default 
  49.     { 
  50.         nDstBitsPiexel = 2; 
  51.     } 
  52.  
  53.     int nDstLineBytes = ByteAlign( nWidth*nDstBitsPiexel*8 ); 
  54.     JpegToBMP24( pJpgOut, nSize, pRGB24Out, nWidth, nHeight, nDstLineBytes ); 
  55.  
  56.  
  57.     //SavetoBmp(...) 
  58.     //parament : pRGB24Out, nWidth, nHeight, "C:\\test.bmp",....... 
(liln0530)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201105/11816.html]
本文出处:CSDN博客 作者:liln0530
顶一下
(4)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容