Skip to content

fix!: correct pixel maths in the color, threshold and quantize plugins - #1500

Merged
hipstersmoothie merged 2 commits into
mainfrom
sesh/jimp-2-0-color-threshold-fixes
Nov 23, 2026
Merged

hipstersmoothie merged 2 commits into
mainfrom
sesh/jimp-2-0-color-threshold-fixes

Conversation

@hipstersmoothie

@hipstersmoothie hipstersmoothie commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Pixel-math bugs in plugin-color, plugin-threshold and plugin-quantize found while writing the 2.0 docs, plus wrong JSDoc examples.

What was wrong

new Jimp({ width: 2, height: 1, color: 0x804020ff }).normalize();
// → every pixel [0, 0, 0, 255]: (v - min) * 255 / (max - min) is 0/0

image.posterize(2); // [200, 127, 64] → [0, 0, 0]: always rounded down

new Jimp({ width: 3, height: 3, color: 0xff0000ff }).convolute([
  [-1, -1, -1], [-1, 8, -1], [-1, -1, -1],
]); // alpha 0: the kernel sums to 0, and there was no way to keep alpha

image.threshold({ max: 100, autoGreyscale: false });
// [200, 10, 10] → [255, 255, 255] and [10, 200, 10] → [10, 10, 10]: grey from red alone

image.quantize({ colors: 0 });
// TypeError: Cannot set properties of undefined (setting 'redMinimum')

contrast(1) computes Infinity * (127 - 127), so 127 only became 0 because the NaN was written into the buffer. The midpoint was 127 and the result was floored, so dark and light channels were treated differently.

What changed

  • normalize: a channel that has the same value in every pixel is left unchanged.
  • contrast: scales each channel's distance from 127.5 by (1 + val) / (1 - val) and rounds halves away from the midpoint. contrast(1) sends 0–127 to 0 and 128–255 to 255 without the NaN. contrast(-1) now gives 128 (it was 127). Other values change by at most one level. contrast(invert(x)) === invert(contrast(x)).
  • posterize: rounds each channel to the nearest of n evenly spaced levels from 0 to 255 (n = 4 → 0, 85, 170, 255). n is rounded and clamped to 2–256.
  • convolute: new preserveAlpha option, like CSS feConvolveMatrix. By default alpha is still convolved (unchanged, and the same as the CSS default); preserveAlpha: true leaves alpha alone, the way convolution does, so edge-detection kernels don't make the image transparent. Empty kernel rows ([[]]) now fail with kernel rows must not be empty. convolute([]) already threw kernel must be a matrix on main.
  • threshold with autoGreyscale: false: each red, green and blue channel is compared with max and replaced separately, so the colours are kept. This matches OpenCV's threshold on a colour image. For a grey image the output is the same as before. otsuThreshold with autoGreyscale: false still reads the red channel, i.e. it expects an already-grey image, matching OpenCV, which only runs Otsu on single-channel images. The docs now say so.
  • quantize: colors must be at least 1 (colors must be at least 1), and at least 2 with paletteQuantization: "rgbquant" (colors must be at least 2 for rgbquant, since rgbquant throws "No colors in image" for 1). Added a JSDoc example and a note that alpha is quantized with the colours. That's how image-q works (the palette is RGBA), so I left it unchanged.
  • Docs: hasAlpha (true when any pixel isn't fully opaque; the example results were swapped), the scanIterator example (undefined j), getPixelIndex(1, 1) (16, not 2), the colorDiff examples (0.666… and 1), and the wasm-jpeg quality @default (75, matching the schema; removed the "lossless" text copied from WebP). The plugin-color convolute/convolution example mix-up was already fixed on main.

Added regression tests for each fix.

BREAKING CHANGE: posterize, contrast and threshold({ autoGreyscale: false }) give different output. quantize throws for colors < 1, or < 2 with rgbquant.

🤖 Generated with Claude Code

- normalize: leave a channel with a single value unchanged instead of
  dividing by zero (a solid-color image no longer turns black)
- contrast: scale around 127.5 and round halves away from it, so
  contrast(1) no longer relies on 0 * Infinity and dark/light channels
  are treated symmetrically; contrast(-1) gives 128
- posterize: round to the nearest of n evenly spaced levels (n is
  rounded and clamped to 2-256) instead of always rounding down
- convolute: leave alpha alone like convolution, so edge-detection
  kernels don't make the image transparent; clearer error for empty
  kernel rows
- threshold: with autoGreyscale false, threshold each color channel on
  its own instead of turning the image grey based on red; Otsu counts
  all three channels
- quantize: reject colors below 1 (and below 2 for rgbquant) with a
  clear message instead of a TypeError inside image-q
- docs: fix hasAlpha, scanIterator, getPixelIndex, colorDiff and
  wasm-jpeg quality JSDoc

BREAKING CHANGE: posterize, contrast, convolute (alpha) and threshold
with autoGreyscale false produce different output; quantize throws for
colors < 1, or < 2 with rgbquant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hipstersmoothie hipstersmoothie added bug there is a bug in the way jimp behaves major Increment the major version when merged labels Nov 23, 2026
…te alpha and Otsu

- convolute convolves alpha by default again, like CSS feConvolveMatrix,
  and takes preserveAlpha: true to leave it alone
- otsuThreshold with autoGreyscale false reads a single (red) channel
  again, like OpenCV, which only runs Otsu on single-channel images

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hipstersmoothie
hipstersmoothie merged commit 3e3fe83 into main Nov 23, 2026
5 checks passed
@hipstersmoothie
hipstersmoothie deleted the sesh/jimp-2-0-color-threshold-fixes branch November 23, 2026 18:16
@hipstersmoothie

Copy link
Copy Markdown
Collaborator Author

🚀 PR was released in v2.0.0 🚀

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

Labels

bug there is a bug in the way jimp behaves major Increment the major version when merged released This issue/pull request has been released.

1 participant