3.1.6.2 Bitmaps with 8 Bits per Pixel

When the Compression field of a BitmapInfoHeader Object (section 2.2.2.3) is BI_RLE8, an RLE compression algorithm is used to compress an 8-bit bitmap. This format specifies encoded and absolute modes, and either mode can occur anywhere in a given bitmap.

Encoded mode involves two bytes. If the first byte of a pair is greater than zero, it specifies the number of consecutive pixels to be drawn using the color index that is contained in the second byte.

If the first byte of a pair is zero and the second byte is 0x02 or less, the second byte is an escape value that can denote the end of a line, the end of the bitmap, or a relative pixel position, as follows.

Second byte value

Meaning

0x00

End of line

0x01

End of bitmap

0x02

Delta

When a delta is specified, the 2 bytes following the escape value contain unsigned values indicating the horizontal and vertical offsets of the next pixel relative to the current position.

In absolute mode, the first byte is zero, and the second byte is a value in the range 0x03 through 0xFF. The second byte represents the number of bytes that follow, each of which contains the color index of a single pixel. In absolute mode, each run is aligned on a word boundary.

The following example shows the hexadecimal contents of an 8-bit compressed bitmap.

  03 04 05 06 00 03 45 56 67 00 02 78 00 02 05 01 
  02 78 00 00 09 1E 00 01 
  

This bitmap is interpreted as follows:

  • 03 04: Encoded mode, specifying 3 pixels with the value 0x04.

  • 05 06: Encoded mode, specifying 5 pixels with the value 0x06.

  • 00 03 45 56 67 00: Absolute mode, specifying 3 pixels with the values 0x45, 0x56, and 0x67, padded to a word boundary.

  • 02 78: Encoded mode, specifying 2 pixels with the value 0x78.

  • 00 02 05 01: Encoded mode, specifying a new relative position 5 pixels to the right and 1 line down.

  • 02 78: Encoded mode, specifying 2 pixels with the value 0x78.

  • 00 00: Encoded mode, specifying the end of a line.

  • 09 1E: Encoded mode, specifying 9 pixels with the value 1E.

  • 00 01: Encoded mode, specifying the end of the bitmap.

Now, suppose the bitmap has a width of 32 pixels and a height of 4 lines, and pixel values that are not specified are 0x00 by default. The resulting expended bitmap would be as follows.

  04 04 04 06 06 06 06 06 45 56 67 78 78 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 78 78 00 00 00 00 00 00 00 00 00 00 00 00
  1E 1E 1E 1E 1E 1E 1E 1E 1E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00