加载png图片并转换为DIB HBITMAP

原文地址::http://blog.csdn.net/tdgx2004/article/details/6444022

 

 

  1. static void pngReaderCallback(png_structp png_ptr, png_bytep data, png_size_t length)  
  2. {  
  3.     ImageSource* isource = (ImageSource*)png_get_io_ptr(png_ptr);  
  4.   
  5.     if(isource->offset + length <= isource->size)  
  6.     {  
  7.        memcpy(data, (unsigned char *)isource->data + isource->offset, length);  
  8.        isource->offset += length;  
  9.     }  
  10.     else  
  11.     {  
  12.         png_error(png_ptr,"pngReaderCallback failed");  
  13.     }  
  14. }  
  15.   
  16. static HBITMAP LoadPNGFromMemory(void * lpData, UINT size, HDC hdc, BYTE **m_ppBits, int &width, int &height)  
  17. {  
  18.   
  19.     HBITMAP hBitBmp=NULL;  
  20.   
  21.     if (lpData==NULL || size==0) return NULL;  
  22.   
  23.   
  24.     png_structp png_ptr;  
  25.     png_infop   info_ptr;  
  26.   
  27.     /* initialize stuff */  
  28.     png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);  
  29.       
  30.     if (!png_ptr) return NULL;  
  31.   
  32.   
  33.     info_ptr = png_create_info_struct(png_ptr);  
  34.   
  35.     if (!info_ptr)  return NULL;  
  36.   
  37.     if (setjmp(png_jmpbuf(png_ptr)))  
  38.     {  
  39.         png_destroy_read_struct(&png_ptr, &info_ptr, 0);  
  40.         return false;  
  41.     }  
  42.   
  43.   
  44.     ImageSource imgsource;  
  45.     imgsource.data = lpData;  
  46.     imgsource.size = size;  
  47.     imgsource.offset = 0;  
  48.   
  49.     png_set_read_fn(png_ptr, &imgsource, pngReaderCallback);  
  50.   
  51.   
  52.     png_read_info(png_ptr, info_ptr);  
  53.   
  54.     unsigned long x,y,i;  
  55.     BYTE r,g,b,a;  
  56.   
  57.     png_bytep * row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * info_ptr->height);  
  58.   
  59.     if (row_pointers)  
  60.     {  
  61.         for (y=0; y<info_ptr->height; y++)  
  62.             row_pointers[y] = (png_byte*) malloc(info_ptr->rowbytes);  
  63.   
  64.         png_read_image(png_ptr, row_pointers);  
  65.   
  66.   
  67.         BITMAPINFOHEADER m_bmpHeader;  
  68.         memset(&m_bmpHeader,NULL,sizeof(m_bmpHeader));  
  69.         m_bmpHeader.biSize = sizeof(m_bmpHeader);  
  70.         m_bmpHeader.biBitCount = 32;;  
  71.         m_bmpHeader.biCompression = BI_RGB;  
  72.         m_bmpHeader.biWidth = info_ptr->width;  
  73.         m_bmpHeader.biHeight = info_ptr->height;  
  74.         m_bmpHeader.biPlanes = 1;  
  75.   
  76.         BYTE *pBits;  
  77.   
  78.         hBitBmp = CreateDIBSection(hdc, (BITMAPINFO *)&m_bmpHeader, DIB_RGB_COLORS, (void **)&pBits, NULL, 0);  
  79.   
  80.         if (hBitBmp && (pBits))  
  81.         {  
  82.   
  83.             width=info_ptr->width;  
  84.             height=info_ptr->height;  
  85.   
  86.             for (y=0; y<info_ptr->height; y++)  
  87.             {  
  88.                 memcpy(pBits+(info_ptr->height-y-1)*info_ptr->rowbytes, row_pointers[y], info_ptr->rowbytes);  
  89.             }  
  90.   
  91.             *m_ppBits=pBits;  
  92.   
  93.   
  94.             for (y=0; y<info_ptr->height; y++)  
  95.             {  
  96.                 for (x=0; x<info_ptr->width; x++)  
  97.                 {  
  98.                     i=(info_ptr->height-1-y)*info_ptr->width*4+x*4;  
  99.   
  100.                     r=pBits[i+0];  
  101.                     g=pBits[i+1];  
  102.                     b=pBits[i+2];  
  103.                     a=pBits[i+3];  
  104.   
  105.                     // RGBA -> BGRA and to AlphaBend() Colors  
  106.                     pBits[i+0]=BYTE(b * a / 255);  
  107.                     pBits[i+1]=BYTE(g * a / 255);  
  108.                     pBits[i+2]=BYTE(r * a / 255);  
  109.                 }  
  110.   
  111.             }  
  112.   
  113.   
  114.         }  
  115.   
  116.         for (y=0; y<info_ptr->height; y++)  
  117.         {  
  118.             free(row_pointers[y]);  
  119.         }  
  120.   
  121.         free(row_pointers);  
  122.     }  
  123.   
  124.     // free memory  
  125.     png_destroy_read_struct(&png_ptr, &info_ptr, 0);  
  126.   
  127.     return hBitBmp;  
  128. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值