diff --git a/README.md b/README.md index 97b345c..09c59a1 100644 --- a/README.md +++ b/README.md @@ -984,7 +984,7 @@ Conventions worth knowing before changing code: | `src/lib/lensNames.test.ts` | EXIF lens-name normalisation, search variants and match scoring | | `src/lib/lensDefaults.test.ts` | Camera + lens default selection and rejection rules | | `src/lib/editSections.test.ts` | Selective settings copy and the ISO-aware auto-noise curve | -| `src/lib/preview16.test.ts` | 16-bit PNG proxy parsing, row flip, half-float encoding and the camera matrix chunk | +| `src/lib/preview16.test.ts` | 16-bit PNG proxy parsing, row order, half-float encoding and the camera matrix chunk | | `src/lib/dcp.test.ts` | DCP tag parsing and the camera matrix recipe against LibRaw's own values | | `src/lib/softProof.test.ts` | ICC parsing (including counted lut16 tables), the printer round trip, gamut threshold, rendering-intent selection and CMYK profiles | | `src/lib/dcpLibrary.test.ts` | Per-camera DCP library: validation, replacement, forgetting and camera matching | diff --git a/src/gl/renderer.ts b/src/gl/renderer.ts index 227e2bd..b5e5524 100644 --- a/src/gl/renderer.ts +++ b/src/gl/renderer.ts @@ -1437,6 +1437,9 @@ export class Renderer { this.srcW = bitmap.width; this.srcH = bitmap.height; gl.bindTexture(gl.TEXTURE_2D, this.srcTex); + // The flag is honoured for the ArrayBufferView sidecar but ignored for the + // ImageBitmap proxy, so the sidecar arrives already in the proxy's row order + // and must be uploaded unflipped. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); const usable = hdr && hdr.width === bitmap.width && hdr.height === bitmap.height ? hdr : null; let half = false; diff --git a/src/lib/preview16.test.ts b/src/lib/preview16.test.ts index 48e3568..82b229d 100644 --- a/src/lib/preview16.test.ts +++ b/src/lib/preview16.test.ts @@ -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 () => { diff --git a/src/lib/preview16.ts b/src/lib/preview16.ts index 4ecb0b6..3133105 100644 --- a/src/lib/preview16.ts +++ b/src/lib/preview16.ts @@ -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= 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]];