Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Grayscale depth reduction with tRNS #470

Merged
merged 2 commits into from
Dec 8, 2022

Conversation

andrews05
Copy link
Collaborator

@andrews05 andrews05 commented Dec 7, 2022

This is a fix for #426. The tRNS chunk was not being accounted for when reducing the bit depth of a grayscale image.
There are two possibilities we need to account for here:

  1. If the tRNS value can be losslessly reduced to the new bit depth, we must do so.
  2. If it can't be, this means the value does not appear in the IDAT and the tRNS must be removed.

I've added two tests for both of these cases.

The example images in #426 are in grayscale-8 with a tRNS value of 1 (near-black). This colour does not appear in the image and when the depth is reduced to 1-bit, the tRNS value of 1 now refers to white, making the image black and transparent instead of black and white.

Closes #426.

[edit]
Would be good if you could double-check my logic for validating the tRNS value. Or see if you have any ideas for achieving the same thing without a loop. I note the current grayscale reduction only supports reducing from 8-bit (not from 4 or 2), but the tRNS reduction/validation should continue to work even if that limitation is removed in future (it would be a bit of work though, so not inclined to do it right now).

@andrews05
Copy link
Collaborator Author

Just realised tRNS isn’t accounted for in 16 to 8 reductions either. It’s probably highly unlikely to happen but I should fix that too…

@andrews05
Copy link
Collaborator Author

So it seems the high bytes of the tRNS chunk are ignored for 8-bit, which means there's no need to reduce it during the 16 to 8 reduction.

@shssoichiro shssoichiro merged commit 54ee621 into shssoichiro:master Dec 8, 2022
@andrews05 andrews05 deleted the grayscale_trns branch December 8, 2022 15:03
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.

PNG files are often corrupted when inputs are monochrome.
2 participants