Files
luminary/src/lib/icc.ts
T
bhursey 0c113e7a91 Land camera-native raw profiles, real ICC soft proofing and 16-bit proxies.
Camera profiles, proofing tables and the half-float sidecar all have to agree
with the pixels the editor actually grades, so they land together with the
preset filter menu that was unusable while thumbnails streamed in.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-11-28 03:59:55 +00:00

219 lines
6.4 KiB
TypeScript

/**
* Self-contained ICC v2 display profiles, generated rather than shipped as
* binary assets.
*
* sRGB, Display P3 and Adobe RGB (1998) are all RGB display spaces, so one
* builder covers them: only the primaries, transfer curve and description
* change. The primaries are the D50-adapted values an ICC profile expects; the
* pixels themselves are converted by `colorSpace.ts` before encoding.
*/
export type OutputColorSpace = 'srgb' | 'p3' | 'adobe';
interface SpaceDefinition {
description: string;
/** D50-adapted XYZ of the red, green and blue primaries. */
primaries: [[number, number, number], [number, number, number], [number, number, number]];
/** sRGB piecewise curve, or a straight display gamma. */
transfer: 'srgb' | number;
}
const SPACES: Record<OutputColorSpace, SpaceDefinition> = {
srgb: {
description: 'sRGB IEC61966-2.1 (Luminary)',
primaries: [
[0.4360747, 0.2225045, 0.0139322],
[0.3850649, 0.7168786, 0.0971045],
[0.1430804, 0.0606169, 0.7141733],
],
transfer: 'srgb',
},
p3: {
description: 'Display P3 (Luminary)',
primaries: [
[0.5151188, 0.2411894, -0.0010657],
[0.2919771, 0.6922441, 0.0418837],
[0.1571035, 0.0665752, 0.7845088],
],
transfer: 'srgb',
},
adobe: {
description: 'Adobe RGB (1998) (Luminary)',
primaries: [
[0.6097559, 0.3111242, 0.0194811],
[0.2052401, 0.625656, 0.0608899],
[0.1493582, 0.0632197, 0.7446702],
],
transfer: 2.2,
},
};
const cache = new Map<OutputColorSpace, Uint8Array>();
function be32(out: DataView, offset: number, value: number) {
out.setUint32(offset, value >>> 0, false);
}
function be16(out: DataView, offset: number, value: number) {
out.setUint16(offset, value & 0xffff, false);
}
function s15(value: number) {
return Math.round(value * 65536) >>> 0;
}
function ascii(text: string) {
return new TextEncoder().encode(text);
}
function pad4(value: number) {
return (value + 3) & ~3;
}
function xyzTag(x: number, y: number, z: number) {
const data = new Uint8Array(20);
const view = new DataView(data.buffer);
data.set(ascii('XYZ '), 0);
be32(view, 8, s15(x));
be32(view, 12, s15(y));
be32(view, 16, s15(z));
return data;
}
function srgbCurveTag() {
// ICC v2 curveType with the piecewise sRGB transfer function sampled at
// 1024 points. A gamma-2.2 shortcut is close, but this is exact to the
// standard (within 16-bit quantisation) and still readable by old CMMs.
const samples = 1024;
const data = new Uint8Array(12 + samples * 2);
const view = new DataView(data.buffer);
data.set(ascii('curv'), 0);
be32(view, 8, samples);
for (let index = 0; index < samples; index++) {
const linear = index / (samples - 1);
const encoded =
linear <= 0.04045
? linear / 12.92
: ((linear + 0.055) / 1.055) ** 2.4;
be16(view, 12 + index * 2, Math.round(encoded * 65535));
}
return data;
}
function gammaCurveTag(gamma: number) {
// A one-entry curveType stores the gamma as u8Fixed8; 2.2 -> 563.
const data = new Uint8Array(14);
const view = new DataView(data.buffer);
data.set(ascii('curv'), 0);
be32(view, 8, 1);
be16(view, 12, Math.round(gamma * 256));
return data;
}
function descTag(text: string) {
const value = new TextEncoder().encode(text);
const size = pad4(12 + value.length + 1 + 4 + 4);
const data = new Uint8Array(size);
const view = new DataView(data.buffer);
data.set(ascii('desc'), 0);
be32(view, 8, value.length + 1);
data.set(value, 12);
data[12 + value.length] = 0;
// Unicode language code and count are both zero.
return data;
}
function textTag(text: string) {
const value = new TextEncoder().encode(text);
const size = pad4(8 + value.length + 1);
const data = new Uint8Array(size);
const view = new DataView(data.buffer);
data.set(ascii('text'), 0);
data.set(value, 8);
data[8 + value.length] = 0;
return data;
}
function buildProfile(space: OutputColorSpace): Uint8Array {
const definition = SPACES[space];
const curve =
definition.transfer === 'srgb' ? srgbCurveTag() : gammaCurveTag(definition.transfer);
const [r, g, b] = definition.primaries;
const tags: { sig: string; data: Uint8Array }[] = [
{ sig: 'desc', data: descTag(definition.description) },
{ sig: 'cprt', data: textTag('No copyright, use freely') },
{ sig: 'wtpt', data: xyzTag(0.9642, 1.0, 0.8249) },
{ sig: 'rXYZ', data: xyzTag(r[0], r[1], r[2]) },
{ sig: 'gXYZ', data: xyzTag(g[0], g[1], g[2]) },
{ sig: 'bXYZ', data: xyzTag(b[0], b[1], b[2]) },
// All three channels share the transfer function; the writer copies the
// bytes at each tag's own offset.
{ sig: 'rTRC', data: curve },
{ sig: 'gTRC', data: curve },
{ sig: 'bTRC', data: curve },
];
const tableSize = 4 + tags.length * 12;
let offset = 128 + tableSize;
const placed = tags.map((tag) => {
offset = pad4(offset);
const at = offset;
offset += tag.data.length;
return { ...tag, at };
});
const total = pad4(offset);
const data = new Uint8Array(total);
const view = new DataView(data.buffer);
// Header. Date is fixed so identical exports have identical profile bytes.
be32(view, 0, total);
data.set(ascii('LNRM'), 4);
be32(view, 8, 0x02100000);
data.set(ascii('mntr'), 12);
data.set(ascii('RGB '), 16);
data.set(ascii('XYZ '), 20);
be16(view, 24, 2026);
be16(view, 26, 1);
be16(view, 28, 1);
be16(view, 30, 0);
be16(view, 32, 0);
be16(view, 34, 0);
data.set(ascii('acsp'), 36);
be32(view, 40, 0);
be32(view, 44, 0);
be32(view, 48, 0);
be32(view, 52, 0);
be32(view, 56, 0);
be32(view, 60, 0);
be32(view, 64, 0); // perceptual rendering intent
be32(view, 68, 0x0000f6d6); // D50 X 0.9642
be32(view, 72, 0x00010000); // D50 Y 1.0
be32(view, 76, 0x0000d32d); // D50 Z 0.8249
data.set(ascii('LNRM'), 80);
// bytes 84-127 (profile ID and reserved) remain zero.
be32(view, 128, placed.length);
placed.forEach((tag, index) => {
const at = 132 + index * 12;
data.set(ascii(tag.sig), at);
be32(view, at + 4, tag.at);
be32(view, at + 8, tag.data.length);
data.set(tag.data, tag.at);
});
return data;
}
export function profileFor(space: OutputColorSpace): Uint8Array {
const hit = cache.get(space);
if (hit) return hit;
const profile = buildProfile(space);
cache.set(space, profile);
return profile;
}
export function srgbProfile(): Uint8Array {
return profileFor('srgb');
}