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:
@@ -0,0 +1,586 @@
|
||||
/**
|
||||
* Real ICC soft proofing.
|
||||
*
|
||||
* A printer profile describes two directions between its device values and the
|
||||
* Profile Connection Space: `A2B0` (device -> PCS) and `B2A0` (PCS -> device).
|
||||
* Simulating the print means going through both: the display colour is turned
|
||||
* into PCS, pushed into the printer's ink space, and pulled back out. Colours
|
||||
* the printer cannot hit come back changed, and that difference is exactly what
|
||||
* the soft proof shows. Saturated colours are also flagged, so the gamut warning
|
||||
* reflects the profile instead of a saturation guess.
|
||||
*
|
||||
* The round trip is baked into a 3D cube that maps sRGB to sRGB, which the
|
||||
* renderer samples with one texture fetch per pixel. Values are kept in the
|
||||
* sRGB encoding the finish pass already works in.
|
||||
*
|
||||
* Both the v2 (`lut8Type`/`lut16Type`) and v4 (`lutAtoBType`/`lutBtoAType`)
|
||||
* table layouts are read, because printer profiles ship in both.
|
||||
*/
|
||||
|
||||
const D50_WHITE = [0.9642, 1, 0.8249] as const;
|
||||
|
||||
/** Linear sRGB (D50 adapted) to XYZ, matching the primaries in `icc.ts`. */
|
||||
const RGB_TO_XYZ = [
|
||||
0.4360747, 0.3850649, 0.1430804, 0.2225045, 0.7168786, 0.0606169, 0.0139322, 0.0971045,
|
||||
0.7141733,
|
||||
];
|
||||
|
||||
/** XYZ (D50) to linear sRGB. */
|
||||
const XYZ_TO_RGB = [
|
||||
3.1338561, -1.6168667, -0.4906146, -0.9787684, 1.9161415, 0.033454, 0.0719453, -0.2289914,
|
||||
1.4052427,
|
||||
];
|
||||
|
||||
export type Pcs = 'Lab ' | 'XYZ ';
|
||||
|
||||
interface Curve {
|
||||
/** Sampled curve in 0-1, or null for a plain gamma. */
|
||||
table: Float32Array | null;
|
||||
/** ICC gamma curve (u8Fixed8), used when there is no table. */
|
||||
gamma: number;
|
||||
}
|
||||
|
||||
interface Clut {
|
||||
inChannels: number;
|
||||
outChannels: number;
|
||||
grid: number;
|
||||
data: Float32Array;
|
||||
}
|
||||
|
||||
/** One step of a LUT, in the order the profile specifies. */
|
||||
type Stage =
|
||||
| { kind: 'curves'; curves: Curve[] }
|
||||
| { kind: 'matrix'; matrix: number[] }
|
||||
| { kind: 'clut'; clut: Clut };
|
||||
|
||||
interface IccTransform {
|
||||
inChannels: number;
|
||||
outChannels: number;
|
||||
stages: Stage[];
|
||||
}
|
||||
|
||||
export interface IccProfile {
|
||||
name: string;
|
||||
/** Profile class signature: 'prtr', 'mntr', 'scnr', ... */
|
||||
profileClass: string;
|
||||
colourSpace: string;
|
||||
pcs: Pcs;
|
||||
a2b: IccTransform | null;
|
||||
b2a: IccTransform | null;
|
||||
}
|
||||
|
||||
export interface SoftProofCube {
|
||||
/** Edge length of the cube, e.g. 33. */
|
||||
size: number;
|
||||
/** RGBA: sRGB out in RGB, gamut flag in A. */
|
||||
data: Float32Array;
|
||||
name: string;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ helpers */
|
||||
|
||||
const clamp01 = (value: number) => (value <= 0 ? 0 : value >= 1 ? 1 : value);
|
||||
|
||||
function srgbToLinear(value: number): number {
|
||||
const v = clamp01(value);
|
||||
return v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
|
||||
}
|
||||
|
||||
function linearToSrgb(value: number): number {
|
||||
const v = value <= 0 ? 0 : value;
|
||||
return v <= 0.0031308 ? v * 12.92 : 1.055 * v ** (1 / 2.4) - 0.055;
|
||||
}
|
||||
|
||||
function multiply3(m: number[], v: number[]): number[] {
|
||||
return [
|
||||
m[0] * v[0] + m[1] * v[1] + m[2] * v[2],
|
||||
m[3] * v[0] + m[4] * v[1] + m[5] * v[2],
|
||||
m[6] * v[0] + m[7] * v[1] + m[8] * v[2],
|
||||
];
|
||||
}
|
||||
|
||||
function xyzToLab(xyz: number[]): [number, number, number] {
|
||||
const f = (t: number) => (t > 0.008856 ? Math.cbrt(t) : 7.787 * t + 16 / 116);
|
||||
const xr = f(xyz[0] / D50_WHITE[0]);
|
||||
const yr = f(xyz[1] / D50_WHITE[1]);
|
||||
const zr = f(xyz[2] / D50_WHITE[2]);
|
||||
return [116 * yr - 16, 500 * (xr - yr), 200 * (yr - zr)];
|
||||
}
|
||||
|
||||
function labToXyz(lab: number[]): [number, number, number] {
|
||||
const fy = (lab[0] + 16) / 116;
|
||||
const fx = fy + lab[1] / 500;
|
||||
const fz = fy - lab[2] / 200;
|
||||
const inv = (t: number) => (t ** 3 > 0.008856 ? t ** 3 : (t - 16 / 116) / 7.787);
|
||||
return [inv(fx) * D50_WHITE[0], inv(fy) * D50_WHITE[1], inv(fz) * D50_WHITE[2]];
|
||||
}
|
||||
|
||||
/** PCS values as the 0-1 fractions a LUT is indexed with. */
|
||||
function encodePcs(pcs: Pcs, lab: number[], xyz: number[]): number[] {
|
||||
if (pcs === 'Lab ') {
|
||||
return [clamp01(lab[0] / 100), clamp01((lab[1] + 128) / 255), clamp01((lab[2] + 128) / 255)];
|
||||
}
|
||||
return [clamp01(xyz[0] / 1.99997), clamp01(xyz[1] / 1.99997), clamp01(xyz[2] / 1.99997)];
|
||||
}
|
||||
|
||||
function decodePcs(pcs: Pcs, values: number[]): { lab: number[]; xyz: number[] } {
|
||||
if (pcs === 'Lab ') {
|
||||
const lab = [values[0] * 100, values[1] * 255 - 128, values[2] * 255 - 128];
|
||||
return { lab, xyz: labToXyz(lab) };
|
||||
}
|
||||
const xyz = [values[0] * 1.99997, values[1] * 1.99997, values[2] * 1.99997];
|
||||
return { lab: xyzToLab(xyz), xyz };
|
||||
}
|
||||
|
||||
function curveAt(curve: Curve, value: number): number {
|
||||
const v = clamp01(value);
|
||||
if (curve.table) {
|
||||
const last = curve.table.length - 1;
|
||||
if (last <= 0) return v;
|
||||
const at = v * last;
|
||||
const index = Math.floor(at);
|
||||
const frac = at - index;
|
||||
const a = curve.table[Math.min(last, index)];
|
||||
const b = curve.table[Math.min(last, index + 1)];
|
||||
return a + (b - a) * frac;
|
||||
}
|
||||
return v ** curve.gamma;
|
||||
}
|
||||
|
||||
/** Multilinear interpolation, first input channel varying slowest. */
|
||||
function sampleClut(clut: Clut, values: number[]): number[] {
|
||||
const { inChannels, outChannels, grid, data } = clut;
|
||||
const lo: number[] = [];
|
||||
const hi: number[] = [];
|
||||
const frac: number[] = [];
|
||||
for (let channel = 0; channel < inChannels; channel++) {
|
||||
const at = clamp01(values[channel]) * (grid - 1);
|
||||
const base = Math.min(grid - 1, Math.floor(at));
|
||||
lo.push(base);
|
||||
hi.push(Math.min(grid - 1, base + 1));
|
||||
frac.push(at - base);
|
||||
}
|
||||
const out = new Array<number>(outChannels).fill(0);
|
||||
const corners = 1 << inChannels;
|
||||
for (let corner = 0; corner < corners; corner++) {
|
||||
let weight = 1;
|
||||
let offset = 0;
|
||||
for (let channel = 0; channel < inChannels; channel++) {
|
||||
const high = (corner >> channel) & 1;
|
||||
weight *= high ? frac[channel] : 1 - frac[channel];
|
||||
offset = offset * grid + (high ? hi[channel] : lo[channel]);
|
||||
}
|
||||
if (weight === 0) continue;
|
||||
for (let channel = 0; channel < outChannels; channel++) {
|
||||
out[channel] += weight * data[offset * outChannels + channel];
|
||||
}
|
||||
}
|
||||
for (let channel = 0; channel < outChannels; channel++) out[channel] = clamp01(out[channel]);
|
||||
return out;
|
||||
}
|
||||
|
||||
function evaluate(transform: IccTransform, input: number[]): number[] {
|
||||
let values = input;
|
||||
for (const stage of transform.stages) {
|
||||
if (stage.kind === 'curves') {
|
||||
values = values.map((value, index) => curveAt(stage.curves[index], value));
|
||||
} else if (stage.kind === 'matrix') {
|
||||
values = multiply3(stage.matrix, values).map(clamp01);
|
||||
} else {
|
||||
values = sampleClut(stage.clut, values);
|
||||
}
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ parsing */
|
||||
|
||||
const identityCurve = (): Curve => ({ table: null, gamma: 1 });
|
||||
const curves = (count: number): Curve[] => Array.from({ length: count }, identityCurve);
|
||||
|
||||
function readS15(view: DataView, at: number): number {
|
||||
return view.getInt32(at, false) / 65536;
|
||||
}
|
||||
|
||||
function parseMatrix(view: DataView, at: number): number[] | null {
|
||||
const values: number[] = [];
|
||||
for (let i = 0; i < 9; i++) values.push(readS15(view, at + i * 4));
|
||||
// An all-zero matrix is how a profile says "no matrix".
|
||||
return values.some((value) => Math.abs(value) > 1e-6) ? values : null;
|
||||
}
|
||||
|
||||
function readTable(
|
||||
view: DataView,
|
||||
cursor: number,
|
||||
count: number,
|
||||
entries: number,
|
||||
wide: boolean,
|
||||
limit: number,
|
||||
): { curves: Curve[]; cursor: number } | null {
|
||||
const entry = wide ? 2 : 1;
|
||||
const scale = wide ? 65535 : 255;
|
||||
if (cursor + count * entries * entry > limit) return null;
|
||||
const table = Array.from({ length: count }, identityCurve);
|
||||
let at = cursor;
|
||||
for (let channel = 0; channel < count; channel++) {
|
||||
const samples = new Float32Array(entries);
|
||||
for (let i = 0; i < entries; i++) {
|
||||
samples[i] = (wide ? view.getUint16(at, false) : view.getUint8(at)) / scale;
|
||||
at += entry;
|
||||
}
|
||||
table[channel] = { table: samples, gamma: 1 };
|
||||
}
|
||||
return { curves: table, cursor: at };
|
||||
}
|
||||
|
||||
/**
|
||||
* lut8Type / lut16Type: input tables, a matrix for three-channel spaces, a CLUT,
|
||||
* then output tables.
|
||||
*
|
||||
* `lut16Type` has two layouts in the wild. ICC v4 stores the input and output
|
||||
* table entry counts at offsets 48 and 50, so tables can hold 4096 samples;
|
||||
* v2 fixed both at 256 with the tables starting at 48. Real vendor paper
|
||||
* profiles (MOAB/Epson, Artifex CMYK) are v2-versioned but use the counted
|
||||
* layout, so both are measured against the tag size and the layout that fills
|
||||
* the tag is taken.
|
||||
*/
|
||||
function parseMft(view: DataView, at: number, size: number, wide: boolean): IccTransform | null {
|
||||
if (size < (wide ? 52 : 48)) return null;
|
||||
const inChannels = view.getUint8(at + 8);
|
||||
const outChannels = view.getUint8(at + 9);
|
||||
const grid = view.getUint8(at + 10);
|
||||
if (!inChannels || !outChannels || grid < 2) return null;
|
||||
if (inChannels > 15 || outChannels > 15) return null;
|
||||
const matrix = inChannels === 3 ? parseMatrix(view, at + 12) : null;
|
||||
const entry = wide ? 2 : 1;
|
||||
const scale = wide ? 65535 : 255;
|
||||
const total = grid ** inChannels * outChannels;
|
||||
if (!Number.isFinite(total) || total > 16_000_000) return null;
|
||||
const limit = at + size;
|
||||
|
||||
let start = at + 48;
|
||||
let inEntries = 256;
|
||||
let outEntries = 256;
|
||||
if (wide) {
|
||||
// A counted layout only counts when its tables fit; on a legacy profile the
|
||||
// first two table samples land here and the computed size overruns the tag.
|
||||
const countedIn = view.getUint16(at + 48, false);
|
||||
const countedOut = view.getUint16(at + 50, false);
|
||||
const countedTotal =
|
||||
52 + inChannels * countedIn * entry + total * entry + outChannels * countedOut * entry;
|
||||
if (
|
||||
countedIn >= 2 &&
|
||||
countedIn <= 4096 &&
|
||||
countedOut >= 2 &&
|
||||
countedOut <= 4096 &&
|
||||
countedTotal <= size &&
|
||||
size - countedTotal <= 32
|
||||
) {
|
||||
start = at + 52;
|
||||
inEntries = countedIn;
|
||||
outEntries = countedOut;
|
||||
} else {
|
||||
const legacyTotal =
|
||||
48 + inChannels * 256 * entry + total * entry + outChannels * 256 * entry;
|
||||
if (legacyTotal > size) return null;
|
||||
}
|
||||
}
|
||||
|
||||
const input = readTable(view, start, inChannels, inEntries, wide, limit);
|
||||
if (!input) return null;
|
||||
let cursor = input.cursor;
|
||||
if (cursor + total * entry > limit) return null;
|
||||
const data = new Float32Array(total);
|
||||
for (let i = 0; i < total; i++) {
|
||||
data[i] = (wide ? view.getUint16(cursor, false) : view.getUint8(cursor)) / scale;
|
||||
cursor += entry;
|
||||
}
|
||||
const output = readTable(view, cursor, outChannels, outEntries, wide, limit);
|
||||
if (!output) return null;
|
||||
|
||||
const stages: Stage[] = [{ kind: 'curves', curves: input.curves }];
|
||||
if (matrix) stages.push({ kind: 'matrix', matrix });
|
||||
stages.push({ kind: 'clut', clut: { inChannels, outChannels, grid, data } });
|
||||
stages.push({ kind: 'curves', curves: output.curves });
|
||||
return { inChannels, outChannels, stages };
|
||||
}
|
||||
|
||||
/** A sequence of `curv` elements, one per channel, each 4-byte aligned. */
|
||||
function parseCurveSet(
|
||||
view: DataView,
|
||||
base: number,
|
||||
offset: number,
|
||||
count: number,
|
||||
limit: number,
|
||||
): Curve[] | null {
|
||||
if (!offset) return curves(count);
|
||||
let at = base + offset;
|
||||
const out: Curve[] = [];
|
||||
for (let channel = 0; channel < count; channel++) {
|
||||
if (at + 12 > limit) return null;
|
||||
const type = String.fromCharCode(
|
||||
view.getUint8(at),
|
||||
view.getUint8(at + 1),
|
||||
view.getUint8(at + 2),
|
||||
view.getUint8(at + 3),
|
||||
);
|
||||
if (type !== 'curv') return null;
|
||||
const entries = view.getUint32(at + 8, false);
|
||||
if (entries === 0) {
|
||||
out.push(identityCurve());
|
||||
at += 12;
|
||||
} else if (entries === 1) {
|
||||
out.push({ table: null, gamma: view.getUint16(at + 12, false) / 256 });
|
||||
at += 14;
|
||||
} else {
|
||||
if (entries > 4096 || at + 12 + entries * 2 > limit) return null;
|
||||
const table = new Float32Array(entries);
|
||||
for (let i = 0; i < entries; i++) {
|
||||
table[i] = view.getUint16(at + 12 + i * 2, false) / 65535;
|
||||
}
|
||||
out.push({ table, gamma: 1 });
|
||||
at += 12 + entries * 2;
|
||||
}
|
||||
at = (at + 3) & ~3;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* lutAtoBType / lutBtoAType: A curves, CLUT, M curves, matrix and B curves, in
|
||||
* the order the direction calls for.
|
||||
*/
|
||||
function parseMab(
|
||||
view: DataView,
|
||||
at: number,
|
||||
size: number,
|
||||
towardsPcs: boolean,
|
||||
): IccTransform | null {
|
||||
if (size < 32) return null;
|
||||
const inChannels = view.getUint8(at + 8);
|
||||
const outChannels = view.getUint8(at + 9);
|
||||
if (!inChannels || !outChannels || inChannels > 15 || outChannels > 15) return null;
|
||||
const bCurvesAt = view.getUint32(at + 12, false);
|
||||
const matrixAt = view.getUint32(at + 16, false);
|
||||
const mCurvesAt = view.getUint32(at + 20, false);
|
||||
const clutAt = view.getUint32(at + 24, false);
|
||||
const aCurvesAt = view.getUint32(at + 28, false);
|
||||
const limit = at + size;
|
||||
|
||||
const matrix = matrixAt ? parseMatrix(view, at + matrixAt) : null;
|
||||
const a = parseCurveSet(view, at, aCurvesAt, inChannels, limit);
|
||||
const b = parseCurveSet(view, at, bCurvesAt, outChannels, limit);
|
||||
const m = parseCurveSet(view, at, mCurvesAt, outChannels, limit);
|
||||
if (!a || !b || !m) return null;
|
||||
|
||||
let clut: Clut | null = null;
|
||||
if (clutAt) {
|
||||
const points = view.getUint8(at + clutAt);
|
||||
const precision = view.getUint8(at + clutAt + 1);
|
||||
if (!points || points < 2) return null;
|
||||
const entry = precision === 2 ? 2 : 1;
|
||||
const scale = precision === 2 ? 65535 : 255;
|
||||
const total = points ** inChannels * outChannels;
|
||||
// Grid point, precision and three reserved bytes, then the data.
|
||||
const cursor = at + clutAt + 5;
|
||||
if (cursor + total * entry > limit) return null;
|
||||
const data = new Float32Array(total);
|
||||
for (let i = 0; i < total; i++) {
|
||||
data[i] =
|
||||
(entry === 2 ? view.getUint16(cursor + i * 2, false) : view.getUint8(cursor + i)) / scale;
|
||||
}
|
||||
clut = { inChannels, outChannels, grid: points, data };
|
||||
}
|
||||
|
||||
const stages: Stage[] = [];
|
||||
if (towardsPcs) {
|
||||
stages.push({ kind: 'curves', curves: a });
|
||||
if (clut) stages.push({ kind: 'clut', clut });
|
||||
stages.push({ kind: 'curves', curves: m });
|
||||
if (matrix) stages.push({ kind: 'matrix', matrix });
|
||||
stages.push({ kind: 'curves', curves: b });
|
||||
} else {
|
||||
stages.push({ kind: 'curves', curves: b });
|
||||
if (matrix) stages.push({ kind: 'matrix', matrix });
|
||||
stages.push({ kind: 'curves', curves: m });
|
||||
if (clut) stages.push({ kind: 'clut', clut });
|
||||
stages.push({ kind: 'curves', curves: a });
|
||||
}
|
||||
return { inChannels, outChannels, stages };
|
||||
}
|
||||
|
||||
function parseDescription(view: DataView, at: number, size: number): string {
|
||||
if (!at || size < 12) return '';
|
||||
const type = String.fromCharCode(
|
||||
view.getUint8(at),
|
||||
view.getUint8(at + 1),
|
||||
view.getUint8(at + 2),
|
||||
view.getUint8(at + 3),
|
||||
);
|
||||
if (type === 'desc') {
|
||||
const length = view.getUint32(at + 8, false);
|
||||
let text = '';
|
||||
for (let i = 0; i < Math.min(length, 255, size - 12); i++) {
|
||||
const code = view.getUint8(at + 12 + i);
|
||||
if (!code) break;
|
||||
text += String.fromCharCode(code);
|
||||
}
|
||||
return text.trim();
|
||||
}
|
||||
if (type === 'mluc') {
|
||||
// Multi-localized text: the first record, whose string is UTF-16BE.
|
||||
const records = view.getUint32(at + 8, false);
|
||||
if (!records) return '';
|
||||
const length = view.getUint32(at + 20, false);
|
||||
const offset = view.getUint32(at + 24, false);
|
||||
let text = '';
|
||||
for (let i = 0; i + 1 < length && offset + i + 1 < size; i += 2) {
|
||||
const code = view.getUint16(at + offset + i, false);
|
||||
if (!code) break;
|
||||
text += String.fromCharCode(code);
|
||||
}
|
||||
return text.trim();
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an ICC profile's colour tags. Returns null when the header or the tag
|
||||
* table is not an ICC profile, so a renamed JPEG cannot become a proof profile.
|
||||
*/
|
||||
export function parseIcc(bytes: Uint8Array): IccProfile | null {
|
||||
if (bytes.length < 132) return null;
|
||||
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
||||
if (String.fromCharCode(...bytes.subarray(36, 40)) !== 'acsp') return null;
|
||||
const declared = view.getUint32(0, false);
|
||||
if (declared > bytes.length) return null;
|
||||
const ascii = (at: number) => String.fromCharCode(...bytes.subarray(at, at + 4)).trim();
|
||||
const colourSpace = ascii(16);
|
||||
const pcs = ascii(20) === 'Lab' ? 'Lab ' : 'XYZ ';
|
||||
|
||||
const count = view.getUint32(128, false);
|
||||
if (!count || 132 + count * 12 > bytes.length) return null;
|
||||
const tags = new Map<string, { at: number; size: number }>();
|
||||
for (let index = 0; index < count; index++) {
|
||||
const entry = 132 + index * 12;
|
||||
const signature = String.fromCharCode(...bytes.subarray(entry, entry + 4));
|
||||
const at = view.getUint32(entry + 4, false);
|
||||
const size = view.getUint32(entry + 8, false);
|
||||
if (at >= bytes.length || at + size > bytes.length) continue;
|
||||
tags.set(signature, { at, size });
|
||||
}
|
||||
|
||||
const transform = (signature: string, towardsPcs: boolean): IccTransform | null => {
|
||||
const tag = tags.get(signature);
|
||||
if (!tag) return null;
|
||||
const type = String.fromCharCode(...bytes.subarray(tag.at, tag.at + 4));
|
||||
if (type === 'mft1') return parseMft(view, tag.at, tag.size, false);
|
||||
if (type === 'mft2') return parseMft(view, tag.at, tag.size, true);
|
||||
if (type === 'mAB ') return parseMab(view, tag.at, tag.size, towardsPcs);
|
||||
if (type === 'mBA ') return parseMab(view, tag.at, tag.size, !towardsPcs);
|
||||
return null;
|
||||
};
|
||||
|
||||
const description = tags.get('desc');
|
||||
return {
|
||||
name: (description && parseDescription(view, description.at, description.size)) || 'ICC profile',
|
||||
profileClass: ascii(12),
|
||||
colourSpace,
|
||||
pcs,
|
||||
// Perceptual first, then relative colorimetric: a printer profile often
|
||||
// ships one and not the other.
|
||||
a2b: transform('A2B0', true) ?? transform('A2B1', true),
|
||||
b2a: transform('B2A0', false) ?? transform('B2A1', false),
|
||||
};
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- proof cube */
|
||||
|
||||
/**
|
||||
* Round-trip Lab error above which a colour is called out of gamut. Pinned ink
|
||||
* values are always flagged; this catches colours the profile bends without
|
||||
* hitting an endpoint. Measured against real paper and press profiles: see the
|
||||
* soft-proof notes in the README.
|
||||
*/
|
||||
export const GAMUT_DELTA = 2.5;
|
||||
|
||||
/**
|
||||
* Bakes the profile's device round trip into an sRGB cube.
|
||||
*
|
||||
* `size` is the cube edge; 33 keeps a smooth ramp (the renderer interpolates
|
||||
* between nodes) without a noticeable upload. Throws when the profile is missing
|
||||
* the tables the round trip needs, because silently showing the paper
|
||||
* approximation instead would misrepresent what was loaded.
|
||||
*/
|
||||
export function buildProofCube(
|
||||
profile: IccProfile,
|
||||
size = 33,
|
||||
gamutDelta = GAMUT_DELTA,
|
||||
): SoftProofCube {
|
||||
if (!profile.a2b || !profile.b2a) {
|
||||
throw new Error('This profile has no A2B/B2A tables, so it cannot be soft proofed');
|
||||
}
|
||||
const edge = Math.max(2, Math.min(65, Math.round(size)));
|
||||
const data = new Float32Array(edge * edge * edge * 4);
|
||||
let at = 0;
|
||||
const scale = edge - 1;
|
||||
for (let b = 0; b < edge; b++) {
|
||||
for (let g = 0; g < edge; g++) {
|
||||
for (let r = 0; r < edge; r++) {
|
||||
const linear = [
|
||||
srgbToLinear(r / scale),
|
||||
srgbToLinear(g / scale),
|
||||
srgbToLinear(b / scale),
|
||||
];
|
||||
const xyz = multiply3(RGB_TO_XYZ, linear);
|
||||
const lab = xyzToLab(xyz);
|
||||
const pcs = encodePcs(profile.pcs, lab, xyz);
|
||||
// Into the printer, then back out: what the paper can actually show.
|
||||
const device = evaluate(profile.b2a, pcs);
|
||||
const printed = evaluate(profile.a2b, device);
|
||||
const back = decodePcs(profile.pcs, printed);
|
||||
const rgb = multiply3(XYZ_TO_RGB, back.xyz);
|
||||
data[at++] = clamp01(linearToSrgb(rgb[0]));
|
||||
data[at++] = clamp01(linearToSrgb(rgb[1]));
|
||||
data[at++] = clamp01(linearToSrgb(rgb[2]));
|
||||
const delta = Math.hypot(
|
||||
lab[0] - back.lab[0],
|
||||
lab[1] - back.lab[1],
|
||||
lab[2] - back.lab[2],
|
||||
);
|
||||
// Pinned device values are not a gamut signal on their own: white and
|
||||
// black sit at 0/1 on every paper. An out-of-gamut colour shows up as a
|
||||
// large round-trip error instead, because the profile clamps it.
|
||||
data[at++] = delta > gamutDelta ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return { size: edge, data, name: profile.name };
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------- active proof profile */
|
||||
|
||||
/**
|
||||
* The profile loaded for this session. The renderer is handed the cube, and the
|
||||
* export path reads it here so a file rendered while the profile is loaded
|
||||
* matches the canvas. An edit only stores whether proofing is on; the table is
|
||||
* a megabyte and does not belong in a photo record.
|
||||
*/
|
||||
let active: SoftProofCube | null = null;
|
||||
|
||||
export function setActiveProofCube(cube: SoftProofCube | null) {
|
||||
active = cube;
|
||||
}
|
||||
|
||||
export function getActiveProofCube(): SoftProofCube | null {
|
||||
return active;
|
||||
}
|
||||
|
||||
/** Parses a .icc/.icm file and makes it the session's proof profile. */
|
||||
export function loadProofProfile(bytes: Uint8Array, size = 33): SoftProofCube {
|
||||
const profile = parseIcc(bytes);
|
||||
if (!profile) throw new Error('That file is not an ICC profile');
|
||||
const cube = buildProofCube(profile, size);
|
||||
setActiveProofCube(cube);
|
||||
return cube;
|
||||
}
|
||||
Reference in New Issue
Block a user