Free open-source image editor for the browser
Add GPU removal and retouch modes (#76)
Rodrigo Belfiored179d689e2a5
diff --git a/API.md b/API.mdindex e32fa46..5fabd13 100644--- a/API.md+++ b/API.md@@ -62,7 +62,11 @@ Commands take explicit layer IDs rather than using UI selection. Without an ID,Create an empty Heal effect with `addLayer("heal")`.-`addHealPatch(id, stroke, offset)` appends a Healing patch and returns its ID. `stroke` is a painted `BrushStroke`, and `offset` is `[sourceX - targetX, sourceY - targetY]` in source pixels. `setHealSource(id, patchId, offset)` changes a donor. Patches replay in order from the accumulated composite below; the tool supplies automatic donors.+`addHealPatch(id, stroke, offset, mode?)` appends a Healing patch and returns its ID. `stroke` is a painted `BrushStroke`, and `offset` is `[sourceX - targetX, sourceY - targetY]` in source pixels. `setHealSource(id, patchId, offset)` changes a donor. Patches replay in order from the accumulated composite below; the tool supplies automatic donors. `mode` is `"heal"` (the default) or `"clone"`; Clone copies donor color without boundary correction and retains feather and opacity.++`addHealStroke(id, patchId, stroke)` appends a painted or erased `BrushStroke` to an existing patch as one undoable edit. It preserves the mode, donor, feather, and opacity. Feather is a patch-wide blend based on the first stroke's diameter, shared by later additions. All patches expose an ordered `strokes` array; each path stays separate, coverage is combined on the GPU, and moving the patch moves paint and erase together. In the editor, Shift adds and Alt/Option subtracts on the selected patch, committing on release. Alt-click chooses a donor for Heal/Clone when no patch is selected. Older saved scenes with a single `stroke` load as one-element arrays.++`addRemovePatch(id, stroke)` appends a Remove patch and returns its ID. It synthesizes the covered region from unmasked surrounding context using multiscale GPU PatchMatch; no offset or manual source is needed. `setHealSource` rejects Remove patches. The same feather, opacity, destination, duplicate, and delete edits apply. The solver and compositor run through `createEditorRenderer` without React; [the solver documentation](src/features/heal/inpaint/algorithm.md) describes limits and reproducible benchmarks.Create an empty Paint layer with `addLayer("paint")`. `addPaintStroke(id, stroke)` appends a `BrushStroke` with a `color`, `#rrggbb` sRGB: a painting stroke lays that color over the layer's earlier paint, and an erasing one removes paint whatever its color. `setPaintBlend(id, blend)` blends the layer's paint over the image below with the modes `setFill` takes, on encoded values as Photoshop does. A paint stroke builds up as a mask's does. In the editor, a layer's strokes settle into pixels once it has 100 and no gesture is open: `getState()` then shows a `raster` ID and fewer strokes, undo still reaches every stroke, and scene files keep the pixels. A photo holds up to 4 Paint layers and 10 brush masks; adding more throws.diff --git a/ARCHITECTURE.md b/ARCHITECTURE.mdindex 8bd8ac9..136f71a 100644--- a/ARCHITECTURE.md+++ b/ARCHITECTURE.md@@ -17,13 +17,17 @@ Where the code lives and the rules that keep it in place. Keep this file true: w| `app/webmcp.ts` | WebMCP tools for browser agents, one per command; see [API.md](API.md#webmcp). || `app/assistant` | Experimental chat that edits the photo, mounted only at `/?assistant`. `index.tsx` sends a message with a summary of the photo and runs the commands that come back; `jev.ts` asks the Jev evaluation model and turns its answers into commands. || `/api`, at the repository root | Vercel functions: `assistant.ts` answers the chat through `jev.ts`; `vercel dev` serves them locally. |-| `app/editor/index.tsx` | The editor with a document: header, then a tool's canvas and controls in the layout. A tool's `View` replaces both. |+| `app/editor/index.tsx` | The editor with a document: header, then a tool's canvas and controls in the layout. A tool's `View` replaces both. Active brush subscriptions update their input context without rendering the layout. || `app/editor/empty.tsx` | The editor before a document opens: welcome or loading status, and the placeholder sidebar. || `app/editor/sidebar.tsx` | Sidebar sections in order: `EditorSidebar` with a document, `PlaceholderSidebar` without one. || `app/editor/tools.tsx`, `tool-rail.tsx` | The tools and the desktop rail. A tool brings a `Canvas` overlay, `Options` for the bar over the image or the dock, or its own `View`. |+| `app/editor/tool-shortcuts.tsx` | Group shortcuts: return to a tool's remembered mode, then cycle its available modes while it is active. |+| `components/editor/brush-input.tsx` | Brush settings and the active input context. Brush and Healing own separate settings above the views; the canvas, cursor, menu, and shortcuts consume the active family. Size is a viewport diameter independent of image resolution and zoom. `BrushCanvas` converts it to source pixels once at stroke start; recorded strokes retain their image-space geometry. |+| `components/editor/brush-canvas.tsx`, `brush-cursor.tsx`, `brush-wheel.ts` | Shared brush gestures, cursor, and wheel input for color, masks, and retouch. Input batches each frame and flushes on release. Remove and modifier edits keep a local contour outside document history and record one edit on release; Remove also previews destination moves until drop. Modifiers and the release policy are latched at gesture start; the retouch feature owns their meaning. Portaled controls retain their own input. Vertical wheel resizes the active brush and horizontal wheel is captured without panning; trackpad pinch and Space override return navigation to the viewport. The cursor adds a center cross above a 100 px visible diameter. |+| `features/heal/modes.ts` | Retouch names, descriptions, and order: Remove, Heal, Clone. The initial mode, desktop buttons, mobile tabs, and shortcut cycle share this order; choosing a mode is remembered across tool visits. || `app/editor/brush.tsx` | The Brush tool: one canvas that sends color or mask strokes to the feature edits its mode picks, and its options. || `app/editor/dock.tsx` | The mobile dock's tabs and what it shows: the layer stack, a tool's options, or the selected layer's dials. |-| `app/editor/renderer.ts` | Composes feature passes into the preview and export pipelines. |+| `app/editor/renderer.ts` | Composes feature passes into the preview and export pipelines and tracks immutable upstream content for derived-image caches. || `app/editor/mask-overlay.tsx` | The only writer of the mask overlay, derived from the selection. || `app/editor/layers.ts` | The effect kinds the app offers and their layer factories. || `app/editor/export` | The export view. |@@ -42,7 +46,7 @@ Where the code lives and the rules that keep it in place. Keep this file true: w- The canvas keeps its place in both layouts, so crossing the breakpoint moves it without mounting it again. The sidebar and the dock swap; neither mounts hidden, since each subscribes to the document and the histogram reads the GPU.- Features describe numbers as `Parameter`s drawn as sliders in the sidebar and dials in the dock, and other controls once for both. Composition that differs sits beside its desktop form: `AdjustPanel` and `AdjustDock`, or a tool's `Options` under the `dock` density.-- State that outlives a layout, such as the tool, selection, camera, and history, lives above `EditorLayout`.+- State that outlives a layout, such as the tool, selection, camera, history, and each brush family's size and feather, lives above `EditorLayout`. Brush settings last for the document and stay outside history.## Layers@@ -70,6 +74,7 @@ Dependencies point downward. Features do not import each other; `app/` connects## Rendering- `core/renderer` defines passes as nodes (`node`, `pipeline`, `split`, `merge`); the graph owns intermediate textures and timing. Features declare passes; `app/editor/renderer.ts` connects them.+- Expensive derived nodes can cache their result by explicit immutable content dependencies. A cache hit prunes the upstream branch; the graph owns at most two revisions per node, releases them with the composition instance, and reports them separately in `inspect()`. Hidden layers retain their instances, including descendant layers and Healing patches. The renderer adds RAW development revision and proxy factor to the keys; app composition supplies upstream content, and the feature supplies its own geometry. Feather and opacity do not invalidate a Remove patch's synthesis. Remove caches only its bounded correspondence field, at most 4 MiB per revision, and samples donor color during composition.- The graph keeps intermediate textures for the last two sets of sizes it rendered, so interactive proxy renders and full renders alternate without reallocating full-size textures, which a phone's GPU memory cannot absorb gesture after gesture.- Use `vgpu` for GPU work and `vgpu-react` for bindings. Create pipelines once per engine and reuse them. Keep `.wgsl` beside its owner.- The working space is linear Rec.2020 in `rgba16float`. Decoders convert into it and display converts out; passes preserve format and primaries unless they explicitly convert.@@ -79,7 +84,8 @@ Dependencies point downward. Features do not import each other; `app/` connects- Preserve HDR headroom through exposure, curves, and vibrance. Exposure multiplies linear RGB by `2^EV`, without clipping, so +3 EV followed by -3 EV recovers the original light even when intermediate values exceed 1.- The adjustment shader's parameters use UI units; its fitted constants are calibration data.- Strokes are scene content; the renderer caches their rasterized coverage, or a Paint layer's premultiplied 8-bit color, at half the source's resolution each way, a quarter of its pixels, which the passes that read them sample back to full size, and stamps only appended dabs. A Paint layer's or brush mask's raster comes with its first paint and stays, cleared if need be, until the layer goes, so painting and undoing allocate nothing. A photo holds up to 4 Paint layers and 10 brush masks.-- A mask's or Paint layer's latest stroke builds up in a half-float stroke buffer at the same resolution, one stroke at a time; its raster takes it in one pass when the next stroke starts, so it rounds to 8 bits once, however many moves drew it. Until then a Paint layer composes the buffer over its raster, and a mask's readers get its raster with the stroke laid over, in a view redrawn where the stroke grows. A Healing patch's hard stroke rounds the same either way, so it stamps straight into a raster over only the 256 px tiles it reaches.+- A mask's or Paint layer's latest stroke builds up in a half-float stroke buffer at the same resolution, one stroke at a time; its raster takes it in one pass when the next stroke starts, so it rounds to 8 bits once, however many moves drew it. Until then a Paint layer composes the buffer over its raster, and a mask's readers get its raster with the stroke laid over, in a view redrawn where the stroke grows. A Healing or Clone patch records its mode, reuses the same coverage, and keeps editable feather and opacity. Clone copies donor color directly, skipping the correction passes. A Healing patch records ordered hard paint/erase strokes, stamped through the shared GPU brush engine into one raster over only the 256 px tiles its paint reaches. Appending a gesture stamps only new dabs; undo or moved geometry replays the sequence. Shift adds and Alt/Option subtracts on the selected patch, preserving its donor and blend; separate strokes stay separate. The local SVG preview composes the same ordered operations and shows union boundaries and erased holes. Scene loading migrates earlier single-stroke patches once at the boundary.+- Remove patches store hard paint/erase strokes without a donor offset. `features/heal/inpaint` builds a local, multiscale PatchMatch graph, entirely on the GPU: exclusion mask, preserved texture descriptors, nearest valid donor initialization, parallel propagation/random search, overlap voting, and final full-source sampling. Matching is bounded to a 512 px long edge; the output still samples the original source. The Heal compositor applies the patch's coverage, feather, and opacity. No processing depends on React or reads pixels to the CPU. See [the solver](src/features/heal/inpaint/algorithm.md) for its constraints and standalone benchmarks.- A brush mask paints coverage as a Paint layer paints color: both are a painting, the latest strokes over settled pixels, in one raster module (r8 for coverage, rgba8 for color), which also owns the open stroke coverage view. At 100 strokes, once no gesture is open, the renderer reads a painting's raster band by band through one mapped buffer, and the pixels, deflated, become a document resource the layer names in place of its strokes, without a history step. Rendering waits until both the scene and cached raster accept the settled pixels. History scenes name the pixels they drew over, so undo reaches past a settle: the renderer loads those pixels, inflating them band by band through a staging texture, and draws the strokes again. Resources go once no retained scene names them.- A mask that only gradients shape needs no texture: the mix pass computes it, children included. A mask with a painted brush or a range combines in the render graph: each child's coverage, a brush's cached raster, a gradient drawn by a node, or a range read from the image below the mask, folds into the group's in stored order through one combine pass, and the mix pass shares its add, subtract, and intersect. The group takes the image's resolution when a range takes part, since a range follows the photo's edges, and the brushes' otherwise. The renderer gives it out, with each range's own coverage, for the overlay and thumbnails. While a color is picked, renders also keep the image below the mask group, which the picker reads through one GPU pass and 16 bytes of readback per pick.- Open history groups render a reduced proxy. Every render image carries `scale`, its source pixels per texel; shaders that take document coordinates or radii apply it.diff --git a/CONTEXT.md b/CONTEXT.mdindex 6c0ac5e..bc952d8 100644--- a/CONTEXT.md+++ b/CONTEXT.md@@ -7,9 +7,9 @@ OpenLight edits documents containing image content and adjustments.| **Document** | An independent editing session with its own canvas, history, and resources. Opening an image creates a document containing that image. || **Scene** | The document's serializable content: canvas geometry, image-source references, and adjustments. Files and GPU resources live outside it. || **Layer** | A source, effect, or mask in the composition. Image layers own basic adjustments; effects process the image below; masks scope their basic adjustments and child effects. Linear, radial, brush, and range child masks add, subtract, or intersect coverage. A Paint layer is an effect that lays its strokes' colors over the image with a blend mode. Layers compose render nodes without owning textures. |-| **Stroke** | One brush drag: paint or erase, size, feather, flow, and points with pressure in source pixels; a Paint layer's strokes also carry a color. Strokes are content; the renderer rasterizes them into a cached texture at half the source's resolution, coverage for a mask and premultiplied color for a Paint layer, and the latest stroke into a shared stroke buffer until the next one starts. A Paint layer's or brush mask's strokes settle into pixels every 100 strokes, which it then paints over; undo still reaches every stroke. A Healing patch stores a hard stroke, rasterized over only the tiles it reaches, and feathers its accumulated shape separately. |+| **Stroke** | One brush drag: paint or erase, size, feather, flow, and points with pressure in source pixels; a Paint layer's strokes also carry a color. Strokes are content; the renderer rasterizes them into a cached texture at half the source's resolution, coverage for a mask and premultiplied color for a Paint layer, and the latest stroke into a shared stroke buffer until the next one starts. A Paint layer's or brush mask's strokes settle into pixels every 100 strokes, which it then paints over; undo still reaches every stroke. A Healing patch stores ordered hard paint/erase strokes, rasterized over only the tiles its paint reaches, and feathers its accumulated shape separately. || **Range** | A mask that selects pixels of the image below its mask group by lightness, a luminance range, or by color, a color range, rather than by position. A new color range has no coverage until it takes a color from a click on the photo. |-| **Healing patch** | One painted, non-destructive repair in a Healing layer. It copies a nearby donor, found automatically or set by hand, with a bounded boundary color correction. Patches replay in order and each sees the result of the preceding patches. Size belongs to the recorded hard stroke; feather and opacity remain editable on its accumulated shape. |+| **Healing patch** | One painted, non-destructive repair in a Healing layer. Its Heal mode copies a nearby donor, found automatically or set by hand, with a bounded boundary color correction. Clone mode copies the donor without color correction. Remove mode synthesizes many local donor patches from surrounding texture and has no donor offset. Patches replay in order and each sees the result of the preceding patches. Each gesture keeps its recorded size and separate path; Shift adds and Alt/Option subtracts on the selected patch. Feather and opacity remain editable on its accumulated shape. || **Image source** | An image used as input to the document's composition. It is content within a document. || **LUT** | A 3D color lookup table, such as a creative grade, read from a `.cube` file. A LUT layer holds one and applies it to the image below, at the layer's opacity. || **Edit** | One reversible content change. A continuous slider or curve drag is one edit, as is importing a set of adjustments. |diff --git a/PERFORMANCE.md b/PERFORMANCE.mdindex 3632b66..88065ca 100644--- a/PERFORMANCE.md+++ b/PERFORMANCE.md@@ -24,3 +24,9 @@ Enable the optional `timestamp-query` device feature, create a vgpu `timer(gpu)`## Running the benchmarkRun `bun run test:browser --config playwright.bench.config.ts` apart from other browser work, and select a workload with `--grep`, e.g. `--grep 'rendering heal'`. Workloads and settings live in [the benchmark](tests/rendering-benchmark.ts): a deterministic 2400×1600 linear Rec.2020 fixture, 8 warmups, and 40 samples. Results and rendered PNGs go to `test-results/benchmarks`; attach them to the PR rather than committing them.++## Object removal++`bun run test:browser --config playwright.bench.config.ts --grep 'inpainting|rendering remove'` runs three headless workloads. `inpainting solver` measures a fresh local synthesis and mask raster without app composition; `inpainting editor` measures the same moving, 240 px patch in a 2400×1600 scene through `createEditorRenderer`, including the final blend. Both move the stroke one pixel each sample to require a new solve. `rendering remove` retains an unchanged stroke, measuring composition after the synthesis cache is populated. All use 8 warmups and 40 samples, separate first render and CPU encoding, and profile timestamps in a second run. None mounts React. Images are already resident on the GPU; decoding, display, and export are excluded.++Cached results appear as `cachedTextures` in `inspect()`; include them alongside `textures` when calculating memory. Remove caches a correspondence field rather than a full-resolution RGB crop (at most 4 MiB per revision). A full/proxy pair or the last two content revisions is bounded per patch; hiding the layer retains them, and removing the patch releases them. Matching uses a local grid capped at 512 px rather than scanning the whole photograph.diff --git a/src/app/controls.ts b/src/app/controls.tsindex a79d160..ced6aa9 100644--- a/src/app/controls.ts+++ b/src/app/controls.ts@@ -20,6 +20,7 @@ import type {Details,Fill,Grain,+ HealMode,Mask,MaskLayer,PaintStroke,@@ -44,7 +45,12 @@ import { defaultDetails } from "@/features/details/model";import { setFill } from "@/features/fill/edits";import { setGrain } from "@/features/grain/edits";import { defaultGrain } from "@/features/grain/model";-import { addHealPatch, setHealSource } from "@/features/heal/edits";+import {+ addHealPatch,+ addHealStroke,+ addRemovePatch,+ setHealSource,+} from "@/features/heal/edits";import {addLayer,deleteLayer,@@ -147,8 +153,16 @@ export function createControls(const document = workspace.getDocument();editEffect(document, "fill", id, (id) => setFill(document, change, id));},- addHealPatch: (id: string, stroke: BrushStroke, offset: Point) =>- addHealPatch(workspace.getDocument(), id, stroke, offset),+ addHealPatch: (+ id: string,+ stroke: BrushStroke,+ offset: Point,+ mode?: Exclude<HealMode, "remove">,+ ) => addHealPatch(workspace.getDocument(), id, stroke, offset, mode),+ addHealStroke: (id: string, patch: string, stroke: BrushStroke) =>+ addHealStroke(workspace.getDocument(), id, patch, stroke),+ addRemovePatch: (id: string, stroke: BrushStroke) =>+ addRemovePatch(workspace.getDocument(), id, stroke),setHealSource: (id: string, patchId: string, offset: Point) =>setHealSource(workspace.getDocument(), id, patchId, offset),addPaintStroke: (id: string, stroke: PaintStroke) =>diff --git a/src/app/editor/adjust/index.tsx b/src/app/editor/adjust/index.tsxindex f812074..84de6ff 100644--- a/src/app/editor/adjust/index.tsx+++ b/src/app/editor/adjust/index.tsx@@ -36,6 +36,7 @@ import {import { FillControls } from "@/features/fill/controls";import { GrainControls, grainParameters } from "@/features/grain/controls";import { HealControls } from "@/features/heal/controls";+import { HealModeButtons } from "@/features/heal/mode-buttons";import { Histogram } from "@/features/histogram";import { setLayer } from "@/features/layers/edits";import { OverlayToggle } from "@/features/layers/overlay-toggle";@@ -207,6 +208,7 @@ export function AdjustPanel() {header={<PanelHeader title={panelTitle(target) ?? target.name}>{mask && <OverlayToggle />}+ {target.kind === "heal" && <HealModeButtons />}</PanelHeader>}>diff --git a/src/app/editor/index.tsx b/src/app/editor/index.tsxindex fadf8a4..d78ad72 100644--- a/src/app/editor/index.tsx+++ b/src/app/editor/index.tsx@@ -3,6 +3,7 @@ import { Separator } from "@roprgm/ui/separator";import { Tooltip, TooltipContent, TooltipTrigger } from "@roprgm/ui/tooltip";import type { ReactNode } from "react";import type { Workspace } from "@/app/workspace";+import { BrushInputProvider } from "@/components/editor/brush-input";import { BrushProvider, useBrushTool } from "@/components/editor/brush-tool";import { Image } from "@/components/editor/image";import {@@ -19,7 +20,7 @@ import {import { EditorViewport, ViewportStage } from "@/components/editor/viewport";import type { Mask } from "@/core/document";import { locateLayer } from "@/core/document";-import { HealingProvider } from "@/features/heal/mode";+import { HealingProvider, useHealBrush } from "@/features/heal/mode";import { addLayer } from "@/features/layers/edits";import { MaskToolProvider, type Nesting } from "@/features/layers/mask-tool";import { CanvasToolbar } from "@/features/layers/toolbar";@@ -40,8 +41,20 @@ import { OpenButton, OpenContext, OpenStatus } from "./open";import { createEditorRenderer } from "./renderer";import { EditorSidebar } from "./sidebar";import { ToolRail } from "./tool-rail";+import { ToolShortcuts } from "./tool-shortcuts";import { exportTool, ToolProvider, tools, useTool } from "./tools";+function ActiveBrushInput({ children }: { children: ReactNode }) {+ const { tool } = useTool();+ const brush = useBrushTool();+ const healing = useHealBrush();+ return (+ <BrushInputProvider value={tool.id === "heal" ? healing : brush}>+ {children}+ </BrushInputProvider>+ );+}+/*** A View replaces the canvas and its controls; otherwise the tool's Canvas joins the shared canvas.* Its Options go in the bar over the canvas on desktop and in the dock on mobile, where the output@@ -71,24 +84,26 @@ function ToolView() {return <tool.View onClose={close} />;}return (- <EditorLayout- canvas={- <EditorViewport size={size}>- <ViewportStage>- <Image original="originalImage" />- {"Canvas" in tool && <tool.Canvas key={tool.id} />}- </ViewportStage>- <ComparisonDivider />- <CanvasToolbar>- {desktop && "Options" in tool && <tool.Options />}- </CanvasToolbar>- {!desktop && <ImageHistogram placement="canvas" />}- <MaskOverlaySync />- </EditorViewport>- }- panel={<EditorSidebar />}- dock={<DockPanel />}- />+ <ActiveBrushInput>+ <EditorLayout+ canvas={+ <EditorViewport size={size}>+ <ViewportStage>+ <Image original="originalImage" />+ {"Canvas" in tool && <tool.Canvas key={tool.id} />}+ </ViewportStage>+ <ComparisonDivider />+ <CanvasToolbar>+ {desktop && "Options" in tool && <tool.Options />}+ </CanvasToolbar>+ {!desktop && <ImageHistogram placement="canvas" />}+ <MaskOverlaySync />+ </EditorViewport>+ }+ panel={<EditorSidebar />}+ dock={<DockPanel />}+ />+ </ActiveBrushInput>);}@@ -206,6 +221,7 @@ function DocumentEditor({/><PaintSettling /><BrushKeys />+ <ToolShortcuts /></HealingTools></MaskTools></ToolProvider>diff --git a/src/app/editor/renderer.ts b/src/app/editor/renderer.tsindex 1a86141..67cdb7a 100644--- a/src/app/editor/renderer.ts+++ b/src/app/editor/renderer.ts@@ -1,7 +1,12 @@import type { Gpu, Timer } from "vgpu";-import { maskModifiers, type ProcessingLayer } from "@/core/document";+import {+ maskModifiers,+ type ProcessingLayer,+ walkLayers,+} from "@/core/document";import type { ImageSource } from "@/core/image";import {+ type CacheKey,type Composition,createRenderer,input,@@ -17,7 +22,7 @@ import { colorMixer } from "@/features/color-mixer/pass";import { unsharpMask } from "@/features/details/unsharp-mask";import { fill } from "@/features/fill/pass";import { grain } from "@/features/grain/pass";-import { heal } from "@/features/heal/pass";+import { heal, retainHealPatches } from "@/features/heal/pass";import { lut } from "@/features/lut/pass";import { paint } from "@/features/paint/pass";import { toneCurves } from "@/features/tone-curves/pass";@@ -26,6 +31,7 @@ import { vignette } from "@/features/vignette/pass";type Branch = {image: RenderImage;input?: RenderImage;+ dependencies: readonly CacheKey[];rangeSource?: RenderImage;};@@ -33,6 +39,7 @@ function composeLayer(below: RenderImage,layer: ProcessingLayer,composition: Composition,+ dependencies: readonly CacheKey[],): Branch {const name = `layer/${layer.id}`;composition.retain(name);@@ -53,12 +60,21 @@ function composeLayer(layer.kind !== "mask" &&layer.children.some((child) => child.kind === "mask");if (bypassed && !inspected && !holdsMasks) {- return { image: below, rangeSource };+ for (const { layer: hidden } of walkLayers([layer])) {+ const instance = `layer/${hidden.id}`;+ composition.retain(instance);+ if (hidden.kind === "heal") {+ retainHealPatches(hidden.patches, instance, composition);+ }+ }+ return { image: below, rangeSource, dependencies };}let input: RenderImage | undefined;let edited = below;+ let content = dependencies;switch (layer.kind) {case "details":+ content = [...dependencies, layer.details];edited = pipeline(below, [unsharpMask(`${name}/clarity`, layer.details.clarity / 200, 64, 16),unsharpMask(@@ -69,6 +85,7 @@ function composeLayer(]);break;case "mask": {+ content = [...dependencies, layer.adjustments, layer.toneCurve];const adjusted = pipeline(below, [adjustments(layer.adjustments, name)]);if (layer.id === composition.inputId) {input = maskInput(name, adjusted, layer.mask, masks, coverage);@@ -79,26 +96,33 @@ function composeLayer(break;}case "exposure":+ content = [...dependencies, layer.exposure];edited = pipeline(below, [exposure(`${name}/exposure`, layer.exposure)]);break;case "vignette":+ content = [...dependencies, layer.vignette];edited = pipeline(below, [vignette(layer.vignette, `${name}/vignette`)]);break;case "grain":+ content = [...dependencies, layer.grain];edited = pipeline(below, [grain(layer.grain, `${name}/grain`)]);break;case "color-mixer":+ content = [...dependencies, layer.colorMixer];edited = pipeline(below, [colorMixer(layer.colorMixer, `${name}/color-mixer`),]);break;case "fill":+ content = [...dependencies, layer.fill];edited = pipeline(below, [fill(layer.fill, `${name}/fill`)]);break;case "lut":+ content = [...dependencies, layer.lut];edited = pipeline(below, [lut(layer.lut, `${name}/lut`)]);break;case "paint":+ content = [...dependencies, layer.blend, layer.strokes, layer.raster];edited = paint(below,composition.paint(layer),@@ -107,7 +131,14 @@ function composeLayer();break;case "heal": {- const result = heal(below, layer.patches, name, composition);+ content = [...dependencies, ...layer.patches];+ const result = heal(+ below,+ layer.patches,+ name,+ composition,+ dependencies,+ );edited = result.image;input = result.input;break;@@ -118,7 +149,7 @@ function composeLayer(layer.kind === "mask"? layer.children.filter((child) => child.kind !== "mask"): layer.children;- const children = composeLayers(edited, effects, composition);+ const children = composeLayers(edited, effects, composition, content);const image = mixAdjustment(name,below,@@ -131,6 +162,7 @@ function composeLayer(return {image,input: input ?? children.input,+ dependencies: [...children.dependencies, layer],rangeSource: rangeSource ?? children.rangeSource,};}@@ -139,17 +171,20 @@ function composeLayers(below: RenderImage,layers: readonly ProcessingLayer[],composition: Composition,+ dependencies: readonly CacheKey[],): Branch {let image = below;let input: RenderImage | undefined;+ let content = dependencies;let rangeSource: RenderImage | undefined;for (const layer of layers) {- const branch = composeLayer(image, layer, composition);+ const branch = composeLayer(image, layer, composition, content);image = branch.image;input ??= branch.input;+ content = branch.dependencies;rangeSource ??= branch.rangeSource;}- return { image, input, rangeSource };+ return { image, input, rangeSource, dependencies: content };}/**@@ -182,8 +217,14 @@ export function createEditorRenderer(developed,sourceLayer.children,composition,+ [sourceLayer.adjustments, sourceLayer.toneCurve],+ );+ const composite = composeLayers(+ children.image,+ layers,+ composition,+ children.dependencies,);- const composite = composeLayers(children.image, layers, composition);const full = composite.image;const [original, output] = transformImages([input(source.image), full],diff --git a/src/app/editor/tool-shortcuts.tsx b/src/app/editor/tool-shortcuts.tsxnew file mode 100644index 0000000..ac175ab--- /dev/null+++ b/src/app/editor/tool-shortcuts.tsx@@ -0,0 +1,42 @@+import { useBrushTool } from "@/components/editor/brush-tool";+import { useDocument } from "@/components/editor/session";+import { findLayer } from "@/core/document";+import { useHealing } from "@/features/heal/mode";+import { nextHealMode } from "@/features/heal/modes";+import { useShortcuts } from "@/hooks/use-shortcuts";+import { type Tool, tools, useTool } from "./tools";++/** A group's key restores its last mode; pressing it while active cycles its modes. */+export function ToolShortcuts() {+ const { tool, setTool } = useTool();+ const brush = useBrushTool();+ const healing = useHealing();+ const document = useDocument();+ const cycles = new Map<Tool["id"], () => void>([+ [+ "brush",+ () =>+ brush.update({+ mode: brush.settings.mode === "color" ? "mask" : "color",+ }),+ ],+ ["heal", () => healing.selectMode(nextHealMode(healing.mode))],+ ]);+ function select(entry: Tool) {+ if (entry.id === "color-range") {+ const selected = document.selection.getState().layerId;+ const layer = findLayer(document.scene.getState().layers, selected);+ if (layer?.kind !== "mask" || layer.mask.kind !== "color-range") return;+ }+ const cycle = cycles.get(entry.id);+ if (entry === tool && cycle) {+ cycle();+ return;+ }+ setTool(entry);+ }+ useShortcuts(+ Object.fromEntries(tools.map((entry) => [entry.key, () => select(entry)])),+ );+ return null;+}diff --git a/src/app/editor/tools.tsx b/src/app/editor/tools.tsxindex 13654cd..e92fba9 100644--- a/src/app/editor/tools.tsx+++ b/src/app/editor/tools.tsx@@ -6,7 +6,6 @@ import {useContext,useState,} from "react";-import { useBrushTool } from "@/components/editor/brush-tool";import { useDocument } from "@/components/editor/session";import { AdjustIcon } from "@/components/icons/adjust";import { BrushIcon } from "@/components/icons/brush";@@ -16,14 +15,12 @@ import { EyedropperIcon } from "@/components/icons/eyedropper";import { HealIcon } from "@/components/icons/heal";import { LinearGradientIcon } from "@/components/icons/linear-gradient";import { RadialGradientIcon } from "@/components/icons/radial-gradient";-import { findLayer } from "@/core/document";import { CropEditor } from "@/features/crop/view";import { HealOptions } from "@/features/heal/options";import { HealOverlay } from "@/features/heal/overlay";import { addLayer } from "@/features/layers/edits";import { GradientOverlay } from "@/features/layers/gradient-overlay";import { RangePicker } from "@/features/layers/range-picker";-import { useShortcuts } from "@/hooks/use-shortcuts";import { BrushToolCanvas, BrushToolOptions } from "./brush";import { ExportMode } from "./export";import { createLayer } from "./layers";@@ -150,34 +147,8 @@ export const tools = [export type Tool = (typeof tools)[number];-/**- * Holds the active tool, and each tool's key selects it in either layout. The Brush's key, pressed on- * the Brush, switches it between color and mask.- */+/** The selected tool outlives its canvas and controls in either layout. */export function ToolProvider({ children }: { children: ReactNode }) {const [tool, setTool] = useState<Tool>(adjust);- const brush = useBrushTool();- const document = useDocument();- function select(entry: Tool) {- if (entry.id === "color-range") {- const selected = document.selection.getState().layerId;- const layer = findLayer(document.scene.getState().layers, selected);- if (layer?.kind !== "mask" || layer.mask.kind !== "color-range") {- return;- }- }- if (entry === tool && entry.id === "brush") {- brush.update({- mode: brush.settings.mode === "color" ? "mask" : "color",- });- return;- }- setTool(entry);- }- useShortcuts(- Object.fromEntries(- tools.map((entry) => [entry.key, () => select(entry)] as const),- ),- );return <ToolContext value={{ tool, setTool }}>{children}</ToolContext>;}diff --git a/src/app/scene-file.ts b/src/app/scene-file.tsindex 80f5e4a..a2c5337 100644--- a/src/app/scene-file.ts+++ b/src/app/scene-file.ts@@ -37,6 +37,22 @@ import { parse, withDefaults } from "@/lib/parse";/** Raised only when older files can no longer load as written; a parameter added later takes its default. */const version = 1;+const savedHealPatch = z.pipe(+ z.transform((input) => {+ if (+ typeof input !== "object" ||+ input === null ||+ "strokes" in input ||+ !("stroke" in input)+ ) {+ return input;+ }+ const { stroke, ...patch } = input;+ return { ...patch, strokes: [stroke] };+ }),+ healPatchSchema,+);+/** A saved scene: the scene as edited and the name and type of each source file, stored beside it by ID. */export type SceneJson = {format: "openlight";@@ -106,7 +122,7 @@ function processingLayer(children: z.ZodMiniType<readonly ProcessingLayer[]>) {z.object({...base,kind: z.literal("heal"),- patches: z.array(healPatchSchema),+ patches: z.array(savedHealPatch),}),z.object({ ...base, kind: z.literal("paint"), ...paintShape }),z.object({diff --git a/src/components/editor/brush-canvas.tsx b/src/components/editor/brush-canvas.tsxindex 16589cf..0fd7f49 100644--- a/src/components/editor/brush-canvas.tsx+++ b/src/components/editor/brush-canvas.tsx@@ -5,15 +5,17 @@ import {type PointerEvent,type ReactNode,useEffect,- useId,+ useEffectEvent,useRef,useState,} from "react";import type { BrushStroke, StrokePoint } from "@/core/document";import type { Point } from "@/core/image/frame";import { useShortcuts } from "@/hooks/use-shortcuts";-import { blurActive } from "@/lib/dom";-import { useBrushParameters, useBrushTool } from "./brush-tool";+import { blurActive, containsTarget } from "@/lib/dom";+import { BrushCursor } from "./brush-cursor";+import { useBrushInput } from "./brush-input";+import { useBrushWheel } from "./brush-wheel";import { CanvasHint } from "./canvas-hint";import { useDocumentMapping } from "./mapping";import { useDocument } from "./session";@@ -27,6 +29,7 @@ type Stroke = {/** The viewport bounds measured once; pointer capture keeps them valid for the drag. */box: DOMRect;pending: StrokePoint[];+ editOnRelease: boolean;frame?: number;};@@ -37,13 +40,15 @@ type PointerLike = {pointerType: string;};+export type BrushModifiers = { shift: boolean; alt: boolean };+/** Shared pressure-aware brush input and cursor. The caller owns the scene edit. */export function BrushCanvas({label,erase,- feather,onStart,onExtend,+ editOnRelease = false,onComplete,onFinish,onPickSource,@@ -52,11 +57,11 @@ export function BrushCanvas({}: {label: string;erase: boolean;- /** Overrides the shared brush feather for tools that own their stroke softness. */- feather?: number;- /** Records the first dab and returns whether the stroke started; a declined stroke leaves nothing behind. */- onStart: (stroke: BrushStroke) => boolean;+ /** Starts the first dab or preview and returns whether accepted; a declined stroke leaves nothing behind. */+ onStart: (stroke: BrushStroke, modifiers: BrushModifiers) => boolean;onExtend: (points: readonly StrokePoint[]) => void;+ /** Keeps a local preview outside history; onFinish records the edit on release. */+ editOnRelease?: boolean | ((modifiers: BrushModifiers) => boolean);onComplete?: (signal: AbortSignal) => void | Promise<void>;onFinish?: (committed: boolean) => void;onPickSource?: (point: Point) => void;@@ -64,9 +69,9 @@ export function BrushCanvas({children?: ReactNode;}) {const document = useDocument();- const brush = useBrushTool();- const strokeFeather = feather ?? brush.settings.feather;+ const brush = useBrushInput();const camera = useViewport();+ const wheelRef = useBrushWheel();const mapping = useDocumentMapping();const stroke = useRef<Stroke | null>(null);const completing = useRef<AbortController | null>(null);@@ -76,11 +81,28 @@ export function BrushCanvas({const [pointerVisible, setPointerVisible] = useState(false);const [busy, setBusy] = useState(false);const [error, setError] = useState<string>();- const gradient = useId();function point(event: PointerLike, box: DOMRect): StrokePoint {const [x, y] = mapping.toDocument(event.clientX, event.clientY, box);return [x, y, event.pointerType === "pen" ? event.pressure : 1];}+ function flush(current: Stroke) {+ current.frame = undefined;+ if (!current.pending.length) {+ return true;+ }+ const points = current.pending;+ current.pending = [];+ try {+ onExtend(points);+ return true;+ } catch (error) {+ stroke.current = null;+ onFinish?.(false);+ if (!current.editOnRelease) document.history.cancel();+ setError(String(error));+ return false;+ }+ }/** Ends the stroke; a cancelled stroke restores the scene. */function finish(commit: boolean) {const current = stroke.current;@@ -94,12 +116,13 @@ export function BrushCanvas({return;}stroke.current = null;- onFinish?.(commit);if (current.frame !== undefined) {cancelAnimationFrame(current.frame);}+ if (commit && !flush(current)) return;+ onFinish?.(commit);+ if (current.editOnRelease) return;if (commit) {- flush(current);if (!onComplete) {document.history.commit();return;@@ -136,28 +159,24 @@ export function BrushCanvas({}document.history.cancel();}- function flush(current: Stroke) {- current.frame = undefined;- if (!current.pending.length) {- return;- }- const points = current.pending;- current.pending = [];- try {- onExtend(points);- } catch (error) {- document.history.cancel();- stroke.current = null;- setError(String(error));- }- }- useEffect(- () => () => {- finish(false);+ const cancelStroke = useEffectEvent(() => finish(false));+ useEffect(() => {+ const unsubscribe = document.history.status.subscribe((state, previous) => {+ if (+ stroke.current &&+ !state.editing &&+ (previous.editing ||+ state.undoCount !== previous.undoCount ||+ state.redoCount !== previous.redoCount)+ )+ cancelStroke();+ });+ return () => {+ unsubscribe();+ cancelStroke();brush.setPreview(false);- },- [],- );+ };+ }, []);useEffect(() => {if (!brush.preview || pointer) return;const bounds = camera.ref.current?.getBoundingClientRect();@@ -171,16 +190,30 @@ export function BrushCanvas({onDone();}},- "[": () => brush.update({ size: Math.round(brush.settings.size / 1.25) }),- "]": () => brush.update({ size: Math.round(brush.settings.size * 1.25) }),+ "[": () => resizeBy(1 / 1.25),+ "]": () => resizeBy(1.25),"shift+[": () => featherBy(-0.1),"shift+]": () => featherBy(0.1),});+ function resizeBy(factor: number) {+ const rounded = Math.round(brush.settings.size * factor);+ const size =+ rounded === brush.settings.size+ ? rounded + Math.sign(factor - 1)+ : rounded;+ brush.update({ size });+ }function featherBy(step: number) {+ if (stroke.current) return;const feather = Math.round((brush.settings.feather + step) * 10) / 10;brush.update({ feather: Math.min(1, Math.max(0, feather)) });}function start(event: PointerEvent<HTMLDivElement>) {+ // Portaled controls share the React tree, but their gestures belong to the UI.+ if (!containsTarget(event)) {+ event.stopPropagation();+ return;+ }const current = stroke.current;if (current?.touch && event.pointerType === "touch" && !event.isPrimary) {// A second finger means a pinch: drop the stroke and let the viewport take both fingers.@@ -204,14 +237,22 @@ export function BrushCanvas({return;}setError(undefined);- const opened = document.history.begin();- const started = onStart({- mode: erase ? "erase" : "paint",- size: brush.settings.size,- feather: strokeFeather,- flow: brush.settings.flow,- points: [first],- });+ const modifiers = { shift: event.shiftKey, alt: event.altKey };+ const deferred =+ typeof editOnRelease === "function"+ ? editOnRelease(modifiers)+ : editOnRelease;+ const opened = !deferred && document.history.begin();+ const started = onStart(+ {+ mode: erase ? "erase" : "paint",+ size: brush.settings.size * mapping.pixelsPerViewportPixel,+ feather: brush.settings.feather,+ flow: brush.settings.flow,+ points: [first],+ },+ modifiers,+ );if (!started) {if (opened) document.history.cancel();return;@@ -222,6 +263,7 @@ export function BrushCanvas({client: [event.clientX, event.clientY],box,pending: [],+ editOnRelease: deferred,};// The viewport below would otherwise capture the pointer to pan.event.preventDefault();@@ -230,8 +272,11 @@ export function BrushCanvas({blurActive();}function move(event: PointerEvent<HTMLDivElement>) {+ if (!containsTarget(event)) return;const bounds = event.currentTarget.getBoundingClientRect();const overHandle =+ !event.shiftKey &&+ !event.altKey &&event.target instanceof Element &&event.target.closest("[data-hide-brush-cursor]");setPointerVisible(!overHandle);@@ -248,7 +293,6 @@ export function BrushCanvas({for (const item of events.length ? events : [native]) {current.pending.push(point(item, current.box));}- // One edit per frame keeps the queue from outrunning the display.current.frame ??= requestAnimationFrame(() => flush(current));}function end(event: PointerEvent<HTMLDivElement>) {@@ -259,19 +303,19 @@ export function BrushCanvas({finish(true);event.currentTarget.releasePointerCapture(event.pointerId);}- const radius = brush.settings.size / 2 / mapping.pixelsPerViewportPixel;- const color = erase ? "black" : "white";- const dash = erase ? "4 3" : undefined;+ const radius = brush.settings.size / 2;const status = error ?? (busy ? "Finishing stroke…" : undefined);const cursor = pointerVisible || brush.preview ? pointer : null;return (<div+ ref={wheelRef}role="application"aria-label={label}className="absolute inset-0 cursor-none touch-none data-[pan=true]:pointer-events-none"data-pan={camera.panMode}onDoubleClick={(event) => event.stopPropagation()}onContextMenu={(event) => {+ if (!containsTarget(event)) return;event.preventDefault();const bounds = event.currentTarget.getBoundingClientRect();setMenu([event.clientX - bounds.left, event.clientY - bounds.top]);@@ -280,54 +324,24 @@ export function BrushCanvas({onPointerMove={move}onPointerUp={end}onPointerLeave={() => setPointerVisible(false)}- onPointerCancel={() => finish(false)}- onLostPointerCapture={() => {- if (stroke.current) {+ onPointerCancel={(event) => {+ if (stroke.current?.pointer === event.pointerId) finish(false);+ }}+ onLostPointerCapture={(event) => {+ if (stroke.current?.pointer === event.pointerId) {finish(false);}}}>{children}{cursor && !camera.panMode && (- <svg- aria-hidden="true"- data-brush-cursor="true"- data-preview={brush.preview}- className="pointer-events-none absolute inset-0 size-full overflow-visible"- >- <defs>- {/* The fill previews the dab: solid inside the feather, fading to the edge. */}- <radialGradient id={gradient} cx="0.5" cy="0.5" r="0.5">- <stop offset="0" stopColor={color} stopOpacity="0.3" />- <stop- offset={1 - strokeFeather}- stopColor={color}- stopOpacity="0.3"- />- <stop offset="1" stopColor={color} stopOpacity="0" />- </radialGradient>- </defs>- <circle- cx={cursor[0]}- cy={cursor[1]}- r={Math.max(2, radius)}- fill={`url(#${gradient})`}- stroke="white"- strokeOpacity="0.9"- strokeDasharray={dash}- />- <circle- cx={cursor[0]}- cy={cursor[1]}- r={Math.max(2, radius)}- fill="none"- stroke="black"- strokeOpacity="0.5"- strokeWidth="3"- strokeDasharray={dash}- style={{ paintOrder: "stroke" }}- />- </svg>+ <BrushCursor+ at={cursor}+ radius={radius}+ feather={brush.settings.feather}+ erase={erase}+ preview={brush.preview}+ />)}{status && <CanvasHint>{status}</CanvasHint>}{menu && <BrushMenu at={menu} onClose={() => setMenu(null)} />}@@ -337,7 +351,7 @@ export function BrushCanvas({/** The brush's size and feather where a right click opened them, as Photoshop's brush menu. */function BrushMenu({ at, onClose }: { at: Point; onClose: () => void }) {- const [size, feather] = useBrushParameters();+ const [size, feather] = useBrushInput().parameters;return (<Popover open onOpenChange={(open) => !open && onClose()}><PopoverTriggerdiff --git a/src/components/editor/brush-cursor.tsx b/src/components/editor/brush-cursor.tsxnew file mode 100644index 0000000..ad6dc3d--- /dev/null+++ b/src/components/editor/brush-cursor.tsx@@ -0,0 +1,71 @@+import { useId } from "react";+import type { Point } from "@/core/image/frame";++/** The active brush's size, feather, and center in viewport pixels. */+export function BrushCursor({+ at,+ radius,+ feather,+ erase,+ preview,+}: {+ at: Point;+ radius: number;+ feather: number;+ erase: boolean;+ preview: boolean;+}) {+ const gradient = useId();+ const color = erase ? "black" : "white";+ const dash = erase ? "4 3" : undefined;+ const center = `M${at[0] - 4} ${at[1]}h8M${at[0]} ${at[1] - 4}v8`;+ return (+ <svg+ aria-hidden="true"+ data-brush-cursor="true"+ data-preview={preview}+ className="pointer-events-none absolute inset-0 size-full overflow-visible"+ >+ <defs>+ {/* Solid inside the feather, fading to the edge. */}+ <radialGradient id={gradient} cx="0.5" cy="0.5" r="0.5">+ <stop offset="0" stopColor={color} stopOpacity="0.3" />+ <stop offset={1 - feather} stopColor={color} stopOpacity="0.3" />+ <stop offset="1" stopColor={color} stopOpacity="0" />+ </radialGradient>+ </defs>+ <circle+ cx={at[0]}+ cy={at[1]}+ r={Math.max(2, radius)}+ fill={`url(#${gradient})`}+ stroke="white"+ strokeOpacity="0.9"+ strokeDasharray={dash}+ />+ <circle+ cx={at[0]}+ cy={at[1]}+ r={Math.max(2, radius)}+ fill="none"+ stroke="black"+ strokeOpacity="0.5"+ strokeWidth="3"+ strokeDasharray={dash}+ style={{ paintOrder: "stroke" }}+ />+ {radius > 50 && (+ <g data-brush-center="true" fill="none">+ <path d={center} stroke="white" strokeOpacity="0.9" />+ <path+ d={center}+ stroke="black"+ strokeOpacity="0.5"+ strokeWidth="3"+ style={{ paintOrder: "stroke" }}+ />+ </g>+ )}+ </svg>+ );+}diff --git a/src/components/editor/brush-input.tsx b/src/components/editor/brush-input.tsxnew file mode 100644index 0000000..3ce27e3--- /dev/null+++ b/src/components/editor/brush-input.tsx@@ -0,0 +1,108 @@+import { createContext, type ReactNode, useContext, useState } from "react";+import { clamp } from "@/lib/math";+import type { Parameter } from "./parameter";++export type BrushShape = {+ /** Diameter in viewport pixels; BrushCanvas converts it to source pixels for each stroke. */+ size: number;+ feather: number;+ flow: number;+};++const maxSize = 1000;++type BrushSettings = {+ settings: BrushShape;+ maxSize: number;+ preview: boolean;+ setPreview: (preview: boolean) => void;+ update: (change: Partial<BrushShape>) => void;+ resize: (factor: number) => void;+};++export type BrushInput = BrushSettings & {+ parameters: [Parameter, Parameter];+};++/** Each brush family keeps its own settings for the lifetime of the document. */+export function useBrushSettings(feather: number) {+ const [settings, setSettings] = useState<BrushShape>({+ size: 50,+ feather,+ flow: 1,+ });+ const [preview, setPreview] = useState(false);+ function resize(factor: number) {+ setSettings((settings) => ({+ ...settings,+ size: clamp(settings.size * factor, 1, maxSize),+ }));+ }+ function update(change: Partial<BrushShape>) {+ setSettings((settings) => ({+ ...settings,+ ...change,+ size: clamp(change.size ?? settings.size, 1, maxSize),+ feather: clamp(change.feather ?? settings.feather, 0, 1),+ flow: clamp(change.flow ?? settings.flow, 0, 1),+ }));+ }+ return {+ settings: { ...settings, size: Math.round(settings.size) },+ maxSize,+ preview,+ setPreview,+ update,+ resize,+ };+}++export function brushParameters({+ settings,+ maxSize,+ setPreview,+ update,+}: BrushSettings): [Parameter, Parameter] {+ return [+ {+ id: "size",+ label: "Size",+ value: settings.size,+ min: 1,+ max: maxSize,+ valueWidth: `${maxSize}`.length,+ onEditingChange: setPreview,+ onChange: (size) => update({ size: Math.round(size) }),+ },+ {+ id: "feather",+ label: "Feather",+ value: Math.round(settings.feather * 100),+ min: 0,+ max: 100,+ origin: 0,+ format: (value) => `${value}%`,+ valueWidth: 3,+ onEditingChange: setPreview,+ onChange: (value) => update({ feather: value / 100 }),+ },+ ];+}++const Input = createContext<BrushInput | null>(null);++export function BrushInputProvider({+ value,+ children,+}: {+ value: BrushInput;+ children: ReactNode;+}) {+ return <Input value={value}>{children}</Input>;+}++export function useBrushInput() {+ const input = useContext(Input);+ if (!input) throw Error("A brush input provider is required.");+ return input;+}diff --git a/src/components/editor/brush-tool.tsx b/src/components/editor/brush-tool.tsxindex 49cb228..c996d5e 100644--- a/src/components/editor/brush-tool.tsx+++ b/src/components/editor/brush-tool.tsx@@ -6,19 +6,19 @@ import {useState,} from "react";import { findLayer, type Layer } from "@/core/document";-import { clamp } from "@/lib/math";+import {+ type BrushInput,+ type BrushShape,+ brushParameters,+ useBrushSettings,+} from "./brush-input";import type { Parameter } from "./parameter";-import { useDocument, useScene } from "./session";+import { useDocument } from "./session";/** What the brush paints: color on a paint layer, or coverage on a brush mask. */export type BrushMode = "color" | "mask";-export type BrushSettings = {- /** Diameter in source pixels. */- size: number;- /** Soft edge as a fraction of the radius, 0 to 1. */- feather: number;- flow: number;+export type BrushSettings = BrushShape & {erase: boolean;mode: BrushMode;/** The primary color, which strokes paint, and the secondary, as `#rrggbb`. */@@ -39,17 +39,15 @@ export function brushMode(layer: Layer | undefined): BrushMode | undefined {return undefined;}-const BrushTool = createContext<{- settings: BrushSettings;- /** The mode the next stroke uses: the setting, inverted while Alt is held. */- erase: boolean;- /** The largest useful diameter: half the image's long edge. */- maxSize: number;- /** Keeps the brush cursor visible while a setting that shapes it is edited. */- preview: boolean;- setPreview: (preview: boolean) => void;- update: (change: Partial<BrushSettings>) => void;-} | null>(null);+const BrushTool = createContext<+ | (Omit<BrushInput, "settings" | "update"> & {+ settings: BrushSettings;+ /** The mode the next stroke uses: the setting, inverted while Alt is held. */+ erase: boolean;+ update: (change: Partial<BrushSettings>) => void;+ })+ | null+>(null);export function useBrushTool() {const tool = useContext(BrushTool);@@ -60,30 +58,16 @@ export function useBrushTool() {}/**- * Brush settings outlive strokes and tool switches; they start relative to the image size. The mode+ * Brush settings outlive strokes and tool switches; size stays fixed on screen. The mode* follows the selection onto a paint layer or brush mask, so the brush paints what is selected.*/export function BrushProvider({ children }: { children: ReactNode }) {const document = useDocument();- const sourceId = useScene((scene) => scene.layers[0].source);- const size = document.resources.get(sourceId).image.size;- const longest = Math.max(size[0], size[1]);- const maxSize = Math.max(1, Math.round(longest / 2));- // 3% of the image in steps of 5, never under 10 px unless the image itself is that small.- const initialSize = Math.min(- maxSize,- Math.max(10, Math.round((longest * 0.03) / 5) * 5),- );- const [settings, setSettings] = useState<BrushSettings>({- size: initialSize,- feather: 0.5,- flow: 1,- erase: false,- mode: "mask",- colors: defaultColors,- });+ const brush = useBrushSettings(0.5);+ const [paint, setPaint] = useState<+ Pick<BrushSettings, "erase" | "mode" | "colors">+ >({ erase: false, mode: "mask", colors: defaultColors });const [alt, setAlt] = useState(false);- const [preview, setPreview] = useState(false);useEffect(() =>document.selection.subscribe(({ layerId }) => {@@ -91,7 +75,7 @@ export function BrushProvider({ children }: { children: ReactNode }) {findLayer(document.scene.getState().layers, layerId),);if (mode) {- setSettings((settings) => ({ ...settings, mode }));+ setPaint((paint) => ({ ...paint, mode }));}}),[document],@@ -105,21 +89,23 @@ export function BrushProvider({ children }: { children: ReactNode }) {window.addEventListener("blur", () => setAlt(false), { signal });return () => controller.abort();}, []);- function update(change: Partial<BrushSettings>) {- setSettings((settings) => ({- ...settings,- ...change,- size: clamp(change.size ?? settings.size, 1, maxSize),- }));+ function update({ size, feather, flow, ...change }: Partial<BrushSettings>) {+ brush.update({ size, feather, flow });+ setPaint((paint) => ({ ...paint, ...change }));}+ const settings = { ...brush.settings, ...paint };+ const [size, feather] = brushParameters(brush);+ const parameters: [Parameter, Parameter] = [+ size,+ { ...feather, defaultValue: 50 },+ ];return (<BrushToolvalue={{+ ...brush,settings,+ parameters,erase: settings.erase !== alt,- maxSize,- preview,- setPreview,update,}}>@@ -137,32 +123,9 @@ export function flowAt(share: number) {/** The next stroke's size, feather, and flow, for sliders and dials wherever they show. */export function useBrushParameters(): [Parameter, Parameter, Parameter] {- const { settings, maxSize, setPreview, update } = useBrushTool();+ const { settings, parameters, update } = useBrushTool();return [- {- id: "size",- label: "Size",- value: settings.size,- min: 1,- max: maxSize,- format: (value) => `${value}px`,- valueWidth: `${maxSize}`.length,- onEditingChange: setPreview,- onChange: (size) => update({ size: Math.round(size) }),- },- {- id: "feather",- label: "Feather",- value: Math.round(settings.feather * 100),- min: 0,- max: 100,- defaultValue: 50,- origin: 0,- format: (value) => `${value}%`,- valueWidth: 3,- onEditingChange: setPreview,- onChange: (value) => update({ feather: value / 100 }),- },+ ...parameters,{id: "flow",label: "Flow",diff --git a/src/components/editor/brush-wheel.ts b/src/components/editor/brush-wheel.tsnew file mode 100644index 0000000..e354cdc--- /dev/null+++ b/src/components/editor/brush-wheel.ts@@ -0,0 +1,31 @@+import { useEffect, useEffectEvent, useRef } from "react";+import { useBrushInput } from "./brush-input";+import { useViewport } from "./viewport";++/** Handles brush sizing before the viewport's native wheel listener. */+export function useBrushWheel() {+ const ref = useRef<HTMLDivElement>(null);+ const brush = useBrushInput();+ const camera = useViewport();+ const wheel = useEffectEvent((event: WheelEvent) => {+ // Trackpad pinch is ctrl+wheel; Space hands all scrolling back to the viewport.+ if (event.ctrlKey || event.metaKey || camera.panMode) {+ return;+ }+ event.preventDefault();+ event.stopPropagation();+ if (!event.deltaY) return;+ let delta = event.deltaY;+ if (event.deltaMode === WheelEvent.DOM_DELTA_LINE) delta *= 16;+ if (event.deltaMode === WheelEvent.DOM_DELTA_PAGE)+ delta *= camera.viewport[1];+ brush.resize(Math.exp(delta / 400));+ });+ useEffect(() => {+ const element = ref.current;+ if (!element) return;+ element.addEventListener("wheel", wheel, { passive: false });+ return () => element.removeEventListener("wheel", wheel);+ }, []);+ return ref;+}diff --git a/src/components/icons/clone.tsx b/src/components/icons/clone.tsxnew file mode 100644index 0000000..759f0cf--- /dev/null+++ b/src/components/icons/clone.tsx@@ -0,0 +1,10 @@+import { Icon, type IconProps } from "./icon";++export function CloneIcon(props: IconProps) {+ return (+ <Icon {...props}>+ <path d="M9 14V9a3 3 0 1 1 6 0v5M7 14h10l2 4H5l2-4Z" />+ <path d="M5 21h14" />+ </Icon>+ );+}diff --git a/src/components/icons/remove.tsx b/src/components/icons/remove.tsxnew file mode 100644index 0000000..3c4d287--- /dev/null+++ b/src/components/icons/remove.tsx@@ -0,0 +1,9 @@+import { Icon, type IconProps } from "./icon";++export function RemoveIcon(props: IconProps) {+ return (+ <Icon {...props}>+ <path d="m4 14 9-9a2 2 0 0 1 3 0l4 4a2 2 0 0 1 0 3l-8 8H8l-4-4a1.5 1.5 0 0 1 0-2ZM9 9l8 8M12 20h8" />+ </Icon>+ );+}diff --git a/src/core/document/index.ts b/src/core/document/index.tsindex e111415..cba4a32 100644--- a/src/core/document/index.ts+++ b/src/core/document/index.ts@@ -18,6 +18,7 @@ export type {Fill,Gradient,Grain,+ HealMode,HealPatch,ImageLayer,Layer,diff --git a/src/core/document/scene.ts b/src/core/document/scene.tsindex 3460a4b..d684bca 100644--- a/src/core/document/scene.ts+++ b/src/core/document/scene.ts@@ -114,15 +114,19 @@ export type Painting<S extends BrushStroke = BrushStroke> = {};/** Coverage painted with strokes; the renderer rasterizes them into a cached texture. */export type BrushMask = { readonly kind: "brush" } & Painting;-/** One non-destructive repair: a painted shape filled from a donor at `offset` source pixels away. */+export type HealMode = "heal" | "clone" | "remove";+/** A painted repair: Heal/Clone use a donor offset; Remove synthesizes surrounding texture. */export type HealPatch = {readonly id: string;- /** Feather applied after the stroke's dabs have accumulated into one patch shape. */+ /** Feather applied after paint and erase strokes have accumulated into one patch shape. */readonly feather: number;- readonly stroke: BrushStroke;+ /** Ordered hard strokes; each gesture adds or subtracts coverage without joining paths. */+ readonly strokes: readonly BrushStroke[];readonly opacity: number;- readonly offset: Point;-};+} & (+ | { readonly mode: "heal" | "clone"; readonly offset: Point }+ | { readonly mode: "remove" }+);/*** The pixels of the image below the mask group whose lightness, in 0..100 UI units from black to* white, lies within `low` to `high`, fading out over `smoothness` past either end.diff --git a/src/core/renderer/graph.ts b/src/core/renderer/graph.tsindex f4559df..23ca3bd 100644--- a/src/core/renderer/graph.ts+++ b/src/core/renderer/graph.ts@@ -9,7 +9,7 @@ import {type Timer,target,} from "vgpu";-import type { RenderImage, RenderNode } from "./node";+import type { CacheKey, RenderImage, RenderNode } from "./node";type Pass = {shader: RenderNode["shader"];@@ -18,11 +18,21 @@ type Pass = {buffers: Map<string, { buffer: Buffer; data: Float32Array }>;};-function plan(outputs: readonly RenderImage[]) {+type Cached = { keys: readonly CacheKey[]; target: Target };++function sameKeys(a: readonly CacheKey[], b: readonly CacheKey[]) {+ return a.length === b.length && a.every((key, index) => key === b[index]);+}++function plan(+ outputs: readonly RenderImage[],+ caches: ReadonlyMap<string, Cached[]>,+) {const uses = new Map<RenderImage, number>();const order: RenderNode[] = [];const live = new Set<Target>();const names = new Set<string>();+ const results = new Map<RenderNode, Target>();function visit(image: RenderImage) {const count = uses.get(image) ?? 0;uses.set(image, count + 1);@@ -37,18 +47,35 @@ function plan(outputs: readonly RenderImage[]) {throw Error(`Duplicate render node: ${image.name}.`);}names.add(image.name);+ const keys = image.cacheKeys;+ const cached =+ keys &&+ caches+ .get(image.name)+ ?.find(+ (cached) =>+ sameKeys(cached.keys, keys) &&+ cached.target.format === image.format &&+ cached.target.size[0] === image.size[0] &&+ cached.target.size[1] === image.size[1],+ );+ if (cached) {+ live.add(cached.target);+ results.set(image, cached.target);+ return;+ }Object.values(image.inputs).forEach(visit);order.push(image);}// Requested outputs count as consumers, keeping their targets live.outputs.forEach(visit);- return { order, uses, live };+ return { order, uses, live, results };}const sizeKey = (image: { size: readonly number[]; format: string }) =>`${image.size[0]}x${image.size[1]} ${image.format}`;-/** Owns effects, storage buffers, and transient targets for one renderer. */+/** Owns effects, storage buffers, transient targets, and cached results for one renderer. */export function createRenderGraph(gpu: Gpu, timer?: Timer) {const pool: Target[] = [];/**@@ -58,6 +85,7 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {*/let recent: Map<string, number>[] = [];const effects = new Map<string, Pass>();+ const caches = new Map<string, Cached[]>();let passes: string[] = [];let disposed = false;function prepare(node: RenderNode) {@@ -117,9 +145,29 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {pool.push(image);return image;}+ function acquireCached(node: RenderNode, live: ReadonlySet<Target>) {+ const entries = caches.get(node.name);+ const oldest = entries?.length === 2 ? entries.pop() : undefined;+ if (oldest) {+ if (+ !live.has(oldest.target) &&+ sizeKey(oldest.target) === sizeKey(node)+ ) {+ return oldest.target;+ }+ // An evicted result can still be an input to this frame; the pool respects its lifetime.+ pool.push(oldest.target);+ }+ return target(gpu, { size: node.size, format: node.format });+ }return {/** Retire removed composition instances; bypassed instances remain reusable. */release(prefix: string) {+ for (const [name, entries] of caches) {+ if (!name.startsWith(prefix)) continue;+ for (const { target } of entries) target.color.dispose();+ caches.delete(name);+ }for (const [name, pass] of effects) {if (name.startsWith(prefix)) {for (const { buffer } of pass.buffers.values()) {@@ -133,8 +181,7 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {if (disposed) {throw Error("Render graph is closed.");}- const { order, uses, live } = plan(outputs);- const results = new Map<RenderNode, Target>();+ const { order, uses, live, results } = plan(outputs, caches);function resolve(image: RenderImage): Target {if (!("inputs" in image)) {return image.target;@@ -158,10 +205,17 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {]),),});- const output = acquire(node, live);+ const output = node.cacheKeys+ ? acquireCached(node, live)+ : acquire(node, live);live.add(output);- written.add(output);+ if (!node.cacheKeys) written.add(output);frame.pass({ target: output, timer: timer?.span(node.name) }, pass);+ if (node.cacheKeys) {+ const entries = caches.get(node.name) ?? [];+ entries.unshift({ keys: node.cacheKeys, target: output });+ caches.set(node.name, entries);+ }results.set(node, output);for (const input of Object.values(node.inputs)) {const remaining = (uses.get(input) ?? 0) - 1;@@ -221,6 +275,12 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {size: [...size],format,})),+ cachedTextures: [...caches.values()].flatMap((entries) =>+ entries.map(({ target }) => ({+ size: [...target.size],+ format: target.format,+ })),+ ),};},dispose() {@@ -231,6 +291,10 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {}}effects.clear();+ for (const entries of caches.values()) {+ for (const { target } of entries) target.color.dispose();+ }+ caches.clear();for (const image of pool) {image.color.dispose();}diff --git a/src/core/renderer/index.ts b/src/core/renderer/index.tsindex 830f646..01537ce 100644--- a/src/core/renderer/index.ts+++ b/src/core/renderer/index.ts@@ -13,7 +13,12 @@ import type { Point } from "@/core/image/frame";import { createRenderGraph } from "./graph";import { createMaskCoverage } from "./mask";import { createPatchRaster, type PatchInput } from "./mask/patches";-import { input, type RenderImage } from "./node";+import {+ type CacheKey,+ input,+ type RenderImage,+ type RenderNode,+} from "./node";import {type AcceptPainting,createPaintRaster,@@ -34,6 +39,7 @@ export {} from "./display";export type { PatchInput } from "./mask/patches";export {+ type CacheKey,input,merge,type NodeDefinition,@@ -52,6 +58,8 @@ export { transformImages } from "./transform";export { createRenderGraph };export type Composition = {+ /** Cache a derived image by its immutable content, RAW revision, and proxy factor. */+ cache: (image: RenderNode, dependencies: readonly CacheKey[]) => RenderNode;inputId?: string;/** The mask whose ranges' image, the one below it, to keep for picking colors from it. */rangeSourceId?: string;@@ -65,7 +73,7 @@ export type Composition = {/** Rasterized paint of a paint layer, prepared before composition. */paint: (layer: PaintLayer) => PaintInput | undefined;/** Rasterized coverage of an effect's own stroke, such as a Healing patch. */- patch: (id: string, stroke: BrushStroke) => PatchInput;+ patch: (id: string, strokes: readonly BrushStroke[]) => PatchInput;};/** App composition describes requested outputs; the engine owns their storage. */@@ -173,6 +181,10 @@ export function createRenderer(}}const images = compose(image, scene, {+ cache: (image, dependencies) => ({+ ...image,+ cacheKeys: [version, factor, ...dependencies],+ }),inputId,rangeSourceId,retain: (id) => active.add(id),@@ -182,7 +194,7 @@ export function createRenderer(show: (id, coverage) => covered.set(id, coverage),}),paint: (layer) => paints.input(layer),- patch: (id, stroke) => patches.patch(id, stroke, developed.size),+ patch: (id, strokes) => patches.patch(id, strokes, developed.size),});for (const id of instances) {if (!active.has(id)) graph.release(`${id}/`);diff --git a/src/core/renderer/mask/patches.ts b/src/core/renderer/mask/patches.tsindex 5a687fa..349e68c 100644--- a/src/core/renderer/mask/patches.ts+++ b/src/core/renderer/mask/patches.ts@@ -3,7 +3,7 @@ import type { BrushStroke } from "@/core/document";import type { Point } from "@/core/image/frame";import { input, type RenderInput } from "@/core/renderer/node";import { createRasterCache } from "@/core/renderer/raster-cache";-import { extendsStroke, type Strokes } from "@/core/renderer/strokes";+import { extendsStrokes, type Strokes } from "@/core/renderer/strokes";import type { DabWalk } from "@/core/renderer/strokes/dabs";type Size = readonly [number, number];@@ -13,7 +13,7 @@ const tile = 256;type Patch = {target: Target;origin: Point;- stroke?: BrushStroke;+ strokes?: readonly BrushStroke[];/** Where the stroke's dabs stopped, to go on from there as it grows. */walk?: DabWalk;};@@ -22,16 +22,27 @@ type Patch = {export type PatchInput = { coverage: RenderInput; origin: Point };/** The tiles a stroke's dabs reach, within the photo. */-function strokeTiles(stroke: BrushStroke, [width, height]: Size) {- const radius = stroke.size / 2 + 1;- const xs = stroke.points.map(([x]) => x);- const ys = stroke.points.map(([, y]) => y);+function strokeTiles(strokes: readonly BrushStroke[], [width, height]: Size) {+ let left = width;+ let top = height;+ let right = 0;+ let bottom = 0;+ for (const stroke of strokes) {+ if (stroke.mode === "erase") continue;+ const radius = stroke.size / 2 + 1;+ for (const [x, y] of stroke.points) {+ left = Math.min(left, x - radius);+ top = Math.min(top, y - radius);+ right = Math.max(right, x + radius);+ bottom = Math.max(bottom, y + radius);+ }+ }const snap = (value: number, round: (value: number) => number, end: number) =>Math.min(end, Math.max(0, round(value / tile) * tile));- const left = snap(Math.min(...xs) - radius, Math.floor, width);- const top = snap(Math.min(...ys) - radius, Math.floor, height);- const right = snap(Math.max(...xs) + radius, Math.ceil, width);- const bottom = snap(Math.max(...ys) + radius, Math.ceil, height);+ left = snap(left, Math.floor, width);+ top = snap(top, Math.floor, height);+ right = snap(right, Math.ceil, width);+ bottom = snap(bottom, Math.ceil, height);return {origin: [left, top] as Point,size: [Math.max(1, right - left), Math.max(1, bottom - top)] as const,@@ -49,28 +60,31 @@ export function createPatchRaster(gpu: Gpu, strokes: Strokes) {origin: [0, 0],}));return {- patch(id: string, stroke: BrushStroke, size: Size): PatchInput {- const tiles = strokeTiles(stroke, size);+ patch(id: string, painted: readonly BrushStroke[], size: Size): PatchInput {+ const tiles = strokeTiles(painted, size);const patch = patches.reserve(id, tiles.size);const moved =patch.origin[0] !== tiles.origin[0] ||patch.origin[1] !== tiles.origin[1];- if (moved || !patch.stroke || !extendsStroke(patch.stroke, stroke)) {+ if (moved || !patch.strokes || !extendsStrokes(patch.strokes, painted)) {strokes.clear(patch.target);Object.assign(patch, {origin: tiles.origin,- stroke: undefined,+ strokes: undefined,walk: undefined,});}- if (patch.stroke !== stroke) {- patch.walk = strokes.stamp(- patch.target,- stroke,- patch.walk,- patch.origin,- );- patch.stroke = stroke;+ if (patch.strokes !== painted) {+ const from = Math.max(0, (patch.strokes?.length ?? 0) - 1);+ for (let i = from; i < painted.length; i++) {+ patch.walk = strokes.stamp(+ patch.target,+ painted[i],+ i === from ? patch.walk : undefined,+ patch.origin,+ );+ }+ patch.strokes = painted;}return { coverage: input(patch.target), origin: patch.origin };},diff --git a/src/core/renderer/node.ts b/src/core/renderer/node.tsindex fc25571..1e84855 100644--- a/src/core/renderer/node.ts+++ b/src/core/renderer/node.ts@@ -1,6 +1,10 @@import type { EffectOptions, ShaderSource, Target } from "vgpu";+export type CacheKey = object | number | string | boolean | undefined;+type NodeOptions = {+ /** Immutable content dependencies for an expensive, retained result. */+ readonly cacheKeys?: readonly CacheKey[];/** Shares state across passes whose shader and non-input bindings are identical. */readonly instance?: string;readonly set?: EffectOptions["set"];diff --git a/src/features/heal/anchor.tsx b/src/features/heal/anchor.tsxindex 78feb87..f26b3c7 100644--- a/src/features/heal/anchor.tsx+++ b/src/features/heal/anchor.tsx@@ -11,11 +11,13 @@ type Drag = {/** The value when the drag began; a live edit changes the rendered value under the pointer. */from: Point;next: Point;+ unsubscribe: () => void;};/** A dragged value: the destination's first point or the donor offset, changed as one history edit. */export type AnchorDrag = {from: Point;+ editOnRelease?: boolean;/** Follows the pointer; no value ends the drag. */onDrag: (next?: Point) => void;onDrop: (next: Point) => void;@@ -69,24 +71,43 @@ function DraggedAnchor({return mapping.toDocument(event.clientX, event.clientY, box);}function cancel() {- if (!current.current) return;+ const active = current.current;+ if (!active) return;current.current = undefined;+ active.unsubscribe();drag.onDrag();end(false);}- useEffect(() => () => end(false), []);+ useEffect(() => () => cancel(), []);function start(event: PointerEvent<SVGCircleElement>) {const box = camera.ref.current?.getBoundingClientRect();- if (event.button !== 0 || !event.isPrimary || camera.panMode || !box) {+ if (+ current.current ||+ event.button !== 0 ||+ !event.isPrimary ||+ camera.panMode ||+ event.shiftKey ||+ event.altKey ||+ !box+ ) {return;}- opened.current = document.history.begin();+ opened.current = !drag.editOnRelease && document.history.begin();current.current = {pointer: event.pointerId,box,start: point(event, box),from: drag.from,next: drag.from,+ unsubscribe: document.history.status.subscribe((state, previous) => {+ if (+ !state.editing &&+ (previous.editing ||+ state.undoCount !== previous.undoCount ||+ state.redoCount !== previous.redoCount)+ )+ cancel();+ }),};event.preventDefault();event.stopPropagation();@@ -108,6 +129,7 @@ function DraggedAnchor({const active = current.current;if (!active || active.pointer !== event.pointerId) return;current.current = undefined;+ active.unsubscribe();event.stopPropagation();event.currentTarget.releasePointerCapture(event.pointerId);const moved =@@ -122,18 +144,27 @@ function DraggedAnchor({end(true);}return (- <circle- {...{ [`data-heal-${kind}-handle`]: "true" }}- data-hide-brush-cursor="true"- cx={center[0]}- cy={center[1]}- {...marker}- className="pointer-events-auto cursor-grab active:cursor-grabbing"- onPointerDown={start}- onPointerMove={move}- onPointerUp={finish}- onPointerCancel={cancel}- onLostPointerCapture={cancel}- />+ <g>+ <circle+ cx={center[0]}+ cy={center[1]}+ {...marker}+ className="pointer-events-none"+ />+ <circle+ {...{ [`data-heal-${kind}-handle`]: "true" }}+ data-hide-brush-cursor="true"+ cx={center[0]}+ cy={center[1]}+ r={14}+ fill="transparent"+ className="pointer-events-auto cursor-grab active:cursor-grabbing"+ onPointerDown={start}+ onPointerMove={move}+ onPointerUp={finish}+ onPointerCancel={cancel}+ onLostPointerCapture={cancel}+ />+ </g>);}diff --git a/src/features/heal/controls.tsx b/src/features/heal/controls.tsxindex daa3356..4fe5772 100644--- a/src/features/heal/controls.tsx+++ b/src/features/heal/controls.tsx@@ -3,22 +3,22 @@ import { ListItem, ListItemAction } from "@roprgm/ui/list-item";import { Menu, MenuContent, MenuItem, MenuTrigger } from "@roprgm/ui/menu";import { CoverageThumbnail } from "@/components/editor/coverage-thumbnail";import { useDocument } from "@/components/editor/session";-import { HealIcon } from "@/components/icons/heal";import { Icon } from "@/components/icons/icon";import type { HealPatch } from "@/core/document";import { deleteHealPatch, duplicateHealPatch } from "./edits";import { useHealing } from "./mode";+import { PatchModeIcon } from "./mode-buttons";import { patchThumbnailRegion } from "./model";/** A patch still finding its donor has no raster yet. */-function PendingThumbnail() {+function PendingThumbnail({ mode }: { mode: HealPatch["mode"] }) {return (<spanrole="img"aria-label="Patch pending"className="grid size-8 shrink-0 place-items-center rounded-sm border border-level-8 bg-level-1 text-secondary">- <HealIcon className="size-4" />+ <PatchModeIcon mode={mode} className="size-4" /></span>);}@@ -76,7 +76,7 @@ function PatchActions({/** The brush size, and the opacity once lowered; the donor is edited on the canvas. */function patchSummary(patch: HealPatch) {- const size = `${Math.round(patch.stroke.size)} px`;+ const size = `${Math.round(patch.strokes[0].size)} px`;return patch.opacity < 1? `${size} · ${Math.round(patch.opacity * 100)}%`: size;@@ -116,17 +116,24 @@ export function HealControls({type="button"aria-label={`Select patch ${index + 1}`}aria-pressed={selectedPatch === patch.id}- onClick={() => selectPatch(patch.id)}+ onClick={(event) => {+ selectPatch(patch.id);+ event.currentTarget.blur();+ }}className="flex min-w-0 flex-1 items-center gap-2 self-stretch px-2 text-left"><CoverageThumbnailid={`layer/${id}/${patch.id}`}version={patch}- region={patchThumbnailRegion(patch.stroke, source.image.size)}+ region={patchThumbnailRegion(patch.strokes, source.image.size)}label="Patch shape"- fallback={<PendingThumbnail />}+ fallback={<PendingThumbnail mode={patch.mode} />}/><span className="min-w-0 flex-1 truncate">Patch {index + 1}</span>+ <PatchModeIcon+ mode={patch.mode}+ className="size-3.5 shrink-0 text-secondary"+ /><span className="shrink-0 text-secondary tabular-nums">{patchSummary(patch)}</span>diff --git a/src/features/heal/coverage.wgsl b/src/features/heal/coverage.wgslnew file mode 100644index 0000000..5408b24--- /dev/null+++ b/src/features/heal/coverage.wgsl@@ -0,0 +1,26 @@+// Softens the hard patch coverage inward by `radius`, never outward.+export fn patchCoverage(+ texture: texture_2d<f32>,+ textureSampler: sampler,+ p: vec2f,+ dimensions: vec2f,+ radius: f32,+) -> f32 {+ let uv = p / dimensions;+ let center = textureSampleLevel(texture, textureSampler, uv, 0.0).r;+ if (radius < 0.5) {+ return center;+ }+ let directions = array(+ vec2f(1.0, 0.0), vec2f(-1.0, 0.0), vec2f(0.0, 1.0), vec2f(0.0, -1.0),+ vec2f(0.707, 0.707), vec2f(-0.707, 0.707), vec2f(0.707, -0.707), vec2f(-0.707, -0.707),+ );+ var total = center * 4.0;+ for (var i = 0u; i < 8u; i++) {+ let direction = directions[i] * radius / dimensions;+ total += textureSampleLevel(texture, textureSampler, uv + direction * 0.35, 0.0).r * 2.0;+ total += textureSampleLevel(texture, textureSampler, uv + direction * 0.85, 0.0).r;+ }+ // Feather only the covered side of the hard patch boundary.+ return min(center, total / 28.0);+}diff --git a/src/features/heal/edits.ts b/src/features/heal/edits.tsindex 235ae2d..842df37 100644--- a/src/features/heal/edits.ts+++ b/src/features/heal/edits.ts@@ -2,26 +2,34 @@ import {type BrushStroke,type EditorDocument,editLayer,+ type HealMode,type HealPatch,type Layer,type StrokePoint,} from "@/core/document";-import { strokePoints } from "@/core/document/brush";+import { strokePoints, strokeSchema } from "@/core/document/brush";import type { Point } from "@/core/image/frame";import { parse, point } from "@/lib/parse";-import { patchBlend, patchStroke } from "./model";+import { donorModeSchema, patchBlend, patchStroke } from "./model";++function healPatches(layer: Layer) {+ if (layer.kind !== "heal") throw Error("Select a Healing layer.");+ return layer.patches;+}export function addHealPatch(document: EditorDocument,id: string,stroke: BrushStroke,offset: Point,+ mode: Exclude<HealMode, "remove"> = "heal",) {const painted = parse(patchStroke, stroke, "Invalid heal stroke");const patch: HealPatch = {id: crypto.randomUUID(),+ mode: parse(donorModeSchema, mode, "Invalid heal mode"),feather: painted.feather,- stroke: { ...painted, feather: 0 },+ strokes: [{ ...painted, feather: 0, flow: 1 }],opacity: 1,offset: parse(point, offset, "Invalid heal source"),};@@ -32,9 +40,24 @@ export function addHealPatch(return patch.id;}-function healPatches(layer: Layer) {- if (layer.kind !== "heal") throw Error("Select a Healing layer.");- return layer.patches;+export function addRemovePatch(+ document: EditorDocument,+ id: string,+ stroke: BrushStroke,+) {+ const painted = parse(patchStroke, stroke, "Invalid remove stroke");+ const patch: HealPatch = {+ id: crypto.randomUUID(),+ mode: "remove",+ feather: painted.feather,+ strokes: [{ ...painted, feather: 0, flow: 1 }],+ opacity: 1,+ };+ editLayer(document, id, (layer) => ({+ ...layer,+ patches: [...healPatches(layer), patch],+ }));+ return patch.id;}/** Rewrites the patch list around one patch, found by id. */@@ -52,7 +75,23 @@ function editPatches(});}-/** Edits one patch's blend; its shape and donor stay as painted. */+/** Adds or subtracts a separate stroke without changing the patch's donor or blend. */+export function addHealStroke(+ document: EditorDocument,+ id: string,+ patchId: string,+ stroke: BrushStroke,+) {+ const painted = parse(strokeSchema, stroke, "Invalid heal stroke");+ editPatches(document, id, patchId, (patches, index) =>+ patches.with(index, {+ ...patches[index],+ strokes: [...patches[index].strokes, { ...painted, feather: 0, flow: 1 }],+ }),+ );+}++/** Edits the blend while preserving the recorded repair geometry. */export function setHealPatch(document: EditorDocument,id: string,@@ -110,10 +149,13 @@ export function extendHealPatch(index === layer.patches.length - 1? {...patch,- stroke: {- ...patch.stroke,- points: [...patch.stroke.points, ...added],- },+ strokes: patch.strokes.with(-1, {+ ...patch.strokes[patch.strokes.length - 1],+ points: [+ ...patch.strokes[patch.strokes.length - 1].points,+ ...added,+ ],+ }),}: patch,),@@ -128,12 +170,14 @@ export function setHealSource(offset: Point,) {const source = parse(point, offset, "Invalid heal source");- editPatches(document, id, patchId, (patches, index) =>- patches.with(index, { ...patches[index], offset: source }),- );+ editPatches(document, id, patchId, (patches, index) => {+ const patch = patches[index];+ if (patch.mode === "remove") throw Error("Remove patches have no donor.");+ return patches.with(index, { ...patch, offset: source });+ });}-/** Moves a patch while its donor stays fixed. */+/** Moves a patch; a Heal/Clone donor stays fixed. */export function setHealDestination(document: EditorDocument,id: string,@@ -143,16 +187,21 @@ export function setHealDestination(const [targetX, targetY] = parse(point, destination, "Invalid heal target");editPatches(document, id, patchId, (patches, index) => {const patch = patches[index];- const [x, y] = patch.stroke.points[0];+ const [x, y] = patch.strokes[0].points[0];const delta: Point = [targetX - x, targetY - y];- return patches.with(index, {+ const moved = {...patch,- stroke: {- ...patch.stroke,- points: patch.stroke.points.map(+ strokes: patch.strokes.map((stroke) => ({+ ...stroke,+ points: stroke.points.map(([x, y, pressure]) => [x + delta[0], y + delta[1], pressure] as const,),- },+ })),+ };+ if (patch.mode === "remove") return patches.with(index, moved);+ return patches.with(index, {+ ...moved,+ mode: patch.mode,offset: [patch.offset[0] - delta[0], patch.offset[1] - delta[1]],});});diff --git a/src/features/heal/heal.wgsl b/src/features/heal/heal.wgslindex b21a700..d6ebbb0 100644--- a/src/features/heal/heal.wgsl+++ b/src/features/heal/heal.wgsl@@ -1,3 +1,4 @@+import { patchCoverage } from "./coverage.wgsl";struct Params {origin: vec2f,extent: vec2f,@@ -35,36 +36,10 @@ fn correction(uv: vec2f) -> vec3f {}return textureSampleLevel(previous, linearSampler, uv, 0.0).rgb;}-// Softens the hard patch coverage inward by `radius`, never outward.-fn patchCoverage(- texture: texture_2d<f32>,- textureSampler: sampler,- p: vec2f,- dimensions: vec2f,- radius: f32,-) -> f32 {- let uv = p / dimensions;- let center = textureSampleLevel(texture, textureSampler, uv, 0.0).r;- if (radius < 0.5) {- return center;- }- let directions = array(- vec2f(1.0, 0.0), vec2f(-1.0, 0.0), vec2f(0.0, 1.0), vec2f(0.0, -1.0),- vec2f(0.707, 0.707), vec2f(-0.707, 0.707), vec2f(0.707, -0.707), vec2f(-0.707, -0.707),- );- var total = center * 4.0;- for (var i = 0u; i < 8u; i++) {- let direction = directions[i] * radius / dimensions;- total += textureSampleLevel(texture, textureSampler, uv + direction * 0.35, 0.0).r * 2.0;- total += textureSampleLevel(texture, textureSampler, uv + direction * 0.85, 0.0).r;- }- // Feather only the covered side of the hard patch boundary.- return min(center, total / 28.0);-}@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {let uv = position.xy / params.grid;- if (params.mode == 3u) {+ if (params.mode == 3u || params.mode == 4u) {let p = uv * params.dimensions;let original = color(p);if (any(p < params.origin) || any(p > params.origin + params.extent)) {@@ -75,6 +50,9 @@ fn patchCoverage(if (amount == 0.0 || any(donor < vec2f(0.5)) || any(donor > params.dimensions - 0.5)) {return original;}+ if (params.mode == 4u) {+ return vec4f(mix(original.rgb, color(donor).rgb, amount), original.a);+ }let donorColor = max(color(donor).rgb, vec3f(0.0));let delta = textureSampleLevel(previous, linearSampler, (p - params.origin) / params.extent, 0.0).rgb;// Bound the gain and damp chroma correction so a small dark channel cannotdiff --git a/src/features/heal/inpaint/algorithm.md b/src/features/heal/inpaint/algorithm.mdnew file mode 100644index 0000000..ae41d69--- /dev/null+++ b/src/features/heal/inpaint/algorithm.md@@ -0,0 +1,27 @@+# GPU object removal++`inpaint(source, coverage, region, name)` declares a pure render graph; it requires no React, document store, model weights, workers, or pixel readback. `pass.ts` caches only `inpaintField`'s bounded donor correspondence field (at most 4 MiB per revision), then calls `sampleInpaint` and blends the result into the scene. All image processing runs in WGSL; TypeScript only describes regions, levels, and passes. The working image remains linear Rec.2020, with HDR values and the destination alpha preserved by the final blend. The field stores `[dx, dy, cost, valid]` in `rgba32float`; offsets are in matching-grid texels. `sampleInpaint` returns donor RGB with validity in alpha for the compositor.++The algorithm follows non-local, multiscale texture synthesis with PatchMatch, adapted for parallel GPU execution. It is not a reproduction of every step of the IPOL reference and does not infer hidden semantic content.++1. Crop the accumulated patch with surrounding context: at least 32 source pixels, otherwise the largest painted brush diameter on every side, clipped to the image. Combine ordered paint/erase strokes in the shared GPU coverage raster; subtraction does not enlarge the region. Exclude the remaining painted object conservatively before creating a color pyramid.+2. Work on at most a 512 px long edge. Precompute logarithmic color once. Keep horizontal and vertical texture energy through the pyramid, so coarse colors do not erase fine texture differences.+3. Initialize from nearby wholly unmasked donors using jump flooding and grow the fill inward at the coarsest level; upsample and preserve the previous level's offsets. A donor's entire 7×7 footprint must be clean.+4. Alternate parallel PatchMatch propagation at jumps 8, 4, 2, 1 and eight shrinking-radius random proposals with weighted overlapping reconstruction. Use six iterations at the coarsest level, four at finer levels. Compare nine samples across each 7×7 footprint plus texture descriptors; increase reconstructed-region confidence as refinement proceeds.+5. Sample donors at whole source texels from the original source for the final image rather than exporting the averaged matching pyramid. Different pixels can use different offsets. Apply the existing inward feather, patch opacity, and original alpha in a separate blend.++Known, wholly clean patches skip search. Candidate comparison stops once its nonnegative partial error cannot beat the current score. Each search pass owns its uniforms; vgpu shares shader modules and pipelines, and the graph reuses temporary textures across iterations. Reconstruction passes share an effect because they have no uniforms. The graph retains only a bounded correspondence field for opacity/feather edits and separates RAW/proxy revisions. Random proposals are deterministic for the same inputs.++## Limits++Small defects in uniform or recurring texture and locally supported boundaries work best. Large missing regions, perspective, unique structures, and several competing backgrounds are ambiguous. Straight edges can continue when they have good local donors; mortar, rails, text, faces, and other precise structures can break or repeat. The local search cannot recover content that only appears elsewhere in the photo. A coarse matching grid can lose features smaller than its texel spacing; output sampling preserves donor detail but cannot recover correspondence detail lost during matching. If there are no clean donors, the affected pixels remain unchanged.++The user should paint over the entire object, including its halo or shadow. Pixels left outside the stroke are treated as valid context. A larger selection is not necessarily easier: it removes more evidence. Adding distant strokes to one patch expands its bounding region and makes the shared matching grid coarser; separate patches preserve more detail for distant spots.++## Verification and measurement++`tests/inpaint-gpu.ts` creates known flat, gradient, periodic, edge, and brick backgrounds, allowing error measurement against a clean reference. Browser tests check actual removal, HDR/alpha, untouched pixels, proxy rendering, cache invalidation/release, unavailable donors, and the H/paint/export/undo workflow. Scene files round-trip Remove patches without offsets. `tests/inpaint-photo.ts` runs decoding, removal, and export on the demo photograph without mounting React.++See [PERFORMANCE.md](../../../../PERFORMANCE.md#object-removal) for standalone and integrated benchmarks, including an unchanged-patch cache workload. Report the actual adapter; software-backend timings do not predict physical-GPU latency.++Algorithm references: [Newson et al., non-local multiscale inpainting](https://www.ipol.im/pub/art/2017/189/), [Barnes et al., PatchMatch](https://pixl.cs.princeton.edu/gfx/pubs/Barnes_2009_PAR/index.php).diff --git a/src/features/heal/inpaint/blend.wgsl b/src/features/heal/inpaint/blend.wgslnew file mode 100644index 0000000..a0f47d4--- /dev/null+++ b/src/features/heal/inpaint/blend.wgsl@@ -0,0 +1,21 @@+import { patchCoverage } from "../coverage.wgsl";+struct Params {+ origin: vec2f, extent: vec2f, dimensions: vec2f, grid: vec2f,+ coverageOrigin: vec2f, coverageSize: vec2f, feather: f32, opacity: f32,+}+@group(0) @binding(0) var source: texture_2d<f32>;+@group(0) @binding(1) var coverage: texture_2d<f32>;+@group(0) @binding(2) var filled: texture_2d<f32>;+@group(0) @binding(3) var linearSampler: sampler;+@group(0) @binding(4) var<uniform> params: Params;+@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {+ let p = position.xy / params.grid * params.dimensions;+ let original = textureSampleLevel(source, linearSampler, p / params.dimensions, 0.0);+ let uv = (p - params.coverageOrigin) / params.coverageSize;+ if (any(uv < vec2f(0.0)) || any(uv > vec2f(1.0))) { return original; }+ let hard = textureSampleLevel(coverage, linearSampler, uv, 0.0).r;+ if (hard == 0.0) { return original; }+ let feathered = patchCoverage(coverage, linearSampler, p - params.coverageOrigin, params.coverageSize, params.feather);+ let replacement = textureSampleLevel(filled, linearSampler, (p - params.origin) / params.extent, 0.0);+ return vec4f(mix(original.rgb, replacement.rgb, feathered * params.opacity * replacement.a), original.a);+}diff --git a/src/features/heal/inpaint/features.wgsl b/src/features/heal/inpaint/features.wgslnew file mode 100644index 0000000..6caec17--- /dev/null+++ b/src/features/heal/inpaint/features.wgsl@@ -0,0 +1,42 @@+struct Params { coarse: u32 }+@group(0) @binding(0) var source: texture_2d<f32>;+@group(0) @binding(1) var fine: texture_2d<f32>;+@group(0) @binding(2) var<uniform> params: Params;+fn sampleSource(p: vec2i) -> vec4f {+ return textureLoad(source, clamp(p, vec2i(0), vec2i(textureDimensions(source)) - 1), 0);+}+fn light(color: vec3f) -> f32 {+ return dot(color, vec3f(0.2627, 0.6780, 0.0593));+}+// Texture descriptors survive coarse color averaging; alpha marks wholly clean donor patches.+@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {+ let p = vec2i(position.xy);+ let end = vec2i(textureDimensions(source)) - 1;+ var eligible = 1.0;+ for (var y = -3; y <= 3; y++) {+ for (var x = -3; x <= 3; x++) {+ let q = p + vec2i(x, y);+ if (any(q < vec2i(0)) || any(q > end)) { eligible = 0.0; }+ eligible *= 1.0 - textureLoad(source, clamp(q, vec2i(0), end), 0).a;+ }+ }+ if (params.coarse != 0u) {+ let endFine = vec2i(textureDimensions(fine)) - 1;+ let energy = (textureLoad(fine, min(p * 2, endFine), 0).rg + textureLoad(fine, min(p * 2 + vec2i(1, 0), endFine), 0).rg + textureLoad(fine, min(p * 2 + vec2i(0, 1), endFine), 0).rg + textureLoad(fine, min(p * 2 + vec2i(1, 1), endFine), 0).rg) * 0.25;+ return vec4f(energy, textureLoad(source, p, 0).a, eligible);+ }+ var energy = vec2f(0.0);+ var count = vec2f(0.0);+ for (var y = -1; y <= 1; y++) {+ for (var x = -1; x <= 1; x++) {+ let q = p + vec2i(x, y);+ let center = sampleSource(q);+ let horizontal = sampleSource(q + vec2i(1, 0));+ let vertical = sampleSource(q + vec2i(0, 1));+ let valid = (1.0 - center.a) * (vec2f(1.0) - vec2f(horizontal.a, vertical.a));+ energy += abs(vec2f(light(horizontal.rgb), light(vertical.rgb)) - light(center.rgb)) * valid;+ count += valid;+ }+ }+ return vec4f(energy / max(count, vec2f(1.0)), textureLoad(source, p, 0).a, eligible);+}diff --git a/src/features/heal/inpaint/finish.wgsl b/src/features/heal/inpaint/finish.wgslnew file mode 100644index 0000000..8021228--- /dev/null+++ b/src/features/heal/inpaint/finish.wgsl@@ -0,0 +1,15 @@+struct Params { origin: vec2f, extent: vec2f, dimensions: vec2f, grid: vec2f, texel: vec2f }+@group(0) @binding(0) var source: texture_2d<f32>;+@group(0) @binding(1) var field: texture_2d<f32>;+@group(0) @binding(2) var linearSampler: sampler;+@group(0) @binding(3) var<uniform> params: Params;+@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {+ let uv = position.xy / params.grid;+ let p = params.origin + uv * params.extent;+ let cell = clamp(vec2i(uv * vec2f(textureDimensions(field))), vec2i(0), vec2i(textureDimensions(field)) - 1);+ let correspondence = textureLoad(field, cell, 0);+ let offset = round(correspondence.xy / vec2f(textureDimensions(field)) * params.extent / params.texel) * params.texel;+ let donor = p + offset;+ let color = textureSampleLevel(source, linearSampler, donor / params.dimensions, 0.0);+ return vec4f(color.rgb, correspondence.w);+}diff --git a/src/features/heal/inpaint/index.ts b/src/features/heal/inpaint/index.tsnew file mode 100644index 0000000..015f94a--- /dev/null+++ b/src/features/heal/inpaint/index.ts@@ -0,0 +1,256 @@+import type { Point } from "@/core/image/frame";+import {+ merge,+ node,+ type PatchInput,+ type RenderImage,+ type RenderNode,+ sourceSize,+} from "@/core/renderer";+import featuresShader from "./features.wgsl";+import finishShader from "./finish.wgsl";+import matchShader from "./match.wgsl";+import nearestShader from "./nearest.wgsl";+import prepareShader from "./prepare.wgsl";+import pyramidShader from "./pyramid.wgsl";+import reconstructShader from "./reconstruct.wgsl";++export type InpaintRegion = { origin: Point; extent: Point };+const samplers = {+ linearSampler: { minFilter: "linear", magFilter: "linear" },+} as const;++function nearest(features: RenderImage, name: string) {+ let image = merge(+ { features, previous: features },+ node(`${name}/nearest/seed`, nearestShader, {+ format: "rgba32float",+ set: { params: { jump: 0 } },+ }),+ );+ for (+ let jump = 2 ** Math.ceil(Math.log2(Math.max(...features.size)));+ jump >= 1;+ jump /= 2+ ) {+ image = merge(+ { features, previous: image },+ node(`${name}/nearest/${jump}`, nearestShader, {+ format: "rgba32float",+ set: { params: { jump } },+ }),+ );+ }+ return image;+}++function reconstruct(+ source: RenderImage,+ field: RenderImage,+ name: string,+ instance: string,+) {+ return merge(+ { source, field },+ node(name, reconstructShader, {+ instance,+ }),+ );+}++function solve(+ source: RenderImage,+ features: RenderImage,+ coarse: RenderImage | undefined,+ name: string,+) {+ const closest = nearest(features, name);+ const params = {+ jump: 1,+ iteration: 0,+ initialize: 1,+ coarse: Number(Boolean(coarse)),+ confidence: 0,+ radius: coarse ? 1e10 : 4,+ };+ let iteration = 0;+ let field = merge(+ {+ source,+ features,+ nearest: closest,+ current: source,+ field: coarse ?? closest,+ },+ node(`${name}/initialize`, matchShader, {+ format: "rgba32float",+ set: { params },+ }),+ );+ let current = reconstruct(+ source,+ field,+ `${name}/iteration/seed`,+ `${name}/reconstruct`,+ );+ if (!coarse) {+ // Grow a reliable initialization inward before the global refinements.+ for (let radius = 7; radius <= Math.max(...source.size) + 3; radius += 3) {+ field = merge(+ { source, features, nearest: closest, current, field },+ node(`${name}/seed/${radius}`, matchShader, {+ format: "rgba32float",+ set: {+ params: {+ ...params,+ initialize: 0,+ radius,+ confidence: 1,+ iteration: ++iteration,+ },+ },+ }),+ );+ current = reconstruct(+ source,+ field,+ `${name}/seed/${radius}/vote`,+ `${name}/reconstruct`,+ );+ }+ }+ const iterations = coarse ? 4 : 6;+ for (let refinement = 0; refinement < iterations; refinement++) {+ for (const jump of [8, 4, 2, 1]) {+ field = merge(+ { source, features, nearest: closest, current, field },+ node(`${name}/iteration/${refinement}/match/${jump}`, matchShader, {+ format: "rgba32float",+ set: {+ params: {+ ...params,+ initialize: 0,+ radius: 1e10,+ iteration: ++iteration,+ jump,+ confidence: 0.2 + 0.8 * (refinement / iterations),+ },+ },+ }),+ );+ }+ current = reconstruct(+ source,+ field,+ `${name}/iteration/${refinement}/vote`,+ `${name}/reconstruct`,+ );+ }+ return field;+}++/** Multiscale, non-local synthesis. Every pixel operation stays on the GPU; no React or readback. */+export function inpaintField(+ source: RenderImage,+ patch: PatchInput,+ region: InpaintRegion,+ name: string,+): RenderNode {+ const dimensions = sourceSize(source);+ const factor = Math.max(...source.scale, Math.max(...region.extent) / 512);+ const size: Point = [+ Math.max(1, Math.ceil(region.extent[0] / factor)),+ Math.max(1, Math.ceil(region.extent[1] / factor)),+ ];+ const params = {+ ...region,+ dimensions,+ coverageOrigin: patch.origin,+ coverageSize: patch.coverage.size,+ grid: size,+ };+ const prepared = merge(+ { source, coverage: patch.coverage },+ node(`${name}/prepare`, prepareShader, { size, samplers, set: { params } }),+ );+ const pyramid: RenderImage[] = [prepared];+ while (Math.max(...pyramid[pyramid.length - 1].size) > 32) {+ const previous = pyramid[pyramid.length - 1];+ const size: Point = [+ Math.ceil(previous.size[0] / 2),+ Math.ceil(previous.size[1] / 2),+ ];+ pyramid.push(+ merge(+ { source: previous },+ node(`${name}/pyramid/${pyramid.length}`, pyramidShader, { size }),+ ),+ );+ }+ const descriptors: RenderImage[] = [];+ for (const [level, source] of pyramid.entries()) {+ descriptors.push(+ merge(+ { source, fine: descriptors[level - 1] ?? source },+ node(`${name}/level/${level}/features`, featuresShader, {+ set: { params: { coarse: Number(level > 0) } },+ }),+ ),+ );+ }+ let field: RenderNode | undefined;+ for (let level = pyramid.length - 1; level >= 0; level--) {+ field = solve(+ pyramid[level],+ descriptors[level],+ field,+ `${name}/level/${level}`,+ );+ }+ if (!field) throw Error("An inpainting pyramid is required.");+ return { ...field, name: `${name}/field` };+}++/** Sample whole source texels from a correspondence field, preserving original donor detail. */+export function sampleInpaint(+ source: RenderImage,+ field: RenderImage,+ region: InpaintRegion,+ name: string,+): RenderNode {+ const dimensions = sourceSize(source);+ const outputSize: Point = [+ Math.ceil(region.extent[0] / source.scale[0]),+ Math.ceil(region.extent[1] / source.scale[1]),+ ];+ return merge(+ { source, field },+ node(name, finishShader, {+ size: outputSize,+ samplers,+ set: {+ params: {+ ...region,+ dimensions,+ grid: outputSize,+ texel: source.scale,+ },+ },+ }),+ );+}++/** Standalone synthesis and reconstruction; app composition can cache just the bounded field. */+export function inpaint(+ source: RenderImage,+ patch: PatchInput,+ region: InpaintRegion,+ name: string,+): RenderNode {+ return sampleInpaint(+ source,+ inpaintField(source, patch, region, name),+ region,+ `${name}/result`,+ );+}diff --git a/src/features/heal/inpaint/match.wgsl b/src/features/heal/inpaint/match.wgslnew file mode 100644index 0000000..ee8ddc3--- /dev/null+++ b/src/features/heal/inpaint/match.wgsl@@ -0,0 +1,97 @@+struct Params {+ jump: i32,+ iteration: u32,+ initialize: u32,+ coarse: u32,+ confidence: f32,+ radius: f32,+}+@group(0) @binding(0) var source: texture_2d<f32>;+@group(0) @binding(1) var features: texture_2d<f32>;+@group(0) @binding(2) var current: texture_2d<f32>;+@group(0) @binding(3) var field: texture_2d<f32>;+@group(0) @binding(4) var nearest: texture_2d<f32>;+@group(0) @binding(5) var<uniform> params: Params;++fn random(seed: u32) -> f32 {+ var n = seed;+ n = (n ^ (n >> 16u)) * 0x7feb352du;+ n = (n ^ (n >> 15u)) * 0x846ca68bu;+ n = n ^ (n >> 16u);+ return f32(n & 0x00ffffffu) / 16777216.0;+}+fn descriptor(p: vec2i) -> vec2f {+ let feature = textureLoad(features, p, 0);+ if (feature.z < 0.5 || params.initialize != 0u) { return feature.rg; }+ let q = clamp(p + vec2i(textureLoad(field, p, 0).xy), vec2i(0), vec2i(textureDimensions(features)) - 1);+ return textureLoad(features, q, 0).rg;+}+fn distance(p: vec2i, q: vec2i, limit: f32) -> f32 {+ let end = vec2i(textureDimensions(source)) - 1;+ if (any(q < vec2i(3)) || any(q > end - 3) || textureLoad(features, q, 0).a < 0.5) { return 1e10; }+ var error = 0.0;+ var weight = 0.0;+ for (var y = -3; y <= 3; y += 3) {+ for (var x = -3; x <= 3; x += 3) {+ let a = clamp(p + vec2i(x, y), vec2i(0), end);+ let b = q + vec2i(x, y);+ let masked = textureLoad(source, a, 0).a;+ let reconstructed = select(0.0, params.confidence, textureLoad(field, a, 0).w > 0.5);+ let confidence = select(1.0, reconstructed, masked > 0.5);+ let destination = textureLoad(current, a, 0).rgb;+ let donor = textureLoad(source, b, 0).rgb;+ let delta = destination - donor;+ let textureDelta = descriptor(a) - textureLoad(features, b, 0).rg;+ error += confidence * (dot(delta, delta) + 2.0 * dot(textureDelta, textureDelta));+ weight += confidence;+ if (error > limit * 9.0) { return 1e10; }+ }+ }+ // A weak spatial preference resolves flat-region ties without overruling appearance.+ let offset = vec2f(q - p) / vec2f(textureDimensions(source));+ return error / max(weight, 0.001) + 0.0001 * dot(offset, offset);+}+fn consider(p: vec2i, offset: vec2i, best: vec4f) -> vec4f {+ let score = distance(p, p + offset, best.z);+ if (score < best.z) { return vec4f(vec2f(offset), score, 1.0); }+ return best;+}+@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {+ let p = vec2i(position.xy);+ let size = vec2i(textureDimensions(source));+ if (textureLoad(features, p, 0).a > 0.5) { return vec4f(0.0, 0.0, 0.0, 1.0); }+ if (textureLoad(source, p, 0).a > 0.5 && sqrt(textureLoad(nearest, p, 0).z) > params.radius) { return vec4f(0.0, 0.0, 1e10, 0.0); }+ var best = vec4f(0.0, 0.0, 1e10, 0.0);+ if (params.initialize != 0u) {+ if (params.coarse != 0u) {+ let scale = vec2f(size) / vec2f(textureDimensions(field));+ let inherited = textureLoad(field, vec2i(vec2f(p) / scale), 0);+ if (inherited.w > 0.5) { best = consider(p, vec2i(round(inherited.xy * scale)), best); }+ // Keep the coarse solution inside the hole instead of resetting it to nearest texture.+ if (best.w > 0.5 && textureLoad(source, p, 0).a > 0.5) { return best; }+ }+ } else {+ best = consider(p, vec2i(textureLoad(field, p, 0).xy), best);+ let directions = array(vec2i(1, 0), vec2i(-1, 0), vec2i(0, 1), vec2i(0, -1));+ for (var i = 0u; i < 4u; i++) {+ let neighbor = p + directions[i] * params.jump;+ if (all(neighbor >= vec2i(0)) && all(neighbor < size)) {+ best = consider(p, vec2i(textureLoad(field, neighbor, 0).xy), best);+ }+ }+ }+ if (best.w < 0.5) {+ let closest = textureLoad(nearest, p, 0);+ if (closest.w > 0.5) { best = consider(p, vec2i(closest.xy) - p, best); }+ }+ let seed = u32(p.x) * 1973u + u32(p.y) * 9277u + params.iteration * 26699u;+ var radius = f32(max(size.x, size.y));+ for (var i = 0u; i < 8u; i++) {+ let noise = vec2f(random(seed + i * 101u), random(seed + i * 101u + 37u)) * 2.0 - 1.0;+ let center = select(vec2f(p) + best.xy, vec2f(size) * 0.5, params.initialize != 0u || best.w < 0.5);+ let candidate = vec2i(round(center + noise * radius));+ best = consider(p, candidate - p, best);+ radius *= 0.5;+ }+ return best;+}diff --git a/src/features/heal/inpaint/nearest.wgsl b/src/features/heal/inpaint/nearest.wgslnew file mode 100644index 0000000..fb275b5--- /dev/null+++ b/src/features/heal/inpaint/nearest.wgsl@@ -0,0 +1,23 @@+struct Params { jump: i32 }+@group(0) @binding(0) var features: texture_2d<f32>;+@group(0) @binding(1) var previous: texture_2d<f32>;+@group(0) @binding(2) var<uniform> params: Params;+@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {+ let p = vec2i(position.xy);+ if (params.jump == 0) {+ return select(vec4f(-1.0, -1.0, 1e10, 0.0), vec4f(vec2f(p), 0.0, 1.0), textureLoad(features, p, 0).a > 0.5);+ }+ let end = vec2i(textureDimensions(previous)) - 1;+ var best = textureLoad(previous, p, 0);+ for (var y = -1; y <= 1; y++) {+ for (var x = -1; x <= 1; x++) {+ let q = p + vec2i(x, y) * params.jump;+ if (any(q < vec2i(0)) || any(q > end)) { continue; }+ var candidate = textureLoad(previous, q, 0);+ let delta = candidate.xy - vec2f(p);+ candidate.z = dot(delta, delta);+ if (candidate.w > 0.5 && (best.w < 0.5 || candidate.z < best.z)) { best = candidate; }+ }+ }+ return best;+}diff --git a/src/features/heal/inpaint/pass.ts b/src/features/heal/inpaint/pass.tsnew file mode 100644index 0000000..4fb9a2d--- /dev/null+++ b/src/features/heal/inpaint/pass.ts@@ -0,0 +1,53 @@+import type { HealPatch } from "@/core/document";+import {+ type CacheKey,+ type Composition,+ merge,+ node,+ type PatchInput,+ type RenderImage,+ sourceSize,+} from "@/core/renderer";+import { patchBounds } from "@/features/heal/model";+import { inpaintField, sampleInpaint } from ".";+import shader from "./blend.wgsl";++export function removePatch(+ source: RenderImage,+ coverage: PatchInput,+ patch: Extract<HealPatch, { mode: "remove" }>,+ name: string,+ cache: Composition["cache"],+ dependencies: readonly CacheKey[],+) {+ const dimensions = sourceSize(source);+ const margin = patch.strokes.reduce((margin, stroke) => {+ if (stroke.mode === "erase") return margin;+ return Math.max(margin, stroke.size);+ }, 32);+ const region = patchBounds(patch.strokes, dimensions, margin);+ const field = cache(+ inpaintField(source, coverage, region, `${name}/inpaint`),+ [...dependencies, patch.strokes],+ );+ const filled = sampleInpaint(source, field, region, `${name}/sample`);+ return merge(+ { source, coverage: coverage.coverage, filled },+ node(`${name}/blend`, shader, {+ samplers: {+ linearSampler: { minFilter: "linear", magFilter: "linear" },+ },+ set: {+ params: {+ ...region,+ dimensions,+ grid: source.size,+ coverageOrigin: coverage.origin,+ coverageSize: coverage.coverage.size,+ feather: (patch.strokes[0].size * patch.feather) / 2,+ opacity: patch.opacity,+ },+ },+ }),+ );+}diff --git a/src/features/heal/inpaint/prepare.wgsl b/src/features/heal/inpaint/prepare.wgslnew file mode 100644index 0000000..ae77543--- /dev/null+++ b/src/features/heal/inpaint/prepare.wgsl@@ -0,0 +1,29 @@+struct Params {+ origin: vec2f,+ extent: vec2f,+ dimensions: vec2f,+ coverageOrigin: vec2f,+ coverageSize: vec2f,+ grid: vec2f,+}+@group(0) @binding(0) var source: texture_2d<f32>;+@group(0) @binding(1) var coverage: texture_2d<f32>;+@group(0) @binding(2) var linearSampler: sampler;+@group(0) @binding(3) var<uniform> params: Params;++@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {+ let p = params.origin + position.xy / params.grid * params.extent;+ let footprint = max(vec2f(1.0), params.extent / params.grid * 0.5);+ var masked = 0.0;+ for (var y = -1; y <= 1; y++) {+ for (var x = -1; x <= 1; x++) {+ let q = p + vec2f(f32(x), f32(y)) * footprint;+ let uv = (q - params.coverageOrigin) / params.coverageSize;+ if (all(uv >= vec2f(0.0)) && all(uv <= vec2f(1.0))) {+ masked = max(masked, textureSampleLevel(coverage, linearSampler, uv, 0.0).r);+ }+ }+ }+ let color = textureSampleLevel(source, linearSampler, p / params.dimensions, 0.0).rgb;+ return vec4f(select(log(vec3f(1.0) + max(color, vec3f(0.0))), vec3f(0.0), masked > 0.001), select(0.0, 1.0, masked > 0.001));+}diff --git a/src/features/heal/inpaint/pyramid.wgsl b/src/features/heal/inpaint/pyramid.wgslnew file mode 100644index 0000000..3f71e88--- /dev/null+++ b/src/features/heal/inpaint/pyramid.wgsl@@ -0,0 +1,18 @@+@group(0) @binding(0) var source: texture_2d<f32>;+@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {+ let p = vec2i(position.xy) * 2;+ let end = vec2i(textureDimensions(source)) - 1;+ var color = vec3f(0.0);+ var weight = 0.0;+ var mask = 0.0;+ for (var y = 0; y < 2; y++) {+ for (var x = 0; x < 2; x++) {+ let sample = textureLoad(source, min(p + vec2i(x, y), end), 0);+ let known = 1.0 - sample.a;+ color += sample.rgb * known;+ weight += known;+ mask = max(mask, sample.a);+ }+ }+ return vec4f(color / max(weight, 1.0), mask);+}diff --git a/src/features/heal/inpaint/reconstruct.wgsl b/src/features/heal/inpaint/reconstruct.wgslnew file mode 100644index 0000000..7486edf--- /dev/null+++ b/src/features/heal/inpaint/reconstruct.wgsl@@ -0,0 +1,25 @@+@group(0) @binding(0) var source: texture_2d<f32>;+@group(0) @binding(1) var field: texture_2d<f32>;+@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {+ let p = vec2i(position.xy);+ let original = textureLoad(source, p, 0);+ if (original.a < 0.5) { return original; }+ let end = vec2i(textureDimensions(source)) - 1;+ var color = vec3f(0.0);+ var weight = 0.0;+ for (var y = -2; y <= 2; y += 2) {+ for (var x = -2; x <= 2; x += 2) {+ let center = clamp(p + vec2i(x, y), vec2i(0), end);+ let correspondence = textureLoad(field, center, 0);+ if (correspondence.w < 0.5) { continue; }+ let donor = clamp(p + vec2i(correspondence.xy), vec2i(0), end);+ let sample = textureLoad(source, donor, 0);+ if (sample.a > 0.5) { continue; }+ let confidence = exp(-min(correspondence.z * 80.0, 20.0));+ color += sample.rgb * confidence;+ weight += confidence;+ }+ }+ let averaged = color / max(weight, 0.000001);+ return vec4f(averaged, original.a);+}diff --git a/src/features/heal/mode-buttons.tsx b/src/features/heal/mode-buttons.tsxnew file mode 100644index 0000000..50d89b9--- /dev/null+++ b/src/features/heal/mode-buttons.tsx@@ -0,0 +1,43 @@+import { IconButton } from "@roprgm/ui/icon-button";+import { CloneIcon } from "@/components/icons/clone";+import { HealIcon } from "@/components/icons/heal";+import type { IconProps } from "@/components/icons/icon";+import { RemoveIcon } from "@/components/icons/remove";+import type { HealMode } from "@/core/document";+import { useHealing } from "./mode";+import { healModes } from "./modes";++export function PatchModeIcon({+ mode,+ ...props+}: IconProps & { mode: HealMode }) {+ if (mode === "clone") return <CloneIcon {...props} />;+ if (mode === "remove") return <RemoveIcon {...props} />;+ return <HealIcon {...props} />;+}++export function HealModeIcon(props: IconProps) {+ const { mode } = useHealing();+ return <PatchModeIcon {...props} mode={mode} />;+}++export function HealModeButtons() {+ const { mode, selectMode } = useHealing();+ return (+ <fieldset aria-label="Retouch mode" className="mr-1 flex gap-0.5">+ {healModes.map((entry) => (+ <IconButton+ key={entry.mode}+ label={`${entry.label} · ${entry.description}`}+ aria-label={entry.label}+ shortcut="H"+ aria-pressed={mode === entry.mode}+ className="aria-pressed:bg-control-hover pointer-coarse:size-10"+ onClick={() => selectMode(entry.mode)}+ >+ <PatchModeIcon mode={entry.mode} className="size-5" />+ </IconButton>+ ))}+ </fieldset>+ );+}diff --git a/src/features/heal/mode.tsx b/src/features/heal/mode.tsxindex 420beb6..0f7f705 100644--- a/src/features/heal/mode.tsx+++ b/src/features/heal/mode.tsx@@ -5,11 +5,21 @@ import {useContext,useState,} from "react";+import {+ type BrushInput,+ brushParameters,+ useBrushSettings,+} from "@/components/editor/brush-input";+import { useDocument, useSelectedLayer } from "@/components/editor/session";+import { findLayer, type HealMode } from "@/core/document";import type { Point } from "@/core/image/frame";+import { setHealPatch } from "./edits";+import { healModes } from "./modes";const HealingContext = createContext<{- feather: number;- setFeather: (feather: number) => void;+ brush: Omit<BrushInput, "parameters">;+ mode: HealMode;+ selectMode: (mode: HealMode) => void;/** A donor chosen with Alt-click for the next strokes; without one, each stroke searches. */source?: Point;setSource: (source?: Point) => void;@@ -19,7 +29,7 @@ const HealingContext = createContext<{hoverPatch: (id?: string) => void;} | null>(null);-/** Owns the tool's next-stroke feather and patch selection, shared by the canvas, bar, and sidebar. */+/** Settings outlive tool visits; each recorded patch keeps its own mode and blend. */export function HealingProvider({children,onEdit,@@ -27,22 +37,40 @@ export function HealingProvider({children: ReactNode;onEdit?: () => void;}) {- const [feather, setFeather] = useState(0.1);+ const document = useDocument();+ const brush = useBrushSettings(0.2);+ const [mode, setMode] = useState<HealMode>(healModes[0].mode);const [source, setSource] = useState<Point>();const [selectedPatch, setSelectedPatch] = useState<string>();const [hoveredPatch, setHoveredPatch] = useState<string>();const selectPatch = useCallback((id?: string) => {setSelectedPatch(id);- if (id) onEdit?.();+ if (!id) return;+ const layer = findLayer(+ document.scene.getState().layers,+ document.selection.getState().layerId,+ );+ const patch =+ layer?.kind === "heal" &&+ layer.patches.find((patch) => patch.id === id);+ if (patch) setMode(patch.mode);+ onEdit?.();},- [onEdit],+ [document, onEdit],);+ function selectMode(mode: HealMode) {+ setMode(mode);+ setSelectedPatch(undefined);+ setHoveredPatch(undefined);+ onEdit?.();+ }return (<HealingContextvalue={{- feather,- setFeather,+ brush,+ mode,+ selectMode,source,setSource,selectedPatch,@@ -61,3 +89,36 @@ export function useHealing() {if (!context) throw Error("A healing provider is required.");return context;}++export function useSelectedHealPatch() {+ const { selectedPatch } = useHealing();+ const layer = useSelectedLayer();+ if (layer?.kind !== "heal") return undefined;+ return layer.patches.find((patch) => patch.id === selectedPatch);+}++/** The controls edit the selected patch's feather and remember it for the next stroke. */+export function useHealBrush(): BrushInput {+ const { brush } = useHealing();+ const document = useDocument();+ const layer = useSelectedLayer();+ const selected = useSelectedHealPatch();+ function update(change: Parameters<typeof brush.update>[0]) {+ brush.update(change);+ if (change.feather !== undefined && selected && layer?.kind === "heal") {+ setHealPatch(document, layer.id, selected.id, {+ feather: change.feather,+ });+ }+ }+ const settings = {+ ...brush.settings,+ feather: selected?.feather ?? brush.settings.feather,+ };+ return {+ ...brush,+ settings,+ update,+ parameters: brushParameters({ ...brush, settings, update }),+ };+}diff --git a/src/features/heal/model.ts b/src/features/heal/model.tsindex 4322570..57bbaa9 100644--- a/src/features/heal/model.ts+++ b/src/features/heal/model.ts@@ -29,22 +29,55 @@ export function dabTouchesImage();}+/** Tests ordered coverage, so an erased hole is available for a new patch. */+export function patchContains(+ strokes: readonly BrushStroke[],+ at: Point,+ minimumRadius = 0,+) {+ let covered = false;+ for (const stroke of strokes) {+ const radius =+ stroke.mode === "paint"+ ? Math.max(minimumRadius, stroke.size / 2)+ : stroke.size / 2;+ const touches = stroke.points.some(([x, y], index) => {+ const [ax, ay] = stroke.points[Math.max(0, index - 1)];+ const dx = x - ax;+ const dy = y - ay;+ const length = dx * dx + dy * dy;+ const t = length+ ? Math.max(+ 0,+ Math.min(1, ((at[0] - ax) * dx + (at[1] - ay) * dy) / length),+ )+ : 0;+ return Math.hypot(at[0] - ax - t * dx, at[1] - ay - t * dy) <= radius;+ });+ if (touches) covered = stroke.mode === "paint";+ }+ return covered;+}+/** Bounds include the whole soft brush edge, with room for a known boundary. */export function patchBounds(- stroke: BrushStroke,+ strokes: readonly BrushStroke[],size: readonly number[],margin = 0,) {- const radius = stroke.size / 2 + margin + 4;let left = size[0];let top = size[1];let right = 0;let bottom = 0;- for (const [x, y] of stroke.points) {- left = Math.min(left, x - radius);- top = Math.min(top, y - radius);- right = Math.max(right, x + radius);- bottom = Math.max(bottom, y + radius);+ for (const stroke of strokes) {+ if (stroke.mode === "erase") continue;+ const radius = stroke.size / 2 + margin + 4;+ for (const [x, y] of stroke.points) {+ left = Math.min(left, x - radius);+ top = Math.min(top, y - radius);+ right = Math.max(right, x + radius);+ bottom = Math.max(bottom, y + radius);+ }}const origin: Point = [Math.max(0, Math.floor(left)),@@ -59,10 +92,10 @@ export function patchBounds(/** A square around the patch with a little margin, so a thumbnail keeps the stroke's proportions. */export function patchThumbnailRegion(- stroke: BrushStroke,+ strokes: readonly BrushStroke[],size: readonly number[],): { origin: Point; extent: Point } {- const { origin, extent } = patchBounds(stroke, size);+ const { origin, extent } = patchBounds(strokes, size);const side = Math.max(extent[0], extent[1]) * 1.1;return {origin: [@@ -82,10 +115,26 @@ export const patchStroke = strokeSchema.check(export const patchBlend = change(z.object({ feather: unit, opacity: unit }));-export const healPatchSchema = z.object({+export const donorModeSchema = z.enum(["heal", "clone"]);++const patchShape = {id: z.string().check(z.minLength(1)),feather: unit,- stroke: patchStroke,+ strokes: z.array(strokeSchema).check(+ z.minLength(1),+ z.refine(+ (strokes) => strokes[0]?.mode === "paint",+ "Start a patch with paint",+ ),+ ),opacity: unit,- offset: point,-}) satisfies z.ZodMiniType<HealPatch>;+};++export const healPatchSchema = z.union([+ z.object({+ ...patchShape,+ mode: z._default(donorModeSchema, "heal"),+ offset: point,+ }),+ z.object({ ...patchShape, mode: z.literal("remove") }),+]) satisfies z.ZodMiniType<HealPatch>;diff --git a/src/features/heal/modes.ts b/src/features/heal/modes.tsnew file mode 100644index 0000000..d80fe34--- /dev/null+++ b/src/features/heal/modes.ts@@ -0,0 +1,25 @@+import type { HealMode } from "@/core/document";++/** Display order, initial mode, and shortcut cycle share the same choices. */+export const healModes = [+ {+ mode: "remove",+ label: "Remove",+ description: "Synthesize texture from surrounding context",+ },+ {+ mode: "heal",+ label: "Heal",+ description: "Copy texture and match surrounding color",+ },+ {+ mode: "clone",+ label: "Clone",+ description: "Copy the source without color correction",+ },+] as const;++export function nextHealMode(mode: HealMode): HealMode {+ const index = healModes.findIndex((entry) => entry.mode === mode);+ return healModes[(index + 1) % healModes.length].mode;+}diff --git a/src/features/heal/options.tsx b/src/features/heal/options.tsxindex 41875bb..34dccb6 100644--- a/src/features/heal/options.tsx+++ b/src/features/heal/options.tsx@@ -1,64 +1,32 @@import { Chip } from "@roprgm/ui/chip";+import { IconButton } from "@roprgm/ui/icon-button";import { Slider } from "@roprgm/ui/slider";import { Tooltip, TooltipContent, TooltipTrigger } from "@roprgm/ui/tooltip";-import { useBrushTool } from "@/components/editor/brush-tool";-import { DockControls } from "@/components/editor/dock";+import { useBrushInput } from "@/components/editor/brush-input";+import { DockChips, DockControls } from "@/components/editor/dock";import type { Parameter } from "@/components/editor/parameter";import { useDocument, useSelectedLayer } from "@/components/editor/session";import { barSlider, useBarDensity } from "@/components/editor/toolbar-density";+import { SparklesIcon } from "@/components/icons/sparkles";import { setHealPatch } from "./edits";-import { useHealing } from "./mode";+import { useHealing, useSelectedHealPatch } from "./mode";+import { HealModeIcon } from "./mode-buttons";+import { healModes } from "./modes";export function HealOptions() {const document = useDocument();- const { settings, maxSize, setPreview, update } = useBrushTool();- const {- feather: nextFeather,- setFeather,- source,- setSource,- selectedPatch,- } = useHealing();+ const { parameters: brushParameters } = useBrushInput();+ const { source, setSource, mode, selectMode } = useHealing();const layer = useSelectedLayer();- const selected =- layer?.kind === "heal"- ? layer.patches.find((patch) => patch.id === selectedPatch)- : undefined;+ const selected = useSelectedHealPatch();const density = useBarDensity();- const feather = selected?.feather ?? nextFeather;+ const modeLabel = `${mode[0].toUpperCase()}${mode.slice(1)} mode`;function changePatch(change: Parameters<typeof setHealPatch>[3]) {if (selected && layer?.kind === "heal") {setHealPatch(document, layer.id, selected.id, change);}}- const parameters: Parameter[] = [- {- id: "size",- label: "Size",- value: settings.size,- min: 1,- max: maxSize,- format: (value) => `${value}px`,- valueWidth: `${maxSize}`.length,- onEditingChange: setPreview,- onChange: (value) => update({ size: Math.round(value) }),- },- {- id: "feather",- label: "Feather",- value: feather * 100,- min: 0,- max: 100,- format: (value) => `${value}%`,- valueWidth: 3,- onEditingChange: setPreview,- onChange: (value) => {- const feather = value / 100;- setFeather(feather);- changePatch({ feather });- },- },- ];+ const parameters: Parameter[] = [...brushParameters];if (selected) {parameters.push({id: "opacity",@@ -71,7 +39,7 @@ export function HealOptions() {onChange: (value) => changePatch({ opacity: value / 100 }),});}- const automatic = source && (+ const automatic = source && mode !== "remove" && (<Tooltip><TooltipTriggerrender={@@ -84,10 +52,41 @@ export function HealOptions() {</Tooltip>);if (density === "dock") {- return <DockControls header={automatic} parameters={parameters} />;+ return (+ <DockControls+ header={+ <DockChips+ label="Retouch mode"+ items={healModes.map(({ mode, label }) => [mode, label] as const)}+ value={mode}+ onChange={selectMode}+ />+ }+ action={+ source &&+ mode !== "remove" && (+ <IconButton+ label="Use automatic source · Alt-click sets a source"+ className="pointer-coarse:size-10"+ onClick={() => setSource(undefined)}+ >+ <SparklesIcon className="size-5" />+ </IconButton>+ )+ }+ parameters={parameters}+ />+ );}return (<>+ <span+ role="img"+ aria-label={modeLabel}+ className="grid size-6 shrink-0 place-items-center"+ >+ <HealModeIcon className="size-4" />+ </span>{parameters.map(({ id, ...parameter }) => (<Slider key={id} {...parameter} variant={barSlider(density)} />))}diff --git a/src/features/heal/outline.tsx b/src/features/heal/outline.tsxindex ad9552c..86d5f0b 100644--- a/src/features/heal/outline.tsx+++ b/src/features/heal/outline.tsx@@ -1,17 +1,21 @@-import { memo, type PointerEvent, useId } from "react";+import { memo, type PointerEvent, useId, useState } from "react";import { useDocumentMapping } from "@/components/editor/mapping";import { useDocument } from "@/components/editor/session";+import { useViewport } from "@/components/editor/viewport";import type { BrushStroke, HealPatch } from "@/core/document";import type { Point } from "@/core/image/frame";import { type AnchorDrag, HealAnchor } from "./anchor";import { setHealDestination, setHealSource } from "./edits";+import { findHealPatch, patchContains } from "./model";type Geometry = {+ mode: BrushStroke["mode"];center: Point;d: string;first: Point;last: Point;width: number;+ bounds: { left: number; top: number; right: number; bottom: number };};type Mapping = ReturnType<typeof useDocumentMapping>;@@ -24,12 +28,24 @@ function geometry(mapping.toScreen([x + offset[0], y + offset[1]]),);const width = stroke.size / mapping.pixelsPerViewportPixel;+ let left = Infinity;+ let top = Infinity;+ let right = -Infinity;+ let bottom = -Infinity;+ for (const [x, y] of points) {+ left = Math.min(left, x - width / 2);+ top = Math.min(top, y - width / 2);+ right = Math.max(right, x + width / 2);+ bottom = Math.max(bottom, y + width / 2);+ }return {+ mode: stroke.mode,center: points[0],d: points.map(([x, y], index) => `${index ? "L" : "M"}${x} ${y}`).join(""),first: points[0],last: points.at(-1) ?? points[0],width,+ bounds: { left, top, right, bottom },};}@@ -68,34 +84,111 @@ function StrokeShape({);}+function CoverageMask({+ id,+ shapes,+ region,+}: {+ id: string;+ shapes: readonly Geometry[];+ region?: { x: number; y: number; width: number; height: number };+}) {+ return (+ <mask id={id} maskUnits="userSpaceOnUse" {...region}>+ <rect width="100%" height="100%" fill="black" />+ {shapes.map((shape, index) => (+ <StrokeShape+ key={index}+ shape={shape}+ width={shape.width}+ color={shape.mode === "paint" ? "white" : "black"}+ />+ ))}+ </mask>+ );+}+function Outline({- shape,+ shapes,kind,}: {- shape: Geometry;+ shapes: readonly Geometry[];kind: "destination" | "source";}) {const mask = useId();- const outer = shape.width + 2;- const inner = Math.max(0, shape.width - 2);+ const edge = `${mask}-edge`;+ const { viewport } = useViewport();+ // Keep the artificial raster edge outside the viewport, including the filter's support.+ const left = Math.max(+ -4,+ Math.min(...shapes.map((shape) => shape.bounds.left)) - 4,+ );+ const top = Math.max(+ -4,+ Math.min(...shapes.map((shape) => shape.bounds.top)) - 4,+ );+ const right = Math.min(+ viewport[0] + 4,+ Math.max(...shapes.map((shape) => shape.bounds.right)) + 4,+ );+ const bottom = Math.min(+ viewport[1] + 4,+ Math.max(...shapes.map((shape) => shape.bounds.bottom)) + 4,+ );+ if (right <= left || bottom <= top) return null;+ const region = { x: left, y: top, width: right - left, height: bottom - top };return (<g data-heal-outline={kind} opacity={kind === "source" ? 0.55 : 1}><defs>- <mask id={mask} maskUnits="userSpaceOnUse">- <rect width="100%" height="100%" fill="black" />- <StrokeShape shape={shape} width={outer} color="white" />- {inner > 0 && (- <StrokeShape shape={shape} width={inner} color="black" />- )}- </mask>+ <CoverageMask id={mask} shapes={shapes} region={region} />+ <filter id={edge} filterUnits="userSpaceOnUse" {...region}>+ <feMorphology+ in="SourceAlpha"+ operator="dilate"+ radius="1"+ result="outer"+ />+ <feMorphology+ in="SourceAlpha"+ operator="erode"+ radius="1"+ result="inner"+ />+ <feComposite in="outer" in2="inner" operator="out" result="border" />+ <feFlood floodColor="white" />+ <feComposite in2="border" operator="in" />+ <feDropShadow dx="0" dy="0" stdDeviation="1" floodColor="black" />+ </filter>+ </defs>+ <g filter={`url(#${edge})`}>+ <rect {...region} fill="white" mask={`url(#${mask})`} />+ </g>+ </g>+ );+}++/** Shows the edited selection without changing the rendered image. */+export function HealStrokePreview({+ strokes,+}: {+ strokes: readonly BrushStroke[];+}) {+ const mapping = useDocumentMapping();+ const mask = useId();+ const shapes = strokes.map((stroke) => geometry(stroke, [0, 0], mapping));+ return (+ <g data-heal-stroke-preview="true">+ <defs>+ <CoverageMask id={mask} shapes={shapes} /></defs><rectwidth="100%"height="100%"fill="white"+ opacity={0.15}mask={`url(#${mask})`}- className="drop-shadow-[0_0_1px_black]"/>+ <Outline shapes={shapes} kind="destination" /></g>);}@@ -114,12 +207,22 @@ export function HealPatchOutline({}) {const document = useDocument();const mapping = useDocumentMapping();- const first = patch.stroke.points[0];- const destination = geometry(patch.stroke, [0, 0], mapping);- const source = showSource && geometry(patch.stroke, patch.offset, mapping);- // Both anchors edit live inside the drag's history group, so the repair follows the pointer.+ const [preview, setPreview] = useState<Point>();+ const first = patch.strokes[0].points[0];+ const offset: Point = preview+ ? [preview[0] - first[0], preview[1] - first[1]]+ : [0, 0];+ const destination = patch.strokes.map((stroke) =>+ geometry(stroke, offset, mapping),+ );+ const source =+ showSource &&+ patch.mode !== "remove" &&+ patch.strokes.map((stroke) => geometry(stroke, patch.offset, mapping));+ // Remove previews the contour and solves on drop; donor repairs follow the drag live.const moveDestination = (next?: Point) => {- if (next) setHealDestination(document, layer, patch.id, next);+ if (next && findHealPatch(document.scene.getState(), layer, patch.id))+ setHealDestination(document, layer, patch.id, next);};const moveSource = (next?: Point) => {if (next) setHealSource(document, layer, patch.id, next);@@ -127,29 +230,35 @@ export function HealPatchOutline({const destinationDrag: AnchorDrag | undefined = interactive? {from: [first[0], first[1]],- onDrag: moveDestination,+ editOnRelease: patch.mode === "remove",+ onDrag: patch.mode === "remove" ? setPreview : moveDestination,onDrop: moveDestination,}: undefined;- const sourceDrag: AnchorDrag | undefined = interactive- ? {- from: patch.offset,- onDrag: moveSource,- onDrop: moveSource,- }- : undefined;+ const sourceDrag: AnchorDrag | undefined =+ interactive && patch.mode !== "remove"+ ? {+ from: patch.offset,+ onDrag: moveSource,+ onDrop: moveSource,+ }+ : undefined;return (<>- <Outline shape={destination} kind="destination" />+ <Outline shapes={destination} kind="destination" /><HealAnchorkind="destination"- center={destination.center}+ center={destination[0].center}drag={destinationDrag}/>{source && (<>- <Outline shape={source} kind="source" />- <HealAnchor kind="source" center={source.center} drag={sourceDrag} />+ <Outline shapes={source} kind="source" />+ <HealAnchor+ kind="source"+ center={source[0].center}+ drag={sourceDrag}+ /></>)}</>@@ -164,20 +273,42 @@ export const HealPatchHitTarget = memo(function HealPatchHitTarget({patch: HealPatch;onSelect: (id: string) => void;}) {- const shape = geometry(patch.stroke, [0, 0], useDocumentMapping());+ const mapping = useDocumentMapping();const select = (event: PointerEvent<SVGElement>) => {+ if (event.shiftKey || event.altKey) return;+ const box = event.currentTarget.ownerSVGElement?.getBoundingClientRect();+ if (+ !box ||+ !patchContains(+ patch.strokes,+ mapping.toDocument(event.clientX, event.clientY, box),+ 8 * mapping.pixelsPerViewportPixel,+ )+ )+ return;event.preventDefault();event.stopPropagation();onSelect(patch.id);};- const width = Math.max(16, shape.width);return (<gdata-heal-hit-target={patch.id}onPointerDown={select}pointerEvents="all">- <StrokeShape shape={shape} width={width} color="transparent" />+ {patch.strokes+ .filter((stroke) => stroke.mode === "paint")+ .map((stroke, index) => {+ const shape = geometry(stroke, [0, 0], mapping);+ return (+ <StrokeShape+ key={index}+ shape={shape}+ width={Math.max(16, shape.width)}+ color="transparent"+ />+ );+ })}</g>);});diff --git a/src/features/heal/overlay.tsx b/src/features/heal/overlay.tsxindex ab0ba3d..ace8a21 100644--- a/src/features/heal/overlay.tsx+++ b/src/features/heal/overlay.tsx@@ -5,20 +5,30 @@ import { useDocumentMapping } from "@/components/editor/mapping";import { useRenderer } from "@/components/editor/pipeline";import { useDocument, useSelectedLayer } from "@/components/editor/session";import { useToolLayer } from "@/components/editor/tool-layer";-import type { Layer } from "@/core/document";+import { type BrushStroke, findLayer, type Layer } from "@/core/document";import type { Point } from "@/core/image/frame";import { useDisposable } from "@/hooks/use-disposable";-import { addHealPatch, extendHealPatch, setHealSource } from "./edits";+import {+ addHealPatch,+ addHealStroke,+ addRemovePatch,+ extendHealPatch,+ setHealSource,+} from "./edits";import { useHealing } from "./mode";import { dabTouchesImage, findHealPatch } from "./model";-import { HealPatchHitTarget, HealPatchOutline } from "./outline";+import {+ HealPatchHitTarget,+ HealPatchOutline,+ HealStrokePreview,+} from "./outline";import { createHealSearch } from "./source";function isHealLayer(layer: Layer): layer is Extract<Layer, { kind: "heal" }> {return layer.kind === "heal";}-/** Paints ordinary brush strokes; only the donor search and patch edits belong to Heal. */+/** Sends brush strokes to retouch edits and resolves Heal/Clone donors after painting. */export function HealOverlay({onCreate,onDone,@@ -32,7 +42,8 @@ export function HealOverlay({const gpu = useGpu();const search = useDisposable(() => createHealSearch(gpu), [gpu]);const {- feather,+ brush,+ mode,source,setSource,selectedPatch,@@ -40,6 +51,7 @@ export function HealOverlay({hoveredPatch,} = useHealing();const [drawingPatch, setDrawingPatch] = useState<string>();+ const [draft, setDraft] = useState<BrushStroke>();const [resolvingSource, setResolvingSource] = useState<string>();// A manual donor belongs to this visit to the tool.useEffect(() => () => setSource(undefined), [setSource]);@@ -51,17 +63,31 @@ export function HealOverlay({const layer = useSelectedLayer();const healLayer = layer && isHealLayer(layer) ? layer : undefined;const patches = healLayer?.patches ?? [];+ const selected = patches.find((patch) => patch.id === selectedPatch);const hovered = patches.some((patch) => patch.id === hoveredPatch)? hoveredPatch: undefined;const visiblePatch = drawingPatch ?? hovered ?? selectedPatch;const pending = useRef<- { layer: string; patch: string; automatic: boolean } | undefined+ | { layer: string; stroke: BrushStroke; patch?: string }+ | {+ layer: string;+ mode: "heal" | "clone";+ patch: string;+ automatic: boolean;+ }+ | undefined>(undefined);async function complete(signal: AbortSignal) {const current = pending.current;try {- if (!current?.automatic || signal.aborted) return;+ if (+ !current ||+ "stroke" in current ||+ !current.automatic ||+ signal.aborted+ )+ return;const scene = document.scene.getState();const patch = findHealPatch(scene, current.layer, current.patch);if (!patch) return;@@ -71,7 +97,7 @@ export function HealOverlay({if (!image) throw Error("Heal input is unavailable.");const dimensions = document.resources.get(scene.layers[0].source).image.size;- const offset = await search.find(image, dimensions, patch.stroke);+ const offset = await search.find(image, dimensions, patch.strokes);// The stroke's group stays open through the search; undo or cancel aborts it and removes the patch.if (signal.aborted ||@@ -81,7 +107,9 @@ export function HealOverlay({}setHealSource(document, current.layer, current.patch, offset);} finally {- setResolvingSource((id) => (id === current?.patch ? undefined : id));+ if (current && !("stroke" in current)) {+ setResolvingSource((id) => (id === current.patch ? undefined : id));+ }}}const marker = source && mapping.toScreen(source);@@ -89,8 +117,10 @@ export function HealOverlay({<BrushCanvaslabel="Healing canvas"erase={false}- feather={feather}- onStart={(stroke) => {+ editOnRelease={({ shift, alt }) =>+ mode === "remove" || Boolean(selected && (shift || alt))+ }+ onStart={(stroke, { shift, alt }) => {const layer = selectedHealLayer();if (!layer) return false;const [x, y] = stroke.points[0];@@ -98,33 +128,95 @@ export function HealOverlay({document.scene.getState().layers[0].source,).image.size;if (!dabTouchesImage([x, y], stroke.size / 2, size)) return false;+ // Keep the displayed feather when starting a stroke changes the patch selection.+ brush.update({ feather: stroke.feather });+ const painted = { ...stroke, flow: 1 };+ if (selected && (shift || alt)) {+ const stroke: BrushStroke = {+ ...painted,+ mode: alt ? "erase" : "paint",+ };+ pending.current = { layer: layer.id, patch: selected.id, stroke };+ setDraft(stroke);+ setDrawingPatch(selected.id);+ return true;+ }+ if (mode === "remove") {+ pending.current = { layer: layer.id, stroke: painted };+ setDraft(painted);+ selectPatch(undefined);+ return true;+ }const offset: Point = source ? [source[0] - x, source[1] - y] : [0, 0];- const patch = addHealPatch(- document,- layer.id,- { ...stroke, flow: 1 },- offset,- );+ const patch = addHealPatch(document, layer.id, painted, offset, mode);setDrawingPatch(patch);setResolvingSource(source ? undefined : patch);selectPatch(patch);- pending.current = { layer: layer.id, patch, automatic: !source };+ pending.current = {+ layer: layer.id,+ mode,+ patch,+ automatic: !source,+ };return true;}}onExtend={(points) => {- const id = pending.current?.layer;- if (id) {- extendHealPatch(document, id, points);+ const current = pending.current;+ if (!current) return;+ if ("stroke" in current) {+ const stroke = {+ ...current.stroke,+ points: [...current.stroke.points, ...points],+ };+ pending.current = { ...current, stroke };+ setDraft(stroke);+ return;}+ extendHealPatch(document, current.layer, points);}}onComplete={complete}onFinish={(committed) => {+ const current = pending.current;+ if (current && "stroke" in current) {+ pending.current = undefined;+ setDraft(undefined);+ const scene = document.scene.getState();+ const available = current.patch+ ? findHealPatch(scene, current.layer, current.patch)+ : findLayer(scene.layers, current.layer)?.kind === "heal";+ if (committed && available) {+ if (current.patch) {+ addHealStroke(+ document,+ current.layer,+ current.patch,+ current.stroke,+ );+ } else {+ selectPatch(+ addRemovePatch(document, current.layer, current.stroke),+ );+ }+ }+ }setDrawingPatch(undefined);if (!committed) setResolvingSource(undefined);}}- onPickSource={setSource}+ onPickSource={mode === "remove" || selected ? undefined : setSource}onDone={onDone}>+ {draft && (+ <svg+ aria-hidden="true"+ className="pointer-events-none absolute inset-0 size-full overflow-visible"+ >+ <HealStrokePreview+ strokes={+ drawingPatch && selected ? [...selected.strokes, draft] : [draft]+ }+ />+ </svg>+ )}{healLayer && patches.length > 0 && (<svgaria-hidden="true"@@ -133,7 +225,8 @@ export function HealOverlay({>{patches.map((patch) =>- visiblePatch === patch.id && (+ visiblePatch === patch.id &&+ !(draft && drawingPatch === patch.id) && (<g key={`outline-${patch.id}`} data-heal-patch={patch.id}><HealPatchOutlinelayer={healLayer.id}@@ -160,7 +253,7 @@ export function HealOverlay({)}</svg>)}- {marker && (+ {marker && mode !== "remove" && (<svgaria-hidden="true"className="pointer-events-none absolute inset-0 size-full overflow-visible"diff --git a/src/features/heal/pass.ts b/src/features/heal/pass.tsindex 2bacdd1..482054b 100644--- a/src/features/heal/pass.ts+++ b/src/features/heal/pass.ts@@ -1,5 +1,6 @@import type { HealPatch } from "@/core/document";import {+ type CacheKey,type Composition,merge,node,@@ -8,18 +9,22 @@ import {sourceSize,} from "@/core/renderer";import shader from "./heal.wgsl";+import { removePatch } from "./inpaint/pass";import { patchBounds } from "./model";-type HealComposition = Pick<Composition, "patch" | "inputId" | "retain">;+type HealComposition = Pick<+ Composition,+ "patch" | "inputId" | "retain" | "cache"+>;function healPatch(source: RenderImage,{ coverage, origin: coverageOrigin }: PatchInput,- patch: HealPatch,+ patch: Extract<HealPatch, { mode: "heal" | "clone" }>,name: string,) {const dimensions = sourceSize(source);- const { origin, extent } = patchBounds(patch.stroke, dimensions, 0);+ const { origin, extent } = patchBounds(patch.strokes, dimensions, 0);const samplers = {linearSampler: { minFilter: "linear", magFilter: "linear" },} as const;@@ -32,37 +37,39 @@ function healPatch(coverageSize: coverage.size,};let correction = source;- // Coarse-to-fine harmonic extension of the destination/donor log color ratio.- // The correction is smooth; the donor retains its full-resolution texture.- for (const resolution of [4, 8, 16, 32, 64, 128]) {- const ratio = Math.min(1, resolution / Math.max(...extent));- const size: [number, number] = [- Math.max(2, Math.ceil(extent[0] * ratio)),- Math.max(2, Math.ceil(extent[1] * ratio)),- ];- const params = { ...common, grid: size, feather: 0, opacity: 1 };- correction = merge(- { source, coverage, previous: correction },- node(`${name}/${resolution}/seed`, shader, {- size,- samplers,- set: { params: { ...params, mode: resolution === 4 ? 0 : 1 } },- }),- );- const iterations = resolution === 4 ? 32 : 8;- for (let i = 0; i < iterations; i++) {+ if (patch.mode === "heal") {+ // Coarse-to-fine harmonic extension of the destination/donor log color ratio.+ // The correction is smooth; the donor retains its full-resolution texture.+ for (const resolution of [4, 8, 16, 32, 64, 128]) {+ const ratio = Math.min(1, resolution / Math.max(...extent));+ const size: [number, number] = [+ Math.max(2, Math.ceil(extent[0] * ratio)),+ Math.max(2, Math.ceil(extent[1] * ratio)),+ ];+ const params = { ...common, grid: size, feather: 0, opacity: 1 };correction = merge({ source, coverage, previous: correction },- node(`${name}/${resolution}/relax-${i}`, shader, {- instance: `${name}/${resolution}/relax`,+ node(`${name}/${resolution}/seed`, shader, {size,samplers,- set: { params: { ...params, mode: 2 } },+ set: { params: { ...params, mode: resolution === 4 ? 0 : 1 } },}),);- }- if (resolution >= Math.max(...extent)) {- break;+ const iterations = resolution === 4 ? 32 : 8;+ for (let i = 0; i < iterations; i++) {+ correction = merge(+ { source, coverage, previous: correction },+ node(`${name}/${resolution}/relax-${i}`, shader, {+ instance: `${name}/${resolution}/relax`,+ size,+ samplers,+ set: { params: { ...params, mode: 2 } },+ }),+ );+ }+ if (resolution >= Math.max(...extent)) {+ break;+ }}}return merge(@@ -73,8 +80,8 @@ function healPatch(params: {...common,grid: source.size,- mode: 3,- feather: (patch.stroke.size * patch.feather) / 2,+ mode: patch.mode === "clone" ? 4 : 3,+ feather: (patch.strokes[0].size * patch.feather) / 2,opacity: patch.opacity,},},@@ -82,21 +89,51 @@ function healPatch();}+function repairsPixels(patch: HealPatch) {+ return (+ patch.mode === "remove" || patch.offset[0] !== 0 || patch.offset[1] !== 0+ );+}++export function retainHealPatches(+ patches: readonly HealPatch[],+ name: string,+ composition: Pick<Composition, "retain">,+) {+ for (const patch of patches) {+ if (repairsPixels(patch)) composition.retain(`${name}/${patch.id}`);+ }+}+export function heal(source: RenderImage,patches: readonly HealPatch[],name: string,composition: HealComposition,+ dependencies: readonly CacheKey[],) {+ retainHealPatches(patches, name, composition);let image = source;let inspected: RenderImage | undefined;+ let content = dependencies;for (const patch of patches) {if (patch.id === composition.inputId) inspected = image;const id = `${name}/${patch.id}`;- if (patch.offset[0] === 0 && patch.offset[1] === 0) continue;- const coverage = composition.patch(id, patch.stroke);- composition.retain(id);- image = healPatch(image, coverage, patch, id);+ if (!repairsPixels(patch)) continue;+ const coverage = composition.patch(id, patch.strokes);+ if (patch.mode === "remove") {+ image = removePatch(+ image,+ coverage,+ patch,+ id,+ composition.cache,+ content,+ );+ } else {+ image = healPatch(image, coverage, patch, id);+ }+ content = [...content, patch];}return { image, input: inspected };}diff --git a/src/features/heal/source.ts b/src/features/heal/source.tsindex c92de07..c190403 100644--- a/src/features/heal/source.ts+++ b/src/features/heal/source.ts@@ -10,9 +10,9 @@ function matchSource(pixels: Float32Array,size: readonly number[],dimensions: readonly number[],- stroke: BrushStroke,+ strokes: readonly BrushStroke[],): Point {- const { origin, extent } = patchBounds(stroke, dimensions);+ const { origin, extent } = patchBounds(strokes, dimensions);const [left, top] = origin;const [width, height] = extent;function color(x: number, y: number, channel: number) {@@ -99,7 +99,7 @@ export function createHealSearch(gpu: Gpu, timer?: Timer) {async find(image: Target,dimensions: readonly number[],- stroke: BrushStroke,+ strokes: readonly BrushStroke[],) {const ratio = Math.min(1, 512 / Math.max(...image.size));const size: [number, number] = [@@ -122,7 +122,7 @@ export function createHealSearch(gpu: Gpu, timer?: Timer) {await thumbnail.readFloats(),size,dimensions,- stroke,+ strokes,);},inspect: graph.inspect,diff --git a/src/hooks/use-pan-zoom.ts b/src/hooks/use-pan-zoom.tsindex ab38307..0d2723d 100644--- a/src/hooks/use-pan-zoom.ts+++ b/src/hooks/use-pan-zoom.ts@@ -1,4 +1,5 @@import {+ type MouseEvent,type PointerEvent,useCallback,useEffect,@@ -12,7 +13,7 @@ import { useStore } from "zustand";import { createStore } from "zustand/vanilla";import type { Point } from "@/core/image/frame";import type { View } from "@/core/renderer";-import { isTyping } from "@/lib/dom";+import { containsTarget, isTyping } from "@/lib/dom";const fit: View = { zoom: 1, pan: [0, 0] };export const createCamera = () => createStore<View>(() => fit);@@ -162,7 +163,7 @@ export function usePanZoom();}return pan(view, -event.deltaX, -event.deltaY);- });+ }, constrain && !panMode);});// One listener and observer per element; the wheel handler reads the latest content and bounds.@@ -214,7 +215,11 @@ export function usePanZoom(element.setPointerCapture(pointerId);}function startDrag(event: PointerEvent<HTMLElement>) {- if (event.button !== 0 || pointers.current.size === 2) {+ if (+ !containsTarget(event) ||+ event.button !== 0 ||+ pointers.current.size === 2+ ) {return;}track(event.pointerId, [event.clientX, event.clientY], event.currentTarget);@@ -227,7 +232,7 @@ export function usePanZoom(}const handlers = {onPointerDownCapture: (event: PointerEvent<HTMLElement>) => {- if (panMode && event.button === 0) {+ if (panMode && event.button === 0 && containsTarget(event)) {event.preventDefault();event.stopPropagation();startDrag(event);@@ -267,7 +272,9 @@ export function usePanZoom(onPointerUp: release,onPointerCancel: release,onLostPointerCapture: release,- onDoubleClick: () => resetView(),+ onDoubleClick: (event: MouseEvent<HTMLElement>) => {+ if (containsTarget(event)) resetView();+ },};return {diff --git a/src/lib/dom.ts b/src/lib/dom.tsindex 200fdbc..d1c3abe 100644--- a/src/lib/dom.ts+++ b/src/lib/dom.ts@@ -2,6 +2,16 @@export const typingFields ='input:not([type="range"], [type="color"]), textarea, select, dialog, [role="dialog"]';+/** React portals bubble through the component tree, outside the element's input area. */+export function containsTarget(event: {+ currentTarget: Node;+ target: EventTarget | null;+}) {+ return (+ event.target instanceof Node && event.currentTarget.contains(event.target)+ );+}+export function isTyping(target: EventTarget | null, fields = typingFields) {return (target instanceof HTMLElement &&diff --git a/tests/brush-navigation.e2e.ts b/tests/brush-navigation.e2e.tsnew file mode 100644index 0000000..ec8854a--- /dev/null+++ b/tests/brush-navigation.e2e.ts@@ -0,0 +1,316 @@+import { expect, test } from "./fixtures";+import { readImage, readPixel } from "./images";+import { box } from "./pointer";++for (const { name, shortcut, presses, label } of [+ { name: "mask", shortcut: "b", presses: 1, label: "Brush canvas" },+ { name: "color", shortcut: "b", presses: 2, label: "Paint canvas" },+ { name: "retouch", shortcut: "h", presses: 1, label: "Healing canvas" },+]) {+ test(`${name} brush menu owns clicks and slider drags without painting or navigating`, async ({+ page,+ }) => {+ await page.setViewportSize({ width: 1440, height: 1000 });+ await page.goto("/");+ await page+ .locator('input[type="file"]')+ .setInputFiles("tests/fixtures/photo.svg");+ await expect(+ page.getByRole("textbox", { name: "Exposure", exact: true }),+ ).toHaveValue("0.00");+ for (let i = 0; i < presses; i++) await page.keyboard.press(shortcut);+ const canvas = page.getByLabel(label, { exact: true });+ await expect(canvas).toBeVisible();+ const state = await page.evaluate(() => window.openlight.getState());+ const zoom = page.getByRole("button", { name: /^\d+%$/ });+ const fit = await zoom.innerText();+ await canvas.click({ button: "right", position: { x: 300, y: 300 } });+ const menu = page.getByRole("dialog");+ await expect(menu).toBeVisible();+ const size = menu.getByRole("textbox", { name: "Size", exact: true });+ await size.click();+ await size.fill("80");+ await size.press("Enter");+ const feather = menu.getByRole("textbox", { name: "Feather", exact: true });+ await feather.click();+ await feather.fill("30");+ await feather.press("Enter");+ for (const name of ["Size", "Feather"]) {+ const thumb = menu.getByRole("slider", { name, exact: true });+ const initial = await thumb.getAttribute("aria-valuenow");+ const bounds = await box(thumb);+ await page.mouse.move(+ bounds.x + bounds.width / 2,+ bounds.y + bounds.height / 2,+ );+ await page.mouse.down();+ await page.mouse.move(+ bounds.x + bounds.width / 2 + 30,+ bounds.y + bounds.height / 2,+ { steps: 4 },+ );+ await page.mouse.up();+ await expect(thumb).not.toHaveAttribute("aria-valuenow", initial ?? "");+ }+ // The viewport capture phase must also leave portaled controls alone while Space is held.+ await page.evaluate(() => {+ if (document.activeElement instanceof HTMLElement)+ document.activeElement.blur();+ });+ await page.keyboard.down("Space");+ await expect(canvas).toHaveAttribute("data-pan", "true");+ await size.click();+ await size.fill("90");+ await size.press("Enter");+ await page.keyboard.up("Space");+ const panel = await box(menu);+ await page.mouse.click(panel.x + panel.width / 2, panel.y + 6);+ await expect(menu).toBeVisible();+ await expect(zoom).toHaveText(fit);+ expect(await page.evaluate(() => window.openlight.getState())).toEqual(+ state,+ );+ await page.keyboard.press("Escape");+ await expect(menu).toHaveCount(0);+ await expect(canvas).toBeVisible();+ await expect(+ page.getByRole("textbox", { name: "Size", exact: true }),+ ).toHaveValue("90");+ });++ test(`${name} brush keeps its screen size across zoom while new strokes scale in the image`, async ({+ page,+ }) => {+ await page.setViewportSize({ width: 1440, height: 1000 });+ await page.goto("/");+ await page+ .locator('input[type="file"]')+ .setInputFiles("tests/fixtures/photo.svg");+ await expect(+ page.getByRole("textbox", { name: "Exposure", exact: true }),+ ).toHaveValue("0.00");+ for (let i = 0; i < presses; i++) await page.keyboard.press(shortcut);+ const canvas = page.getByLabel(label, { exact: true });+ await expect(canvas).toBeVisible();+ const size = page.getByRole("textbox", { name: "Size", exact: true });+ await expect(size).toHaveValue("50");+ await size.fill("100");+ await size.press("Enter");+ const bounds = await box(canvas);+ const cursor = canvas.locator('[data-brush-cursor="true"] circle').first();+ const strokes = () =>+ page.evaluate(() => {+ const { scene, selectedLayerId } = window.openlight.getState();+ const layer = scene?.layers.find(+ (layer) => layer.id === selectedLayerId,+ );+ if (layer?.kind === "paint") return layer.strokes;+ if (layer?.kind === "mask" && layer.mask.kind === "brush")+ return layer.mask.strokes;+ if (layer?.kind === "heal")+ return layer.patches.flatMap((patch) => patch.strokes);+ return [];+ });+ const x = bounds.x + bounds.width / 2 + 180;+ const y = bounds.y + bounds.height / 2 + 120;+ await page.mouse.move(x, y);+ await expect(cursor).toHaveAttribute("r", "50");+ await page.mouse.click(x, y);+ await expect.poll(async () => (await strokes()).length).toBe(1);+ const first = (await strokes())[0];+ await page.getByRole("button", { name: "Zoom in", exact: true }).click();+ await page.getByRole("button", { name: "Zoom in", exact: true }).click();+ await page.mouse.move(x + 180, y);+ await expect(cursor).toHaveAttribute("r", "50");+ await expect(size).toHaveValue("100");+ await page.mouse.click(x + 180, y);+ await expect.poll(async () => (await strokes()).length).toBe(2);+ const recorded = await strokes();+ expect(recorded[0]).toEqual(first);+ expect(recorded[1].size).toBeCloseTo(first.size / 1.25 ** 2, 6);+ // Navigating does not resize existing content; each stroke remains one undo step.+ await page.keyboard.press("ControlOrMeta+z");+ expect(await strokes()).toEqual([first]);+ await page.keyboard.press("ControlOrMeta+Shift+z");+ expect(await strokes()).toEqual(recorded);+ await page.getByRole("button", { name: /^\d+%$/ }).click();+ await page.mouse.move(x, y);+ await expect(cursor).toHaveAttribute("r", "50");+ await expect(size).toHaveValue("100");+ });++ test(`${name} wheel sizes the brush while pinch zoom and Space-drag navigate`, async ({+ page,+ }) => {+ await page.setViewportSize({ width: 1440, height: 1000 });+ await page.goto("/");+ await page+ .locator('input[type="file"]')+ .setInputFiles("tests/fixtures/photo.svg");+ await expect(+ page.getByRole("textbox", { name: "Exposure", exact: true }),+ ).toHaveValue("0.00");+ for (let i = 0; i < presses; i++) await page.keyboard.press(shortcut);+ const canvas = page.getByLabel(label, { exact: true });+ await expect(canvas).toBeVisible();+ const size = page.getByRole("textbox", { name: "Size", exact: true });+ const feather = page.getByRole("textbox", {+ name: "Feather",+ exact: true,+ });+ await size.fill("50");+ await size.press("Enter");+ await feather.fill("30");+ await feather.press("Enter");+ const state = await page.evaluate(() => window.openlight.getState());+ const zoom = page.getByRole("button", { name: /^\d+%$/ });+ const fit = await zoom.innerText();+ const cursor = canvas.locator('[data-brush-cursor="true"]');+ const center = cursor.locator('[data-brush-center="true"]');+ await canvas.hover();+ await expect(center).toHaveCount(0);+ // Positive vertical deltas enlarge the brush; negative deltas reduce it.+ await page.mouse.wheel(0, 100);+ await expect(size).toHaveValue("64");+ await page.mouse.wheel(0, -100);+ await expect(size).toHaveValue("50");+ // Several wheel events before a React render must accumulate, not overwrite each other.+ await canvas.evaluate((element) => {+ for (let i = 0; i < 2; i++) {+ element.dispatchEvent(+ new WheelEvent("wheel", {+ deltaY: 100,+ bubbles: true,+ cancelable: true,+ }),+ );+ }+ });+ await expect(size).toHaveValue("82");+ await expect(feather).toHaveValue("30");+ await expect(zoom).toHaveText(fit);+ await size.fill("1");+ await size.press("Enter");+ await canvas.hover();+ await page.mouse.wheel(0, 100);+ await expect(size).toHaveValue("1");+ await page.mouse.wheel(0, 100);+ await expect(size).toHaveValue("2");+ await page.mouse.wheel(0, -100_000);+ await expect(size).toHaveValue("1");+ await page.mouse.wheel(0, 100_000);+ await expect(size).toHaveValue("1000");+ // Chromium represents a trackpad pinch as ctrl+wheel.+ await page.keyboard.down("Control");+ await page.mouse.wheel(0, -80);+ await page.keyboard.up("Control");+ await expect(zoom).not.toHaveText(fit);+ await expect(size).toHaveValue("1000");+ await expect(center).toHaveCount(1);+ // Sample both sides of a visible edge: either scroll direction would move it across a sample.+ const zoomBounds = await box(canvas);+ const zoomScale = Number.parseInt(await zoom.innerText(), 10) / 100;+ // Screenshots include the translucent cursor; keep it away from the sampled edge.+ await page.mouse.move(+ zoomBounds.x + zoomBounds.width - 16,+ zoomBounds.y + zoomBounds.height - 100,+ );+ const rendered = page+ .getByRole("region", { name: "Image canvas" })+ .locator("canvas");+ const edgeSamples = [440, 460].map(+ (x) =>+ [+ zoomBounds.width / 2 + (x - 600) * zoomScale,+ zoomBounds.height / 2 + (200 - 400) * zoomScale,+ ] as const,+ );+ const edgePixels = async () =>+ (await readImage(page, await rendered.screenshot(), edgeSamples)).samples;+ const expectedEdge = [+ [48, 80, 128, 255],+ [128, 128, 128, 255],+ ];+ await expect.poll(edgePixels).toEqual(expectedEdge);+ for (const x of [60, -60]) {+ await page.mouse.wheel(x, 0);+ await expect(size).toHaveValue("1000");+ await expect(zoom).not.toHaveText(fit);+ await expect.poll(edgePixels).toEqual(expectedEdge);+ }+ await zoom.click();+ await expect(zoom).toHaveText(fit);+ await size.fill("100");+ await size.press("Enter");+ await canvas.hover();+ await expect(center).toHaveCount(0);+ await page.mouse.wheel(300, 100);+ await expect(size).toHaveValue("128");+ await expect(center).toHaveCount(1);+ await size.fill("50");+ await size.press("Enter");+ // Tiny trackpad events preserve the same scale as one combined wheel event.+ await canvas.evaluate((element) => {+ for (let i = 0; i < 100; i++) {+ element.dispatchEvent(+ new WheelEvent("wheel", {+ deltaY: 1,+ bubbles: true,+ cancelable: true,+ }),+ );+ }+ });+ await expect(size).toHaveValue("64");+ await size.fill("1");+ await size.press("Enter");+ await canvas.hover();+ await page.keyboard.press("]");+ await expect(size).toHaveValue("2");+ await page.keyboard.press("[");+ await expect(size).toHaveValue("1");+ await size.fill("128");+ await size.press("Enter");+ const bounds = await box(canvas);+ const scale = Math.min(+ (bounds.width - 48) / 1200,+ (bounds.height - 48) / 800,+ 2,+ );+ const sample = {+ x: bounds.x + bounds.width / 2 + (430 - 600) * scale,+ y: bounds.y + bounds.height / 2 + (150 - 400) * scale,+ };+ await expect+ .poll(() => readPixel(page, sample))+ .toEqual([48, 80, 128, 255]);+ await canvas.hover();+ await page.keyboard.down("Space");+ await expect(canvas).toHaveAttribute("data-pan", "true");+ await page.mouse.down();+ await page.mouse.move(+ bounds.x + bounds.width / 2 + 100,+ bounds.y + bounds.height / 2,+ );+ await page.keyboard.up("Space");+ await page.mouse.move(+ bounds.x + bounds.width / 2 + 101,+ bounds.y + bounds.height / 2,+ );+ await page.mouse.up();+ await expect+ .poll(() => readPixel(page, { ...sample, x: sample.x + 101 }))+ .toEqual([48, 80, 128, 255]);+ await page.keyboard.down("Space");+ await expect(center).toHaveCount(0);+ await page.mouse.wheel(60, 40);+ await page.keyboard.up("Space");+ await expect(size).toHaveValue("128");+ await expect+ .poll(() => readPixel(page, { x: sample.x + 41, y: sample.y - 40 }))+ .toEqual([48, 80, 128, 255]);+ expect(await page.evaluate(() => window.openlight.getState())).toEqual(+ state,+ );+ });+}diff --git a/tests/brush.e2e.ts b/tests/brush.e2e.tsindex 8ab36c3..aa7f6bd 100644--- a/tests/brush.e2e.ts+++ b/tests/brush.e2e.ts@@ -53,7 +53,11 @@ test("paint a brush mask, adjust it in the sidebar, erase, and undo", async ({page.getByRole("heading", { name: "Mask adjustments", exact: true }),).toBeVisible();const bounds = await box(canvas);- const scale = Math.min(bounds.width / 1200, bounds.height / 800, 2);+ const scale = Math.min(+ (bounds.width - 48) / 1200,+ (bounds.height - 48) / 800,+ 2,+ );const center = [bounds.x + bounds.width / 2, bounds.y + bounds.height / 2];const from = [center[0] - 150 * scale, center[1]];const to = [center[0] + 150 * scale, center[1]];@@ -83,7 +87,7 @@ test("paint a brush mask, adjust it in the sidebar, erase, and undo", async ({expect(mask).toMatchObject({ kind: "mask", name: "Brush" });if (mask?.kind === "mask" && mask.mask.kind === "brush") {expect(mask.mask.strokes).toHaveLength(1);- expect(mask.mask.strokes[0].size).toBe(200);+ expect(mask.mask.strokes[0].size).toBeCloseTo(200 / scale, 6);expect(mask.mask.strokes[0].points.length).toBeGreaterThan(2);}expect((await state()).selectedLayerId).toBe(mask?.id);diff --git a/tests/editing.e2e.ts b/tests/editing.e2e.tsindex 0c22a82..d368733 100644--- a/tests/editing.e2e.ts+++ b/tests/editing.e2e.ts@@ -452,10 +452,12 @@ test("edit a photo, inspect the preview and histograms, undo changes, and export// A neutral mask receives the same pixels, counted only where it covers them.await expect(plot).toHaveAttribute("points", /,\d{1,2}\./);await expect(plot).not.toHaveAttribute("points", whole ?? "");+ const neutral = await plot.getAttribute("points");await page.evaluate(() => {const api = window.openlight;api.setAdjustments({ exposure: 1 }, api.getState().selectedLayerId);});+ await expect(plot).not.toHaveAttribute("points", neutral ?? "");await expect(plot).toHaveAttribute("points", /,\d{1,2}\./);const points = await plot.getAttribute("points");await pagediff --git a/tests/fixtures/spots.svg b/tests/fixtures/spots.svgnew file mode 100644index 0000000..1e1ec99--- /dev/null+++ b/tests/fixtures/spots.svg@@ -0,0 +1,9 @@+<svg xmlns="http://www.w3.org/2000/svg" width="1200" height="800">+ <title>Isolated retouch spots</title>+ <rect width="1200" height="800" fill="#808080"/>+ <g fill="#305080">+ <circle cx="350" cy="160" r="6"/>+ <circle cx="350" cy="260" r="6"/>+ <circle cx="350" cy="210" r="3"/>+ </g>+</svg>diff --git a/tests/heal-edit.e2e.ts b/tests/heal-edit.e2e.tsnew file mode 100644index 0000000..a96793c--- /dev/null+++ b/tests/heal-edit.e2e.ts@@ -0,0 +1,187 @@+import { expect, test } from "./fixtures";+import { readImage } from "./images";+import { box } from "./pointer";++for (const mode of ["Remove", "Heal", "Clone"]) {+ test(`${mode} adds and subtracts separate strokes on the selected patch, on release`, async ({+ page,+ }) => {+ await page.setViewportSize({ width: 1440, height: 1000 });+ await page.goto("/");+ await page+ .locator('input[type="file"]')+ .setInputFiles("tests/fixtures/spots.svg");+ await expect(+ page.getByRole("textbox", { name: "Exposure", exact: true }),+ ).toHaveValue("0.00");+ await page.keyboard.press("h");+ await page+ .getByRole("group", { name: "Retouch mode" })+ .getByRole("button", { name: mode, exact: true })+ .click();+ const canvas = page.getByLabel("Healing canvas", { exact: true });+ const bounds = await box(canvas);+ const scale = Math.min(+ (bounds.width - 48) / 1200,+ (bounds.height - 48) / 800,+ 2,+ );+ const point = (x: number, y: number) =>+ [+ bounds.x + bounds.width / 2 + (x - 600) * scale,+ bounds.y + bounds.height / 2 + (y - 400) * scale,+ ] as const;+ const size = page.getByRole("textbox", { name: "Size", exact: true });+ await size.fill("80");+ await size.press("Enter");+ const feather = page.getByRole("textbox", { name: "Feather", exact: true });+ await feather.fill("0");+ await feather.press("Enter");+ const samples = [+ [350, 160],+ [350, 260],+ [350, 210],+ ] as const;+ const before = await readImage(page, undefined, samples);+ if (mode !== "Remove") {+ await page.keyboard.down("Alt");+ await page.mouse.click(...point(350, 560));+ await page.keyboard.up("Alt");+ }+ await page.mouse.click(...point(350, 160));+ await expect+ .poll(() =>+ page.evaluate(() => {+ const { history } = window.openlight.getState();+ return "editing" in history && history.editing;+ }),+ )+ .toBe(false);+ const patches = () =>+ page.evaluate(() => {+ const layer = window.openlight+ .getState()+ .scene?.layers.find((layer) => layer.kind === "heal");+ return layer?.kind === "heal" ? layer.patches : [];+ });+ const original = (await patches())[0];+ const layerId = await page.evaluate(+ () => window.openlight.getState().selectedLayerId,+ );+ if (!layerId) throw Error("Healing layer missing.");+ const undoCount = await page.evaluate(+ () => window.openlight.getState().history.undoCount,+ );+ await page.keyboard.down("Shift");+ await page.mouse.move(...point(350, 260));+ await page.mouse.down();+ await page.mouse.move(...point(355, 260));+ const preview = canvas.locator('[data-heal-stroke-preview="true"]');+ await expect(preview).toBeVisible();+ // Feather shortcuts must not discard a modifier stroke that is still being drawn.+ await page.keyboard.press("BracketRight");+ await expect(preview).toBeVisible();+ await page.keyboard.press("BracketLeft");+ await expect(feather).toHaveValue("0");+ expect(await patches()).toEqual([original]);+ expect(+ await page.evaluate(() => window.openlight.getState().history.undoCount),+ ).toBe(undoCount);+ await page.mouse.up();+ await page.keyboard.up("Shift");+ await expect(preview).toHaveCount(0);+ const added = (await patches())[0];+ expect(await patches()).toHaveLength(1);+ expect(added).toMatchObject({+ id: original.id,+ mode: original.mode,+ strokes: [{ mode: "paint" }, { mode: "paint" }],+ });+ if (original.mode !== "remove")+ expect(added).toHaveProperty("offset", original.offset);+ expect(+ await page.evaluate(() => window.openlight.getState().history.undoCount),+ ).toBe(undoCount + 1);+ const repaired = await readImage(page, undefined, samples);+ for (const index of [0, 1]) {+ for (const channel of repaired.samples?.[index].slice(0, 3) ?? [])+ expect(Math.abs(channel - 128)).toBeLessThanOrEqual(4);+ }+ // Separate strokes never draw a connecting segment through the uncovered gap.+ expect(repaired.samples?.[2]).toEqual(before.samples?.[2]);+ await size.fill("20");+ await size.press("Enter");+ await page.keyboard.down("Alt");+ // A modifier over the anchor paints instead of starting a destination drag.+ await page.mouse.move(...point(350, 260));+ await page.mouse.down();+ await page.mouse.move(...point(355, 260));+ await expect(preview).toBeVisible();+ expect((await patches())[0]).toEqual(added);+ await page.mouse.up();+ await page.keyboard.up("Alt");+ expect(await patches()).toMatchObject([+ {+ id: original.id,+ strokes: [{ mode: "paint" }, { mode: "paint" }, { mode: "erase" }],+ },+ ]);+ const erased = await readImage(page, undefined, samples);+ expect(erased.samples?.[1]).toEqual(before.samples?.[1]);+ expect(erased.samples?.[0]).toEqual(repaired.samples?.[0]);+ await page.keyboard.press("ControlOrMeta+z");+ expect((await patches())[0]).toEqual(added);+ expect((await readImage(page, undefined, samples)).samples).toEqual(+ repaired.samples,+ );+ await page.keyboard.press("ControlOrMeta+Shift+z");+ expect((await readImage(page, undefined, samples)).samples).toEqual(+ erased.samples,+ );+ for (const cancel of ["Escape", "pointercancel", "b"]) {+ await page+ .getByRole("button", { name: "Select patch 1", exact: true })+ .click();+ await page.keyboard.down("Shift");+ await page.mouse.move(...point(350, 210));+ await page.mouse.down();+ await expect(preview).toBeVisible();+ if (cancel === "pointercancel")+ await canvas.dispatchEvent("pointercancel", { pointerId: 1 });+ else await page.keyboard.press(cancel);+ await page.mouse.up();+ await page.keyboard.up("Shift");+ expect((await patches())[0].strokes).toHaveLength(3);+ if (cancel === "b") {+ await page.keyboard.press("h");+ await page.evaluate((id) => window.openlight.selectLayer(id), layerId);+ }+ }+ await page.mouse.click(...point(800, 500));+ await expect.poll(async () => (await patches()).length).toBe(2);+ await expect+ .poll(() =>+ page.evaluate(() => {+ const { history } = window.openlight.getState();+ return "editing" in history && history.editing;+ }),+ )+ .toBe(false);+ // Editing a selected earlier patch does not append to the last one.+ await page+ .getByRole("button", { name: "Select patch 1", exact: true })+ .click();+ await page.keyboard.down("Shift");+ await page.mouse.click(...point(390, 260));+ await page.keyboard.up("Shift");+ expect((await patches()).map((patch) => patch.strokes.length)).toEqual([+ 4, 1,+ ]);+ await page+ .getByRole("button", { name: "Select patch 2", exact: true })+ .click();+ await page.mouse.click(...point(350, 260));+ // Erased holes do not intercept clicks with the earlier painted footprint.+ await expect.poll(async () => (await patches()).length).toBe(3);+ });+}diff --git a/tests/heal-gpu.ts b/tests/heal-gpu.tsindex b8cd042..8415dee 100644--- a/tests/heal-gpu.ts+++ b/tests/heal-gpu.ts@@ -1,12 +1,18 @@import { effect, frame, init, target } from "vgpu";import { createImageLayer, createLayer } from "@/app/editor/layers";import { createEditorRenderer } from "@/app/editor/renderer";-import type { Scene } from "@/core/document";+import type { HealMode, Scene } from "@/core/document";import { createImageSource } from "@/core/image";import { imageFrame } from "@/core/image/frame";/** A dark spot crosses a luminance edge on an HDR gradient. Its donor crosses the same edge. */-export async function renderHealReference() {+export async function renderHealReference({+ mode = "heal",+ opacity = 1,+}: {+ mode?: Exclude<HealMode, "remove">;+ opacity?: number;+} = {}) {const gpu = await init();const errors: string[] = [];gpu.onError((error) => errors.push(error.message));@@ -32,15 +38,18 @@ export async function renderHealReference() {patches: [{id: "spot",+ mode,feather: 0.25,- opacity: 1,- stroke: {- mode: "paint",- size: 56,- feather: 0,- flow: 1,- points: [[128, 96, 1]],- },+ opacity,+ strokes: [+ {+ mode: "paint",+ size: 56,+ feather: 0,+ flow: 1,+ points: [[128, 96, 1]],+ },+ ],offset: [0, 60],},],@@ -68,9 +77,13 @@ export async function renderHealReference() {};for (const proxy of [false, true]) {renderer.setDisplayScale(0.25);- await renderer.update(renderScene, undefined, proxy);+ await renderer.update(renderScene, "spot", proxy);const output = renderer.fullImage();const pixels = await output.readFloats();+ const input = renderer.inputImage("spot");+ if (!input) throw Error("Patch input missing.");+ const before =+ mode === "clone" ? await input.readFloats() : new Float32Array();const scale = 256 / output.size[0];const samples = [[128, 96],@@ -79,19 +92,39 @@ export async function renderHealReference() {[100, 96],[158, 96],[64, 64],- ].map(([x, y]) => {+ ].map(([x, y], index) => {const px = Math.floor(x / scale);const py = Math.floor(y / scale);const p = [(px + 0.5) * scale, (py + 0.5) * scale];const i = (py * output.size[0] + px) * 4;+ const background = [+ 0.2 + p[0] * 0.002 + (p[0] >= 128 ? 0.5 : 0),+ 0.3 + p[1] * 0.001,+ 1.4,+ ];+ const inputPixel = (x: number, y: number) =>+ (Math.floor((y * input.size[1]) / 192) * input.size[0] ++ Math.floor((x * input.size[0]) / 256)) *+ 4;+ const original = before.slice(+ inputPixel(p[0], p[1]),+ inputPixel(p[0], p[1]) + 3,+ );+ const donor = before.slice(+ inputPixel(p[0], p[1] + 60),+ inputPixel(p[0], p[1] + 60) + 3,+ );+ const cloned =+ index < 3+ ? [...donor].map(+ (value, channel) =>+ original[channel] + (value - original[channel]) * opacity,+ )+ : background;+ const expected = mode === "clone" ? cloned : background;return {actual: [...pixels.slice(i, i + 4)],- expected: [- 0.2 + p[0] * 0.002 + (p[0] >= 128 ? 0.5 : 0),- 0.3 + p[1] * 0.001,- 1.4,- 0.75,- ],+ expected: [...expected, 0.75],};});results.push({ feather, proxy, samples });diff --git a/tests/heal-settle.e2e.ts b/tests/heal-settle.e2e.tsnew file mode 100644index 0000000..6563a26--- /dev/null+++ b/tests/heal-settle.e2e.ts@@ -0,0 +1,124 @@+import { expect, test } from "./fixtures";+import { box } from "./pointer";++for (const gesture of ["stroke", "destination"] as const) {+ test(`Remove ${gesture} survives a background painting settle and commits on release`, async ({+ page,+ }) => {+ await page.setViewportSize({ width: 1440, height: 1000 });+ await page.goto("/");+ await page.locator('input[type="file"]').setInputFiles({+ name: "small.svg",+ mimeType: "image/svg+xml",+ buffer: Buffer.from(+ '<svg xmlns="http://www.w3.org/2000/svg" width="64" height="66"><rect width="64" height="66" fill="#808080"/></svg>',+ ),+ });+ await expect(+ page.getByRole("textbox", { name: "Exposure", exact: true }),+ ).toHaveValue("0.00");+ const paint = await page.evaluate(() => {+ const id = window.openlight.addLayer("paint");+ window.openlight.setLayer(id, { visible: false });+ return id;+ });+ await page.keyboard.press("h");+ const canvas = page.getByLabel("Healing canvas", { exact: true });+ const size = page.getByRole("textbox", { name: "Size", exact: true });+ await size.fill("20");+ await size.press("Enter");+ const bounds = await box(canvas);+ const x = bounds.x + bounds.width / 2;+ const y = bounds.y + bounds.height / 2;+ if (gesture === "destination") await page.mouse.click(x, y);+ await page.evaluate((id) => {+ // The 32x33 rgba8 painting reads 33 rows aligned to 256 bytes.+ const prototype: GPUBuffer = Reflect.get(+ globalThis,+ "GPUBuffer",+ ).prototype;+ const map = prototype.mapAsync;+ const gate = new Promise<void>((resolve) => {+ window.addEventListener("release-paint-settle", () => resolve(), {+ once: true,+ });+ });+ prototype.mapAsync = async function (...args) {+ const reading = map.apply(this, args);+ if (args[2] !== 256 * 33) return reading;+ prototype.mapAsync = map;+ document.documentElement.dataset.paintSettle = "held";+ await reading;+ await gate;+ };+ for (let i = 0; i < 100; i++) {+ window.openlight.addPaintStroke(id, {+ mode: "paint",+ size: 8,+ feather: 0,+ flow: 1,+ color: "#000000",+ points: [[10, 10, 1]],+ });+ }+ }, paint);+ await expect(page.locator("html")).toHaveAttribute(+ "data-paint-settle",+ "held",+ );+ const target =+ gesture === "stroke"+ ? canvas+ : canvas.locator('[data-heal-destination-handle="true"]');+ const handle = await box(target);+ const start =+ gesture === "stroke"+ ? { x, y }+ : { x: handle.x + handle.width / 2, y: handle.y + handle.height / 2 };+ await page.mouse.move(start.x, start.y);+ await page.mouse.down();+ await page.mouse.move(start.x + 40, start.y + 20, { steps: 4 });+ const preview = canvas.locator('[data-heal-stroke-preview="true"]');+ if (gesture === "stroke") await expect(preview).toBeVisible();+ const before = await page.evaluate(() => window.openlight.getState());+ const position = await box(target);+ await page.evaluate(() =>+ window.dispatchEvent(new Event("release-paint-settle")),+ );+ await expect+ .poll(() =>+ page.evaluate((id) => {+ const layer = window.openlight+ .getState()+ .scene?.layers.find((l) => l.id === id);+ return layer?.kind === "paint" && layer.strokes.length === 0;+ }, paint),+ )+ .toBe(true);+ if (gesture === "stroke") await expect(preview).toBeVisible();+ else expect((await box(target)).x).toBeCloseTo(position.x);+ await page.mouse.move(start.x + 60, start.y + 30);+ await page.mouse.up();+ await expect(preview).toHaveCount(0);+ const state = await page.evaluate(() => window.openlight.getState());+ const layer = state.scene?.layers.find((l) => l.kind === "heal");+ expect(layer?.kind === "heal" && layer.patches.length).toBe(1);+ if (layer?.kind !== "heal") throw Error("Healing layer missing.");+ if (gesture === "stroke")+ expect(layer.patches[0].strokes[0].points.length).toBeGreaterThan(1);+ else {+ const original = before.scene?.layers.find((l) => l.kind === "heal");+ expect(layer.patches[0].strokes).not.toEqual(+ original?.kind === "heal" && original.patches[0].strokes,+ );+ }+ await page.evaluate(() => window.openlight.undo());+ const undone = await page.evaluate(() =>+ window.openlight.getState().scene?.layers.find((l) => l.kind === "heal"),+ );+ const original = before.scene?.layers.find((l) => l.kind === "heal");+ expect(undone?.kind === "heal" && undone.patches).toEqual(+ original?.kind === "heal" && original.patches,+ );+ });+}diff --git a/tests/heal.e2e.ts b/tests/heal.e2e.tsindex 4bc257e..2f097b0 100644--- a/tests/heal.e2e.ts+++ b/tests/heal.e2e.ts@@ -50,6 +50,7 @@ test("Healing paints patches, edits them, and undoes", async ({ page }) => {[600, 400],]);await page.keyboard.press("h");+ await page.keyboard.press("h");const canvas = page.getByLabel("Healing canvas", { exact: true });await expect(canvas).toBeVisible();expect(await box(canvas)).toEqual(@@ -73,7 +74,7 @@ test("Healing paints patches, edits them, and undoes", async ({ page }) => {await size.press("Enter");await expect(brushCursor).toHaveCount(0);const feather = page.getByRole("textbox", { name: "Feather", exact: true });- await expect(feather).toHaveValue("10");+ await expect(feather).toHaveValue("20");await feather.focus();await expect(brushCursor).toHaveAttribute("data-preview", "true");await feather.fill("0");@@ -83,7 +84,11 @@ test("Healing paints patches, edits them, and undoes", async ({ page }) => {await toolbar.evaluate((element) => element.getBoundingClientRect().width),).toBe(initialToolbarWidth);const bounds = await box(canvas);- const scale = Math.min(bounds.width / 1200, bounds.height / 800, 2);+ const scale = Math.min(+ (bounds.width - 48) / 1200,+ (bounds.height - 48) / 800,+ 2,+ );await page.mouse.click(bounds.x + bounds.width / 2 - 250 * scale,bounds.y + bounds.height / 2 - 200 * scale,@@ -104,6 +109,8 @@ test("Healing paints patches, edits them, and undoes", async ({ page }) => {}expect(layer.patches).toHaveLength(1);// A click in the canvas margin, where the brush cannot reach the image, creates nothing.+ await size.fill("20");+ await size.press("Enter");await page.mouse.click(bounds.x + 4, bounds.y + 4);expect(await page.evaluate(() => {@@ -115,6 +122,8 @@ test("Healing paints patches, edits them, and undoes", async ({ page }) => {};}),).toEqual({ patches: 1, editing: false });+ await size.fill("300");+ await size.press("Enter");await expect(page.getByText("Patch 1", { exact: true })).toBeVisible();const patchList = page.getByRole("list", { name: "Healing patches" });await expect(@@ -147,11 +156,13 @@ test("Healing paints patches, edits them, and undoes", async ({ page }) => {const healing = window.openlight.getState().scene?.layers.find((item) => item.kind === "heal");- return healing?.kind === "heal" ? healing.patches[0].stroke.size : 0;+ return healing?.kind === "heal" ? healing.patches[0].strokes[0].size : 0;}),- ).toBe(300);+ ).toBeCloseTo(300 / scale, 6);await size.fill("300");await size.press("Enter");+ expect(layer.patches[0]).toMatchObject({ mode: "heal" });+ if (layer.patches[0].mode === "remove") throw Error("Expected a donor patch");expect(layer.patches[0].offset).not.toEqual([0, 0]);const after = await readImage(page, undefined, [[350, 200],@@ -164,7 +175,7 @@ test("Healing paints patches, edits them, and undoes", async ({ page }) => {expect(after.samples?.[1]).toEqual(before.samples?.[1]);expect(after.samples?.[2]).toEqual([128, 128, 128, 255]);await page.evaluate(- ({ id, patch }) => window.openlight.setHealSource(id, patch, [0, 360]),+ ({ id, patch }) => window.openlight.setHealSource(id, patch, [20, 360]),{ id: layer.id, patch: layer.patches[0].id },);expect(@@ -206,7 +217,7 @@ test("Healing paints patches, edits them, and undoes", async ({ page }) => {.scene?.layers.find((layer) => layer.kind === "heal");if (layer?.kind !== "heal") throw Error("Healing layer missing");const patch = layer.patches.at(-1);- if (!patch) throw Error("Healing patch missing");+ if (!patch || patch.mode === "remove") throw Error("Healing patch missing");return { id: patch.id };});expect((await readImage(page, undefined, [[350, 200]])).samples?.[0]).toEqual(@@ -232,10 +243,12 @@ test("Healing paints patches, edits them, and undoes", async ({ page }) => {.getState().scene?.layers.find((item) => item.kind === "heal");return healing?.kind === "heal"- ? healing.patches.map(({ stroke, offset }) => [- Math.round(stroke.points[0][0] + offset[0]),- Math.round(stroke.points[0][1] + offset[1]),- ])+ ? healing.patches+ .filter((patch) => patch.mode !== "remove")+ .map(({ strokes, offset }) => [+ Math.round(strokes[0].points[0][0] + offset[0]),+ Math.round(strokes[0].points[0][1] + offset[1]),+ ]): [];});expect(donors[1]).not.toEqual(donors[0]);@@ -266,30 +279,41 @@ test("Healing paints patches, edits them, and undoes", async ({ page }) => {healing?.kind === "heal"? healing.patches.find((item) => item.id === patchId): undefined;- if (!patch) throw Error("Healing patch missing");- return Math.round(patch.stroke.points[0][0] + patch.offset[0]);+ if (!patch || patch.mode === "remove")+ throw Error("Healing patch missing");+ return Math.round(patch.strokes[0].points[0][0] + patch.offset[0]);}, manual.id);const beforeDrag = await sourceX();const handle = canvas.locator('[data-heal-source-handle="true"]');const handleBounds = await handle.boundingBox();if (!handleBounds) throw Error("Healing source handle is unavailable");- await handle.hover();+ const grabX = handleBounds.x + handleBounds.width / 2 + 11;+ const grabY = handleBounds.y + handleBounds.height / 2;+ await page.mouse.move(grabX, grabY);+ await expect(handle).toHaveCSS("cursor", "grab");await expect(canvas.locator("svg:has(radialGradient)")).toHaveCount(0);await page.mouse.move(bounds.x + 10, bounds.y + 10);await expect(canvas.locator("svg:has(radialGradient)")).toHaveCount(1);- await page.mouse.move(- handleBounds.x + handleBounds.width / 2,- handleBounds.y + handleBounds.height / 2,- );+ await page.mouse.move(grabX, grabY);await page.mouse.down();- await page.mouse.move(- handleBounds.x + handleBounds.width / 2 + 30,- handleBounds.y + handleBounds.height / 2,- );+ await page.mouse.move(grabX + 30, grabY);// The repair follows the donor while it is dragged, not only once it drops.await expect.poll(sourceX).not.toBe(beforeDrag);await page.mouse.up();const fixedSource = await sourceX();+ const sourceBounds = await box(handle);+ const sourceCenter = [+ sourceBounds.x + sourceBounds.width / 2,+ sourceBounds.y + sourceBounds.height / 2,+ ];+ await page.mouse.move(sourceCenter[0], sourceCenter[1]);+ await page.mouse.down();+ await page.mouse.move(sourceCenter[0] + 20, sourceCenter[1]);+ await expect.poll(sourceX).not.toBe(fixedSource);+ await page.keyboard.press("ControlOrMeta+z");+ await page.mouse.move(sourceCenter[0] + 40, sourceCenter[1]);+ await page.mouse.up();+ expect(await sourceX()).toBe(fixedSource);const beforeDestination = await page.evaluate((patchId) => {const healing = window.openlight.getState()@@ -298,7 +322,7 @@ test("Healing paints patches, edits them, and undoes", async ({ page }) => {healing?.kind === "heal"? healing.patches.find((item) => item.id === patchId): undefined;- return patch?.stroke.points[0][0];+ return patch?.strokes[0].points[0][0];}, manual.id);const destination = canvas.locator('[data-heal-destination-handle="true"]');const destinationBounds = await destination.boundingBox();@@ -323,7 +347,7 @@ test("Healing paints patches, edits them, and undoes", async ({ page }) => {healing?.kind === "heal"? healing.patches.find((item) => item.id === patchId): undefined;- return patch?.stroke.points[0][0];+ return patch?.strokes[0].points[0][0];}, manual.id),).not.toBe(beforeDestination);@@ -370,7 +394,7 @@ test("Healing paints patches, edits them, and undoes", async ({ page }) => {).toHaveValue("50");});-test("Healing takes the first stroke after H and finds donors inside a mask", async ({+test("Healing takes the first stroke after selecting Heal and finds donors inside a mask", async ({page,}) => {await page.goto("/");@@ -402,6 +426,7 @@ test("Healing takes the first stroke after H and finds donors inside a mask", as});}+ await page.keyboard.press("h");await page.keyboard.press("h");await page.mouse.click(spot[0], spot[1]);expect(await healedPatches()).toHaveLength(1);@@ -428,5 +453,219 @@ test("Healing takes the first stroke after H and finds donors inside a mask", asawait page.evaluate(() => window.openlight.getState().selectedLayerId),).toBe(nested);expect(patches).toHaveLength(1);+ if (patches[0].mode === "remove") throw Error("Expected a donor patch");expect(patches[0].offset).not.toEqual([0, 0]);});++test("Clone copies donor color without correction, with feather, opacity, HDR, and proxy rendering", async ({+ page,+}) => {+ await page.goto("/tests/gpu.html");+ for (const opacity of [1, 0.4]) {+ const result = await page.evaluate(async (opacity) => {+ const path = "/tests/heal-gpu.ts";+ const { renderHealReference } = (await import(+ path+ )) as typeof import("./heal-gpu");+ return renderHealReference({ mode: "clone", opacity });+ }, opacity);+ expect(result.errors).toEqual([]);+ for (const { samples } of result.results) {+ for (const [index, { actual, expected }] of samples.entries()) {+ if (index === 3) continue;+ for (let channel = 0; channel < 4; channel++) {+ expect(Math.abs(actual[channel] - expected[channel])).toBeLessThan(+ 0.015,+ );+ }+ }+ }+ for (const proxy of [false, true]) {+ const edges = result.results+ .filter((result) => result.proxy === proxy)+ .map(({ samples }) => samples[3].actual);+ expect(edges[1][1]).toBeLessThan(edges[0][1] - 0.001);+ expect(edges[1][3]).toBeCloseTo(0.75, 3);+ }+ }+});++test("retouch modes cycle, preserve each family's brush, and keep patches independent", async ({+ page,+}) => {+ await page.setViewportSize({ width: 1440, height: 1000 });+ await page.goto("/");+ await page+ .locator('input[type="file"]')+ .setInputFiles("tests/fixtures/photo.svg");+ await expect(+ page.getByRole("textbox", { name: "Exposure", exact: true }),+ ).toHaveValue("0.00");+ await page.keyboard.press("h");+ const canvas = page.getByLabel("Healing canvas", { exact: true });+ await expect(canvas).toBeVisible();+ const bounds = await box(canvas);+ const scale = Math.min(+ (bounds.width - 48) / 1200,+ (bounds.height - 48) / 800,+ 2,+ );+ const point = (x: number, y: number) => [+ bounds.x + bounds.width / 2 + (x - 600) * scale,+ bounds.y + bounds.height / 2 + (y - 400) * scale,+ ];+ const source = point(600, 400);+ const target = point(350, 200);+ const size = page.getByRole("textbox", { name: "Size", exact: true });+ const feather = page.getByRole("textbox", { name: "Feather", exact: true });+ await size.fill("20");+ await size.press("Enter");+ await feather.fill("35");+ await feather.press("Enter");+ const modes = page.getByRole("group", { name: "Retouch mode" });+ const heal = modes.getByRole("button", { name: "Heal", exact: true });+ const clone = modes.getByRole("button", { name: "Clone", exact: true });+ const remove = modes.getByRole("button", { name: "Remove", exact: true });+ expect(+ await modes+ .getByRole("button")+ .evaluateAll((buttons) =>+ buttons.map((button) => button.getAttribute("aria-label")),+ ),+ ).toEqual(["Remove", "Heal", "Clone"]);+ await expect(remove).toHaveAttribute("aria-pressed", "true");+ await page.keyboard.press("b");+ await page.keyboard.press("h");+ await expect(remove).toHaveAttribute("aria-pressed", "true");+ for (const name of ["Heal", "Clone", "Remove", "Heal"]) {+ await page.keyboard.press("h");+ await expect(+ modes.getByRole("button", { name, exact: true }),+ ).toHaveAttribute("aria-pressed", "true");+ }+ const layerId = await page.evaluate(+ () => window.openlight.getState().selectedLayerId,+ );+ if (!layerId) throw Error("Healing layer missing.");+ await clone.click();+ await expect(clone).toHaveAttribute("aria-pressed", "true");+ await page.keyboard.down("Alt");+ await page.mouse.click(source[0], source[1]);+ await page.keyboard.up("Alt");+ await page.mouse.click(target[0], target[1]);+ async function patches() {+ await expect+ .poll(() =>+ page.evaluate(() => {+ const { history } = window.openlight.getState();+ return "editing" in history && history.editing;+ }),+ )+ .toBe(false);+ return page.evaluate(() => {+ const layer = window.openlight+ .getState()+ .scene?.layers.find((layer) => layer.kind === "heal");+ return layer?.kind === "heal" ? layer.patches : [];+ });+ }+ expect(await patches()).toMatchObject([+ {+ mode: "clone",+ feather: 0.35,+ strokes: [{ size: expect.closeTo(20 / scale, 6) }],+ },+ ]);+ const copied = await readImage(page, undefined, [+ [350, 200],+ [600, 400],+ ]);+ expect(copied.samples?.[0]).toEqual(copied.samples?.[1]);+ await page.mouse.click(source[0], source[1], { button: "right" });+ const menu = page.getByRole("dialog");+ await expect(+ menu.getByRole("textbox", { name: "Feather", exact: true }),+ ).toHaveValue("35");+ await menu.getByRole("textbox", { name: "Feather", exact: true }).fill("45");+ await menu+ .getByRole("textbox", { name: "Feather", exact: true })+ .press("Enter");+ await page.keyboard.press("Escape");+ await expect(feather).toHaveValue("45");+ await page.keyboard.press("b");+ await size.fill("70");+ await size.press("Enter");+ await feather.fill("60");+ await feather.press("Enter");+ await page.keyboard.press("c");+ await page.keyboard.press("h");+ await expect(size).toHaveValue("20");+ await expect(feather).toHaveValue("45");+ await expect(clone).toHaveAttribute("aria-pressed", "true");+ await heal.click();+ await expect(heal).toHaveAttribute("aria-pressed", "true");+ await page.evaluate((id) => window.openlight.selectLayer(id), layerId);+ await page.keyboard.press("Shift+BracketRight");+ await expect(feather).toHaveValue("60");+ await page.keyboard.press("Shift+BracketLeft");+ await expect(feather).toHaveValue("50");+ await page.keyboard.down("Alt");+ await page.mouse.click(source[0], source[1]);+ await page.keyboard.up("Alt");+ const second = point(850, 400);+ await page.mouse.click(second[0], second[1]);+ expect(await patches()).toHaveLength(2);+ await page.keyboard.press("h");+ await expect(clone).toHaveAttribute("aria-pressed", "true");+ const third = point(900, 400);+ await page.mouse.click(third[0], third[1]);+ expect(await patches()).toMatchObject([+ {+ mode: "clone",+ feather: 0.45,+ strokes: [{ size: expect.closeTo(20 / scale, 6) }],+ },+ {+ mode: "heal",+ feather: 0.5,+ strokes: [{ size: expect.closeTo(20 / scale, 6) }],+ },+ {+ mode: "clone",+ feather: 0.5,+ strokes: [{ size: expect.closeTo(20 / scale, 6) }],+ },+ ]);+ await page.keyboard.press("ControlOrMeta+z");+ expect(await patches()).toHaveLength(2);+ await page.keyboard.press("ControlOrMeta+Shift+z");+ expect(await patches()).toHaveLength(3);+ await page+ .getByRole("button", { name: "Select patch 1", exact: true })+ .click();+ await expect(clone).toHaveAttribute("aria-pressed", "true");+ await expect(feather).toHaveValue("45");+ const cursorFeather = canvas+ .locator('[data-brush-cursor="true"] stop')+ .nth(1);+ await page.mouse.move(target[0] + 80, target[1]);+ await expect(cursorFeather).toHaveAttribute("offset", "0.55");+ await feather.fill("80");+ await feather.press("Enter");+ await page.mouse.move(target[0] + 80, target[1]);+ await expect(feather).toHaveValue("80");+ await expect(cursorFeather).toHaveAttribute("offset", `${1 - 0.8}`);+ await page.keyboard.press("ControlOrMeta+z");+ await expect(feather).toHaveValue("45");+ await expect(cursorFeather).toHaveAttribute("offset", "0.55");+ const next = point(950, 600);+ await page.mouse.click(next[0], next[1]);+ expect((await patches()).at(-1)?.feather).toBe(0.45);+ await page+ .getByRole("button", { name: "Select patch 2", exact: true })+ .click();+ await expect(heal).toHaveAttribute("aria-pressed", "true");+ await page.keyboard.press("b");+ await expect(size).toHaveValue("70");+ await expect(feather).toHaveValue("60");+});diff --git a/tests/heal.test.ts b/tests/heal.test.tsindex 24a36ec..c5310cf 100644--- a/tests/heal.test.ts+++ b/tests/heal.test.ts@@ -1,6 +1,6 @@-import { expect, test } from "bun:test";+import { expect, spyOn, test } from "bun:test";import { init, target } from "vgpu/mock";-import { createImageLayer, createLayer } from "@/app/editor/layers";+import { createImageLayer, createLayer, createMask } from "@/app/editor/layers";import { createEditorRenderer } from "@/app/editor/renderer";import {type BrushStroke,@@ -10,8 +10,12 @@ import {} from "@/core/document";import { createImageSource } from "@/core/image";import { imageFrame } from "@/core/image/frame";+import { createRenderGraph, input } from "@/core/renderer";+import { setAdjustments } from "@/features/adjustments/edits";import {addHealPatch,+ addHealStroke,+ addRemovePatch,deleteHealPatch,duplicateHealPatch,extendHealPatch,@@ -19,7 +23,8 @@ import {setHealPatch,setHealSource,} from "@/features/heal/edits";-import { addLayer, deleteLayer } from "@/features/layers/edits";+import { inpaintField } from "@/features/heal/inpaint";+import { addLayer, deleteLayer, setLayer } from "@/features/layers/edits";function healFixture(size: readonly [number, number] = [64, 64]) {const resources = createResources();@@ -42,12 +47,220 @@ const dab: BrushStroke = {points: [[10, 10, 1]],};+test("Remove passes never overwrite distinct uniforms before their frame submits", async () => {+ const gpu = await init();+ const source = target(gpu, { size: [64, 64], format: "rgba16float" });+ const coverage = target(gpu, { size: [64, 64], format: "r8unorm" });+ const graph = createRenderGraph(gpu);+ const uploads = spyOn(gpu.gpu.queue, "writeBuffer");+ try {+ graph.render([+ inpaintField(+ input(source),+ { coverage: input(coverage), origin: [0, 0] },+ { origin: [0, 0], extent: source.size },+ "remove",+ ),+ ]);+ const uniforms = new Map<GPUBuffer, Uint8Array>();+ for (const [buffer, , data] of uploads.mock.calls) {+ const bytes = ArrayBuffer.isView(data)+ ? new Uint8Array(data.buffer, data.byteOffset, data.byteLength)+ : new Uint8Array(data);+ const previous = uniforms.get(buffer);+ if (previous) expect(bytes).toEqual(previous);+ uniforms.set(buffer, bytes.slice());+ }+ expect(uniforms.size).toBeGreaterThan(1);+ } finally {+ uploads.mockRestore();+ graph.dispose();+ coverage.color.dispose();+ source.color.dispose();+ gpu.dispose();+ }+});++test("patch strokes add, erase, replay on undo, and move together while retaining the donor", async () => {+ const gpu = await init();+ const source = createImageSource(+ target(gpu, { size: [1024, 1024], format: "rgba16float" }),+ );+ const { document, layer } = healFixture([1024, 1024]);+ const renderer = createEditorRenderer(gpu, source);+ try {+ const id = addHealPatch(document, layer, dab, [20, 0], "clone");+ await renderer.update(document.scene.getState());+ expect(renderer.inspect().stamped).toBe(1);+ addHealStroke(document, layer, id, { ...dab, points: [[30, 30, 1]] });+ await renderer.update(document.scene.getState());+ expect(renderer.inspect().stamped).toBe(2);+ addHealStroke(document, layer, id, { ...dab, mode: "erase", size: 1000 });+ await renderer.update(document.scene.getState());+ // Erasure outside the patch never grows its raster or repeats earlier dabs.+ expect(renderer.inspect().stamped).toBe(3);+ expect(renderer.inspect().rasters[0].size).toEqual([256, 256]);+ await renderer.update(document.scene.getState());+ expect(renderer.inspect().stamped).toBe(3);+ document.history.undo();+ await renderer.update(document.scene.getState());+ expect(renderer.inspect().stamped).toBe(5);+ document.history.redo();+ await renderer.update(document.scene.getState());+ expect(renderer.inspect().stamped).toBe(6);+ setHealDestination(document, layer, id, [20, 25]);+ expect(patchesOf(document, layer)).toMatchObject([+ {+ id,+ mode: "clone",+ offset: [10, -15],+ strokes: [+ { mode: "paint", points: [[20, 25, 1]] },+ { mode: "paint", points: [[40, 45, 1]] },+ { mode: "erase", points: [[20, 25, 1]] },+ ],+ },+ ]);+ } finally {+ renderer.dispose();+ document.dispose();+ source.dispose();+ gpu.dispose();+ }+});++test("Remove patches edit, move, duplicate, and undo without a donor", () => {+ const { document, layer } = healFixture();+ try {+ document.history.begin();+ const patch = addRemovePatch(document, layer, { ...dab, feather: 0.3 });+ extendHealPatch(document, layer, [[12, 14, 1]]);+ document.history.commit();+ setHealPatch(document, layer, patch, { opacity: 0.5 });+ setHealDestination(document, layer, patch, [20, 30]);+ const copy = duplicateHealPatch(document, layer, patch);+ expect(patchesOf(document, layer)).toMatchObject([+ {+ id: patch,+ mode: "remove",+ opacity: 0.5,+ feather: 0.3,+ strokes: [+ {+ points: [+ [20, 30, 1],+ [22, 34, 1],+ ],+ },+ ],+ },+ { id: copy, mode: "remove" },+ ]);+ expect(patchesOf(document, layer)[0]).not.toHaveProperty("offset");+ expect(() => setHealSource(document, layer, patch, [10, 10])).toThrow(+ "no donor",+ );+ document.history.undo();+ expect(patchesOf(document, layer)).toHaveLength(1);+ document.history.undo();+ document.history.undo();+ document.history.undo();+ expect(patchesOf(document, layer)).toHaveLength(0);+ document.history.redo();+ expect(patchesOf(document, layer)[0].mode).toBe("remove");+ } finally {+ document.dispose();+ }+});+function patchesOf(document: ReturnType<typeof createDocument>, layer: string) {const healing = findLayer(document.scene.getState().layers, layer);if (healing?.kind !== "heal") throw Error("Healing layer missing.");return healing.patches;}+test("a nested Remove retains synthesis when bypassed and invalidates upstream content and proxy changes", async () => {+ const gpu = await init();+ const source = createImageSource(+ target(gpu, { size: [128, 96], format: "rgba16float" }),+ );+ const { document } = healFixture([128, 96]);+ const renderer = createEditorRenderer(gpu, source);+ const mask = addLayer(+ document,+ createMask({ kind: "linear", start: [0, 0], end: [0, 96] }),+ );+ const layer = addLayer(document, createLayer("heal"), { inside: mask });+ const patch = addRemovePatch(document, layer, {+ ...dab,+ points: [[64, 48, 1]],+ });+ const solves = () =>+ renderer.inspect().passes.some((name) => name.includes("/inpaint/"));+ try {+ await renderer.update(document.scene.getState());+ expect(solves()).toBe(true);+ setHealPatch(document, layer, patch, { opacity: 0.5, feather: 0.4 });+ await renderer.update(document.scene.getState());+ expect(solves()).toBe(false);+ for (const id of [layer, mask]) {+ setLayer(document, id, { visible: false });+ await renderer.update(document.scene.getState());+ expect(renderer.inspect().cachedTextures).toHaveLength(1);+ setLayer(document, id, { visible: true });+ await renderer.update(document.scene.getState());+ expect(solves()).toBe(false);+ setLayer(document, id, { opacity: 0 });+ await renderer.update(document.scene.getState());+ expect(renderer.inspect().cachedTextures).toHaveLength(1);+ setLayer(document, id, { opacity: 1 });+ await renderer.update(document.scene.getState());+ expect(solves()).toBe(false);+ }+ for (const mode of ["paint", "erase"] as const) {+ addHealStroke(document, layer, patch, {+ ...dab,+ mode,+ size: 2,+ points: [[64, 48, 1]],+ });+ await renderer.update(document.scene.getState());+ expect(solves()).toBe(true);+ setHealPatch(document, layer, patch, {+ opacity: mode === "paint" ? 0.6 : 0.5,+ });+ await renderer.update(document.scene.getState());+ expect(solves()).toBe(false);+ }+ setLayer(document, mask, { opacity: 0.5 });+ await renderer.update(document.scene.getState());+ expect(solves()).toBe(false);+ setLayer(document, mask, { visible: false });+ await renderer.update(document.scene.getState());+ setAdjustments(document, { exposure: 1 }, mask);+ await renderer.update(document.scene.getState());+ expect(solves()).toBe(false);+ setLayer(document, mask, { visible: true });+ await renderer.update(document.scene.getState());+ expect(solves()).toBe(true);+ renderer.setDisplayScale(0.5);+ await renderer.update(document.scene.getState(), undefined, true);+ expect(solves()).toBe(true);+ await renderer.update(document.scene.getState());+ expect(solves()).toBe(false);+ setLayer(document, mask, { visible: false });+ await renderer.update(document.scene.getState());+ deleteLayer(document, mask);+ await renderer.update(document.scene.getState());+ expect(renderer.inspect().cachedTextures).toEqual([]);+ } finally {+ renderer.dispose();+ document.dispose();+ source.dispose();+ gpu.dispose();+ }+});+test("heal patches reuse brush rasters, scale with the proxy, undo, and release with the layer", async () => {const gpu = await init();const source = createImageSource(@@ -80,7 +293,7 @@ test("heal patches reuse brush rasters, scale with the proxy, undo, and releaseexpect(created?.kind === "heal" &&created.patches.find((item) => item.id === patch),- ).toMatchObject({ feather: 0.4, stroke: { size: 30, feather: 0 } });+ ).toMatchObject({ feather: 0.4, strokes: [{ size: 30, feather: 0 }] });renderer.setDisplayScale(0.25);await renderer.update(document.scene.getState(), id, true);expect(renderer.fullImage().size).toEqual([64, 48]);@@ -199,7 +412,11 @@ test("one Healing layer composes its patches in order through render nodes", asypoints: [[64, 48, 1]],};const first = addHealPatch(document, layer, stroke, [30, 0]);- const second = addHealPatch(document, layer, stroke, [-30, 0]);+ const second = addHealPatch(document, layer, stroke, [-30, 0], "clone");+ expect(patchesOf(document, layer).map((patch) => patch.mode)).toEqual([+ "heal",+ "clone",+ ]);await renderer.update(document.scene.getState(), layer);expect(renderer.inspect().passes).toContain(`layer/${layer}/${first}/blend`,@@ -215,6 +432,11 @@ test("one Healing layer composes its patches in order through render nodes", asyexpect(renderer.inputImage(second)).toBeDefined();expect(renderer.inputImage(second)).not.toBe(firstInput);const correctionGraph = renderer.inspect();+ expect(+ correctionGraph.passes.filter((name) =>+ name.startsWith(`layer/${layer}/${second}/`),+ ),+ ).toEqual([`layer/${layer}/${second}/blend`]);setHealDestination(document, layer, first, [80, 60]);setHealPatch(document, layer, first, { feather: 0.2, opacity: 0.6 });await renderer.update(document.scene.getState(), second);@@ -224,7 +446,7 @@ test("one Healing layer composes its patches in order through render nodes", asyexpect(patchesOf(document, layer)).toMatchObject([{id: first,- stroke: { points: [[80, 60, 1]], size: 24, feather: 0 },+ strokes: [{ points: [[80, 60, 1]], size: 24, feather: 0 }],offset: [14, -12],feather: 0.2,opacity: 0.6,diff --git a/tests/inpaint-benchmark.ts b/tests/inpaint-benchmark.tsnew file mode 100644index 0000000..c035cd6--- /dev/null+++ b/tests/inpaint-benchmark.ts@@ -0,0 +1,180 @@+import { effect, frame, init, type Timer, target, timer } from "vgpu";+import { createImageLayer, createLayer } from "@/app/editor/layers";+import { createEditorRenderer } from "@/app/editor/renderer";+import type { BrushStroke, Scene } from "@/core/document";+import { createImageSource } from "@/core/image";+import { imageFrame } from "@/core/image/frame";+import { createRenderGraph, input } from "@/core/renderer";+import { createPatchRaster } from "@/core/renderer/mask/patches";+import { createStrokes } from "@/core/renderer/strokes";+import { inpaint } from "@/features/heal/inpaint";+import { patchBounds } from "@/features/heal/model";++function summarize(values: number[]) {+ const sorted = values.toSorted((a, b) => a - b);+ return {+ samples: values,+ median:+ (sorted[Math.floor((sorted.length - 1) / 2)] ++ sorted[Math.ceil((sorted.length - 1) / 2)]) /+ 2,+ p95: sorted[Math.ceil(sorted.length * 0.95) - 1],+ };+}++/** Recompute a moving removal, isolated or composed, without React or image decoding in the timings. */+export async function benchmarkInpaint(+ scope: "solver" | "editor",+ warmup = 8,+ samples = 40,+) {+ const adapter = await navigator.gpu.requestAdapter();+ if (!adapter) throw Error("WebGPU is unavailable.");+ const timestamps = adapter.features.has("timestamp-query");+ const gpu = await init({+ requiredFeatures: timestamps ? ["timestamp-query"] : [],+ });+ const errors: string[] = [];+ gpu.onError((error) => errors.push(error.message));+ const size: [number, number] = [2400, 1600];+ const image = target(gpu, { size, format: "rgba16float" });+ const source = createImageSource(image);+ const base = createImageLayer("source", "Benchmark");+ const clock = timestamps ? timer(gpu) : undefined;+ const measurements: Record<string, number>[] = [];+ clock?.onResults((spans) => measurements.push(spans));+ function strokeAt(index: number): BrushStroke {+ return {+ mode: "paint",+ size: 240,+ feather: 0,+ flow: 1,+ points: [[1200 + (index % 2), 800, 1]],+ };+ }+ function create(clock?: Timer) {+ if (scope === "editor") {+ const editor = createEditorRenderer(gpu, source, { timer: clock });+ return {+ async render(index: number) {+ const scene: Scene = {+ frame: imageFrame(size),+ layers: [+ base,+ {+ ...createLayer("heal"),+ id: "heal",+ patches: [+ {+ id: "spot",+ mode: "remove",+ feather: 0.4,+ opacity: 1,+ strokes: [strokeAt(index)],+ },+ ],+ },+ ],+ };+ await editor.update(scene);+ },+ inspect: editor.inspect,+ dispose: editor.dispose,+ };+ }+ const graph = createRenderGraph(gpu, clock);+ const strokes = createStrokes(gpu);+ const raster = createPatchRaster(gpu, strokes);+ return {+ async render(index: number) {+ const stroke = strokeAt(index);+ const coverage = raster.patch("spot", [stroke], size);+ graph.render([+ inpaint(+ input(image),+ coverage,+ patchBounds([stroke], size, 240),+ "inpaint",+ ),+ ]);+ },+ inspect: graph.inspect,+ dispose() {+ graph.dispose();+ raster.dispose();+ strokes.dispose();+ },+ };+ }+ let engine = create();+ async function measure() {+ const encoded: number[] = [];+ const completed: number[] = [];+ const duration: number[] = [];+ for (let index = 0; index < warmup + samples; index++) {+ const start = performance.now();+ await engine.render(index);+ const submitted = performance.now();+ await gpu.gpu.queue.onSubmittedWorkDone();+ const end = performance.now();+ await gpu.settled();+ const spans = measurements.pop();+ if (errors.length) throw Error(errors[0]);+ if (index < warmup) continue;+ encoded.push(submitted - start);+ completed.push(end - start);+ if (spans)+ duration.push(Object.values(spans).reduce((sum, ms) => sum + ms, 0));+ }+ return {+ cpuEncodeMs: summarize(encoded),+ completedMs: summarize(completed),+ gpuMs: duration.length ? summarize(duration) : null,+ };+ }+ try {+ const fill = effect(+ gpu,+ `+ @fragment fn fs_main(@builtin(position) p: vec4f) -> @location(0) vec4f {+ return vec4f(0.2 + p.x / 4000.0, 0.3 + p.y / 3000.0, 1.4, 1.0);+ }+ `,+ );+ frame(gpu, (f) => f.pass(image, fill));+ await gpu.gpu.queue.onSubmittedWorkDone();+ const start = performance.now();+ await engine.render(0);+ await gpu.gpu.queue.onSubmittedWorkDone();+ const firstRenderMs = performance.now() - start;+ const rendering = await measure();+ const storage = engine.inspect();+ engine.dispose();+ engine = create(clock);+ const profile = clock ? await measure() : null;+ return {+ scope,+ size,+ diameter: 240,+ gridLimit: 512,+ warmup,+ samples,+ timestamps,+ adapter: {+ vendor: adapter.info.vendor,+ architecture: adapter.info.architecture,+ device: adapter.info.device,+ description: adapter.info.description,+ },+ firstRenderMs,+ rendering,+ profile,+ storage,+ };+ } finally {+ engine.dispose();+ source.dispose();+ clock?.dispose();+ gpu.dispose();+ }+}diff --git a/tests/inpaint-gpu.ts b/tests/inpaint-gpu.tsnew file mode 100644index 0000000..777517b--- /dev/null+++ b/tests/inpaint-gpu.ts@@ -0,0 +1,225 @@+import { effect, frame, init, target } from "vgpu";+import { encodeImage } from "@/app/editor/export/export-image";+import { createImageLayer, createLayer } from "@/app/editor/layers";+import { createEditorRenderer } from "@/app/editor/renderer";+import type { HealPatch, Scene } from "@/core/document";+import { createImageSource } from "@/core/image";+import { imageFrame } from "@/core/image/frame";++export type InpaintFixture =+ | "flat"+ | "gradient"+ | "texture"+ | "edge"+ | "structure";++/** Known backgrounds make removal measurable, including cases that cannot be inferred locally. */+export async function renderInpaintReference(+ fixture: InpaintFixture,+ diameter = 32,+ proxy = false,+) {+ const gpu = await init();+ const errors: string[] = [];+ gpu.onError((error) => errors.push(error.message));+ const size: [number, number] = [192, 144];+ const image = target(gpu, { size, format: "rgba16float" });+ const clean = target(gpu, { size, format: "rgba16float" });+ const source = createImageSource(image);+ const renderer = createEditorRenderer(gpu, source);+ const backgrounds: Record<InpaintFixture, string> = {+ flat: "vec3f(0.25, 0.55, 1.4)",+ gradient: "vec3f(0.25 + p.x * 0.002, 0.3 + p.y * 0.001, 1.4)",+ texture:+ "vec3f(0.3, 0.5, 0.7) + 0.12 * sin(p.x * 0.785398) * cos(p.y * 0.785398)",+ edge: "select(vec3f(0.2, 0.35, 0.6), vec3f(0.65, 0.5, 0.3), p.y >= 72.0)",+ structure:+ "select(vec3f(0.55, 0.22, 0.12), vec3f(0.75), (u32(p.y) % 24u < 2u) || (u32(p.x + f32(u32(p.y) / 24u % 2u) * 24.0) % 48u < 2u))",+ };+ const shader = (defect: boolean) =>+ effect(+ gpu,+ `+ struct Params { damaged: u32, radius: f32 }+ @group(0) @binding(0) var<uniform> params: Params;+ @fragment fn fs_main(@builtin(position) p: vec4f) -> @location(0) vec4f {+ let background = ${backgrounds[fixture]};+ let defect = params.damaged != 0u && distance(p.xy, vec2f(96.0, 72.0)) < params.radius;+ return vec4f(select(background, vec3f(0.015), defect), 0.75);+ }+ `,+ { set: { params: { damaged: Number(defect), radius: diameter / 2 } } },+ );+ const patch: HealPatch = {+ id: "object",+ mode: "remove",+ feather: 0,+ opacity: 1,+ strokes: [+ {+ mode: "paint",+ size: diameter + 8,+ feather: 0,+ flow: 1,+ points: [[96, 72, 1]],+ },+ ],+ };+ const healing = { ...createLayer("heal"), patches: [patch] };+ const scene: Scene = {+ frame: imageFrame(size),+ layers: [createImageLayer("source", "Reference"), healing],+ };+ try {+ frame(gpu, (f) => {+ f.pass(image, shader(true));+ f.pass(clean, shader(false));+ });+ await gpu.gpu.queue.onSubmittedWorkDone();+ renderer.setDisplayScale(0.5);+ const start = performance.now();+ await renderer.update(scene, undefined, proxy);+ await gpu.gpu.queue.onSubmittedWorkDone();+ const completedMs = performance.now() - start;+ const solved = renderer.inspect();+ const result = renderer.fullImage();+ const actual = await result.readFloats();+ const before = await image.readFloats();+ const expected = await clean.readFloats();+ let errorBefore = 0;+ let errorAfter = 0;+ let count = 0;+ let outsideError = 0;+ let alphaError = 0;+ const scale = size[0] / result.size[0];+ for (let y = 0; y < result.size[1]; y++) {+ for (let x = 0; x < result.size[0]; x++) {+ const px = Math.floor((x + 0.5) * scale);+ const py = Math.floor((y + 0.5) * scale);+ const index = (y * result.size[0] + x) * 4;+ const reference = (py * size[0] + px) * 4;+ const distance = Math.hypot(px - 96, py - 72);+ for (let channel = 0; channel < 3; channel++) {+ if (distance < diameter / 2 - 2) {+ errorBefore += Math.abs(+ before[reference + channel] - expected[reference + channel],+ );+ errorAfter += Math.abs(+ actual[index + channel] - expected[reference + channel],+ );+ count++;+ } else if (!proxy && distance > diameter / 2 + 6) {+ outsideError = Math.max(+ outsideError,+ Math.abs(actual[index + channel] - before[reference + channel]),+ );+ }+ }+ alphaError = Math.max(alphaError, Math.abs(actual[index + 3] - 0.75));+ }+ }+ const images = await Promise.all(+ [image, result, clean].map(async (target) => [+ ...new Uint8Array(await (await encodeImage(gpu, target)).arrayBuffer()),+ ]),+ );+ await renderer.update(+ { ...scene, layers: [scene.layers[0], { ...healing, visible: false }] },+ undefined,+ proxy,+ );+ const hiddenCaches = renderer.inspect().cachedTextures.length;+ await renderer.update(scene, undefined, proxy);+ const shown = renderer.inspect();+ const visible = await renderer.fullImage().readFloats();+ const visibilityError = visible.reduce(+ (maximum, value, index) =>+ Math.max(maximum, Math.abs(value - actual[index])),+ 0,+ );+ await renderer.update(+ {+ ...scene,+ layers: [+ scene.layers[0],+ { ...healing, patches: [{ ...patch, opacity: 0.5 }] },+ ],+ },+ undefined,+ proxy,+ );+ const cached = renderer.inspect();+ await renderer.update(+ {+ ...scene,+ layers: [+ {+ ...scene.layers[0],+ adjustments: { ...scene.layers[0].adjustments, exposure: 0.5 },+ },+ healing,+ ],+ },+ undefined,+ proxy,+ );+ const changed = renderer.inspect();+ await renderer.update(+ {+ ...scene,+ layers: [+ {+ ...scene.layers[0],+ adjustments: { ...scene.layers[0].adjustments, exposure: 1 },+ },+ healing,+ ],+ },+ undefined,+ proxy,+ );+ await renderer.update(scene, undefined, proxy);+ const restored = await renderer.fullImage().readFloats();+ const restoredError = restored.reduce(+ (maximum, value, index) =>+ Math.max(maximum, Math.abs(value - actual[index])),+ 0,+ );+ await renderer.update({ ...scene, layers: [scene.layers[0]] });+ const released = renderer.inspect();+ await gpu.settled();+ return {+ fixture,+ diameter,+ proxy,+ completedMs,+ beforeError: errorBefore / count,+ afterError: errorAfter / count,+ outsideError,+ alphaError,+ restoredError,+ visibilityError,+ hiddenCaches,+ shownSolverPasses: shown.passes.filter((name) =>+ name.includes("/inpaint/"),+ ).length,+ finite: actual.every(Number.isFinite),+ errors,+ images,+ solverPasses: solved.passes.filter((name) => name.includes("/inpaint/"))+ .length,+ cachedSolverPasses: cached.passes.filter((name) =>+ name.includes("/inpaint/"),+ ).length,+ changedSolverPasses: changed.passes.filter((name) =>+ name.includes("/inpaint/"),+ ).length,+ releasedCaches: released.cachedTextures.length,+ };+ } finally {+ renderer.dispose();+ source.dispose();+ clean.color.dispose();+ gpu.dispose();+ }+}diff --git a/tests/inpaint-photo.ts b/tests/inpaint-photo.tsnew file mode 100644index 0000000..a0ec578--- /dev/null+++ b/tests/inpaint-photo.ts@@ -0,0 +1,88 @@+import { effect, frame, init, target } from "vgpu";+import { encodeImage } from "@/app/editor/export/export-image";+import { createImageLayer, createLayer } from "@/app/editor/layers";+import { createEditorRenderer } from "@/app/editor/renderer";+import type { BrushStroke, Scene } from "@/core/document";+import { createImageSource } from "@/core/image";+import { decode } from "@/core/image/decode";+import { imageFrame } from "@/core/image/frame";++/** Exercise decoding, removal and export without mounting React. A spot adds a known defect. */+export async function removeFromPhoto(stroke: BrushStroke, spot = false) {+ const gpu = await init();+ const errors: string[] = [];+ gpu.onError((error) => errors.push(error.message));+ const blob = await (await fetch("/images/demo.jpg")).blob();+ const original = await decode(+ gpu,+ new File([blob], "demo.jpg", { type: blob.type }),+ );+ const damaged = target(gpu, {+ size: original.image.size,+ format: "rgba16float",+ });+ const source = createImageSource(damaged);+ const renderer = createEditorRenderer(gpu, source);+ const [x, y] = stroke.points[0];+ try {+ const copy = effect(+ gpu,+ `+ struct Params { center: vec2f, radius: f32, damaged: u32 }+ @group(0) @binding(0) var source: texture_2d<f32>;+ @group(0) @binding(1) var<uniform> params: Params;+ @fragment fn fs_main(@builtin(position) p: vec4f) -> @location(0) vec4f {+ let color = textureLoad(source, vec2i(p.xy), 0);+ let defect = params.damaged != 0u && distance(p.xy, params.center) < params.radius;+ return vec4f(select(color.rgb, vec3f(0.01), defect), color.a);+ }+ `,+ {+ set: {+ source: original.image.color,+ params: {+ center: [x, y],+ radius: stroke.size / 2 - 5,+ damaged: Number(spot),+ },+ },+ },+ );+ frame(gpu, (f) => f.pass(damaged, copy));+ const scene: Scene = {+ frame: imageFrame(damaged.size),+ layers: [+ createImageLayer("photo", "Demo"),+ {+ ...createLayer("heal"),+ patches: [+ {+ id: "object",+ mode: "remove",+ feather: stroke.feather,+ opacity: 1,+ strokes: [{ ...stroke, feather: 0 }],+ },+ ],+ },+ ],+ };+ await gpu.gpu.queue.onSubmittedWorkDone();+ const start = performance.now();+ await renderer.update(scene);+ await gpu.gpu.queue.onSubmittedWorkDone();+ const completedMs = performance.now() - start;+ const images = await Promise.all(+ [damaged, renderer.fullImage(), original.image].map(async (image) => [+ ...new Uint8Array(await (await encodeImage(gpu, image)).arrayBuffer()),+ ]),+ );+ await gpu.settled();+ return { completedMs, images, errors, storage: renderer.inspect() };+ } finally {+ renderer.dispose();+ source.dispose();+ original.dispose();+ gpu.dispose();+ }+}diff --git a/tests/inpaint.bench.ts b/tests/inpaint.bench.tsnew file mode 100644index 0000000..ac130c9--- /dev/null+++ b/tests/inpaint.bench.ts@@ -0,0 +1,50 @@+import { execFileSync } from "node:child_process";+import { writeFile } from "node:fs/promises";+import { cpus, release } from "node:os";+import { expect, test } from "./fixtures";++for (const scope of ["solver", "editor"] as const) {+ test(`inpainting ${scope}`, async ({ page, browser }, info) => {+ test.setTimeout(600_000);+ await page.goto("/tests/gpu.html");+ const result = await page.evaluate(async (scope) => {+ const path = "/tests/inpaint-benchmark.ts";+ const { benchmarkInpaint } = (await import(+ path+ )) as typeof import("./inpaint-benchmark");+ return benchmarkInpaint(scope);+ }, scope);+ expect(result.rendering.completedMs.samples).toHaveLength(result.samples);+ if (result.timestamps)+ expect(result.profile?.gpuMs?.samples).toHaveLength(result.samples);+ const report = {+ revision: execFileSync("git", ["rev-parse", "HEAD"], {+ encoding: "utf8",+ }).trim(),+ workingTreeChanged:+ execFileSync("git", ["status", "--porcelain"], {+ encoding: "utf8",+ }).trim().length > 0,+ browser: browser.version(),+ platform: process.platform,+ kernel: release(),+ cpu: cpus()[0]?.model,+ logicalCpus: cpus().length,+ flags: info.project.use.launchOptions?.args,+ ...result,+ };+ await writeFile(+ info.outputPath("timings.json"),+ JSON.stringify(report, null, 2),+ );+ console.log(+ JSON.stringify({+ scope,+ firstRenderMs: result.firstRenderMs,+ completedMs: result.rendering.completedMs.median,+ p95: result.rendering.completedMs.p95,+ gpuMs: result.profile?.gpuMs?.median,+ }),+ );+ });+}diff --git a/tests/inpaint.e2e.ts b/tests/inpaint.e2e.tsnew file mode 100644index 0000000..2bdf384--- /dev/null+++ b/tests/inpaint.e2e.ts@@ -0,0 +1,304 @@+import { expect, test } from "./fixtures";+import { readImage } from "./images";+import { box } from "./pointer";++test("Remove previews each frame, solves on release, exports, undoes, and cancels", async ({+ page,+}) => {+ test.setTimeout(90_000);+ await page.setViewportSize({ width: 1440, height: 1000 });+ await page.goto("/");+ await page+ .locator('input[type="file"]')+ .setInputFiles("tests/fixtures/photo.svg");+ await expect(+ page.getByRole("textbox", { name: "Exposure", exact: true }),+ ).toHaveValue("0.00");+ const before = await readImage(page, undefined, [[350, 200]]);+ await page.keyboard.press("h");+ const remove = page+ .getByRole("group", { name: "Retouch mode" })+ .getByRole("button", { name: "Remove", exact: true });+ await expect(remove).toHaveAttribute("aria-pressed", "true");+ const size = page.getByRole("textbox", { name: "Size", exact: true });+ await size.fill("320");+ await size.press("Enter");+ const feather = page.getByRole("textbox", { name: "Feather", exact: true });+ await feather.fill("0");+ await feather.press("Enter");+ const canvas = page.getByLabel("Healing canvas", { exact: true });+ const bounds = await box(canvas);+ const scale = Math.min(+ (bounds.width - 48) / 1200,+ (bounds.height - 48) / 800,+ 2,+ );+ const x = bounds.x + bounds.width / 2 - 250 * scale;+ const y = bounds.y + bounds.height / 2 - 200 * scale;+ const patchPoints = () =>+ page.evaluate(() => {+ const layer = window.openlight+ .getState()+ .scene?.layers.find((layer) => layer.kind === "heal");+ return layer?.kind === "heal"+ ? layer.patches[0]?.strokes[0].points+ : undefined;+ });+ const history = await page.evaluate(+ () => window.openlight.getState().history,+ );+ const time = new Date();+ await page.clock.install({ time });+ await page.clock.pauseAt(time);+ await page.mouse.move(x, y);+ await page.mouse.down();+ const preview = canvas.locator('[data-heal-stroke-preview="true"]');+ await expect(preview).toBeVisible();+ const path = preview.locator("path").first();+ const initialWidth = (await box(path)).width;+ for (let step = 1; step <= 6; step++) {+ await page.mouse.move(x + step * 20 * scale, y);+ await page.clock.runFor(17);+ expect((await box(path)).width).toBeCloseTo(+ initialWidth + step * 20 * scale,+ 0,+ );+ expect(await patchPoints()).toBeUndefined();+ }+ // The visible border grows with the stroke, beyond the initial circle.+ const { samples: edge } = await readImage(+ page,+ await page.screenshot({+ clip: {+ x: Math.round(x + 60 * scale),+ y: Math.round(y - 160) - 1,+ width: 1,+ height: 3,+ },+ }),+ [+ [0, 0],+ [0, 1],+ [0, 2],+ ],+ );+ expect(edge?.some(([r, g, b]) => Math.min(r, g, b) > 220)).toBe(true);+ // Pausing while held never creates an edit or runs synthesis.+ await page.clock.runFor(500);+ expect(await patchPoints()).toBeUndefined();+ expect(+ await page.evaluate(() => window.openlight.getState().history),+ ).toEqual(history);+ expect((await readImage(page, undefined, [[350, 200]])).samples).toEqual(+ before.samples,+ );+ await page.mouse.move(x + 150 * scale, y);+ // Release also includes points queued since the last animation frame.+ await page.mouse.up();+ await expect(preview).toHaveCount(0);+ await expect+ .poll(() =>+ page.evaluate(() => window.openlight.getState().history.undoCount),+ )+ .toBe(history.undoCount + 1);+ expect((await patchPoints())?.at(-1)?.[0]).toBeCloseTo(500, 0);+ expect((await patchPoints())?.length).toBeGreaterThanOrEqual(8);+ await expect(canvas.locator('[data-heal-source-handle="true"]')).toHaveCount(+ 0,+ );+ const patches = await page.evaluate(() => {+ const layer = window.openlight+ .getState()+ .scene?.layers.find((layer) => layer.kind === "heal");+ return layer?.kind === "heal" ? layer.patches : [];+ });+ expect(patches).toMatchObject([+ { mode: "remove", strokes: [{ size: expect.closeTo(320 / scale, 6) }] },+ ]);+ expect(patches[0]).not.toHaveProperty("offset");+ const after = await readImage(page, undefined, [[350, 200]]);+ for (const channel of after.samples?.[0].slice(0, 3) ?? [])+ expect(Math.abs(channel - 128)).toBeLessThanOrEqual(2);+ await page.keyboard.press("ControlOrMeta+z");+ const undone = await readImage(page, undefined, [[350, 200]]);+ expect(undone.samples).toEqual(before.samples);+ // A brush crossing the viewport edge has no artificial contour along the clipping boundary.+ await page.mouse.move(bounds.x + 30, bounds.y + bounds.height / 2);+ await page.mouse.down();+ await page.clock.runFor(17);+ const { samples: clippedEdge } = await readImage(+ page,+ await page.screenshot({+ clip: {+ x: bounds.x,+ y: bounds.y + bounds.height / 2,+ width: 2,+ height: 1,+ },+ }),+ [+ [0, 0],+ [1, 0],+ ],+ );+ expect(+ clippedEdge?.every((pixel) => Math.min(...pixel.slice(0, 3)) < 220),+ ).toBe(true);+ await page.keyboard.press("Escape");+ await page.mouse.up();+ for (const cancel of ["Escape", "pointercancel", "exit", "ControlOrMeta+z"]) {+ if (!(await canvas.isVisible())) await page.keyboard.press("h");+ await page.mouse.move(x, y);+ await page.mouse.down();+ await page.mouse.move(x + 100 * scale, y);+ if (cancel === "pointercancel")+ await canvas.dispatchEvent("pointercancel", { pointerId: 1 });+ else if (cancel === "exit") await page.keyboard.press("b");+ else await page.keyboard.press(cancel);+ await page.mouse.up();+ await page.clock.runFor(300);+ expect(await patchPoints()).toBeUndefined();+ await expect(preview).toHaveCount(0);+ }+ await page.clock.resume();+});++test("Remove moves on drop and cancels a pending move on undo, pointer cancellation, and tool exit", async ({+ page,+}) => {+ await page.setViewportSize({ width: 1440, height: 1000 });+ await page.goto("/");+ await page+ .locator('input[type="file"]')+ .setInputFiles("tests/fixtures/photo.svg");+ await expect(+ page.getByRole("textbox", { name: "Exposure", exact: true }),+ ).toHaveValue("0.00");+ await page.keyboard.press("h");+ const size = page.getByRole("textbox", { name: "Size", exact: true });+ await size.fill("20");+ await size.press("Enter");+ const canvas = page.getByLabel("Healing canvas", { exact: true });+ const bounds = await box(canvas);+ await page.mouse.click(+ bounds.x + bounds.width / 2,+ bounds.y + bounds.height / 2,+ );+ const handle = canvas.locator('[data-heal-destination-handle="true"]');+ await expect(handle).toBeVisible();+ const points = () =>+ page.evaluate(() => {+ const layer = window.openlight+ .getState()+ .scene?.layers.find((layer) => layer.kind === "heal");+ return layer?.kind === "heal" ? layer.patches[0]?.strokes[0].points : [];+ });+ const initial = await points();+ const history = await page.evaluate(+ () => window.openlight.getState().history.undoCount,+ );+ async function startMove() {+ const bounds = await box(handle);+ const x = bounds.x + bounds.width / 2;+ const y = bounds.y + bounds.height / 2;+ await page.mouse.move(x, y);+ await page.mouse.down();+ await page.mouse.move(x + 40, y + 20, { steps: 4 });+ await expect+ .poll(async () => (await box(handle)).x)+ .toBeGreaterThan(bounds.x + 30);+ return { x, y };+ }+ await startMove();+ expect(await points()).toEqual(initial);+ expect(+ await page.evaluate(() => window.openlight.getState().history),+ ).toMatchObject({ editing: false });+ await page.mouse.up();+ await expect.poll(points).not.toEqual(initial);+ expect(+ await page.evaluate(() => window.openlight.getState().history.undoCount),+ ).toBe(history + 1);+ for (const cancel of ["undo", "pointercancel", "exit"]) {+ const { x, y } = await startMove();+ if (cancel === "undo") await page.keyboard.press("ControlOrMeta+z");+ if (cancel === "pointercancel") await handle.dispatchEvent("pointercancel");+ if (cancel === "exit") await page.keyboard.press("Escape");+ await page.mouse.move(x + 60, y + 30);+ await page.mouse.up();+ expect(await points()).toEqual(initial);+ expect(+ await page.evaluate(() => window.openlight.getState().history),+ ).toMatchObject({ undoCount: history, editing: false });+ }+});++test("Remove synthesizes a spot without donor selection and reuses only unchanged content", async ({+ page,+}) => {+ await page.goto("/tests/gpu.html");+ const result = await page.evaluate(async () => {+ const path = "/tests/inpaint-gpu.ts";+ const { renderInpaintReference } = (await import(+ path+ )) as typeof import("./inpaint-gpu");+ return renderInpaintReference("flat");+ });+ expect(result.errors).toEqual([]);+ expect(result.finite).toBe(true);+ expect(result.afterError).toBeLessThan(0.002);+ expect(result.outsideError).toBe(0);+ expect(result.alphaError).toBe(0);+ expect(result.restoredError).toBe(0);+ expect(result.visibilityError).toBe(0);+ expect(result.hiddenCaches).toBeGreaterThan(0);+ expect(result.shownSolverPasses).toBe(0);+ expect(result.solverPasses).toBeGreaterThan(0);+ expect(result.cachedSolverPasses).toBe(0);+ expect(result.changedSolverPasses).toBeGreaterThan(0);+ expect(result.releasedCaches).toBe(0);+});++for (const fixture of ["gradient", "texture", "edge"] as const) {+ test(`Remove reconstructs ${fixture} context and preserves pixels outside the stroke`, async ({+ page,+ }) => {+ await page.goto("/tests/gpu.html");+ const result = await page.evaluate(async (fixture) => {+ const path = "/tests/inpaint-gpu.ts";+ const { renderInpaintReference } = (await import(+ path+ )) as typeof import("./inpaint-gpu");+ return renderInpaintReference(fixture);+ }, fixture);+ expect(result.errors).toEqual([]);+ expect(result.finite).toBe(true);+ expect(result.afterError).toBeLessThan(0.015);+ expect(result.afterError).toBeLessThan(result.beforeError * 0.05);+ expect(result.outsideError).toBe(0);+ expect(result.alphaError).toBe(0);+ });+}++test("Remove supports a proxy and leaves an image alone when no clean donor exists", async ({+ page,+}) => {+ await page.goto("/tests/gpu.html");+ const results = await page.evaluate(async () => {+ const path = "/tests/inpaint-gpu.ts";+ const { renderInpaintReference } = (await import(+ path+ )) as typeof import("./inpaint-gpu");+ return [+ await renderInpaintReference("texture", 32, true),+ await renderInpaintReference("flat", 400),+ ];+ });+ for (const result of results) {+ expect(result.errors).toEqual([]);+ expect(result.finite).toBe(true);+ expect(result.alphaError).toBe(0);+ }+ expect(results[0].afterError).toBeLessThan(0.04);+ expect(results[1].afterError).toBe(results[1].beforeError);+});diff --git a/tests/render-graph.test.ts b/tests/render-graph.test.tsindex 43e07a9..0f64bfc 100644--- a/tests/render-graph.test.ts+++ b/tests/render-graph.test.ts@@ -18,6 +18,49 @@ const shader = `}`;const options = { storage: { weights: new Float32Array([0.5]) } };+test("cached branches skip their dependencies, retain two revisions, and release owned results", async () => {+ const gpu = await init();+ const image = target(gpu, { size: [8, 8], format: "rgba16float" });+ const source = input(image);+ const graph = createRenderGraph(gpu);+ const expensive = merge(+ { source, base: source },+ node("repair/search", shader, options),+ );+ const cached = (key: object) =>+ merge(+ { source: expensive, base: source },+ node("repair/result", shader, { ...options, cacheKeys: [key] }),+ );+ try {+ const firstKey = {};+ const secondKey = {};+ const [first] = graph.render([cached(firstKey)]);+ expect(graph.inspect().passes).toEqual(["repair/search", "repair/result"]);+ expect(graph.render([cached(firstKey)])[0]).toBe(first);+ expect(graph.inspect().passes).toEqual([]);+ const [second] = graph.render([cached(secondKey)]);+ expect(second).not.toBe(first);+ expect(graph.render([cached(firstKey)])[0]).toBe(first);+ expect(graph.render([cached({})])[0]).toBe(first);+ expect(graph.inspect().cachedTextures).toHaveLength(2);+ // Eviction must keep a result that the caller is still using in this frame.+ const latest = cached({});+ expect(graph.render([input(second), latest])[0]).toBe(second);+ expect(() => second.color.view).not.toThrow();+ graph.render([latest]);+ expect(() => second.color.view).toThrow("destroyed");+ graph.release("repair/");+ expect(graph.inspect().cachedTextures).toHaveLength(0);+ expect(() => second.color.view).toThrow("destroyed");+ expect(() => image.color.view).not.toThrow();+ } finally {+ graph.dispose();+ image.color.dispose();+ gpu.dispose();+ }+});+test("pipeline and split preserve order, bypasses, and named merge inputs", async () => {const gpu = await init();const image = target(gpu, { size: [8, 4], format: "rgba16float" });diff --git a/tests/rendering-benchmark.ts b/tests/rendering-benchmark.tsindex 92fe0bb..7ff7c75 100644--- a/tests/rendering-benchmark.ts+++ b/tests/rendering-benchmark.ts@@ -4,6 +4,7 @@ import { createImageLayer } from "@/app/editor/layers";import { createEditorRenderer } from "@/app/editor/renderer";import type {BrushStroke,+ HealPatch,LookupTable,Mask,ProcessingLayer,@@ -35,6 +36,8 @@ export type Workload =| "lut"| "paint"| "heal"+ | "clone"+ | "remove"| "heal-empty"| "heal-proxy"| "pipeline-proxy";@@ -276,34 +279,39 @@ export async function benchmarkRendering(}if (workload === "heal" ||+ workload === "clone" ||+ workload === "remove" ||workload === "heal-empty" ||workload === "heal-proxy") {+ const shape = {+ id: "spot",+ feather: 0.4,+ opacity: 1,+ strokes: [+ {+ mode: "paint" as const,+ size: 240,+ feather: 0,+ flow: 1,+ points: [[size[0] / 2, size[1] / 2, 1] as const],+ },+ ],+ };+ const donorMode = workload === "clone" ? "clone" : "heal";+ const patch: HealPatch =+ workload === "remove"+ ? { ...shape, mode: "remove" }+ : { ...shape, mode: donorMode, offset: [320, 0] };effects.push({...common,id: "benchmark-heal",name: "Heal",kind: "heal",- patches:- workload === "heal-empty"- ? []- : [- {- id: "spot",- feather: 0.4,- opacity: 1,- stroke: {- mode: "paint",- size: 240,- feather: 0,- flow: 1,- points: [[size[0] / 2, size[1] / 2, 1]],- },- offset: [320, 0],- },- ],+ patches: workload === "heal-empty" ? [] : [patch],});}+const scene: Scene = {frame: imageFrame(size),layers: [@@ -437,7 +445,10 @@ export async function benchmarkRendering(.map((value) => value.toString(16).padStart(2, "0")).join(""),// Source and driver memory are excluded.- intermediateBytes: storage.textures.reduce(+ intermediateBytes: [+ ...storage.textures,+ ...storage.cachedTextures,+ ].reduce((sum, { size, format }) => sum + size[0] * size[1] * pixelBytes[format],0,),diff --git a/tests/rendering.bench.ts b/tests/rendering.bench.tsindex 0abe130..9b90006 100644--- a/tests/rendering.bench.ts+++ b/tests/rendering.bench.ts@@ -23,6 +23,8 @@ for (const workload of ["lut","paint","heal",+ "clone",+ "remove","heal-empty","heal-proxy","pipeline-proxy",diff --git a/tests/scene-file.test.ts b/tests/scene-file.test.tsindex 47e7fac..59c4e88 100644--- a/tests/scene-file.test.ts+++ b/tests/scene-file.test.ts@@ -109,11 +109,38 @@ test("scene files reopen the photo with every layer for further editing", async});api.setMaskOperation(subtract, "subtract");const heal = api.addLayer("heal");- api.addHealPatch(+ const firstPatch = api.addHealPatch(heal,{ mode: "paint", size: 4, feather: 0.2, flow: 1, points: [[40, 20, 1]] },[-6, 2],);+ api.addHealStroke(heal, firstPatch, {+ mode: "paint",+ size: 6,+ feather: 0.2,+ flow: 1,+ points: [[44, 22, 1]],+ });+ api.addHealStroke(heal, firstPatch, {+ mode: "erase",+ size: 2,+ feather: 0,+ flow: 1,+ points: [[40, 20, 1]],+ });+ api.addHealPatch(+ heal,+ { mode: "paint", size: 4, feather: 0.2, flow: 1, points: [[48, 20, 1]] },+ [-6, 2],+ "clone",+ );+ api.addRemovePatch(heal, {+ mode: "paint",+ size: 4,+ feather: 0.2,+ flow: 1,+ points: [[52, 24, 1]],+ });const paint = api.addLayer("paint");api.addPaintStroke(paint, {mode: "paint",@@ -256,9 +283,25 @@ test("scene files reopen the photo with every layer for further editing", async);delete vignette.vignette.softness;delete json.scene.layers[0].whiteBalance;+ const healing = json.scene.layers.find(+ (layer: { kind: string }) => layer.kind === "heal",+ );+ delete healing.patches[0].mode;+ // Earlier files held one painted stroke per patch.+ for (const patch of healing.patches) {+ patch.stroke = patch.strokes[0];+ delete patch.strokes;+ }const older = await openSceneFile(await archive(json), raw.decode);const loaded = older.scene.getState();expect(loaded.layers[0].whiteBalance).toEqual(asShot);+ expect(loaded.layers.find((layer) => layer.kind === "heal")).toMatchObject({+ patches: [+ { mode: "heal", strokes: [{ mode: "paint" }] },+ { mode: "clone", strokes: [{ mode: "paint" }] },+ { mode: "remove", strokes: [{ mode: "paint" }] },+ ],+ });expect(loaded.layers.find((layer) => layer.kind === "vignette"),).toMatchObject({ vignette: { intensity: 70, softness: 50 } });diff --git a/tests/tool-shortcuts.e2e.ts b/tests/tool-shortcuts.e2e.tsnew file mode 100644index 0000000..434d46f--- /dev/null+++ b/tests/tool-shortcuts.e2e.ts@@ -0,0 +1,91 @@+import { expect, test } from "./fixtures";++test("Enter leaves retouch after selecting a patch by mouse or keyboard", async ({+ page,+}) => {+ await page.setViewportSize({ width: 1440, height: 1000 });+ await page.goto("/");+ await page+ .locator('input[type="file"]')+ .setInputFiles("tests/fixtures/photo.svg");+ await expect(+ page.getByRole("textbox", { name: "Exposure", exact: true }),+ ).toHaveValue("0.00");+ await page.keyboard.press("h");+ const canvas = page.getByLabel("Healing canvas", { exact: true });+ const size = page.getByRole("textbox", { name: "Size", exact: true });+ await size.fill("20");+ await size.press("Enter");+ await canvas.click({ position: { x: 600, y: 500 } });+ const patch = page.getByRole("button", {+ name: "Select patch 1",+ exact: true,+ });+ await expect(patch).toHaveAttribute("aria-pressed", "true");+ const state = await page.evaluate(() => window.openlight.getState());+ await patch.click();+ await expect(patch).not.toBeFocused();+ await page.keyboard.press("Enter");+ await expect(canvas).toHaveCount(0);+ await expect(+ page.getByRole("tab", { name: "Adjust", exact: true }),+ ).toHaveAttribute("aria-selected", "true");+ expect(await page.evaluate(() => window.openlight.getState())).toEqual(state);+ // Explicit keyboard activation still selects a patch and returns control to the canvas.+ await patch.focus();+ await patch.press("Enter");+ await expect(canvas).toBeVisible();+ await expect(patch).not.toBeFocused();+ await page.keyboard.press("Enter");+ await expect(canvas).toHaveCount(0);+ expect(await page.evaluate(() => window.openlight.getState())).toEqual(state);+});++test("tool shortcuts restore remembered submodes and cycle only the active tool", async ({+ page,+}) => {+ await page.setViewportSize({ width: 1440, height: 1000 });+ await page.goto("/");+ await page+ .locator('input[type="file"]')+ .setInputFiles("tests/fixtures/photo.svg");+ await expect(+ page.getByRole("textbox", { name: "Exposure", exact: true }),+ ).toHaveValue("0.00");+ const modes = page.getByRole("group", { name: "Retouch mode" });+ const remove = modes.getByRole("button", { name: "Remove", exact: true });+ const mask = page.getByLabel("Brush canvas", { exact: true });+ const color = page.getByLabel("Paint canvas", { exact: true });+ await page.keyboard.press("h");+ await remove.click();+ await page.keyboard.press("b");+ await expect(mask).toBeVisible();+ await page.keyboard.press("h");+ await expect(remove).toHaveAttribute("aria-pressed", "true");+ await page.keyboard.press("b");+ await expect(mask).toBeVisible();+ await page.keyboard.press("b");+ await expect(color).toBeVisible();+ await page.keyboard.press("h");+ await expect(remove).toHaveAttribute("aria-pressed", "true");+ await page.keyboard.press("b");+ await expect(color).toBeVisible();+ await page.keyboard.press("h");+ await expect(remove).toHaveAttribute("aria-pressed", "true");+ for (const mode of ["Heal", "Clone", "Remove"]) {+ await page.keyboard.press("h");+ await expect(+ modes.getByRole("button", { name: mode, exact: true }),+ ).toHaveAttribute("aria-pressed", "true");+ }+ await page.keyboard.press("c");+ await page.keyboard.press("b");+ await expect(color).toBeVisible();+ await page.keyboard.press("b");+ await expect(mask).toBeVisible();+ await page.keyboard.press("a");+ await page.keyboard.press("b");+ await expect(mask).toBeVisible();+ await page.keyboard.press("h");+ await expect(remove).toHaveAttribute("aria-pressed", "true");+});