Skip to content

tiff: unpremultiply associated alpha when decoding - #3149

Open
MeherabHS wants to merge 1 commit into
image-rs:mainfrom
MeherabHS:tiff-associated-alpha
Open

MeherabHS wants to merge 1 commit into
image-rs:mainfrom
MeherabHS:tiff-associated-alpha

Conversation

@MeherabHS

Copy link
Copy Markdown

Fixes #1743

What was wrong

TIFF files can store alpha as either associated (premultiplied) or unassociated (straight). The decoder wasn't checking the ExtraSamples tag, 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 ExtraSamples and 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-safe functions through resize.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 tiff encoder doesn't support writing planar images.

Two reference PNGs for hpredict.tiff and hpredict_packbits.tiff were 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.
  • Confirmed the associated-alpha regression tests fail without the relevant fixes and pass with them.
  • Full --all-features verification wasn't possible locally because avif-native requires a system C library and benchmarks requires 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.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Some TIFF images with alpha decoded incorrectly

1 participant