Part 1: overview BMP file structure (Reference: http://en.wikipedia.org/wiki/BMP_file_format#File_structure)
The bitmap image file consists of fixed-size structures (headers) as well as variable-size structures appearing in a predetermined sequence. Many different versions of some of these structures can appear in the file, due to the long evolution of this file format.
(Source of picture: WikiPedia.org)
Referring to the diagram 1, the bitmap file is composed of structures in the following order:
|Bitmap File Header||No||14 Bytes||To store general information about the Bitmap Image File||Not needed after the file is loaded in memory|
(however 7 different versions exist)
|To store detailed information about the bitmap image and define the pixel format||Immediately follows the Bitmap File Header|
|Extra bit masks||Yes||3 or 4 DWORDs
(12 or 16 Bytes)
|To define the pixel format||Present only in case the DIB Header is the BITMAPINFOHEADER|
|Color Table||Semi-optional||Variable-size||To define colors used by the bitmap image data (Pixel Array)||Mandatory for color depths <= 8|
|Gap1||Yes||Variable-size||Structure alignment||An artifact of the File Offset to PixelArray in the Bitmap File Header|
|Pixel Array||No||Variable-size||To define the actual values of the pixels||The pixel format is defined by the DIB Header or Extra bit masks. Each row in the Pixel Array is padded to a multiple of 4 bytes in size|
|Gap2||Yes||Variable-size||Structure alignment||An artifact of the ICC Profile Data offset field in the DIB Header|
|ICC Color Profile||Yes||Variable-size||To define the color profile for color management||Can also contain a path to an external file containing the color profile. When loaded in memory as “non-packed DIB”, it is located between the color table and gap1.|
In a bitmap image file on a disk or a bitmap image in memory, the pixels can be defined by a varying number of bits.
- The 1-bit per pixel (1bpp) format supports 2 distinct colors, (for example: black and white). The pixel values are stored in each bit, with the first (left-most) pixel in the most-significant bit of the first byte.Each bit is an index into a table of 2 colors. An unset bit will refer to the first color table entry, and a set bit will refer to the last (second) color table entry.
- The 2-bit per pixel (2bpp) format supports 4 distinct colors and stores 4 pixels per 1 byte, the left-most pixel being in the two most significant bits (Windows CE only). Each pixel value is a 2-bit index into a table of up to 4 colors.
- The 4-bit per pixel (4bpp) format supports 16 distinct colors and stores 2 pixels per 1 byte, the left-most pixel being in the more significant nibble. Each pixel value is a 4-bit index into a table of up to 16 colors.
- The 8-bit per pixel (8bpp) format supports 256 distinct colors and stores 1 pixel per 1 byte. Each byte is an index into a table of up to 256 colors.
- The 16-bit per pixel (16bpp) format supports 65536 distinct colors and stores 1 pixel per 2 byte WORD. Each WORD can define the alpha, red, green and blue samples of the pixel.
- The 24-bit pixel (24bpp) format supports 16,777,216 distinct colors and stores 1 pixel value per 3 bytes. Each pixel value defines the red, green and blue samples of the pixel (18.104.22.168.0 in RGBAX notation). Specifically in the order (blue, green and red, 8-bits per each sample).
- The 32-bit per pixel (32bpp) format supports 4,294,967,296 distinct colors and stores 1 pixel per 4 byte DWORD. Each DWORD can define the Alpha, Red, Green and Blue samples of the pixel.