Files
luminary/server/color.mjs
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

145 lines
5.0 KiB
JavaScript

/**
* Camera-native colour conversion for the raw pipeline.
*
* LibRaw can hand back the demosaiced frame before it applies any output
* colourspace (`outputColor: 0`). Those samples are the camera's own RGB,
* white-balanced and gamma encoded, which is what a camera profile needs: the
* camera-to-sRGB step becomes a matrix the editor can swap out without
* decoding the raw again.
*
* One white point is chosen for the whole frame from the *converted* pixels, so
* the JPEG proxy and the 16-bit sidecar share an exposure. LibRaw's own
* auto-bright is deliberately off: it measures each output path separately,
* which leaves the two outputs a third of a stop apart.
*/
export function srgbToLinear(value) {
const v = value <= 0 ? 0 : value >= 1 ? 1 : value;
return v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
}
export function linearToSrgb(value) {
const v = value <= 0 ? 0 : value >= 1 ? 1 : value;
return v <= 0.0031308 ? v * 12.92 : 1.055 * v ** (1 / 2.4) - 0.055;
}
let decodeTable = null;
let encodeTable = null;
/** 16-bit sample -> linear light, for the camera's own gamma encoding. */
function linearTable() {
if (!decodeTable) {
decodeTable = new Float32Array(65536);
for (let i = 0; i < 65536; i++) decodeTable[i] = srgbToLinear(i / 65535);
}
return decodeTable;
}
/** Linear light (indexed as a 16-bit fraction) -> 8-bit sRGB. */
function encodedTable() {
if (!encodeTable) {
encodeTable = new Uint8Array(65536);
for (let i = 0; i < 65536; i++) {
encodeTable[i] = Math.round(linearToSrgb(i / 65535) * 255);
}
}
return encodeTable;
}
/** Flattens the metadata's 4x4 `rgb_cam` into the 3x3 camera -> sRGB matrix. */
export function readRgbCam(meta) {
const raw = meta?.color_data?.rgb_cam;
if (!Array.isArray(raw) || raw.length < 3) return null;
const matrix = [];
for (let row = 0; row < 3; row++) {
const values = raw[row];
if (!Array.isArray(values) || values.length < 3) return null;
for (let col = 0; col < 3; col++) {
const value = Number(values[col]);
if (!Number.isFinite(value)) return null;
matrix.push(value);
}
}
// A zero matrix would render the whole frame black; that is a metadata
// failure, not a look, so fall back to LibRaw's own conversion.
if (matrix.every((value) => Math.abs(value) < 1e-6)) return null;
return matrix;
}
/**
* Converts a camera-native 16-bit frame into the two buffers the editor needs,
* in place:
*
* - `rgb8` the sRGB proxy the JPEG is encoded from
* - `rgb16` the camera samples, white-point scaled, still camera native
*
* Returns the white point gain that was applied. The gain is baked into both
* buffers, so the client only has to apply the camera matrix; keeping it out of
* the sidecar would mean shipping a second 16-bit table for no benefit.
*/
export function cameraToProxy(rgb16, rgb8, matrix) {
const linear = linearTable();
const encode = encodedTable();
const count = Math.floor(rgb16.length / 3);
const m = matrix ?? [1, 0, 0, 0, 1, 0, 0, 0, 1];
// Pass 1: where does the fastest channel sit? Choosing the white point after
// the matrix keeps highlight clipping at about the same 1% LibRaw targets,
// instead of clipping whatever the matrix pushes past 1 on top of that.
const hist = new Uint32Array(65536);
for (let i = 0; i < count; i++) {
const s = i * 3;
const r = linear[rgb16[s]];
const g = linear[rgb16[s + 1]];
const b = linear[rgb16[s + 2]];
const max = Math.max(
m[0] * r + m[1] * g + m[2] * b,
m[3] * r + m[4] * g + m[5] * b,
m[6] * r + m[7] * g + m[8] * b,
);
const bin = max <= 0 ? 0 : max >= 1 ? 65535 : Math.round(max * 65535);
hist[bin]++;
}
let seen = 0;
let p99 = 65535;
const target = count * 0.99;
for (let i = 0; i < 65536; i++) {
seen += hist[i];
if (seen >= target) {
p99 = i;
break;
}
}
// Cap the gain: a nearly black frame is a real photograph, not a reason to
// multiply read noise by fifty.
const gain = Math.min(48, Math.max(0.25, 65535 / Math.max(1, p99)));
// Camera samples stay gamma encoded, so a shadow lift still has 16 bits of
// encoded detail to work from rather than 16 bits of linear light.
const camera = new Uint16Array(65536);
for (let i = 0; i < 65536; i++) {
camera[i] = Math.round(linearToSrgb(linear[i] * gain) * 65535);
}
// Pass 2: write both buffers.
for (let i = 0; i < count; i++) {
const s = i * 3;
const r = linear[rgb16[s]] * gain;
const g = linear[rgb16[s + 1]] * gain;
const b = linear[rgb16[s + 2]] * gain;
rgb8[s] = encode[clampIndex(m[0] * r + m[1] * g + m[2] * b)];
rgb8[s + 1] = encode[clampIndex(m[3] * r + m[4] * g + m[5] * b)];
rgb8[s + 2] = encode[clampIndex(m[6] * r + m[7] * g + m[8] * b)];
rgb16[s] = camera[rgb16[s]];
rgb16[s + 1] = camera[rgb16[s + 1]];
rgb16[s + 2] = camera[rgb16[s + 2]];
}
return gain;
}
function clampIndex(value) {
if (!(value > 0)) return 0;
if (value >= 1) return 65535;
return Math.round(value * 65535);
}