Group local mask shapes into editable components.

A local adjustment is now a list of shapes composited in order (add, subtract,
intersect) instead of a single shape, so dodging and burning can be built from
several brushes, gradients and auto masks without spending layer channels.
Existing edits migrate on load: the flat layer fields become components[0],
keeping geometry, invert, feather and the layer's blend mode unchanged. The
panel gains a shape list with active-shape editing, and the render gate now
asserts a subtract shape clears the group while an intersect keeps it.
This commit is contained in:
2026-11-28 16:09:14 +00:00
parent acf582251f
commit b884616889
10 changed files with 666 additions and 183 deletions
+65 -26
View File
@@ -6,6 +6,7 @@ import type {
Edits,
Flag,
HealSpot,
LocalComponent,
LocalLayer,
LocalMaskKind,
LocalPoint,
@@ -99,6 +100,9 @@ export const MAX_HEAL_SPOTS = 24;
/** Eight masks are packed into two RGBA textures. */
export const MAX_LOCAL_LAYERS = 8;
/** More shapes than this in one group stops being quicker than a second layer. */
export const MAX_LOCAL_COMPONENTS = 8;
export function defaultLocalAdjust(): LocalLayer['adjust'] {
return {
exposure: 0,
@@ -119,10 +123,9 @@ export function defaultLocalAdjust(): LocalLayer['adjust'] {
};
}
export function defaultLocalLayer(kind: LocalMaskKind, index: number): LocalLayer {
export function defaultLocalComponent(kind: LocalMaskKind): LocalComponent {
return {
id: `local-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 7)}`,
name: `Local ${index + 1}`,
id: `shape-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 7)}`,
kind,
mode: 'add',
inverted: false,
@@ -130,6 +133,15 @@ export function defaultLocalLayer(kind: LocalMaskKind, index: number): LocalLaye
brush: { strokes: [] },
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 },
};
}
export function defaultLocalLayer(kind: LocalMaskKind, index: number): LocalLayer {
return {
id: `local-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 7)}`,
name: `Local ${index + 1}`,
mode: 'add',
components: [defaultLocalComponent(kind)],
range: { enabled: false, lumaMin: 0, lumaMax: 100, hue: 30, hueRange: 180, satMin: 0 },
curve: identityCurve(),
adjust: defaultLocalAdjust(),
@@ -287,40 +299,67 @@ function localCurve(input: unknown): CurvePoint[] {
return points.length >= 2 ? points : identityCurve();
}
/**
* One shape from stored edits. It also reads the pre-group flat layout, where
* the layer itself carried kind/feather/brush/...: that is how every mask saved
* before mask groups existed migrates into `components[0]`.
*/
function localComponent(input: unknown, index: number): LocalComponent | null {
if (!input || typeof input !== 'object') return null;
const shape = input as Partial<LocalComponent> & { kind?: unknown };
const kind: LocalMaskKind =
shape.kind === 'linear' || shape.kind === 'radial' || shape.kind === 'raster' ? shape.kind : 'brush';
const linear = (shape.linear ?? {}) as Partial<LocalComponent['linear']>;
const radial = (shape.radial ?? {}) as Partial<LocalComponent['radial']>;
const raster = kind === 'raster' ? rasterMask(shape.raster) : undefined;
// A raster shape without its coverage would composite an empty hole, so it
// falls back to a brush shape that can at least be painted over.
const resolved: LocalMaskKind = kind === 'raster' && !raster ? 'brush' : kind;
return {
id: typeof shape.id === 'string' && shape.id ? shape.id.slice(0, 60) : `shape-${index}`,
kind: resolved,
mode: shape.mode === 'subtract' || shape.mode === 'intersect' ? shape.mode : 'add',
inverted: !!shape.inverted,
feather: num(shape.feather, 50, 0, 100),
brush: { strokes: localStrokes(shape.brush?.strokes) },
raster: resolved === 'raster' ? raster : undefined,
linear: {
x1: clamp01(num(linear.x1, 0.5, 0, 1)),
y1: clamp01(num(linear.y1, 0.2, 0, 1)),
x2: clamp01(num(linear.x2, 0.5, 0, 1)),
y2: clamp01(num(linear.y2, 0.8, 0, 1)),
},
radial: {
cx: clamp01(num(radial.cx, 0.5, 0, 1)),
cy: clamp01(num(radial.cy, 0.5, 0, 1)),
rx: num(radial.rx, 0.25, 0.01, 1.5),
ry: num(radial.ry, 0.3, 0.01, 1.5),
},
};
}
function localLayers(input: unknown): LocalLayer[] {
if (!Array.isArray(input)) return [];
const out: LocalLayer[] = [];
for (const raw of input.slice(0, MAX_LOCAL_LAYERS)) {
if (!raw || typeof raw !== 'object') continue;
const layer = raw as Partial<LocalLayer>;
const kind: LocalMaskKind =
layer.kind === 'linear' || layer.kind === 'radial' || layer.kind === 'raster' ? layer.kind : 'brush';
const adjust = (layer.adjust ?? {}) as Partial<LocalLayer['adjust']>;
const range = (layer.range ?? {}) as Partial<LocalLayer['range']>;
const linear = (layer.linear ?? {}) as Partial<LocalLayer['linear']>;
const radial = (layer.radial ?? {}) as Partial<LocalLayer['radial']>;
const stored = Array.isArray(layer.components) && layer.components.length ? layer.components : [layer];
const components: LocalComponent[] = [];
for (const entry of stored.slice(0, MAX_LOCAL_COMPONENTS)) {
const component = localComponent(entry, components.length);
if (component) components.push(component);
}
// A layer must keep at least one shape, or it would adjust nothing with no
// way to tell why from the panel.
if (!components.length) components.push(defaultLocalComponent('brush'));
out.push({
id: typeof layer.id === 'string' && layer.id ? layer.id : `local-${out.length}`,
name: typeof layer.name === 'string' && layer.name.trim() ? layer.name.slice(0, 40) : `Local ${out.length + 1}`,
kind,
mode:
layer.mode === 'subtract' || layer.mode === 'intersect' ? layer.mode : 'add',
inverted: !!layer.inverted,
feather: num(layer.feather, 50, 0, 100),
brush: { strokes: localStrokes(layer.brush?.strokes) },
raster: kind === 'raster' ? rasterMask(layer.raster) : undefined,
linear: {
x1: clamp01(num(linear.x1, 0.5, 0, 1)),
y1: clamp01(num(linear.y1, 0.2, 0, 1)),
x2: clamp01(num(linear.x2, 0.5, 0, 1)),
y2: clamp01(num(linear.y2, 0.8, 0, 1)),
},
radial: {
cx: clamp01(num(radial.cx, 0.5, 0, 1)),
cy: clamp01(num(radial.cy, 0.5, 0, 1)),
rx: num(radial.rx, 0.25, 0.01, 1.5),
ry: num(radial.ry, 0.3, 0.01, 1.5),
},
mode: layer.mode === 'subtract' || layer.mode === 'intersect' ? layer.mode : 'add',
components,
range: {
enabled: !!range.enabled,
lumaMin: num(range.lumaMin, 0, 0, 100),
+106
View File
@@ -0,0 +1,106 @@
import { describe, expect, it } from 'vitest';
import { defaultLocalLayer, MAX_LOCAL_COMPONENTS, normalizeEdits } from './edits';
/** Stored edits as they look before mask groups: geometry on the layer itself. */
function legacyLayer() {
return {
id: 'local-1',
name: 'Sky',
kind: 'gradientish' as unknown as string,
mode: 'subtract',
inverted: true,
feather: 20,
brush: { strokes: [] },
linear: { x1: 0.1, y1: 0.2, x2: 0.3, y2: 0.9 },
radial: { cx: 0.4, cy: 0.6, rx: 0.2, ry: 0.3 },
range: { enabled: false, lumaMin: 0, lumaMax: 100, hue: 30, hueRange: 180, satMin: 0 },
curve: [
{ x: 0, y: 0 },
{ x: 1, y: 1 },
],
adjust: {},
};
}
describe('mask groups', () => {
it('migrates a flat layer into a single component without changing the mask', () => {
const edits = normalizeEdits({ local: [legacyLayer()] as never });
const layer = edits.local[0];
expect(layer.components).toHaveLength(1);
const shape = layer.components[0];
// An unknown kind is a brush, as it always was; geometry and the layer's
// combine mode move onto the shape, layer mode stays for blending layers.
expect(shape.kind).toBe('brush');
expect(shape.mode).toBe('subtract');
expect(shape.inverted).toBe(true);
expect(shape.feather).toBe(20);
expect(shape.linear.x2).toBe(0.3);
expect(shape.radial.ry).toBe(0.3);
expect(layer.mode).toBe('subtract');
expect(layer.name).toBe('Sky');
});
it('keeps a stored component list in order with its own modes', () => {
const edit = normalizeEdits({
local: [
{
id: 'local-a',
name: 'Group',
mode: 'add',
components: [
{ id: 's1', kind: 'radial', mode: 'add', radial: { cx: 0.5, cy: 0.5, rx: 0.4, ry: 0.4 } },
{ id: 's2', kind: 'linear', mode: 'subtract', feather: 0, linear: { x1: 0, y1: 0, x2: 1, y2: 1 } },
{ id: 's3', kind: 'brush', mode: 'intersect', brush: { strokes: [] } },
],
curve: [],
adjust: {},
},
],
} as never);
const shapes = edit.local[0].components;
expect(shapes.map((shape) => shape.id)).toEqual(['s1', 's2', 's3']);
expect(shapes.map((shape) => shape.mode)).toEqual(['add', 'subtract', 'intersect']);
expect(shapes[1].kind).toBe('linear');
expect(shapes[2].kind).toBe('brush');
});
it('drops a raster component that lost its coverage instead of baking an empty mask', () => {
const edit = normalizeEdits({
local: [
{
id: 'local-r',
name: 'Raster',
mode: 'add',
components: [{ id: 's1', kind: 'raster', mode: 'add' }],
curve: [],
adjust: {},
},
],
} as never);
expect(edit.local[0].components[0].kind).toBe('brush');
expect(edit.local[0].components[0].raster).toBeUndefined();
});
it('always leaves a layer with at least one shape and caps the group', () => {
const many = Array.from({ length: MAX_LOCAL_COMPONENTS + 4 }, (_, i) => ({
id: `s${i}`,
kind: 'radial',
mode: 'add',
}));
const edit = normalizeEdits({
local: [
{ id: 'local-m', name: 'Many', mode: 'add', components: many, curve: [], adjust: {} },
{ id: 'local-e', name: 'Empty', mode: 'add', components: [], curve: [], adjust: {} },
],
} as never);
expect(edit.local[0].components).toHaveLength(MAX_LOCAL_COMPONENTS);
expect(edit.local[1].components).toHaveLength(1);
});
it('creates new layers with one shape so the panel always has something to edit', () => {
const layer = defaultLocalLayer('linear', 0);
expect(layer.components).toHaveLength(1);
expect(layer.components[0].kind).toBe('linear');
expect(layer.components[0].mode).toBe('add');
});
});
+4 -8
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { defaultEdits, normalizeEdits } from './edits';
import { defaultEdits, defaultLocalLayer, normalizeEdits } from './edits';
import { editsToXmp, xmpFileName, xmpToEdits } from './xmp';
describe('xmp sidecars', () => {
@@ -12,13 +12,9 @@ describe('xmp sidecars', () => {
edits.parametric.darks = -17;
edits.hsl.hue[3] = 12;
edits.grain.roughness = 71;
edits.local = [
{
...defaultEdits().local[0],
kind: 'linear',
linear: { x1: 0.1, y1: 0.2, x2: 0.8, y2: 0.9 },
},
];
const mask = defaultLocalLayer('linear', 0);
if (mask.components[0]) mask.components[0].linear = { x1: 0.1, y1: 0.2, x2: 0.8, y2: 0.9 };
edits.local = [mask];
const xml = editsToXmp(edits);
const back = xmpToEdits(xml, defaultEdits());