/** * Per-camera DCP profile library. * * A DCP's colour matrix describes the sensor, not the frame, so every raw from * the same body can use the same derived matrix. The library keeps those * matrices in localStorage (name, camera and nine numbers — never the file) so * switching photos does not mean opening a file dialog again. Each edit still * stores its own copy of the matrix, so a photo stays self-contained and the * library is only a shortcut for filling it in. */ import { cameraKey } from './lensNames'; export interface SavedCameraProfile { id: string; name: string; camera: string; matrix: number[]; savedAt: number; } const STORAGE_KEY = 'lumenroom.cameraProfiles'; /** Enough for a shelf of bodies without letting a stray write grow unbounded. */ const LIMIT = 32; /** The slice of Storage the library needs; injectable so it is testable. */ export type ProfileStore = Pick; function defaultStore(): ProfileStore | null { try { return globalThis.localStorage ?? null; } catch { return null; } } function validMatrix(value: unknown): value is number[] { return ( Array.isArray(value) && value.length === 9 && value.every((entry) => typeof entry === 'number' && Number.isFinite(entry)) ); } /** Reads the library, dropping anything that no longer looks like a matrix. */ export function loadSavedCameraProfiles(store: ProfileStore | null = defaultStore()): SavedCameraProfile[] { if (!store) return []; try { const raw = store.getItem(STORAGE_KEY); if (!raw) return []; const parsed: unknown = JSON.parse(raw); if (!Array.isArray(parsed)) return []; return parsed.flatMap((entry) => { if (!entry || typeof entry !== 'object') return []; const { id, name, camera, matrix, savedAt } = entry as Record; if (typeof id !== 'string' || typeof name !== 'string' || typeof camera !== 'string') return []; if (!validMatrix(matrix)) return []; return [ { id, name, camera, matrix: [...matrix], savedAt: typeof savedAt === 'number' && Number.isFinite(savedAt) ? savedAt : 0, }, ]; }); } catch { return []; } } function persist(entries: SavedCameraProfile[], store: ProfileStore | null) { if (!store) return; try { store.setItem(STORAGE_KEY, JSON.stringify(entries)); } catch { /* Quota or private mode: the edit still has the matrix it was given. */ } } /** * Adds or replaces a profile for the same camera and name. Returns the library * as saved so a panel can update without re-reading storage. */ export function saveCameraProfile( entry: { name: string; camera: string; matrix: number[] }, store: ProfileStore | null = defaultStore(), ): SavedCameraProfile[] { const saved = loadSavedCameraProfiles(store); if (!entry.name.trim() || !validMatrix(entry.matrix)) return saved; const camera = entry.camera.trim() || 'Unknown camera'; const id = `${cameraKey(camera) || 'camera'}|${entry.name.trim()}`.toLowerCase(); const next: SavedCameraProfile[] = [ { id, name: entry.name.trim(), camera, matrix: [...entry.matrix], savedAt: Date.now(), }, ...saved.filter((profile) => profile.id !== id), ].slice(0, LIMIT); persist(next, store); return next; } export function removeCameraProfile( id: string, store: ProfileStore | null = defaultStore(), ): SavedCameraProfile[] { const next = loadSavedCameraProfiles(store).filter((profile) => profile.id !== id); persist(next, store); return next; } /** Minimal photo shape needed to pick the raws of one body. */ export interface CameraProfileTarget { id: string; raw?: boolean; decode?: string; deletedAt?: number; meta?: { camera?: string }; } /** * Every live camera-native raw from the same body, including the one on screen. * A JPEG, or a raw the server decoded straight to sRGB, already has its colour * baked in, so a camera profile has nothing to act on there. */ export function photosForCamera( photos: T[], camera?: string, ): T[] { const key = cameraKey(camera); if (!key) return []; return photos.filter( (photo) => photo.raw && photo.decode !== 'embedded' && !photo.deletedAt && cameraKey(photo.meta?.camera) === key, ); } /** Profiles that belong to this body, most recently saved first. */ export function savedProfilesForCamera( profiles: SavedCameraProfile[], camera?: string, ): SavedCameraProfile[] { const key = cameraKey(camera); if (!key) return []; return profiles .filter((profile) => cameraKey(profile.camera) === key) .sort((left, right) => right.savedAt - left.savedAt || left.name.localeCompare(right.name)); }