You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Data files — images, audio, video, and text — can be very large. Compression reduces the file size so that files take up less storage space and can be transmitted more quickly. This lesson covers the two main types of compression: lossy and lossless.
There are several important reasons to compress data:
Lossy compression reduces file size by permanently removing some of the data. The removed data is typically information that humans are unlikely to notice.
| Type | Formats |
|---|---|
| Images | JPEG (.jpg) |
| Audio | MP3 (.mp3), AAC (.aac) |
| Video | MP4 (.mp4), AVI (.avi) |
A 24-bit bitmap image of 1920 × 1080 pixels would be approximately 6.2 MB uncompressed. Saved as a JPEG with moderate compression, it might be reduced to 300–500 KB — a reduction of over 90%. The trade-off is a slight reduction in image quality that is usually not noticeable on screen.
Lossless compression reduces file size without losing any data. The original file can be perfectly reconstructed from the compressed version.
Lossless compression algorithms find patterns and redundancy in the data and encode them more efficiently. Two common techniques are:
RLE works by identifying consecutive repeated values and storing them as a single value plus a count.
Example:
Original data: AAAAAABBBBCCCCCCCDD
RLE encoded: 6A4B7C2D
This is much shorter — 4 pairs instead of 19 characters.
RLE works well for data with many repeated values, such as simple graphics with large areas of the same colour. It is less effective for complex images like photographs, where consecutive pixels are often different colours.
Dictionary-based methods build a dictionary (lookup table) of recurring patterns in the data. Each pattern is replaced with a shorter code that references the dictionary entry.
Example:
Original text: "the cat sat on the mat"
A dictionary might store:
The text could then be encoded using these shorter dictionary references wherever the patterns occur, reducing the overall file size.
| Type | Formats |
|---|---|
| Images | PNG (.png), GIF (.gif), BMP (.bmp with RLE) |
| Audio | FLAC (.flac), WAV (.wav) |
| General | ZIP (.zip), RAR (.rar), 7Z (.7z) |
flowchart TD
A[File to compress] --> B{Can data be lost?}
B -->|No| C[Use Lossless]
B -->|Yes| D[Use Lossy]
C --> E[PNG, FLAC, ZIP]
D --> F[JPEG, MP3, MP4]
C --> G[Original recoverable]
D --> H[Smaller file size]
| Feature | Lossy | Lossless |
|---|---|---|
| Data lost? | Yes, permanently | No, fully recoverable |
| Compression ratio | High (much smaller files) | Lower (larger files) |
| Quality | Some reduction (may be imperceptible) | Identical to original |
| Best for | Photos, music, video (where slight quality loss is acceptable) | Text, programs, medical images, archiving |
| Examples | JPEG, MP3, MP4 | PNG, FLAC, ZIP |
| Can decompress to original? | No | Yes |
The choice between lossy and lossless depends on the situation:
Exam Tip: A common exam question asks you to compare lossy and lossless compression or to recommend which type to use for a given scenario. Always explain the trade-off: lossy gives smaller files but loses data; lossless preserves all data but produces larger files. Give a specific example to support your answer.
A row from a black-and-white logo is:
1111111100000000111111110000111111110000000011111111
That is 52 characters. Counting consecutive runs:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.