Repository navigation
fix(plugin-resize): premultiply alpha in BILINEAR, BICUBIC, HERMITE and BEZIER - #1459
Merged
Merged
Conversation
…nd BEZIER These modes interpolated RGB without weighting it by alpha, so the colour of fully transparent pixels (usually black) bled into the edges of what they surrounded and gave transparent images a dark outline. A white square on transparent black resized with BEZIER came out with edge pixels as dark as 53,53,53. Read source pixels with RGB premultiplied by alpha, interpolate in premultiplied space and un-premultiply the result. The box filter used when shrinking below half size now averages colours weighted by alpha too. Opaque pixels read back unchanged and keep the same rounding, so opaque images resize byte for byte as before. Fully transparent output pixels get black, like the default mode. NEAREST_NEIGHBOR doesn't blend and is unchanged. Fixes #442 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hipstersmoothie
deleted the
sesh/premultiply-alpha-in-resize-modes-442
branch
November 23, 2026 06:27
This was referenced Nov 23, 2026
2 of 4 tasks
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 #442
Problem
ResizeStrategy.BILINEAR,BICUBIC,HERMITEandBEZIERinterpolated each channel on its own. Fully transparent pixels still have an RGB value, usually black, and that colour got blended into the semi-transparent edge pixels. Transparent images ended up with a dark outline. For example, a white 20x20 square on transparent black resized from 40px to 15px with BEZIER had edge pixels as dark as53,53,53. The default resize mode doesn't have this problem.Fix
In
modules/resize2.ts:bilinearInterpolationand both passes of_interpolate2D(used by bicubic, hermite and bezier) now read source pixels with RGB premultiplied by alpha, and un-premultiply the result withround(p * 255 / a), clamped to 255._interpolate2D(the box filter used when shrinking below half size) now takes the alpha-weighted average of colours. Before, every pixel with alpha above zero counted equally.0,0,0, the same as the default mode. Previously BILINEAR and the upscaling path kept the RGB hidden under alpha 0. This doesn't make Make resize() not remove rgb data when alpha = 0 #819 any harder: premultiplying is a separate read step and a separate un-premultiply step, so an option to skip them could be added later.nearestNeighbordoesn't blend, so it's unchanged.Opaque images come out byte for byte the same. Premultiplying at alpha 255 returns the value unchanged, and the intermediate rounding is the same as before. I checked this by comparing main's
resize2.tswith this branch on 12,000 random opaque cases (sizes 1–30px resized to 1–40px, all four modes): there were no differences. None of the existing snapshots changed.Tests
255,255,255.pnpm build,pnpm lintandpnpm testall pass.🤖 Generated with Claude Code