The BMP format
The BMP is one of the simplest formats. It was jointly developed by Microsoft and IBM, which explains why it is particularly widespread on Windows and OS/2 platforms. A BMP file is a bitmap file, i.e. a graphic image file, with pixels stored in the form of point table and managing the colors either as true colors or using an indexed palette. The BMP format has been studied in such a way as to obtain a bitmap that is independent of the peripheral display device (DIB, Device independent bitmap).
The structure of a bitmap file is the following:
- File header
- Bitmap information header (also called information Header)
- Palette (optional)
- The image body
The file header provides information on the type of file (Bitmap) and its size as well as indicating where information concerning the image actually starts.
The header comprises four fields:
- The signature (on 2 bytes), indicating that it is a BMP file, using two characters
- BM, 424D in hexadecimal, indicates that it is a Windows Bitmap.
- BA indicates that it is an OS/2 Bitmap.
- CI indicates that it is an OS/2 color icon.
- CP indicates that it is an OS/2 color pointer.
- IC indicates that it an OS/2 icon.
- PT indicates that it is an OS/2 pointer.
- The total size of the file in bytes (coded on 4 bytes)
- A reserved field (on 4 bytes)
- The offset of the image (on 4 bytes), that is, it is the location of the start of the image information relative to the start of the file
Bitmap information header
The bitmap information header provides information on the image, particularly its dimensions and its colors.
The bitmap information header comprises four fields:
- The size of the bitmap information header in bytes (coded on 4 bytes). The following hexadecimal values are possible according to the type of BMP format:
- 28 for Windows 3.1x, 95, NTâ€¦
- 0C for OS/2 1.x
- F0 for OS/2 2.x
- The width of the image (on 4 bytes), i.e. the number of pixels counted horizontally
- The height of the image (on 4 bytes), i.e. the number of pixels counted vertically
- The number of planes (on 2 bytes). This value is always 1
- The color model depth of (on 2 bytes), i.e. the number of bits used to code the color. This value may be equal to 1, 4, 8,16, 24 or 32
- The compression method (on 4 bytes). This value is 0 when the image is not compressed, or either 1, 2 or 3 depending on the type of compression used:
- 1 for RLE coding of 8 bits per pixel
- 2 for RLE coding of 4 bits per pixel
- 3 for bitfield coding, meaning that the color is coded by a triple mask represented by the palette
- The total size of the image in bytes (on 4 bytes).
- The horizontal resolution (on 4 bytes), i.e. the number of pixels per metre counted horizontally
- The vertical resolution (on 4 bytes), i.e. the number of pixels per metre counted vertically
- The number of palette colors (on 4 bytes)
- The number of important palette colors (on 4 bytes). This field may be equal to 0 when all colors are important.
The palette is optional. When a palette is defined, it contains 4 bytes successively for each of its entries, representing:
- The blue component (on one byte)
- The green component (on one byte)
- The red component (on one byte)
- A reserved field (on one byte)
The coding of the image is done by successively writing the bits corresponding to each pixel, line by line, starting with the pixel in the bottom left-hand corner.
- 2-Color images use 1 bit per pixel, which means that one byte allows 8 pixels to be coded
- 16-Color images use 4 bits per pixel, which means that one byte allows 2 pixels to be coded
- 256-Color images use 8 bits per pixel, which means that one byte is needed to code each pixel
- Real color images use 24 bits per pixel, which means that 3 bytes are needed to code each pixel, taking care to respect the alternating color order for blue, green and red.
Each image line must comprise a total number of bytes that is a multiple of 4; if this is not the case, the line must be completed with as many 0 as necessary in order to respect this criterion.