如何理解bmp的bitmapinfoheader中的biBitCount和biClrUsed?

「已注销」 2010-06-20 03:28:07
biClrUsed,说是实际用到的颜色数目,如何理解?若为零,则与bibitcount有关?
我用了两个DIB类:(A),(B)来进行位图显示,结果,有一个类出现了错位,发现(A)类计算颜色表项数改为256,也能正常显示,但源图像为24位深的位图。若不改,颜色表项数改为0,但是有错位,并且每个像素的颜色也发生了变化?不明白为什么?
(B)类可以正常显示,主要是对biBitCount和biClrUsed进行了操作,代码如下:
/*************************************************************************
* DIBNumColors()
*
* Return Value:
* WORD - number of colors in the color table
*
* Description:
* This function calculates the number of colors in the DIB's color table
* by finding the bits per pixel for the DIB (whether Win3.0 or other-style
* DIB). If bits per pixel is 1: colors=2, if 4: colors=16, if 8: colors=256,
* if 24, no colors in color table.
************************************************************************/
WORD CDibImage::DIBNumColors() const
{
if (!m_pBMI)
return 0;

WORD wBitCount; // DIB bit count

/* The number of colors in the color table can be less than
* the number of bits per pixel allows for (i.e. lpbi->biClrUsed
* can be set to some value).
* If this is the case, return the appropriate value.
*/

DWORD dwClrUsed;

dwClrUsed = m_pBMI->bmiHeader.biClrUsed;
if (dwClrUsed != 0)
return (WORD)dwClrUsed;

/* Calculate the number of colors in the color table based on
* the number of bits per pixel for the DIB.
*/
wBitCount = m_pBMI->bmiHeader.biBitCount;

/* return number of colors based on bits per pixel */
switch (wBitCount)
{
case 1:
return 2;

case 4:
return 16;

case 8:
return 256;

default:
return 0;
}
}有些糊涂。
...全文
684 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiuxianshen 2010-06-20
  • 打赏
  • 举报
回复
biBitCount 单个像素所占直接数,例如,二值为1,灰度为8,RGB为24,RGBA为32
biClrUsed http://topic.csdn.net/u/20080606/20/ea8c6852-78fc-40e8-bcef-be78d37c6a42.html
Eleven 2010-06-20
  • 打赏
  • 举报
回复
参考MSDN上的BITMAPINFOHEADER结构体中的biBitCount成员的描述
if(strPathName == "") return false; BITMAPFILEHEADER * pBFH; BITMAPINFOHEADER * pBIH; CFile file(strPathName,CFile::modeCreate | CFile::modeNoTruncate | CFile::modeRead); DWORD filelength = file.GetLength(); // 分配一块内存用于装入图象数据 if(m_hDib!=NULL) ::GlobalFree(m_hDib); m_hDib = :: GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,filelength); if(m_hDib==NULL) { return (FALSE); } m_pDib = (BYTE *)::GlobalLock(m_hDib); if(m_pDib==NULL) { ::GlobalFree(m_hDib); return (FALSE); } // 将图象数据读入内存 file.Read(m_pDib,(UINT)filelength); file.Close(); // 读入头信息 pBFH = (BITMAPFILEHEADER *) m_pDib; pBIH = (BITMAPINFOHEADER *) (m_pDib+sizeof(BITMAPFILEHEADER)); WORD bfType = pBFH->bfType; if(bfType!=19778) // "BM"标志 { AfxMessageBox("Not a valid BMP image!"); return FALSE; } //得到位图信息 m_nWidth = (int)pBIH->biWidth; m_nHeight = (int)pBIH->biHeight; m_nBits = (int)pBIH->biBitCount; m_nColors = (int)pBIH->biClrUsed; if(m_nColors == 0) { if(m_nBits <= 8) m_nColors = 1 << m_nBits; } m_strPathName = strPathName; // 载入调色板 m_Palette.DeleteObject(); if( m_nBits <= 8 ) m_nPaletteInBytes = m_nColors * sizeof( RGBQUAD ); if( m_nBits <= 8 ) { RGBQUAD *pRGBPalette; pRGBPalette = ( RGBQUAD * )&m_pDib[ sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER ) ]; LOGPALETTE *pLogPalette; pLogPalette = ( LOGPALETTE * )new char[ sizeof( LOGPALETTE ) + m_nColors * sizeof( PALETTEENTRY ) ]; pLogPalette->palVersion = 0x300; pLogPalette->palNumEntries = ( unsigned short )m_nColors; for(int i=0; ipalPalEntry[ i ].peRed = pRGBPalette[ i ].rgbRed; pLogPalette->palPalEntry[ i ].peGreen = pRGBPalette[ i ].rgbGreen; pLogPalette->palPalEntry[ i ].peBlue = pRGBPalette[ i ].rgbBlue; pLogPalette->palPalEntry[ i ].peFlags = 0; } if( pLogPalette == NULL ) { ::GlobalUnlock( m_hDib ); return FALSE; } m_Palette.CreatePalette( pLogPalette ); delete[] pLogPalette; }

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