Files
luminary/server/denoise-worker.mjs
bhursey f7942d0bd0 Offer SCUNet and NLM alongside the guided denoise
The AI denoise panel now picks between three bases: the existing guided
filter, non-local means and a learned SCUNet model run through
onnxruntime-node on overlapping tiles. The base is part of the render key, so
each model keeps its own cached render and switching back is instant, and jobs
report stage and tile progress instead of a bare spinner.

onnxruntime-node is pinned to 1.23.0, the last release that ships both macOS
architectures, and every Docker and desktop install skips its CUDA download
because only the CPU binaries in the package are used. The SCUNet weights and
ONNX Runtime are added to the third-party notices, the deb copyright file and
the README's denoise, API and redistribution sections.
2026-12-01 04:03:53 +00:00

76 lines
2.7 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 { createBaseDenoiser } from './denoise/baseDenoiser.ts';
try {
const { sourcePath, outPath, base = 'guided' } = 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. The
// base denoiser (guided / nlm / scunet) is chosen by the request; the cache
// key already folds it in, so a different model is a different render.
//
// Progress is the only window the panel has into a 95 s SCUNet run, so both
// the stage boundaries and the tile counter are forwarded to the job.
const report = (progress) => parentPort?.postMessage({ type: 'progress', progress });
const out = await denoiseRgb(
{ rgb, width, height },
{ amount: 1 },
createBaseDenoiser(base, (tiles) =>
report({ stage: 'base', done: tiles.done, total: tiles.total }),
),
(stage) => report({ stage }),
);
// 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) });
}