import { describe, expect, it } from 'vitest'; import { buildProofCube, getActiveProofCube, loadProofProfile, parseIcc, setActiveProofCube, } from './softProof'; const ascii = (text: string) => new TextEncoder().encode(text); const clamp = (value: number) => Math.min(1, Math.max(0, value)); /** * A lut16Type element. `map` turns normalised device values into PCS values, so * a test can describe the printer's response directly instead of hand-writing * CLUT entries. */ function mft2({ inChannels = 3, outChannels = 3, grid = 2, entries = 256, counted = false, map, }: { inChannels?: number; outChannels?: number; grid?: number; /** Samples per input/output table: v4 `mft2` allows more than 256. */ entries?: number; /** Write the entry counts at offsets 48/50, as real paper profiles do. */ counted?: boolean; map: (inputs: number[]) => number[]; }): Uint8Array { const corners = grid ** inChannels; const header = counted ? 52 : 48; const size = header + inChannels * entries * 2 + corners * outChannels * 2 + outChannels * entries * 2; const data = new Uint8Array(size); const view = new DataView(data.buffer); data.set(ascii('mft2'), 0); view.setUint8(8, inChannels); view.setUint8(9, outChannels); view.setUint8(10, grid); if (counted) { view.setUint16(48, entries, false); view.setUint16(50, entries, false); } let at = header; for (let channel = 0; channel < inChannels; channel++) { for (let i = 0; i < entries; i++) { view.setUint16(at, Math.round((i / (entries - 1)) * 65535), false); at += 2; } } for (let index = 0; index < corners; index++) { const inputs = new Array(inChannels).fill(0); let rest = index; for (let channel = inChannels - 1; channel >= 0; channel--) { inputs[channel] = (rest % grid) / (grid - 1); rest = Math.floor(rest / grid); } const out = map(inputs); for (let channel = 0; channel < outChannels; channel++) { view.setUint16(at, Math.round(clamp(out[channel]) * 65535), false); at += 2; } } for (let channel = 0; channel < outChannels; channel++) { for (let i = 0; i < entries; i++) { view.setUint16(at, Math.round((i / (entries - 1)) * 65535), false); at += 2; } } return data; } /** A minimal lutAtoBType whose curves, matrix and CLUT are all absent. */ function mabIdentity(inChannels = 3, outChannels = 3): Uint8Array { const data = new Uint8Array(32); data.set(ascii('mAB '), 0); new DataView(data.buffer).setUint8(8, inChannels); new DataView(data.buffer).setUint8(9, outChannels); return data; } /** * A v4 lutAtoBType with identity A curves and a two-point identity CLUT. The * CLUT sits after a five-byte header (grid, precision, three reserved bytes), * which is easy to misread and silently garbles every v4 printer profile. */ function mabWithClut(): Uint8Array { const grid = 2; const corners = grid ** 3; const curveBytes = 12 * 3; // three empty (identity) curves const aAt = 32; const clutAt = aAt + curveBytes; const clutData = clutAt + 5; const size = (clutData + corners * 3 + 3) & ~3; const data = new Uint8Array(size); const view = new DataView(data.buffer); data.set(ascii('mAB '), 0); view.setUint8(8, 3); view.setUint8(9, 3); view.setUint32(28, aAt, false); view.setUint32(24, clutAt, false); for (let c = 0; c < 3; c++) data.set(ascii('curv'), aAt + c * 12); view.setUint8(clutAt, grid); view.setUint8(clutAt + 1, 1); // 8-bit CLUT entries for (let index = 0; index < corners; index++) { const values = new Array(3).fill(0); let rest = index; for (let c = 2; c >= 0; c--) { values[c] = rest % grid; rest = Math.floor(rest / grid); } for (let c = 0; c < 3; c++) view.setUint8(clutData + index * 3 + c, values[c] * 255); } return data; } function descTag(text: string): Uint8Array { const body = ascii(`${text}\0`); const size = (12 + body.length + 3) & ~3; const data = new Uint8Array(size); const view = new DataView(data.buffer); data.set(ascii('desc'), 0); view.setUint32(8, body.length, false); data.set(body, 12); return data; } function buildIcc({ name, colourSpace = 'RGB ', pcs = 'Lab ', a2b, b2a, }: { name: string; colourSpace?: string; pcs?: string; a2b: Uint8Array; b2a: Uint8Array; }): Uint8Array { const tags: { signature: string; bytes: Uint8Array }[] = [ { signature: 'desc', bytes: descTag(name) }, { signature: 'A2B0', bytes: a2b }, { signature: 'B2A0', bytes: b2a }, ]; let cursor = 128 + 4 + tags.length * 12; const placed = tags.map((tag) => { const at = cursor; cursor += (tag.bytes.length + 3) & ~3; return { ...tag, at }; }); const out = new Uint8Array(cursor); const view = new DataView(out.buffer); view.setUint32(0, out.length, false); out.set(ascii('prtr'), 12); out.set(ascii(colourSpace), 16); out.set(ascii(pcs), 20); out.set(ascii('acsp'), 36); view.setUint32(128, placed.length, false); placed.forEach((tag, index) => { const at = 132 + index * 12; out.set(ascii(tag.signature), at); view.setUint32(at + 4, tag.at, false); view.setUint32(at + 8, tag.bytes.length, false); out.set(tag.bytes, tag.at); }); return out; } /** One node of the proof cube; grid coordinates are sRGB encoded values. */ function cubeAt(cube: { size: number; data: Float32Array }, r: number, g: number, b: number) { const n = cube.size; const at = ((b * n + g) * n + r) * 4; return Array.from(cube.data.subarray(at, at + 4)); } const identity = (values: number[]) => values; describe('parseIcc', () => { it('reads the description, class, PCS and both tables', () => { const profile = parseIcc( buildIcc({ name: 'Matte Proof', a2b: mft2({ map: identity }), b2a: mft2({ map: identity }) }), ); expect(profile).not.toBeNull(); expect(profile!.name).toBe('Matte Proof'); expect(profile!.profileClass).toBe('prtr'); expect(profile!.pcs).toBe('Lab '); expect(profile!.a2b).not.toBeNull(); expect(profile!.b2a).not.toBeNull(); }); it('reads the counted v4 lut16 tables real paper profiles use', () => { // MOAB/Epson and the Artifex press profile both declare 4096-entry input // tables at offsets 48/50 while claiming a v2 version. Treating them as the // fixed 256-entry v2 layout puts the CLUT in the middle of the input curve. const profile = parseIcc( buildIcc({ name: 'Counted paper', a2b: mft2({ map: identity, entries: 4096, counted: true }), b2a: mft2({ map: identity, entries: 4096, counted: true }), }), ); expect(profile).not.toBeNull(); expect(profile!.a2b).not.toBeNull(); const cube = buildProofCube(profile!, 9); const mid = cubeAt(cube, 4, 4, 4); expect(mid[0]).toBeCloseTo(0.5, 2); expect(mid[0]).toBeCloseTo(mid[2], 2); }); it('reads a v4 mAB table as well as v2 lut16', () => { const profile = parseIcc( buildIcc({ name: 'V4', a2b: mabIdentity(), b2a: mabIdentity() }), ); expect(profile).not.toBeNull(); expect(profile!.a2b).not.toBeNull(); expect(profile!.b2a).not.toBeNull(); }); it('reads the CLUT of a v4 mAB table at the right offset', () => { const profile = parseIcc(buildIcc({ name: 'mAB cube', a2b: mabWithClut(), b2a: mabWithClut() }))!; expect(profile.a2b).not.toBeNull(); const cube = buildProofCube(profile, 9); const mid = cubeAt(cube, 4, 4, 4); expect(mid[0]).toBeCloseTo(0.5, 2); expect(mid[1]).toBeCloseTo(0.5, 2); expect(mid[2]).toBeCloseTo(0.5, 2); }); it('refuses data without an ICC signature', () => { expect(parseIcc(new Uint8Array(200))).toBeNull(); expect(parseIcc(new Uint8Array(4))).toBeNull(); }); }); describe('buildProofCube', () => { it('is an identity for a profile whose printer reproduces everything', () => { const profile = parseIcc( buildIcc({ name: 'Identity', a2b: mft2({ map: identity }), b2a: mft2({ map: identity }) }), )!; const cube = buildProofCube(profile, 9); expect(cube.size).toBe(9); const mid = cubeAt(cube, 4, 4, 4); expect(mid[0]).toBeCloseTo(0.5, 2); expect(mid[1]).toBeCloseTo(0.5, 2); expect(mid[2]).toBeCloseTo(0.5, 2); expect(mid[3]).toBe(0); const colour = cubeAt(cube, 7, 3, 2); expect(colour[0]).toBeGreaterThan(colour[1]); expect(colour[1]).toBeGreaterThan(colour[2]); }); it('shows a printer that cannot reach the top of the tonescale', () => { // The printed lightness is 80% of the requested one, so a mid grey comes // back darker: that is the simulation the finish pass displays. const profile = parseIcc( buildIcc({ name: 'Dim', a2b: mft2({ map: ([lightness, a, b]) => [lightness * 0.8, a, b] }), b2a: mft2({ map: identity }), }), )!; const cube = buildProofCube(profile, 9); const mid = cubeAt(cube, 4, 4, 4); expect(mid[0]).toBeLessThan(0.47); expect(mid[0]).toBeGreaterThan(0.3); }); it('flags the colours a printer cannot reproduce', () => { const profile = parseIcc( buildIcc({ name: 'Clip', // The printer renders everything 10 % darker than asked, so bright // colours come back with a large round-trip error while shadows stay // within the 2.5 ΔE band. a2b: mft2({ map: ([lightness, a, b]) => [lightness * 0.9, a, b] }), b2a: mft2({ map: identity }), }), )!; const cube = buildProofCube(profile, 9); // A bright patch cannot be reproduced, so its round-trip error exceeds the // gamut threshold without relying on a pinned endpoint. expect(cubeAt(cube, 8, 8, 8)[3]).toBe(1); // A dark patch stays inside. expect(cubeAt(cube, 1, 1, 1)[3]).toBe(0); // Raising the threshold past the error clears the warning. const loose = buildProofCube(profile, 9, Infinity); expect(cubeAt(loose, 8, 8, 8)[3]).toBe(0); }); it('treats a CMYK printer profile as four channels in, three out', () => { const profile = parseIcc( buildIcc({ name: 'CMYK', colourSpace: 'CMYK', a2b: mft2({ inChannels: 4, outChannels: 3, grid: 2, map: ([c, m, y, k]) => [1 - c - k, 1 - m - k, 1 - y - k] }), b2a: mft2({ inChannels: 3, outChannels: 4, grid: 2, map: ([r, g, b]) => [1 - r, 1 - g, 1 - b, 0] }), }), )!; const cube = buildProofCube(profile, 9); const mid = cubeAt(cube, 4, 4, 4); expect(mid[0]).toBeGreaterThan(0); expect(mid[0]).toBeLessThan(1); }); it('refuses a profile without a round trip', () => { const bytes = buildIcc({ name: 'Half', a2b: mft2({ map: identity }), b2a: mft2({ map: identity }), }); const profile = parseIcc(bytes)!; expect(() => buildProofCube({ ...profile, a2b: null }, 5)).toThrow(); }); }); describe('active proof profile', () => { it('remembers the cube the export path needs', () => { const bytes = buildIcc({ name: 'Session', a2b: mft2({ map: identity }), b2a: mft2({ map: identity }), }); setActiveProofCube(null); expect(getActiveProofCube()).toBeNull(); const cube = loadProofProfile(bytes, 5); expect(cube.name).toBe('Session'); expect(getActiveProofCube()).toBe(cube); setActiveProofCube(null); }); it('rejects a file that is not a profile', () => { expect(() => loadProofProfile(new Uint8Array(300))).toThrow(/not an ICC profile/); }); });