Apply the lens vignetting gain in linear light.

Lensfun's vignetting coefficients describe linear transmission, so
multiplying them into the sRGB-encoded proxy brightened the corners far
more than the lens had lost and over-exposed the edges. The gain now runs
after the decode, in both the grade and the eyedropper.

Existing records that stored the old auto-enabled flag are cleared once,
on the server catalogue at boot and in the browser cache on its next
IndexedDB upgrade, and the Optics panel says when the correction is off.
This commit is contained in:
2026-11-28 16:34:31 +00:00
parent be8cf3c88a
commit 5c2903eeef
7 changed files with 159 additions and 11 deletions
+24 -2
View File
@@ -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<IDBDatabase> | null = null;
@@ -9,7 +10,7 @@ export function openDB(): Promise<IDBDatabase> {
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<IDBDatabase> {
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<IDBDatabase> {
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<T>(request: IDBRequest<T>): Promise<T> {
return new Promise((resolve, reject) => {
request.onsuccess = () => resolve(request.result);
+30
View File
@@ -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.
+29 -2
View File
@@ -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();
});
});
+5 -1
View File
@@ -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;
}