diff --git a/server/db.mjs b/server/db.mjs index 106325c..52ccdd5 100644 --- a/server/db.mjs +++ b/server/db.mjs @@ -281,6 +281,56 @@ if (!immichAlbumColumns.includes('kind')) { db.exec("ALTER TABLE immich_albums ADD COLUMN kind TEXT NOT NULL DEFAULT 'import'"); } +/** + * Applying a lens profile used to enable its vignetting correction, and that + * flat-field gain over-exposed the edges. The correction is opt-in now, so the + * flag older edits stored is cleared once, here, before anything reads the + * catalogue. Matching on the JSON text keeps the scan to the few rows that ever + * carried it, and the rows it rewrites no longer match, so it is idempotent. + */ +const legacyVignetting = db + .prepare(`SELECT id, data FROM photos WHERE instr(data, '"vignetting":true') > 0`) + .all(); +if (legacyVignetting.length) { + const updateData = db.prepare('UPDATE photos SET data = ?, updated_at = ? WHERE id = ?'); + const clear = (edits) => { + const lens = edits && edits.lens; + if (!lens || !lens.corrections || lens.corrections.vignetting !== true) return edits; + return { ...edits, lens: { ...lens, corrections: { ...lens.corrections, vignetting: false } } }; + }; + let cleared = 0; + for (const row of legacyVignetting) { + let record; + try { + record = JSON.parse(row.data); + } catch { + continue; + } + if (!record || typeof record !== 'object') continue; + let changed = false; + const strip = (edits) => { + const next = clear(edits); + if (next !== edits) changed = true; + return next; + }; + record.edits = strip(record.edits); + if (Array.isArray(record.snapshots)) { + record.snapshots = record.snapshots.map((snap) => + snap && snap.edits ? { ...snap, edits: strip(snap.edits) } : snap, + ); + } + if (Array.isArray(record.history)) { + record.history = record.history.map((entry) => + entry && entry.edits ? { ...entry, edits: strip(entry.edits) } : entry, + ); + } + if (!changed) continue; + updateData.run(JSON.stringify(record), Date.now(), row.id); + cleared += 1; + } + if (cleared) console.log(`lens vignetting: cleared the stored corner gain on ${cleared} photo(s)`); +} + const q = { upsertEvent: db.prepare(` INSERT INTO events (id, owner_id, sort_index, created_at, data) VALUES (?, ?, ?, ?, ?) diff --git a/src/components/Panels.tsx b/src/components/Panels.tsx index 65b7fc8..0504aa7 100644 --- a/src/components/Panels.tsx +++ b/src/components/Panels.tsx @@ -2226,6 +2226,12 @@ export function OpticsPanel({ ); })} + {!profile.corrections.vignetting && ( +

+ Vignetting correction is off, so the corners keep the lens's own falloff. + Tick Vignetting to lift them back to the centre. +

+ )} {lensState?.loading &&

Building correction maps…

} {lensState?.error &&

{lensState.error}

} {onLensSplit && ( diff --git a/src/gl/renderer.ts b/src/gl/renderer.ts index 6771da7..2ac4728 100644 --- a/src/gl/renderer.ts +++ b/src/gl/renderer.ts @@ -333,10 +333,6 @@ void main(){ ? sampleBicubic(u_src, uv, u_srcSize) : textureLod(u_src, uv, u_srcLod).rgb; } - if (showLens && (u_lensFlags & 4) != 0) { - src *= lensSample(u_lensVig, u_lensVigSize, lensQ).rgb; - } - // Camera-native proxies are converted once, before the source is read any // further: the frame, the healed patches and the eyedropper all work on the // same sRGB values whatever the camera matrix was. @@ -369,9 +365,15 @@ void main(){ } } - // Eyedropper: hand back the untouched source colour at the sampled point. + // Eyedropper: hand back the source colour at the sampled point, with the lens + // gain applied the same way the frame is, so a corner sample reads the + // brightness on screen. if (u_pick > 0.5) { - outColor = vec4(src, 1.0); + vec3 picked = toLinear(clamp(src, 0.0, 1.0)); + if (showLens && (u_lensFlags & 4) != 0) { + picked *= lensSample(u_lensVig, u_lensVigSize, lensQ).rgb; + } + outColor = vec4(clamp(toSRGB(picked), 0.0, 1.0), 1.0); return; } @@ -385,6 +387,13 @@ void main(){ vec3 col = toLinear(clamp(src, 0.0, 1.0)); + // Lens vignetting is a flat-field gain from Lensfun's linear-light + // transmission model, so it belongs after the sRGB decode. Multiplying it into + // the encoded values brightened the corners far more than the lens lost. + if (showLens && (u_lensFlags & 4) != 0) { + col *= lensSample(u_lensVig, u_lensVigSize, lensQ).rgb; + } + // White balance gains, solved on the CPU: either the relative Temp offset or // the absolute Kelvin control, both normalised so exposure is unchanged. col *= u_wbGain; diff --git a/src/lib/db.ts b/src/lib/db.ts index 64e1396..64fdc07 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -1,7 +1,8 @@ import type { EventRecord, PhotoRecord } from '../types'; +import { withoutLensVignetting } from './edits'; const DB_NAME = 'lumenroom'; -const DB_VERSION = 1; +const DB_VERSION = 2; let dbPromise: Promise | null = null; @@ -9,7 +10,7 @@ export function openDB(): Promise { if (dbPromise) return dbPromise; dbPromise = new Promise((resolve, reject) => { const req = indexedDB.open(DB_NAME, DB_VERSION); - req.onupgradeneeded = () => { + req.onupgradeneeded = (event) => { const db = req.result; if (!db.objectStoreNames.contains('photos')) { const store = db.createObjectStore('photos', { keyPath: 'id' }); @@ -18,6 +19,11 @@ export function openDB(): Promise { if (!db.objectStoreNames.contains('assets')) db.createObjectStore('assets'); if (!db.objectStoreNames.contains('events')) db.createObjectStore('events', { keyPath: 'id' }); if (!db.objectStoreNames.contains('kv')) db.createObjectStore('kv'); + // v2: applying a lens profile used to enable its vignetting correction, + // and the corner gain over-exposed the edges. Clear that flag once, here, + // instead of on every load, so a profile the user enables afterwards stays + // enabled. + if (event.oldVersion < 2) clearStoredLensVignetting(req.transaction); }; req.onsuccess = () => resolve(req.result); req.onerror = () => reject(req.error ?? new Error('Unable to open database')); @@ -25,6 +31,22 @@ export function openDB(): Promise { return dbPromise; } +/** + * Walks the photo store inside the version-change transaction and clears any + * stored lens vignetting flag. Runs once, on the database upgrade. + */ +function clearStoredLensVignetting(tx: IDBTransaction | null) { + if (!tx) return; + const cursorRequest = tx.objectStore('photos').openCursor(); + cursorRequest.onsuccess = () => { + const cursor = cursorRequest.result; + if (!cursor) return; + const cleared = withoutLensVignetting(cursor.value as PhotoRecord); + if (cleared) cursor.update(cleared); + cursor.continue(); + }; +} + function wrap(request: IDBRequest): Promise { return new Promise((resolve, reject) => { request.onsuccess = () => resolve(request.result); diff --git a/src/lib/edits.ts b/src/lib/edits.ts index d444440..75669e8 100644 --- a/src/lib/edits.ts +++ b/src/lib/edits.ts @@ -431,6 +431,36 @@ function lensProfile(input: unknown): LensProfile | undefined { }; } +/** + * One-off cleanup for records saved while applying a lens profile enabled all + * three corrections. The vignetting gain over-exposed the edges on the tone + * mapped proxy, so it is opt-in now; this clears the flag old edits stored, + * including snapshots and history, and returns null when nothing needed doing. + */ +export function withoutLensVignetting(photo: PhotoRecord): PhotoRecord | null { + let changed = false; + const strip = (edits: Edits): Edits => { + const lens = edits.lens; + if (!lens || lens.corrections.vignetting !== true) return edits; + changed = true; + return { + ...edits, + lens: { ...lens, corrections: { ...lens.corrections, vignetting: false } }, + }; + }; + const edits = strip(photo.edits); + const snapshots = photo.snapshots?.map((snapshot) => ({ + ...snapshot, + edits: strip(snapshot.edits), + })); + const history = photo.history?.map((entry) => ({ ...entry, edits: strip(entry.edits) })); + if (!changed) return null; + const next: PhotoRecord = { ...photo, edits }; + if (snapshots) next.snapshots = snapshots; + if (history) next.history = history; + return next; +} + /** * Keeps a stored camera profile usable: a profile with a missing or non-finite * matrix would black out the photo, so it is dropped instead. diff --git a/src/lib/lensDefaults.test.ts b/src/lib/lensDefaults.test.ts index 5cf2f55..1db79c2 100644 --- a/src/lib/lensDefaults.test.ts +++ b/src/lib/lensDefaults.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from 'vitest'; -import { normalizeEdits } from './edits'; +import { normalizeEdits, withoutLensVignetting } from './edits'; import { defaultForMeta, type CameraLensDefault } from './lensDefaults'; -import type { LensProfile } from '../types'; +import type { LensProfile, PhotoRecord } from '../types'; const profile = (model: string): LensProfile => ({ maker: 'Nikon', @@ -81,4 +81,31 @@ describe('camera-lens default selection', () => { } as never); expect(optedIn.lens?.corrections.vignetting).toBe(true); }); + + it('clears a stored vignetting flag once, in edits, snapshots and history', () => { + const profile: LensProfile = { + maker: 'Canon', + model: 'EF 50mm', + crop: 1, + corrections: { distortion: true, tca: true, vignetting: true }, + }; + const photo = { + id: 'p1', + edits: { lens: profile }, + snapshots: [{ id: 's1', name: 'Before', ts: 1, edits: { lens: profile } }], + history: [ + { label: 'Import', ts: 1, edits: { lens: profile } }, + { label: 'Exposure', ts: 2, edits: {} }, + ], + } as unknown as PhotoRecord; + + const cleared = withoutLensVignetting(photo); + expect(cleared?.edits.lens?.corrections.vignetting).toBe(false); + expect(cleared?.snapshots?.[0].edits.lens?.corrections.vignetting).toBe(false); + expect(cleared?.history?.[0].edits.lens?.corrections.vignetting).toBe(false); + // Geometry and CA stay on: only the corner gain was the problem. + expect(cleared?.edits.lens?.corrections.distortion).toBe(true); + // Nothing left to do the second time, so the upgrade is idempotent. + expect(withoutLensVignetting(cleared as PhotoRecord)).toBeNull(); + }); }); diff --git a/src/lib/lensEngine.ts b/src/lib/lensEngine.ts index 964dfa1..5c65c72 100644 --- a/src/lib/lensEngine.ts +++ b/src/lib/lensEngine.ts @@ -140,7 +140,11 @@ export interface MapSet { geometry: Float32Array; /** 6 floats per point: red x/y, input x/y, blue x/y. */ tca?: Float32Array; - /** 3 floats per point: the same gain for R, G and B. */ + /** + * 3 floats per point: the same gain for R, G and B. Lensfun's vignetting + * model describes linear light, so the renderer must apply this after the + * sRGB decode rather than to encoded code values. + */ vignetting?: Float32Array; }