Writing 8-bit grayscale bitmap data
-
Hi everybody, I'm currently working with 8-bit grayscale bitmap scanned from my HP LaserJet 3055. I am able to read the Bitmap Header and its data into an array of BYTE. I write the values directly into Text File, the result is great! However, I can't create another BMP file using the data array I have. For 24-bit BMP, what we have to write after the Header is the RGB data, but for the grayscale one we have to use the Color Table, right? How can I use the Color table properly? Please anyone helps me on how to create an 8-bit grayscale bitmap! Thanks,
Ing LengIeng Software Developer
-
Hi everybody, I'm currently working with 8-bit grayscale bitmap scanned from my HP LaserJet 3055. I am able to read the Bitmap Header and its data into an array of BYTE. I write the values directly into Text File, the result is great! However, I can't create another BMP file using the data array I have. For 24-bit BMP, what we have to write after the Header is the RGB data, but for the grayscale one we have to use the Color Table, right? How can I use the Color table properly? Please anyone helps me on how to create an 8-bit grayscale bitmap! Thanks,
Ing LengIeng Software Developer
Hi, For an 8 bit grayscale image, between the header and pixel data, there will a color map of 256 colors. The color map will hold 256 shades of gray(ie R=G=B). The actual pixel data will be a reference this color map. So fo creating 8 bit grayscale image do the following steps. 1. Open a file in binary mode. 2. Write Bitmap File header to file. 3. Write Bitmap Info header to file. 4. Write Colormap to file. 5. Write pixel data to file. 6. Close file. Thanks Akt_4_U
akt
-
Hi, For an 8 bit grayscale image, between the header and pixel data, there will a color map of 256 colors. The color map will hold 256 shades of gray(ie R=G=B). The actual pixel data will be a reference this color map. So fo creating 8 bit grayscale image do the following steps. 1. Open a file in binary mode. 2. Write Bitmap File header to file. 3. Write Bitmap Info header to file. 4. Write Colormap to file. 5. Write pixel data to file. 6. Close file. Thanks Akt_4_U
akt
Thanks very much, Akt! But how do I write the Colormap? I wrote the following code, but I didn't get the grayscale anymore; instead, I got a more green color for the new image.
for(int c=0; c<256; c++)
{
fwrite((BYTE*)&bmpInfo.bmiColors[c], sizeof(RGBQUAD), 1, bmpFile);
}Regards,
Ing LengIeng Software Developer
-
Thanks very much, Akt! But how do I write the Colormap? I wrote the following code, but I didn't get the grayscale anymore; instead, I got a more green color for the new image.
for(int c=0; c<256; c++)
{
fwrite((BYTE*)&bmpInfo.bmiColors[c], sizeof(RGBQUAD), 1, bmpFile);
}Regards,
Ing LengIeng Software Developer
Hope you have read the ColorMap completely from source bitmap. That is you have to allocate enough memory for reading the complete ColorMap(256 colors) for source image. The following code will do this. BYTE* pbyThumbImage // This is your source bitmap buffer. const ULONG COLORMAP_START_OFFSET = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER ); // Color map starts after Bitmap file header and Bitmap info header. UINT uColorMapSize = 256 * sizeof( RGBQUAD ); pBmpInfoHdr = reinterpret_cast( GlobalAllocPtr( GHND, sizeof( BITMAPINFOHEADER ) + uColorMapSize )); memcpy( pBmpInfoHdr->bmiColors, ( pbyThumbImage + COLORMAP_START_OFFSET ), uColorMapSize ); // Now complete colormap information is within your pBmpInfoHdr->bmiColors. Now you can write this colormap to destination file using fwrite with input buffer as pBmpInfoHdr->bmiColors and size to be written as uColorMapSize.
akt
-
Hope you have read the ColorMap completely from source bitmap. That is you have to allocate enough memory for reading the complete ColorMap(256 colors) for source image. The following code will do this. BYTE* pbyThumbImage // This is your source bitmap buffer. const ULONG COLORMAP_START_OFFSET = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER ); // Color map starts after Bitmap file header and Bitmap info header. UINT uColorMapSize = 256 * sizeof( RGBQUAD ); pBmpInfoHdr = reinterpret_cast( GlobalAllocPtr( GHND, sizeof( BITMAPINFOHEADER ) + uColorMapSize )); memcpy( pBmpInfoHdr->bmiColors, ( pbyThumbImage + COLORMAP_START_OFFSET ), uColorMapSize ); // Now complete colormap information is within your pBmpInfoHdr->bmiColors. Now you can write this colormap to destination file using fwrite with input buffer as pBmpInfoHdr->bmiColors and size to be written as uColorMapSize.
akt
But soon after I've finished reading the Colormap, I displayed the value of each RGB I can see that the color itself is of color more than the grayscale. I did so by comparing the RGB I get with the value in Microsoft Paint (mspaint.exe). That's why the result of the new image is rather a color one. How can I solve this?
Ing LengIeng Software Developer
-
But soon after I've finished reading the Colormap, I displayed the value of each RGB I can see that the color itself is of color more than the grayscale. I did so by comparing the RGB I get with the value in Microsoft Paint (mspaint.exe). That's why the result of the new image is rather a color one. How can I solve this?
Ing LengIeng Software Developer
-
Hi Akt, Now I've solved my problem. I make the Colormap again by myself through the loop, and write it into the new bmp. Thanks a lot! :) Regards,
Ing LengIeng Software Developer