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>
This commit is contained in:
2026-11-28 03:59:55 +00:00
parent 6dfddffc14
commit 0c113e7a91
53 changed files with 9217 additions and 476 deletions
+84 -18
View File
@@ -1,13 +1,54 @@
/**
* A small, self-contained ICC v2 sRGB display profile.
* Self-contained ICC v2 display profiles, generated rather than shipped as
* binary assets.
*
* It is generated rather than shipped as a binary so the export pipeline can
* tag sRGB files without adding a third-party profile asset. The matrix and
* gamma describe the standard sRGB primaries; the browser already converted
* the working pixels into sRGB when it decoded the source image.
* 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.
*/
let cached: Uint8Array | null = null;
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);
@@ -39,7 +80,7 @@ function xyzTag(x: number, y: number, z: number) {
return data;
}
function curveTag() {
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.
@@ -59,6 +100,16 @@ function curveTag() {
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);
@@ -83,20 +134,24 @@ function textTag(text: string) {
return data;
}
export function srgbProfile(): Uint8Array {
if (cached) return cached;
function buildProfile(space: OutputColorSpace): Uint8Array {
const definition = SPACES[space];
const curve =
definition.transfer === 'srgb' ? srgbCurveTag() : gammaCurveTag(definition.transfer);
const [r, g, b] = definition.primaries;
// sRGB primaries adapted to the D50 profile connection space.
const tags: { sig: string; data: Uint8Array }[] = [
{ sig: 'desc', data: descTag('sRGB IEC61966-2.1 (Luminary)') },
{ 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(0.4360747, 0.2225045, 0.0139322) },
{ sig: 'gXYZ', data: xyzTag(0.3850649, 0.7168786, 0.0971045) },
{ sig: 'bXYZ', data: xyzTag(0.1430804, 0.0606169, 0.7141733) },
{ sig: 'rTRC', data: curveTag() },
{ sig: 'gTRC', data: curveTag() },
{ sig: 'bTRC', data: curveTag() },
{ 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;
@@ -147,6 +202,17 @@ export function srgbProfile(): Uint8Array {
data.set(tag.data, tag.at);
});
cached = data;
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');
}