import sharp from 'sharp'; import { describe, expect, it } from 'vitest'; import { cameraThumb16, encodePng16, png16Matrix, resizeRgb16 } from './imaging.mjs'; /** A sample camera-to-sRGB matrix, in the shape a DCP derivation produces. */ const MATRIX = [ 1.66943598, -0.4847292, -0.18470675, -0.00096075, 1.25268972, -0.25172901, 0.04713935, -0.30006605, 1.25292671, ]; /** Flat 16-bit RGB samples, row major. */ function flat16(width, height, rgb) { const out = new Uint16Array(width * height * 3); for (let i = 0; i < out.length; i += 3) { out[i] = rgb[0]; out[i + 1] = rgb[1]; out[i + 2] = rgb[2]; } return out; } describe('png16Matrix', () => { it('reads the nine numbers back out of the tEXt chunk', () => { const png = encodePng16(flat16(2, 1, [1000, 2000, 3000]), 2, 1, MATRIX); const parsed = png16Matrix(png); expect(parsed).not.toBeNull(); expect(parsed).toHaveLength(9); parsed.forEach((value, index) => expect(value).toBeCloseTo(MATRIX[index], 8)); }); it('returns null when the tag is absent or short', () => { expect(png16Matrix(encodePng16(flat16(1, 1, [0, 0, 0]), 1, 1))).toBeNull(); const short = Buffer.concat([Buffer.from('rgbCam\0', 'latin1'), Buffer.from('1,2,3,4')]); expect(png16Matrix(short)).toBeNull(); }); it('stops at the end of the tag instead of reading the chunk CRC', () => { // The first parser scanned for the next NUL, so a CRC with no zero byte ran // into the following chunk header and every matrix came back as NaN. const text = `rgbCam\0${MATRIX.map((value) => value.toFixed(8)).join(',')}`; const png = Buffer.concat([ Buffer.from(text, 'latin1'), Buffer.from([0x31, 0x32, 0x00, 0x33]), ]); const parsed = png16Matrix(png); expect(parsed).toHaveLength(9); parsed.forEach((value, index) => expect(value).toBeCloseTo(MATRIX[index], 8)); }); }); describe('resizeRgb16', () => { it('box-averages samples when shrinking', () => { const source = new Uint16Array([ 1000, 1000, 1000, 1000, 1000, 1000, 3000, 3000, 3000, 3000, 3000, 3000, ]); expect(Array.from(resizeRgb16(source, 4, 1, 2, 1))).toEqual([ 1000, 1000, 1000, 3000, 3000, 3000, ]); }); it('leaves an already-sized image untouched', () => { const source = new Uint16Array([1, 2, 3, 4, 5, 6]); expect(resizeRgb16(source, 2, 1, 2, 1)).toBe(source); }); }); describe('cameraThumb16', () => { it('writes a 16-bit RGB thumbnail at the requested grid size', async () => { const source = encodePng16(flat16(4, 2, [2000, 4000, 6000]), 4, 2, MATRIX); const out = await cameraThumb16(source, 3, 2); expect(out).not.toBeNull(); // PNG signature, then IHDR: length at 8, type at 12, data from 16. expect(out.readUInt32BE(16)).toBe(3); expect(out.readUInt32BE(20)).toBe(2); expect(out[24]).toBe(16); // bit depth expect(out[25]).toBe(2); // colour type RGB }); it('keeps the full 16-bit samples, not just their high bytes', async () => { // sharp's raw output is 8-bit unless the pipeline is asked for rgb16, and a // downconverted thumbnail decodes to 7/15/23 instead of 2000/4000/6000. const source = encodePng16(flat16(4, 2, [2000, 4000, 6000]), 4, 2, MATRIX); const out = await cameraThumb16(source, 2, 1); const { data } = await sharp(out) .toColourspace('rgb16') .raw({ depth: 'ushort' }) .toBuffer({ resolveWithObject: true }); const samples = [0, 2, 4].map((at) => data.readUInt16LE(at)); expect(samples).toEqual([2000, 4000, 6000]); }); it('keeps the camera matrix so the client can convert the samples', async () => { const source = encodePng16(flat16(4, 2, [1000, 2000, 3000]), 4, 2, MATRIX); const out = await cameraThumb16(source, 2, 1); const parsed = png16Matrix(out); expect(parsed).toHaveLength(9); parsed.forEach((value, index) => expect(value).toBeCloseTo(MATRIX[index], 8)); }); it('falls back to the matrix stored in the source sidecar', async () => { const source = encodePng16(flat16(2, 2, [500, 600, 700]), 2, 2, MATRIX); const out = await cameraThumb16(source, 1, 1); expect(png16Matrix(out)).toHaveLength(9); }); it('declines input sharp cannot decode', async () => { expect(await cameraThumb16(Buffer.alloc(64), 2, 2)).toBeNull(); }); });