Files
luminary/server/denoise-worker.mjs
T
bhursey cd52a2f82f Initial commit: Luminary photo catalog and RAW editor
Browser-based photo catalog with an optional team server (SQLite + LibRaw).
Local database, uploaded photos, build output and dependencies are gitignored.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-11-23 15:19:25 +00:00

63 lines
2.2 KiB
JavaScript

/**
* Denoise one proxy. Runs in a worker thread: this is seconds of solid CPU and
* the request thread is also serving the rest of the library.
*
* The proxy is decoded rather than the raw file, deliberately -- these are the
* exact pixels the editor edits, so the result lines up with the canvas and with
* the client's export path.
*/
import { parentPort, workerData } from 'node:worker_threads';
import fs from 'node:fs/promises';
import sharp from 'sharp';
import { denoiseRgb } from './denoise/rgbPipeline.ts';
import { guidedDenoiser } from './denoise/standin.ts';
try {
const { sourcePath, outPath } = workerData;
const buffer = await fs.readFile(sourcePath);
const { data, info } = await sharp(buffer)
.rotate()
.toColourspace('srgb')
.removeAlpha()
.raw()
.toBuffer({ resolveWithObject: true });
const { width, height, channels } = info;
const rgb = new Uint8Array(width * height * 3);
if (channels === 3) {
rgb.set(data);
} else {
for (let i = 0; i < width * height; i++) {
const s = i * channels;
rgb[i * 3] = data[s];
rgb[i * 3 + 1] = data[Math.min(s + 1, s + channels - 1)];
rgb[i * 3 + 2] = data[Math.min(s + 2, s + channels - 1)];
}
}
const started = performance.now();
// Always full strength: the Amount slider blends on the client, so one render
// serves every setting and the cache key does not move with the slider.
const out = await denoiseRgb({ rgb, width, height }, { amount: 1 }, guidedDenoiser());
// 4:4:4 so the chroma work is not immediately thrown away by the encoder.
const jpeg = await sharp(Buffer.from(out.rgb), { raw: { width, height, channels: 3 } })
.jpeg({ quality: 95, chromaSubsampling: '4:4:4' })
.toBuffer();
await fs.writeFile(outPath, jpeg);
parentPort?.postMessage({
type: 'done',
ms: Math.round(performance.now() - started),
bytes: jpeg.length,
stats: {
amount: out.stats.amount,
autoGate: out.stats.autoGate,
gate: out.stats.gateCoverage,
noise: out.stats.noise.global,
baseWidth: out.stats.baseWidth,
},
});
} catch (error) {
parentPort?.postMessage({ type: 'error', error: error?.message ?? String(error) });
}