---
id: "error-diffusion-dithering"
title: "Error-diffusion dithering (Floyd-Steinberg)"
domain: "npr"
stage: "post"
realtime: "offline"
platforms: [cpu, python, cuda]
year_introduced: 1976
inputs: [ldr-image, hdr-image]
outputs: [ldr-image]
style: "stylized"
dynamic: "static"
cost: "offline"
authority: "Wikipedia"
url: "https://texelatlas.vercel.app/t/error-diffusion-dithering/"
sources_verified_at: "2026-11-29"
---
# Error-diffusion dithering (Floyd-Steinberg) (Wikipedia)

> Quantizes pixels one at a time and spreads each pixel's rounding error to unvisited neighbours, giving the best tonal accuracy among simple dithers.

## Reach for it when

- banding when reducing colors to a small palette
- retro limited palette image looks posterized
- convert to 1-bit image that keeps tones
- smooth gradients on low bit depth displays

## How it works

Pixels are scanned in raster order. For each pixel, the colour plus accumulated error is rounded to the nearest palette entry, and the difference between wanted and output is distributed to neighbours not yet visited: for Floyd-Steinberg, 7/16 to the right, 3/16 below-left, 5/16 below and 1/16 below-right. Serpentine scanning reverses direction every row to reduce directional artefacts. Alternative kernels (Jarvis-Judice-Ninke, Stucki, Atkinson) trade smoothness against sharpness. The sequential dependence makes it hard to parallelise, so on GPUs it needs wavefront or tile tricks, and the pattern changes across the whole image whenever a single pixel changes.

## When to use it

Choose it for offline conversion, texture baking, and stills where detail fidelity matters. Prefer [Blue-noise dithering](https://texelatlas.vercel.app/t/blue-noise-dithering.md) or [Ordered dithering with Bayer matrices](https://texelatlas.vercel.app/t/ordered-dithering-bayer.md) for real-time 3D, because error diffusion crawls and shimmers with any change in the image.

## Cost

offline — Sequential per-pixel work; trivially fast on CPU for stills but poor fit to massively parallel shaders.
Measured GPU Weight: 123.21× baseline (extreme) — 4.0537 ms median / 4.2581 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 108.15× baseline (extreme) — 1.7196 ms median (2.36× faster on L4).
Ampere A100 scaling: 73.1× baseline (extreme) — 2.1053 ms median (1.93× faster on A100).

## Failure modes

- Pattern changes everywhere when the source changes, so animated output flickers badly
- Worm-like directional artefacts in near-flat regions
- Raster-order dependence blocks straightforward GPU parallelism
- Edge bleed of error across hard boundaries

## Dataflow

- Inputs: ldr-image, hdr-image
- Outputs: ldr-image
- Platforms: cpu, python, cuda

## Presets

### Error-diffusion dithering — split continuous tone vs Floyd-Steinberg residual stipple (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_LEVELS": 2,
  "P_DIFFUSION": 1,
  "P_PIXEL_SIZE": 2.5,
  "P_SPLIT": 0.36
}
```

```glsl
const float P_LEVELS = 2.0; // P Quant Levels [2.0, 6.0, 1.0]
const float P_DIFFUSION = 1.0; // P Error Diffusion [0.0, 1.4, 0.05]
const float P_PIXEL_SIZE = 2.5; // P Pixel Size [1.0, 6.0, 0.25]
const float P_SPLIT = 0.36; // P Split Position [0.0, 1.0, 0.02]

mat2 rot2(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }

float sdTorus(vec3 p, vec2 t) {
  vec2 q = vec2(length(p.xz) - t.x, p.y);
  return length(q) - t.y;
}

float mapObj(vec3 p) {
  vec3 q = p;
  q.xz = rot2(iTime * 0.42) * q.xz;
  q.xy = rot2(0.45 + 0.15 * sin(iTime * 0.6)) * q.xy;
  float d1 = sdTorus(q, vec2(0.62, 0.22));
  float d2 = length(p - vec3(0.0, 0.0, 0.0)) - 0.34;
  float dFloor = p.y + 0.88;
  return min(min(d1, d2), dFloor);
}

vec3 calcN(vec3 p) {
  vec2 e = vec2(0.002, 0.0);
  return normalize(vec3(
    mapObj(p + e.xyy) - mapObj(p - e.xyy),
    mapObj(p + e.yxy) - mapObj(p - e.yxy),
    mapObj(p + e.yyx) - mapObj(p - e.yyx)
  ));
}

