directshow 之 视频位图24位32位存储
2011-09-02 11:42
363 查看
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
}
{
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
}
相关文章推荐
- 把24位和32位BMP位图转为灰色bmp位图
- directshow 之 视频文件位图提取
- 将24位及32位图像数据保存到位图中
- 32位位图到24位位图的转换
- 实现1位,4位,8位,24位BMP位图的互相转换的方法,32位转24位
- 24位 位图与32位 位图差异解析
- libjpeg库编码图片为jpg(ffmpeg解码视频存储图片:RGB格式位图压缩为jpg格式图片)
- libjpeg库编码图片为jpg(ffmpeg解码视频存储图片:RGB格式位图压缩为jpg格式图片)
- BMP位图32位转为24位深度
- 32位位图与24位位图互相转换
- 32位位图到24位位图的转换
- directshow 之 视频控制
- 视频采集卡 DirectShow VFW V4L 私有SDK
- 基于DirectShow的MPEG-4视频传输系统的研究与实现
- 【MFC两种视频图像採集方法】DirectShow与Opencv
- CYQ.Data 轻量数据层之路 使用篇-MProc 存储过程与SQL 视频[最后一集] H (二十八)
- DirectShow 视频录制保存为avi文件
- DirectShow控制台输出和保存视频设备名称
- 提取DirectShow中视频采集的数据
- 利用DirectShow实现对视频文件H264编码与解码基类 2