Repository navigation
feat: grain() and gradient dithering to fix banding - #1568
Merged
Merged
Conversation
hipstersmoothie
force-pushed
the
sesh/grain-gradient-dithering
branch
2 times, most recently
from
November 25, 2026 03:36
aaec985 to
3a7e3ff
Compare
- 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
force-pushed
the
sesh/grain-gradient-dithering
branch
from
November 25, 2026 03:45
3a7e3ff to
0f91adf
Compare
Collaborator
Author
|
🚀 PR was released in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds film grain, and a
ditheroption for gradients, to fix the banding in large gradient backgrounds. That's the main problem in OG images.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 → #3a2d7abackground at 1200 × 630, rendered with@jimp/layout. Regenerate it withnode packages/docs/scripts/gradient-banding-images.mjs.API
Also new:
pixelNoise(x, y, seed)in@jimp/utils, the seeded hash that both features use.GrainOptionsis exported fromjimp.Dithering default: off (recommended)
I measured on a 1200 × 630 card with the OG guide's gradient:
dither: truedither: truegrain({ amount: 0.01 })grain({ amount: 0.012, monochrome: false })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
ditheron the layout schema. Non-dithered output is byte-identical to before: the sharedgradientTablein@jimp/utilsnow returns unrounded floats, andresolvePaintrounds them once whenditheris off. No existing snapshots changed, includinggradientMap's. For lossy formats the fix isgrain, and the docs say which to use for each format.Design decisions worth reviewing
plugin-color.grainis a per-pixel tone effect, likeposterize,sepiaandpixelate, which already live there. plugin-blur only mentions noise insharpen's threshold docs, and plugin-dither is about reducing to palettes. On the docs sidebar it sits under Effects, next todither.pixelNoisemixes 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.randomisn't used anywhere. Tests check that it's uniform and that neighbouring values don't correlate.size: 1, grain is triangular (TPDF, the sum of two uniforms), the standard dither shape. Forsize > 1, per-pixel noise is gaussian-blurred (σ = (size − 1) / 2) and rescaled to the same standard deviation, soamountmeans the same thing at every size (tested for sizes 1, 2, 4 and 8).sizeis 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 withgaussian.gradientTableno longer rounds (in@jimp/utils/src/gradient.ts). Callers round or dither.gradientMaprounds its own LUT values; withamount < 1a channel can now differ by at most 1, slightly more accurately, and its tests and snapshots are unchanged.monochrome: falseat quality 95.@jimp/server:grainis added todefaultAllowedEdits.apply()and@jimp/workerneed no registration: both look up plugin methods on the class. A test covers each.dithertakes only a boolean, not a strength. Grain applied to the gradient alone (for JPEG) would need a number, but that can come later.pixelNoisehas no docs page; jimp doesn't re-export it, and it's for plugin authors.Testing
pnpm exec vitest runin utils, plugin-color, plugin-draw, layout, server, worker and jimp (including typecheck). Browser tests pass for plugin-color, plugin-draw and layout.pnpm build(including the browser bundle check),pnpm lintand the docs build (pnpm buildinpackages/docs) pass.🤖 Generated with Claude Code