AQA 7517, section 4.5.6.9
Data compression means storing data in fewer bits than the original, so that it takes up less space.
Why compress?
- Save storage space. More files fit on a device.
- Transfer files faster. Smaller files download, upload and stream more quickly, and use less bandwidth.
- Reduce costs for storage and data transfer.
Images and sound are compressed very often, because their raw files are huge (a raw photo or CD audio is many megabytes). Other files, such as text files, can be compressed too, because they contain repeated patterns.
Lossless and lossy compression
Lossless compression
Lossless compression reduces the size without losing any data. When the file is decompressed, you get back exactly the original, bit for bit.
- Used for text, programs, spreadsheets and any data where every bit matters, and for images such as PNG.
- Examples: ZIP, PNG, FLAC.
- Advantage: no loss of quality, and the original can be rebuilt perfectly.
- Disadvantage: the file doesn't get as small as with lossy compression.
Lossy compression
Lossy compression makes the file smaller by permanently removing some data, usually the parts people are least likely to notice. The original can't be recovered.
- Used for photographs, music and video.
- Examples: JPEG, MP3, MPEG video.
- Advantage: much smaller files, often 10 times smaller.
- Disadvantage: quality is lost, and more compression means worse quality. Repeated saving makes it worse, and it can't be used where exact data is needed.
| Lossless | Lossy | |
|---|---|---|
| Data lost? | No | Yes |
| Original recovered? | Exactly | No, only an approximation |
| File size | Smaller | Much smaller |
| Suitable for | Text, code, data | Photos, audio, video |
| Examples | ZIP, PNG | JPEG, MP3 |
Run length encoding (RLE)
Run length encoding is a lossless method. It replaces a run of identical values with one copy of the value and a count of how many there are.
Text example: AAAABBBCCDAA has runs of 4 A, 3 B, 2 C, 1 D, 2 A.
The 12 characters became 10. It works best when there are long runs.
Bitmap example: one row of a black and white image, 000000111100000000 (6 white, 4 black, 8 white), could be stored as the counts 6, 4, 8, instead of 18 bits. A picture with big areas of the same colour, such as a cartoon or a logo, compresses well.
def rle(text):
result = ""
i = 0
while i < len(text):
count = 1
while i + count < len(text) and text[i + count] == text[i]:
count = count + 1
result = result + str(count) + text[i]
i = i + count
return result
print(rle("AAAABBBCCDAA")) # 4A3B2C1D2A
Weakness: if there are no runs, RLE can make the data bigger. ABCD becomes 1A1B1C1D, which is twice the size. It suits some data but not all.
Dictionary-based methods
Dictionary-based compression is a lossless method that builds a dictionary of repeated sequences of data. Each time a sequence appears, it is replaced by a short reference to its entry in the dictionary.
Example: take the sentence
the cat and the hat and the bat
Create a dictionary of repeated words:
| Code | Entry |
|---|---|
| 1 | the |
| 2 | and |
The text becomes 1 cat 2 1 hat 2 1 bat. Each reference is shorter than the word it replaces, and the more often a sequence repeats, the more is saved. The dictionary is stored with the compressed data (or rebuilt while decompressing), so the original can be restored exactly.
Methods such as LZW (used in GIF and ZIP-type formats) build the dictionary as they go, so no dictionary has to be sent. It works well on text, where words and phrases repeat, and on any data with repeating patterns.
Worked example
Task: compress the bitmap row WWWWWWWWBBBWWWWWWWWWWWWBW using run length encoding. Compare the sizes.
Runs: 8 W, 3 B, 12 W, 1 B, 1 W.
Original: 25 characters. Compressed: 11 characters, which is 44% of the original size. The data can be rebuilt exactly, so this is lossless.
Exam tip: know both types, with a reason to use each. Lossless gives back the exact original (text, code), and lossy gives much smaller files by discarding detail (photos, music). To show you understand RLE, write the encoded form and note that it works best with long runs.
Check yourself
Give two reasons for compressing files
To save storage space, and to reduce transfer time and bandwidth.
What is the difference between lossless and lossy compression?
Lossless compression allows the exact original data to be recovered. Lossy compression permanently removes some data, so only an approximation of the original can be recovered.
Compress QQQQRRSSSSSS using run length encoding
4Q2R6S.
How does a dictionary-based method reduce file size?
It replaces repeated sequences with short references to entries in a dictionary.