Replace the Lensfun WebAssembly runtime with our own lens engine.
The wrapper's wasm, its 5 MB data blob and the runtime chunk were fragile to serve (CSP, MIME, cache) and pulled an LGPL runtime into the bundle just for calibration data. The Lensfun XML is now converted at build time into public/lens-db/profiles.json, and search, matching and the correction maths live in src/lib/lensDb.ts and src/lib/lensEngine.ts. Maps are pinned against output captured from the previous implementation, so results are unchanged. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { autoScaleFor, computeMaps, type LensCorrection, type LensMap } from './lensEngine';
|
||||
import { fixtureLenses, referenceCases } from './lensEngine.fixture';
|
||||
|
||||
const GRID = { width: 64, height: 43 };
|
||||
|
||||
function mapsFor(model: string, focal: number, aperture: number) {
|
||||
const entry = fixtureLenses.find((lens) => lens.model === model);
|
||||
expect(entry, model).toBeTruthy();
|
||||
return computeMaps(entry!, {
|
||||
...GRID,
|
||||
focal,
|
||||
crop: entry!.crop,
|
||||
aperture,
|
||||
distance: 1000,
|
||||
distortion: true,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The reference values were captured from the Lensfun WebAssembly build this
|
||||
* engine replaced, at 25 points of a 64x43 grid. Matching them proves the ported
|
||||
* models (PanoTools and poly3 distortion, poly3 TCA, pa vignetting) agree with
|
||||
* what the editor used to render.
|
||||
*/
|
||||
describe('lens engine matches the reference implementation', () => {
|
||||
for (const testCase of referenceCases) {
|
||||
it(`corrects ${testCase.model} the same way`, () => {
|
||||
const maps = mapsFor(testCase.model, testCase.focal, testCase.aperture);
|
||||
for (const point of testCase.points) {
|
||||
const index = point.y * GRID.width + point.x;
|
||||
const where = `${testCase.key} at ${point.x},${point.y}`;
|
||||
expect(maps.geometry[index * 2], `${where} x`).toBeCloseTo(point.g[0], 3);
|
||||
expect(maps.geometry[index * 2 + 1], `${where} y`).toBeCloseTo(point.g[1], 3);
|
||||
if (point.t && maps.tca) {
|
||||
expect(maps.tca[index * 6], `${where} red x`).toBeCloseTo(point.t[0], 3);
|
||||
expect(maps.tca[index * 6 + 1], `${where} red y`).toBeCloseTo(point.t[1], 3);
|
||||
expect(maps.tca[index * 6 + 4], `${where} blue x`).toBeCloseTo(point.t[2], 3);
|
||||
expect(maps.tca[index * 6 + 5], `${where} blue y`).toBeCloseTo(point.t[3], 3);
|
||||
}
|
||||
if (point.v != null && maps.vignetting) {
|
||||
expect(maps.vignetting[index * 3], `${where} gain`).toBeCloseTo(point.v, 3);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('lens engine basics', () => {
|
||||
it('leaves coordinates untouched when the calibration has no distortion', () => {
|
||||
const entry = { ...fixtureLenses[0], d: [] };
|
||||
const maps = computeMaps(entry, { ...GRID, focal: 50, crop: entry.crop, distortion: true });
|
||||
for (const [x, y] of [
|
||||
[0, 0],
|
||||
[31, 21],
|
||||
[63, 42],
|
||||
]) {
|
||||
const index = y * GRID.width + x;
|
||||
expect(maps.geometry[index * 2]).toBeCloseTo(x, 3);
|
||||
expect(maps.geometry[index * 2 + 1]).toBeCloseTo(y, 3);
|
||||
}
|
||||
});
|
||||
|
||||
it('only emits the maps the calibration and the toggles ask for', () => {
|
||||
const entry = fixtureLenses[0];
|
||||
const plain = computeMaps(entry, { ...GRID, focal: 50, crop: entry.crop, distortion: false });
|
||||
expect(plain.tca).toBeUndefined();
|
||||
|
||||
const cooled = computeMaps(entry, { ...GRID, focal: 28, crop: entry.crop, aperture: 2.8, distortion: true });
|
||||
expect(cooled.tca?.length).toBe(GRID.width * GRID.height * 6);
|
||||
expect(cooled.vignetting?.length).toBe(GRID.width * GRID.height * 3);
|
||||
// Flat-field gains brighten the corners of a vignetted lens.
|
||||
expect(cooled.vignetting![0]).toBeGreaterThan(cooled.vignetting![(21 * GRID.width + 31) * 3]);
|
||||
});
|
||||
|
||||
it('scales up when the correction samples outside the frame', () => {
|
||||
const geometry: LensMap = {
|
||||
width: 3,
|
||||
height: 3,
|
||||
// Every sample points slightly outside the image.
|
||||
data: new Float32Array(3 * 3 * 4),
|
||||
};
|
||||
for (let i = 0; i < 3 * 3; i++) {
|
||||
geometry.data[i * 4] = -0.1;
|
||||
geometry.data[i * 4 + 1] = -0.1;
|
||||
geometry.data[i * 4 + 3] = 1;
|
||||
}
|
||||
const correction: LensCorrection = { key: 'test', geometry, scale: 1 };
|
||||
expect(autoScaleFor(correction.geometry)).toBeGreaterThan(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user