位图的读取,PNG转化为BMP

#include <imaging.h>
#include <initguid.h>
#include <imgguids.h>

 

unsigned char* LoadBMP(char* file)
{
 FILE *fp = fopen(file,"rb");
 if(!fp)
 {
  MessageBox(g_hWnd, _T("Open file error"), _T("error"), MB_OK);
 }

 BITMAPFILEHEADER hdr;

 fread(&hdr,1,sizeof(hdr),fp);

 if(!(((hdr.bfType & 0xff) == 'B') &&  ((hdr.bfType >> 8) == 'M')))
 {
  MessageBox(g_hWnd, _T("read file error"), _T("error"), MB_OK);
  fclose(fp);
 }

 BITMAPINFOHEADER bih;
 fread(&bih,1,sizeof(bih),fp);
 if(bih.biBitCount != 32)
 {
  MessageBox(g_hWnd, _T("it is not 32 bits color"), _T("error"), MB_OK);
 }
 unsigned char *pBuf = new unsigned char[bih.biWidth * bih.biHeight*4];
 fread(pBuf,bih.biWidth * bih.biHeight*4,sizeof(unsigned char),fp);
 fclose(fp);


 if(!pBuf)
 {
  MessageBox(g_hWnd, _T("new buffer error"), _T("error"), MB_OK);
 }

 for(int i = 0; i < bih.biHeight ; i ++)
 {


  for(int j = 0 ;  j <bih.biWidth ; j ++)
  {

   int temp=(i *  bih.biWidth   + j )* 4;


   pBuf[temp ] = pBuf[temp ]*pBuf[temp +3]>>8 ;
   pBuf[temp+1] = pBuf[temp + 1 ]*pBuf[temp +3]>>8 ;
   pBuf[temp + 2]= pBuf[temp + 2 ]*pBuf[temp +3]>>8 ;


  }

 }

 int rowSize = bih.biWidth * 4;

 int bound =bih.biHeight/2;

 byte *temp = new byte[rowSize];

 for(int i=0; i<bound; i++)

 {

  memcpy(temp, pBuf+i*rowSize, rowSize);

  memcpy(pBuf+i*rowSize, pBuf+(bih.biHeight-i-1)*rowSize, rowSize);

  memcpy(pBuf+(bih.biHeight-i-1)*rowSize, temp, rowSize);

 }

 delete []temp;
 return pBuf;
}


HBITMAP LoadPngImage2 (/*HDC hdc,*/ LPCTSTR filename)  
{  
 IImagingFactory* pImageFactory = 0;  
 IImage* pImage = 0;  
 ImageInfo imageInfo;  
 CoInitializeEx(0, COINIT_MULTITHREADED);  
 HBITMAP hBitmap = 0;  
 LPBYTE lpByte;  
 if (SUCCEEDED(CoCreateInstance(CLSID_ImagingFactory, 0, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void**)&pImageFactory)))  
 {  
  if (SUCCEEDED(pImageFactory->CreateImageFromFile(filename, &pImage))&& SUCCEEDED(pImage->GetImageInfo(&imageInfo)))  
  {  
   /*HDC bmpDC = CreateCompatibleDC(hdc); */ 
   //LPBYTE lpByte;  
   BITMAPINFO *pbinfo ;  
   pbinfo = (BITMAPINFO *)calloc(1, sizeof(BITMAPINFO) + 4 * sizeof(INT)) ;  
   if(!pbinfo)  
    return FALSE ;  
   pbinfo->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);  
   pbinfo->bmiHeader.biWidth = imageInfo.Width ;  
   pbinfo->bmiHeader.biHeight = imageInfo.Height ;  
   pbinfo->bmiHeader.biPlanes = 1;  
   pbinfo->bmiHeader.biBitCount = 32;  
   pbinfo->bmiHeader.biCompression = BI_ALPHABITFIELDS;  
   pbinfo->bmiHeader.biSizeImage = 0 ;  
   pbinfo->bmiHeader.biXPelsPerMeter = 11811;  
   pbinfo->bmiHeader.biYPelsPerMeter = 11811;  
   pbinfo->bmiHeader.biClrUsed = 0;  
   pbinfo->bmiHeader.biClrImportant = 0;  
   int *pMask = (int*)&(pbinfo->bmiColors[0]) ;  
   *pMask++ = 0x00FF0000 ;  
   *pMask++ = 0x0000FF00 ;  
   *pMask++ = 0x000000FF ;  
   *pMask++ = 0xFF000000 ;  
   hBitmap = CreateDIBSection(NULL, pbinfo, DIB_RGB_COLORS, (void **)&lpByte, NULL, 0) ;  
   free(pbinfo) ;  
   if(!hBitmap || !lpByte)  
    return FALSE ;  
   RECT rect = {0, 0, imageInfo.Width, imageInfo.Height};  
   IBitmapImage *pBitmapImage;  
   BitmapData bitmapData;  
   bitmapData.Width = imageInfo.Width;  
   bitmapData.Height = imageInfo.Height;  
   bitmapData.PixelFormat = imageInfo.PixelFormat;  
   pBitmapImage = NULL;  
   pImageFactory->CreateBitmapFromImage(pImage, imageInfo.Width, imageInfo.Height, PIXFMT_32BPP_ARGB,  
    InterpolationHintDefault, &pBitmapImage);  
   pBitmapImage->LockBits(&rect, ImageLockModeRead,PIXFMT_32BPP_ARGB, &bitmapData);  
   //transferring the pixels  
   memcpy(lpByte, bitmapData.Scan0, imageInfo.Width * imageInfo.Height * 4);  
   pBitmapImage->UnlockBits(&bitmapData);  
   pBitmapImage->Release();  
   pImage->Release();  
   /*DeleteDC(bmpDC); */ 
  }  
  pImageFactory->Release();  
 }    
 CoUninitialize();  
 //ProcessThePixelsWithAlphaChannel Here       
 // vertical flip and ProcessThePixelsWithAlphaChannel here  
 for (UINT y=0; y<imageInfo.Height/2; y++)  
 {  
  BYTE * pPixel = (BYTE *) lpByte + imageInfo.Width * 4 * y;  
  BYTE * pDstPixel = (BYTE*) lpByte + imageInfo.Width * 4 * (imageInfo.Height-y-1);   
  for (UINT x=0; x<imageInfo.Width; x++)  
  {  
   pPixel[0] = pPixel[0] * pPixel[3] / 255;  
   pPixel[1] = pPixel[1] * pPixel[3] / 255;  
   pPixel[2] = pPixel[2] * pPixel[3] / 255;  
   pDstPixel[0] = pDstPixel[0] * pDstPixel[3] / 255;  
   pDstPixel[1] = pDstPixel[1] * pDstPixel[3] / 255;  
   pDstPixel[2] = pDstPixel[2] * pDstPixel[3] / 255;  
   INT* pOrigin = (INT*)pPixel;  
   INT* pDst    = (INT*)pDstPixel;  
   INT temp = *pOrigin;  
   *pOrigin = *pDst;  
   *pDst = temp;  
   pPixel += 4;  
   pDstPixel += 4;  
  }  
 }  
 return hBitmap;  
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值