Hello, I could really use a little help from one of you bitwise gurus. I trying to add 16 bit capabilities to a function to greyscale an image inplace that I use for generating a disabled toolbar imagelist.
I found and tried tried 2 different methods;
The first one returns me a RGB value of (248, 248, 248) for a pixel that should be (255, 255, 255).
The second method does as well when I use the code for RGB 555 (so I take it that Windows uses RGB 555 for 16 bit memory bitmaps).
here is the function:
inline void GreyScaleImage(CBitmap* pbmpTarget, COLORREF crMask)
{
BITMAP bm = {0};
pbmpTarget->GetBitmap(&bm);
DWORD dwSize = (bm.bmHeight * bm.bmWidthBytes);
BYTE* pData = new BYTE[dwSize];
memset(pData, 0, dwSize);
pbmpTarget->GetBitmapBits(dwSize, pData);
if(bm.bmBitsPixel == 16)
{
BYTE* pPixel = pData;
WORD* p = (WORD*)pData;
for(int x = 0; x < (bm.bmHeight * bm.bmWidth); x++)
{
BYTE blue = pPixel[0] & 0x1F;
BYTE green = pPixel[0] >> 5;
green |= (pPixel[1] & 0x03) << 3;
BYTE red = pPixel[1] & 0x1F;
WORD wRed = red*8;
WORD wBlue = blue*8;
WORD wGreen = green*8;
WORD pixel = p[0];
BYTE red_value = (pixel & 0xF800) >> 11;
BYTE green_value = (pixel & 0x7E0) >> 5;
BYTE blue_value = (pixel & 0x1F);
BYTE bRed = red_value << 3;
BYTE bGreen = green_value << 2;
BYTE bBlue = blue_value << 3;
if(RGB(bRed,bGreen,bBlue) != crMask)
{
int iGrey = (((bRed)+(bGreen)+(bBlue)) / 3);
}
p++;
pPixel += 2;
}
}
else if(bm.bmBitsPixel == 32)
{
BYTE* pPixel = pData;
for(int x = 0; x < (bm.bmHeight * bm.bmWidth); x++)
{
BYTE bRed = pPixel[0];
BYTE bGreen = pPixel[1];
BYTE bBlue = pPixel[2];
if(RGB(bRed,bGreen,bBlue) != crMask)
{
int iGrey = (((bRed)+(bGreen)+(bBlue)) / 3);
pPixel[0] = iGrey;
pPixel[1] = iGrey;
pPixel[2] = iGrey;
}
pPixel += 4;
}
}
pbmpTarget->SetBitmapBits(dwSize, pData);
delete pData;
}
Any insight would be greatly appreciated, bitwise stuff is not my cup of tea.