← Making data smaller

Run-length encoding

Compression stores data in fewer numbers, so it takes less space and less time to send. Run-length encoding stores how many of each colour there are in turn (3, 4, 1) instead of every pixel. It gives back exactly the same picture (lossless), and works best on data with long runs.

A fax machine sent whole pages down a phone line in the 1980s, as numbers. How do you send a page with far fewer numbers than it has dots?

Pictures, songs and videos are huge: a phone photo is tens of millions of numbers. Compression stores the same data in fewer numbers, so it fits in less space and is quicker to send.

Here the pictures are black and white, one number a pixel. Run-length encoding stores each row as the lengths of its runs (pixels of the same colour in a row), taking turns: white first, then black, then white, and so on.

PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

A shorter row

A row of 8 pixels is 3 white, then 4 black, then 1 white. Stored pixel by pixel, that is 8 numbers. Which is the shortest way to store it so you get **exactly** the same row back?

TryTry it on the board and compare the result with your prediction.

Try: paint a run

Was your prediction right? Change a pixel and watch the code change.

Try it yourselfYour edits are saved in this browser.

Paint pixels 4 to 7 black to match the picture. What code does the board write for the row?

Use the board. This ticks when you get there.

InvestigateWork out why it happens, one step at a time.

Worked example: encode a picture

Worked exampleBefore each step, say what you think comes next.
  1. 3,4,13, 4, 1Row 1: 3 white, 4 black, 1 white. 3 numbers instead of 8.

Decompressing reads the codes back and gives exactly the same picture, so run-length encoding is lossless. Now decode 0, 2, 6.

A row's code is **0, 2, 6**. What is the row?

Sam encodes a row that starts with black. Click the line where it first goes wrong.

Why compress data at all?

ModifyChange the problem on purpose and see what happens.

Vary: one change at a time

Try it yourselfYour edits are saved in this browser.

Paint the whole row black. What is its code?

Use the board. This ticks when you get there.

Paint every second pixel black: white, black, white, black… How many numbers is the code?

Use the board. This ticks when you get there.

Change this picture so its codes use as few numbers as possible.

Use the board. This ticks when you get there.

An 8 × 8 icon (64 pixels) compresses to 16 numbers. What percentage of the original size is that?

Which picture would run-length encoding shrink the most?

PractiseA few on your own, to check you have it.

Codes

What is the code for the row: white, white, black, black, black, black, black, black?

A row is decoded from **1, 3, 2, 2**. How many pixels long is it?

Run-length encoding on text: **aaaabbbcc** can be stored as…

Can run-length encoding make data **bigger**?

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

Draw from the codes

Then: how many numbers would the plus sign need if it were twice as big (12 × 12)?

Try it yourselfYour edits are saved in this browser.
Need a hint?

Each row's first number counts white, then they take turns.

Another hint

2, 2, 2 means 2 white, 2 black, 2 white.

Another hint

0, 6 means no white, then 6 black: a whole black row.

The codes on the right describe a 6 × 6 picture. Paint it.

Use the board. This ticks when you get there.

For teachers

Students paint black and white pixels and watch each row's run-length code, to explain why data is compressed, apply run-length encoding, calculate the saving and see when it fails (AC9TDI10K03 nodes 1 and 2). About 45 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

CS Unplugged's Image representation activity (the fax-machine pixel codes) is the same model on paper; doing one page of it first works well.

The one idea: store runs instead of every value; decompressing gives back the data exactly (lossless). It saves when the data has long runs, and can make data bigger when it has none.

Simplification: real formats use a count and a value, as the lossy lesson does; the black-and-white form here (taking turns from white) is CS Unplugged's.

Running the lesson

  1. Predict: students who chose 4 lose the position; ask them to rebuild the row from 4.
  2. Try: ask what code a row of one black pixel at the end would get (7, 1).
  3. Worked example: pause before row 2 and ask how to say "no white".
  4. Vary: the checkerboard task usually surprises; ask what kind of real picture is like it.
  5. Make: swap codes between pairs: one draws a picture, the other decodes it.

The hard part (on purpose)

Rows that start black. Insist the first number always counts white, even when it is 0.

When students get stuck

Stretch and support

Stretch: Design an 8 × 8 picture that compresses to 8 numbers or fewer. Design one that makes the code longer than 64.

Support: Have students say the colours aloud while counting: white 3, black 4, white 1.

Where it leads

Next: Lossy or lossless.

Builds on: Data representation (pixels and bytes). Where it leads: file formats (PNG, GIF and ZIP use lossless methods; JPEG and MP3 lossy).

Sources

What each step is for

Now you can
Next lesson2. Lossy or losslessLossless compression gives back exactly the original data. Lossy compression throws away detail people are unlikely to notice, such as tiny differences in shade, so files get much smaller, but that detail is gone for good. Music and photos online are usually lossy; text, medical scans and evidence must be lossless.