Skip to content

Commit 6c2d626

Browse files
Add WebGL2 rendering pipeline
Global adjustments, HDR tone mapping, local adjustments and spot healing run as full-screen passes on half-float targets. Geometry (crop, quarter turns, mirror, straighten) is applied in the final pass so local edits stay in source coordinates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 052c186 commit 6c2d626

10 files changed

Lines changed: 1625 additions & 0 deletions

File tree

‎src/edit/curves.js‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Tone curves: monotone cubic interpolation (Fritsch–Carlson) so that the curve
2+
// never overshoots between control points, which keeps gradients clean.
3+
4+
export const CURVE_CHANNELS = ['rgb', 'red', 'green', 'blue'];
5+
6+
export function isIdentity(points) {
7+
return (
8+
points.length === 2 &&
9+
points[0][0] === 0 && points[0][1] === 0 &&
10+
points[1][0] === 1 && points[1][1] === 1
11+
);
12+
}
13+
14+
function prepare(points) {
15+
const sorted = points
16+
.map(([x, y]) => [Math.min(1, Math.max(0, x)), Math.min(1, Math.max(0, y))])
17+
.sort((a, b) => a[0] - b[0]);
18+
const unique = [];
19+
for (const point of sorted) {
20+
const last = unique[unique.length - 1];
21+
if (last && point[0] - last[0] < 1e-6) last[1] = point[1];
22+
else unique.push(point);
23+
}
24+
return unique;
25+
}
26+
27+
function tangents(xs, ys) {
28+
const n = xs.length;
29+
const delta = [];
30+
for (let i = 0; i < n - 1; i++) delta.push((ys[i + 1] - ys[i]) / (xs[i + 1] - xs[i]));
31+
32+
const m = new Array(n);
33+
m[0] = delta[0];
34+
m[n - 1] = delta[n - 2];
35+
for (let i = 1; i < n - 1; i++) {
36+
m[i] = delta[i - 1] * delta[i] <= 0 ? 0 : (delta[i - 1] + delta[i]) / 2;
37+
}
38+
39+
for (let i = 0; i < n - 1; i++) {
40+
if (delta[i] === 0) {
41+
m[i] = 0;
42+
m[i + 1] = 0;
43+
continue;
44+
}
45+
const a = m[i] / delta[i];
46+
const b = m[i + 1] / delta[i];
47+
const s = a * a + b * b;
48+
if (s > 9) {
49+
const t = 3 / Math.sqrt(s);
50+
m[i] = t * a * delta[i];
51+
m[i + 1] = t * b * delta[i];
52+
}
53+
}
54+
return m;
55+
}
56+
57+
// Returns a function x → y valid on [0, 1].
58+
export function curveFunction(points) {
59+
const pts = prepare(points);
60+
if (pts.length === 0) return (x) => x;
61+
if (pts.length === 1) return () => pts[0][1];
62+
63+
const xs = pts.map((p) => p[0]);
64+
const ys = pts.map((p) => p[1]);
65+
const m = tangents(xs, ys);
66+
const last = xs.length - 1;
67+
68+
return (x) => {
69+
if (x <= xs[0]) return ys[0];
70+
if (x >= xs[last]) return ys[last];
71+
let i = 0;
72+
while (x > xs[i + 1]) i++;
73+
const h = xs[i + 1] - xs[i];
74+
const t = (x - xs[i]) / h;
75+
const t2 = t * t;
76+
const t3 = t2 * t;
77+
const y =
78+
(2 * t3 - 3 * t2 + 1) * ys[i] +
79+
(t3 - 2 * t2 + t) * h * m[i] +
80+
(-2 * t3 + 3 * t2) * ys[i + 1] +
81+
(t3 - t2) * h * m[i + 1];
82+
return Math.min(1, Math.max(0, y));
83+
};
84+
}
85+
86+
export function sampleCurve(points, size = 256) {
87+
const fn = curveFunction(points);
88+
const out = new Float32Array(size);
89+
for (let i = 0; i < size; i++) out[i] = fn(i / (size - 1));
90+
return out;
91+
}
92+
93+
// Packs the four curves into a 256×1 RGBA lookup table. Each colour channel
94+
// goes through the master curve first, then through its own curve.
95+
export function buildCurveLut(curves, size = 256) {
96+
const master = curveFunction(curves.rgb);
97+
const channels = [curves.red, curves.green, curves.blue].map(curveFunction);
98+
const lut = new Uint8Array(size * 4);
99+
for (let i = 0; i < size; i++) {
100+
const v = master(i / (size - 1));
101+
for (let c = 0; c < 3; c++) lut[i * 4 + c] = Math.round(channels[c](v) * 255);
102+
lut[i * 4 + 3] = 255;
103+
}
104+
return lut;
105+
}
106+
107+
export function curvesAreIdentity(curves) {
108+
return CURVE_CHANNELS.every((key) => isIdentity(curves[key]));
109+
}
110+
111+
export const CURVE_PRESETS = [
112+
{ id: 'linear', label: 'Linéaire', points: [[0, 0], [1, 1]] },
113+
{ id: 'soft', label: 'Contraste doux', points: [[0, 0], [0.25, 0.21], [0.75, 0.8], [1, 1]] },
114+
{ id: 'strong', label: 'Contraste fort', points: [[0, 0], [0.25, 0.16], [0.75, 0.86], [1, 1]] },
115+
{ id: 'matte', label: 'Mat', points: [[0, 0.1], [0.25, 0.24], [0.75, 0.78], [1, 0.95]] },
116+
{ id: 'brighten', label: 'Éclaircir', points: [[0, 0], [0.45, 0.58], [1, 1]] },
117+
{ id: 'darken', label: 'Assombrir', points: [[0, 0], [0.55, 0.42], [1, 1]] },
118+
];

