Click to See Complete Forum and Search --> : Creating 8bit bmp from display
I am trying to generate an 8-bit, grayscale image from a window. The hardware on the machine I am using will allow a 24 bit image, and it seems to be defaulting to that when the bitmap is created. ie, the bitsperpixel field in the bitmap info structure equals 24. How can I force the program to generate an 8-bitperpixel bmp file?
Abadon
May 13th, 1999, 10:16 AM
In your code that sets all the BITMAPINFOHEADER member var's just set the bitsperpixel to 8. And with 256 color bmp's you must use a RGBQUAD struct for the color table. Here is an example:
unsigned short bitspersample = 8;
unsigned long bmpimagesize;
DWORD offset;
RGBQUAD* prgbq;
bmpwidth = ((imagewidth + 31) / 32) * 32;
colors=256;
bmpimagesize = imagewidth*imagelength;
// THIS IS FOR A GRAYSCALE COLOR TABLE
prgbq = new RGBQUAD[colors];
for (loop = 0; loop < colors; loop++)
{
j = (BYTE)((loop * 255 + colors - 2) / (colors - 1));
prgbq[loop].rgbBlue = j;
prgbq[loop].rgbGreen = j;
prgbq[loop].rgbRed = j;
prgbq[loop].rgbReserved = 0;
}
DWORD size = sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD)*colors + bmpimagesize;
//--------------------------------------------------------------------------------
// Set the address of the BITMAPFILEHEADER and BITMAPINFOHEADER.
//--------------------------------------------------------------------------------
m_pBmfh = (BITMAPFILEHEADER*) m_pData;
m_pBmih = (BITMAPINFOHEADER*) &m_pData[sizeof(BITMAPFILEHEADER)];
//---------------------------------
// Set the BITMAPFILEHEADER fields.
//---------------------------------
m_pBmfh->bfType = * (WORD *) "BM";
m_pBmfh->bfSize = (DWORD) size;
m_pBmfh->bfReserved1 = 0;
m_pBmfh->bfReserved2 = 0;
m_pBmfh->bfOffBits = (DWORD) (sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD)*colors);
m_pBmih->biSize = sizeof(BITMAPINFOHEADER);
m_pBmih->biWidth = bmpwidth ;
m_pBmih->biHeight = imagelength;
m_pBmih->biPlanes = 1;
m_pBmih->biBitCount = bitspersample;
m_pBmih->biCompression = BI_RGB;
m_pBmih->biSizeImage = 0;
m_pBmih->biClrUsed = 0;
m_pBmih->biClrImportant = 0;
//---------------------------------
// Copy the color map into m_pData.
//---------------------------------
CopyMemory(&m_pData[sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)],
prgbq, sizeof(RGBQUAD)*colors);
delete [] prgbq;
offset = m_pBmfh->bfOffBits;
// THIS IS A USER BUFFER THAT I USED TO ALTER PIXELS
// AND IS COPIED INTO THE BMP
CopyMemory(&m_pData[offset], pBinBuf, bmpimagesize);
Hope this helps, I use this in a CDib Class that i wrote to do image processing in grayscale.
Regards,
Abadon
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.