Creating a color table
-
Hi guys, I am trying to use the Win32 API to fill a bitmap, using the function GetDIBBits(). My source bitmap is a 32-bit image, but I am hoping to create a 4-bit destination bitmap. I am of the understanding that this requires me to create a color table, and keep it in a BITMAPINFO structure. Does anybody see a problem with this snippet of code?
pbmi = (PBITMAPINFO) LocalAlloc(LPTR,sizeof(BITMAPINFO)); // Initialize the fields in the BITMAPINFO structure. pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER) + 16*sizeof(long); pbmi->bmiHeader.biWidth = bmp.bmWidth; pbmi->bmiHeader.biHeight = bmp.bmHeight; pbmi->bmiHeader.biPlanes = bmp.bmPlanes; pbmi->bmiHeader.biBitCount = 4; pbmi->bmiHeader.biCompression = BI_RGB; if (pbmi->bmiHeader.biSizeImage == 0) pbmi->bmiHeader.biSizeImage = WIDTHBYTES((DWORD)pbmi->bmiHeader.biWidth * pbmi->bmiHeader.biBitCount) * pbmi->bmiHeader.biHeight; pbmi->bmiHeader.biClrImportant = 0; for(i = 0; i<16; i++) { pbmi->bmiColors[i].rgbRed = 0; pbmi->bmiColors[i].rgbBlue = 0; pbmi->bmiColors[i].rgbGreen = 0; pbmi->bmiColors[i].rgbReserved = 0; } // Bright Red pbmi->bmiColors[1].rgbRed = 255; // Red pbmi->bmiColors[2].rgbRed = 128; // Bright Green pbmi->bmiColors[3].rgbGreen = 255; // Green pbmi->bmiColors[4].rgbGreen = 128; // Bright Blue pbmi->bmiColors[5].rgbBlue = 255; // Blue pbmi->bmiColors[6].rgbBlue = 128; //Yellow pbmi->bmiColors[7].rgbRed = 255; pbmi->bmiColors[7].rgbGreen = 255; //Brown pbmi->bmiColors[8].rgbRed = 128; pbmi->bmiColors[8].rgbGreen = 128; //Bright Magenta pbmi->bmiColors[9].rgbRed = 255; pbmi->bmiColors[9].rgbBlue = 255; //Magenta pbmi->bmiColors[10].rgbRed = 128; pbmi->bmiColors[10].rgbBlue = 128; //Bright Cyan pbmi->bmiColors[11].rgbGreen = 255; pbmi->bmiColors[11].rgbBlue = 255; //Cyan pbmi->bmiColors[12].rgbGreen = 128; pbmi->bmiColors[12].rgbBlue = 128; //Gray pbmi->bmiColors[13].rgbRed = 0; pbmi->bmiColors[13].rgbGreen = 128; pbmi->bmiColors[13].rgbBlue = 0; //Bright Gray pbmi->bmiColors[14].rgbRed = 192; pbmi->bmiColors[14].rgb
-
Hi guys, I am trying to use the Win32 API to fill a bitmap, using the function GetDIBBits(). My source bitmap is a 32-bit image, but I am hoping to create a 4-bit destination bitmap. I am of the understanding that this requires me to create a color table, and keep it in a BITMAPINFO structure. Does anybody see a problem with this snippet of code?
pbmi = (PBITMAPINFO) LocalAlloc(LPTR,sizeof(BITMAPINFO)); // Initialize the fields in the BITMAPINFO structure. pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER) + 16*sizeof(long); pbmi->bmiHeader.biWidth = bmp.bmWidth; pbmi->bmiHeader.biHeight = bmp.bmHeight; pbmi->bmiHeader.biPlanes = bmp.bmPlanes; pbmi->bmiHeader.biBitCount = 4; pbmi->bmiHeader.biCompression = BI_RGB; if (pbmi->bmiHeader.biSizeImage == 0) pbmi->bmiHeader.biSizeImage = WIDTHBYTES((DWORD)pbmi->bmiHeader.biWidth * pbmi->bmiHeader.biBitCount) * pbmi->bmiHeader.biHeight; pbmi->bmiHeader.biClrImportant = 0; for(i = 0; i<16; i++) { pbmi->bmiColors[i].rgbRed = 0; pbmi->bmiColors[i].rgbBlue = 0; pbmi->bmiColors[i].rgbGreen = 0; pbmi->bmiColors[i].rgbReserved = 0; } // Bright Red pbmi->bmiColors[1].rgbRed = 255; // Red pbmi->bmiColors[2].rgbRed = 128; // Bright Green pbmi->bmiColors[3].rgbGreen = 255; // Green pbmi->bmiColors[4].rgbGreen = 128; // Bright Blue pbmi->bmiColors[5].rgbBlue = 255; // Blue pbmi->bmiColors[6].rgbBlue = 128; //Yellow pbmi->bmiColors[7].rgbRed = 255; pbmi->bmiColors[7].rgbGreen = 255; //Brown pbmi->bmiColors[8].rgbRed = 128; pbmi->bmiColors[8].rgbGreen = 128; //Bright Magenta pbmi->bmiColors[9].rgbRed = 255; pbmi->bmiColors[9].rgbBlue = 255; //Magenta pbmi->bmiColors[10].rgbRed = 128; pbmi->bmiColors[10].rgbBlue = 128; //Bright Cyan pbmi->bmiColors[11].rgbGreen = 255; pbmi->bmiColors[11].rgbBlue = 255; //Cyan pbmi->bmiColors[12].rgbGreen = 128; pbmi->bmiColors[12].rgbBlue = 128; //Gray pbmi->bmiColors[13].rgbRed = 0; pbmi->bmiColors[13].rgbGreen = 128; pbmi->bmiColors[13].rgbBlue = 0; //Bright Gray pbmi->bmiColors[14].rgbRed = 192; pbmi->bmiColors[14].rgb
TheDelChop wrote:
Am I missing something here?
Yes, you didn't allocate memory for the colour entries (
pbmi->bmiColors[]
). You have to do:pbmi = (PBITMAPINFO) LocalAlloc(LPTR, sizeof(BITMAPINFO) + 15 * sizeof(RGBQUAD));
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.