Give every mask its own tone curve.

Local masks could only shift tone globally, so a burnt sky or a backlit face had
to fight the frame-wide sliders. Each mask now carries an RGB point curve,
applied to the masked pixels before the mask blend. All eight curves ride in one
256×8 LUT texture, so the grade shader needs a single extra sampler rather than
eight. Masks saved before this exist migrate to an identity curve, so old edits
keep their look; the render check fails if the curve stops reaching the grade,
and a unit test pins the LUT's identity, per-row isolation and monotonicity.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
2026-11-28 15:42:24 +00:00
parent 00e6fee9bd
commit acf582251f
6 changed files with 159 additions and 2 deletions
+19
View File
@@ -131,6 +131,7 @@ export function defaultLocalLayer(kind: LocalMaskKind, index: number): LocalLaye
linear: { x1: 0.5, y1: 0.2, x2: 0.5, y2: 0.8 },
radial: { cx: 0.5, cy: 0.5, rx: 0.25, ry: 0.3 },
range: { enabled: false, lumaMin: 0, lumaMax: 100, hue: 30, hueRange: 180, satMin: 0 },
curve: identityCurve(),
adjust: defaultLocalAdjust(),
};
}
@@ -269,6 +270,23 @@ function rasterMask(input: unknown): RasterMask | undefined {
return { data: raw.data, width, height };
}
/**
* Per-mask curve points. Masks saved before the field existed have none, and
* they must come back as the identity rather than leaving the shader LUT row
* undefined.
*/
function localCurve(input: unknown): CurvePoint[] {
if (!Array.isArray(input)) return identityCurve();
const points: CurvePoint[] = [];
for (const raw of input.slice(0, 16)) {
if (!raw || typeof raw !== 'object') continue;
const point = raw as Partial<CurvePoint>;
points.push({ x: clamp01(num(point.x, 0, 0, 1)), y: clamp01(num(point.y, 0, 0, 1)) });
}
points.sort((a, b) => a.x - b.x);
return points.length >= 2 ? points : identityCurve();
}
function localLayers(input: unknown): LocalLayer[] {
if (!Array.isArray(input)) return [];
const out: LocalLayer[] = [];
@@ -311,6 +329,7 @@ function localLayers(input: unknown): LocalLayer[] {
hueRange: num(range.hueRange, 180, 0, 180),
satMin: num(range.satMin, 0, 0, 100),
},
curve: localCurve(layer.curve),
adjust: {
exposure: num(adjust.exposure, 0, -4, 4),
contrast: num(adjust.contrast, 0, -100, 100),
+40
View File
@@ -0,0 +1,40 @@
import { describe, expect, it } from 'vitest';
import { buildLocalCurveLUT } from '../gl/renderer';
/** Bytes of one LUT row (red channel; R/G/B are written identically). */
function row(data: Uint8Array, index: number) {
return Array.from(data.slice(index * 256 * 4, (index + 1) * 256 * 4)).filter((_, i) => i % 4 === 0);
}
describe('mask curve LUT', () => {
it('is the identity for a mask without a curve', () => {
const values = row(buildLocalCurveLUT([{}]), 0);
expect(values[0]).toBe(0);
expect(values[64]).toBeGreaterThan(60);
expect(values[64]).toBeLessThan(69);
expect(values[255]).toBe(255);
});
it('lifts only the row belonging to that mask', () => {
const data = buildLocalCurveLUT([{}, { curve: [{ x: 0, y: 0.25 }, { x: 1, y: 1 }] }]);
expect(row(data, 0)[64]).toBeLessThan(80);
expect(row(data, 1)[64]).toBeGreaterThan(100);
});
it('keeps an inverted curve monotonic instead of folding', () => {
// A curve that dips and rises again would posterise the masked area.
const values = row(
buildLocalCurveLUT([
{ curve: [{ x: 0, y: 0 }, { x: 0.3, y: 0.8 }, { x: 0.7, y: 0.2 }, { x: 1, y: 1 }] },
]),
0,
);
for (let i = 1; i < values.length; i++) expect(values[i]).toBeGreaterThanOrEqual(values[i - 1]);
});
it('fills all eight rows so a ninth mask cannot read stale data', () => {
const data = buildLocalCurveLUT([{ curve: [{ x: 0, y: 0 }, { x: 1, y: 0.5 }] }]);
expect(data.length).toBe(256 * 8 * 4);
expect(row(data, 7)[255]).toBe(255);
});
});