Files
luminary/server/denoise/standin.ts
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

167 lines
5.6 KiB
TypeScript

/**
* Stand-in denoisers for the reduced-resolution base.
*
* These occupy exactly the slot the ONNX model occupies: take an interleaved
* RGB plane at inference scale plus the noise level there, return a denoised
* plane of the same shape. Having credible classical implementations in that
* slot is what lets the pipeline, the harness and the baselines be finished and
* measured before a model exists -- and they are the numbers the model has to
* beat.
*
* The seam is RGB, not luma, deliberately: a denoiser trained on colour images
* fed a replicated greyscale plane is out of distribution and underperforms a
* plain guided filter. Chroma also comes from this plane, so the model gets a
* say in colour noise, which is the part users actually complain about.
*
* `nlm` is the honest reference. `guided` is fast enough to iterate against.
*/
import { guidedFilter } from './detail.ts';
import { boxBlur } from './planes.ts';
export interface BaseImage {
/** Interleaved RGB, `width * height * 3`, 0..255. */
rgb: Float32Array;
width: number;
height: number;
}
/**
* Synchronous for the classical stand-ins, a promise for a model session.
* ONNX inference cannot be made synchronous, so the seam allows both and the
* pipeline awaits the result.
*/
export type BaseDenoiser = (image: BaseImage, sigma: number) => BaseImage | Promise<BaseImage>;
export function deinterleave(image: BaseImage): [Float32Array, Float32Array, Float32Array] {
const n = image.width * image.height;
const r = new Float32Array(n);
const g = new Float32Array(n);
const b = new Float32Array(n);
for (let i = 0, p = 0; i < n; i++, p += 3) {
r[i] = image.rgb[p];
g[i] = image.rgb[p + 1];
b[i] = image.rgb[p + 2];
}
return [r, g, b];
}
export function interleave(
r: Float32Array,
g: Float32Array,
b: Float32Array,
width: number,
height: number,
): BaseImage {
const rgb = new Float32Array(width * height * 3);
for (let i = 0, p = 0; i < r.length; i++, p += 3) {
rgb[p] = r[i];
rgb[p + 1] = g[i];
rgb[p + 2] = b[i];
}
return { rgb, width, height };
}
/**
* Robust sigma of one plane, from its own 3x3 high-pass.
*
* The caller's `sigma` is a luma figure, and per-channel noise differs a lot on
* a CFA sensor -- blue is the noisiest -- so filtering all three at the luma
* level over-smooths one channel and under-smooths another. Measuring each
* plane costs three cheap passes at base scale and removes the guesswork.
*/
function planeSigma(plane: Float32Array, width: number, height: number, fallback: number): number {
const smooth = boxBlur(plane, width, height, 1);
const residual = new Float32Array(plane.length);
for (let i = 0; i < plane.length; i++) residual[i] = Math.abs(plane[i] - smooth[i]);
const sorted = residual.sort();
const median = sorted[sorted.length >> 1] ?? 0;
const sigma = (median * 1.4826) / 0.943;
return sigma > 0.5 ? sigma : Math.max(1, fallback);
}
/** Guided filter per channel: cheap, edge-preserving, mildly waxy. */
export function guidedDenoiser(radius = 3, strength = 2.6): BaseDenoiser {
return (image, sigma) => {
const [r, g, b] = deinterleave(image);
const filter = (plane: Float32Array) => {
const s = planeSigma(plane, image.width, image.height, sigma);
return guidedFilter(plane, image.width, image.height, radius, (strength * s) ** 2);
};
return interleave(filter(r), filter(g), filter(b), image.width, image.height);
};
}
/**
* Non-local means, per channel. Slow, and deliberately so: it is the quality
* reference the neural model must beat, not a shipping path.
*/
export function nlmDenoiser(search = 2, patch = 1): BaseDenoiser {
const patchW = 2 * patch + 1;
const patchArea = patchW * patchW;
return (image) => {
const [r, g, b] = deinterleave(image);
const planes = [r, g, b].map(
(plane) => nlmPlane(plane, image.width, image.height, search, patch, patchW, patchArea),
);
return interleave(planes[0], planes[1], planes[2], image.width, image.height);
};
}
function nlmPlane(
plane: Float32Array,
width: number,
height: number,
search: number,
patch: number,
patchW: number,
patchArea: number,
): Float32Array {
const out = new Float32Array(plane.length);
const s = Math.max(1, search | 0);
const clampX = (v: number) => (v < 0 ? 0 : v >= width ? width - 1 : v);
const clampY = (v: number) => (v < 0 ? 0 : v >= height ? height - 1 : v);
let sumSq = 0;
let count = 0;
for (let i = 0; i < plane.length; i += 7) {
sumSq += plane[i] * plane[i];
count++;
}
const h = Math.max(2, Math.sqrt(sumSq / Math.max(1, count)) * 0.18);
const cutoff = 4 * h * h;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const centre = plane[y * width + x];
let acc = 0;
let weight = 0;
for (let dy = -s; dy <= s; dy++) {
for (let dx = -s; dx <= s; dx++) {
if (dx === 0 && dy === 0) continue;
const ny = y + dy;
const nx = x + dx;
if (ny < 0 || ny >= height || nx < 0 || nx >= width) continue;
let dist = 0;
for (let py = -patch; py <= patch; py++) {
const ay = clampY(y + py) * width;
const by = clampY(ny + py) * width;
for (let px = -patch; px <= patch; px++) {
const d = plane[ay + clampX(x + px)] - plane[by + clampX(nx + px)];
dist += d * d;
}
}
dist /= patchArea;
if (dist > cutoff) continue;
const w = Math.exp(-dist / (h * h));
acc += w * plane[ny * width + nx];
weight += w;
}
}
out[y * width + x] = (acc + centre) / (weight + 1);
}
}
return out;
}