directshow 之 视频位图24位32位存储

HRESULT CVideoSource::SaveToBmpFile(char* szFile)
{
 HRESULT hr =NULL;
 
 TRY
 {
  BYTE *buffer = NULL;
  hr = m_pWindowsLessControl->GetCurrentImage(&buffer);
  if (!SUCCEEDED(hr))
  {    
   return hr;
  }
 
  BITMAPFILEHEADER hdr;    
  BITMAPINFOHEADER *lpbi = (BITMAPINFOHEADER *)buffer;
  //颜色空间转换,如果为32位位图,转换为24
  bool isSize24 = false;
  DWORD dwSize24 = 0;
  DWORD dwSize32 = lpbi->biSizeImage;
  dwSize24 = lpbi->biBitCount==32?(dwSize32*3)/4:dwSize32;   //RGB32与RGB24的像素点空间只差了一个字节
  BYTE* pImg24 = new BYTE[dwSize24]; //存放RGB24存储空间
  BYTE* pImg24Temp = pImg24;
  BYTE* pImg32 = buffer + sizeof(BITMAPINFOHEADER);
  if (lpbi->biBitCount==32)
  {
   isSize24 = true;
   for (DWORD index=0; index<dwSize32/4; index++)
   {
    unsigned char r = *(pImg32++);
    unsigned char g = *(pImg32++);
    unsigned char b = *(pImg32++);
    pImg32++;    //跳过alpha分量,实现转换
    *(pImg24++) = r;
    *(pImg24++) = g;
    *(pImg24++) = b;   //赋值
   }

   lpbi->biBitCount = 24;
  }
  int nColors = 1 << lpbi->biBitCount;
  if (nColors > 256)        
   nColors = 0;     
  hdr.bfType = ((WORD)('M' << 8) | 'B'); // always is "BM"    
  hdr.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nColors * sizeof(RGBQUAD) + lpbi->biSizeImage;   
  hdr.bfReserved1 = 0;    
  hdr.bfReserved2 = 0;    
  hdr.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER) + lpbi->biSize + nColors * sizeof(RGBQUAD));

  //将图像调整为640*480,非这种格式,强制调整
  
  

  FILE *bmpFile = NULL;    
  bmpFile = fopen((LPCTSTR)szFile, "wb");//inFile为文件名    
  if (bmpFile != NULL)    
  {        
   fwrite(&hdr, 1, sizeof(BITMAPFILEHEADER), bmpFile);//位图文件头        
   fwrite(buffer, 1, sizeof(BITMAPINFOHEADER), bmpFile);//位图信息头
   if (!isSize24)
   {
    fwrite(buffer+sizeof(BITMAPINFOHEADER), 1, nColors * sizeof(RGBQUAD), bmpFile);//颜色表        
    fwrite(buffer+sizeof(BITMAPINFOHEADER)+nColors * sizeof(RGBQUAD), 1, lpbi->biSizeImage, bmpFile);//位图数据    
   }
   else
   {
    fwrite(pImg24Temp,1,dwSize24,bmpFile);
    //fwrite(pImgNormal,1,700*500*3,bmpFile);
   }
  }    
  fclose(bmpFile);  
  
  
  
  CoTaskMemFree(buffer);//释放资源
  delete pImg24Temp;
  return hr;
 }
 CATCH (CMemoryException, e)
 {
  
 }
 END_CATCH
 }

转载于:https://www.cnblogs.com/cngreatwall/archive/2011/09/02/2163550.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值