On this page
Run-length encoding is one of the simplest compression ideas, and it still runs inside fax machines, old image formats and JPEG photos. A run is a stretch of one repeated symbol.
How does run-length encoding work?
Run-length encoding replaces each run of identical symbols with one pair: how long the run is, and what it is made of. AAAABBB becomes 4A3B. Decoding writes each symbol that many times, so you get back the original exactly: it is lossless.
It only wins when runs are long. AAAAAAAAAA becomes 10A; ABCD becomes 1A1B1C1D, longer than what you started with. Whether any compressor can shrink a file depends on how predictable the data is, which is exactly what Shannon entropy measures in bits.
A worked fax example: one row of black-and-white pixels
The Group 3 fax standard, ITU-T Recommendation T.4, scans a page as lines of 1728 black and white picture elements along a 215 mm line. In its one-dimensional scheme, each code word stands for a run of all white or all black pixels, the colours alternate, and every line begins with a white run; if a line actually starts with black, a white run of length zero is sent first.
Take a 32-pixel row: 10 white, 4 black, 8 white, 6 black, 4 white. Encoding it with T.4 Table 2 (the terminating codes) works like this:
Look up each code word: white 10 is 00111, black 4 is 011, white 8 is 10011, black 6 is 0010, white 4 is 1011.
Join them: 00111 011 10011 0010 1011, which is 21 bits.
Compare: stored raw at one bit per pixel, the row needs 32 bits.
To decode, read code words in turn, switching colour after each one, starting with white.
One catch: T.4 follows every line with a 12-bit end-of-line code, 000000000001, so a row this short gains nothing overall. The saving comes from long runs. T.4 codes runs of 64 to 1728 pixels as a make-up code followed by a terminating code. A completely blank line is white 1728 (make-up code 010011011, 9 bits) plus white 0 (00110101, 8 bits): 17 bits, or 29 with the end-of-line code, for 1,728 pixels, about a sixtieth of the raw size. Mostly blank pages compress enormously.
When does run-length encoding make files bigger?
When runs are short, the counts cost more than they save. The worst case is a checkerboard: an alternating 32-pixel row needs 16 white-1 codes (000111) and 16 black-1 codes (010) from T.4 Table 2, which is 144 bits for 32 pixels.
Random data is nearly as bad. In a row of n random pixels, each neighbouring pair differs with probability 1/2, so by linearity of expectation with indicator variables the expected number of runs is 1 + (n - 1)/2. Each run length roughly follows a geometric distribution with mean 2. For a 1,728-pixel fax line that is 864.5 runs. Weighting the T.4 code lengths by those probabilities (our calculation), a random white run costs about 5 bits and a black run about 2.7 bits, so the line comes to roughly 3,300 bits: nearly twice the 1,728 raw bits.
A naive byte-level RLE, one count byte plus one value byte, does the same on random bytes. Neighbours match only 1 time in 256, so almost every byte becomes a two-byte pair and the file nearly doubles.
A quick decision guide:
Compresses well: scanned text, line drawings, icons, screenshots with flat colour, masks.
Compresses badly: photographs, noise, checkerboard patterns, files that are already compressed.
Rule of thumb: if a (count, value) pair costs two symbols, runs must average more than two symbols for RLE to win.
How do PackBits and JPEG use run-length encoding?
PackBits fixes the random-data problem with literal packets. In Apple's Technical Note TN1023, a flag-counter byte comes first: a negative value n means the next byte repeats 1 - n times, and a positive value n means n + 1 incompressible bytes follow as they are. Because unrepeated bytes travel as one block, Apple gives the worst case as about one extra byte per 127 bytes of input. The scheme was introduced with MacPaint and can be used in TIFF files, according to the PackBits article on Wikipedia.
JPEG uses run-length coding on zeros. The standard, ITU-T T.81, approved on 18 September 1992, lists the quantized DCT coefficients of each 8 x 8 block in zig-zag order and, because many are zero, codes the runs of zeros. Each nonzero AC coefficient gets an 8-bit value RRRRSSSS, where RRRR counts the zeros before it. X'F0' (ZRL) stands for 16 zeros, and 00000000 marks end-of-block: every remaining coefficient is zero. In the Huffman mode those values are then Huffman coded, so RLE is one stage of JPEG, not the whole of it.
Where to practise the maths behind it
The expected-runs argument rests on indicator variables, the geometric distribution and entropy. If any feels shaky, the Fermi Concept Library has a lesson for every concept in mathematics and computing. To check your working on a similar problem, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer.



