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(null); const [draft, setDraft] = useState(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(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) => setDraft((current) => (current ? { ...current, ...next } : current)); const patchRow = ( key: K, index: number, patchValue: Partial, ) => 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') => ( ); return (
e.target === e.currentTarget && onClose()}>
Team

Lens calibrations

{draft ? ( <>

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.

{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')}
{field('Notes', draft.notes, (value) => patch({ notes: value }))}
Distortion
{draft.d.length === 0 &&

No distortion measured.

} {draft.d.map((row, index) => (
{field('Focal mm', row.focal, (value) => patchRow('d', index, { focal: value }), 'number')} {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')}
))}
Chromatic aberration
{draft.t.length === 0 &&

No lateral CA measured.

} {draft.t.map((row, index) => (
{field('Focal mm', row.focal, (value) => patchRow('t', index, { focal: value }), 'number')} {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')}
))}
Vignetting
{draft.v.length === 0 &&

No vignetting measured.

} {draft.v.map((row, 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')}
))}
) : profiles === null ? (

Loading the team calibrations…

) : ( <>

These are shared with everyone signed in to this library. The Optics panel finds them by maker or model, next to the bundled profiles.

{profiles.length === 0 && (

No team calibrations yet — add the first one.

)} {profiles.map((entry) => (
{entry.maker} {entry.model} crop {entry.crop}×

{entry.mount || 'any mount'} · {sampleCounts(entry)}

{entry.notes &&

{entry.notes}

}
))}
)} {error &&

{error}

}
); }