Byte Ordering

The BMP format was designed to work with systems based on Intel processors with their Little-endian byte-ordering scheme. This means that whenever a multibyte field is read (such as a 16- or 32-bit integer), the first byte read will be the least significant, and the last, the most significant.

 

TABLE 1: BMP File Structure

Byte # to fseek file pointer

Information

0

Signature

2

File size

18

Width (number of columns)

22

Height (number of rows)

28

Bits/pixel

46

Number of colors used

54

Start of color table

54 + 4*(number of colors)

Start of raster data

 

 

The first 14 bytes are dedicated to the header information of the BMP.

The next 40 bytes are dedicated towards the info header, where one can retrieve such characteristics as width, height, file size, and number of colors used. Next, is the color table, which is 4 x (number of colors used) bytes long.

So for an 8-bit grayscale image (number of colors is 256), the color table would be 4 x 256 bytes long or 1024 bytes. And the last bit of data in a BMP file is the pixel data, or raster data.

The raster data starts at byte 54 (header + info header) + 4 x number of colors (color table). For an 8-bit grayscale image, the raster data would start at byte 54 + 1024 = 1078. The size of the raster data is (width x height) – 1 bytes.

Therefore, a 100 row by 100 column 8-bit grayscale image would have (100 x 100) – 1 = 9,999 bytes of raster data starting at byte 1078 and continuing to the end of the BMP.

In terms of image processing, the most important information is the following:
(1) Number of columns – byte #18
(2) Number of rows - byte #22
(3) Raster data – byte #(4 x number of colors) to byte #1078 + (number of columns x number of rows) – 1