Repository navigation
Conversation
TIFF files whose first ExtraSamples entry is associated alpha store premultiplied color, but the decoder returned those samples as-is in color types that use unassociated alpha, darkening translucent pixels. Read the ExtraSamples tag and unpremultiply after decoding, for both chunky and planar layouts. Tests cover 8-bit, 16-bit and float samples, the planar path, and unassociated alpha being left unchanged. The hpredict reference images were rendered from the premultiplied samples; regenerate them (they now match Pillow within rounding). Fixes image-rs#1743
This branch has not been deployed
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 #1743
What was wrong
TIFF files can store alpha as either associated (premultiplied) or unassociated (straight). The decoder wasn't checking the
ExtraSamplestag, so it returned premultiplied color values directly as straight-alpha pixels. This caused semi-transparent areas to appear darker than expected.What I changed
The decoder now reads
ExtraSamplesand unpremultiplies the color channels when the first extra sample indicates associated alpha.The conversion handles both interleaved and planar TIFF layouts. It reuses the existing
pic-scale-safefunctions throughresize.rs, without adding another dependency or separate conversion logic.I also added regression tests for RGBA8, RGBA16, RGBA32F, planar RGBA8, and unassociated alpha. The planar test constructs a small TIFF manually because the
tiffencoder doesn't support writing planar images.Two reference PNGs for
hpredict.tiffandhpredict_packbits.tiffwere regenerated because the previous outputs contained premultiplied values. Comparing against Pillow, the corrected outputs differ by at most 1 per channel due to rounding, compared with a maximum difference of 239 in the previous references.Testing
cargo fmt --check— passed.cargo clippy -D warnings— passed with default features and with all features that build on stable Rust.cargo test— 630 passed, 0 failed, including the updated reference-image tests.--all-featuresverification wasn't possible locally becauseavif-nativerequires a system C library andbenchmarksrequires nightly Rust.AI assistance disclosure
This PR was substantially developed with AI assistance, including the implementation and tests. I reviewed the changes, requested additional planar and floating-point test coverage, strengthened the assertions, and checked the reported results locally.