CS With NehalCS With Nehal

Compression Simulator

Compare lossless dictionary compression of text with lossy compression of an image, and see the size saved and the data lost.

Choose a compression type

Lossless compression removes repetition without losing anything: decompressing gives back the exact original. It is used for text, program files and anything where every bit matters.

Dictionary compression

Each different word is stored once in a dictionary. The text is replaced by the index of each word.

Compressed text (indexes)

0123045016073048072301

Decompressed

the cat sat on the mat and the cat saw the dog on the mat so the dog sat on the cat

✓ The decompressed text is identical to the original, so no data was lost.
664 bits
Original
83 characters × 8
360 bits
Compressed
dictionary + 22 × 4-bit indexes
54%
Size after compression
9
Different words

Dictionary

IndexWordUses
0the7
1cat3
2sat2
3on3
4mat2
5and1
6saw1
7dog2
8so1

About This Simulation

This compression simulator compares the two kinds of compression. Lossless compression is shown with dictionary coding of text, where every word can be rebuilt exactly. Lossy compression is shown on a small greyscale image, where reducing resolution and shades saves space but permanently removes detail.

Features

  • Dictionary compression of your own text with the index list and dictionary table
  • Original and compressed sizes in bits with the percentage saved
  • Check that decompressed text is identical (lossless)
  • Lossy image compression by block averaging and fewer grey levels
  • Side-by-side images with the number of changed pixels and average error

Topics covered

  • Lossless Compression
  • Lossy Compression
  • Dictionary Coding
  • Compression Ratio
  • File Formats

Explanation

Compression reduces file size so that files use less storage and transfer faster. Lossless compression removes redundancy and can always be reversed exactly. Lossy compression discards data that people are unlikely to notice, giving much smaller files, but the original cannot be recovered.

How it works

  1. Dictionary (lossless): each different word is stored once in a dictionary, and the text is replaced by the index of each word. Repeated words cost only a few bits each.
  2. Decompressing looks each index up in the dictionary, giving back the exact text.
  3. Lossy (resolution): blocks of pixels are replaced by their average colour, so there are fewer pixels to store.
  4. Lossy (colour depth): each pixel is rounded to fewer shades, so each pixel needs fewer bits.
  5. Compression ratio = original size ÷ compressed size. Higher means better compression.

Why it matters

Exams ask you to choose between lossless and lossy compression and justify it. Text, program code and databases need lossless compression because every bit matters; photos, music and video use lossy compression because a small loss of quality is acceptable for a much smaller file.

Frequently Asked Questions

What is the difference between lossy and lossless compression?

Lossless compression lets the original data be rebuilt exactly. Lossy compression permanently removes some data to make the file smaller, so the original cannot be recovered.

Give examples of lossless and lossy formats.

Lossless: ZIP, PNG, FLAC, GIF. Lossy: JPEG, MP3, AAC, most video formats such as MP4 (H.264).

Why can't text use lossy compression?

Losing even one character can change the meaning of text or break a program, so text must be compressed losslessly.

Can compression make a file bigger?

Yes. If data has little repetition, the extra information needed (such as a dictionary) can outweigh the savings.

Related Simulations