Back

Activity 2.17: Compression

divider

Activity 2.17

Compression

Key Concepts

Redundancy

Compression Ratio

Lossless and Lossy

Choosing Between Them

Activity 2.16 Ended With a Bill

Sampling more often made the sound meter more accurate, and every extra sample was one more number to store. Every photo, song and video has the same bill.

Compression is how you pay less of it. Activity 1.2 named the two kinds, lossless and lossy. Today you build one of each, measure what they save, and find data that compression makes bigger.

What Compression Means

Data compression reduces the number of bits needed to store or send some data.

It works by finding a shorter way to say the same thing. The question is always what shorter way, and that depends on what the data looks like.

Say "Ten A's" Instead of Ten A's

Run-length encoding stores each run of repeated symbols as the symbol and how many times it repeats. Work out the encoding of this line before it appears.

Value Trace
AAAAAAAAAABBBBBCC 17 letters
A10 B5 C2 6 symbols
ratio 6 / 17 = 0.35

Say "Ten A's" Instead of Ten A's

The compression ratio is the compressed size divided by the original size. 0.35 means the encoded line is about a third as big.

And nothing was lost. From A10 B5 C2 you can write the original back out, letter for letter.

Now Try It on This

Same algorithm, different data. Predict the ratio before it appears.

Value Trace
ABCABCABCABCABCABC 18 letters
A1 B1 C1 A1 B1 C1 ... 36 symbols
ratio 36 / 18 = 2.00

Now Try It on This

It doubled. Every letter is a run of one, so every letter now costs two symbols.

Compression saves by removing redundancy: repetition in the original. The College Board's wording: how much you save depends on both the amount of redundancy in the original and the algorithm applied. No redundancy the algorithm can see, no saving.

Fewer Bits Is Not Less Information

A10 B5 C2 holds everything the 17 letters held. You could rebuild them from it without a single guess.

Size and information are different quantities. The size is how many bits are stored. The information is what you can get back out of them.

Two Families

Lossless and Lossy
LOSSLESS the original can be rebuilt exactly
usually saves less
ZIP files, PNG images
LOSSY only an approximation can be rebuilt
usually saves more
JPEG photos, streamed music and video

Two Families

Lossless compression guarantees the original can be rebuilt exactly. Run-length encoding is lossless.

Lossy compression throws away detail you are unlikely to miss, such as tiny color differences in a photo, so only an approximation comes back. Because it is allowed to throw things away, it usually saves much more.

So How Do You Choose?

If the exact original or its quality matters most, choose lossless. A contract, a medical scan, a program's source code.

If size or the time to send it matters most, choose lossy. A video call, a song streamed on a phone, a photo posted online.

Test Yourself Before the Build

Your C# program, compressed lossily. It is 40% smaller and almost the same.

What would “almost the same source code” mean? One changed character can stop it compiling, or change what it does. That is why the C# workspace you downloaded is a ZIP file: lossless.

Today's Objectives

  • Calculating a compression ratio
  • Explaining why saving depends on redundancy and on the algorithm
  • Distinguishing lossless from lossy compression
  • Choosing between them for a stated situation, and defending the choice

Key Terms

Data compression
Reducing the number of bits needed to store or send data.
Redundancy
Repetition in the original data, which compression removes.
Lossless compression
Compression that guarantees the original can be rebuilt exactly.
Lossy compression
Compression that allows only an approximation to be rebuilt, and usually saves more.

'F' → Fullscreen

divider

Build

You are squeezing the art for a retro game onto a tiny memory chip, in a new program named 2-17-pixel-packer. The image is three rows of twenty pixels, stored back to back in one array: 1 is a lit pixel, drawn as #, and 0 is dark, drawn as .. You will pack it losslessly, unpack it to prove nothing was lost, then pack it lossily and count the damage.

How to read the code blocks. Each task's code block shows your whole program, from line 1, as it should look when that task is finished.

The green lines marked with a + are the ones you add or change for that task — everything else is already in your file. Do not type the + sign.


Task 1: Pack It

  • Each row starts at a multiple of 20, so a method that works on one row takes a start: row 2 starts at position 20. Encode(start) walks along the row. At each pixel, the inner while counts how many in a row match it, then the pixel's value and that count go into codes as a pair. It returns how many numbers it used.
  • ShowCodes prints each pair as value x count, so 0x9 means nine dark pixels.
  • Before you run it, encode the brick wall by hand and work out its ratio. Then run it and check.
  • The checkerboard's ratio is above 1. Explain why, using the word redundancy.
