Repository navigation
Conversation
wleeaf
force-pushed
the
png-packed-palette-transparency
branch
from
October 27, 2026 00:12
c095e7b to
e16f8e3
Compare
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.
What kind of change does this PR introduce?
Bug fix. A PNG image with a palette of 1, 2 or 4 bits per index and a
tRNSchunk comes out mostly transparent.tests/images/pallete-transparent-white-1bit.pngis an example: drawn withdoc.image(), only about 4,300 of its 30,400 dark pixels show up when the page is rendered at the image's size, and the rest of the image is blank.loadIndexedAlphaChannelreads the pixels png-js decodes as one palette index per byte. For fewer than 8 bits per index, png-js leaves several indices packed in each byte, so each pixel gets the alpha of a byte made up of several indices. The alpha channel also runs out of data afterwidth * height * bits / 8values, and the pixels after that stay transparent. The image data itself is fine, because it goes into the PDF as packed PNG rows with a PNG predictor.png-js's
decodePixelscan't simply be reused for these images: it unfilters rows in steps ofbits / 8bytes, a fraction of a byte, whereas PNG filters them a byte at a time. So for non-interlaced images with fewer than 8 bits per index, this change inflates and unfilters the rows itself and unpacks the indices. It addsinflateSyncto the zlib wrappers for that, usingunzlibSyncfrom fflate in the browser build. Images with 8 bits per index, and interlaced ones, go through png-js as before.The rendering of the fixture above now matches its decoding by Pillow. With PNGs I made with 4 and 2 bits per index and one transparent entry, the mean difference per channel drops from 117 and 77 to 0.1 and 0.4.
Checklist:
Pallete indexed transparency 1bit alpha channelintests/unit/png.spec.jsruns the realloadIndexedAlphaChannelon the 1-bit fixture. It checks that there is one alpha value per pixel and that 6,900 pixels are transparent and 7,600 opaque, as Pillow decodes the image. Onmasterit finds 12,737 transparent pixels.npx vitest run tests/unit(525 tests) andtests/visual(53 tests) pass, and eslint and prettier are clean on the changed files.