Floyd–Steinberg dithering

Gallery: 4 images
Other articles that mention 'Floyd–Steinberg dithering'
Montage 1, Image 1: Original picture
Montage 1, Image 2: Original picture
Montage 1, Image 3: Original picture
A 1-bit image of the Statue of David, dithered with Floyd–Steinberg algorithm

Floyd–Steinberg dithering is an image dithering algorithm first published in 1976 by Robert W. Floyd and Louis Steinberg. It is commonly used by image manipulation software, for example, when converting an image from a Truecolor 24-bit PNG format into a GIF format, which is restricted to a maximum of 256 colors.

Section links: (5 Articles)

Implementation

The algorithm achieves dithering using error diffusion, meaning it pushes (adds) the residual quantization error of a pixel onto its neighboring pixels, to be quantized after. It spreads the debt out according to the distribution (shown as a map of the neighboring pixels):

Math Formula: {{\begin{bmatrix}&&*&{\frac {7}{16}}&\ldots \\\ldots &{\frac {3}{16}}&{\frac {5}{16}}&{\frac {1}{16}}&\ldots \\\end{bmatrix}}}

The pixel indicated with a star (*) indicates the pixel currently being scanned, and the blank pixels are the previously-scanned pixels. The specific values (7/16, 3/16, 5/16, 1/16) were originally found by trial-and-error, "guided by the desire to have a region of desired density 0.5 come out as a checkerboard pattern". The algorithm scans the image from left to right, top to bottom, quantizing pixel values one by one. Each time, the quantization error is transferred to the neighboring pixels, while not affecting the pixels that already have been quantized. Hence, if a number of pixels have been rounded downwards, it becomes more likely that the next pixel is rounded upwards, such that on average, the quantization error is close to zero.

The diffusion coefficients have the property that if the original pixel values are exactly halfway in between the nearest available colors, the dithered result is a checkerboard pattern. For example, 50% grey data could be dithered as a black-and-white checkerboard pattern. For optimal dithering, the counting of quantization errors should be in sufficient accuracy to prevent rounding errors from affecting the result.

For correct results, all values should be linearized first, rather than operating directly on sRGB values as is common for images stored on computers.

In some implementations, the horizontal direction of scan alternates between lines; this is called "serpentine scanning" or boustrophedon transform dithering.

The algorithm described above is in the following pseudocode. This works for any approximately linear encoding of pixel values, such as 8-bit integers, 16-bit integers or real numbers in the range [0, 1].

for each "y" from top to bottom do
    for each "x" from left to right do
        oldpixel := pixels["x"]["y"]
        newpixel := find_closest_palette_color(oldpixel)
        pixels["x"]["y"] := newpixel
        quant_error := oldpixel - newpixel
        pixels["x" + 1]["y"    ] := pixels["x" + 1]["y"    ] + quant_error × 7 / 16
        pixels["x" - 1]["y" + 1] := pixels["x" - 1]["y" + 1] + quant_error × 3 / 16
        pixels["x"    ]["y" + 1] := pixels["x"    ]["y" + 1] + quant_error × 5 / 16
        pixels["x" + 1]["y" + 1] := pixels["x" + 1]["y" + 1] + quant_error × 1 / 16

When converting grayscale pixel values from a high to a low bit depth (e.g. 8-bit grayscale to 1-bit black-and-white), find_closest_palette_color() may perform just a simple rounding, for example:

find_closest_palette_color(oldpixel) = round(oldpixel / 255)

The pseudocode can result in pixel values exceeding the valid values (such as greater than 255 in 8-bit grayscale images). Such values should ideally be handled by the find_closest_palette_color() function, rather than clipping the intermediate values, since a subsequent error may bring the value back into range. However, if fixed-width integers are used, wrapping of intermediate values would cause inversion of black and white, and so should be avoided.

The find_closest_palette_color() implementation is nontrivial for a palette that is not evenly distributed, however small inaccuracies in selecting the correct palette color have minimal visual impact due to error being propagated to future pixels. A nearest neighbor search in 3D is frequently used.

Section links: (6 Articles)

See also

Atkinson dithering, a variant of Floyd–Steinberg dithering designed by Bill Atkinson

Article Resources

List of all 14 referenced articles
Search for articles that mention 'Floyd–Steinberg dithering'
Read this article in another language
Download article PDF for offline access
Source on Wikipedia website

--

Gemipedia Home
Go to Article
Using English Wikipedia. Change Language?

--

Size: 6.66 KB. 74.79% smaller than original: 26.41 KB 🤮

Fetched: 1094 ms. Converted: 531 ms. 🐇

Made with 📚 and ❤️ by Acidus

All Wikipedia content is licensed under CC BY-SA 3.0