Skip to content

feat: grain() and gradient dithering to fix banding - #1568

Merged
hipstersmoothie merged 2 commits into
mainfrom
sesh/grain-gradient-dithering
Nov 25, 2026
Merged

hipstersmoothie merged 2 commits into
mainfrom
sesh/grain-gradient-dithering

Conversation

@hipstersmoothie

@hipstersmoothie hipstersmoothie commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Adds film grain, and a dither option for gradients, to fix the banding in large gradient backgrounds. That's the main problem in OG images.

The same purple OG-card gradient saved six ways, as it is and with contrast ×6

The left column is the image as it is, and the right column has its contrast turned up ×6 so the bands are easy to see. The source is the OG guide's #1b1840 → #3a2d7a background at 1200 × 630, rendered with @jimp/layout. Regenerate it with node packages/docs/scripts/gradient-banding-images.mjs.

API

image.grain(options?: {
  amount?: number;      // 0–1, default 0.1: each pixel moves by at most amount × 255
  size?: number;        // 1–16, default 1: grain width in px (gaussian-blurred noise, same strength)
  monochrome?: boolean; // default true: same offset for r, g, b (luma only)
  seed?: number;        // whole number, default 0
}): this

// @jimp/plugin-draw gradients, and @jimp/layout gradient fills
{ type: "linear" | "radial", ..., dither?: boolean } // default false

Also new: pixelNoise(x, y, seed) in @jimp/utils, the seeded hash that both features use. GrainOptions is exported from jimp.

Dithering default: off (recommended)

I measured on a 1200 × 630 card with the OG guide's gradient:

Banding Cost
PNG visible diagonal bands about 40 px wide 75 KB
PNG, dither: true gone (local-mean error 0.20 → 0.03 LSB) 512 KB (6.8×)
JPEG 85 bands 6 KB
JPEG 85, dither: true still bands: JPEG quantization wipes out sub-LSB noise ~6 KB
JPEG 85, grain({ amount: 0.01 }) gone 13 KB
WebP 80 blocky bands, whatever the noise 3 KB
WebP 95, grain({ amount: 0.012, monochrome: false }) mostly gone 21 KB

It also costs about 45 ms extra per full-frame 1080p gradient (25 → 70 ms).

So dithering only helps lossless output, and there it makes the file 6–7× bigger. That's a visible cost beyond fixing the banding, so I left it off by default in both plugin-draw and layout and exposed dither on the layout schema. Non-dithered output is byte-identical to before: the shared gradientTable in @jimp/utils now returns unrounded floats, and resolvePaint rounds them once when dither is off. No existing snapshots changed, including gradientMap's. For lossy formats the fix is grain, and the docs say which to use for each format.

Design decisions worth reviewing

  • Home: plugin-color. grain is a per-pixel tone effect, like posterize, sepia and pixelate, which already live there. plugin-blur only mentions noise in sharpen's threshold docs, and plugin-dither is about reducing to palettes. On the docs sidebar it sits under Effects, next to dither.
  • The PRNG is a hash, not a sequence. pixelNoise mixes x, y and the seed, then runs MurmurHash3's finalizer. A pixel's noise depends only on its position and the seed, not on image size or the order pixels are visited, so the output is deterministic across Node, browsers and workers. A bigger image has the same grain where it overlaps a smaller one. Math.random isn't used anywhere. Tests check that it's uniform and that neighbouring values don't correlate.
  • Grain distribution. At size: 1, grain is triangular (TPDF, the sum of two uniforms), the standard dither shape. For size > 1, per-pixel noise is gaussian-blurred (σ = (size − 1) / 2) and rescaled to the same standard deviation, so amount means the same thing at every size (tested for sizes 1, 2, 4 and 8). size is capped at 16 to bound the kernel cost on @jimp/server. The worst case is about 4 s at 12 MP with colored size-16 grain, which is in line with gaussian.
  • The gradient dither is RPDF ±0.5 LSB, the same offset for r, g, b and alpha. Colors that are already whole levels, like the flat areas before and after the stops, never change, and with that range no clamping is needed.
  • gradientTable no longer rounds (in @jimp/utils/src/gradient.ts). Callers round or dither. gradientMap rounds its own LUT values; with amount < 1 a channel can now differ by at most 1, slightly more accurately, and its tests and snapshots are unchanged.
  • WebP needs colored grain. Lossy WebP's bands are mostly chroma, so luma-only grain doesn't help. The docs recommend monochrome: false at quality 95.
  • @jimp/server: grain is added to defaultAllowedEdits. apply() and @jimp/worker need no registration: both look up plugin methods on the class. A test covers each.
  • Not done: dither takes only a boolean, not a strength. Grain applied to the gradient alone (for JPEG) would need a number, but that can come later. pixelNoise has no docs page; jimp doesn't re-export it, and it's for plugin authors.

Testing

  • pnpm exec vitest run in utils, plugin-color, plugin-draw, layout, server, worker and jimp (including typecheck). Browser tests pass for plugin-color, plugin-draw and layout.
  • New grain image snapshots are stable across runs.
  • Root pnpm build (including the browser bundle check), pnpm lint and the docs build (pnpm build in packages/docs) pass.

🤖 Generated with Claude Code

@hipstersmoothie hipstersmoothie added the minor Increment the minor version when merged label Nov 25, 2026
@hipstersmoothie
hipstersmoothie force-pushed the sesh/grain-gradient-dithering branch 2 times, most recently from aaec985 to 3a7e3ff Compare November 25, 2026 03:36
hipstersmoothie and others added 2 commits November 24, 2026 19:44
- image.grain({ amount, size, monochrome, seed }) in @jimp/plugin-color:
  film grain from a hash of each pixel's position and the seed, so the same
  input always gives the same output. Alpha is kept and fully transparent
  pixels are skipped.
- dither: true on @jimp/plugin-draw linear and radial gradients, and on
  @jimp/layout gradient fills: sub-LSB noise before rounding, so large,
  smooth gradients don't band. Off by default; non-dithered output is
  unchanged.
- pixelNoise(x, y, seed) in @jimp/utils, shared by both.
- grain is in @jimp/server's defaultAllowedEdits, and works with apply()
  and @jimp/worker.
- Docs: grain page, a Banding section with a PNG/JPEG/WebP comparison, and
  the dither option in the drawing, render and OG image docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hipstersmoothie
hipstersmoothie force-pushed the sesh/grain-gradient-dithering branch from 3a7e3ff to 0f91adf Compare November 25, 2026 03:45
@hipstersmoothie
hipstersmoothie merged commit a20a771 into main Nov 25, 2026
7 checks passed
@hipstersmoothie
hipstersmoothie deleted the sesh/grain-gradient-dithering branch November 25, 2026 03:51
@hipstersmoothie

Copy link
Copy Markdown
Collaborator Author

🚀 PR was released in v2.2.0 🚀

@hipstersmoothie hipstersmoothie added the released This issue/pull request has been released. label Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor Increment the minor version when merged released This issue/pull request has been released.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant