Skip to content

Commit 48cfee7

Browse files
Add auto correction, looks and healing source search
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6c2d626 commit 48cfee7

7 files changed

Lines changed: 470 additions & 0 deletions

File tree

‎src/edit/analysis.js‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Image statistics computed on a small copy of the photo (any RGBA buffer).
2+
3+
const srgbToLinear = (v) => (v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4);
4+
5+
export function imageStats({ data, width, height }, { maxSamples = 250_000 } = {}) {
6+
const pixels = width * height;
7+
const step = Math.max(1, Math.floor(pixels / maxSamples));
8+
const histogram = new Uint32Array(256);
9+
const neutral = [0, 0, 0];
10+
let neutralCount = 0;
11+
let chromaSum = 0;
12+
let count = 0;
13+
14+
for (let p = 0; p < pixels; p += step) {
15+
const i = p * 4;
16+
const r = data[i] / 255;
17+
const g = data[i + 1] / 255;
18+
const b = data[i + 2] / 255;
19+
const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b;
20+
histogram[Math.min(255, Math.round(luma * 255))]++;
21+
const max = Math.max(r, g, b);
22+
const min = Math.min(r, g, b);
23+
chromaSum += max - min;
24+
count++;
25+
// Pixels that are probably grey in reality drive the white balance.
26+
if (luma > 0.12 && max < 0.97 && (max - min) / max < 0.35) {
27+
neutral[0] += srgbToLinear(r);
28+
neutral[1] += srgbToLinear(g);
29+
neutral[2] += srgbToLinear(b);
30+
neutralCount++;
31+
}
32+
}
33+
34+
return {
35+
histogram,
36+
count,
37+
meanChroma: count ? chromaSum / count : 0,
38+
neutral: neutralCount / count > 0.02 ? neutral.map((v) => v / neutralCount) : null,
39+
};
40+
}
41+
42+
export function percentile(histogram, count, fraction) {
43+
const goal = count * fraction;
44+
let acc = 0;
45+
for (let i = 0; i < histogram.length; i++) {
46+
acc += histogram[i];
47+
if (acc >= goal) return i / 255;
48+
}
49+
return 1;
50+
}
51+
52+
export function fractionBetween(histogram, count, from, to) {
53+
let acc = 0;
54+
for (let i = Math.round(from * 255); i <= Math.round(to * 255); i++) acc += histogram[i];
55+
return count ? acc / count : 0;
56+
}

‎src/edit/auto.js‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Automatic correction from image statistics: levels, midtone brightness,
2+
// shadow/highlight balance and white balance. The result is deliberately
3+
// moderate, it is a starting point rather than a final grade.
4+
import { fractionBetween, percentile } from './analysis.js';
5+
6+
const clamp = (v, min, max) => Math.min(max, Math.max(min, v));
7+
const ui = (v) => Math.round(v * 100);
8+
9+
// Inverse of whiteBalanceGains() in render/uniforms.js for a measured grey.
10+
export function whiteBalanceFor([r, g, b]) {
11+
const q = b / r;
12+
const temperature = ((q - 1) / (q + 1)) / 0.3;
13+
const target = (1 / g) / ((1 / r + 1 / b) / 2);
14+
const tint = (1 - target) / 0.2;
15+
return { temperature, tint };
16+
}
17+
18+
export function autoTune(stats) {
19+
const { histogram, count } = stats;
20+
const low = percentile(histogram, count, 0.005);
21+
const high = percentile(histogram, count, 0.995);
22+
const median = percentile(histogram, count, 0.5);
23+
24+
// Stretch levels, leaving some room at both ends: low-key and high-key
25+
// photos should keep their character.
26+
const blacks = -clamp((low * 0.8) / 0.12, 0, 0.6);
27+
const whites = clamp(((1 - high) * 0.7) / 0.2, 0, 0.6);
28+
const black = -blacks * 0.12;
29+
const white = 1 - whites * 0.2;
30+
const stretched = clamp((median - black) / (white - black), 0.02, 0.98);
31+
32+
// Move the median part of the way to mid grey (brightness maps to a
33+
// midtone gamma of 2^(-b·0.8)). Bright scenes are often bright on purpose,
34+
// so darkening is limited more than brightening.
35+
const gamma = Math.log(0.5) / Math.log(stretched);
36+
const brightness = clamp((-Math.log2(gamma) / 0.8) * 0.5, -0.3, 0.35);
37+
38+
const deepShadows = fractionBetween(histogram, count, 0, 0.1);
39+
const clipped = fractionBetween(histogram, count, 0.95, 1);
40+
const shadows = clamp((deepShadows - 0.08) * 2.5, 0, 0.45);
41+
const highlights = -clamp((clipped - 0.03) * 4, 0, 0.5);
42+
43+
const spread = high - low;
44+
const contrast = spread < 0.7 ? clamp((0.7 - spread) * 0.6, 0, 0.25) : 0;
45+
const vibrance = stats.meanChroma < 0.18 ? 0.2 : 0.1;
46+
47+
let temperature = 0;
48+
let tint = 0;
49+
if (stats.neutral) {
50+
const wb = whiteBalanceFor(stats.neutral);
51+
// Only correct part of the cast, the ambient mood is often wanted.
52+
temperature = clamp(wb.temperature * 0.6, -0.3, 0.3);
53+
tint = clamp(wb.tint * 0.6, -0.2, 0.2);
54+
}
55+
56+
return {
57+
brightness: ui(brightness),
58+
contrast: ui(contrast),
59+
highlights: ui(highlights),
60+
shadows: ui(shadows),
61+
whites: ui(whites),
62+
blacks: ui(blacks),
63+
vibrance: ui(vibrance),
64+
temperature: ui(temperature),
65+
tint: ui(tint),
66+
};
67+
}

