Skip to content

Write RGB30 canvases to PNG as 16-bit RGB - #2638

Open
bardrop wants to merge 1 commit into
Automattic:masterfrom
bardrop-oss:fix-rgb30-png-export
Open

bardrop wants to merge 1 commit into
Automattic:masterfrom
bardrop-oss:fix-rgb30-png-export

Conversation

@bardrop

@bardrop bardrop commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Thanks for contributing!

  • Have you updated CHANGELOG.md?

Fixes #2625

Problem

canvas_write_png() set the bit depth to 10 for CAIRO_FORMAT_RGB30. PNG truecolor only allows 8 or 16 bits per sample, so libpng rejected the IHDR and longjmp'd out. Because the error path returns the (still successful) status, toBuffer('image/png') gave back only the 8-byte PNG signature with no error:

const canvas = createCanvas(8, 8)
const ctx = canvas.getContext('2d', { pixelFormat: 'RGB30' })
ctx.fillRect(0, 0, 8, 8)
canvas.toBuffer('image/png') // <Buffer 89 50 4e 47 0d 0a 1a 0a> + "libpng error: Invalid bit depth for RGB image"

(cairo's own write_png never hits its RGB30 → bpc = 10 case, because it coerces RGB30 to RGB24 first. This copy of the code skips that step.)

Fix

Write RGB30 as 16-bit RGB. Each row is converted from native-endian x2R10G10B10 to big-endian 16-bit samples (10-bit value scaled with (v << 6) | (v >> 4), so 0x3ff maps to 0xffff), using a one-row buffer and png_write_row(). Keeping 16 bits means no precision is lost. Other formats are unchanged.

Tests

  • New test in canvas.test.js: an RGB30 canvas exported to PNG has bit depth 16 / color type 2 and decodes back to the drawn colors. Fails on master ('IHDR' == ''), passes here.
  • npm test: 308 passing.
  • WPT: same failure list before and after.
  • Also checked by hand: a 4097×300 gradient canvas round-trips, and createPNGStream() works for RGB30.

@bardrop

bardrop commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Note on the red macOS jobs: I think they ran a stale object for Canvas.cc, not this change.

The log still shows libpng warning: Invalid bit depth in IHDR / Invalid bit depth for RGB image, which is what master does (bpc 10). This PR only edits src/PNG.h, which is included by Canvas.cc. The macOS jobs restored the zig cache from the master run (setup-zig-cache-v2-macOS-...-43343482229-1), and on macOS zig did not rebuild Canvas.cc after the header change. I see the same thing locally on macOS: after editing only PNG.h, zig build says the canvas lib is cached until Canvas.cc itself changes. Linux used a cache from the same run, rebuilt, and passed.

With a clean .zig-cache the new test passes on macOS for me. If you'd rather I avoid this, I can move the helper so Canvas.cc changes too, but that felt like working around the cache. Happy to do whatever you prefer.

@bardrop
bardrop force-pushed the fix-rgb30-png-export branch from 5306b62 to 9fba0a5 Compare November 14, 2026 00:11
The PNG encoder passed a bit depth of 10 for RGB30 surfaces. PNG
truecolor only allows 8 or 16 bits per sample, so libpng rejected the
header and toBuffer('image/png') returned just the 8-byte signature
without an error.

Widen each x2R10G10B10 row to big-endian 16-bit RGB (10-bit values
scaled to the full 16-bit range) and write the image with bit depth 16.

Fixes Automattic#2625
@bardrop
bardrop force-pushed the fix-rgb30-png-export branch from 9fba0a5 to 0563189 Compare November 14, 2026 00:26
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.

RGB30 surfaces select an invalid PNG truecolor bit depth

1 participant