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:
@@ -281,6 +281,56 @@ if (!immichAlbumColumns.includes('kind')) {
|
|||||||
db.exec("ALTER TABLE immich_albums ADD COLUMN kind TEXT NOT NULL DEFAULT 'import'");
|
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 = {
|
const q = {
|
||||||
upsertEvent: db.prepare(`
|
upsertEvent: db.prepare(`
|
||||||
INSERT INTO events (id, owner_id, sort_index, created_at, data) VALUES (?, ?, ?, ?, ?)
|
INSERT INTO events (id, owner_id, sort_index, created_at, data) VALUES (?, ?, ?, ?, ?)
|
||||||
|
|||||||
@@ -2226,6 +2226,12 @@ export function OpticsPanel({
|
|||||||
</label>
|
</label>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
{!profile.corrections.vignetting && (
|
||||||
|
<p className="muted small lens-msg">
|
||||||
|
Vignetting correction is off, so the corners keep the lens's own falloff.
|
||||||
|
Tick Vignetting to lift them back to the centre.
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
{lensState?.loading && <p className="muted small lens-msg">Building correction maps…</p>}
|
{lensState?.loading && <p className="muted small lens-msg">Building correction maps…</p>}
|
||||||
{lensState?.error && <p className="small lens-error">{lensState.error}</p>}
|
{lensState?.error && <p className="small lens-error">{lensState.error}</p>}
|
||||||
{onLensSplit && (
|
{onLensSplit && (
|
||||||
|
|||||||
+15
-6
@@ -333,10 +333,6 @@ void main(){
|
|||||||
? sampleBicubic(u_src, uv, u_srcSize)
|
? sampleBicubic(u_src, uv, u_srcSize)
|
||||||
: textureLod(u_src, uv, u_srcLod).rgb;
|
: 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
|
// 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
|
// further: the frame, the healed patches and the eyedropper all work on the
|
||||||
// same sRGB values whatever the camera matrix was.
|
// 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) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -385,6 +387,13 @@ void main(){
|
|||||||
|
|
||||||
vec3 col = toLinear(clamp(src, 0.0, 1.0));
|
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
|
// White balance gains, solved on the CPU: either the relative Temp offset or
|
||||||
// the absolute Kelvin control, both normalised so exposure is unchanged.
|
// the absolute Kelvin control, both normalised so exposure is unchanged.
|
||||||
col *= u_wbGain;
|
col *= u_wbGain;
|
||||||
|
|||||||
+24
-2
@@ -1,7 +1,8 @@
|
|||||||
import type { EventRecord, PhotoRecord } from '../types';
|
import type { EventRecord, PhotoRecord } from '../types';
|
||||||
|
import { withoutLensVignetting } from './edits';
|
||||||
|
|
||||||
const DB_NAME = 'lumenroom';
|
const DB_NAME = 'lumenroom';
|
||||||
const DB_VERSION = 1;
|
const DB_VERSION = 2;
|
||||||
|
|
||||||
let dbPromise: Promise<IDBDatabase> | null = null;
|
let dbPromise: Promise<IDBDatabase> | null = null;
|
||||||
|
|
||||||
@@ -9,7 +10,7 @@ export function openDB(): Promise<IDBDatabase> {
|
|||||||
if (dbPromise) return dbPromise;
|
if (dbPromise) return dbPromise;
|
||||||
dbPromise = new Promise((resolve, reject) => {
|
dbPromise = new Promise((resolve, reject) => {
|
||||||
const req = indexedDB.open(DB_NAME, DB_VERSION);
|
const req = indexedDB.open(DB_NAME, DB_VERSION);
|
||||||
req.onupgradeneeded = () => {
|
req.onupgradeneeded = (event) => {
|
||||||
const db = req.result;
|
const db = req.result;
|
||||||
if (!db.objectStoreNames.contains('photos')) {
|
if (!db.objectStoreNames.contains('photos')) {
|
||||||
const store = db.createObjectStore('photos', { keyPath: 'id' });
|
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('assets')) db.createObjectStore('assets');
|
||||||
if (!db.objectStoreNames.contains('events')) db.createObjectStore('events', { keyPath: 'id' });
|
if (!db.objectStoreNames.contains('events')) db.createObjectStore('events', { keyPath: 'id' });
|
||||||
if (!db.objectStoreNames.contains('kv')) db.createObjectStore('kv');
|
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.onsuccess = () => resolve(req.result);
|
||||||
req.onerror = () => reject(req.error ?? new Error('Unable to open database'));
|
req.onerror = () => reject(req.error ?? new Error('Unable to open database'));
|
||||||
@@ -25,6 +31,22 @@ export function openDB(): Promise<IDBDatabase> {
|
|||||||
return dbPromise;
|
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> {
|
function wrap<T>(request: IDBRequest<T>): Promise<T> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
request.onsuccess = () => resolve(request.result);
|
request.onsuccess = () => resolve(request.result);
|
||||||
|
|||||||
@@ -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
|
* 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.
|
* matrix would black out the photo, so it is dropped instead.
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { describe, expect, it } from 'vitest';
|
import { describe, expect, it } from 'vitest';
|
||||||
import { normalizeEdits } from './edits';
|
import { normalizeEdits, withoutLensVignetting } from './edits';
|
||||||
import { defaultForMeta, type CameraLensDefault } from './lensDefaults';
|
import { defaultForMeta, type CameraLensDefault } from './lensDefaults';
|
||||||
import type { LensProfile } from '../types';
|
import type { LensProfile, PhotoRecord } from '../types';
|
||||||
|
|
||||||
const profile = (model: string): LensProfile => ({
|
const profile = (model: string): LensProfile => ({
|
||||||
maker: 'Nikon',
|
maker: 'Nikon',
|
||||||
@@ -81,4 +81,31 @@ describe('camera-lens default selection', () => {
|
|||||||
} as never);
|
} as never);
|
||||||
expect(optedIn.lens?.corrections.vignetting).toBe(true);
|
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();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -140,7 +140,11 @@ export interface MapSet {
|
|||||||
geometry: Float32Array;
|
geometry: Float32Array;
|
||||||
/** 6 floats per point: red x/y, input x/y, blue x/y. */
|
/** 6 floats per point: red x/y, input x/y, blue x/y. */
|
||||||
tca?: Float32Array;
|
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;
|
vignetting?: Float32Array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user