Lossless Compression: Run-Length Coding

Run-length encoding is a lossless method that stores repeated pixels as one colour code plus a count, instead of listing each pixel separately.

Colour Palette (3-bit)

000
001
010
011
111

Activity 1: Encode the first two lines of the image.

Look at the first two rows of pixels and fill the boxes in order from left to right as run-length pairs: enter a 3-bit colour code in each colour box, then a 4-bit count in the next box. Repeat this pattern (colour, count, colour, count) until the line is fully encoded.

Activity 2: Third line uncompressed bits

Count uncompressed bits from the pixel row (3 bits per pixel), then count compressed bits from the binary runs below (colour bits + count bits).

Explain how recording a colour code and then how many times it repeats creates lossless compression.

Activity 3: Compression Totals Challenge

Use the compressed binary data below to calculate total compressed bits (add colour bits and count bits for every run). Then use the image above to calculate total uncompressed bits (3 bits per pixel), and work out the compression percentage.

Will run-length coding always reduce file size?