OCR H446, section 1.3.1 (b)
Both methods give lossless compression: the original can be rebuilt exactly. They work by removing redundancy, which means data that is repeated.
Run length encoding (RLE)
A run is a sequence of identical values. RLE replaces each run with one copy of the value and a count of how many times it repeats.
Text example
Original: AAAABBBCCDAA
Runs: 4 A, 3 B, 2 C, 1 D, 2 A
Encoded: 4A3B2C1D2A
The original has 12 characters, and the encoded version has 10.
Bitmap example
A black and white image is made of rows of pixels. Suppose that W is white and B is black. One row of 24 pixels:
WWWWWWWWWWBBBBWWWWWWWWWW
becomes 10 W, 4 B, 10 W. Stored as the counts and the colour, it takes only a few values instead of 24. Pictures with large areas of one colour, such as cartoons, icons, diagrams and scanned text, compress very well.
Decoding
Read each count and repeat the value that many times: 4A3B2C1D2A becomes AAAA BBB CC D AA, which is AAAABBBCCDAA.
Strengths and weaknesses
- Simple and quick, and needs little memory.
- Excellent when there are long runs.
- If there are few repeats, it can make the data bigger:
ABCDbecomes1A1B1C1D, which is twice the size.
Dictionary coding
Dictionary coding builds a dictionary of sequences that occur in the data. Each time a sequence appears, it is replaced with a short code that points to its dictionary entry. The more often a sequence repeats, the more is saved.
A simple example
The sentence: the cat and the hat and the bat
A dictionary of repeated words:
| Code | Entry |
|---|---|
| 1 | the |
| 2 | and |
The coded text is 1 cat 2 1 hat 2 1 bat. Each reference is shorter than the word it replaces. To decode, the dictionary is used to put the words back. The dictionary must be stored or sent with the data (or rebuilt as the data is read).
Some methods, such as LZW (used in GIF images), build the dictionary as they compress, so it doesn't need to be sent: the decoder rebuilds the same dictionary as it reads the codes.
Comparison
| Run length encoding | Dictionary coding | |
|---|---|---|
| Finds | Runs of identical consecutive values | Repeated sequences anywhere in the data |
| Output | Count and value pairs | Codes pointing to dictionary entries |
| Good for | Images with large blocks of one colour, simple data | Text, and data with repeating patterns |
| Complexity | Very simple | More complex |
| Poor when | There are few runs | The data has no repeating patterns |
| Lossless? | Yes | Yes |
| Used in | Simple image formats, fax | ZIP, GIF, PNG |
Worked example
Task: compress the bitmap row 000000111100000000 (6 zeros, 4 ones, 8 zeros) using RLE, and find the saving.
- The runs are: six 0s, four 1s, eight 0s.
- Encoded:
6 0, 4 1, 8 0, written as the counts with the first value, such as 0: 6, 4, 8, or as the pairs(6,0)(4,1)(8,0). - The original has 18 bits. If each count is stored in 5 bits (enough for up to 31) and the value in 1 bit, the three runs take bits, the same size. For longer rows with longer runs, the saving becomes large.
This shows that RLE is only useful when the runs are long enough that a count is shorter than the run it replaces.
Exam tip: for RLE, write out the runs and show the encoded form (such as 4A3B2C). For dictionary coding, say it replaces repeated sequences with short codes stored in a dictionary. Both are lossless, and say when each works badly (RLE with no runs, dictionary with no repeats).
Check yourself
Apply run length encoding to WWWBBWWWWW
3W2B5W.
Decode 2A4B1C
AABBBBC.
How does dictionary coding reduce file size?
It replaces sequences of data that occur repeatedly with short codes that refer to entries in a dictionary.
Why can RLE make some data larger?
If there are few or no runs of identical values, every item is stored with a count as well, so the encoded data takes more space than the original.