Repository navigation
feat(plugin-resize): add premultiplyAlpha option to keep the colour of transparent pixels - #1502
Merged
hipstersmoothie merged 3 commits intoNov 23, 2026
Merged
Conversation
…a single pass `premultiplyAlpha` (default `true`) says what the option does rather than how: with `false` alpha is straight data, so every channel is resampled on its own and transparent pixels keep their colour. It mirrors `createImageBitmap`'s `premultiplyAlpha`. Instead of resizing twice, the flag is threaded into the code that premultiplies: the default resizer, BILINEAR, the BICUBIC/HERMITE/BEZIER interpolator and the LANCZOS/LANCZOS2/MITCHELL convolution. The convolution weights straight colour exactly as it would weight an opaque image, so opaque images give byte-identical results either way and alpha always matches the default. scale and scaleToFit pass the option on. The default resizer now rounds instead of truncating when only the width changes, so its alpha matches the premultiplied path. Co-authored-by: hskl18 <thb182003@gmail.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
cover passes it on to scale. contain does too, and with `false` copies the scaled image into the letter box instead of drawing it over the background, since alpha isn't opacity there. Co-authored-by: hskl18 <thb182003@gmail.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hipstersmoothie
force-pushed
the
sesh/jimp-2-0-premultiplyalpha-resize-option
branch
from
November 23, 2026 18:22
5550c84 to
068c0a1
Compare
hipstersmoothie
deleted the
sesh/jimp-2-0-premultiplyalpha-resize-option
branch
November 23, 2026 18:27
This was referenced Nov 23, 2026
hipstersmoothie
added a commit
that referenced
this pull request
Nov 24, 2026
* docs: Kid Studio redesign with full method reference and playgrounds (WIP) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: register sharpen, toColorSpace, otsuThreshold and the Color Spaces guide Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: register skew, affine, exposure, vibrance and the Migrate to v2 guide Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: update effects and compositing pages for 2.0 Add the sharpen page and playground op. Document dither's type and palette options, convolute/convolution divisor, bias, preserveAlpha and even-sized kernels, blur/gaussian radius changes, region clamping in pixelate, convolute and blit, composite across Jimp classes with clamped opacity and the new blend-mode error, mask luminance and alpha, centered shadow size, and invalid boxShadow colors. Remove the cautions for bugs fixed in 2.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: replace obsolete gotchas in WRITING.md with version-note guidance Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: update size, shape and transform pages for 2.0; add skew and affine - New skew and affine method pages with playgrounds (#1515) - resize/scale/scaleToFit: LANCZOS, LANCZOS2, MITCHELL (#1481), premultiplyAlpha (#1502), default resizer premultiplies (#1499), rounding fix - cover/contain: { x, y } fractional align (#1513), missing axis centered and stricter flag validation (#1499), premultiplyAlpha - crop: clamps to the image and throws for empty regions (#1501, #1511) - autocrop/autocropRect: ignoreSides/leaveBorder with cropOnlyFrames, transparent pixels match (#1499) - rotate: centered rotation and new canvas size (#1490), ResizeStrategy modes interpolate (#1508) - circle radius 0, border invalid colors throw, displace alpha/stretched map and offset { x, y } (#1499, #1497, #1514) - Playground: rotate interpolation modes, fractional align for cover/contain, displace y offset, new skew and affine ops Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: update color, pixel and constants pages for 2.0 - color(): multiply action, CSS colors in mix/multiply/xor, greyscale action matching greyscale(), ColorActionName exported, changed saturate/lighten maths - new pages: exposure(), vibrance(), otsuThreshold() - sepia(amount), threshold max "otsu" and per-channel autoGreyscale:false, contrast around 127.5, posterize nearest level, normalize flat channels, greyscale rounding, quantize colors validation - pixels: setPixelColor ignores off-image points, NaN throws, Edge.WRAP any distance, Edge as a runtime export, scan/scanIterator region clamping - concepts: color spaces section, Edge and ColorActionName, new ResizeStrategy members, JimpMime.ico, align rules, exported type names - playground: exposure, vibrance and otsuThreshold ops, multiply action, sepia amount, threshold otsu toggle, getPixelIndex uses Edge Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: update text, comparison and errors pages for 2.0 - print: box layout from x/y, alignment without maxWidth/maxHeight, whole-word wrapping, kept blank lines, cb position (#1498, #1505) - loadFont: HTTP/fetch errors, non-PNG page errors, bundled font coverage (#1475, #1498, #1509) - measureText/measureTextHeight: widest line, optional maxWidth, empty lines - hash/pHash/distance/distanceFromHash: options added in 2.0, hashes differ from 1.x (#1466, #1472) - compareHashes: base argument, decodes hash() output (#1499) - diff: options object, DiffColor, dark diff example (#1510) - errors-and-validation: re-verified every message; colors, regions, ENOENT, ICO/BMP/SVG errors (#1497, #1501, #1503, #1506, #1518, #1522) - playground: alignment without a box, blank-line preset, diffMask, diffColor and alpha controls Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: update loading, saving, formats and color space pages for 2.0 - read/fromBuffer: Uint8Array/Blob input, ENOENT for missing files, exifRotate and ReadOptions - fromBitmap/new Jimp: data is copied and length-checked, colorSpace, invalid colors throw - getBuffer/write: icc option, BMP bitPP, ICO, per-extension option typing; remove dead BMP/PNG options; PNG bitDepth 16 is broken - clone copies mime, background and ICC profile - New toColorSpace page; cssColorToHex throws and takes color() - Formats: ICO, JPEG 2000, SVG, strict WASM options, community plugins (jimp-heic) - Guides: Color Spaces restyle, exifRotate, JPEG 2000 and SVG sections with bundler notes, corrected JPEG memory limits, browser fonts path and fromBitmap copying, ICO in defaults, Node 22 - Playground encode op gains ICO; webp-example casts around the wasm-webp declaration bug Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: write the Migrate to v2 guide and update guides and home for 2.0 - guides/migrate-to-v2: every breaking change merged for 2.0 (Node 22, errors that now throw, behavior changes, removed options, types), with before/after code checked against 1.7.0 and the 2.0 build, plus a "New in 2.0" section linking the new methods, options and formats - guides/migrate-to-v1: point to the v2 guide; note Edge is a runtime export only since 2.0 and that opaque PNGs drop alpha automatically - guides/getting-started: Node.js 22, Jimp.read path vs URL rules and input types, ENOENT for missing files, new WASM formats - guides/writing-plugins: the format detect() hook, color spaces in plugins (CSS colors, keeping iccProfile on new canvases), 2.0 versions - home and methods index: sharpen and roundCorners in the playground, ICO and the new WASM formats in the feature copy, 2.0 methods in the section intros Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: flag stored-hash incompatibility and add #1357 and morphology to the v2 guide - Caution near the top: pHash/hash values differ for every image in 2.0, so 1.x hashes can't be compared with 2.0 hashes; re-hash stored images - Hashes section explains the degenerate 1.x hashes and cross-version compareHashes/distanceFromHash - Jimp.read now passes decode options for file paths and buffers (#1357) - New in 2.0: erode, dilate, open and close (#1523) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: add erode, dilate, open and close method pages Document @jimp/plugin-morphology (#1523): four method pages with scanned-document cleanup, mask, outline and ink examples, playground ops with radius and shape controls, and method entries under Blur & effects. Link the new pages from the v2 migration guide, threshold and dither. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- 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.
Fixes #819. Supersedes #1496 — builds on @hskl18's implementation (their commit is included with authorship preserved).
Builds on the premultiplied default resizer and convolution filters from #1499 (already on
main).What it does
Resizing treats alpha as opacity: colour is premultiplied by alpha, so transparent pixels don't darken their neighbours, and pixels that end up fully transparent come out as transparent black. That's wrong when alpha is data, such as a game texture with an emissive/self-illumination mask or packed channels in alpha (Unity's "Alpha Is Transparency" off). The new option turns it off:
premultiplyAlpha?: boolean, defaulttrue(current behaviour).false: straight alpha. R, G, B and A are each resampled on their own. The colour comes out exactly as if the image were opaque, and the alpha exactly as it does by default, so transparent pixels keep their colour.NEAREST_NEIGHBORcopies pixels, so it isn't affected.createImageBitmap({ premultiplyAlpha }).Changes vs #1496
separateAlpha: truepremultiplyAlpha: falseseparateAlphawas never released, so it's removed.resizeonly;scale/scaleToFit/cover/containsilently drop it (zod strips unknown keys)containcopies the scaled image into the letter box whenfalse, because drawing it over the background (source-over) would treat alpha as opacity again and drop the colour of transparent pixels.premultiplyflag is threaded into every code path that premultiplies (default resizer,BILINEAR,BICUBIC/HERMITE/BEZIER,LANCZOS/LANCZOS2/MITCHELL)Also fixed along the way: when only the width changes and the image is opaque, the default resizer truncated its float output instead of rounding it. Without the fix, straight alpha wouldn't match the default's (rounded) alpha. No existing snapshot changed.
Testing
plugins/plugin-resize, for every mode (default + all 9 strategies) at 4×4, 6×6, 3×5, 12×12, 20×17, w-only (12×8), h-only (8×5),{ h: 5 }and fractional{ w: 5.4, h: 10.6 }:premultiplyAlpha: falseand by defaultfalse, and loses it by default (exceptNEAREST_NEIGHBOR, which keeps it either way)false, alpha equals the default's alpha, and colour equals what the opaque image gives (from feat(plugin-resize): add separateAlpha to keep the colour of transparent pixels #1496's tests)falseit doesscaleandscaleToFitpass the option on;coverandcontainhave their own tests, including the letter box being copied, not compositedI also stress-checked (a) and (c) on 37×23 and 120×80 noise across 7 target sizes and all modes: 0 differing bytes out of about 2.9M.
pnpm turbo run build,lint,testfor every package depending on@jimp/plugin-resize(all pass), andtest:browserfor resize, contain and cover.Co-authored-by: hskl18 thb182003@gmail.com
🤖 Generated with Claude Code