2-17-pixel-packer
int[] image =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1
};
string[] names = { "Rooftop", "Brick wall", "Checkerboard" };
int[] codes = new int[40];
for (int row = 0; row < 3; row++)
{
int start = row * 20;
int used = Encode(start);
Console.WriteLine(names[row]);
Console.Write(" original: ");
ShowImage(start);
Console.Write(" encoded: ");
ShowCodes(used);
Console.WriteLine($" 20 numbers became {used}, ratio {used / 20.0:F2}");
Console.WriteLine();
}
int Encode(int start)
{
int used = 0;
int i = start;
while (i < start + 20)
{
int run = 1;
while (i + run < start + 20 && image[i + run] == image[i])
{
run = run + 1;
}
codes[used] = image[i];
codes[used + 1] = run;
used = used + 2;
i = i + run;
}
return used;
}
void ShowImage(int start)
{
for (int i = start; i < start + 20; i++)
{
if (image[i] == 1)
{
Console.Write("#");
}
else
{
Console.Write(".");
}
}
Console.WriteLine();
}
void ShowCodes(int used)
{
for (int i = 0; i < used; i = i + 2)
{
Console.Write($" {codes[i]}x{codes[i + 1]}");
}
Console.WriteLine();
}

Task 2: Unpack It

  • Packing is only half of it: the game has to draw the art. Decode reads the pairs in codes and writes each value into restored as many times as its count says.
  • CountWrong compares restored with the original row, pixel by pixel. All three rows must show 0 pixels wrong. If one does not, Encode and Decode disagree about the format, and the compression is not lossless.
  • Break it on purpose: change line 45 so Encode stores run - 1 instead of run. Record what happens to the pixels-wrong counts, then change it back.
2-17-pixel-packer
int[] image =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1
};
string[] names = { "Rooftop", "Brick wall", "Checkerboard" };
int[] codes = new int[40];
int[] restored = new int[20];
for (int row = 0; row < 3; row++)
{
int start = row * 20;
int used = Encode(start);
Console.WriteLine(names[row]);
Console.Write(" original: ");
ShowImage(start);
Console.Write(" encoded: ");
ShowCodes(used);
Console.WriteLine($" 20 numbers became {used}, ratio {used / 20.0:F2}");
Decode(used);
Console.Write(" restored: ");
ShowRestored();
Console.WriteLine($" pixels wrong: {CountWrong(start)}");
Console.WriteLine();
}
int Encode(int start)
{
int used = 0;
int i = start;
while (i < start + 20)
{
int run = 1;
while (i + run < start + 20 && image[i + run] == image[i])
{
run = run + 1;
}
codes[used] = image[i];
codes[used + 1] = run;
used = used + 2;
i = i + run;
}
return used;
}
void Decode(int used)
{
int next = 0;
for (int i = 0; i < used; i = i + 2)
{
for (int j = 0; j < codes[i + 1]; j++)
{
restored[next] = codes[i];
next = next + 1;
}
}
}
int CountWrong(int start)
{
int wrong = 0;
for (int i = 0; i < 20; i++)
{
if (restored[i] != image[start + i])
{
wrong = wrong + 1;
}
}
return wrong;
}
void ShowImage(int start)
{
for (int i = start; i < start + 20; i++)
{
if (image[i] == 1)
{
Console.Write("#");
}
else
{
Console.Write(".");
}
}
Console.WriteLine();
}
void ShowRestored()
{
for (int i = 0; i < 20; i++)
{
if (restored[i] == 1)
{
Console.Write("#");
}
else
{
Console.Write(".");
}
}
Console.WriteLine();
}
void ShowCodes(int used)
{
for (int i = 0; i < used; i = i + 2)
{
Console.Write($" {codes[i]}x{codes[i + 1]}");
}
Console.WriteLine();
}

Task 3: Pack It Smaller, and Lose Something

  • Now a lossy version. Squash keeps only every other pixel, which is always 10 numbers. Stretch rebuilds the row by drawing each kept pixel twice.
  • Run it and record, for each row, the lossy ratio and pixels wrong next to run-length encoding's.
  • Answer three questions. (a) On which rows did the lossy version save more than run-length encoding, and what did each one cost? On which row did it save less and still lose a pixel? (b) Why did the checkerboard come back completely dark? (c) Name a game where you would ship the lossy version anyway, and data you never would.
