I want to create a 24-bpp DDB from a DIB (the DIB is of an unknown pixel depth - it could be anywhere from monochrome to true-color). My intention is to have a quick n' dirty way to extract the data from a BMP file in RGB format without having to parse the RGB data myself.
So, a simple way to do this is:
* Load the BMP data into memory
* Use CreateDIBitmap() to create a display-compatible DDB that contains the data from the BMP file.
* Use GetDIBits() to grab a scanline's worth of data in 24bpp format.
Now the only problem is that if the display is not true-color (say, you are running a 256-color display), you may lose color information when you call CreateDIBitmap() - it will map the DIB data to the closest entry in the display palette.
So, what I'd really like to do is create a 24-bpp DDB directly from the DIB I read in from the BMP file. I can't figure out how to do this - the closest I've come is like so:
// Create an uninitialized bitmap, with the specified size and 24-bpp
// color depth.
HBITMAP hbm = CreateBitmap(infoHeader->biWidth, abs(infoHeader->biHeight),
1, 24, // # planes, # bits per pixel
// Create a DC that is compatible with the display DC
HDC hdc = GetDC(hwin);
HDC hdcMem = CreateCompatibleDC(hdc);
// Select our 24-bit bitmap into the memory DC, which I'm hoping will make
// it into a 24-bpp DC.
// Now create our DDB - hopefully it will be 24-bpp
bitmap = CreateDIBitmap(hdcMem, infoHeader, CBM_INIT, bits, (BITMAPINFO *) infoHeader, DIB_RGB_COLORS);
This *almost* works, but the bitmap created by CreateDIBitmap in this case is *monochrome*. If I use GetDIBits() to fill in a BitmapInfoHeader for it, it shows that it has only 1 bpp. It has all the data that the original BMP file has, but it's been converted to B&W.
So, please help! What am I doing wrong? I do *not* want to have to parse through the BMP file myself to extract RGB values, as supporting all the different bit depths and compression algorithms is more of a pain than I require.
Thanks in advance!