gpuguy
gpuguy

Reputation: 4585

What is the BMP format for Gray scale Images?

What is the BMP format for Gray scale Images (especially for 16 bit per pixel) ? The wikipedia just talks about colour images for bmp.

Update:

Just for an update and information for future visitors, I am going for PGM as this is uncompressed and can support 16 bit gray-scale. Another option was to use PNG, but it compresses the data (which is not what I want) as discussed here . Also note that the image may appear distorted, since most of the monitors support 256 colors and not 4096 for 16 bit. So the Image will be saturated.

It was though surprising to know that BMP is almost helpless in case of Gray-scale for 16 bit images.
Thanks for people who helped me understand the issue.

Upvotes: 16

Views: 43472

Answers (4)

Mark Ransom
Mark Ransom

Reputation: 308432

You're right, BMP only knows about colors. The way to do this is to create a palette of 256 entries, where each entry has the same value for R,G,B: first entry (0,0,0), second entry (1,1,1) etc. Now make the image 8 bits per pixel using the palette.

Edit: given your new requirement for 16 bit grayscale, I think you have 2 choices: convert to 8 bit, or use a different format other than BMP. If you convert to 8 bit, you can use dithering to make a result that is visually indistinguishable from your source 16 bit image.

Upvotes: 3

jsalonen
jsalonen

Reputation: 30521

For grayscale images, I would use 8-bit BMP. 8 bit BMPs can encode colors with palette. However, if you don't use one, you can simply interpret color values [0...255] as colors from black (0) to white (255).

Edit: I wouldn't use BMP for 16-bit grayscale images. Technically, you could use 16-bits per pixel BMP format for encoding 16-bit grayscale data (http://en.wikipedia.org/wiki/BMP_file_format#Pixel_format). However in practice this is a bad idea (read: hacky) since that depth is designed to encode alpha, red, green and blue samples of the pixels.

A better format for storing 16-bit per pixel grayscale data is PNG.

Also ask yourself, do you really, really need that extra-precision? For most applications, 8 bits per pixel is just fine (=if you don't have any specific requirements on precision, this would be the case).

Upvotes: 3

Vladimir Panteleev
Vladimir Panteleev

Reputation: 25187

What is the BMP format for Gray scale Images

Aside from using a palette, you can create a grayscale BMP (8 bits per pixel) by writing a BMP with a BITMAPV4HEADER. and set bV4RedMask, bV4GreenMask and bV4BlueMask to the same value. However, the minimum bcBitCount value for such a format is 16, so each pixel will still need to occupy two bytes. You can use the second byte for the alpha channel (transparency) though.

especially for 16 bit per pixel

It doesn't look like any BMP version supports 16 bit color depth. Even though the file format seemingly allows it (bcBitCount=16 and bV4…Mask=0xFFFF), image editors and libraries discard the extra bits.

Some documents refer to 64-BPP BMP files, however it's unclear how that would fit with the BITMAPV4HEADER fields, as the mask fields are all 32 bits in size (so it's not possible to specify a channel mask for 64 BPP).

Another option was to use PNG, but it compresses the data (which is not what I want) as discussed here .

You don't have to compress PNG files if you don't want to. Using a compression level of 0 will put the pixels as they are (plus a zlib header/footer).

Also note that the image may appear distorted, since most of the monitors support 256 colors and not 4096 for 16 bit.

16 bit depth would allow for 65536 distinct per-channel luminosity values, not 4096. The distortion sounds like an issue with the gamma curve, and isn't really related to the file format question.

Upvotes: 1

trumpetlicks
trumpetlicks

Reputation: 7075

As it appears, TIFF and PNG natively support 16 bit grayscale.

http://en.wikipedia.org/wiki/Grayscale

Im assuming that these images you wish to save are not simply for display but for some sort of post processing where you need the extra precision? If not, then I would suggest deleting the least significant 8 bits, and storing a simpler 8 bit bitmap with a color map to map each value to an RGB with a value -> (value, value, value) RGB mapping.

Upvotes: 2

Related Questions