Unit 1: Digital information · Lossless data compression

Article

Why we compress data

4 min read

Take a photo with your phone. Stored as raw pixels, a photo that is 4,000 pixels wide and 3,000 pixels tall needs 4,000 × 3,000 × 3 = 36,000,000 bytes (3 bytes for each pixel's colour). But a photo on your phone is often only 3,000,000 bytes. Where did the other 33,000,000 bytes go?

They were never stored. The phone used compression.

What compression does

Data compression means storing or sending the same information with fewer bits. A program called a compressor turns the data into a smaller form. To use the data again, a decompressor turns it back.

  1. Original data

    e.g. a 12 MB photo

    compress
  2. Smaller data

    quicker to send and store

    decompress
  3. Usable data

    ready to view again

Compression is not magic. It works because most data is wasteful. Many of its bits repeat something, or store detail that nobody needs. Compression finds that waste and removes it.

Why we compress

Fewer bits help in three ways:

ReasonExample
Storage: more fits in the same spaceA phone holds thousands of photos instead of hundreds
Speed: fewer bits take less time to sendA web page loads sooner, and a video starts sooner
Cost: less to store and less to sendA small company needs fewer disks, and your mobile data lasts longer

Check your understanding

Which is a main reason to compress a file?

Measuring how well it worked

Two numbers tell you how much a compressor saved.

  • The compression ratio is the original size divided by the compressed size. A ratio of 4 to 1 means the original is 4 times as big as the compressed file.
  • The percentage saved is the amount saved divided by the original size, times 100.

A bigger ratio means better compression. A ratio of 1 to 1 means nothing was saved at all.

Worked example

A text document is 80 KB. After compression it is 20 KB.

  1. Ratio: 80 ÷ 20 = 4, so the ratio is 4 to 1.
  2. Amount saved: 80 − 20 = 60 KB.
  3. Percentage saved: 60 ÷ 80 = 0.75, which is 75%.

Check your understanding

A file is 50 MB. After compression it is 10 MB. What is the compression ratio (original size divided by compressed size)?

Two families of compression

All compression methods fit one of two families. A lossless method lets you rebuild the original data exactly, bit for bit. A lossy method throws some detail away forever, so you get back only a close copy. The next articles show how lossless methods work. After that you will meet lossy methods.

Compression also has costs. The compressor and decompressor need computer time. And some data cannot be made smaller at all.

Common mistakes

  • Turning the ratio upside down. The ratio is original ÷ compressed, so it is usually bigger than 1.
  • Giving the amount saved when asked for the ratio. 60 KB saved is not the same as a ratio of 4 to 1.
  • Thinking compression just deletes things. Some methods remove nothing. They only store the same information in a smarter way.
  • Thinking every file shrinks. Data with no repeats and no waste may not get smaller.

On the exam

You may be asked why a company would compress its files, or to say what a compressed file loses and what it keeps. Know that compression reduces the number of bits needed, and that the two families are lossless and lossy.

Key words

Compression: storing or sending the same information with fewer bits.

Compressor: a program that makes data smaller.

Decompressor: a program that rebuilds usable data from compressed data.

Compression ratio: the original size divided by the compressed size.

Lossless: a method that can rebuild the original data exactly.

Lossy: a method that throws some detail away forever.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.