{
 "id": "error-diffusion-dithering",
 "title": "Error-diffusion dithering (Floyd-Steinberg)",
 "aliases": [],
 "domain": "npr",
 "subdomain": "dithering",
 "stage": "post",
 "realtime": "offline",
 "platforms": [
  "cpu",
  "python",
  "cuda"
 ],
 "year_introduced": 1976,
 "inputs": [
  "ldr-image",
  "hdr-image"
 ],
 "outputs": [
  "ldr-image"
 ],
 "style": "stylized",
 "dynamic": "static",
 "summary": "Quantizes pixels one at a time and spreads each pixel's rounding error to unvisited neighbours, giving the best tonal accuracy among simple dithers.",
 "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": "Choose it for offline conversion, texture baking, and stills where detail fidelity matters. Prefer `blue-noise-dithering` or `ordered-dithering-bayer` for real-time 3D, because error diffusion crawls and shimmers with any change in the image.",
 "cost": {
  "class": "offline",
  "notes": "Sequential per-pixel work; trivially fast on CPU for stills but poor fit to massively parallel shaders."
 },
 "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"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "url:https://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering",
   "type": "web",
   "title": "Floyd-Steinberg dithering",
   "authors": [
    "Wikipedia"
   ],
   "year": null,
   "venue": "en.wikipedia.org",
   "url": "https://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20261114075253/https://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering",
     "timestamp": "20261114075253"
    },
    "page_title": "Floyd–Steinberg dithering - Wikipedia",
    "host": "en.wikipedia.org",
    "title_confirmed_by_page": true,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "http",
     "endpoint": "GET https://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "http",
     "endpoint": "GET https://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 0.95,
   "__bib": "wikipediafloyd"
  }
 ],
 "presets": [
  {
   "id": "error-diffusion-dithering-live",
   "title": "Error-diffusion dithering — split continuous tone vs Floyd-Steinberg residual stipple",
   "runtime": "glsl-fragment",
   "params": {
    "P_LEVELS": 2,
    "P_DIFFUSION": 1,
    "P_PIXEL_SIZE": 2.5,
    "P_SPLIT": 0.36
   },
   "basis": "original",
   "license_note": "Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).",
   "code": "const float P_LEVELS = 2.0; // P Quant Levels [2.0, 6.0, 1.0]\nconst float P_DIFFUSION = 1.0; // P Error Diffusion [0.0, 1.4, 0.05]\nconst float P_PIXEL_SIZE = 2.5; // P Pixel Size [1.0, 6.0, 0.25]\nconst float P_SPLIT = 0.36; // P Split Position [0.0, 1.0, 0.02]\n\nmat2 rot2(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }\n\nfloat sdTorus(vec3 p, vec2 t) {\n  vec2 q = vec2(length(p.xz) - t.x, p.y);\n  return length(q) - t.y;\n}\n\nfloat mapObj(vec3 p) {\n  vec3 q = p;\n  q.xz = rot2(iTime * 0.42) * q.xz;\n  q.xy = rot2(0.45 + 0.15 * sin(iTime * 0.6)) * q.xy;\n  float d1 = sdTorus(q, vec2(0.62, 0.22));\n  float d2 = length(p - vec3(0.0, 0.0, 0.0)) - 0.34;\n  float dFloor = p.y + 0.88;\n  return min(min(d1, d2), dFloor);\n}\n\nvec3 calcN(vec3 p) {\n  vec2 e = vec2(0.002, 0.0);\n  return normalize(vec3(\n    mapObj(p + e.xyy) - mapObj(p - e.xyy),\n    mapObj(p + e.yxy) - mapObj(p - e.yxy),\n    mapObj(p + e.yyx) - mapObj(p - e.yyx)\n  ));\n}\n\n// Continuous luminance of a 3D classical still-life scene at normalized cell coord\nfloat sampleTone(vec2 uv) {\n  vec3 ro = vec3(0.0, 0.35, 2.55);\n  vec3 rd = normalize(vec3(uv, -1.55));\n  float t = 0.0;\n  bool hit = false;\n  for (int i = 0; i < 42; i++) {\n    float d = mapObj(ro + rd * t);\n    if (d < 0.0025) { hit = true; break; }\n    t += d;\n    if (t > 8.0) break;\n  }\n  if (!hit) {\n    // Smooth studio gradient backdrop to showcase gradient dithering quality\n    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);\n  }\n  vec3 p = ro + rd * t;\n  vec3 n = calcN(p);\n  vec3 l = normalize(vec3(0.65, 0.78, 0.55));\n  vec3 l2 = normalize(vec3(-0.7, 0.25, -0.4));\n  float diff = max(dot(n, l), 0.0);\n  float rim = pow(1.0 - max(dot(n, -rd), 0.0), 3.0) * 0.45;\n  vec3 h = normalize(l - rd);\n  float spec = pow(max(dot(n, h), 0.0), 32.0) * 0.55;\n  float back = max(dot(n, l2), 0.0) * 0.22;\n  float ao = clamp(0.55 + 0.45 * smoothstep(-0.88, -0.3, p.y), 0.0, 1.0);\n  return clamp((0.08 + 0.72 * diff + back + rim + spec) * ao, 0.0, 1.0);\n}\n\n// Local finite-horizon Floyd-Steinberg (7/16, 3/16, 5/16, 1/16) error-diffusion solver\n// Runs a 5-column x 3-row causal error carry window ending at cell c\nfloat floydSteinbergLocal(vec2 c, float pxScale, float levels, float diffWeight) {\n  float errRow[5];\n  for (int k = 0; k < 5; k++) errRow[k] = 0.0;\n\n  float stepVal = 1.0 / max(levels - 1.0, 1.0);\n  float outQ = 0.0;\n\n  for (int ry = -2; ry <= 0; ry++) {\n    float nextRow[5];\n    for (int k = 0; k < 5; k++) nextRow[k] = 0.0;\n    float carryRight = 0.0;\n\n    // Slight blue-like phase seed on top boundary to break periodic lock\n    for (int rx = 0; rx < 5; rx++) {\n      vec2 sc = c + vec2(float(rx - 4), float(ry));\n      vec2 suv = (sc * pxScale - 0.5 * iResolution.xy) / iResolution.y;\n      float val = sampleTone(suv);\n      // Add causal errors diffused from left (7/16) and previous row (3/16, 5/16, 1/16)\n      float accum = val + diffWeight * (carryRight + errRow[rx]);\n      float q = floor(accum / stepVal + 0.5) * stepVal;\n      q = clamp(q, 0.0, 1.0);\n      float err = accum - q;\n\n      carryRight = err * (7.0 / 16.0);\n      if (rx > 0) nextRow[rx - 1] += err * (3.0 / 16.0);\n      nextRow[rx] += err * (5.0 / 16.0);\n      if (rx < 4) nextRow[rx + 1] += err * (1.0 / 16.0);\n      if (ry == 0 && rx == 4) outQ = q;\n    }\n    for (int k = 0; k < 5; k++) errRow[k] = nextRow[k];\n  }\n  return outQ;\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 screenUV = fragCoord / iResolution.xy;\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n\n  float pxSize = clamp(P_PIXEL_SIZE, 1.0, 8.0);\n  float levels = floor(clamp(P_LEVELS, 2.0, 8.0) + 0.5);\n  float diffW = clamp(P_DIFFUSION, 0.0, 1.5);\n  float splitX = clamp(P_SPLIT, 0.0, 1.0);\n\n  vec3 inkDark = vec3(0.055, 0.06, 0.085);\n  vec3 paperLight = vec3(0.95, 0.93, 0.87);\n\n  vec3 col;\n  if (screenUV.x < splitX) {\n    float tone = sampleTone(uv);\n    col = mix(inkDark, paperLight, tone);\n  } else {\n    vec2 cell = floor(fragCoord / pxSize) + 0.5;\n    float q = floydSteinbergLocal(cell, pxSize, levels, diffW);\n    col = mix(inkDark, paperLight, q);\n  }\n\n  if (splitX > 0.01 && splitX < 0.99 && abs(screenUV.x - splitX) < 0.004) {\n    col = vec3(0.92, 0.36, 0.22);\n  }\n\n  fragColor = vec4(pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "error-diffusion-dithering",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T16:50:13.852Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.702,
      "std": 0.2774,
      "unique_colors": 48
     },
     "image": "/specimens/error-diffusion-dithering-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "ordered-dithering-bayer",
  "blue-noise-dithering"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/npr/error-diffusion-dithering.json"
 },
 "solves": [
  "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"
 ],
 "authority": "Wikipedia",
 "url": "https://texelatlas.vercel.app/t/error-diffusion-dithering/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 123.21,
  "tier": "extreme",
  "p50_ms": 4.0537,
  "p95_ms": 4.2581,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "error-diffusion-dithering-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 108.15,
   "tier": "extreme",
   "p50_ms": 1.7196,
   "p95_ms": 1.7823,
   "speedup_x": 2.36,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 73.1,
   "tier": "extreme",
   "p50_ms": 2.1053,
   "p95_ms": 2.1364,
   "speedup_x": 1.93,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}