Match EXIF lens names by brand and share team calibrations

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>
This commit is contained in:
2026-11-28 05:49:47 +00:00
parent 16456c3c69
commit 749047ae9e
13 changed files with 1363 additions and 15 deletions
+510
View File
@@ -0,0 +1,510 @@
import { useEffect, useState } from 'react';
import {
deleteTeamProfile,
loadTeamProfiles,
saveTeamProfile,
type TeamLensDraft,
type TeamLensProfile,
} from '../lib/lensProfiles';
import type { LensProfile, PhotoRecord } from '../types';
/**
* Calibrations the team measured for lenses the bundled database lacks. The
* numbers are stored on the server next to the bundled data, so everyone's
* editor and exports pick them up.
*/
interface Props {
photo: PhotoRecord;
/** Profile currently applied, used to prefill the form. */
profile?: LensProfile;
onClose: () => void;
/** Apply a saved calibration to the photo on screen. */
onUse: (profile: TeamLensProfile) => void;
}
const DISTORTION_MODEL_ID = { poly3: 0, poly5: 1, ptlens: 2 } as const;
const DISTORTION_MODEL_NAME = { 0: 'poly3', 1: 'poly5', 2: 'ptlens' } as const;
const TCA_MODEL_ID = { poly3: 0, linear: 1 } as const;
const TCA_MODEL_NAME = { 0: 'poly3', 1: 'linear' } as const;
type DistortionModel = keyof typeof DISTORTION_MODEL_ID;
type TcaModel = keyof typeof TCA_MODEL_ID;
interface DistortionRow {
focal: string;
model: DistortionModel;
a: string;
b: string;
c: string;
}
interface TcaRow {
focal: string;
model: TcaModel;
br: string;
cr: string;
vr: string;
bb: string;
cb: string;
vb: string;
}
interface VignettingRow {
focal: string;
aperture: string;
distance: string;
k1: string;
k2: string;
k3: string;
}
interface Draft {
id?: string;
maker: string;
model: string;
mount: string;
crop: string;
notes: string;
d: DistortionRow[];
t: TcaRow[];
v: VignettingRow[];
}
const num = (value: string) => (value.trim() === '' ? 0 : Number(value));
const text = (value: number) => (Number.isFinite(value) && value !== 0 ? String(value) : '');
function toDraft(profile: TeamLensProfile): Draft {
return {
id: profile.id,
maker: profile.maker,
model: profile.model,
mount: profile.mount,
crop: String(profile.crop),
notes: profile.notes,
d: profile.d.map(([focal, model, a, b, c]) => ({
focal: String(focal),
model: DISTORTION_MODEL_NAME[model as 0 | 1 | 2] ?? 'poly3',
a: text(a),
b: text(b),
c: text(c),
})),
t: profile.t.map(([focal, model, br, cr, vr, bb, cb, vb]) => ({
focal: String(focal),
model: TCA_MODEL_NAME[model as 0 | 1] ?? 'poly3',
br: text(br),
cr: text(cr),
vr: text(vr),
bb: text(bb),
cb: text(cb),
vb: text(vb),
})),
v: profile.v.map(([focal, aperture, distance, k1, k2, k3]) => ({
focal: String(focal),
aperture: String(aperture),
distance: text(distance),
k1: text(k1),
k2: text(k2),
k3: text(k3),
})),
};
}
function toPayload(draft: Draft): TeamLensDraft {
const focal = draft.d.find((row) => row.focal.trim())?.focal;
return {
id: draft.id,
maker: draft.maker.trim(),
model: draft.model.trim(),
mount: draft.mount.trim(),
crop: num(draft.crop) || 1,
aspect: 0,
centerX: 0,
centerY: 0,
notes: draft.notes.trim(),
d: draft.d
.filter((row) => row.focal.trim())
.map((row) => [
num(row.focal),
DISTORTION_MODEL_ID[row.model],
num(row.a),
num(row.b),
num(row.c),
0,
]),
t: draft.t
.filter((row) => row.focal.trim())
.map((row) => [
num(row.focal),
TCA_MODEL_ID[row.model],
num(row.br),
num(row.cr),
num(row.vr),
num(row.bb),
num(row.cb),
num(row.vb),
]),
v: draft.v
.filter((row) => row.focal.trim())
.map((row) => [
num(row.focal),
num(row.aperture),
num(row.distance) || 1000,
num(row.k1),
num(row.k2),
num(row.k3),
]),
};
}
const sampleCounts = (profile: TeamLensProfile) =>
[
profile.d.length ? `${profile.d.length} distortion` : '',
profile.t.length ? `${profile.t.length} CA` : '',
profile.v.length ? `${profile.v.length} vignetting` : '',
]
.filter(Boolean)
.join(' · ') || 'no samples';
export default function LensCalibrationModal({ photo, profile, onClose, onUse }: Props) {
const [profiles, setProfiles] = useState<TeamLensProfile[] | null>(null);
const [draft, setDraft] = useState<Draft | null>(null);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const blank = (): Draft => ({
maker: profile?.maker ?? '',
model: profile?.model ?? photo.meta?.lens ?? '',
mount: '',
crop: String(profile?.crop ?? 1),
notes: '',
d: [
{
focal: photo.meta?.focal ? String(photo.meta.focal) : '',
model: 'ptlens',
a: '',
b: '',
c: '',
},
],
t: [],
v: [],
});
useEffect(() => {
let alive = true;
loadTeamProfiles(true)
.then((saved) => alive && setProfiles(saved))
.catch((err) => {
if (!alive) return;
setProfiles([]);
setError(err instanceof Error ? err.message : 'Could not load the team calibrations');
});
return () => {
alive = false;
};
}, []);
useEffect(() => {
const onKey = (event: KeyboardEvent) => {
if (event.key !== 'Escape') return;
event.preventDefault();
event.stopImmediatePropagation();
if (!busy) onClose();
};
window.addEventListener('keydown', onKey, true);
return () => window.removeEventListener('keydown', onKey, true);
}, [busy, onClose]);
const patch = (next: Partial<Draft>) => setDraft((current) => (current ? { ...current, ...next } : current));
const patchRow = <K extends 'd' | 't' | 'v'>(
key: K,
index: number,
patchValue: Partial<Draft[K][number]>,
) =>
setDraft((current) => {
if (!current) return current;
const rows = current[key].map((row, i) => (i === index ? { ...row, ...patchValue } : row));
return { ...current, [key]: rows } as Draft;
});
const addRow = (key: 'd' | 't' | 'v') =>
setDraft((current) => {
if (!current) return current;
const focal = photo.meta?.focal ? String(photo.meta.focal) : '';
const aperture = photo.meta?.aperture ? String(photo.meta.aperture) : '';
if (key === 'd') return { ...current, d: [...current.d, { focal, model: 'ptlens', a: '', b: '', c: '' }] };
if (key === 't') {
return {
...current,
t: [...current.t, { focal, model: 'poly3', br: '', cr: '', vr: '', bb: '', cb: '', vb: '' }],
};
}
return { ...current, v: [...current.v, { focal, aperture, distance: '1000', k1: '', k2: '', k3: '' }] };
});
const removeRow = (key: 'd' | 't' | 'v', index: number) =>
setDraft((current) => {
if (!current) return current;
return { ...current, [key]: current[key].filter((_, i) => i !== index) } as Draft;
});
const save = async () => {
if (!draft || busy) return;
setBusy(true);
setError(null);
try {
const saved = await saveTeamProfile(toPayload(draft));
setProfiles((current) =>
current
? [saved, ...current.filter((entry) => entry.id !== saved.id)]
: [saved],
);
setDraft(null);
onUse(saved);
} catch (err) {
setError(err instanceof Error ? err.message : 'Could not save the calibration');
} finally {
setBusy(false);
}
};
const remove = async (entry: TeamLensProfile) => {
if (busy) return;
if (!window.confirm(`Delete the team calibration for ${entry.maker} ${entry.model}?`)) return;
setBusy(true);
setError(null);
try {
await deleteTeamProfile(entry.id);
setProfiles((current) => current?.filter((saved) => saved.id !== entry.id) ?? []);
} catch (err) {
setError(err instanceof Error ? err.message : 'Could not delete the calibration');
} finally {
setBusy(false);
}
};
const field = (label: string, value: string, onChange: (value: string) => void, type = 'text') => (
<label className="lens-calib-field">
<span className="muted small">{label}</span>
<input
className="text-input"
type={type}
step="any"
value={value}
onChange={(event) => onChange(event.target.value)}
/>
</label>
);
return (
<div className="modal-backdrop" onPointerDown={(e) => e.target === e.currentTarget && onClose()}>
<div className="modal lens-calib-modal">
<header className="modal-head">
<div>
<span className="settings-kicker">Team</span>
<h2>Lens calibrations</h2>
</div>
<button className="icon-btn" onClick={onClose} title="Close">
✕
</button>
</header>
<div className="modal-body">
{draft ? (
<>
<p className="muted small lens-defaults-lead">
Enter the measurements from your calibration tool. Coefficients are used as measured:
distortion is a·r³ + b·r² + c·r + 1 for PanoTools, 1 + a·r² + b·r⁴ for poly5 and
1 + k₁·r² for poly3. Add a row per focal length you measured.
</p>
<div className="lens-calib-grid">
{field('Maker', draft.maker, (value) => patch({ maker: value }))}
{field('Model', draft.model, (value) => patch({ model: value }))}
{field('Mount', draft.mount, (value) => patch({ mount: value }))}
{field('Crop factor', draft.crop, (value) => patch({ crop: value }), 'number')}
</div>
{field('Notes', draft.notes, (value) => patch({ notes: value }))}
<section className="lens-calib-section">
<header>
<strong>Distortion</strong>
<button className="btn ghost tiny" onClick={() => addRow('d')}>
+ Sample
</button>
</header>
{draft.d.length === 0 && <p className="muted small">No distortion measured.</p>}
{draft.d.map((row, index) => (
<div className="lens-calib-row cols-distortion" key={index}>
{field('Focal mm', row.focal, (value) => patchRow('d', index, { focal: value }), 'number')}
<label className="lens-calib-field">
<span className="muted small">Model</span>
<select
className="text-input"
value={row.model}
onChange={(event) =>
patchRow('d', index, { model: event.target.value as DistortionModel })
}
>
<option value="ptlens">PanoTools</option>
<option value="poly3">poly3</option>
<option value="poly5">poly5</option>
</select>
</label>
{field('a', row.a, (value) => patchRow('d', index, { a: value }), 'number')}
{field('b', row.b, (value) => patchRow('d', index, { b: value }), 'number')}
{field('c', row.c, (value) => patchRow('d', index, { c: value }), 'number')}
<button
className="icon-btn"
title="Remove sample"
onClick={() => removeRow('d', index)}
>
✕
</button>
</div>
))}
</section>
<section className="lens-calib-section">
<header>
<strong>Chromatic aberration</strong>
<button className="btn ghost tiny" onClick={() => addRow('t')}>
+ Sample
</button>
</header>
{draft.t.length === 0 && <p className="muted small">No lateral CA measured.</p>}
{draft.t.map((row, index) => (
<div className="lens-calib-row cols-tca" key={index}>
{field('Focal mm', row.focal, (value) => patchRow('t', index, { focal: value }), 'number')}
<label className="lens-calib-field">
<span className="muted small">Model</span>
<select
className="text-input"
value={row.model}
onChange={(event) => patchRow('t', index, { model: event.target.value as TcaModel })}
>
<option value="poly3">poly3</option>
<option value="linear">linear</option>
</select>
</label>
{field('red b', row.br, (value) => patchRow('t', index, { br: value }), 'number')}
{field('red c', row.cr, (value) => patchRow('t', index, { cr: value }), 'number')}
{field('red v', row.vr, (value) => patchRow('t', index, { vr: value }), 'number')}
{field('blue b', row.bb, (value) => patchRow('t', index, { bb: value }), 'number')}
{field('blue c', row.cb, (value) => patchRow('t', index, { cb: value }), 'number')}
{field('blue v', row.vb, (value) => patchRow('t', index, { vb: value }), 'number')}
<button
className="icon-btn"
title="Remove sample"
onClick={() => removeRow('t', index)}
>
✕
</button>
</div>
))}
</section>
<section className="lens-calib-section">
<header>
<strong>Vignetting</strong>
<button className="btn ghost tiny" onClick={() => addRow('v')}>
+ Sample
</button>
</header>
{draft.v.length === 0 && <p className="muted small">No vignetting measured.</p>}
{draft.v.map((row, index) => (
<div className="lens-calib-row cols-vignetting" key={index}>
{field('Focal mm', row.focal, (value) => patchRow('v', index, { focal: value }), 'number')}
{field('Aperture', row.aperture, (value) => patchRow('v', index, { aperture: value }), 'number')}
{field('Distance', row.distance, (value) => patchRow('v', index, { distance: value }), 'number')}
{field('k₁', row.k1, (value) => patchRow('v', index, { k1: value }), 'number')}
{field('k₂', row.k2, (value) => patchRow('v', index, { k2: value }), 'number')}
{field('k₃', row.k3, (value) => patchRow('v', index, { k3: value }), 'number')}
<button
className="icon-btn"
title="Remove sample"
onClick={() => removeRow('v', index)}
>
✕
</button>
</div>
))}
</section>
</>
) : profiles === null ? (
<p className="muted small">Loading the team calibrations…</p>
) : (
<>
<p className="muted small lens-defaults-lead">
These are shared with everyone signed in to this library. The Optics panel finds them
by maker or model, next to the bundled profiles.
</p>
<div className="lens-defaults-list">
{profiles.length === 0 && (
<p className="muted small">No team calibrations yet — add the first one.</p>
)}
{profiles.map((entry) => (
<article className="lens-default-item" key={entry.id}>
<div className="lens-default-main">
<div className="lens-default-title">
<strong>
{entry.maker} {entry.model}
</strong>
<span className="muted small">crop {entry.crop}×</span>
</div>
<p className="lens-default-match">
{entry.mount || 'any mount'} <span className="muted small">· {sampleCounts(entry)}</span>
</p>
{entry.notes && <p className="muted small lens-default-meta">{entry.notes}</p>}
</div>
<div className="lens-default-actions">
<button className="btn ghost small" onClick={() => onUse(entry)}>
Use
</button>
<button className="btn ghost tiny" onClick={() => setDraft(toDraft(entry))}>
Edit
</button>
<button
className="btn ghost tiny"
disabled={busy}
onClick={() => void remove(entry)}
>
Delete
</button>
</div>
</article>
))}
</div>
</>
)}
{error && <p className="small lens-error">{error}</p>}
</div>
<footer className="modal-foot">
{draft ? (
<>
<button className="btn ghost" onClick={() => setDraft(null)} disabled={busy}>
Cancel
</button>
<button className="btn primary" onClick={() => void save()} disabled={busy}>
{busy ? 'Saving…' : draft.id ? 'Save calibration' : 'Add calibration'}
</button>
</>
) : (
<>
<button className="btn ghost" onClick={onClose}>
Done
</button>
<button className="btn primary" onClick={() => setDraft(blank())} disabled={busy}>
New calibration
</button>
</>
)}
</footer>
</div>
</div>
);
}