‎src/render/geometry.js‎

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// Geometry of the output frame: quarter turns, mirror, straighten angle and crop.
2+
//
3+
// Affine transforms are stored as [a, b, c, d, e, f]:
4+
// x' = a·x + c·y + e
5+
// y' = b·x + d·y + f
6+
//
7+
// The pipeline works in source space; the final pass maps every output pixel
8+
// back to the source with `toSource`. Normalised coordinates have their origin
9+
// at the top-left corner, y pointing down.
10+
11+
export const IDENTITY = Object.freeze([1, 0, 0, 1, 0, 0]);
12+
const MIRROR = Object.freeze([-1, 0, 0, 1, 0, 0]);
13+
const QUARTER_TURNS = [
14+
[1, 0, 0, 1, 0, 0],
15+
[0, 1, -1, 0, 0, 0],
16+
[-1, 0, 0, -1, 0, 0],
17+
[0, -1, 1, 0, 0, 0],
18+
];
19+
20+
export function multiply(m, n) {
21+
const [a1, b1, c1, d1, e1, f1] = m;
22+
const [a2, b2, c2, d2, e2, f2] = n;
23+
return [
24+
a1 * a2 + c1 * b2,
25+
b1 * a2 + d1 * b2,
26+
a1 * c2 + c1 * d2,
27+
b1 * c2 + d1 * d2,
28+
a1 * e2 + c1 * f2 + e1,
29+
b1 * e2 + d1 * f2 + f1,
30+
];
31+
}
32+
33+
export function invert([a, b, c, d, e, f]) {
34+
const det = a * d - b * c;
35+
return [d / det, -b / det, -c / det, a / det, (c * f - d * e) / det, (b * e - a * f) / det];
36+
}
37+
38+
export function apply([a, b, c, d, e, f], x, y) {
39+
return [a * x + c * y + e, b * x + d * y + f];
40+
}
41+
42+
export function rotation(radians) {
43+
const cos = Math.cos(radians);
44+
const sin = Math.sin(radians);
45+
return [cos, sin, -sin, cos, 0, 0];
46+
}
47+
48+
export function toMat3([a, b, c, d, e, f]) {
49+
return new Float32Array([a, b, 0, c, d, 0, e, f, 1]);
50+
}
51+
52+
export function frameSize(width, height, rotate) {
53+
return rotate % 2 ? [height, width] : [width, height];
54+
}
55+
56+
// Zoom needed so that the straightened image still covers the whole frame.
57+
export function straightenScale(frameWidth, frameHeight, radians) {
58+
const cos = Math.abs(Math.cos(radians));
59+
const sin = Math.abs(Math.sin(radians));
60+
return Math.max(cos + (frameHeight / frameWidth) * sin, (frameWidth / frameHeight) * sin + cos);
61+
}
62+
63+
export function outputGeometry(crop, width, height, { frame = false } = {}) {
64+
const [frameWidth, frameHeight] = frameSize(width, height, crop.rotate);
65+
const radians = (crop.angle * Math.PI) / 180;
66+
const scale = straightenScale(frameWidth, frameHeight, radians);
67+
const rect = frame ? { x: 0, y: 0, w: 1, h: 1 } : crop;
68+
69+
const steps = [
70+
[1 / width, 0, 0, 1 / height, 0.5, 0.5],
71+
multiply(crop.flip ? MIRROR : IDENTITY, QUARTER_TURNS[(4 - crop.rotate) % 4]),
72+
multiply([1 / scale, 0, 0, 1 / scale, 0, 0], rotation(-radians)),
73+
[frameWidth, 0, 0, frameHeight, -frameWidth / 2, -frameHeight / 2],
74+
[rect.w, 0, 0, rect.h, rect.x, rect.y],
75+
];
76+
const toSource = steps.reduce(multiply);
77+
78+
return {
79+
width: rect.w * frameWidth,
80+
height: rect.h * frameHeight,
81+
frameWidth,
82+
frameHeight,
83+
scale,
84+
toSource,
85+
fromSource: invert(toSource),
86+
};
87+
}
88+
89+
export function rotateCrop(crop, direction) {
90+
const { x, y, w, h } = crop;
91+
const rect = direction > 0
92+
? { x: 1 - y - h, y: x, w: h, h: w }
93+
: { x: y, y: 1 - x - w, w: h, h: w };
94+
return {
95+
...crop,
96+
...rect,
97+
rotate: (crop.rotate + (direction > 0 ? 1 : 3)) % 4,
98+
aspect: crop.aspect ? 1 / crop.aspect : null,
99+
};
100+
}
101+
102+
export function mirrorCrop(crop) {
103+
return {
104+
...crop,
105+
x: 1 - crop.x - crop.w,
106+
rotate: (4 - crop.rotate) % 4,
107+
flip: !crop.flip,
108+
angle: crop.angle ? -crop.angle : 0,
109+
};
110+
}
111+
112+
// Largest centred rectangle with the given pixel aspect ratio.
113+
export function fitAspect(crop, ratio, frameWidth, frameHeight) {
114+
if (!ratio) return { ...crop, aspect: null };
115+
let w = 1;
116+
let h = 1;
117+
if (frameWidth / frameHeight > ratio) w = (ratio * frameHeight) / frameWidth;
118+
else h = frameWidth / (ratio * frameHeight);
119+
return { ...crop, x: (1 - w) / 2, y: (1 - h) / 2, w, h, aspect: ratio };
120+
}
121+
122+
const clamp = (v, min, max) => Math.min(max, Math.max(min, v));
123+
124+
export function moveCrop(rect, dx, dy) {
125+
return {
126+
...rect,
127+
x: clamp(rect.x + dx, 0, 1 - rect.w),
128+
y: clamp(rect.y + dy, 0, 1 - rect.h),
129+
};
130+
}
131+
132+
// `handle` combines the letters n, s, e and w ("nw", "e"…). With a ratio the
133+
// opposite corner stays anchored and the pointer is projected on the diagonal.
134+
export function resizeCrop(rect, handle, px, py, options = {}) {
135+
const { ratio = null, frameWidth = 1, frameHeight = 1, min = 0.04 } = options;
136+
let x0 = rect.x;
137+
let y0 = rect.y;
138+
let x1 = rect.x + rect.w;
139+
let y1 = rect.y + rect.h;
140+
141+
if (!ratio) {
142+
if (handle.includes('w')) x0 = clamp(px, 0, x1 - min);
143+
if (handle.includes('e')) x1 = clamp(px, x0 + min, 1);
144+
if (handle.includes('n')) y0 = clamp(py, 0, y1 - min);
145+
if (handle.includes('s')) y1 = clamp(py, y0 + min, 1);
146+
return { ...rect, x: x0, y: y0, w: x1 - x0, h: y1 - y0 };
147+
}
148+
149+
const r = (ratio * frameHeight) / frameWidth;
150+
const west = handle.includes('w');
151+
const north = handle.includes('n');
152+
const anchorX = west ? x1 : x0;
153+
const anchorY = north ? y1 : y0;
154+
const w = Math.abs(px - anchorX);
155+
const h = Math.abs(py - anchorY);
156+
let t = (w * r + h) / (r * r + 1);
157+
const maxW = west ? anchorX : 1 - anchorX;
158+
const maxH = north ? anchorY : 1 - anchorY;
159+
t = clamp(t, min, Math.min(maxH, maxW / r));
160+
const nw = t * r;
161+
const nh = t;
162+
return {
163+
...rect,
164+
x: west ? anchorX - nw : anchorX,
165+
y: north ? anchorY - nh : anchorY,
166+
w: nw,
167+
h: nh,
168+
};
169+
}

0 commit comments

Comments
 (0)