Fixed PNG chunk stripping breaking image validation#54
Merged
Conversation
The TRMNL device firmware rejected grayscale PNGs with "First two header bytes are invalid" because two layers of chunk stripping were active in the pipeline: `png:exclude-chunks=all` in configureOutputFormat and an unconditional `.strip()` in applyDithering. Together they produced PNGs containing only IHDR, IDAT, and IEND. The PNG spec marks these as sufficient, but the firmware parser needs ancillary chunks (bKGD in particular), so the misleading error was a symptom of missing chunks further into the file — not the signature itself. Removed the exclude-chunks directive entirely and scoped `.strip()` to non-PNG formats. The full saving from chunk stripping was ~170 bytes on a typical 800x480 image — under 0.5% of the 50KB budget — which is not worth the firmware compatibility risk. `.strip()` still runs for JPEG/BMP where it removes larger ICC profiles and EXIF. The shared `png:compression-level` define was hoisted out of the if/else in configureOutputFormat since both branches set it identically. Locked the fix in with three tests per grayscale palette: valid PNG signature, required IHDR/IDAT/IEND chunks, and at least one ancillary chunk beyond the three-chunk minimum. The ancillary assertion checks `chunks.length > 3` rather than hard-coding a specific chunk name so it stays robust across ImageMagick version changes that might swap which default chunks get emitted. Fixes #47 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
The TRMNL device image validation rejected grayscale PNGs with "First two header bytes are invalid" because two layers of chunk stripping were active in the pipeline:
png:exclude-chunks=allin configureOutputFormat and an unconditional.strip()in applyDithering.Fixes #47