登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

面包会有的

... ...

 
 
 

日志

 
 

DirectShow 中保存图像(图像倒置了!)  

2011-03-30 03:44:43|  分类: VC++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

SaveBMP ( BYTE* Buffer, int width, int height, long paddedsize, const char* bmpfile )
{
// declare bmp structures 
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER info;
// and initialize them to zero
memset ( &bmfh, 0, sizeof (BITMAPFILEHEADER ) );
memset ( &info, 0, sizeof (BITMAPINFOHEADER ) );
// fill the fileheader with data
bmfh.bfType = 0x4d42; // 0x4d42 = 'BM'
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + paddedsize;
bmfh.bfOffBits = 0x36; // number of bytes to start of bitmap bits
// fill the infoheader
info.biSize = sizeof(BITMAPINFOHEADER);
info.biWidth = width;
info.biHeight = height;
info.biPlanes = 1; // we only have one bitplane
info.biBitCount = 24; // RGB mode is 24 bits
info.biCompression = BI_RGB; 
info.biSizeImage = 0; // can be 0 for 24 bit images
info.biXPelsPerMeter = 0x0ec4; // paint and PSP use this values
info.biYPelsPerMeter = 0x0ec4; 
info.biClrUsed = 0; // we are in RGB mode and have no palette
info.biClrImportant = 0; // all colors are important
// now we open the file to write to
HANDLE file = CreateFile ( bmpfile , GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
if ( file == NULL )
{
CloseHandle ( file );
return false;
}
// write file header
unsigned long bwritten;
if ( WriteFile ( file, &bmfh, sizeof ( BITMAPFILEHEADER ), &bwritten, NULL ) == false )

CloseHandle ( file );
return false;
}
// write infoheader
if ( WriteFile ( file, &info, sizeof ( BITMAPINFOHEADER ), &bwritten, NULL ) == false )

CloseHandle ( file );
return false;
}
// write image data
if ( WriteFile ( file, Buffer, paddedsize, &bwritten, NULL ) == false )

CloseHandle ( file );
return false;
}
// and close file
CloseHandle ( file );
return true;
}

 

GrabFrame()
{
long lOut=-1;
if(m_pWC ) 
  {
  BYTE* lpCurrImage = NULL;
  // Read the current video frame into a byte buffer. The information
  // will be returned in a packed Windows DIB and will be allocated
  // by the VMR.
  if(m_pWC->GetCurrentImage(&lpCurrImage) == S_OK)
  {
LPBITMAPINFOHEADER pdib = (LPBITMAPINFOHEADER) lpCurrImage;
if(m_pFrame==NULL || (pdib->biHeight * pdib->biWidth * 3) !=m_nFramelen )
{
if(m_pFrame!=NULL)
delete []m_pFrame;
m_nFramelen=pdib->biHeight * pdib->biWidth * 3;
m_pFrame=new BYTE [pdib->biHeight * pdib->biWidth * 3] ;
}
if(pdib->biBitCount ==32) 
{
DWORD dwSize=0, dwWritten=0;
BYTE *pTemp32;
pTemp32=lpCurrImage + sizeof(BITMAPINFOHEADER);
//change from 32 to 24 bit /pixel
this->Convert24Image(pTemp32, m_pFrame, pdib->biSizeImage);
}
CoTaskMemFree(lpCurrImage); //free the image 
}
else
{
return lOut;
}
}
else
{
return lOut;
}
   
  return lOut=m_nFramelen;
}
Convert24Image(BYTE *p32Img, BYTE *p24Img,DWORD dwSize32)
{
if(p32Img != NULL && p24Img != NULL && dwSize32>0)
{
DWORD dwSize24;
dwSize24=(dwSize32 * 3)/4;
BYTE *pTemp,*ptr;
//pTemp=p32Img + sizeof(BITMAPINFOHEADER); ;
pTemp=p32Img;
ptr=p24Img + dwSize24-1 ;
int ival=0;
for (DWORD index = 0; index < dwSize32/4 ; index++)
{
unsigned char r = *(pTemp++);
unsigned char g = *(pTemp++);
unsigned char b = *(pTemp++);
(pTemp++);//skip alpha
*(ptr--) = b;
*(ptr--) = g;
*(ptr--) = r;
}
}
else
{
return false;
}
return true;
}
GetFrame(BYTE **pFrame)
{
if(m_pFrame && m_nFramelen)
{
*pFrame=m_pFrame;
}
return m_nFramelen;
}
GrabFrame接口是获取保存保存当前图片帧的缓存大小
GetFrame获取缓存指针
Convert24Image接口将32位转换成24位


确实是上下倒置了
  评论这张
 
阅读(1184)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018