Skip to content

Comments

Fix PNG decoder: 16-bit channels, grayscale+alpha (colorType 4), and sub-byte formats#96

Open
Copilot wants to merge 3 commits intomainfrom
copilot/fix-critical-issues
Open

Fix PNG decoder: 16-bit channels, grayscale+alpha (colorType 4), and sub-byte formats#96
Copilot wants to merge 3 commits intomainfrom
copilot/fix-critical-issues

Conversation

Copy link
Contributor

Copilot AI commented Feb 20, 2026

  • Explore repository and identify critical issues
  • Fix PNG unfilterAndConvert bugs in src/formats/png_base.ts:
    • Fix 16-bit per-channel images: use x * bytesPerPixel stride and channelSize offset instead of hardcoded x*4, x*3, x
    • Add colorType 4 (grayscale+alpha) support (was throwing "Unsupported PNG color type: 4")
    • Fix sub-byte formats: scanlineLength now uses ceil(width * bitsPerPixel / 8) and pixel values are unpacked from packed bytes
    • Make sub-byte branch explicit: if (bitDepth < 8 && (colorType === 0 || colorType === 3)) to avoid silently mishandling future indexed-color support
  • Add 3 new tests in test/formats/png.test.ts covering all three fixed cases
  • Run precommit checks - all 552 tests pass
  • Update CHANGELOG with fix descriptions

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…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
Copilot AI requested a review from Hexagon February 20, 2026 22:35
@Hexagon Hexagon marked this pull request as ready for review February 20, 2026 22:36
@Hexagon Hexagon requested a review from Copilot February 20, 2026 22:36
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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>
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.

2 participants