Compression
What Is Compression?
Compression reduces the number of bits in a file, making it smaller. Compressed files:
- Take up less storage space
- Transfer more quickly over networks
- Allow more content to be stored on limited-capacity devices (e.g., phone storage)
Compressed files often need to be extracted/uncompressed before they can be used.
Why It Matters
- Streaming music and video over the internet is only practical because of compression
- More photos fit on a phone’s flash storage when compressed
- Downloading software is faster when files are compressed
Lossy Compression
Lossy compression reduces file size by permanently removing some data. The removed data is unrecoverable.
How It Works
- Reduces the number of colours used in an image (fewer bits per pixel)
- Groups areas of similar colour together and treats them as one
- Removes data that human perception is least sensitive to
Characteristics
| Aspect | Detail |
|---|---|
| Data loss | Yes, some original data is permanently deleted |
| Quality | Slightly reduced |
| File size reduction | Very significant |
| Reversible? | No, cannot recover original |
| Suitable for | Images, audio, video |
| Not suitable for | Text files, executable programs, documents |
Formats
- JPEG (images)
- MP3 (audio)
- GIF (images, limited colour palette)
- H.264/H.265 (video)
Info
With photographs and video, small quality losses are often imperceptible to the human eye/ear, making lossy compression a worthwhile trade-off.
Lossless Compression
Lossless compression reduces file size by encoding data more efficiently, without removing any data. The original file can be perfectly reconstructed.
How It Works (Run-Length Encoding example)
Instead of storing every pixel individually:
Original: WWWWWWWWWWWWBBBWWWWWWWW
Encoded: 12W 3B 8W (12 white, 3 black, 8 white)
This is run-length encoding (RLE), repeated values are stored as (count, value) pairs.
This is most effective on images with large areas of the same colour (cartoons, diagrams, clip art) but ineffective on photographs where colours vary pixel-to-pixel.
Characteristics
| Aspect | Detail |
|---|---|
| Data loss | None, all original data preserved |
| Quality | Unchanged |
| File size reduction | Less than lossy (but still meaningful) |
| Reversible? | Yes, fully recoverable |
| Suitable for | All file types |
| Best for | Documents, executable files, PNG images |
Formats
- ZIP (general files)
- PNG (images)
- FLAC (audio)
- GIF (images, also lossless within its limited palette)
Comparison Table
| Lossy | Lossless | |
|---|---|---|
| Data removed? | Yes | No |
| Quality change | Reduced | None |
| File size reduction | Large | Moderate |
| Reversible? | No | Yes |
| Use cases | Photos, audio, video | Docs, executables, text |
| Examples | JPEG, MP3 | ZIP, PNG, FLAC |
Choosing the Right Type
Rule of Thumb
, Use lossy for images, audio and video where minor quality loss is acceptable and maximum size reduction is needed. , Use lossless for executable files, documents and any data where no loss is acceptable, a single corrupted byte in an executable will make it fail.
Exam Questions
Q: What is the main difference between lossy and lossless compression?
Lossy compression removes some data to reduce file size, meaning the original cannot be fully recovered and quality is reduced. Lossless compression retains all original data by encoding it more efficiently, so the original can be perfectly reconstructed.
Q: Why can’t lossy compression be used on executable program files?
Executable files contain precise instructions for the CPU. Removing any data could corrupt instructions and cause the program to fail or behave incorrectly.
Q: Explain how run-length encoding works.
Instead of storing every value individually, repeated sequences are stored as a count and a value. For example, 12 white pixels followed by 3 black pixels becomes “12W 3B”, reducing the number of bits needed.
Exam Tip
MP3 vs FLAC is a good example: MP3 = lossy (smaller file, slight quality loss); FLAC = lossless (larger file, identical quality). Mentioning a specific format as an example strengthens exam answers.