如何获取24位bmp图片某个像素点的颜色信息?

starlee325 2005-05-13 10:22:25
我想获得的是RGB中B的灰度值
我的代码是这样的:
RGBQUAD cColor;
cColor=m_dib.GetPixel(x1, y1);
int a=cColor[10].rgbBlue;
所要的就是a
其中GetPixel(x,y)函数是这样的
RGBQUAD CDib::GetPixel(int x, int y)
{
// 颜色结构
RGBQUAD cColor;

// 根据每象素比特数得到此点的象素值
switch (m_lpBMIH->biBitCount)
{
case 1 :
if (1<<(7-x%8) & *(LPBYTE)(m_lpImage+GetPixelOffset(x, y)))
{
cColor.rgbBlue = 255;
cColor.rgbGreen = 255;
cColor.rgbRed = 255;
cColor.rgbReserved =0;
}
else
{
cColor.rgbBlue = 0;
cColor.rgbGreen = 0;
cColor.rgbRed = 0;
cColor.rgbReserved =0;
}
break;
case 4 :
{
int nIndex = (*(LPBYTE)(m_lpImage+GetPixelOffset(x, y)) &
(x%2 ? 0x0f : 0xf0)) >> (x%2 ? 0 : 4);
LPRGBQUAD pDibQuad = (LPRGBQUAD) (m_lpvColorTable) + nIndex;
cColor.rgbBlue = pDibQuad->rgbBlue;
cColor.rgbGreen = pDibQuad->rgbGreen;
cColor.rgbRed = pDibQuad->rgbRed;
cColor.rgbReserved =0;
}
break;
case 8 :
{
int nIndex = *(BYTE*)(m_lpImage+GetPixelOffset(x, y));
LPRGBQUAD pDibQuad = (LPRGBQUAD) (m_lpvColorTable) + nIndex;
cColor.rgbBlue = pDibQuad->rgbBlue;
cColor.rgbGreen = pDibQuad->rgbGreen;
cColor.rgbRed = pDibQuad->rgbRed;
cColor.rgbReserved =0;
}
break;
default:
int nIndex = *(BYTE*)(m_lpImage+GetPixelOffset(x, y));
cColor.rgbRed = m_lpImage[nIndex];
cColor.rgbGreen = m_lpImage[nIndex + 1];
cColor.rgbBlue = m_lpImage[nIndex + 2];
cColor.rgbReserved =0;
break;
}
这样做调试的时候a出了问题,得不到值
如何修改阿?
...全文
728 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xyw2278 2005-05-13
  • 打赏
  • 举报
回复
假设m_lpImage直接指向位图数据,估计可用下面得到:(没试过,你看对不对?)
BYTE r = *((BYTE*)(m_lpImage+GetPixelOffset(x, y) * 3 ));
BYTE g = *((BYTE*)(m_lpImage+GetPixelOffset(x, y) * 3 + 1));
BYTE b = *((BYTE*)(m_lpImage+GetPixelOffset(x, y) * 3 + 2));
xyw2278 2005-05-13
  • 打赏
  • 举报
回复
24位位图还有调色板吗,好像没有吧!

应该直接从位图本身的数据中读取,每24位代表一个像素,其中rgb各占8位。
vcmute 2005-05-13
  • 打赏
  • 举报
回复
RGBQUAD cColor;
int a=cColor[10].rgbBlue;

error C2676: binary '[' : 'struct tagRGBQUAD' does not define this operator or a conversion to a type acceptable to the predefined operator

用int a=cColor.rgbBlue;即可

8位图 你用的可能是指向调色板的BITMAPINFO
starlee325 2005-05-13
  • 打赏
  • 举报
回复
语法肯定没有错误的
刚才用8位图试过的
tangyongok 2005-05-13
  • 打赏
  • 举报
回复

int a=cColor[10].rgbBlue;//语法有问题

//RGBQUAD定义
typedef struct tagRGBQUAD { // rgbq
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;

19,468

社区成员

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

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