Why compress at all
Compression reduces the number of bits a file occupies. That matters for three reasons the syllabus asks about: less storage space is used, files transmit faster over a network, and less bandwidth is consumed — which costs money on a metered connection.
There are two kinds, and the difference between them is the whole topic. Lossless compression allows the original to be reconstructed exactly. Lossy compression discards some data permanently in exchange for a much smaller file.
| Lossless | Lossy | |
|---|---|---|
| Original recoverable? | exactly | never |
| Size reduction | modest | large |
| Used for | text, spreadsheets, program code | photographs, music, video |
| Example formats | ZIP, PNG, FLAC | JPEG, MP3, MP4 |
| Repeated compression | safe | quality degrades each time |
Never use lossy compression on a program or a spreadsheet
Lossy compression works because the eye and the ear do not notice small changes. A program file has no such tolerance — altering a single bit can stop it running, and a changed digit in a spreadsheet is simply a wrong number. Any file where every bit matters must be compressed losslessly or not at all.
How lossless compression works
Lossless methods find repetition and describe it more briefly, without discarding anything.
Run-length encoding replaces a sequence of identical values with the value and a count: WWWWWWBBB becomes 6W3B. It works extremely well on images with large flat areas and badly on photographs, where adjacent pixels rarely match exactly.
Dictionary methods build a table of repeated sequences and replace each occurrence with a short reference to the table. Since the table is stored with the file, the original can always be rebuilt exactly.
Apply run-length encoding to AAAABBBCCCCCCD and calculate the saving, assuming one byte per character and one byte per count.
- Group the runs: four A, three B, six C, one D.Each run becomes one value and one count.
- Encoded as
4A 3B 6C 1D.Eight items — four counts and four characters. - Original
= 14bytes; encoded= 8bytes. - Saving
= 6 bytes, about 43%.Note that a single D still costs two bytes to encode — run-length encoding can make a file with no repetition LARGER.
4A3B6C1D — 8 bytes instead of 14, a saving of about 43%
How lossy compression works
Lossy methods discard information that human perception is least likely to notice, then store what remains.
For images, a JPEG reduces the precision of colour information — the eye is far more sensitive to brightness than to fine colour detail — and merges similar nearby colours. For sound, an MP3 removes frequencies outside human hearing and quieter sounds masked by louder ones occurring at the same moment.
The reduction is dramatic: a JPEG is routinely a tenth of the size of the equivalent bitmap. What is discarded is gone permanently, and compressing an already-compressed file again degrades it further.
Generation loss
Open a JPEG, edit it, save it, and repeat. Each save discards a little more, and after enough cycles the image visibly deteriorates. This is why photographers keep an uncompressed or losslessly compressed master and export lossy copies from it, rather than editing the JPEG itself.
Storage devices and units
Compression is one answer to limited storage; the other is choosing the right medium. Each has different characteristics and the syllabus expects the comparison.
Magnetic storage — hard disks — uses magnetised regions on spinning platters. Large and cheap per gigabyte, but the moving parts make it slower and vulnerable to shock. Optical storage — CD, DVD, Blu-ray — uses pits burned into a surface and read by laser. Cheap, portable and durable, but slow and small by modern standards. Solid state storage — SSDs and flash drives — has no moving parts, so it is fast, silent and shock-resistant, but costs more per gigabyte and each cell tolerates only a limited number of writes.
Before you leave this chapter
- Compression saves storage, transmission time and bandwidth.
- Lossless recovers the original exactly; lossy discards data permanently.
- Text, spreadsheets and program code must be lossless. Photos, music and video can be lossy.
- Run-length encoding replaces runs with a value and a count — and can enlarge a file with no repetition.
- Magnetic is cheap and large, optical is portable, solid state is fast with no moving parts.
Reducing a file without a compression algorithm
Compression is not the only way to make a media file smaller. The settings that determine its size in the first place can simply be reduced, and the syllabus expects both routes.
For an image: fewer pixels, or fewer bits per pixel. For a sound: a lower sample rate, a lower resolution, or mono instead of stereo. Each reduces the file in direct proportion, and each loses something specific — detail, colour accuracy, high frequencies or stereo separation.
Drag sample rate down and the samples no longer follow the wave. Drag resolution down and each sample lands further from the true height. Both shrink the file, and both are visible as a worse reconstruction.
Reducing settings is lossy too
Halving the sample rate is a permanent loss just as much as MP3 compression is — the discarded measurements cannot be recovered. The difference is that a compression algorithm chooses which information to discard using knowledge of human perception, whereas reducing the settings discards indiscriminately. That is why an MP3 at a given size sounds better than a raw recording reduced to the same size.