// Continuous luminance of a 3D classical still-life scene at normalized cell coord
float sampleTone(vec2 uv) {
  vec3 ro = vec3(0.0, 0.35, 2.55);
  vec3 rd = normalize(vec3(uv, -1.55));
  float t = 0.0;
  bool hit = false;
  for (int i = 0; i < 42; i++) {
    float d = mapObj(ro + rd * t);
    if (d < 0.0025) { hit = true; break; }
    t += d;
    if (t > 8.0) break;
  }
  if (!hit) {
    // Smooth studio gradient backdrop to showcase gradient dithering quality
    return clamp(0.14 + 0.56 * smoothstep(-0.55, 0.55, uv.y) + 0.16 * exp(-length(uv - vec2(0.28, 0.26)) * 2.2), 0.0, 1.0);
  }
  vec3 p = ro + rd * t;
  vec3 n = calcN(p);
  vec3 l = normalize(vec3(0.65, 0.78, 0.55));
  vec3 l2 = normalize(vec3(-0.7, 0.25, -0.4));
  float diff = max(dot(n, l), 0.0);
  float rim = pow(1.0 - max(dot(n, -rd), 0.0), 3.0) * 0.45;
  vec3 h = normalize(l - rd);
  float spec = pow(max(dot(n, h), 0.0), 32.0) * 0.55;
  float back = max(dot(n, l2), 0.0) * 0.22;
  float ao = clamp(0.55 + 0.45 * smoothstep(-0.88, -0.3, p.y), 0.0, 1.0);
  return clamp((0.08 + 0.72 * diff + back + rim + spec) * ao, 0.0, 1.0);
}

// Local finite-horizon Floyd-Steinberg (7/16, 3/16, 5/16, 1/16) error-diffusion solver
// Runs a 5-column x 3-row causal error carry window ending at cell c
float floydSteinbergLocal(vec2 c, float pxScale, float levels, float diffWeight) {
  float errRow[5];
  for (int k = 0; k < 5; k++) errRow[k] = 0.0;

  float stepVal = 1.0 / max(levels - 1.0, 1.0);
  float outQ = 0.0;

  for (int ry = -2; ry <= 0; ry++) {
    float nextRow[5];
    for (int k = 0; k < 5; k++) nextRow[k] = 0.0;
    float carryRight = 0.0;

    // Slight blue-like phase seed on top boundary to break periodic lock
    for (int rx = 0; rx < 5; rx++) {
      vec2 sc = c + vec2(float(rx - 4), float(ry));
      vec2 suv = (sc * pxScale - 0.5 * iResolution.xy) / iResolution.y;
      float val = sampleTone(suv);
      // Add causal errors diffused from left (7/16) and previous row (3/16, 5/16, 1/16)
      float accum = val + diffWeight * (carryRight + errRow[rx]);
      float q = floor(accum / stepVal + 0.5) * stepVal;
      q = clamp(q, 0.0, 1.0);
      float err = accum - q;

      carryRight = err * (7.0 / 16.0);
      if (rx > 0) nextRow[rx - 1] += err * (3.0 / 16.0);
      nextRow[rx] += err * (5.0 / 16.0);
      if (rx < 4) nextRow[rx + 1] += err * (1.0 / 16.0);
      if (ry == 0 && rx == 4) outQ = q;
    }
    for (int k = 0; k < 5; k++) errRow[k] = nextRow[k];
  }
  return outQ;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  vec2 screenUV = fragCoord / iResolution.xy;
  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;

  float pxSize = clamp(P_PIXEL_SIZE, 1.0, 8.0);
  float levels = floor(clamp(P_LEVELS, 2.0, 8.0) + 0.5);
  float diffW = clamp(P_DIFFUSION, 0.0, 1.5);
  float splitX = clamp(P_SPLIT, 0.0, 1.0);

  vec3 inkDark = vec3(0.055, 0.06, 0.085);
  vec3 paperLight = vec3(0.95, 0.93, 0.87);

  vec3 col;
  if (screenUV.x < splitX) {
    float tone = sampleTone(uv);
    col = mix(inkDark, paperLight, tone);
  } else {
    vec2 cell = floor(fragCoord / pxSize) + 0.5;
    float q = floydSteinbergLocal(cell, pxSize, levels, diffW);
    col = mix(inkDark, paperLight, q);
  }

  if (splitX > 0.01 && splitX < 0.99 && abs(screenUV.x - splitX) < 0.004) {
    col = vec3(0.92, 0.36, 0.22);
  }

  fragColor = vec4(pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}
```

Attribution: Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).

## Sources

1. [primary] Wikipedia Floyd-Steinberg dithering. en.wikipedia.org. https://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering (verified via http on 2026-11-29)

## Cite this entry

Texel. "Error-diffusion dithering (Floyd-Steinberg)." https://texelatlas.vercel.app/t/error-diffusion-dithering/ (accessed 2026-12-01).
