Fix PNG decoder: 16-bit channels, grayscale+alpha (colorType 4), and sub-byte formats#96
Open
Fix PNG decoder: 16-bit channels, grayscale+alpha (colorType 4), and sub-byte formats#96
Conversation
…sub-byte formats Co-authored-by: Hexagon <419737+Hexagon@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Identify and fix critical issues in the code
Fix PNG decoder: 16-bit channels, grayscale+alpha (colorType 4), and sub-byte formats
Feb 20, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes critical bugs in the PNG decoder that prevented correct decoding of 16-bit per-channel images, grayscale+alpha (colorType 4) images, and sub-byte grayscale formats (bitDepth 1/2/4).
Changes:
- Fixed pixel stride calculation for 16-bit images to use actual channel size instead of hardcoded 8-bit offsets
- Added support for PNG colorType 4 (grayscale+alpha) which was previously unsupported
- Corrected scanline length calculation and pixel unpacking for sub-byte grayscale formats (bitDepth 1, 2, 4)
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/formats/png_base.ts | Core decoder fix: replaces hardcoded pixel strides with dynamic calculations based on bitDepth and colorType, adds grayscale+alpha handling, and implements proper sub-byte pixel unpacking |
| test/formats/png.test.ts | Adds three decode-only tests with minimal hand-crafted PNG binaries to verify each of the three bug fixes |
| CHANGELOG.md | Documents the three decoder fixes in the Unreleased section under Fixed |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Hexagon <419737+Hexagon@users.noreply.github.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.
unfilterAndConvertbugs insrc/formats/png_base.ts:x * bytesPerPixelstride andchannelSizeoffset instead of hardcodedx*4,x*3,xscanlineLengthnow usesceil(width * bitsPerPixel / 8)and pixel values are unpacked from packed bytesif (bitDepth < 8 && (colorType === 0 || colorType === 3))to avoid silently mishandling future indexed-color supporttest/formats/png.test.tscovering all three fixed cases💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.