Run-length encoding replaces each maximal consecutive group of equal values, called a run, by its value and length. Decoding repeats each value the specified number of times. It is lossless source coding: no values are discarded. For a binary string, successive maximal runs alternate between 0 and 1, so storing the first bit and all run lengths is enough.
Take this raster image, with 0 black and 1 white, read left to right across each row and then top to bottom:Reading the whole image as one 64-bit binary string gives exactly seven runs:The lengths sum to . Repeating the alternating values for these lengths and splitting after every eighth pixel reconstructs every displayed row exactly.
00000000
00001111
11110000
00001111
11110000
00001111
11110000
00000000Here is a complete toy file format. Its file header is an eight-bit unsigned width, an eight-bit unsigned height, then a one-bit mode: 0 means raw pixels, 1 means run-length encoding. Fields are written most significant bit first; this profile accepts only width and height 8, so both decoders know to stop at 64 pixels. Raw mode contains 64 bits. RLE mode contains the initial bit, followed by seven-bit unsigned run lengths in the range 1 to 64. Zero and lengths above 64 are invalid; a decoder rejects a run that would exceed the remaining pixel count. It stops when the decoded lengths sum to 64, so no run-count field or end marker is needed.
For this image, the complete RLE serialization is:Spaces and vertical bars are explanatory separators, not stored characters. The seven-bit fields directly store the lengths; 64 would be
00001000 00001000 1 | 0 | 0001100 0001000 0001000 0001000 0001000 0001000 00011001000000. The payload costs bits, versus 64 raw bits. The 17-bit file header has the same size in either mode, giving complete sizes of 67 versus 81 bits. If packed into eight-bit units, append zero padding only at the end: the sizes become 72 versus 88 bits. These are sizes of this toy representation, not sizes of PNG files.The field design matters. Restarting RLE on every row costs eight initial bits plus four bits per run when lengths are stored directly: three bits represent only 0 through 7, whereas a row can have length 8. This pattern has row-run counts , totalling 14, so that payload costs bits, with no saving. Storing length minus one would instead fit lengths 1 through 8 into three bits, but is a different declared file format. An alternating flat 64-bit binary string has 64 runs and costs payload bits in our flat format; restarting each row costs payload bits. A one-run image costs only eight payload bits. These extremes explain when RLE is useful: long uninterrupted regions make short instructions, while frequent changes make instructions expensive.
Prefix codes of unequal lengths can replace fixed-width run lengths, provided the decoder knows the code and any necessary code description is counted. Run-length encoding names a technique, not one universal serialization. For a real implementation, Microsoft's bitmap documentation specifies
BI_RLE8 with count and value fields plus special commands, rather than our initial-bit convention.[ref]https://learn.microsoft.com/en-us/windows/win32/gdi/bitmap-compression. Microsoft, Bitmap Compression, BI_RLE8 encoded and absolute modes
Articles by others on the same topic
Run-length encoding (RLE) is a simple data compression technique that represents sequences of identical values (or "runs") in a more compact form. The basic principle of RLE is to replace consecutive occurrences of the same data value with a single value and a count of how many times that value occurs consecutively. ### How It Works 1. **Input**: Take a sequence of data that has repeated values.
