您的位置:首页 > 其它

新手求助解决如何提取像素点RGB值的问题

2006-01-26 14:17 411 查看
在已知
BITMAPINFO *Bitmap ;
BITMAPFILEHEADER bfh;
bmp头文件和信息飚的情况下!
bfh中可以提取出图片的大小和存储格式了!
但是如何提取出每个橡树点的RGB值呢?
假设定义x=100
Bitmap->bmiColors[100].rgbBlue;代表的是什么?
typedef struct tagBITMAPINFO {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BITMAPINFO, FAR *LPBITMAPINFO, *PBITMAPINFO;
BITMAPINFO结构中的RGBQUAD bmiColors[1];代表的是什么?
希望高手指导!

bmiColors[1];代表的是像素的颜色组成。

bmiColors是在有调色板情况下才使用,如256色的时候
即使用索引,数据中只用用一个字节甚至更少就能代表一种颜色
RGB模式中,颜色分为RGB三个值,每个多有8个字节(16位色除外)
x=100
Bitmap->bmiColors[100].rgbBlue;代表的是调色板中index=100的蓝色值

其实只要对DIB有足够的了解,这是一个极其简单的问题!
我写的一个程序,还未经过调试和优化,权作参考:
/*************************************************************************
*
* GetPixelColor()
*
* Return Value:
*
* BOOL - whether the color of assigned pixel is acquired
*
* Parameters:
*
*CPoint ptLocation:the location of assigned pixel;
*
*COLORREF &clrPixel:the color of assigned pixel to be stored;
*
* Description:
*
*Get the color of assigned pixel. This function treat BI_RGB style BMP
* only.
*
*************************************************************************/
BOOL CBmp::GetPixelColor(CPoint ptLocation, COLORREF &clrPixel)
{
if( m_hBmp == NULL )
return FALSE;
CSize bmpSize;
GetBmpSize(bmpSize);
if( ptLocation.x < 0 || ptLocation.x >= bmpSize.cx ||
ptLocation.y < 0 || ptLocation.y >= bmpSize.cy )
return FALSE;
;LPBITMAPINFOHEADER lpBi = (LPBITMAPINFOHEADER) ::GlobalLock((HGLOBAL) m_hBmp);
if( lpBi == NULL )
{
::GlobalUnlock((HGLOBAL)m_hBmp);
return FALSE;
}
if( lpBi->biCompression != BI_RGB/* && lpBi->biCompression != BI_BITFIELDS*/ )
{
::GlobalUnlock((HGLOBAL)m_hBmp);
return FALSE;
}
LPSTR lpBmpBitsAddr = GetBmpBitsAddr();
int nRowLen = BMP_ROW_LEN(lpBi->biBitCount * lpBi->biWidth),
nPalIndex = (bmpSize.cy - ptLocation.y - 1) * nRowLen;//Row addr
switch( lpBi->biBitCount )
{
case 1:
nPalIndex += (ptLocation.x >> 3);//Col addr
nPalIndex = *(lpBmpBitsAddr + nPalIndex);
nPalIndex >>= ((ptLocation.x + 7) % 8);
nPalIndex &= 0x1;
m_pBmpPal->GetPaletteEntries((UINT)nPalIndex, 1, (LPPALETTEENTRY)clrPixel);
break;
case 4:
nPalIndex += (ptLocation.x >> 1);//Col addr
nPalIndex = *(lpBmpBitsAddr + nPalIndex);
nPalIndex >>= (ptLocation.x % 2);
nPalIndex &= 0x0f;
m_pBmpPal->GetPaletteEntries((UINT)nPalIndex, 1, (LPPALETTEENTRY)clrPixel);
break;
case 8:
nPalIndex += ptLocation.x;//Col addr
nPalIndex = *(lpBmpBitsAddr + nPalIndex);
nPalIndex &= 0xff;
m_pBmpPal->GetPaletteEntries((UINT)nPalIndex, 1, (LPPALETTEENTRY)clrPixel);
break;
case 16://Treat BI_RGB only:
nPalIndex += ptLocation.x << 1;//Col addr
nPalIndex = *(lpBmpBitsAddr + nPalIndex);
nPalIndex &= 0xffff;
clrPixel = RGB((nPalIndex >> 10) & 0x1f, (nPalIndex >> 5) & 0x1f, nPalIndex & 0x1f);
break;
case 24:
nPalIndex += ptLocation.x * 3;//Col addr
clrPixel = RGB(((BYTE)*(lpBmpBitsAddr + nPalIndex + 2)),
((BYTE)*(lpBmpBitsAddr + nPalIndex + 1)),
((BYTE)*(lpBmpBitsAddr + nPalIndex)));
break;
case 32://Treat BI_RGB only:
nPalIndex += ptLocation.x << 2;//Col addr
clrPixel = RGB(((BYTE)*(lpBmpBitsAddr + nPalIndex + 2)),
((BYTE)*(lpBmpBitsAddr + nPalIndex + 1)),
((BYTE)*(lpBmpBitsAddr + nPalIndex)));
break;
default:
break;
::GlobalUnlock((HGLOBAL)m_hBmp);
lpBmpBitsAddr = NULL;
lpBi = NULL;
return FALSE;
}
::GlobalUnlock((HGLOBAL)m_hBmp);
lpBmpBitsAddr = NULL;
lpBi = NULL;
return TRUE;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