Run the camera-to-sRGB pass on a worker thread

At four parallel imports the synchronous colour conversion was the widest
serial section left in the pipeline. The frame buffers now live on
SharedArrayBuffer, so a worker writes the same memory the encoder reads
and the main thread is free for the decoder pool and zlib.
This commit is contained in:
2026-12-02 04:02:33 +00:00
parent 7f0838156b
commit d24cbf26b8
3 changed files with 79 additions and 4 deletions
+8 -4
View File
@@ -1,5 +1,6 @@
import { RAW_16BIT, RAW_PREVIEW_EDGE, RAW_TIMEOUT_MS, RAW_XTRANS_QUALITY } from './config.mjs';
import { cameraToProxy, readRgbCam } from './color.mjs';
import { readRgbCam } from './color.mjs';
import { cameraToProxyAsync } from './color-offload.mjs';
import {
encodePng16Async,
encodeRgb,
@@ -144,11 +145,14 @@ function toRgb(img) {
);
}
const rgb = Buffer.alloc(pixelCount * 3);
// Both outputs are shared memory: the camera-to-sRGB pass runs in a worker
// (color-offload.mjs) and writes these same bytes, so a 24 MP frame is not
// copied across the thread boundary.
const rgb = Buffer.from(new SharedArrayBuffer(pixelCount * 3));
let rgb16 = null;
if (bits > 8) {
const view = wide instanceof Uint16Array ? wide : src;
rgb16 = new Uint16Array(pixelCount * 3);
rgb16 = new Uint16Array(new SharedArrayBuffer(pixelCount * 3 * 2));
for (let i = 0; i < pixelCount; i++) {
const s = i * colors;
const r = view[s];
@@ -313,7 +317,7 @@ export async function decodeRaw(buffer, { sidecar = RAW_16BIT } = {}) {
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);
if (rgbCam && frame.rgb16) await cameraToProxyAsync(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