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.

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.

Mã hoá loạt dài (RLE): thay mỗi loạt giống nhau bằng cặp (số lượng, giá trị)8W3B7W1B1W20 điểm ảnh × 1 byte = 20 byte → 8W 3B 7W 1B 1W = 5 cặp × 2 byte = 10 bytegiải nén ra đúng 20 điểm ảnh ban đầu, không sai một điểm: nén không mất dữ liệuhàng xen kẽ BWBWBW… cho mỗi điểm một cặp: 20 điểm thành 40 byte, to gấp đôi
Mã hoá loạt dài. Dữ liệu càng có nhiều loạt giá trị giống nhau liền nhau thì nén càng tốt. Dữ liệu đổi giá trị liên tục thì RLE làm tệp to ra — không thuật toán nén không mất nào thu nhỏ được mọi tệp.

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.

Common mistake

Bẫy “nén không mất luôn làm nhỏ tệp”. Không có thuật toán không mất nào thu nhỏ được mọi tệp; RLE với dữ liệu ít lặp làm tệp to ra. Bẫy thứ hai ở bài tính: quên chi phí của bảng từ điển, quên giới hạn độ dài loạt (255 với 1 byte). Bẫy thứ ba: chọn “có mất” cho dữ liệu cần phân tích chính xác chỉ vì “giảm được nhiều hơn” — đề hỏi tình huống nào đòi khôi phục chính xác.

Key takeaway

Chốt: không mất = khôi phục đúng từng bit, dùng khi không được sai lệch; có mất = giảm mạnh hơn, chỉ khôi phục xấp xỉ, dùng khi dung lượng/tốc độ quan trọng hơn. RLE: (số lượng, giá trị), coi chừng trần độ dài loạt và dữ liệu ít lặp. Nén từ điển: tiết kiệm = số lần × (độ dài cụm − độ dài ký hiệu), trừ đi chi phí bảng.

← Overflow, Round-off and Digitizing Analog Data · All AP Computer Science Principles lessons · Practice AP Computer Science Principles questions (free, instant scoring) · Extracting Information from Data →