Sanyanggae LogoSanyanggae

💻 Web & Frontend

Why does one row of two colors compress better than the other?

Keep all twelve pixels and their colors, but rearrange them. Find out which row bitmap RLE8 can describe in four bytes, then rebuild the row from the stored numbers.

사냥개 (Sanyanggae)Published Updated 9 min read

This is a translation of the Korean original. Korean original · Translation review: AI review

Two strips of twelve cells. The top has six teal 1s followed by six orange 2s. The bottom alternates 1 and 2. Brackets group consecutive cells with the same number.
Diagram by Sanyanggae. The numbers beneath the brackets give the run lengths. Both rows use six cells of each color, but the top has two runs and the bottom has twelve.

Find the boundaries before counting the colors

Both strips in the diagram have twelve tiles, six teal and six orange. Two broad bands fill the top strip, while the bottom changes color at every tile. Describe the top to a friend facing away: “From the left, paint six teal tiles, then six orange ones.” Your friend can account for every tile. Give the same instructions for the bottom and the color totals will match, yet the pattern will be wrong well before tile seven. A receipt listing how many tiles of each color you bought wouldn’t tell a tiler how to lay the pattern.

Slide a pencil from left to right, lifting it only when the color changes. You’ll lift it once on top and eleven times below. The very first tile starts a group, so that makes two groups above and twelve below. An uninterrupted stretch of one value is called a run. Run-length encoding (RLE) records the value and length of each stretch in order. Knowing where the colors change tells you something about the space you’ll need that counting colors in the palette alone can’t tell you.

How many pixels can a pair of numbers paint?

Take RLE8 in Windows bitmaps. A pixel refers to a palette entry by a one-byte index. We assigned teal to entry 1 and orange to entry 2. This choice belongs to our example’s palette. Entry 1 in another image could be an entirely different color. Even if two neighboring pixels look similar, different stored indexes keep them from joining the same run. Change entry 1 to purple in the palette and the first six tiles above and the odd-numbered tiles below will all turn purple without changing the index sequence.

A repeat instruction in RLE8 takes two bytes. The first gives the number of consecutive pixels to draw and the second gives the color index. The top row reads (6, 1), (6, 2). The commas and brackets are just for us. The four values stored are 6, 1, 6, 2. To decode, put your finger on the first empty square. Fill six squares with color 1, move to the next empty square and fill six with color 2. The switch should fall on the seventh tile, with all twelve filled at the end. There’s no averaging of colors or guessing where an edge lies. With the same palette you recover the original pixels exactly.

The cost of narrow stripes

Writing one index per pixel costs twelve bytes for either row. Using only repeat instructions, the top needs two pairs, or four bytes. On the bottom, one orange pixel always follows one teal pixel. Writing (1, 1), (1, 2) six times takes twelve pairs, or twenty-four bytes. The same twelve squares now take twice their original space. You can count for yourself how compression can cost space. Neither the proportions of the colors nor the pixel count explain the difference, since both are identical in the two rows.

Where do you break even? Twelve bytes can hold six two-byte pairs. If the row divides into six runs, the cost is the same. Five runs take ten bytes, saving two. The runs don’t all need to be equal in length. Try five teal tiles, one orange, one teal and five orange. The proportions are still half and half. Despite the two single-tile runs in the middle, the whole row fits in four pairs, or eight bytes. Spotting a single-pixel run isn’t enough to decide whether the whole row loses space.

When copying the values works better

RLE8 lets you mix two-byte repeats with absolute mode, which lists individual indexes. To use absolute mode for the bottom row, write 0, 12 followed by the twelve indexes in order. On encountering that first zero the reader interprets the next number differently. Here the 12 says to take twelve following values and apply each to one pixel. The two instruction bytes and twelve indexes add up to fourteen bytes instead of twenty-four. That’s shorter than using repeats throughout, though still two bytes more than writing the twelve indexes on their own.

An absolute block can carry three to 255 indexes. For an odd count, the reader skips a padding byte so the next command starts on a two-byte boundary. For five pixels, paint the five values after the two instruction bytes and skip the next byte. Six pixels need no padding, so either block takes eight bytes. The pair 0, 0 signals the end of a line, and 0, 1 the end of the bitmap. Our four- and fourteen-byte results measure just the instructions representing the pixels of a row. Add control commands, the header, the palette and any other parts present to work out the size of the file.

Turn the same tiles sideways

On a twelve-by-twelve grid, color the first row entirely teal and the next entirely orange. Repeat to the bottom. Read each row from the left, ending its run at the edge. Each of the twelve rows has one run, for twelve repeat instructions in all. Turn the paper 90 degrees to make vertical stripes. There are just as many tiles of both colors, but every row now alternates one tile at a time. Twelve pairs per row gives 144 pairs altogether. A real encoder could switch to absolute mode for the alternating stretches. The calculation isolates the effect of reading direction when only repeats are allowed.

What happens if you extend the teal band on the top left? The largest repeat count one byte of RLE8 can hold is 255. A row of 260 teal pixels looks like a single run, yet can be written as (255, 1) followed by (5, 1). Mistaking the start of the second command for a color change would send you looking for an edge that isn’t there. Reading the picture, mark where neighboring indexes change. Reading the file, count how many tiles each instruction fills. Finish the experiment by checking that both records put the same colors in the same places.

Sources and further reading

This article explains information from public sources. Citing an organization does not imply its review or endorsement.

About the publisher and contact

Related articles