Skip to content

Fix invisible 4-bit indexed PNG with transparency (#392) - #393

Open
jafin wants to merge 1 commit into
empira:masterfrom
jafin:fix/392-indexed4-png-alpha
Open

jafin wants to merge 1 commit into
empira:masterfrom
jafin:fix/392-indexed4-png-alpha

Conversation

@jafin

@jafin jafin commented Sep 21, 2026

Copy link
Copy Markdown

Fixes #392.

Cause

PngImageImporter builds the alpha mask for an indexed PNG with a tRNS chunk by looking up each pixel's palette alpha. In the Indexed4 branch, offsetAlpha was never incremented. Every pixel's alpha went to alphaMask[0], and the rest of the mask stayed 0. PdfImage then wrote that mask as the image's /SMask, so the image was fully transparent in every viewer. It was embedded and referenced from the page, but you could not see it.

The Indexed1 and Indexed8 branches do advance their offset. Only 4-bit palette images were affected. That is why converting the same image to RGBA fixed it.

Fix

Increment offsetAlpha after each pixel's alpha is written, for both pixels of the nibble pair.

Test

ImageTests.Indexed4_PNG_with_transparency_has_same_alpha_mask_as_RGBA_PNG (Core only). The test embeds the two sample images from the issue: the 4-bit indexed PNG and its RGBA conversion. It checks that the decoded /SMask of the indexed image is not all zero and is byte-for-byte equal to the RGBA image's mask.

Checked against the issue's samples (96×36, alphas 0/255/55/199/121):

non-zero alpha bytes in /SMask equal to RGBA mask
before 0 of 3456 no
after 1174 of 3456 yes

Note: I verified this with a small harness that references PdfSharp.csproj. I did not run the new test inside PdfSharp.Tests, because building that project needs the downloaded assets and I did not have them locally.

The Indexed4 branch of PngImageImporter never advanced offsetAlpha, so
every palette alpha was written to index 0 of the alpha mask and the
rest of the mask stayed zero. The /SMask therefore made the whole image
fully transparent.

Fixes empira#392.
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.

Indexed PNG with transparency is embedded but renders invisible

1 participant