‎src/edit/heal.js‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Picks the patch used to repair a spot: the candidate whose surroundings look
2+
// the most like the surroundings of the blemish, without reaching outside the
3+
// image. Works on a small RGBA copy of the photo.
4+
5+
const RING_SAMPLES = 24;
6+
7+
function sampler({ data, width, height }) {
8+
return (x, y) => {
9+
const px = Math.min(width - 1, Math.max(0, Math.round(x)));
10+
const py = Math.min(height - 1, Math.max(0, Math.round(y)));
11+
const i = (py * width + px) * 4;
12+
return [data[i], data[i + 1], data[i + 2]];
13+
};
14+
}
15+
16+
// x, y and the returned position are normalised; r is relative to the long side.
17+
export function findHealSource(image, x, y, r) {
18+
const { width, height } = image;
19+
const long = Math.max(width, height);
20+
const radius = Math.max(1, r * long);
21+
const cx = x * width;
22+
const cy = y * height;
23+
const at = sampler(image);
24+
25+
const offsets = [];
26+
for (let ring = 0; ring < 2; ring++) {
27+
const dist = radius * (ring ? 1.6 : 1.15);
28+
for (let i = 0; i < RING_SAMPLES; i++) {
29+
const a = (i / RING_SAMPLES) * Math.PI * 2 + ring * 0.13;
30+
offsets.push([Math.cos(a) * dist, Math.sin(a) * dist]);
31+
}
32+
}
33+
const inner = [];
34+
for (let i = 0; i < 12; i++) {
35+
const a = (i / 12) * Math.PI * 2;
36+
inner.push([Math.cos(a) * radius * 0.5, Math.sin(a) * radius * 0.5]);
37+
}
38+
inner.push([0, 0]);
39+
40+
const reference = offsets.map(([ox, oy]) => at(cx + ox, cy + oy));
41+
const mean = reference.reduce((acc, c) => acc.map((v, k) => v + c[k] / reference.length), [0, 0, 0]);
42+
43+
let best = null;
44+
for (const factor of [2.2, 3, 4]) {
45+
for (let i = 0; i < 16; i++) {
46+
const a = (i / 16) * Math.PI * 2;
47+
const sx = cx + Math.cos(a) * radius * factor;
48+
const sy = cy + Math.sin(a) * radius * factor;
49+
const margin = radius * 1.7;
50+
if (sx < margin || sy < margin || sx > width - margin || sy > height - margin) continue;
51+
52+
let score = 0;
53+
offsets.forEach(([ox, oy], k) => {
54+
const c = at(sx + ox, sy + oy);
55+
const ref = reference[k];
56+
score += (c[0] - ref[0]) ** 2 + (c[1] - ref[1]) ** 2 + (c[2] - ref[2]) ** 2;
57+
});
58+
// The patch itself should look like the clean skin/sky around the spot.
59+
for (const [ox, oy] of inner) {
60+
const c = at(sx + ox, sy + oy);
61+
score += 2 * ((c[0] - mean[0]) ** 2 + (c[1] - mean[1]) ** 2 + (c[2] - mean[2]) ** 2);
62+
}
63+
score *= 1 + (factor - 2.2) * 0.15;
64+
if (!best || score < best.score) best = { score, sx, sy };
65+
}
66+
}
67+
68+
if (!best) {
69+
// Spot too close to the borders: fall back to the side with the most room.
70+
const sx = cx < width / 2 ? cx + radius * 2.5 : cx - radius * 2.5;
71+
best = { sx, sy: cy };
72+
}
73+
return { sx: best.sx / width, sy: best.sy / height };
74+
}

