Show camera profiles in every view and remember proof and DCP settings.
A profile chosen for a raw only changed Develop and exports, so the grid, Compare and preset previews could disagree with the file. They now grade camera-native samples, while the proof profile persists with a rendering intent and DCP matrices can be reused per camera body. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
/**
|
||||
* 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<Storage, 'getItem' | 'setItem'>;
|
||||
|
||||
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<string, unknown>;
|
||||
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;
|
||||
}
|
||||
|
||||
/** 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));
|
||||
}
|
||||
Reference in New Issue
Block a user