import { describe, expect, it } from 'vitest'; import { halfBits, parsePreview16 } from './preview16'; /** * A stored-block zlib stream: valid deflate without a dependency, which is all * the parser needs to prove it reads a real PNG byte stream rather than a mock. */ function zlibStored(data: Uint8Array): Uint8Array { const blocks = Math.max(1, Math.ceil(data.length / 65535)); const out = new Uint8Array(2 + data.length + blocks * 5 + 4); out[0] = 0x78; out[1] = 0x01; let at = 2; for (let b = 0; b < blocks; b++) { const start = b * 65535; const len = Math.min(65535, data.length - start); out[at++] = b === blocks - 1 ? 1 : 0; out[at++] = len & 0xff; out[at++] = (len >> 8) & 0xff; out[at++] = ~len & 0xff; out[at++] = (~len >> 8) & 0xff; out.set(data.subarray(start, start + len), at); at += len; } let a = 1; let c = 0; for (const byte of data) { a = (a + byte) % 65521; c = (c + a) % 65521; } out[at++] = (c >> 8) & 0xff; out[at++] = c & 0xff; out[at++] = (a >> 8) & 0xff; out[at++] = a & 0xff; return out; } const CRC_TABLE = (() => { const table = new Int32Array(256); for (let n = 0; n < 256; n++) { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; table[n] = c; } return table; })(); function crc32(bytes: Uint8Array): number { let c = 0xffffffff; for (const byte of bytes) c = CRC_TABLE[(c ^ byte) & 0xff] ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; } function concat(parts: Uint8Array[]): Uint8Array { const total = parts.reduce((n, p) => n + p.length, 0); const out = new Uint8Array(total); let at = 0; for (const part of parts) { out.set(part, at); at += part.length; } return out; } /** Mirror of the server encoder: 16-bit RGB, Sub-filtered rows. */ function encodePng16( samples: number[][], width: number, height: number, text?: string, ): ArrayBuffer { const stride = width * 6; const raw = new Uint8Array((stride + 1) * height); const row = new Uint8Array(stride); for (let y = 0; y < height; y++) { for (let x = 0; x < width; x++) { const [r, g, b] = samples[y * width + x]; row[x * 6] = (r >> 8) & 0xff; row[x * 6 + 1] = r & 0xff; row[x * 6 + 2] = (g >> 8) & 0xff; row[x * 6 + 3] = g & 0xff; row[x * 6 + 4] = (b >> 8) & 0xff; row[x * 6 + 5] = b & 0xff; } const base = y * (stride + 1); raw[base] = 1; // Sub for (let i = 0; i < stride; i++) raw[base + 1 + i] = (row[i] - (i >= 6 ? row[i - 6] : 0)) & 0xff; } const ihdr = new Uint8Array(13); ihdr[0] = (width >>> 24) & 0xff; ihdr[1] = (width >>> 16) & 0xff; ihdr[2] = (width >>> 8) & 0xff; ihdr[3] = width & 0xff; ihdr[4] = (height >>> 24) & 0xff; ihdr[5] = (height >>> 16) & 0xff; ihdr[6] = (height >>> 8) & 0xff; ihdr[7] = height & 0xff; ihdr[8] = 16; ihdr[9] = 2; const chunk = (type: string, data: Uint8Array) => { const head = new Uint8Array(8 + data.length + 4); head[0] = (data.length >>> 24) & 0xff; head[1] = (data.length >>> 16) & 0xff; head[2] = (data.length >>> 8) & 0xff; head[3] = data.length & 0xff; for (let i = 0; i < 4; i++) head[4 + i] = type.charCodeAt(i); head.set(data, 8); const crc = crc32(concat([head.subarray(4, 8), data])); head[8 + data.length] = (crc >>> 24) & 0xff; head[9 + data.length] = (crc >>> 16) & 0xff; head[10 + data.length] = (crc >>> 8) & 0xff; head[11 + data.length] = crc & 0xff; return head; }; const png = concat([ new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk('IHDR', ihdr), ...(text ? [chunk('tEXt', new TextEncoder().encode(text))] : []), chunk('IDAT', zlibStored(raw)), chunk('IEND', new Uint8Array(0)), ]); const copy = new Uint8Array(png.length); copy.set(png); return copy.buffer; } describe('halfBits', () => { it('matches the reference encodings', () => { expect(halfBits(0)).toBe(0); expect(halfBits(1)).toBe(0x3c00); expect(halfBits(0.5)).toBe(0x3800); expect(halfBits(0.25)).toBe(0x3400); expect(halfBits(2)).toBe(0x3c00); }); }); describe('parsePreview16', () => { it('reads 16-bit samples back as half-float RGBA', async () => { const samples = [ [0, 32768, 65535], [65535, 0, 32768], ]; const parsed = await parsePreview16(encodePng16(samples, 2, 1)); expect(parsed).not.toBeNull(); expect(parsed!.width).toBe(2); expect(parsed!.height).toBe(1); expect(Array.from(parsed!.rgba.slice(0, 4))).toEqual([0, 0x3800, 0x3c00, 0x3c00]); expect(Array.from(parsed!.rgba.slice(4, 8))).toEqual([0x3c00, 0, 0x3800, 0x3c00]); }); it('keeps the PNG row order so the sidecar matches the JPEG texture', async () => { const parsed = await parsePreview16( encodePng16( [ [65535, 65535, 65535], [0, 0, 0], ], 1, 2, ), ); expect(parsed).not.toBeNull(); // 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 () => { const matrix = [1.66943598, -0.4847292, -0.18470675, -0.00096075, 1.25268972, -0.25172901, 0.04713935, -0.30006605, 1.25292671]; const parsed = await parsePreview16( encodePng16([[1000, 2000, 3000]], 1, 1, `rgbCam\0${matrix.join(',')}`), ); expect(parsed).not.toBeNull(); expect(parsed!.rgbCam).toHaveLength(9); expect(parsed!.rgbCam![0]).toBeCloseTo(matrix[0], 6); expect(parsed!.rgbCam![8]).toBeCloseTo(matrix[8], 6); }); it('leaves rgbCam undefined for canister sRGB sidecars', async () => { const parsed = await parsePreview16(encodePng16([[1, 2, 3]], 1, 1)); expect(parsed).not.toBeNull(); expect(parsed!.rgbCam).toBeUndefined(); }); it('rejects 8-bit or non-PNG input instead of guessing', async () => { const eight = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); expect(await parsePreview16(eight.buffer)).toBeNull(); expect(await parsePreview16(new Uint8Array(64).buffer)).toBeNull(); }); });