‎src/edit/presets.js‎

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// Looks: ready-made global settings. Applying one resets the global sections to
2+
// their defaults and blends the look in with the requested strength; geometry
3+
// and local retouching are left alone.
4+
import { DEFAULT_EDIT, GLOBAL_SECTIONS } from '../state/defaults.js';
5+
6+
export const LOOKS = [
7+
{ id: 'original', label: 'Original', values: {} },
8+
{
9+
id: 'portrait',
10+
label: 'Portrait',
11+
values: {
12+
tune: { brightness: 8, shadows: 20, highlights: -12, temperature: 8, vibrance: 15, saturation: -5 },
13+
details: { structure: -18 },
14+
effects: { glow: 12, vignette: -15 },
15+
},
16+
},
17+
{
18+
id: 'landscape',
19+
label: 'Paysage',
20+
values: {
21+
tune: { contrast: 15, highlights: -30, shadows: 25, vibrance: 35 },
22+
details: { structure: 30, sharpen: 25 },
23+
hdr: { strength: 20, detail: 40, spread: 70 },
24+
},
25+
},
26+
{
27+
id: 'bright',
28+
label: 'Lumineux',
29+
values: {
30+
tune: { brightness: 25, contrast: -10, highlights: -25, shadows: 30, temperature: 6, vibrance: 15 },
31+
},
32+
},
33+
{
34+
id: 'hdr-natural',
35+
label: 'HDR naturel',
36+
values: {
37+
tune: { highlights: -20, shadows: 15 },
38+
hdr: { strength: 45, detail: 40, spread: 65, saturation: 10 },
39+
},
40+
},
41+
{
42+
id: 'hdr-strong',
43+
label: 'HDR intense',
44+
values: {
45+
details: { structure: 25 },
46+
hdr: { strength: 85, detail: 80, spread: 35, saturation: 30 },
47+
},
48+
},
49+
{
50+
id: 'drama',
51+
label: 'Drame',
52+
values: {
53+
tune: { contrast: 20, saturation: -35 },
54+
hdr: { strength: 60, detail: 90, spread: 20 },
55+
effects: { vignette: -35 },
56+
},
57+
},
58+
{
59+
id: 'matte',
60+
label: 'Mat',
61+
values: {
62+
tune: { contrast: -15, blacks: 35, saturation: -15 },
63+
split: { shadowHue: 220, shadowAmount: 18, highlightHue: 40, highlightAmount: 12 },
64+
},
65+
},
66+
{
67+
id: 'film',
68+
label: 'Film',
69+
values: {
70+
tune: { contrast: 12, highlights: -20, blacks: 15, saturation: -10, temperature: 5 },
71+
split: { shadowHue: 190, shadowAmount: 12, highlightHue: 45, highlightAmount: 10 },
72+
effects: { grain: 25, grainSize: 35 },
73+
},
74+
},
75+
{
76+
id: 'vintage',
77+
label: 'Vintage',
78+
values: {
79+
tune: { contrast: -10, blacks: 25, saturation: -25, temperature: 25 },
80+
split: { shadowHue: 200, shadowAmount: 20, highlightHue: 45, highlightAmount: 25 },
81+
effects: { vignette: -35, grain: 30 },
82+
},
83+
},
84+
{
85+
id: 'teal-orange',
86+
label: 'Ciné',
87+
values: {
88+
tune: { contrast: 15, vibrance: 15, highlights: -15 },
89+
split: { shadowHue: 195, shadowAmount: 45, highlightHue: 30, highlightAmount: 40, balance: 10 },
90+
},
91+
},
92+
{
93+
id: 'cold',
94+
label: 'Froid',
95+
values: {
96+
tune: { contrast: 10, highlights: -10, temperature: -25, tint: 5, saturation: -10 },
97+
},
98+
},
99+
{
100+
id: 'bw',
101+
label: 'N&B',
102+
values: {
103+
tune: { contrast: 20 },
104+
bw: { amount: 100, filter: 'neutral' },
105+
},
106+
},
107+
{
108+
id: 'bw-contrast',
109+
label: 'N&B contrasté',
110+
values: {
111+
tune: { contrast: 45, blacks: -20 },
112+
details: { structure: 25 },
113+
bw: { amount: 100, filter: 'red' },
114+
effects: { vignette: -20, grain: 15 },
115+
},
116+
},
117+
{
118+
id: 'noir',
119+
label: 'Noir',
120+
values: {
121+
tune: { contrast: 30 },
122+
bw: { amount: 100, filter: 'yellow' },
123+
effects: { glow: 25, vignette: -50, grain: 40 },
124+
},
125+
},
126+
{
127+
id: 'sepia',
128+
label: 'Sépia',
129+
values: {
130+
tune: { contrast: 10 },
131+
bw: { amount: 100, filter: 'orange' },
132+
split: { shadowHue: 30, shadowAmount: 40, highlightHue: 45, highlightAmount: 30 },
133+
},
134+
},
135+
];
136+
137+
// Hues and enumerations are taken as is; numbers are blended from the default.
138+
function blend(defaults, values, amount) {
139+
const out = { ...defaults };
140+
for (const [key, value] of Object.entries(values)) {
141+
const base = defaults[key];
142+
if (typeof value === 'number' && typeof base === 'number' && !/Hue$/.test(key)) {
143+
out[key] = Math.round(base + (value - base) * amount);
144+
} else if (value && typeof value === 'object' && !Array.isArray(value)) {
145+
out[key] = blend(base ?? {}, value, amount);
146+
} else {
147+
out[key] = value;
148+
}
149+
}
150+
return out;
151+
}
152+
153+
export function applyLook(edit, values, amount = 1) {
154+
const next = { ...edit };
155+
for (const section of GLOBAL_SECTIONS) {
156+
next[section] = values[section]
157+
? blend(DEFAULT_EDIT[section], values[section], amount)
158+
: DEFAULT_EDIT[section];
159+
}
160+
return next;
161+
}

0 commit comments

Comments
 (0)