Count a stretch, then write it once
A run is a stretch of the same symbol with no break. Run-length encoding stores the count and the symbol once for that stretch. The symbol is not stored again and again.
- Start at the left.
- Count how many times that symbol repeats before it changes.
- Write the count, then the symbol. The pair
5Ameans A, five times. - Start the next run on the next symbol.
The picture uses the same rule, one row at a time. A new row starts a new run. Five A’s become 5A, which is two symbols instead of five. One A becomes 1A, which is two symbols instead of one, so the encoded text is longer.
| Example | Original | Encoded |
|---|---|---|
| Repeated | AAAAABBBCC10 symbols | 5A 3B 2C6 symbols |
| Mixed | ABCD4 symbols | 1A 1B 1C 1D8 symbols |
| Picture | 8 by 4 squares 32 pixels | 8 pairs 16 symbols |
A count and a symbol are two symbols. A pixel is one symbol.
Run the simulation
Look at the highlighted run, then at the pair that is written.
A run, a pair, then a check
The same rule is used on letters and on each row of the picture.
A run
A run is the same symbol repeated with no break. A new letter, a new colour, or a new row ends it.
Count, then symbol
Each pair stores the count first, then the letter or the colour. Both of those count as symbols.
Not always shorter
Long runs use fewer symbols. A run of 1 still needs a count and a symbol, so the encoded form can be longer.