'F' → Fullscreen
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.
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.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();}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.Encode stores run - 1 instead of run. Record what happens to the pixels-wrong counts, then change it back.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();}Squash keeps only every other pixel, which is always 10 numbers. Stretch rebuilds the row by drawing each kept pixel twice.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();}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.Decode would need to know.)Task 3 prints exactly this.
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: 10Encode on purpose, recorded what happened, and fixed itAnswer the following questions before submitting your work.
0x9 1x9 0x2 is 6 numbers and holds what 20 numbers held. Explain, in one sentence, why fewer bits does not mean less information.Submit four things to the dropbox:
Encode did, and your Task 3 table and answers.