8000 fix(plugin-resize): premultiply alpha in BILINEAR, BICUBIC, HERMITE and BEZIER by hipstersmoothie · Pull Request #1459 · jimp-dev/jimp · GitHub
[go: up one dir, main page]

Skip to content

fix(plugin-resize): premultiply alpha in BILINEAR, BICUBIC, HERMITE and BEZIER - #1459

Merged
hipstersmoothie merged 1 commit into
mainfrom
sesh/premultiply-alpha-in-resize-modes-442
Nov 23, 2026
Merged

hipstersmoothie merged 1 commit into
mainfrom
sesh/premultiply-alpha-in-resize-modes-442

Conversation

@hipstersmoothie
Copy link
Copy Markdown
Collaborator

Fixes #442

Problem

ResizeStrategy.BILINEAR, BICUBIC, HERMITE and BEZIER interpolated 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 as 53,53,53. The default resize mode doesn't have this problem.

Fix

In modules/resize2.ts:

  • bilinearInterpolation and both passes of _interpolate2D (used by bicubic, hermite and bezier) now read source pixels with RGB premultiplied by alpha, and un-premultiply the result with round(p * 255 / a), clamped to 255.
  • Pass 3 of _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.
  • Pixels with alpha 0 get RGB 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.
  • nearestNeighbor doesn'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.ts with 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

  • Added tests that resize the white-square-on-transparent-black image from 40px to 15, 25 and 60px in each of the four modes. They check that the edges are blended and that every visible pixel is pure white. All 12 fail on main and pass here.
  • I also ran the "Hello" PNG from the issue through each mode at 1/3 size. Every semi-transparent pixel is now 255,255,255.

pnpm build, pnpm lint and pnpm test all pass.

🤖 Generated with Claude Code

…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
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

released This issue/pull request has been released.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resizing an image with transparency

1 participant

0