Show camera profiles in every view and remember proof and DCP settings.

A profile chosen for a raw only changed Develop and exports, so the grid,
Compare and preset previews could disagree with the file. They now grade
camera-native samples, while the proof profile persists with a rendering
intent and DCP matrices can be reused per camera body.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
2026-11-28 04:04:44 +00:00
parent 0c113e7a91
commit 637629385a
16 changed files with 720 additions and 39 deletions
+57
View File
@@ -161,6 +161,63 @@ export function encodePng16(rgb16, width, height, matrix = null) {
chunk('IEND', Buffer.alloc(0)),
]);
}
/** The camera matrix carried in a 16-bit sidecar's `rgbCam` tEXt chunk. */
export function png16Matrix(png) {
const needle = Buffer.from('rgbCam\0', 'latin1');
const at = png.indexOf(needle);
if (at < 0) return null;
const start = at + needle.length;
// The text runs to the end of the tEXt chunk, and the chunk's CRC is binary
// and may contain a NUL. Stop at the first byte that cannot be part of the
// comma-separated number list instead of searching for a terminator.
let end = start;
while (end < png.length) {
const code = png[end];
const numeric =
(code >= 0x30 && code <= 0x39) || // 0-9
code === 0x2e || // .
code === 0x2c || // ,
code === 0x2d || // -
code === 0x2b || // +
code === 0x65 || // e
code === 0x45; // E
if (!numeric) break;
end++;
}
const values = png.toString('latin1', start, end).split(',').map(Number);
return values.length === 9 && values.every(Number.isFinite) ? values : null;
}
/**
* A camera-native grid thumbnail, resized from the 16-bit sidecar with sharp's
* 16-bit path. The grid normally renders the sRGB JPEG, whose body matrix is
* already baked in; this gives the client camera samples small enough to fetch
* per tile, so a per-photo DCP profile shows everywhere the photo does. The
* `rgbCam` tag is rewritten so the client still knows how to convert them.
*/
export async function cameraThumb16(png16, width, height, matrix = null) {
const sharp = await loadSharp();
if (!sharp || width < 1 || height < 1) return null;
try {
const { data, info } = await sharp(png16)
.resize({ width, height, fit: 'fill' })
.raw({ depth: 'ushort' })
.toBuffer({ resolveWithObject: true });
if (info.channels !== 3 || info.depth !== 'ushort') return null;
// Copy through a fresh buffer: a raw output offset is not guaranteed to be
// 16-bit aligned, and Uint16Array refuses an odd byteOffset.
const samples = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
return encodePng16(
new Uint16Array(samples),
info.width,
info.height,
matrix ?? png16Matrix(png16),
);
} catch {
return null;
}
}
export async function encodeRgb(rgb, width, height, quality = 92, maxEdge = 0) {
const sharp = await loadSharp();
if (!sharp) {
+44 -5
View File
@@ -60,10 +60,12 @@ import {
findProof,
findProofThumb,
findThumb,
findThumb16,
isCustomRoot,
originalTarget,
removeAsset,
removePreview16,
removeThumb16,
setStorageRoot,
storageBytes,
storageRoot,
@@ -72,6 +74,7 @@ import {
writeProof,
writeProofThumb,
writeThumb,
writeThumb16,
} from './storage.mjs';
import { decodeRaw, isRawName, rawSupport } from './raw.mjs';
import {
@@ -84,7 +87,7 @@ import {
purgeExpiredBridges,
verifyBridgeToken,
} from './bridge.mjs';
import { jpegInfo, makeThumb, toJpegProxy } from './imaging.mjs';
import { cameraThumb16, jpegInfo, makeThumb, toJpegProxy } from './imaging.mjs';
import { createZip } from './zip.mjs';
import {
cancelPrune,
@@ -1940,8 +1943,15 @@ route('POST', '/api/photos/:id/original', async ({ req, res, params }) => {
const data = await fsp.readFile(target);
const decoded = await decodeRaw(data);
await writePreview(params.id, decoded.jpeg, decoded.ext);
if (decoded.preview16) await writePreview16(params.id, decoded.preview16.data);
else await removePreview16(params.id);
if (decoded.preview16) {
await writePreview16(params.id, decoded.preview16.data);
// The thumbnail may not exist yet; drop any camera-native tile so the
// first grid view rebuilds it from the new pixels.
await removeThumb16(params.id);
} else {
await removePreview16(params.id);
await removeThumb16(params.id);
}
const info = await jpegInfo(decoded.jpeg);
return json(res, 201, {
raw: true,
@@ -1980,8 +1990,13 @@ route('POST', '/api/photos/:id/reprocess', async ({ req, res, params }) => {
try {
const decoded = await decodeRaw(await fsp.readFile(original));
await writePreview(params.id, decoded.jpeg, decoded.ext);
if (decoded.preview16) await writePreview16(params.id, decoded.preview16.data);
else await removePreview16(params.id);
if (decoded.preview16) {
await writePreview16(params.id, decoded.preview16.data);
await removeThumb16(params.id);
} else {
await removePreview16(params.id);
await removeThumb16(params.id);
}
const thumb = await makeThumb(decoded.jpeg, 560);
if (thumb) await writeThumb(params.id, thumb);
// The old denoised renders were made from the previous proxy.
@@ -2023,6 +2038,30 @@ route('GET', '/api/photos/:id/thumb', async ({ req, res, params }) => {
return sendBuffer(res, data, 'image/jpeg');
});
/**
* Camera-native version of the grid thumbnail, built on first request from the
* 16-bit sidecar and the JPEG thumbnail so the two have identical dimensions.
* Without this the grid can only show the camera profile's effect in Develop.
*/
route('GET', '/api/photos/:id/thumb16', async ({ req, res, params }) => {
const user = currentUser(req);
if (!user) return error(res, 401, 'Not signed in');
if (!ownedBy(photoOwner(params.id), user)) return error(res, 404, 'Not found');
let file = findThumb16(params.id);
if (!file) {
const sidecar = findPreview16(params.id);
const thumb = findThumb(params.id);
if (sidecar && thumb) {
const info = await jpegInfo(await fsp.readFile(thumb));
if (info?.width && info?.height) {
const png = await cameraThumb16(await fsp.readFile(sidecar), info.width, info.height);
if (png) file = await writeThumb16(params.id, png);
}
}
}
return sendFile(req, res, file, 'image/png');
});
/**
* Store a rendered proof for an edited photo. The editable proxy and original
* stay untouched; galleries read this copy so clients see the developed image.
+25
View File
@@ -141,6 +141,30 @@ export async function removePreview16(id) {
if (found) await fsp.rm(found, { force: true });
}
/**
* Camera-native grid thumbnail: the sidecar's samples at the JPEG thumbnail's
* size, so the grid can honour a per-photo camera profile without downloading
* the full sidecar. Lives beside the sidecar and is regenerated on demand.
*/
export async function writeThumb16(id, data) {
ensureDirs();
const target = path.join(storageRoot(), 'previews', `${safeId(id)}.t16.png`);
await fsp.writeFile(target, data);
return target;
}
export function findThumb16(id) {
const safe = lookupId(id);
if (!safe) return null;
const p = path.join(dirs().previews, `${safe}.t16.png`);
return fs.existsSync(p) ? p : null;
}
export async function removeThumb16(id) {
const found = findThumb16(id);
if (found) await fsp.rm(found, { force: true });
}
/**
* Denoised proxies, keyed by the hash of the parameters that produced them, so
* switching Amount back to a previous value reuses the file instead of spending
@@ -234,6 +258,7 @@ export async function removeAsset(id) {
findOriginal(id),
findPreview(id),
findPreview16(id),
findThumb16(id),
findThumb(id),
findProof(id),
findProofThumb(id),