Skip to content

Kernel: a PNG colour key with bits above the bit depth is written as a /Mask outside the sample range #768

In review

Description

@Tim81

Why

PNG (W3C PNG 3rd edition, tRNS chunk) stores a grey or RGB colour key as two bytes per sample whatever the bit depth. When the bit
depth is below 16, only the low bits count: encoders should set the other bits to 0, and decoders must mask them to 0 before
using the value. So a PNG whose tRNS has stray high bits is still a valid PNG, and its key is the masked value.

ISO 32000-1 8.9.6.4 (and 32000-2 8.9.6.4): each integer of a colour-key Mask array shall be in the range 0 to
2^BitsPerComponent - 1.

PngImageLoader (colorKeyMask, lines 444 to 478 at 8d1f6a2) uses the raw 16-bit word for 8-bit images and multiplies it for
1, 2 and 4-bit images, without masking first.

Measured at 8d1f6a2, each PNG saved through LayoutImage.FromBytes under PDF/A-2b:

PNG tRNS bytes Written Masked key a decoder uses
2 x 1 grey, 8 bit, samples 10 80 01 10 /BitsPerComponent 8 /Mask [272 272] 16
2 x 1 grey, 4 bit, samples 5 A 00 15 /BitsPerComponent 8 /Mask [357 357] 5, written as 85
2 x 1 RGB, 8 bit, red and green 01 FF 00 00 00 00 /BitsPerComponent 8 /Mask [511 511 0 0 0 0] (255, 0, 0)

veraPDF 1.30.4 reports no failure for any of the three (it has no rule for the colour-key range). poppler (pdftoppm, 72 dpi) paints
the first pixel of each, which the PNG makes transparent: grey 16, grey 85 and red, where a white background is expected.

Sweep: git grep -n "colorKeyMask\|\"Mask\"" 8d1f6a2 -- src finds the colour-key Mask written only from PngImageLoader
(through PdfImageXObject). The other loaders express transparency as a soft mask built from bytes, so their values cannot
leave the range. The Reader's ImageDecoder reads Mask and was not examined.

Proposal

Mask the tRNS word to the source bit depth (raw16 & ((1 << bitDepth) - 1)) before the existing rescale, as the PNG
specification requires of a decoder. No PNG whose tRNS high bits are zero changes its output.

Acceptance

  • The three inputs above write [16 16], [85 85] and [255 255 0 0 0 0], and the first pixel of each is not painted.
  • Iso32000_1_8_9_6_4_AColourKeyIsPairsOfIntegersInsideTheSampleRangeAndNamesAStoredSample gains the three kinds.
  • A PNG whose tRNS high bits are zero writes the same bytes as before.

Activity

  1. added
    bug
    on Nov 29, 2026
  2. Tim81 commented on Nov 29, 2026

    @Tim81
    OwnerAuthor

    The tests that pin and accept this issue were renamed during the audit's review rounds; the acceptance section above names earlier ones. Current names, all in ConformanceAuditG4Tests (tests/VellumPdf.Kernel.Tests/ConformanceAuditG4Tests.*.cs, PR #765). A pin passes today and is rewritten by the fix; an accept test is skipped now and loses its skip with the fix; a keep test passes before and after.

    • Pin: Iso32000_1_8_9_6_4_PinsTodayAPngKeyWithBitsAboveTheDepthIsWrittenOutsideTheSampleRange (three cases: grey 8, grey 4, RGB 8)
    • Accept (skipped): Iso32000_1_8_9_6_4_APngKeyWithBitsAboveTheDepthIsMaskedBeforeItIsWritten (three cases, exact values [16 16], [85 85], [255 255 0 0 0 0])
    • Keep: Iso32000_1_8_9_6_4_AColourKeyIsPairsOfIntegersInsideTheSampleRangeAndNamesAStoredSample; also Iso32000_1_8_9_6_4_PinsTodayReducingToEightBitsWidensASixteenBitKeyToAllSamplesWithTheSameHighByte, which a masking fix leaves as it is.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions