1. Run-Length Encoding (RLE)

The simplest compression algorithm

Imagine you need to describe a row of colored tiles to someone over the phone. You could say each tile's color one at a time: "white, white, white, white, white, red, red, white, white, white..." Or you could say: "five white, two red, three white..." The second way is shorter. That's Run-Length Encoding.

RLE is the simplest form of lossless compression. Wherever you have consecutive repeated values — a run — you replace the entire run with two pieces of information: what the value is and how many times it repeats.

A Simple Example

Input (19 characters)

A A A A A A B B B C C D D D D D D D D

Written out: AAAAAABBBCCDDDDDDDD

Encoded (8 characters)

6A 3B 2C 8D

Each pair says "repeat this character N times." 6A means "six A's", 3B means "three B's", and so on.

Original

19 characters

Compressed

8 characters — 58% smaller

How the Algorithm Works

  1. Read the first value

    Note it as the "current value" and set a counter to 1.

  2. Look at the next value

    If it's the same as the current value, increment the counter.

  3. If it's different (or you've reached the end)

    Output the counter followed by the current value. Then start fresh with the new value and counter = 1.

  4. Repeat until you've processed all the data

    Each run of identical values becomes a (count, value) pair in the output.

Pseudocode

function rle_encode(input):
    output = ""
    i = 0

    while i < length(input):
        current = input[i]
        count = 1

        while i + count < length(input) AND input[i + count] == current:
            count = count + 1

        output = output + str(count) + current
        i = i + count

    return output

Decoding is even simpler: read a number, read a character, output that character that many times. Repeat.

Binary RLE — Compressing Bytes

The text example above is for illustration. In practice, RLE operates on bytes (or bits). Consider a row of pixels in an image:

A mostly-white image row (pixel values)

FF FF FF FF FF FF FF FF 00 00 00 FF FF FF FF FF
(8 white)              (3 black) (5 white)

RLE-encoded (count, value pairs)

08 FF  03 00  05 FF

16 bytes → 6 bytes. Each pair is one byte for the count and one byte for the pixel value.

One practical detail: if a run is longer than 255 (the max value of a single byte), you split it into multiple (255, value) pairs. Different formats handle this in different ways.

When RLE Works Well

  • Simple graphics — logos, icons, diagrams with large solid-colored regions
  • Fax machines — early fax transmission used RLE because scanned documents are mostly white with occasional black lines of text
  • Sparse data — scientific datasets with long runs of zeros
  • Sorted data — sorting brings identical values together, creating runs
  • As a stage in a pipeline — after the Burrows-Wheeler Transform (page 6), data has lots of runs, and RLE is a natural next step

When RLE Fails

Critical Insight

Compression algorithms make assumptions about the data. RLE assumes the data has runs of repeated values. When that assumption is wrong, the compressed output can be larger than the input.

No runs to exploit

Input:    A B C D E F G H      (8 characters)
Encoded:  1A 1B 1C 1D 1E 1F 1G 1H  (16 characters!)

Every "run" has length 1, so each character now costs two characters. The encoded output is twice as large as the original.

This is a fundamental lesson in compression: there is no universal compressor that can make every possible input smaller. Any compression algorithm that shrinks some inputs must necessarily expand others. (This is provable by a simple counting argument — there are more possible inputs of length N than there are shorter strings to map them to.)

Practical RLE implementations handle this by adding escape codes or flag bytes to distinguish runs from literal (non-run) data, reducing the overhead for non-compressible sections.

Real-World Usage

Format How it uses RLE
PCX Image format from the 1980s — uses byte-level RLE as its only compression
BMP Windows bitmap — supports optional RLE for 4-bit and 8-bit images
TIFF Supports "PackBits" — a variant of RLE
TGA Targa image format — optional RLE encoding
bzip2 Uses RLE as one stage in a multi-stage pipeline (after BWT and MTF)
JPEG Uses RLE on sequences of zero-valued DCT coefficients

Key Takeaways

  • RLE replaces consecutive repeated values with (count, value) pairs
  • It's the simplest possible compression algorithm — easy to understand, trivial to implement
  • Works beautifully on data with long runs; can make data larger if there aren't runs
  • All compression algorithms have assumptions — when the assumptions don't hold, compression fails
  • RLE is a building block used as one stage inside more advanced compressors

Try It Yourself — Interactive RLE Encoder

Type any string below to see how RLE encodes it, with a visual run map and compression statistics.

Encoded Output

Run Map

Compression Statistics


RLE finds one kind of redundancy: consecutive repetition. But what about data where some values are just more common than others, even if they don't appear in runs? That's the problem Huffman coding solves.