一个关于位图(BMP)的问题!

yyfzy yyfzy 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
最近我在写一个图片库,首先要支持的当然是最简单的位图格式图片,但是还是出现了不明白的地方。

我现在是用下面的代码来加载bmp文件,并使用GDI绘制出来,效果没有任何问题。

//Open file
ifstream inf(pszFile, ios::binary);
if(0 == inf.is_open())
return false;

//Load the file header
BITMAPFILEHEADER header;
memset(&header, 0, sizeof(header));
inf.read((char*)&header, sizeof(header));
if(header.bfType != 0x4D42)
return false;

//Load the image information header
BITMAPINFOHEADER infoheader;
memset(&infoheader, 0, sizeof(infoheader));
inf.read((char*)&infoheader, sizeof(infoheader));
m_iImageWidth = infoheader.biWidth;
m_iImageHeight = infoheader.biHeight;
m_iBitsPerPixel = infoheader.biBitCount;

//Calculate the image data size
int iLineByteCnt = (((m_iImageWidth*m_iBitsPerPixel) + 31) >> 5) << 2;
m_iImageDataSize = iLineByteCnt * m_iImageHeight;

//Load the image data to buffer
if(m_pImageData) delete []m_pImageData;
m_pImageData = new unsigned char[m_iImageDataSize];
inf.read((char*)m_pImageData, m_iImageDataSize);

inf.close();

但是内存里(也就是指针pImageData )的数据不是RGB格式,而是BGR格式!然后我用UltraEdit打开bmp文件,发现在文图文件里的确是BGR格式。

位图文件里的图片数据不应该是RGB格式吗?怎么成了BGR?
既然是BGR各式的数据,我是用
StretchDIBits(hdc, iLeft, iTop, iWidth, iHeight,
0, 0, m_iImageWidth, m_iImageHeight,
m_pImageData, &bmi, DIB_RGB_COLORS, SRCCOPY);
绘制出来的图像怎么没有错误呢?R与B的效果没有互换?

同时我在写Tga图片的解码,Tga图片格式的确是BGR,也就是说加载后要进行BGR->RGB的转化后再使用GDI绘制才能看到正确的效果,奇怪的时候也不需要这个转化,把BGR各式的数据送给GDI就可以看到正确的效果,为什么?

希望有人指点迷津!
...全文
评论 14
收藏 已收藏
分享
返回顶部
yyfzy
VC/MFC 16,551 成员 421,605 内容