sciandu
Computer science

Computer science

Data compression

How big files become small without losing anything important.

What you need first

You send a photo, stream music or download an app: almost everything that travels over the internet is compressed. Without compression your phone storage would fill up fast and every video would take forever to load. The idea behind it is surprisingly simple: repetitions do not have to be stored one by one.

Exploiting repetition

Imagine a row of in an image: white, white, white, white, white, white, white. We shorten white to w and black to s. Instead of storing white seven times, you simply write 7w: the number says how often the symbol after it repeats. This is called run-length encoding, and a stretch of identical pixels is called a run. A row like wwwwwwwsss then becomes 7w3s, turning 10 characters into 4. The longer the same-coloured stretches, the more you save.

Pixel grid
Row 1 as bits:01100110

64 Pixel = 64 Bit = 8 Byte

Try it: colour in some pixels and see how short or long the compressed description of your picture becomes.

Lossless or lossy

With run-length encoding nothing gets lost: from 7w3s you can restore the original row exactly. Such methods are called lossless. ZIP files are also lossless, though they use more sophisticated methods than plain run-length encoding. For photos and music we often go one step further: JPG and MP3 deliberately throw away details your eye or ear barely notices. This is called lossy. The file gets much smaller, but the original never fully comes back. For a contract that would be fatal, for a holiday photo it is usually perfectly fine.

Why the sky shrinks so well

Now you also understand why a photo of a blue sky shrinks much more than a photo of a colourful meadow: the sky consists of huge areas of almost the same colour, in other words long runs that can be described extremely briefly. A wild jumble of colours offers hardly any repetition. That is exactly why some files are tiny after zipping and others barely smaller than before.

Exercises

0 of 6 solved

Time to try it yourself. You can't break anything, every attempt counts.

What does 7w mean in run-length encoding?

How many pixels does the code 5w2s describe?

How many characters does the compressed form of wwwwsswww (that is 4w2s3w) have?

Match each term to the description that fits.

Run-length encoding
Lossless
Lossy

A row consists of 12 white and then 8 black pixels. How many characters does the code 12w8s have?

A photo of a blue sky shrinks a lot when compressed, because identical colours lie in long one after another.