Skip to content

fix(picture): compare PNG tRNS keys by their low byte - #2100

Merged
ashhart merged 1 commit into
ashhart:mainfrom
shuuul:fix/png-trns-low-byte
Nov 4, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
shuuul:fix/png-trns-low-byte

Conversation

@shuuul

@shuuul shuuul commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2083.

What this changes

PNG tRNS keys for grayscale and RGB are compared with the decoded samples, which are the file's 8-bit values in the mode Pillow opens. Keys.of kept the whole 16-bit key, so a 16-bit key of 257 left sample 1 opaque, and a key of 300 left sample 44 opaque. The comment above the code already says "with the 8-bit values of its mode".

This masks the gray and RGB key components with 0xff, matching Pillow 12.3's Convert.c rgbT2a. The 1-bit grayscale rule is unchanged: key 256 still makes bit 1 transparent by its whole value.

Fixtures and test

zig/tests/fixtures/picture/trns.json holds five PNGs generated with Pillow 12.3 and their expected RGBA bytes:

Case Kind Before After
png_rgb16_key 16-bit RGB key whose low bytes match visible pixels mismatch match
png_gray16_key_low_byte 16-bit gray keys 257 and 300 mismatch match
png_gray8_key_past_255 8-bit gray key 300 (Pillow masks to 44) mismatch match
png_rgb8_key_past_255 8-bit RGB key 300 (Pillow masks to 44) mismatch match
png_gray1_key_256 1-bit gray key 256 (whole-key rule pinned) match match

picture_test.zig decodes each case and compares the SHA-256 of the RGBA bytes. With main's whole-key comparison the test reports the four masking cases as mismatches and png_gray1_key_256 as a match; with this change all five match.

zig/tests/fixtures/picture/edges.json already carried png_rgb16_key and png_gray16_key cases, but their keys are 37/92/147 and 255, all below 256, so masking does not change them and they pass either way; the new fixture uses keys that distinguish the two rules.

Checks on this head

  • Head commit: 14a2a4a (rebased on main 206e629)
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 425 and 425 problems, 0 new.
  • Tests run:
    • zig build test-picture-host: 25 passed, 4 skipped (the four require the pinned libwebp). The new test is one of the 25.
    • With png.zig reverted to main's comparison, the same test fails on exactly the four masking cases and passes png_gray1_key_256; restored, it passes.
    • zig build: 31/31 steps.
    • zig build test test-golden: 509/509 steps; 4,664 of 4,696 tests passed, 32 skipped; server goldens 322/335 with 13 known differences and 0 unexpected; security 55/55.
  • CI: waiting on this pull request.
  • Not run: GPU and checkpoint tests; the change is host-only picture decoding.

Receipt

Host-only change to the native picture decoder; no inference, checkpoint, kernel or backend path is reached.

  • Environment: ashhart/TensorFold 45876f5f, Zig 0.17.0, aarch64 Linux, host only, no GPU.
  • Paths and platforms run: zig/src/core/picture/png.zig and its test on aarch64 Linux. Platform-specific decoder paths are unchanged.
  • Exactness: n/a; the expected bytes are Pillow 12.3's and the fixtures pin them.
  • Decode speed: n/a for a key comparison on PNG decode; no measurement supports a claim in either direction.
  • Licence of new weights or drafter: n/a, no weights.

@shuuul

shuuul commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main 206e629 (head 14a2a4a), conflict only in CHANGELOG.md; the PNG line now names the affected files and keeps the 1-bit rule. zig build test-picture-host passes on this head (25 passed, 4 skipped for the pinned libwebp).

@ashhart

ashhart commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Thanks @shuuul. This is what we agreed on #2083: the low-byte mask for gray and RGB keys, the 1-bit rule kept whole, five Pillow fixtures that tell the two rules apart, and a CHANGELOG line that says which pictures change. Ready to land.

Match Pillow RGB and grayscale transparency conversion while preserving mode 1 whole-key behavior. Five Pillow 12.3 fixtures cover 16-bit RGB and grayscale, 8-bit out-of-range keys and a 1-bit key of 256.

Validation on upstream main f5bc3c5; these sources are unchanged at 45876f5, where only CHANGELOG.md differs: zig build (31 steps); zig build test test-golden (509/509 steps; 4664/4696 tests passed, 32 skipped; 322/335 parity, 13 known differences, 0 unexpected; 55/55 security). All commands held the shared machine lock.
@ashhart
ashhart force-pushed the fix/png-trns-low-byte branch from f7ac7b0 to 029d175 Compare November 4, 2026 19:17
@ashhart
ashhart merged commit 93e04ff into ashhart:main Nov 4, 2026
@ashhart

ashhart commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Thank you @shuuul. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Thanks @shuuul! This is in TensorFold 1.2.3, out now: https://github.com/ashhart/TensorFold/releases/tag/v1.2.3

@shuuul
shuuul deleted the fix/png-trns-low-byte branch November 9, 2026 11:02
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.

Bug: PNG tRNS transparency keys are compared whole, so 16-bit gray/RGB keys differ from Pillow

2 participants