Skip to content

Dithering

Fact Value
Also called Floyd-Steinberg dithering, Atkinson dithering, Ordered dithering, Bayer dithering, Error diffusion
Status Implemented and tested
Family Artistic and geometric effects
Engine Browser
Type Nonlinear, spatial, quantization with error diffusion or an ordered threshold
Output Image
Operator shown as algorithm (exact)
Support radius global
Code web/src/core/filters/artistic/dither.ts, ditheringFilter
Operator code web/src/core/filters/artistic/dither.ts, ditherPlane
Channels RGB, Brightness only

Overview

Dithering shows many tones with only a few levels, such as black and white. Each pixel is rounded to an allowed level, and the rounding error is hidden in a pattern of dots. From a distance, the eye averages the dots back into the tones of the original.

Four panels: a gray ramp from black to white, and the ramp in black and white dots with Floyd-Steinberg, with Atkinson, which has cleaner black and white ends, and with the Bayer matrix, which shows a regular cross-hatch. Four panels: a gray ramp from black to white, and the ramp in black and white dots with Floyd-Steinberg, with Atkinson, which has cleaner black and white ends, and with the Bayer matrix, which shows a regular cross-hatch.
A gray ramp dithered to black and white with Floyd-Steinberg, Atkinson and the 8 x 8 Bayer matrix.

How it works

  1. Visit the pixels row by row and round each one to the nearest level.
  2. Pass its rounding error on to the next pixels, so they make up for it.
  3. For the Bayer method, shift each pixel by a fixed threshold pattern before rounding instead.

Mathematical foundation

\[ q(v) = s \operatorname{round}(v / s), \quad s = \frac{255}{L - 1}, \qquad L = 2 \]

The formula shows the default parameter values.

where

  • \(q(v)\): The nearest level to v
  • \(s\): The step between levels
  • \(L\): The number of levels
  • \(M\): The Bayer matrix, 0 to n^2 - 1

q rounds a value to the nearest of L levels. Error diffusion visits the pixels row by row from the top left, rounds each one, and adds its error e = v - q(v) to neighbors not yet visited. Floyd-Steinberg gives 7/16 of e to the right, 3/16 below left, 5/16 below and 1/16 below right, so all of e is passed on. Atkinson gives 1/8 of e to six neighbors, so only 3/4 of it is passed on. That keeps more contrast and loses detail near black and white. The Bayer method adds s ((M + 0.5) / n^2 - 0.5) before rounding, where M is the Bayer matrix entry at (x mod n, y mod n). Adding 0.5 to M centers the shifts, so their mean is 0.

Error diffusion keeps the local mean: the error of each pixel is spread over its neighbors, so a patch of dots has about the mean value of the patch before. Floyd and Steinberg chose their weights so that a gray of 0.5 turns into a checkerboard. The Bayer matrix orders the thresholds so that the dots of each gray level spread out as evenly as possible.

Kernel or operator

The Inspector shows the rounding step and how the error is spread, or the ordered threshold.

Algorithm

  1. For error diffusion, visit the pixels row by row from the top left.
  2. Round each value, with the error it received, to the nearest level.
  3. Pass the rounding error on to the neighbors not yet visited.
  4. For Bayer, shift each value by the threshold of its place in the matrix, then round.

Parameters

Parameter Default Values Meaning
Method method Floyd-Steinberg Floyd-Steinberg, Atkinson, Bayer 4 x 4, Bayer 8 x 8 How the rounding error is hidden.
Levels levels 2 2 to 16 The number of evenly spaced levels per channel, from 0 to 255.
Output mapping outputMapping Clip to 0 to 255 Absolute value, Offset by 128, Stretch to full range, Clip to 0 to 255 How signed results, such as gradients, become 8-bit pixel values.
Channels channelScope RGB RGB, Brightness only Which part of the color image the filter changes.

Implementation

ditheringFilter in web/src/core/filters/artistic/dither.ts calls ditherPlane. The Bayer matrices come from bayerMatrix, which builds each size from the one half as large.

Worked example

Method Error after a 5 x 5 blur
Floyd-Steinberg 6.16
Atkinson 13.95
Bayer 4 x 4 9.64
Bayer 8 x 8 9.44

A ramp from 0 at the left to 255 at the right, 256 by 48 pixels, dithered to black and white. The error is the mean absolute difference between a 5 x 5 box blur of the result and the ramp, away from the edges. It shows how well the pattern keeps the tone when the eye blurs it. Atkinson passes on only 3/4 of the error, so it loses tone near black and white.

Complexity

  • Time: \(O(N)\)
  • Memory: \(O(N)\)

N is the number of pixels. Each pixel passes its error to at most six neighbors.

Artifacts and limitations

Limitations

  • Error diffusion depends on the scan order, so a small change can move dots far away.
  • Atkinson loses detail in the darkest and brightest areas.

Artifacts

  • Worm-like patterns in flat areas with error diffusion.
  • A visible cross-hatch with Bayer.

Tests

The fixture cases compare every pixel with a NumPy version of the same scans and matrices, for 2, 3 and 4 levels. Before the cases are written, the 4 x 4 Bayer matrix is checked against its printed form. Unit tests check the matrices, that Floyd-Steinberg keeps the mean of a flat gray, and that the Bayer matrix turns mid gray into half black and half white.

References

  • Floyd, R. W. and Steinberg, L. An adaptive algorithm for spatial grey scale. Proceedings of the Society of Information Display 17, 75 to 77, 1976.
  • Bayer, B. E. An optimum method for two-level rendition of continuous-tone pictures. IEEE International Conference on Communications, volume 1, 11 to 15, 1973.
  • Wikipedia, Atkinson dithering. https://en.wikipedia.org/wiki/Atkinson_dithering