Monday, September 29, 2014

How the BMP (bitmap) image looks from the inside, in binary

I needed to create a mitmap image in to code converter for Nokia 5110 display. So i decided to use .bmp images, just because they are simple.

If you want to look inside image file (binary code) you can use any "hex editor", if you open any file, programm will display to you hexadecimal codes from which this file consist of. I use "Hex Editor XVI32"


Example of the bmp file (4 pixels of different colors):

At first I would like to point out that the number of bytes which are used for representing 1 horizontal line should be dividable by 4, because of that sometimes there could be "padding" or empty bytes used only to insure that size of the array containing information about 1 line of pixels is dividable by 4.

Pixels are stored in memory in that way: At first we have bottom left pixel, after that we go to the right until the end of the line, then we rise to the second line (from the bottom) and the proccess is repeated.

This table shows you how this simple image is represented in binary data:
OffsetSizeHex ValueValueDescription
BMP Header
0h242 4D"BM"ID field (42h, 4Dh)
2h446 00 00 0070 bytes (54+16)Size of the BMP file
6h200 00UnusedApplication specific
8h200 00UnusedApplication specific
Ah436 00 00 0054 bytes (14+40)Offset where the pixel array (bitmap data) can be found
DIB Header
Eh428 00 00 0040 bytesNumber of bytes in the DIB header (from this point)
12h402 00 00 002 pixels (left to right order)Width of the bitmap in pixels
16h402 00 00 002 pixels (bottom to top order)Height of the bitmap in pixels. Positive for bottom to top pixel order.
1Ah201 001 planeNumber of color planes being used
1Ch218 0024 bitsNumber of bits per pixel
1Eh400 00 00 000BI_RGB, no pixel array compression used
22h410 00 00 0016 bytesSize of the raw bitmap data (including padding)
(how many bytes for 1 pixel)
26h413 0B 00 002835 pixels/meter horizontalPrint resolution of the image,
72 DPI × 39.3701 inches per meter yields 2834.6472
2Ah413 0B 00 002835 pixels/meter vertical
2Eh400 00 00 000 colorsNumber of colors in the palette
32h400 00 00 000 important colors0 means all colors are important
Start of pixel array (bitmap data)
36h300 00 FF0 0 255Red, Pixel (0,1)
39h3FF FF FF255 255 255White, Pixel (1,1)
3Ch200 000 0Padding for 4 byte alignment (could be a value other than zero)
3Eh3FF 00 00255 0 0Blue, Pixel (0,0)
41h300 FF 000 255 0Green, Pixel (1,0)
44h200 000 0Padding for 4 byte alignment (could be a value other than zero)
1 pixel can be represented by different number of bits:

  • 1-bit per pixel (1bpp), supports 2 colors (black and white). 0 == bottom left pixel color, 1 == second color.
  • 2-bits per pixel (2bpp), supports 4 colors
  • 4-bits per pixel (4bpp), supports 16 colors, in theory it also contains a palette of colors, but i didn't understood how he works with them or where he keeps them.
  • 8-bits per pixel  (8bpp), supports 256 colors, can use palette or color encoding (RGB):
Bit     7   6   5   4   3   2   1  0
Data   R  R  R  G  G  G  B  B

  • 16-bits per pixel  (16bpp), supports 65536 colors, can store Alpha channel (transparency).


  • 24-bits per pixel (24bpp), supports 16,777,216 colors цвет stored as BGR, blue, green and red, 1 byte for every color (8 bit).
  • 32-bits per pixel (24bpp), supports 4,294,967,296 colors, could be presented in this way:


It is possible to represent any color by different number of bits, you can configure it in the bit masks at 36h (if they are used). e.g. we can use 8 bit on color (RGB) and 8 bits for alpha channel.

Source:
wikipedia

No comments :

Post a Comment