Compression
What compression is and why we use it
Compression means reducing the number of bits needed to store a file. Smaller files:
- take up less storage space;
- transfer faster over a network and use less bandwidth;
- download/stream more quickly and cost less to send.
There are two types: lossy and lossless.
Lossy compression
Lossy compression permanently removes some data — usually detail the human eye or ear won't easily notice.
- The file cannot be restored to its exact original; some quality is lost forever.
- Achieves much smaller file sizes.
- Used for images, audio and video where a small quality drop is acceptable.
- Examples: JPEG (images), MP3 (audio), MP4 (video).
Lossless compression
Lossless compression reduces size without losing any data — the original can be perfectly reconstructed.
- File sizes are reduced less than with lossy.
- Essential where every bit matters: text documents, program code, spreadsheets.
- Examples: PNG (images), FLAC (audio), ZIP (general files).
Two lossless techniques you must know
Run-Length Encoding (RLE)
RLE replaces runs of repeated values with a single value and a count.
Instead of storing:
W W W W W B B W W W
RLE stores it as pairs of (value, number of repeats):
5W 2B 3W
- Works brilliantly when data has long runs of the same value (e.g. simple graphics with large blocks of one colour).
- Works badly — and can even make files bigger — when there are few repeats (e.g. a detailed photo).
Huffman coding (dictionary/frequency-based)
- The most frequently used characters are given the shortest binary codes; rare characters get longer codes.
- A frequency table / tree records which code means which character, so the file can be rebuilt exactly.
- Reduces overall size because common characters no longer waste a full byte each.
Worked example
A row of pixels reads AAAAAABBBBCCCCCCCC. Show it using run-length encoding and state the saving.
1. Runs: six A, four B, eight C.
2. RLE: 6A 4B 8C.
3. Original = 18 characters; RLE stores 3 value+count pairs — a large saving because the runs are long. ✓
Choosing the right type
| Need | Use | Example |
|---|---|---|
| Exact copy essential | Lossless | text, code, spreadsheets |
| Small size, minor quality loss OK | Lossy | streaming music, web photos |
Common mistakes
- Saying lossy "deletes the whole file" — it removes some detail, not everything.
- Claiming lossless gives the smallest files — lossy usually compresses more.
- Forgetting that RLE can increase size on data with few repeats.
- Mixing up examples (JPEG/MP3 = lossy; PNG/FLAC/ZIP = lossless).
Exam tips
- Learn one clear definition and one example for each type — that's often 2 easy marks.
- If asked to justify a choice, link it to the use case (e.g. "text must be lossless so no words are lost").
- For an RLE question, show the (value, count) pairs clearly and comment on whether it saves space for that data.
Key facts to remember
- Compression reduces file size → less storage, faster transfer, less bandwidth.
- Lossy removes data permanently (smaller files, some quality lost) — JPEG, MP3.
- Lossless keeps all data (perfect rebuild, less shrinkage) — PNG, FLAC, ZIP; techniques include RLE and Huffman coding.