# Format - RLE bitmap

RLE bitmaps are stored line per line, each line encoded with a particular algorithm, usually a variation of RLE.

Let N be the number of bits per pixel of this bitmap.

- N bits being the "zero" color (it doesn't really matter what this color is, but the compression is best if this is the background color)

Then, for each row in the bitmap:

- 3 bits representing the type of encoding used for this line.

For encoding 0 (empty):
  There is no more data for this line, it consists of a single line of pixels of the "zero" color.

For encoding 1 (basic):
  While there are still pixels to decode in this line:
  - 4 bits representing the number P of pixels of an identical color, minus 1 (eg, 0 means a single pixel).
  - N bits representing the color C.
  Then the next P pixels are filled with color C.

For encoding 2 (delta):
  While there are still pixels to decode in this line:
  - 1 bit being 1 if the pixel run is encoded as delta, 0 if not
  If the bit is 1:
  - 6 bits representing the number P of pixels in this run, minus 1 The next P pixels are filled from a copy of the pixels in the previous line, starting at the same horizontal offset, or, if this is the first line of the bitmap, they are filled with the "zero" color.
  If the bit is 0:
  - 3 bits representing the number P of pixels in this run, minus 1
  - N bits representing the color C. Then the next P pixels are filled with color C.

For encoding 3 (basic/stop):
  - 8 bits representing a number P of pixels in an initial run - if P is greater than zero, P pixels are filled with the "zero" color.
  Then, while there are still pixels to decode in this line:
  - 6 bits representing the number P of pixels in this run, or 0
  - if P is greater than zero, N bits being the color to fill the next P pixels with.
  - if P is zero, the remainder of the line is filled with the zero color.

For encoding 4 (basic/startend):
  - 9 bits representing the number P of pixels in a run of color "zero" at the start of the line (0 for an empty run)
  - 9 bits representing the number P of pixels in a run of color "zero" at the end of the line (0 for an empty run)
  Then, while there are still pixels to decode in this line:
  - 3 bits representing the number P of pixels in this run, minus 1
  - N bits representing the color C. Then the next P pixels are filled with color C.

For encoding 5 (delta/stop):
  While there are still pixels to decode in this line:
  - 1 bit being 1 if the next run is delta encoded, and 0 otherwise.
  If the bit is 1:
  - 5 bits representing the number P of pixels in this run, minus 1 The next P pixels are filled from a copy of the pixels in the previous line, starting at the same horizontal offset, or, if this is the first line of the bitmap, they are filled with the "zero" color.
  If the bit is 0:
  - 3 bits representing the number P of pixels in this run, or 0.
  - if P is greater than zero, N bits representing the color of the pixels in this run.
  - if P is zero, the remainder of the line is filled with the zero color.

For encoding 6 (basic/zero):
  While there are still pixels to decode in this line:
  - N bits representing the color C of this run.
  - if C is the "zero" color, 8 bits representing the length P of this run, minus 1.
  - if C is not the "zero" color, 3 bits representing the length P of this run, minus 1. Then, the next P pixels are filled with color C.

For encoding 7:
  Encoding 7 is reserved for future use. A decoder encountering encoding 7 will not be able to decode the line, and thus the bitmap.