📘 CodingMarble Learn

Data Compression

Compression makes a file smaller so it uses less storage and travels faster. Lossless compression (RLE, Huffman, dictionary methods, ZIP, PNG) gives back exactly the original data. Lossy compression (JPEG, MP3, MP4) removes detail people hardly notice, so files get much smaller but the lost detail cannot return. Compression ratio = original size ÷ compressed size.

🎬 Step-by-step story

  1. Here is one row of a black-and-white picture: 16 pixels stored one by one. Many of them repeat.
  2. Run-length encoding stores each run as a count and a value: 6W 3B 7W. 16 values shrink to 6.
  3. Huffman coding gives short codes to common symbols and long codes to rare ones. ABRACADABRA drops from 88 bits to 23 bits.
  4. Lossless gives back exactly the same data. Lossy throws away small detail: 16 grey shades become 4.
  5. Compression ratio = original size ÷ new size. 88 ÷ 23 is about 3.8 : 1, a saving of about 74%.
  6. Your turn: change the bits per pixel and see how many shades are kept and how big a photo would be.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

If compression removes data, how can lossless get it all back?

Lossless only removes repetition, not information. 6W still means six white pixels, so the decoder rebuilds them exactly.

Can RLE ever make a file bigger?

Yes. If no value repeats, every pixel becomes a pair (1, value), so the data doubles. RLE needs long runs.

Why do Huffman codes not get mixed up when there are no spaces?

No code is the beginning of another code (prefix-free), so as soon as the bits match a code you know which symbol it is.

Why do photos use lossy compression if it loses data?

Our eyes do not notice tiny changes in shade, so removing them saves a lot of space without a visible change.

Is a bigger compression ratio always better?

For size, yes, but with lossy methods a very high ratio means more lost detail. Choose a balance.

Why does a photo look blocky after being shared many times?

Every lossy save throws away a little more detail. The losses add up, just like keeping fewer and fewer shades.

What is data compression and why do we need it?

Compression means storing the same information using fewer bits. A program called an encoder makes the file smaller. A decoder opens it again.

Compression works because real data has redundancy: repeated values, common letters, or detail our eyes and ears do not notice. The price is time: the computer must spend work to compress and decompress.

Lossless and lossy compression

Lossless compression gives back exactly the original bits. We need it for text, program files, spreadsheets and anything where one wrong bit matters. Examples: ZIP, PNG, GIF, FLAC.

Lossy compression removes detail that people are unlikely to notice: tiny colour changes in a photo, very high or masked sounds in music. Files become much smaller, but the removed data is gone for ever. Examples: JPEG photos, MP3 and AAC music, MP4 video.

LosslessLossy
Original back?Yes, exactlyNo, close copy
Size savingSmallerMuch bigger
Good fortext, code, dataphotos, music, video

Run-length encoding (RLE)

A run is a group of the same value next to each other. RLE stores each run as a pair: (count, value).

Example: WWWWWWBBBWWWWWWW becomes 6W 3B 7W, or as numbers 6 0 3 1 7 0 if white = 0 and black = 1.

RLE is great for images with large flat areas (icons, cartoons, scanned pages). It can make data bigger if values rarely repeat: ABCD becomes 1A 1B 1C 1D, which is twice as long.

Huffman coding and dictionary methods

Normal ASCII gives every character 8 bits. Huffman coding gives short codes to frequent symbols and long codes to rare ones.

  1. Count how often each symbol appears.
  2. Take the two least frequent items and join them into one node whose count is their sum.
  3. Repeat until one tree is left.
  4. Read codes from the root: left branch = 0, right branch = 1.

In ABRACADABRA: A×5, B×2, R×2, C×1, D×1. One Huffman tree gives A = 0, R = 10, B = 110, C = 1110, D = 1111. Total = 5×1 + 2×2 + 2×3 + 1×4 + 1×4 = 23 bits instead of 11 × 8 = 88 bits.

Huffman codes are prefix-free: no code is the start of another, so the decoder never gets confused. The tree (or the code table) must be stored with the file.

Dictionary methods

Methods like LZ (used in ZIP and PNG) keep a dictionary of patterns already seen. When a pattern repeats, they store a short reference ("go back 12 places, copy 5") instead of the pattern itself.

Measuring compression

Compression ratio = original size ÷ compressed size. A 10 MB file squeezed to 2 MB has a ratio of 5 : 1.

Space saving = (original − compressed) ÷ original × 100%. Here (10 − 2) ÷ 10 = 80%.

For images: raw size (bits) = width × height × colour depth. Fewer bits per pixel or fewer pixels means a smaller file, but lower quality. A good choice balances quality, size and speed.

Key formulas and definitions

Worked examples

1. Encode AAAABBBCCD with RLE.

Runs: AAAA, BBB, CC, D. RLE: 4A 3B 2C 1D. 10 characters became 8 symbols (4 pairs).

2. A 12 MB video is compressed to 3 MB. Find the ratio and the space saving.

Ratio = 12 ÷ 3 = 4 : 1. Saving = (12 − 3) ÷ 12 × 100 = 75%.

3. Which compression would you use for (a) a school report in a word processor, (b) a holiday photo for social media?

(a) Lossless – every letter must stay exactly right. (b) Lossy (JPEG) – a tiny loss of colour detail is fine and the file becomes much smaller.

4. Symbols: E×6, T×3, A×2, Z×1. Huffman codes E = 0, T = 10, A = 110, Z = 111. How many bits are needed? How many in 8-bit ASCII?

Huffman: 6×1 + 3×2 + 2×3 + 1×3 = 6 + 6 + 6 + 3 = 21 bits. ASCII: 12 characters × 8 = 96 bits. Ratio ≈ 4.6 : 1.

5. Decode 0101100 using A = 0, R = 10, B = 110.

Read from the left and stop as soon as a code matches: 0 → A, 10 → R, 110 → B, 0 → A. Answer: ARBA.

6. A 1000 × 800 image uses 24-bit colour. Find its raw size in kB. Lossy compression gives a 120 kB file. What is the ratio?

Bits = 1000 × 800 × 24 = 19 200 000. Bytes = 2 400 000 = 2400 kB. Ratio = 2400 ÷ 120 = 20 : 1.

Common mistakes

Practice quiz

1. Which type of compression gives back the exact original file?
2. RLE of WWWBBW is:
3. In Huffman coding the most frequent symbol gets:
4. Which format normally uses lossy compression?
5. A 20 MB file becomes 5 MB. The compression ratio is:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is data compression in simple words?

It is a way of making a file smaller by storing the same information with fewer bits, so it takes less space and moves faster.

What is the difference between lossy and lossless compression?

Lossless gives back the exact original (ZIP, PNG). Lossy removes small details for a much smaller file and cannot be fully reversed (JPEG, MP3).

Where is Huffman coding used?

It is part of many formats, including ZIP (DEFLATE), JPEG and MP3, where it shrinks the final stream of symbols without loss.

Where this is taught

PolandLiceum ogólnokształcące, klasa IVUsing computers, digital devices and networks
Ukraine8 класInformation and information literacy: models and data structures
South Korea고등학교 2학년Data
Russia11 классTheoretical foundations

Learn first

Learn next

Related lessons

All Computer Science lessons