Repository navigation
Keep high bits when saving L images with fewer PNG bits - #10708
Open
guru-bharadwaj20 wants to merge 2 commits into
Open
guru-bharadwaj20 wants to merge 2 commits into
guru-bharadwaj20 wants to merge 2 commits into
Conversation
The L;1, L;2 and L;4 packers kept the low bits of each pixel, while the unpackers read them as the high bits, so a PNG saved with bits=4 did not read back as the same image. Pack the high bits instead, adjust the Palm plugin so its output is unchanged, and reduce PNG transparency the same way.
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.
Saving an L image as PNG with
bits=1,2or4keeps the low bits of each pixel, but reading the file back treats the stored values as the high bits. So the round trip changes the image:The cause is in
Pack.c. The L;4 unpacker reads each nibble as the high bits (n * 17), butpackL4andpackL2keepx & 15andx & 3, and L;1 usespack1, which sets a bit for any non-zero pixel. The PNGbitsoption for L just picks the L;1, L;2 or L;4 rawmode, so it inherits the mismatch.Changes proposed in this pull request:
packL1,packL2andpackL4, which keep the highest 1, 2 or 4 bits of each pixel, and use them for the L rawmodes. The old low-bit functions are renamed topackP2andpackP4and stay on the P rawmodes, where the low bits are the palette index.PalmImagePluginwas the other caller of these rawmodes, and it shifted values into the low bits before packing. Both of its L paths now produce values in the high bits instead. I saved hopper, a gradient and a random image at bpp 1, 2 and 4 through both paths (plus the 1 and P modes), and the output bytes are identical to main for all 20 cases.transparencyfor an L image saved with fewer bits is shifted the same way, so the tRNS value still matches the pixels it was meant for.Checks run locally (Linux, Python 3.14):
python3 -m pytest Tests/test_lib_pack.py Tests/test_file_png.py Tests/test_file_palm.py Tests/test_image_point.py: 166 passed, 8 skipped (the Palm tests need ImageMagick, which isn't installed here, so I compared the Palm output bytes directly as described above).src/changes reverted, the newtest_Lpack test and the 4 new PNG tests fail.pre-commit runon the changed files passes.tobytes("raw", "L;4")and the other L rawmodes now give the high bits too, which the release note mentions.