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

在已知
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;
}
<script language="jscript" src="../2.js" type="text/javascript"></script>
  • 0
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值