What is the BMP format for Gray scale Images?

跟風遠走 提交于 2019-11-28 10:57:48

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 practise 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).

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!