Upload the 16-bit sidecar unflipped so it matches the JPEG proxy.

Chrome ignores UNPACK_FLIP_Y_WEBGL for ImageBitmap uploads, so the JPEG proxy
lands in the texture top-down while the parser pre-flipped the sidecar rows.
The picture jumped upside down a few seconds after load, when the sidecar
replaced the proxy. The parser now keeps the PNG's row order.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
2026-11-28 06:59:55 +00:00
parent 2dc569256c
commit c10ff97670
4 changed files with 18 additions and 10 deletions
+6 -4
View File
@@ -149,7 +149,7 @@ describe('parsePreview16', () => {
expect(Array.from(parsed!.rgba.slice(4, 8))).toEqual([0x3c00, 0, 0x3800, 0x3c00]);
});
it('flips the rows so upload needs no UNPACK_FLIP_Y', async () => {
it('keeps the PNG row order so the sidecar matches the JPEG texture', async () => {
const parsed = await parsePreview16(
encodePng16(
[
@@ -161,9 +161,11 @@ describe('parsePreview16', () => {
),
);
expect(parsed).not.toBeNull();
// Row 0 of the returned buffer is the last image row.
expect(parsed!.rgba[0]).toBe(0);
expect(parsed!.rgba[4]).toBe(0x3c00);
// Chrome ignores UNPACK_FLIP_Y_WEBGL for ImageBitmap uploads, so the JPEG
// proxy is top-down in the texture. Flipping here put the 16-bit sidecar
// upside down once it replaced the proxy a few seconds after load.
expect(parsed!.rgba[0]).toBe(0x3c00);
expect(parsed!.rgba[4]).toBe(0);
});
it('reads the camera matrix out of the sidecar text chunk', async () => {
+8 -5
View File
@@ -8,14 +8,16 @@
* always optional.
*
* The parser only accepts what the encoder writes (16-bit, non-interlaced,
* RGB or RGBA) and returns the samples as half-float RGBA with the rows already
* flipped, because `UNPACK_FLIP_Y_WEBGL` is ignored for ArrayBufferView uploads.
* RGB or RGBA) and returns the samples as half-float RGBA in the PNG's own
* top-down row order. That is the order the JPEG proxy already lands in: an
* ImageBitmap upload ignores `UNPACK_FLIP_Y_WEBGL`, so the sidecar has to match
* it row for row or the picture jumps upside down when it swaps in.
*/
export interface Preview16 {
width: number;
height: number;
/** RGBA half-float, rows bottom-up, ready for `texImage2D` without a flip. */
/** RGBA half-float, top-down like the proxy, ready for `texImage2D` unflipped. */
rgba: Uint16Array;
/**
* Camera-to-sRGB matrix, row major, when the samples are camera native.
@@ -195,8 +197,9 @@ export async function parsePreview16(buffer: ArrayBuffer): Promise<Preview16 | n
const rgba = new Uint16Array(width * height * 4);
const opaque = table[65535];
let out = 0;
for (let y = height - 1; y >= 0; y--) {
// Bottom-up: the texture upload cannot flip an ArrayBufferView.
for (let y = 0; y < height; y++) {
// Top-down: the JPEG texture is uploaded from an ImageBitmap, which ignores
// UNPACK_FLIP_Y_WEBGL, so both sources must hand the GPU the same row order.
let src = y * (stride + 1) + 1;
for (let x = 0; x < width; x++) {
rgba[out++] = table[(raw[src] << 8) | raw[src + 1]];