Compression: lossy and lossless

Computer Science · GCSE · The School

Smaller files cost less to store and send

A file takes up space on a disk and time to send over a network. COMPRESSION reduces that size. A photo attached to an email compresses in seconds; sending the uncompressed original could take minutes. Storage and bandwidth both cost money, which is why almost everything you download has already been compressed.

Lossless: the original comes back exactly

LOSSLESS compression removes nothing. It finds patterns and stores them more efficiently, then reverses the process perfectly when the file is opened again. A text document, a program file and a spreadsheet all need lossless compression, because a single wrong character can break them.

Run-length encoding: counting repeats

RUN-LENGTH ENCODING (RLE) replaces a run of the same value with a count and the value. AAAAABBBCCCCCCCC becomes 5A3B8C — three pairs instead of sixteen characters. It works well on data with long repeated runs, such as a simple graphic with large blocks of one colour, and works badly on data that never repeats.

Lossy: discarding what is hardest to notice

LOSSY compression throws data away permanently, chosen because human eyes and ears rarely notice it is gone. A JPEG photo discards fine colour detail the eye is poor at detecting; an MP3 discards frequencies most ears cannot hear well. The saving is much bigger than lossless alone achieves, and the loss cannot be undone by decompressing again.

Choosing lossy or lossless for the job

A program file, a legal document or a medical scan needs lossless: every bit matters and errors are unacceptable. A holiday photo or a streamed song can accept lossy: a slightly smaller file matters more than a difference nobody hears. GCSE questions expect you to justify the choice against the SPECIFIC file, not state a general preference.

Tracing RLE both ways

Encode AAAAAABBBAAAAAAAAAAAA: 6A3B12A — twenty-one characters become eight. Decode 4X2Y5Z: write X four times, Y twice, Z five times — XXXXYYZZZZZ. RLE only saves space when runs are long; encoding ABCABC as 1A1B1C1A1B1C makes the file LONGER than the original, which is why real compressors check first.

The School — all subjects · Knowledge map