Data Compression: Lossless and Lossy
AP Computer Science Principles · Unit 2 — Data
Why compress, and the basic trade-off
Data compression reduces the number of bits needed to store or transmit data. Fewer bits mean less storage and faster transfer, which is why nearly every photo, song, video and web page you receive has been compressed. How much a file can be reduced depends on how much redundancy it contains and on the algorithm used; a file of random bits can barely be compressed at all.
There are two families of algorithm.
- Lossless compression reduces the number of bits while guaranteeing complete reconstruction of the original data. Decompressing gives back every bit exactly. It is required when any change is unacceptable: text, program files, spreadsheets, financial records, and images that will be measured or analysed precisely, such as medical scans or legal documents.
- Lossy compression significantly reduces the number of bits by discarding information, usually details people are unlikely to notice, so only an approximation of the original can be reconstructed. It typically achieves much greater reduction. It suits photos for a website, streaming video and music, where file size or speed matters more than perfect fidelity.
The decision rule the exam wants: if the original must be restored exactly, use lossless; if minimizing size or transmission time is most important and an approximation is acceptable, lossy is usually the better choice. Lossy compression cannot be undone — once details are discarded, re-saving at higher quality does not bring them back.
Two lossless techniques you can compute by hand
Run-length encoding (RLE) replaces each run of identical values with a pair: the count and the value. The text AAAABBBCCD (10 characters) becomes 4A3B2C1D (8 characters); decoding expands each pair back. If each pair is stored in a fixed number of bytes, every implementation also has a maximum run length: with a one-byte count, a run of 300 white pixels must be split into 255 and 45. Data with few repeats can grow: ABAB becomes 1A1B1A1B, twice as long.
Dictionary (substitution) compression replaces a frequently repeated pattern with a short symbol and stores the pattern once in a table. If an 8-character phrase appears 40 times in a 1,000-character text and each occurrence is replaced by a 1-character symbol, the text shrinks by $40 \times (8-1)=280$ characters, but the table must also be stored: 8 characters for the phrase plus 1 for its symbol. The compressed total is $1000-280+9=729$ characters. Forgetting the table is the most common error in these calculations, and for a phrase that appears only once or twice the table can cost more than the substitution saves.
Both techniques are lossless: the decoder reproduces the input exactly. Lossy techniques, in contrast, work by measuring which details matter least to a human — subtle colour differences, sounds masked by louder sounds — and discarding them.
Worked example: Chọn kiểu nén và tính kích thước
A law office scans signed contracts as black-and-white images and sends them to a court. One scanned row is WWWWWWWWBBBWWWWWWWBW (W = white pixel, B = black pixel). Uncompressed, each pixel uses 1 byte. The office's RLE stores each run as 2 bytes (count, colour).
(1) Should the office use lossless or lossy compression? Justify. (2) Encode the row and give its compressed size. (3) Describe a row of 20 pixels that RLE would make larger.
Solution
(1) Không mất. Hợp đồng đã ký là văn bản pháp lý; một nét chữ ký hay dấu chấm thập phân bị nhoè có thể đổi ý nghĩa, và toà cần bản đúng từng điểm ảnh. Ở đây độ chính xác quan trọng hơn dung lượng.
(2) Các loạt: 8 trắng, 3 đen, 7 trắng, 1 đen, 1 trắng → 8W 3B 7W 1B 1W. Năm cặp × 2 byte = 10 byte, so với 20 byte ban đầu, tức giảm một nửa.
(3) Hàng xen kẽ BWBWBWBWBWBWBWBWBWBW: 20 loạt dài 1, thành 20 cặp = 40 byte, gấp đôi bản gốc. Hàng nào có trung bình mỗi loạt ngắn hơn 2 điểm ảnh thì RLE kiểu này làm to ra.