2-17-pixel-packer
int[] image =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1
};
string[] names = { "Rooftop", "Brick wall", "Checkerboard" };
int[] codes = new int[40];
int[] restored = new int[20];
int[] kept = new int[10];
for (int row = 0; row < 3; row++)
{
int start = row * 20;
int used = Encode(start);
Console.WriteLine(names[row]);
Console.Write(" original: ");
ShowImage(start);
Console.Write(" encoded: ");
ShowCodes(used);
Console.WriteLine($" 20 numbers became {used}, ratio {used / 20.0:F2}");
Decode(used);
Console.Write(" restored: ");
ShowRestored();
Console.WriteLine($" pixels wrong: {CountWrong(start)}");
Squash(start);
Stretch();
Console.Write(" lossy: ");
ShowRestored();
Console.WriteLine($" 20 numbers became 10, ratio 0.50, pixels wrong: {CountWrong(start)}");
Console.WriteLine();
}
int Encode(int start)
{
int used = 0;
int i = start;
while (i < start + 20)
{
int run = 1;
while (i + run < start + 20 && image[i + run] == image[i])
{
run = run + 1;
}
codes[used] = image[i];
codes[used + 1] = run;
used = used + 2;
i = i + run;
}
return used;
}
void Decode(int used)
{
int next = 0;
for (int i = 0; i < used; i = i + 2)
{
for (int j = 0; j < codes[i + 1]; j++)
{
restored[next] = codes[i];
next = next + 1;
}
}
}
void Squash(int start)
{
for (int i = 0; i < 10; i++)
{
kept[i] = image[start + i * 2];
}
}
void Stretch()
{
for (int i = 0; i < 10; i++)
{
restored[i * 2] = kept[i];
restored[i * 2 + 1] = kept[i];
}
}
int CountWrong(int start)
{
int wrong = 0;
for (int i = 0; i < 20; i++)
{
if (restored[i] != image[start + i])
{
wrong = wrong + 1;
}
}
return wrong;
}
void ShowImage(int start)
{
for (int i = start; i < start + 20; i++)
{
if (image[i] == 1)
{
Console.Write("#");
}
else
{
Console.Write(".");
}
}
Console.WriteLine();
}
void ShowRestored()
{
for (int i = 0; i < 20; i++)
{
if (restored[i] == 1)
{
Console.Write("#");
}
else
{
Console.Write(".");
}
}
Console.WriteLine();
}
void ShowCodes(int used)
{
for (int i = 0; i < used; i = i + 2)
{
Console.Write($" {codes[i]}x{codes[i + 1]}");
}
Console.WriteLine();
}

Challenge (Optional): Never Make It Bigger

  • Real compressors refuse to make data bigger. When Encode uses more than 20 numbers, print stored raw instead of the codes, and report the ratio as if the row were stored unchanged plus one extra number.
  • What is that one extra number for? (Think about what Decode would need to know.)
divider

Checkpoint

Task 3 prints exactly this.

Example Output
Rooftop
original: .........#########..
encoded: 0x9 1x9 0x2
20 numbers became 6, ratio 0.30
restored: .........#########..
pixels wrong: 0
lossy: ..........########..
20 numbers became 10, ratio 0.50, pixels wrong: 1
Brick wall
original: ...##....###..####..
encoded: 0x3 1x2 0x4 1x3 0x2 1x4 0x2
20 numbers became 14, ratio 0.70
restored: ...##....###..####..
pixels wrong: 0
lossy: ....##....##..####..
20 numbers became 10, ratio 0.50, pixels wrong: 3
Checkerboard
original: .#.#.#.#.#.#.#.#.#.#
encoded: 0x1 1x1 0x1 1x1 0x1 1x1 0x1 1x1 0x1 1x1 0x1 1x1 0x1 1x1 0x1 1x1 0x1 1x1 0x1 1x1
20 numbers became 40, ratio 2.00
restored: .#.#.#.#.#.#.#.#.#.#
pixels wrong: 0
lossy: ....................
20 numbers became 10, ratio 0.50, pixels wrong: 10
  • You encoded the brick wall by hand before running, and it matched
  • Every row restores with 0 pixels wrong
  • You broke Encode on purpose, recorded what happened, and fixed it
  • You answered Task 3's three questions
divider

Reflection

Answer the following questions before submitting your work.

  1. The checkerboard row packed to twice its original size. Explain what that says about the relationship between the data and the algorithm.
  2. The lossy version saved more than run-length encoding did on the brick wall. Why is that not enough to make it the better choice?
  3. 0x9 1x9 0x2 is 6 numbers and holds what 20 numbers held. Explain, in one sentence, why fewer bits does not mean less information.
divider

Submit

Submit four things to the dropbox:

  1. Your 2-17-pixel-packer program file.
  2. Your hand encoding of the brick wall, and its ratio.
  3. What breaking Encode did, and your Task 3 table and answers.
  4. Your three reflection answers.

Activity Complete