749047ae9e
EXIF folds the brand into the focal range ("EF75-300mm"), so split letters
from digits before scoring, keep the score a pure name score that a caller can
trust, and rank a same-mount-family lens higher without filtering. Auto-match
now declines below 0.75 rather than applying the best of a bad list.
Lenses the bundled database lacks can be measured in-house: /api/lens-profiles
stores calibrations in SQLite, the Optics panel edits them and marks them
"Team", and search treats them as ordinary entries.
🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
139 lines
5.7 KiB
JavaScript
139 lines
5.7 KiB
JavaScript
/**
|
|
* Validation for team-measured lens calibrations.
|
|
*
|
|
* A profile carries the same sample rows the bundled Lensfun database uses, so
|
|
* the correction engine can read both without a second code path:
|
|
*
|
|
* d: [focal, model, a, b, c, realFocal] distortion
|
|
* t: [focal, model, br, cr, vr, bb, cb, vb] lateral chromatic aberration
|
|
* v: [focal, aperture, distance, k1, k2, k3] vignetting
|
|
*
|
|
* Coefficients are entered as measured: the engine rescales them by focal and
|
|
* sensor size exactly as it does for the bundled data.
|
|
*/
|
|
|
|
const MAX_DISTORTION = 60;
|
|
const MAX_TCA = 60;
|
|
const MAX_VIGNETTING = 120;
|
|
const MAX_COEFFICIENT = 100;
|
|
|
|
const DISTORTION_MODELS = [0, 1, 2]; // poly3, poly5, ptlens
|
|
const TCA_MODELS = [0, 1]; // poly3, linear
|
|
|
|
const finite = (value) => typeof value === 'number' && Number.isFinite(value);
|
|
const clampText = (value, max) =>
|
|
typeof value === 'string' ? value.trim().replace(/\s+/g, ' ').slice(0, max) : '';
|
|
|
|
function row(values, count) {
|
|
if (!Array.isArray(values) || values.length !== count) return null;
|
|
const numbers = values.map((value) => (typeof value === 'number' ? value : Number(value)));
|
|
return numbers.every(finite) ? numbers : null;
|
|
}
|
|
|
|
function distortionRows(input) {
|
|
if (input === undefined) return { rows: [] };
|
|
if (!Array.isArray(input)) return { error: 'Distortion samples must be a list' };
|
|
if (input.length > MAX_DISTORTION) return { error: `At most ${MAX_DISTORTION} distortion samples` };
|
|
const rows = [];
|
|
for (const entry of input) {
|
|
const values = row(entry, 6);
|
|
if (!values) return { error: 'Each distortion sample needs 6 numbers' };
|
|
const [focal, model, a, b, c, realFocal] = values;
|
|
if (!(focal > 0)) return { error: 'Distortion samples need a focal length above zero' };
|
|
if (!DISTORTION_MODELS.includes(model)) return { error: 'Unknown distortion model' };
|
|
if ([a, b, c].some((value) => Math.abs(value) > MAX_COEFFICIENT)) {
|
|
return { error: 'Distortion coefficients look out of range' };
|
|
}
|
|
if (realFocal < 0 || realFocal > 2000) return { error: 'Real focal length looks out of range' };
|
|
rows.push([focal, model, a, b, c, realFocal]);
|
|
}
|
|
return { rows };
|
|
}
|
|
|
|
function tcaRows(input) {
|
|
if (input === undefined) return { rows: [] };
|
|
if (!Array.isArray(input)) return { error: 'Chromatic aberration samples must be a list' };
|
|
if (input.length > MAX_TCA) return { error: `At most ${MAX_TCA} chromatic aberration samples` };
|
|
const rows = [];
|
|
for (const entry of input) {
|
|
const values = row(entry, 8);
|
|
if (!values) return { error: 'Each chromatic aberration sample needs 8 numbers' };
|
|
const [focal, model] = values;
|
|
if (!(focal > 0)) return { error: 'Chromatic aberration samples need a focal length above zero' };
|
|
if (!TCA_MODELS.includes(model)) return { error: 'Unknown chromatic aberration model' };
|
|
if (values.some((value) => Math.abs(value) > MAX_COEFFICIENT)) {
|
|
return { error: 'Chromatic aberration coefficients look out of range' };
|
|
}
|
|
rows.push(values);
|
|
}
|
|
return { rows };
|
|
}
|
|
|
|
function vignettingRows(input) {
|
|
if (input === undefined) return { rows: [] };
|
|
if (!Array.isArray(input)) return { error: 'Vignetting samples must be a list' };
|
|
if (input.length > MAX_VIGNETTING) return { error: `At most ${MAX_VIGNETTING} vignetting samples` };
|
|
const rows = [];
|
|
for (const entry of input) {
|
|
const values = row(entry, 6);
|
|
if (!values) return { error: 'Each vignetting sample needs 6 numbers' };
|
|
const [focal, aperture, distance, k1, k2, k3] = values;
|
|
if (!(focal > 0)) return { error: 'Vignetting samples need a focal length above zero' };
|
|
if (!(aperture > 0)) return { error: 'Vignetting samples need an aperture above zero' };
|
|
if (!(distance > 0)) return { error: 'Vignetting samples need a focus distance above zero' };
|
|
if ([k1, k2, k3].some((value) => Math.abs(value) > MAX_COEFFICIENT)) {
|
|
return { error: 'Vignetting coefficients look out of range' };
|
|
}
|
|
rows.push(values);
|
|
}
|
|
return { rows };
|
|
}
|
|
|
|
/** Returns `{ value }` for a valid body, or `{ error }` describing the first problem. */
|
|
export function parseCalibration(body) {
|
|
if (!body || typeof body !== 'object') return { error: 'Expected a lens profile' };
|
|
const maker = clampText(body.maker, 80);
|
|
const model = clampText(body.model, 120);
|
|
if (!maker) return { error: 'Lens maker is required' };
|
|
if (!model) return { error: 'Lens model is required' };
|
|
|
|
const crop = Number(body.crop);
|
|
if (!(crop >= 0.5 && crop <= 3)) return { error: 'Crop factor must be between 0.5 and 3' };
|
|
|
|
const aspect = body.aspect === undefined || body.aspect === '' ? 0 : Number(body.aspect);
|
|
if (!(aspect >= 0 && aspect <= 4)) return { error: 'Aspect ratio must be between 0 and 4' };
|
|
|
|
const centerX = body.centerX === undefined || body.centerX === '' ? 0 : Number(body.centerX);
|
|
const centerY = body.centerY === undefined || body.centerY === '' ? 0 : Number(body.centerY);
|
|
if (Math.abs(centerX) > 0.5 || Math.abs(centerY) > 0.5) {
|
|
return { error: 'Lens centre offset must be between -0.5 and 0.5' };
|
|
}
|
|
|
|
const distortion = distortionRows(body.d);
|
|
if (distortion.error) return { error: distortion.error };
|
|
const tca = tcaRows(body.t);
|
|
if (tca.error) return { error: tca.error };
|
|
const vignetting = vignettingRows(body.v);
|
|
if (vignetting.error) return { error: vignetting.error };
|
|
|
|
if (!distortion.rows.length && !tca.rows.length && !vignetting.rows.length) {
|
|
return { error: 'Add at least one distortion, chromatic aberration or vignetting sample' };
|
|
}
|
|
|
|
return {
|
|
value: {
|
|
maker,
|
|
model,
|
|
mount: clampText(body.mount, 40),
|
|
crop,
|
|
aspect,
|
|
centerX,
|
|
centerY,
|
|
notes: clampText(body.notes, 500),
|
|
d: distortion.rows,
|
|
t: tca.rows,
|
|
v: vignetting.rows,
|
|
},
|
|
};
|
|
}
|