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:
+93
-19
@@ -1,5 +1,6 @@
|
||||
import { RAW_PREVIEW_EDGE } from './config.mjs';
|
||||
import { encodeRgb, jpegInfo, loadSharp, toJpegProxy } from './imaging.mjs';
|
||||
import { RAW_16BIT, RAW_PREVIEW_EDGE } from './config.mjs';
|
||||
import { cameraToProxy, readRgbCam } from './color.mjs';
|
||||
import { encodePng16, encodeRgb, jpegInfo, loadSharp, resizeRgb16, toJpegProxy } from './imaging.mjs';
|
||||
import { loadLibRaw, rawPool } from './libraw-pool.mjs';
|
||||
|
||||
/** Every raw container the pipeline will attempt, grouped by vendor family. */
|
||||
@@ -57,12 +58,25 @@ export async function rawEngine() {
|
||||
}
|
||||
|
||||
const DECODE_SETTINGS = {
|
||||
outputBps: 8,
|
||||
// Decode at 16 bits and let LibRaw blend reconstructed highlights instead of
|
||||
// clipping them; the 8-bit proxy is downsampled after the rolloff, so a
|
||||
// recovered sky keeps gradation instead of a flat white patch.
|
||||
outputBps: 16,
|
||||
useCameraWb: true,
|
||||
outputColor: 1, // sRGB
|
||||
// Camera-native, not sRGB: the camera-to-output matrix is applied here in JS
|
||||
// (see color.mjs) and again by the editor on the 16-bit sidecar. That makes
|
||||
// the conversion one swappable matrix instead of a fixed choice inside
|
||||
// LibRaw, which is what a camera profile needs.
|
||||
outputColor: 0,
|
||||
userQual: 3,
|
||||
highlight: 0,
|
||||
noAutoBright: false,
|
||||
highlight: 2,
|
||||
// One white point for the frame, chosen after the matrix. LibRaw's per-path
|
||||
// auto-bright measures each output colourspace separately, so the JPEG and
|
||||
// the camera-native sidecar would disagree by up to half a stop.
|
||||
noAutoBright: true,
|
||||
// Always use the camera's own colour matrix. The default only does so when
|
||||
// white balance is applied, which leaves some bodies on the generic matrix.
|
||||
useCameraMatrix: 3,
|
||||
// Use the orientation stored in the raw container: phone and DSLR files often
|
||||
// record the shot rotated (EXIF 6/8), and demosaicing without it produces a
|
||||
// sideways proxy that no later step can fix without losing the original axes.
|
||||
@@ -70,7 +84,13 @@ const DECODE_SETTINGS = {
|
||||
useFujiRotate: -1,
|
||||
};
|
||||
|
||||
/** Turns one LibRaw `imageData()` result into an interleaved RGB buffer. */
|
||||
/**
|
||||
* Turns one LibRaw `imageData()` result into an interleaved RGB buffer.
|
||||
*
|
||||
* The 8-bit copy is what the JPEG proxy is encoded from. The 16-bit copy keeps
|
||||
* every sample LibRaw produced so the editor can upload a half-float source
|
||||
* texture; without it, a heavy shadow lift is applied to 256 levels per channel.
|
||||
*/
|
||||
function toRgb(img) {
|
||||
if (!img || !img.width || !img.height || !img.data) throw new Error('LibRaw returned no pixels');
|
||||
const width = Number(img.width);
|
||||
@@ -79,19 +99,36 @@ function toRgb(img) {
|
||||
const bits = Number(img.bits ?? 8);
|
||||
const src = img.data;
|
||||
const pixelCount = width * height;
|
||||
const needed = pixelCount * colors * (bits > 8 ? 2 : 1);
|
||||
if (src.length < needed) {
|
||||
throw new Error(`LibRaw buffer too small (${src.length} bytes for ${width}×${height}×${colors})`);
|
||||
const needed = pixelCount * colors;
|
||||
// libraw-wasm hands back samples, not bytes: with outputBps 16 the buffer is a
|
||||
// Uint16Array already. Older builds returned the raw 16-bit bytes, so accept
|
||||
// both shapes instead of assuming one and rejecting every 16-bit decode.
|
||||
const wide =
|
||||
bits > 8 && !(src instanceof Uint16Array)
|
||||
? new Uint16Array(src.buffer, src.byteOffset, Math.floor(src.byteLength / 2))
|
||||
: src;
|
||||
if (wide.length < needed) {
|
||||
throw new Error(
|
||||
`LibRaw buffer too small (${wide.length} samples for ${width}×${height}×${colors})`,
|
||||
);
|
||||
}
|
||||
|
||||
const rgb = Buffer.alloc(pixelCount * 3);
|
||||
let rgb16 = null;
|
||||
if (bits > 8) {
|
||||
const view = new Uint16Array(src.buffer, src.byteOffset, Math.floor(src.byteLength / 2));
|
||||
const view = wide instanceof Uint16Array ? wide : src;
|
||||
rgb16 = new Uint16Array(pixelCount * 3);
|
||||
for (let i = 0; i < pixelCount; i++) {
|
||||
const s = i * colors;
|
||||
rgb[i * 3] = view[s] >> 8;
|
||||
rgb[i * 3 + 1] = view[s + 1] >> 8;
|
||||
rgb[i * 3 + 2] = view[s + 2] >> 8;
|
||||
const r = view[s];
|
||||
const g = view[s + 1];
|
||||
const b = view[s + 2];
|
||||
rgb[i * 3] = r >> 8;
|
||||
rgb[i * 3 + 1] = g >> 8;
|
||||
rgb[i * 3 + 2] = b >> 8;
|
||||
rgb16[i * 3] = r;
|
||||
rgb16[i * 3 + 1] = g;
|
||||
rgb16[i * 3 + 2] = b;
|
||||
}
|
||||
} else {
|
||||
for (let i = 0; i < pixelCount; i++) {
|
||||
@@ -101,20 +138,29 @@ function toRgb(img) {
|
||||
rgb[i * 3 + 2] = src[s + 2];
|
||||
}
|
||||
}
|
||||
return { rgb, width, height };
|
||||
return { rgb, rgb16, width, height };
|
||||
}
|
||||
|
||||
/**
|
||||
* Full demosaic through a pooled LibRaw worker. `open()` only unpacks the file;
|
||||
* the pixels come from `imageData()`, which rejects when the camera is unsupported.
|
||||
*
|
||||
* `cameraNative` asks for the frame before LibRaw's output matrix, plus the
|
||||
* matrix metadata. A body LibRaw has no matrix for falls back to the plain sRGB
|
||||
* decode, which is the same result the pipeline produced before.
|
||||
*/
|
||||
async function demosaic(buffer) {
|
||||
async function demosaic(buffer, cameraNative = true) {
|
||||
if ((await rawEngine()) !== 'ready') throw new Error('LibRaw is not installed');
|
||||
return rawPool.use(async (lib) => {
|
||||
// LibRaw transfers the buffer to its worker, which detaches it here. Hand it
|
||||
// a copy so the caller's bytes survive for the embedded-preview fallback.
|
||||
await lib.open(Buffer.from(buffer), DECODE_SETTINGS);
|
||||
return toRgb(await lib.imageData());
|
||||
await lib.open(
|
||||
Buffer.from(buffer),
|
||||
cameraNative ? DECODE_SETTINGS : { ...DECODE_SETTINGS, outputColor: 1, noAutoBright: false },
|
||||
);
|
||||
const image = toRgb(await lib.imageData());
|
||||
const rgbCam = cameraNative ? readRgbCam(await lib.metadata(true)) : null;
|
||||
return { ...image, rgbCam };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -163,10 +209,38 @@ export async function decodeRaw(buffer) {
|
||||
let librawError = null;
|
||||
if (looksLikeRaw(buffer)) {
|
||||
try {
|
||||
const { rgb, width, height } = await demosaic(buffer);
|
||||
let frame = await demosaic(buffer, true);
|
||||
// No usable matrix (an odd body or older engine): decode the familiar sRGB
|
||||
// way instead of shipping camera samples nobody can convert.
|
||||
if (!frame.rgbCam || !frame.rgb16) frame = await demosaic(buffer, false);
|
||||
const { width, height, rgbCam } = frame;
|
||||
// Both buffers are rewritten in place: `rgb` becomes the sRGB proxy and
|
||||
// `rgb16` the camera-native sidecar, sharing one white point.
|
||||
if (rgbCam && frame.rgb16) cameraToProxy(frame.rgb16, frame.rgb, rgbCam);
|
||||
const { rgb } = frame;
|
||||
const encoded = await encodeRgb(rgb, width, height, 92, RAW_PREVIEW_EDGE);
|
||||
// The sidecar must match the JPEG pixel for pixel: the editor swaps the
|
||||
// two textures at the same uv, so a size disagreement would misalign the
|
||||
// whole grade. It is optional -- encoding failures leave the JPEG path.
|
||||
let preview16 = null;
|
||||
if (frame.rgb16 && RAW_16BIT) {
|
||||
try {
|
||||
const scaled = resizeRgb16(frame.rgb16, width, height, encoded.width, encoded.height);
|
||||
preview16 = {
|
||||
// The matrix travels with the samples so the editor knows how to
|
||||
// turn camera RGB into sRGB without a second metadata fetch.
|
||||
data: encodePng16(scaled, encoded.width, encoded.height, rgbCam),
|
||||
width: encoded.width,
|
||||
height: encoded.height,
|
||||
rgbCam,
|
||||
};
|
||||
} catch {
|
||||
preview16 = null;
|
||||
}
|
||||
}
|
||||
return {
|
||||
jpeg: encoded.data,
|
||||
preview16,
|
||||
width: encoded.width,
|
||||
height: encoded.height,
|
||||
engine: 'libraw',
|
||||
|
||||
Reference in New Issue
Block a user