Speed up raw imports and add a per-import 16-bit toggle

Import raws four at a time to match the decoder pool, compress the 16-bit
sidecar with RLE on the libuv pool, and demosaic X-Trans files in a single
Markesteijn pass so Fuji imports no longer queue into decode timeouts. The
import dialog now offers 16-bit (default) or 8-bit per import, sent as
`?sidecar=0`, and the camera thumbnail and embedded-preview fallbacks no
longer drop to 8-bit samples or store a 0x0 photo.
This commit is contained in:
2026-12-02 03:25:53 +00:00
parent ea7b5b846c
commit fa0a7e64ef
11 changed files with 464 additions and 101 deletions
+40 -1
View File
@@ -119,7 +119,38 @@ export function resizeRgb16(rgb16, width, height, tw, th) {
* client would have to guess the conversion, and a self-describing sidecar also
* keeps working for imports that predate a catalogue migration.
*/
/**
* Deflate settings for the 16-bit sidecar. RLE wins here because the rows are
* Sub-filtered first: a photographic row turns into mostly small byte
* differences, and run-length coding stores them in a few bits each. On a
* 40 MP sidecar this took compression from 2.87 s to 1.10 s and the file from
* 58.47 MB to 58.06 MB; the decoded samples are byte-identical.
*/
const SIDECAR_DEFLATE = { level: 6, strategy: zlib.constants.Z_RLE };
export function encodePng16(rgb16, width, height, matrix = null) {
const raw = png16Rows(rgb16, width, height);
return png16File(zlib.deflateSync(raw, SIDECAR_DEFLATE), width, height, matrix);
}
/**
* The same PNG, with the deflate handed to libuv's thread pool.
*
* A 40 MP sidecar is ~110 MB and takes about five seconds to compress. Run
* synchronously that stalls the entire server for the duration, so four
* parallel imports finished one after another while the event loop was busy;
* the import felt frozen and preview requests queued behind it.
*/
export async function encodePng16Async(rgb16, width, height, matrix = null) {
const raw = png16Rows(rgb16, width, height);
const idat = await new Promise((resolve, reject) => {
zlib.deflate(raw, SIDECAR_DEFLATE, (err, out) => (err ? reject(err) : resolve(out)));
});
return png16File(idat, width, height, matrix);
}
/** Sub-filtered rows: each byte minus the byte six to its left. */
function png16Rows(rgb16, width, height) {
const stride = width * 6;
const raw = Buffer.alloc((stride + 1) * height);
const row = Buffer.alloc(stride);
@@ -134,7 +165,10 @@ export function encodePng16(rgb16, width, height, matrix = null) {
raw[base + 1 + i] = (row[i] - (i >= 6 ? row[i - 6] : 0)) & 0xff;
}
}
const idat = zlib.deflateSync(raw, { level: 6 });
return raw;
}
function png16File(idat, width, height, matrix = null) {
const ihdr = Buffer.alloc(13);
ihdr.writeUInt32BE(width, 0);
ihdr.writeUInt32BE(height, 4);
@@ -199,7 +233,12 @@ export async function cameraThumb16(png16, width, height, matrix = null) {
const sharp = await loadSharp();
if (!sharp || width < 1 || height < 1) return null;
try {
// `depth: 'ushort'` on the output only sets the container: without asking
// for the rgb16 colourspace, libvips runs the resize on 8-bit pixels and the
// samples come back as their high bytes alone (7 instead of 2000), which
// renders as a near-black tile.
const { data, info } = await sharp(png16)
.toColourspace('rgb16')
.resize({ width, height, fit: 'fill' })
.raw({ depth: 'ushort' })
.toBuffer({ resolveWithObject: true });