Repository navigation
fix!: correct pixel maths in the color, threshold and quantize plugins - #1500
Merged
Merged
Conversation
- 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>
…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>
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.
Pixel-math bugs in
plugin-color,plugin-thresholdandplugin-quantizefound while writing the 2.0 docs, plus wrong JSDoc examples.What was wrong
contrast(1)computesInfinity * (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
(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)).nevenly spaced levels from 0 to 255 (n = 4→ 0, 85, 170, 255).nis rounded and clamped to 2–256.preserveAlphaoption, like CSSfeConvolveMatrix. By default alpha is still convolved (unchanged, and the same as the CSS default);preserveAlpha: trueleaves alpha alone, the wayconvolutiondoes, so edge-detection kernels don't make the image transparent. Empty kernel rows ([[]]) now fail withkernel rows must not be empty.convolute([])already threwkernel must be a matrixon main.autoGreyscale: false: each red, green and blue channel is compared withmaxand replaced separately, so the colours are kept. This matches OpenCV'sthresholdon a colour image. For a grey image the output is the same as before.otsuThresholdwithautoGreyscale: falsestill 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.colorsmust be at least 1 (colors must be at least 1), and at least 2 withpaletteQuantization: "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.hasAlpha(true when any pixel isn't fully opaque; the example results were swapped), thescanIteratorexample (undefinedj),getPixelIndex(1, 1)(16, not 2), thecolorDiffexamples (0.666… and 1), and the wasm-jpegquality@default(75, matching the schema; removed the "lossless" text copied from WebP). The plugin-colorconvolute/convolutionexample mix-up was already fixed on main.Added regression tests for each fix.
BREAKING CHANGE:
posterize,contrastandthreshold({ autoGreyscale: false })give different output.quantizethrows forcolors < 1, or< 2with rgbquant.🤖 Generated with Claude Code