CIE AS & A Level Computer Science, section 1.3 (Compression)
Run-length encoding (RLE)
Run-length encoding is a lossless method that replaces a run of identical values with the value and a count.
The original has 10 characters and the encoded version has 8. RLE works best on data with long runs, and can make the data bigger if there are few repeats.
Text files
Text can be compressed losslessly, for example by RLE or by replacing common words and patterns with shorter codes.
Bitmap images
- Lossless: RLE, because images often have runs of the same colour (a row of white pixels becomes "10W"). A row becomes .
- Lossy: methods like JPEG reduce the colour detail and combine similar neighbouring pixels. This gives large savings for photographs.
Vector graphics
A vector graphic is already stored as a small list of objects and properties, so it needs little compression. A more efficient description (reusing shapes) can reduce it further.
Sound files
- Lossy: MP3 removes sounds that humans are less able to hear (very high or low frequencies, and quiet sounds masked by louder ones), and can reduce the sampling resolution or rate.
- Lossless: formats such as FLAC reduce the size without any loss.
Common mistake: saying RLE always makes a file smaller. If there are few repeated values (such as ABCD, which becomes 1A1B1C1D), the encoded data can be larger than the original.
Exam tip: when asked how a file is compressed, name the method (such as RLE or JPEG), say whether it is lossy or lossless, and describe what it does to the data.
Check yourself
Apply RLE to AAABBBBC.
.
Why does RLE work well for a simple cartoon image?
It has large areas of the same colour, giving long runs.
How does MP3 compression reduce file size?
It discards sounds that are hard to hear and reduces detail, which is lossy compression.