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
-1
@@ -68,7 +68,99 @@ export function encodePng(rgb, width, height) {
|
||||
]);
|
||||
}
|
||||
|
||||
/** Encode an interleaved RGB byte buffer as JPEG (sharp) or PNG (fallback). */
|
||||
/**
|
||||
* Box-average a 16-bit RGB image down to the proxy size. sharp's raw pipeline
|
||||
* does not round-trip 16-bit samples faithfully, and this keeps the sidecar
|
||||
* pixel-aligned with the JPEG that was encoded by a different library.
|
||||
*/
|
||||
export function resizeRgb16(rgb16, width, height, tw, th) {
|
||||
if (width === tw && height === th) return rgb16;
|
||||
const out = new Uint16Array(tw * th * 3);
|
||||
const fx = width / tw;
|
||||
const fy = height / th;
|
||||
for (let y = 0; y < th; y++) {
|
||||
const y0 = Math.floor(y * fy);
|
||||
const y1 = Math.min(height, Math.max(y0 + 1, Math.ceil((y + 1) * fy)));
|
||||
for (let x = 0; x < tw; x++) {
|
||||
const x0 = Math.floor(x * fx);
|
||||
const x1 = Math.min(width, Math.max(x0 + 1, Math.ceil((x + 1) * fx)));
|
||||
let r = 0;
|
||||
let g = 0;
|
||||
let b = 0;
|
||||
let n = 0;
|
||||
for (let sy = y0; sy < y1; sy++) {
|
||||
let at = (sy * width + x0) * 3;
|
||||
for (let sx = x0; sx < x1; sx++) {
|
||||
r += rgb16[at];
|
||||
g += rgb16[at + 1];
|
||||
b += rgb16[at + 2];
|
||||
at += 3;
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
const d = (y * tw + x) * 3;
|
||||
out[d] = Math.round(r / n);
|
||||
out[d + 1] = Math.round(g / n);
|
||||
out[d + 2] = Math.round(b / n);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* 16-bit RGB PNG (colour type 2, no interlacing, Sub-filtered rows). The editor
|
||||
* inflates this in the browser and uploads it as a half-float texture, so it has
|
||||
* to be a standard PNG rather than a private container. Sub filtering subtracts
|
||||
* each byte from the pixel six bytes to its left, which compresses photographic
|
||||
* rows far better than storing the samples raw.
|
||||
*
|
||||
* `matrix` is the camera-to-sRGB matrix (9 numbers, row major). It is written to
|
||||
* a tEXt chunk because the sidecar holds camera-native samples: without it the
|
||||
* client would have to guess the conversion, and a self-describing sidecar also
|
||||
* keeps working for imports that predate a catalogue migration.
|
||||
*/
|
||||
export function encodePng16(rgb16, width, height, matrix = null) {
|
||||
const stride = width * 6;
|
||||
const raw = Buffer.alloc((stride + 1) * height);
|
||||
const row = Buffer.alloc(stride);
|
||||
for (let y = 0; y < height; y++) {
|
||||
let at = y * width * 3;
|
||||
for (let i = 0; i < stride; i += 2) {
|
||||
row.writeUInt16BE(rgb16[at++] & 0xffff, i);
|
||||
}
|
||||
const base = y * (stride + 1);
|
||||
raw[base] = 1;
|
||||
for (let i = 0; i < stride; i++) {
|
||||
raw[base + 1 + i] = (row[i] - (i >= 6 ? row[i - 6] : 0)) & 0xff;
|
||||
}
|
||||
}
|
||||
const idat = zlib.deflateSync(raw, { level: 6 });
|
||||
const ihdr = Buffer.alloc(13);
|
||||
ihdr.writeUInt32BE(width, 0);
|
||||
ihdr.writeUInt32BE(height, 4);
|
||||
ihdr[8] = 16;
|
||||
ihdr[9] = 2;
|
||||
ihdr[10] = 0;
|
||||
ihdr[11] = 0;
|
||||
ihdr[12] = 0;
|
||||
return Buffer.concat([
|
||||
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
||||
chunk('IHDR', ihdr),
|
||||
...(matrix && matrix.length === 9
|
||||
? [
|
||||
chunk(
|
||||
'tEXt',
|
||||
Buffer.from(
|
||||
`rgbCam\0${matrix.map((value) => Number(value).toFixed(8)).join(',')}`,
|
||||
'latin1',
|
||||
),
|
||||
),
|
||||
]
|
||||
: []),
|
||||
chunk('IDAT', idat),
|
||||
chunk('IEND', Buffer.alloc(0)),
|
||||
]);
|
||||
}
|
||||
export async function encodeRgb(rgb, width, height, quality = 92, maxEdge = 0) {
|
||||
const sharp = await loadSharp();
|
||||
if (!sharp) {
|
||||
|
||||
Reference in New Issue
Block a user