BMP

This graphics format is used by the operating system Microsoft Windows. Originally, it was intended for 16 color images. It supports only simple compression. This is because it is used frequently, and a long compression would slow down significantly the speed of the whole system. Therefore, the format is simple and fast. A negative feature is large size of the image.



Figure: Fragment of the BMP header, 16.7 mill. colors, 60x35 pixels, file size = 6354 bytes

Bitmap file structure

    Each bitmap file comprises the following sections :

Bitmap-file header 
Bitmap-information header 
Color table
Field of bytes defining bitmap

Bitmap-file header

This section contains information on the type, size and specifications of the bitmap file independent from the device.

Offset Length   Meaning
0 2 Identification of BMP file. Must be always = BM (in hex.:4D42)
2 4 Total length of the file in bytes
6 2 Must be 0
8 2 Must be 0
10 4 ImageDataOffset
- the beginning of the image data in bytes (offset from the start of the file) 


Bitmap-information header

    Specifies the dimension, compression type, and color format of the bitmap.

Offset Length   Meaning
0 4 HeaderSize
specifies the number of bytes in the header. Always 40.
4 4 ImageWidth
Width of the image in points
8 4 ImageHeight
Height of the image in points
12 2 NumberOfImagePlanes
Specifies the number of bit planes
14 2 BitsPerPixe
Specifies the number of bits per pixel. Can be: 1, 4, 8, or 24.
16 4 CompressionMethod
Type of compression:
  0 = none
  1 = 8. bit. RLE
  2 = 4. bit. RLE
20 4 SizeImage 
Specifies the size of image in bytes. May not be 0.
24 4 HResolution
Specifies the horizontal resolution in pixels per meter.
28 4 VResolution
 Specifies the vertical resolution in pixels per meter.
32 4 ColorsUsed
 Specifies the current number of colors in the pallet. If the value = 0, then the bitmap uses all colors from the color table. 
36 4 ColorsImportant 
Specifies how many colors are important for displaying an image. If the value = 0, then all colors are important.


BitPerPixle It can have these values:
 

 1   Here the bitmap is monochromatic, comprising of two values. Every bit in the bitmap field represents one pixel. If the bit is empty, the pixel is displayed with the color of the first value in the  Color Table. If the bit is full, then the pixel has the color of the second value in the Color Table.
4 The bitmap has maximally 16 colors. Each pixel in the bitmap is represented by a 4-bit index from the Color Table. For instance, if the first byte in the bitmap is 1Fh, it represents two pixels. The first pixel is displayed with the color number 2, and the second one with the color number 16.
8 The bitmap has maximally 256 colors. Each pixel of the bitmap is represented by 1-byte, specifying the number of the pixel. For instance, if the first in the bitmap is 1Fh, then the first pixel has the color number 32 of the Color Table.
24 The bitmap has maximally 2^24 colors. The bmiColor (or bmciColor) member is NULL, and each 3-byte sequence in the map array represents the relative intensities of red, green, and blue, respectively, for a pixel


Pallet of colors

    This defines the array ? RGB structure ? comprising colors used in the bitmap. Color in the table area is arranged with growing importance. This is advantageous if the display device is not capable of displaying all the colors used in the bitmap. The pallet of a BMP image has the size of the number of colors * 4 bytes. The pallet has meaning if the value of the BitPerPixle is either 1, 4, or 8. In using 24-bits per pixel, the color is stored directly in data as a sequence of values of individual color components RGB. So, for instance, data 10, 60, 30 mean that red is 10, green is 60, blue is 30. For one dot or pixel it is necessary to store always 4 bytes. Because in the pallet there is stored also 1 byte more, for 256 colors it is necessary to store 1024 bytes instead of 786 bytes.



Example of storing the pallet of a 16-color image :

         Blue Green Red Not used
[00000000]  84   252    84     0
[00000001] 252   252    84     0
[00000002]  84    84   252     0
[00000003] 252    84   252     0
[00000004]  84   252   252     0
[00000005] 252   252   252     0
[00000006]   0     0     0     0
[00000007] 168     0     0     0
[00000008]   0   168     0     0
[00000009] 168   168     0     0
[0000000A]   0     0   168     0
[0000000B] 168     0   168     0
[0000000C]   0   168   168     0
[0000000D] 168   168   168     0
[0000000E]  84    84    84     0
[0000000F] 252    84    84     0




The field of bytes defining the bitmap

    This is set in accordance with the header and the color table. The data, immediately following after the color table, comprise the array of bytes representing the sequence of a line of the bitmap (scanline). Each scanline contains a sequence of bytes representing a series of pixels in the scanline, in the order from left to right. The size of bits representing the scanline depth of color format and idth in pixels of the bitmap. If it is necessary, the scan line must null the unused empty bits up to the 32-bit edge. But, the segment of the edge can occur anywhere in the bitmap. The scan lines in the bitmap are stored from bottom to top. This means that the first byte in the array represents the pixel in the bottom left edge of the bitmap, and the last represents the pixel in the top right edge.

    CompressionMethod

  1.     Windows version 3.0 and higher support the run-length encoded (RLE) format for the compression of the bitmap, using 4 bits per pixel or 8 bits per pixel. This compression reduces the size of resulting bitmap.

    Value 0    BI_RGB - the bitmap is not compressed?
  2. Value 1    BI_RLE8 - the bitmap is encoded by RLE compression with 8 bits per pixel. The compression format is 2-bytes, comprising a sequence of bytes, followed by one byte containing the value from the color table.
  3. Value 2    BI_RLE4 Specifies RLE coding with 4 bits per pixel. The compression format is 2-bytes, containing the sequence of bytes followed in 2-word index color. 

Reading a non-compressed BMP file.

    The image is stored in a non-coded state and is directly displayable. For 1-bit images, there are in one byte stored 8 points (1 bit = 1 point). For 4-bit images, there are stored in one byte 2 points. Four bits represent the value: 0-15, which specifies the index of a color from the pallet. For 8-bit images (meaning 256-color images) it is the value 0-255, which specifies the color index of the pallet. A special case are 24-bit images, when one point requires 4 bytes. Each point takes up 4 bytes in the file, where the first three bytes specify the ratio of red, green and blue components in the resulting color, and one byte is empty. Each of the components represents the intensity of the given color from 0-255.


    Example of compressed data :
 

Compressed data Expanded data
03 04 04 04 04
05 06 06 06 06 06 06
   
02 78 78 78
00 02 05 01 Move 5 right and 1 down
02 78 78 78
00 00 End of the line
09 1E 1E 1E 1E 1E 1E 1E 1E 1E 1E
00 01 End of the RLE bitmap