import type { Edits } from '../types'; import { clamp } from './curves'; interface Stats { mean: number; p05: number; p95: number; saturation: number; } async function analyze(thumb: string): Promise { const img = new Image(); img.src = thumb; await img.decode(); const w = 96; const h = Math.max(1, Math.round((img.height / img.width) * w)) || 96; const canvas = document.createElement('canvas'); canvas.width = w; canvas.height = h; const ctx = canvas.getContext('2d'); if (!ctx) throw new Error('Canvas unavailable'); ctx.drawImage(img, 0, 0, w, h); const { data } = ctx.getImageData(0, 0, w, h); const hist = new Uint32Array(256); let total = 0; let lumaSum = 0; let satSum = 0; for (let i = 0; i < data.length; i += 4) { const r = data[i] / 255; const g = data[i + 1] / 255; const b = data[i + 2] / 255; const l = 0.2126 * r + 0.7152 * g + 0.0722 * b; hist[Math.round(l * 255)]++; lumaSum += l; satSum += Math.max(r, g, b) - Math.min(r, g, b); total++; } const percentile = (p: number) => { const target = total * p; let acc = 0; for (let i = 0; i < 256; i++) { acc += hist[i]; if (acc >= target) return i / 255; } return 1; }; return { mean: lumaSum / total, p05: percentile(0.05), p95: percentile(0.95), saturation: satSum / total, }; } /** Simple histogram based auto tone, in the spirit of Lightroom's Auto button. */ export async function computeAutoEdits(thumb: string): Promise> { try { const s = await analyze(thumb); const mean = clamp(s.mean, 0.03, 0.97); const exposure = clamp(Math.log2(0.46 / mean), -2, 2); return { exposure: Math.round(exposure * 100) / 100, contrast: s.p95 - s.p05 < 0.55 ? 18 : 10, highlights: s.p95 > 0.94 ? -32 : 4, shadows: s.p05 < 0.055 ? 32 : 4, whites: s.p95 > 0.96 ? -8 : 10, blacks: s.p05 < 0.03 ? 6 : -6, vibrance: s.saturation < 0.18 ? 18 : 8, }; } catch { return { contrast: 12, shadows: 12, highlights: -12, vibrance: 8 }; } } /** * Grey-world auto white balance. It averages the unclipped midtones, then * searches the two WB sliders for the multipliers that neutralise them. This is * a starting point for mixed or unknown light, not a substitute for the * eyedropper on a known neutral. */ export async function computeAutoWhiteBalance( thumb: string, ): Promise<{ temp: number; tint: number }> { try { const img = new Image(); img.src = thumb; await img.decode(); const w = 96; const h = Math.max(1, Math.round((img.height / img.width) * w)) || 96; const canvas = document.createElement('canvas'); canvas.width = w; canvas.height = h; const ctx = canvas.getContext('2d'); if (!ctx) throw new Error('Canvas unavailable'); ctx.drawImage(img, 0, 0, w, h); const { data } = ctx.getImageData(0, 0, w, h); let r = 0; let g = 0; let b = 0; let count = 0; for (let i = 0; i < data.length; i += 4) { const rr = data[i]; const gg = data[i + 1]; const bb = data[i + 2]; const sum = rr + gg + bb; // Clipped highlights and near-black pixels carry no reliable cast. if (sum > 735 || sum < 24) continue; r += rr; g += gg; b += bb; count += 1; } if (!count) return { temp: 0, tint: 0 }; const ar = r / count / 255; const ag = g / count / 255; const ab = b / count / 255; let best = { temp: 0, tint: 0, err: Number.POSITIVE_INFINITY }; for (let temp = -100; temp <= 100; temp += 4) { for (let tint = -100; tint <= 100; tint += 4) { const t = temp / 100; const ti = tint / 100; // Same channel multipliers the grade shader applies. const mr = 1 + 0.3 * t + 0.06 * ti; const mg = 1 - 0.16 * ti; const mb = 1 - 0.26 * t + 0.06 * ti; const rr = ar * mr; const gg = ag * mg; const bb = ab * mb; const mean = (rr + gg + bb) / 3; const err = (rr - mean) ** 2 + (gg - mean) ** 2 + (bb - mean) ** 2; if (err < best.err) best = { temp, tint, err }; } } return { temp: best.temp, tint: best.tint }; } catch { return { temp: 0, tint: 0 }; } }