Skip to content

Fix color key mask for greyscale and RGB PNGs - #1865

Open
1AbdullahButt wants to merge 2 commits into
foliojs:masterfrom
1AbdullahButt:fix/1864-png-color-key
Open

1AbdullahButt wants to merge 2 commits into
foliojs:masterfrom
1AbdullahButt:fix/1864-png-color-key

Conversation

@1AbdullahButt

Copy link
Copy Markdown

What kind of change does this PR introduce?

Bug fix. Fixes #1864.

The tRNS chunk of a greyscale or truecolour PNG holds one 2-byte sample per color component. The PDF /Mask color key needs one min/max pair per component, using the image's bit depth.

  • Greyscale: png-js keeps only the first (high) byte of the sample. For 8-bit and lower images that byte is 0, so the mask keyed out black instead of the real transparent grey. For example, a 4-bit image with tRNS 0x000f got [0, 0] instead of [15, 15].
  • RGB: this path already read 3 samples. Both paths now go through one helper.

The fix reads the samples straight from the tRNS chunk (PNGImage#colorKey) and builds the mask from them for both color types. Indexed and alpha-channel images are unchanged.

Tests

  • Added two tests in tests/unit/png.spec.js using small PNGs built in the test: 8-bit grey with grey (128) transparent, and 8-bit RGB with red transparent.
  • Corrected the expected masks for the existing 16-bit white and 4-bit black transparent fixtures to match the samples actually stored in those files.
  • png.spec.js had 4 failures before the fix and all 18 tests pass after it. png, image, document, pdfa1 and pdfa2 specs all pass: 69 tests.
  • I didn't get a full tests/unit run to finish in my container (it timed out on setup), so CI is the check for the rest.
  • eslint and prettier --check pass.

Checklist:

  • Unit Tests
  • Documentation N/A
  • Update CHANGELOG.md
  • Ready to be merged

This change was written with Claude Code (an AI coding assistant).

🤖 Generated with Claude Code

The tRNS chunk of a greyscale or truecolour PNG holds one 2-byte sample
per color component. The /Mask array needs one min/max pair per
component, at the image's bit depth. Greyscale images got a mask built
from only the high byte png-js keeps, so 8-bit and lower images used
the wrong key.

Read the samples straight from the tRNS chunk and build the mask from
them for both color types.

Fixes foliojs#1864

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread lib/image/png.js
});
}

// For greyscale and truecolour images, the tRNS chunk holds one 2-byte

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create a PR in https://github.com/foliojs/png.js to fix the transparency property

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to use an existing png file instead of a synthetic one

The existing PngSuite fixtures already cover both cases: the 4-bit
greyscale fixture has the key 0x000f (was masked as 0) and the 16-bit
RGB fixture has one sample per channel (was 12 mask values).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@1AbdullahButt

Copy link
Copy Markdown
Author

Switched the tests to the existing fixtures (d16f4e0). The PngSuite 4-bit grey (tRNS 0x000f) and 16-bit RGB files already show both problems, so I removed the synthetic PNGs.

Opened foliojs/png.js#113 to read tRNS as full 2-byte samples (grayscale as one value, rgb as [r, g, b]). Once that is released, the colorKey() helper here can go and the original mask code works as is. Since pdfkit is still on png-js ^2.0.0 and 3.0 drops png.data, should that also land as a 2.x patch?

@blikblum

Copy link
Copy Markdown
Member

No. I will release a new pngjs and update dependency

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.

PNG colour-key transparency (tRNS for greyscale and truecolour) gives a wrong /Mask

2 participants