Thought Toys · Strategy & computation · Exhibit 120

How one bit of ink makes a grey

A newspaper press can put a dot of ink on the paper or leave it blank. That is the whole palette: no grey, not one shade. And yet a newspaper photograph has hundreds of greys in it. The trick is not in the dot. It is in what you do with the amount you were wrong by when you rounded — throw it away and you get bands, hand it to the neighbours and you get a photograph.

The same picture, in one bit of ink per pixel ● ink laid down   ● the darkness that was asked for

shades available ink laid down ink asked for worst patch is off by

your turn — drag How much of the rounding error you pass on down to nothing and watch the photograph fall apart into bands

What you're seeing

On the left is a picture with as many greys as you like. On the right is the same picture after every pixel has been forced to one of two values: ink, or no ink. Nothing else is available.

Start by dragging the first slider to zero. Now each pixel is simply rounded to whichever of black and white it is nearer — the obvious thing to do, and it is a disaster. The picture becomes a handful of flat regions with hard edges between them, because every pixel in a wide band of similar tones rounds the same way. All the shading is gone, and it is gone because it was thrown away one pixel at a time: each rounding discarded up to half a unit of darkness and never accounted for it.

Now drag the slider back up. The rule is only this: after rounding a pixel, take the amount you were wrong by and add it to the neighbours you have not visited yet — seven sixteenths to the pixel on the right, and three, five and one sixteenth to the three below. Nothing is discarded. A pixel that was rounded down to white makes the pixels after it slightly darker, so one of them will round to black to make up for it.

Watch the strip underneath while you drag. It compares, column by column, the ink actually laid down against the darkness the original asked for. At zero it is a staircase: whole regions of the picture are getting far too much ink or none. At full strength the two traces lie on top of one another. The count of black dots in any patch is the darkness of that patch, and the eye, which averages over a small neighbourhood without being asked, reads that density as a shade of grey.

So the greys are real but they are not anywhere. No pixel is grey. The grey is a property of a region, and error diffusion is a machine for getting the regional totals right while having only two values to spend.

One last thing to find, with the second slider. Flatten the picture all the way to a uniform fifty percent grey. The speckle stops being speckle and snaps into a perfect checkerboard — every pixel the opposite of all four of its neighbours. That is the algorithm's most famous quirk, and it is not a fault: at exactly half grey, the checkerboard is the finest halftone this grid can physically hold.

The rule, exactly. Visit the pixels in reading order, alternating direction each row. At each one, add in the error already handed to it, round to black or white, and pass the difference on with these weights:

          ●    7/16 3/16   5/16   1/16

The four weights sum to exactly one, which is the whole reason the picture keeps its darkness: the error is only ever moved, never created or destroyed. Robert Floyd and Louis Steinberg published it in 1976. The only error that genuinely disappears is the small amount pushed off the edges of the picture.

The slider scales how much of the error is passed on, so 0% is exactly plain rounding — not almost, exactly: the two produce identical pixels — and 100% is Floyd–Steinberg.

The comparison on this page is against plain rounding, because that is what the slider turns off. It is not a claim to be the best halftone available. Ordered dithering — comparing each pixel against a fixed repeating matrix — also keeps the local density honest. It trades differently: an obvious repeating pattern, in exchange for a scan that can be done in any order, or all at once.

Verified in node before this page existed (improve/verify/120-error-diffusion.js, 22 checks):

  • Every output pixel is exactly 0 or exactly 1. There are no greys hiding in the output.
  • On a picture that never once crosses the rounding threshold — so plain rounding outputs pure white and loses all of it — error diffusion asks for 0.2250 of the page in ink and delivers 0.2240.
  • Tile by tile, in squares of 16 by 16, diffusion is off by 0.45% of ink on average and 1.74% at worst. Plain rounding is off by 21.7% on average and 43.3% at worst.
  • Down a smooth ramp, the column-by-column ink density of the diffused picture is a straight line in the original (R² = 0.992). Plain rounding gives exactly two distinct densities: a step, which is what banding is.
  • The gap between 0.992 and 1 is the halftone itself, and that is measured rather than asserted: the residual of the straight-line fit alternates from column to column, with a lag-one autocorrelation of −0.72 and a lag-two of +0.32. That is the signature of the dot lattice, not of noise driven by the picture. Demanding a tighter fit would only be demanding that the picture stop being made of dots.
  • Three controls prove the mean test can actually fail. Weights that sum to a half instead of one — the classic typo, which still looks plausible — are caught. So is a kernel aimed backwards at pixels already decided, which is silently equivalent to doing nothing. So is a strength of zero.
  • A vacuity guard on the flat field: white stays white, black stays black, and 50% grey comes out at 50.00% ink.
  • The gate corrected the page here too. That last check originally demanded the pattern look irregular, on the assumption that a healthy dither does. It failed, and it was right to: at 50% grey every horizontal and vertical neighbour differs, 100% of them. The checkerboard is now claimed rather than treated as a defect.

Also in Strategy & computation: Sorting algorithms →

All 35 in Strategy & computation
  1. 10The evolution of trust
  2. 100The measure went up. The thing barely moved.
  3. 101Noise sets a speed limit, not an accuracy limit.
  4. 103The fastest slide dips below its finish.
  5. 106They meet in the middle. The beach walks twice as far.
  6. 109Twice the soldiers is four times the army
  7. 111Any party of six hides a trio
  8. 116He told them nothing they did not already know
  9. 120How one bit of ink makes a grey — you are here
  10. 23Sorting algorithms
  11. 24PageRank & the random surfer
  12. 25Huffman coding
  13. 26Dijkstra's shortest path
  14. 27Nash equilibria
  15. 33The learning-rate cliff
  16. 36A* pathfinding
  17. 37Braess's paradox
  18. 44Diffie–Hellman key exchange
  19. 45Preferential attachment
  20. 46Aliasing & the Nyquist limit
  21. 47The secretary problem
  22. 49Freeze too fast, stay stuck
  23. 51Cross one line, and its territory closes
  24. 56Catch one error, miss the next
  25. 57Why more processors stop helping
  26. 58Why a busy line explodes
  27. 59The set that's only sure when it says no
  28. 60The fit that memorizes instead of learns
  29. 61When the wire breaks, pick one
  30. 67Better at both, and still better off trading
  31. 69Everyone was consistent. The vote wasn't.
  32. 78Every world map is lying. You get to pick the lie.
  33. 82Your computer can't hold one tenth
  34. 85The shape that has only one side
  35. 97Same votes. Different winner.

← the cabinet