Data Compression
What is Data Compression?
Definition:
Data compression is the process of reducing the size
of a file so that it uses less storage space and can be
transmitted more quickly.
The compressed file contains the same information in
a smaller amount of data.
Advantages of Compression
1. Saves Storage Space
• More files can be stored on a device.
• Reduces storage costs.
2. Faster File Transfer
• Smaller files take less time to send over a
network.
3. Faster Downloads and Uploads
• Less data needs to be transmitted.
4. Reduces Bandwidth Usage
• Useful for websites and streaming services.
pg. 1
Disadvantages of Compression
1. Compression Takes Processing Time
• The computer must compress and decompress
files.
2. Quality May Be Lost
• With lossy compression, some data is
permanently removed.
3. Additional Software May Be Required
• Some compressed files need special software to
open.
4. Repeated Compression Can Reduce Quality
• Especially with images, audio, and video.
Types of Compression
There are two main types:
1. Lossless Compression
Definition
No data is lost during compression.
The original file can be reconstructed exactly.
Suitable For
• Text files Program files
• Databases Spreadsheets
Examples ZIP, PNG, GIF, RLE
pg. 2
2. Lossy Compression
Definition
Some data is permanently removed to achieve a
smaller file size.
The original file cannot be perfectly reconstructed.
Suitable For
• Photos
• Audio
• Video
Examples
• JPEG/JPG, MP3, MP4, MPEG
Comparison
Feature Lossless Lossy
Data lost? No Yes
Original file restored
Yes No
exactly?
Compression ratio Lower Higher
Quality loss None Possible
Text, Images, audio,
Used for
programs video
pg. 3
Run Length Encoding (RLE)
What is RLE?
Run Length Encoding (RLE) is a lossless compression
technique that replaces repeated data with the
number of times it occurs and the value itself.
RLE with Text
Original Text: AAAAABBBBCCCCCCDD
Compression
5A4B6C2D
Explanation:
AAAAA → 5A
BBBB → 4B
CCCCCC → 6C
DD → 2D
The repeated characters are replaced by:
Count + Character
Another Example Original:
HHHHHHHHHH
Compressed:
10H
pg. 4
How RLE Works with Images
RLE works best when neighboring pixels have the
same color.
Original Pixel Row
BBBBBWWWWRRRRRR
(B = Black, W = White, R = Red)
Compressed:
5B4W6R
Instead of storing every pixel individually, we store:
Number of pixels + Color
This greatly reduces file size when large areas have
the same color.
Why RLE is Good for Images
Works well for:
• Logos
• Icons
• Cartoons
• Simple graphics
Does not work well for:
• Photographs
• Complex images
because colors change frequently.
pg. 5
File Types and Compression Types
Lossless Compression File Types
File Type Used For
ZIP General file compression
PNG Images
GIF Images and animations
BMP (RLE version) Images
TIFF (optional compression) High-quality images
TXT inside ZIP Text files
Lossy Compression File Types
File Type Used For
JPEG / JPG Photographs
MP3 Audio
AAC Audio
MP4 Video
MPEG Video
WebP (lossy mode) Images
pg. 6
Exam Question Example
Question
What type of compression should be used for a text
document?
Answer
Lossless compression, because no data can be lost
and the original file must be reconstructed exactly.
Question
What type of compression is normally used for
photographs?
Answer
Lossy compression, such as JPEG, because small
losses in image quality are usually not noticeable and
much smaller file sizes can be achieved.
Easy Exam Notes
Lossless Compression
• No data lost.
• Original file recovered exactly.
• Example: ZIP, PNG, GIF, RLE.
Lossy Compression
• Some data lost permanently.
pg. 7
• Smaller file size.
• Example: JPEG, MP3, MP4.
RLE
• Lossless compression.
• Stores repeated values as:
o Count + Value
• Example:
o AAAAAA → 6A
o BBBBWWWWRRR → 4B4W3R
pg. 8