A single high-resolution photograph can be dozens of megabytes. A feature-length film in raw format can be hundreds of gigabytes. Without compression, streaming video would be impossible, email attachments would fill up inboxes in seconds, and hard drives would be exhausted in days. Compression is one of the most practical and widely used ideas in computing.
Why Compress?
Compression reduces file size by finding and eliminating redundancy in the data. Smaller files:
- Take less storage space
- Transfer faster over a network
- Use less bandwidth when streaming
- Are cheaper to store in the cloud
The trade-off is that compression and decompression take time and processing power — though modern hardware handles this almost instantly for most everyday tasks.
Lossless Compression
Lossless compression reduces file size while preserving every single bit of the original data. When you decompress the file, you get back an exact copy of the original.
Run-Length Encoding (RLE)
One of the simplest lossless techniques is run-length encoding. Instead of repeating a value many times, you store how many times it appears.
Example: the string AAAABBBCCCCCDDAA can be encoded as 4A3B5C2D2A — storing the count and the value rather than each individual character. The original is 16 characters; the encoded version is 10.
ZIP Compression
The ZIP format uses the DEFLATE algorithm, which combines two techniques:
- LZ77 — finds repeated sequences of bytes and replaces later occurrences with a reference to the first occurrence.
- Huffman coding — assigns shorter bit patterns to the most common characters, and longer ones to rare characters (similar to Morse code).
Text files, program code, and CSV files compress very well with lossless algorithms — often to 10–30% of their original size. Already-compressed files (like JPEG or MP3) show little further reduction.
Lossy Compression
Lossy compression achieves much smaller file sizes by permanently discarding data that is considered less important — usually information the human senses struggle to detect.
JPEG Image Compression
JPEG compression works by:
- Converting the image from RGB colour space to a different space that separates brightness from colour.
- Reducing the precision of colour information (humans are more sensitive to brightness than colour).
- Applying a mathematical transform (DCT — Discrete Cosine Transform) to blocks of 8x8 pixels.
- Discarding high-frequency detail (fine textures) that the eye barely notices.
- Applying lossless compression to the remaining data.
At high quality settings, a JPEG looks almost identical to the original. At low quality, you see blocky artefacts around edges.
Every time you save a JPEG, more data is lost. If you need to edit an image repeatedly, always keep the original in a lossless format (PNG or RAW) and only export to JPEG as a final step.
MP3 Audio Compression
MP3 uses perceptual coding — it exploits the limits of human hearing to discard sounds we cannot easily perceive:
- Very quiet sounds that occur at the same time as loud sounds (masking)
- Frequencies above the range of human hearing
- Subtle nuances in less perceptually important frequency bands
A typical MP3 file is roughly one-tenth the size of the equivalent uncompressed WAV, with only slight quality loss at standard bitrates.
Lossless vs Lossy: Choosing the Right Type
| Lossless | Lossy | |
|---|---|---|
| Data preserved? | 100% — exact original | No — some data permanently removed |
| File size reduction | Moderate (good for text) | Large (good for media) |
| Common formats | ZIP, PNG, FLAC | JPEG, MP3, MP4, AAC |
| Best for | Documents, code, archives | Photos, audio, video for distribution |
As a rule of thumb: use lossless compression when accuracy matters (contracts, medical images, software), and lossy compression when a good-enough quality is acceptable and file size is important (photos shared online, streaming music).
Practice Problems
A student uses run-length encoding on the string 'BBBBBAAAACCCC'. What is the encoded result?
Show answer
Explain why JPEG compression is described as lossy.