Skip to content

Free open-source image editor for the browser

Add manual perspective correction to Crop (#86)

Rodrigo Belfiored5be9c8f3d9e
diff --git a/API.md b/API.md
index 37d6a69..b5c95a5 100644
--- a/API.md
+++ b/API.md
@@ -78,6 +78,8 @@ Edits update the scene and history synchronously. Rendering may finish later, pa
`setFrame(frame)` replaces the complete [ImageFrame](src/core/image/frame.ts) as an undoable edit. Geometry uses finite two-element coordinate pairs, positive dimensions, and nonzero scale. The frame is validated and copied; later changes to the supplied object do not affect the document.
+The frame's `perspective`, `[x, y]` from −100 to 100 and `[0, 0]` when omitted, corrects keystone along the photo's own axes before the crop, rotation, and flips. Positive `x` enlarges the right edge against the left, positive `y` the bottom against the top: at 100 they take 1/0.55 and 1/1.45 of their scale. At value `v`, lines that meet 1/(0.0045·|v|) half-sizes from the photo's center, beyond the edge `v` enlarges, become parallel; a building shot looking up straightens at a negative `y`. The center keeps its scale, and lines stay straight. Without perspective, `center` is in source pixels; with it, in the corrected photo's pixels, which share the source's center. The Crop tool shows the correction as Vertical and Horizontal along the displayed axes, so they follow quarter turns and flips, and magnifies around the crop's center only as much as covering the crop requires, keeping the output size. Layers keep their source-pixel geometry, so correcting perspective leaves paint, masks, and patches on the photo.
+
## Layers
`scene.layers` is ordered bottom to top, beginning with the locked image at index 0. Processing layers have `children`, also ordered bottom to top; the image cannot contain layers. Editing supports two levels. Adding, duplicating, moving, and deleting commit an open gesture and record one history entry each. Selecting a layer changes the inspector without adding history; changing selection commits an active gesture. Removing the selected layer or an ancestor returns selection to the image.
@@ -98,7 +100,7 @@ Direct mask children of another mask modify coverage instead of processing image
| `moveLayer(id, index, parentId?)` | Moves to a final sibling index, bottom to top. Omit the parent for the root stack, where index 0 is reserved for the image. Image parents, cycles, and third-level nesting are rejected. |
| `deleteLayer(id)` | Removes a processing layer and its children; undo restores them. |
-A linear gradient has full coverage at `start`, zero at `end`; its points must be finite and distinct. A radial gradient covers the ellipse inside `radius`, with positive radii and a feathered falloff toward its edge. Crop, rotation, and viewport navigation do not move it within the document.
+A linear gradient has full coverage at `start`, zero at `end`; its points must be finite and distinct. A radial gradient covers the ellipse inside `radius`, with positive radii and a feathered falloff toward its edge. Crop, rotation, perspective, and viewport navigation do not move it within the document.
A brush mask is a list of strokes, over the pixels earlier strokes settled into, as a Paint layer's are: in the editor they settle every 100 strokes, and `getState()` shows the mask's `raster` ID. `setLayerMask` keeps a `raster` only if it names the document's own settled pixels. Each stroke has `mode` (`"paint"` or `"erase"`), `size` (diameter in source pixels), `feather` and `flow` (0–1), and `points`, each `[x, y, pressure]` in source pixels with pressure 0–1. A paint stroke adds `flow × pressure` of the remaining coverage every quarter diameter along the points, and an erase stroke removes that share of the existing coverage; dabs land sixteen times per diameter, each laying a fraction of that, so soft edges show no ripples. The renderer rasterizes strokes into a cached coverage texture at half the photo's resolution each way and only stamps new points, so appending to the last stroke is cheap and undo replays at most 99. A brush inside another mask keeps its own coverage, paint and erase strokes alike, which the mask adds or subtracts scaled by the brush layer's opacity. A mask with no painted coverage and nothing added to it is bypassed.
@@ -183,7 +185,7 @@ The image renders at the document dimensions and downsamples to `longEdge` with
`scene` is the `getState()` scene; each image layer's `source` names an entry in `sources`, and each Remove patch's `field` an entry in `fields`: where its texels sit, or, for a field not yet synthesized, `{ "base": "<id>" }` naming the field it extends. Version 1 files, which kept each Remove field inside its patch, open as version 2. Opening the file with `loadScene`, `openFile`, a drop, or the file picker decodes the stored source again and restores the frame and every layer as a new document with empty history. Preview settings and history are not saved.
-Opening validates every value as the matching command does, and a file that fails leaves the workspace in its error state with a message naming the first invalid field. Fields OpenLight does not know are dropped. A parameter missing from `adjustments`, `details`, `vignette`, `grain`, `fill`, or `colorMixer` takes its default, so older files still open when a group gains a parameter; a RAW image without a white balance uses its As Shot value. `version` increases only when older files can no longer open as written; a newer version is rejected.
+Opening validates every value as the matching command does, and a file that fails leaves the workspace in its error state with a message naming the first invalid field. Fields OpenLight does not know are dropped. A parameter missing from `adjustments`, `details`, `vignette`, `grain`, `fill`, or `colorMixer` takes its default, so older files still open when a group gains a parameter; a RAW image without a white balance uses its As Shot value, and a frame without `perspective` has none. `version` increases only when older files can no longer open as written, or an older OpenLight would drop what a file holds: a scene that corrects perspective is version 3, which OpenLight before perspective correction rejects, and every other scene stays version 2. A newer version is rejected.
## Drafts
@@ -222,8 +224,8 @@ editor.run({
| `set-grain` | `amount?`, `size?`, `roughness?`, `layerId?` | Like `setGrain`. |
| `add-mask` | `mask`, `adjustments?` | Adds a mask layer with those adjustments on top of the stack as one edit. |
| `delete-layer` | `layerId` | Like `deleteLayer`. |
-| `set-crop` | `aspectRatio?`, `straighten?` | Replaces the frame with the largest centered crop of the source at `aspectRatio`, width over height, straightened by −45 to 45 degrees. Without either, it removes the crop. |
-| `reset` | none | Removes every layer and returns adjustments, the tone curve, white balance, and the frame to how the photo opened, as one edit. |
+| `set-crop` | `aspectRatio?`, `straighten?` | Replaces the frame with the largest centered crop of the source at `aspectRatio`, width over height, straightened by −45 to 45 degrees, keeping the frame's perspective. Without either, it removes the crop. |
+| `reset` | none | Removes every layer and returns adjustments, the tone curve, white balance, and the frame, perspective included, to how the photo opened, as one edit. |
| `undo`, `redo` | none | Like `undo` and `redo`. |
| `set-preview` | `comparison` | Shows `"edited"`, `"original"`, or `"split"`. |
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md
index b1fc424..7359479 100644
--- a/ARCHITECTURE.md
+++ b/ARCHITECTURE.md
@@ -22,7 +22,8 @@ Where the code lives and the rules that keep it in place. Keep this file true: w
| `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-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, covering the same area there under perspective; recorded strokes retain their image-space geometry, round in source pixels, and the cursor shows the dab as the viewport does. |
+| `components/editor/mapping.ts` | Viewport ↔ document positions through the frame, perspective included, for every canvas tool: exact both ways, with source circles and polygons drawn as their exact images. Pointer input stops at a straight line short of the correction's horizon, joining a stroke that crosses it along that line; a shape the viewport cannot show whole is clipped where it leaves the viewport. |
| `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. |
@@ -90,4 +91,5 @@ Dependencies point downward. Features do not import each other; `app/` connects
- 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.
+- The scene's frame maps output to source in one homogeneous transform, `frameTransform` in `core/image/frame.ts`: crop, rotation, flips, and scale, then the perspective correction about the source's center. Layers, paint, masks, and patches stay in source pixels, so correcting perspective leaves them in place; `transformImages` resamples the composite and the original once, the display and mask overlay use the same transform, and a frame that changes nothing adds no pass. The proxy follows the frame's strongest magnification, perspective's included. `lib/projective.ts` holds the plane projective geometry the frame, the Crop tool's coverage, and the canvas mapping share.
- TIFF and camera RAW decode through `raw-webgpu`; OpenLight adapts its resources to document ownership.
diff --git a/CONTEXT.md b/CONTEXT.md
index 8d1c493..3716d8a 100644
--- a/CONTEXT.md
+++ b/CONTEXT.md
@@ -10,6 +10,7 @@ OpenLight edits documents containing image content and adjustments.
| **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. 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; it keeps where each pixel copies from as its field, so the layers below change only the copied colors, and a stroke added or erased keeps what it already filled. 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. |
+| **Perspective correction** | Part of the frame: Vertical and Horizontal keystone that makes lines converging in the photo, such as a building's edges, parallel, before the crop, rotation, and flips. Layers keep their source pixels and follow it; tools draw and place them as it maps them. |
| **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/public/llms.txt b/public/llms.txt
index 6d65405..5b07e14 100644
--- a/public/llms.txt
+++ b/public/llms.txt
@@ -14,7 +14,7 @@ OpenLight is free to use with no account or upload, and works as an online alter
- Color Mixer with eight hue, saturation, and luminance ranges, film grain, vignette, and 3D LUTs from `.cube` files.
- Effect layers with linear, radial, and brush masks, including pen pressure.
- Healing tool to remove spots and distractions.
-- Crop, rotate, straighten, and flip.
+- Crop, rotate, straighten, flip, and correct vertical and horizontal perspective.
- Opens JPEG, PNG, WebP, AVIF, GIF, BMP, SVG, HEIC, TIFF, and camera RAW/DNG files, and imports Camera Raw XMP settings.
- Exports PNG, JPEG, or WebP, and saves scenes to keep editing later.
diff --git a/src/app/commands.ts b/src/app/commands.ts
index b8e345f..a70bd0b 100644
--- a/src/app/commands.ts
+++ b/src/app/commands.ts
@@ -164,19 +164,24 @@ export const commands = {
},
),
"set-crop": command(
- "Replaces the crop, rotation, and flips with the largest centered crop of the photo. aspectRatio is width / height and defaults to the photo's; straighten rotates by -45 to 45 degrees. Omit both to remove the crop.",
+ "Replaces the crop, rotation, and flips with the largest centered crop of the photo, keeping its perspective correction. aspectRatio is width / height and defaults to the photo's; straighten rotates by -45 to 45 degrees. Omit both to remove the crop.",
z.strictObject({
aspectRatio: z.optional(z.number().check(z.positive())),
straighten: z.optional(range(-45, 45)),
}),
(document, { aspectRatio, straighten = 0 }) => {
const size = sourceSize(document);
- const frame = rotate(imageFrame(size), straighten, size);
+ const { perspective } = document.scene.getState().frame;
+ const frame = rotate(
+ { ...imageFrame(size), perspective },
+ straighten,
+ size,
+ );
applyCrop(document, aspectRatio ? fitRatio(frame, aspectRatio) : frame);
},
),
reset: command(
- "Removes every layer and returns the photo's adjustments, tone curve, white balance, and crop to how it opened, as one edit.",
+ "Removes every layer and returns the photo's adjustments, tone curve, white balance, crop, and perspective to how it opened, as one edit.",
none,
reset,
),
diff --git a/src/app/scene-file.ts b/src/app/scene-file.ts
index 20ca55a..9c89f98 100644
--- a/src/app/scene-file.ts
+++ b/src/app/scene-file.ts
@@ -38,8 +38,16 @@ import { defaultVignette, vignetteSchema } from "@/features/vignette/model";
import { whiteBalanceSchema } from "@/features/white-balance/edits";
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 = 2;
+/**
+ * The newest version this opens. It rises only when older files can no longer load as written, or
+ * an older OpenLight would drop what a file holds; a parameter added later takes its default.
+ */
+const version = 3;
+
+/** A perspective correction makes a scene version 3, which an older OpenLight rejects rather than drop. */
+function sceneVersion(scene: Scene) {
+ return scene.frame.perspective.some((value) => value !== 0) ? 3 : 2;
+}
const savedHealPatch = z.pipe(
z.transform((input) => {
@@ -117,7 +125,7 @@ export async function completeScene(
}
const json: SceneJson = {
format: "openlight",
- version,
+ version: sceneVersion(scene),
sources: {
[source.id]: { name: source.file.name, type: source.file.type },
},
diff --git a/src/components/editor/brush-canvas.tsx b/src/components/editor/brush-canvas.tsx
index 0fd7f49..02986fd 100644
--- a/src/components/editor/brush-canvas.tsx
+++ b/src/components/editor/brush-canvas.tsx
@@ -28,6 +28,8 @@ type Stroke = {
client: Point;
/** The viewport bounds measured once; pointer capture keeps them valid for the drag. */
box: DOMRect;
+ /** The last sample in viewport pixels, where the path to the next one starts. */
+ last: Point;
pending: StrokePoint[];
editOnRelease: boolean;
frame?: number;
@@ -76,14 +78,18 @@ export function BrushCanvas({
const stroke = useRef<Stroke | null>(null);
const completing = useRef<AbortController | null>(null);
const [pointer, setPointer] = useState<Point | null>(null);
+ /** The source diameter of the stroke being drawn, which the cursor shows wherever it goes. */
+ const [drawn, setDrawn] = useState<number>();
/** Where a right click opened the brush menu. */
const [menu, setMenu] = useState<Point | null>(null);
const [pointerVisible, setPointerVisible] = useState(false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string>();
- 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 pressure(event: PointerLike) {
+ return event.pointerType === "pen" ? event.pressure : 1;
+ }
+ function viewportPoint(event: PointerLike, box: DOMRect): Point {
+ return [event.clientX - box.left, event.clientY - box.top];
}
function flush(current: Stroke) {
current.frame = undefined;
@@ -97,6 +103,7 @@ export function BrushCanvas({
return true;
} catch (error) {
stroke.current = null;
+ setDrawn(undefined);
onFinish?.(false);
if (!current.editOnRelease) document.history.cancel();
setError(String(error));
@@ -116,6 +123,7 @@ export function BrushCanvas({
return;
}
stroke.current = null;
+ setDrawn(undefined);
if (current.frame !== undefined) {
cancelAnimationFrame(current.frame);
}
@@ -226,12 +234,13 @@ export function BrushCanvas({
return;
}
const box = camera.ref.current?.getBoundingClientRect();
- if (busy || !box) {
+ const last = box && viewportPoint(event, box);
+ const at = last && mapping.toDocument(last);
+ if (busy || !box || !last || !at) {
return;
}
- const first = point(event, box);
if (event.altKey && onPickSource) {
- onPickSource([first[0], first[1]]);
+ onPickSource(at);
event.preventDefault();
event.stopPropagation();
return;
@@ -243,13 +252,15 @@ export function BrushCanvas({
? editOnRelease(modifiers)
: editOnRelease;
const opened = !deferred && document.history.begin();
+ // The viewport diameter becomes source pixels where the stroke starts, covering the same area there.
+ const size = brush.settings.size / mapping.scale(at);
const started = onStart(
{
mode: erase ? "erase" : "paint",
- size: brush.settings.size * mapping.pixelsPerViewportPixel,
+ size,
feather: brush.settings.feather,
flow: brush.settings.flow,
- points: [first],
+ points: [[...at, pressure(event)]],
},
modifiers,
);
@@ -257,11 +268,13 @@ export function BrushCanvas({
if (opened) document.history.cancel();
return;
}
+ setDrawn(size);
stroke.current = {
pointer: event.pointerId,
touch: event.pointerType === "touch",
client: [event.clientX, event.clientY],
box,
+ last,
pending: [],
editOnRelease: deferred,
};
@@ -291,7 +304,11 @@ export function BrushCanvas({
const native = event.nativeEvent;
const events = native.getCoalescedEvents?.() ?? [];
for (const item of events.length ? events : [native]) {
- current.pending.push(point(item, current.box));
+ const next = viewportPoint(item, current.box);
+ for (const [x, y] of mapping.trail(current.last, next)) {
+ current.pending.push([x, y, pressure(item)]);
+ }
+ current.last = next;
}
current.frame ??= requestAnimationFrame(() => flush(current));
}
@@ -303,9 +320,15 @@ export function BrushCanvas({
finish(true);
event.currentTarget.releasePointerCapture(event.pointerId);
}
- const radius = brush.settings.size / 2;
const status = error ?? (busy ? "Finishing stroke…" : undefined);
const cursor = pointerVisible || brush.preview ? pointer : null;
+ /** The dab a stroke lays at a document point: the one being drawn, or one starting there. */
+ function dabAt(at: Point) {
+ const radius = (drawn ?? brush.settings.size / mapping.scale(at)) / 2;
+ return mapping.ellipse({ center: at, radii: [radius, radius], angle: 0 });
+ }
+ const under = cursor ? mapping.toDocument(cursor) : undefined;
+ const dab = under ? dabAt(under) : undefined;
return (
<div
ref={wheelRef}
@@ -334,10 +357,10 @@ export function BrushCanvas({
}}
>
{children}
- {cursor && !camera.panMode && (
+ {cursor && dab && !camera.panMode && (
<BrushCursor
at={cursor}
- radius={radius}
+ dab={dab}
feather={brush.settings.feather}
erase={erase}
preview={brush.preview}
diff --git a/src/components/editor/brush-cursor.tsx b/src/components/editor/brush-cursor.tsx
index ad6dc3d..38c022a 100644
--- a/src/components/editor/brush-cursor.tsx
+++ b/src/components/editor/brush-cursor.tsx
@@ -1,16 +1,21 @@
import { useId } from "react";
import type { Point } from "@/core/image/frame";
+import { MappedShape } from "./mapped-shape";
+import type { Shape } from "./mapping";
-/** The active brush's size, feather, and center in viewport pixels. */
+/**
+ * The dab the active brush lays at the pointer, as the viewport shows it, with its feather and center.
+ * With perspective the outline is the dab's exact image; the feather shading only follows it.
+ */
export function BrushCursor({
at,
- radius,
+ dab,
feather,
erase,
preview,
}: {
at: Point;
- radius: number;
+ dab: Shape;
feather: number;
erase: boolean;
preview: boolean;
@@ -19,6 +24,24 @@ export function BrushCursor({
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`;
+ // A dab smaller than the ring still shows one.
+ const shape: Shape =
+ "ellipse" in dab
+ ? {
+ ellipse: {
+ ...dab.ellipse,
+ radii: [
+ Math.max(2, dab.ellipse.radii[0]),
+ Math.max(2, dab.ellipse.radii[1]),
+ ],
+ },
+ }
+ : dab;
+ // Wider than 100 visible pixels, so rounding in the projection never tips a 100 px brush over.
+ const wide =
+ "path" in dab ||
+ Math.round(2 * Math.sqrt(dab.ellipse.radii[0] * dab.ellipse.radii[1])) >
+ 100;
return (
<svg
aria-hidden="true"
@@ -34,19 +57,15 @@ export function BrushCursor({
<stop offset="1" stopColor={color} stopOpacity="0" />
</radialGradient>
</defs>
- <circle
- cx={at[0]}
- cy={at[1]}
- r={Math.max(2, radius)}
+ <MappedShape
+ shape={shape}
fill={`url(#${gradient})`}
stroke="white"
strokeOpacity="0.9"
strokeDasharray={dash}
/>
- <circle
- cx={at[0]}
- cy={at[1]}
- r={Math.max(2, radius)}
+ <MappedShape
+ shape={shape}
fill="none"
stroke="black"
strokeOpacity="0.5"
@@ -54,7 +73,7 @@ export function BrushCursor({
strokeDasharray={dash}
style={{ paintOrder: "stroke" }}
/>
- {radius > 50 && (
+ {wide && (
<g data-brush-center="true" fill="none">
<path d={center} stroke="white" strokeOpacity="0.9" />
<path
diff --git a/src/components/editor/image.tsx b/src/components/editor/image.tsx
index 913d8dd..aad97f4 100644
--- a/src/components/editor/image.tsx
+++ b/src/components/editor/image.tsx
@@ -1,8 +1,8 @@
-import { useEffect } from "react";
+import { useEffect, useMemo } from "react";
import type { Target } from "vgpu";
import { useCanvas, useFrame, useGpu } from "vgpu-react";
import { useStore } from "zustand";
-import type { ImageFrame } from "@/core/image/frame";
+import { correctionStretch, type ImageFrame } from "@/core/image/frame";
import { createDisplay } from "@/core/renderer";
import { useDisposable } from "@/hooks/use-disposable";
import { fitScale } from "@/hooks/use-pan-zoom";
@@ -33,6 +33,10 @@ export function Image({
useScene((scene) => scene.layers[0].source),
).image.size;
const renderer = useRenderer();
+ const stretch = useMemo(
+ () => correctionStretch(sceneFrame, sourceSize),
+ [sceneFrame, sourceSize],
+ );
const display = useDisposable(() => createDisplay(gpu), [gpu]);
const render = useFrame((frame) => {
const before = original && renderer[original]();
@@ -48,9 +52,11 @@ export function Image({
zoom: camera.scale / (fitScale(size, camera.viewport) || 1),
};
if (typeof image === "string") {
- // The main canvas tells the renderer how many device pixels a source pixel gets.
+ // The main canvas tells the renderer how many device pixels a source pixel gets where
+ // perspective enlarges the photo most.
renderer.setDisplayScale(
- (camera.scale * devicePixelRatio) / Math.abs(sceneFrame.scale[0]),
+ (camera.scale * devicePixelRatio * stretch) /
+ Math.abs(sceneFrame.scale[0]),
);
}
const overlay = preview.maskOverlay;
diff --git a/src/components/editor/mapped-shape.tsx b/src/components/editor/mapped-shape.tsx
new file mode 100644
index 0000000..6dfc890
--- /dev/null
+++ b/src/components/editor/mapped-shape.tsx
@@ -0,0 +1,27 @@
+import type { SVGAttributes } from "react";
+import type { Shape } from "./mapping";
+
+/** Draws a source shape where the viewport shows it: an exact ellipse, or its visible path. */
+export function MappedShape({
+ shape,
+ ...props
+}: { shape: Shape } & SVGAttributes<SVGElement>) {
+ if ("path" in shape) {
+ return <path d={shape.path} {...props} />;
+ }
+ const {
+ center: [cx, cy],
+ radii: [rx, ry],
+ angle,
+ } = shape.ellipse;
+ return (
+ <ellipse
+ cx={cx}
+ cy={cy}
+ rx={rx}
+ ry={ry}
+ transform={`rotate(${angle} ${cx} ${cy})`}
+ {...props}
+ />
+ );
+}
diff --git a/src/components/editor/mapping.ts b/src/components/editor/mapping.ts
index 0b4960f..dd09944 100644
--- a/src/components/editor/mapping.ts
+++ b/src/components/editor/mapping.ts
@@ -1,39 +1,139 @@
import { useMemo } from "react";
-import { outputOffset, type Point, sourceOffset } from "@/core/image/frame";
-import { useScene } from "./session";
+import {
+ type ImageFrame,
+ outputToSource,
+ type Point,
+ sourceToOutput,
+} from "@/core/image/frame";
+import type { View } from "@/core/renderer";
+import {
+ apply,
+ type Ellipse,
+ ellipsePath,
+ type Homography,
+ jacobian,
+ multiply,
+ polygonPath,
+ project,
+ projectEllipse,
+} from "@/lib/projective";
+import { useDocument, useScene } from "./session";
import { useViewport } from "./viewport";
-/** Converts between viewport pixels and source-pixel document positions through the displayed frame. */
+/** A source shape as the viewport shows it: an ellipse exactly, or path data for a visible part. */
+export type Shape = { ellipse: Ellipse } | { path: string };
+
+/**
+ * Converts between viewport pixels, from the viewport's top-left corner, and source-pixel document
+ * positions through the displayed frame, perspective included.
+ */
+export function createMapping(
+ frame: ImageFrame,
+ source: readonly number[],
+ view: View,
+ scale: number,
+ viewport: Point,
+) {
+ const [x, y] = [viewport[0] / 2 + view.pan[0], viewport[1] / 2 + view.pan[1]];
+ const toSource = multiply(outputToSource(frame, source), [
+ 1 / scale,
+ 0,
+ -x / scale,
+ 0,
+ 1 / scale,
+ -y / scale,
+ 0,
+ 0,
+ 1,
+ ]);
+ const toViewport = multiply(
+ [scale, 0, x, 0, scale, y, 0, 0, 1],
+ sourceToOutput(frame, source),
+ );
+ const weight = (matrix: Homography, point: Point) => apply(matrix, point)[2];
+ const corners = (size: readonly number[]): Point[] => [
+ [0, 0],
+ [size[0], 0],
+ [0, size[1]],
+ [size[0], size[1]],
+ ];
+ // A viewport point reaches the source over a weight that varies linearly across the viewport, 1
+ // everywhere without perspective, and falls to 0 at the perspective's horizon. Input stops at half
+ // the least weight within the photo, the inverse of the most its corners take the other way: a
+ // straight line short of the horizon, so document positions stay finite.
+ const reach =
+ 0.5 /
+ Math.max(...corners(source).map((point) => weight(toViewport, point)));
+ // A source point shown in the viewport takes the inverse of its weight there, at least 1/deepest;
+ // one below half that lands outside the viewport.
+ const deepest = Math.max(
+ ...corners(viewport).map((point) => weight(toSource, point)),
+ );
+ const visible = deepest > 0 ? 0.5 / deepest : Infinity;
+ function place(point: Point): Point {
+ const [sx, sy, w] = apply(toSource, point);
+ return [sx / w, sy / w];
+ }
+ return {
+ /** The document position under a viewport point; none past where input stops. */
+ toDocument: (point: Point) =>
+ weight(toSource, point) >= reach ? place(point) : undefined,
+ /**
+ * The document points a pointer adds moving between two viewport points: where it crosses the
+ * line input stops at, then its end if input reaches it. A stroke that leaves and returns joins
+ * along that line, off the photo.
+ */
+ trail(from: Point, to: Point): Point[] {
+ const [a, b] = [weight(toSource, from), weight(toSource, to)];
+ const end = b >= reach ? [place(to)] : [];
+ if (a >= reach === b >= reach) {
+ return end;
+ }
+ const t = (a - reach) / (a - b);
+ const crossing = place([
+ from[0] + (to[0] - from[0]) * t,
+ from[1] + (to[1] - from[1]) * t,
+ ]);
+ return [crossing, ...end];
+ },
+ /** The viewport position of a document position; none behind the horizon. */
+ toScreen: (point: Point) => project(toViewport, point),
+ /** Viewport pixels per source pixel around a document position, by area. */
+ scale(point: Point) {
+ const [[a, b], [c, d]] = jacobian(toViewport, point);
+ return Math.sqrt(Math.abs(a * d - b * c));
+ },
+ /** The viewport direction of a source direction at a document position, along which lines stay lines. */
+ direction(point: Point, [dx, dy]: Point): Point {
+ const [[a, b], [c, d]] = jacobian(toViewport, point);
+ return [a * dx + b * dy, c * dx + d * dy];
+ },
+ /** A source ellipse's image, exactly where it is bounded; otherwise the part the viewport can show. */
+ ellipse(ellipse: Ellipse): Shape | undefined {
+ const exact = projectEllipse(toSource, ellipse);
+ if (exact) return { ellipse: exact };
+ const path = ellipsePath(toViewport, ellipse, visible);
+ return path ? { path } : undefined;
+ },
+ /** Path data for a source polygon's image, as far as the viewport can show it. */
+ polygon: (points: readonly Point[]) =>
+ polygonPath(toViewport, points, visible),
+ };
+}
+
+export type DocumentMapping = ReturnType<typeof createMapping>;
+
+/** The mapping of the viewport this renders in, following the scene's frame and the camera. */
export function useDocumentMapping() {
const camera = useViewport();
+ const document = useDocument();
const frame = useScene((scene) => scene.frame);
+ const size = document.resources.get(
+ useScene((scene) => scene.layers[0].source),
+ ).image.size;
const { view, scale, viewport } = camera;
return useMemo(
- () => ({
- /** Source pixels per viewport pixel along the frame's axes. */
- pixelsPerViewportPixel: Math.abs(frame.scale[0]) / scale,
- /** The document position under client coordinates, given the viewport bounds. */
- toDocument(clientX: number, clientY: number, box: DOMRect): Point {
- const offset = sourceOffset(
- frame,
- (clientX - box.left - box.width / 2 - view.pan[0]) / scale,
- (clientY - box.top - box.height / 2 - view.pan[1]) / scale,
- );
- return [frame.center[0] + offset[0], frame.center[1] + offset[1]];
- },
- /** Viewport pixels, relative to the viewport's top-left corner, of a document position. */
- toScreen(point: Point): Point {
- const [x, y] = outputOffset(
- frame,
- point[0] - frame.center[0],
- point[1] - frame.center[1],
- );
- return [
- viewport[0] / 2 + view.pan[0] + x * scale,
- viewport[1] / 2 + view.pan[1] + y * scale,
- ];
- },
- }),
- [frame, view, scale, viewport],
+ () => createMapping(frame, size, view, scale, viewport),
+ [frame, size, view, scale, viewport],
);
}
diff --git a/src/core/image/frame.ts b/src/core/image/frame.ts
index ff1cf2d..1097a41 100644
--- a/src/core/image/frame.ts
+++ b/src/core/image/frame.ts
@@ -1,5 +1,12 @@
import { z } from "zod/mini";
-import { parse, point } from "@/lib/parse";
+import { parse, point, range } from "@/lib/parse";
+import {
+ apply,
+ type Homography,
+ jacobian,
+ multiply,
+ stretch,
+} from "@/lib/projective";
export type Point = readonly [number, number];
export type ImageFrame = {
@@ -8,9 +15,14 @@ export type ImageFrame = {
rotation: number;
angle: number;
scale: Point;
+ /** Keystone along the source's x and y axes, −100 to 100: more enlarges its right or bottom edge. */
+ perspective: Point;
};
-/** Source pixels under the output center, output dimensions, and signed source scale. */
+/**
+ * Corrected pixels under the output center, output dimensions, signed scale, and the perspective
+ * correction the source takes first. Without perspective, corrected pixels are source pixels.
+ */
export function imageFrame(size: readonly number[]): ImageFrame {
return {
center: [size[0] / 2, size[1] / 2],
@@ -18,6 +30,7 @@ export function imageFrame(size: readonly number[]): ImageFrame {
rotation: 0,
angle: 0,
scale: [1, 1],
+ perspective: [0, 0],
};
}
@@ -28,6 +41,7 @@ export function frameValues(frame: ImageFrame) {
...frame.scale,
frame.rotation,
frame.angle,
+ ...frame.perspective,
];
}
@@ -35,6 +49,7 @@ const nonzero = z
.number()
.check(z.refine((value) => value !== 0, "Scale is zero"));
const side = z.number().check(z.minimum(1));
+const keystone = range(-100, 100);
export const frameSchema = z.object({
center: point,
@@ -42,13 +57,20 @@ export const frameSchema = z.object({
rotation: z.number(),
angle: z.number(),
scale: z.tuple([nonzero, nonzero]),
+ // Frames from before perspective correction have none.
+ perspective: z._default(z.tuple([keystone, keystone]), [0, 0]),
});
export function validateFrame(frame: unknown): ImageFrame {
return parse(frameSchema, frame, "Invalid image frame");
}
-export function sourceOffset(frame: ImageFrame, x: number, y: number): Point {
+/** Corrected-pixel offset of an output offset, through rotation, flips, and scale. */
+export function correctedOffset(
+ frame: ImageFrame,
+ x: number,
+ y: number,
+): Point {
const angle = (-(frame.rotation + frame.angle) * Math.PI) / 180;
const dx = x * frame.scale[0];
const dy = y * frame.scale[1];
@@ -58,7 +80,7 @@ export function sourceOffset(frame: ImageFrame, x: number, y: number): Point {
];
}
-/** Output offset of a source-pixel offset: the inverse of sourceOffset. */
+/** Output offset of a corrected-pixel offset: the inverse of correctedOffset. */
export function outputOffset(frame: ImageFrame, dx: number, dy: number): Point {
const angle = ((frame.rotation + frame.angle) * Math.PI) / 180;
return [
@@ -67,19 +89,130 @@ export function outputOffset(frame: ImageFrame, dx: number, dy: number): Point {
];
}
-/** Affine UV mapping used by image display and resampling, independent of editing tools. */
+/**
+ * The correction's weights over source coordinates running from −1 to 1 across the photo: a point
+ * corrects to itself over 1 + weights·point, so the center stays and lines stay straight. At 100 an
+ * edge takes 1/0.55 of its scale and the opposite one 1/1.45; at full strength on both axes every
+ * corner keeps a weight of 0.1 or more, in front of the horizon where the weight reaches 0.
+ */
+function weights(frame: ImageFrame): Point {
+ return [-0.0045 * frame.perspective[0], -0.0045 * frame.perspective[1]];
+}
+
+/**
+ * The perspective correction as a homogeneous map from source to corrected pixels, or back with
+ * `inverse`, the same map with opposite weights. A point in front of either maps in front of the other.
+ */
+export function correction(
+ frame: ImageFrame,
+ source: readonly number[],
+ inverse = false,
+): Homography {
+ const sign = inverse ? -1 : 1;
+ const [qx, qy] = weights(frame);
+ const [wx, wy] = [(2 * sign * qx) / source[0], (2 * sign * qy) / source[1]];
+ const [hx, hy] = [source[0] / 2, source[1] / 2];
+ const bias = sign * (qx + qy);
+ return [
+ 1 + hx * wx,
+ hx * wy,
+ -hx * bias,
+ hy * wx,
+ 1 + hy * wy,
+ -hy * bias,
+ wx,
+ wy,
+ 1 - bias,
+ ];
+}
+
+/** Output pixels, from the output center, to homogeneous source pixels. */
+export function outputToSource(
+ frame: ImageFrame,
+ source: readonly number[],
+): Homography {
+ const [ax, ay] = correctedOffset(frame, 1, 0);
+ const [bx, by] = correctedOffset(frame, 0, 1);
+ const [cx, cy] = frame.center;
+ return multiply(correction(frame, source, true), [
+ ax,
+ bx,
+ cx,
+ ay,
+ by,
+ cy,
+ 0,
+ 0,
+ 1,
+ ]);
+}
+
+/** Source pixels to homogeneous output pixels, from the output center. */
+export function sourceToOutput(
+ frame: ImageFrame,
+ source: readonly number[],
+): Homography {
+ const [ax, ay] = outputOffset(frame, 1, 0);
+ const [bx, by] = outputOffset(frame, 0, 1);
+ const [cx, cy] = frame.center;
+ return multiply(
+ [ax, bx, -ax * cx - bx * cy, ay, by, -ay * cx - by * cy, 0, 0, 1],
+ correction(frame, source),
+ );
+}
+
+/**
+ * How much the perspective correction enlarges the photo where the output shows it largest, at
+ * one of its corners: 1 without perspective.
+ */
+export function correctionStretch(
+ frame: ImageFrame,
+ source: readonly number[],
+) {
+ const toSource = outputToSource(frame, source);
+ const correct = correction(frame, source);
+ return Math.max(
+ ...[-1, 1].flatMap((x) =>
+ [-1, 1].map((y) => {
+ const [sx, sy, w] = apply(toSource, [
+ (x * frame.size[0]) / 2,
+ (y * frame.size[1]) / 2,
+ ]);
+ return stretch(jacobian(correct, [sx / w, sy / w]));
+ }),
+ ),
+ );
+}
+
+/**
+ * Homogeneous UV mapping used by image display and resampling, independent of editing tools: an
+ * output UV reaches source UV `origin + u·xAxis + v·yAxis` over the sum of their third components,
+ * 1 without perspective.
+ */
export function frameTransform(
frame: ImageFrame,
sourceSize: readonly number[],
) {
- const xAxis = sourceOffset(frame, frame.size[0], 0).map(
+ const xAxis = correctedOffset(frame, frame.size[0], 0).map(
(value, axis) => value / sourceSize[axis],
);
- const yAxis = sourceOffset(frame, 0, frame.size[1]).map(
+ const yAxis = correctedOffset(frame, 0, frame.size[1]).map(
(value, axis) => value / sourceSize[axis],
);
const origin = frame.center.map(
(value, axis) => value / sourceSize[axis] - (xAxis[axis] + yAxis[axis]) / 2,
);
- return { origin, xAxis, yAxis };
+ // Corrected UV u reaches source UV (u + (w − 1)/2) / w, w = 1 − weights·(2u − 1).
+ const [wx, wy] = weights(frame);
+ const slope = ([x, y]: number[]) => -2 * (wx * x + wy * y);
+ const level = 1 + slope(origin) + wx + wy;
+ const axis = (values: number[], weight: number, base = 0) => [
+ ...values.map((value) => value + (weight - base) / 2),
+ weight,
+ ];
+ return {
+ origin: axis(origin, level, 1),
+ xAxis: axis(xAxis, slope(xAxis)),
+ yAxis: axis(yAxis, slope(yAxis)),
+ };
}
diff --git a/src/core/renderer/transform/transform.wgsl b/src/core/renderer/transform/transform.wgsl
index 3a90c4c..d231aa5 100644
--- a/src/core/renderer/transform/transform.wgsl
+++ b/src/core/renderer/transform/transform.wgsl
@@ -1,8 +1,11 @@
+// Homogeneous: the third components sum to the weight dividing the point, 1 for a frame without perspective.
export struct Transform {
- origin: vec2f,
- xAxis: vec2f,
- yAxis: vec2f,
+ origin: vec3f,
+ xAxis: vec3f,
+ yAxis: vec3f,
}
+/** Source UV under an output UV; a point past the perspective's horizon lands outside the source. */
export fn sourcePoint(transform: Transform, uv: vec2f) -> vec2f {
- return transform.origin + uv.x * transform.xAxis + uv.y * transform.yAxis;
+ let p = transform.origin + uv.x * transform.xAxis + uv.y * transform.yAxis;
+ return select(vec2f(-1.0), p.xy / p.z, p.z > 0.0);
}
diff --git a/src/features/crop/edits.ts b/src/features/crop/edits.ts
index ed0a75d..4d26b85 100644
--- a/src/features/crop/edits.ts
+++ b/src/features/crop/edits.ts
@@ -1,7 +1,7 @@
import type { EditorDocument } from "@/core/document";
import { type ImageFrame, validateFrame } from "@/core/image/frame";
-/** Replaces the crop, rotation, and flip as one edit, respecting an open history group; the frame is copied. */
+/** Replaces the crop, rotation, flips, and perspective as one edit, respecting an open history group; the frame is copied. */
export function applyCrop(document: EditorDocument, frame: ImageFrame) {
document.edit({ ...document.scene.getState(), frame: validateFrame(frame) });
}
diff --git a/src/features/crop/geometry.ts b/src/features/crop/geometry.ts
index a4918ca..45f3d05 100644
--- a/src/features/crop/geometry.ts
+++ b/src/features/crop/geometry.ts
@@ -1,27 +1,111 @@
-import { type ImageFrame, type Point, sourceOffset } from "@/core/image/frame";
+import {
+ correctedOffset,
+ correction,
+ type ImageFrame,
+ type Point,
+} from "@/core/image/frame";
import { clamp } from "@/lib/math";
+import { apply, type Homography } from "@/lib/projective";
-function radius(frame: ImageFrame): Point {
- const x = sourceOffset(frame, frame.size[0] / 2, 0);
- const y = sourceOffset(frame, 0, frame.size[1] / 2);
- return [Math.abs(x[0]) + Math.abs(y[0]), Math.abs(x[1]) + Math.abs(y[1])];
+/** A half-plane a·x + b·y + c ≥ 0 with a unit normal, so its value is a distance. */
+type Edge = readonly [number, number, number];
+
+/** A point of the photo stays in front of the correction's horizon, so its weight is positive. */
+function map(matrix: Homography, point: Point): Point {
+ const [x, y, w] = apply(matrix, point);
+ return [x / w, y / w];
+}
+
+/**
+ * The photo in corrected pixels: a corrected point lies inside these half-planes exactly when its
+ * source lies inside the photo. Without perspective they are the photo's edges.
+ */
+function edges(frame: ImageFrame, source: Point): Edge[] {
+ const matrix = correction(frame, source, true);
+ const [x, y, w] = [0, 3, 6].map((row) => matrix.slice(row, row + 3));
+ return [
+ x,
+ w.map((value, i) => source[0] * value - x[i]),
+ y,
+ w.map((value, i) => source[1] * value - y[i]),
+ ].map(([a, b, c]) => {
+ const length = Math.hypot(a, b);
+ return [a / length, b / length, c / length] as const;
+ });
+}
+
+function distance(edge: Edge, [x, y]: Point) {
+ return edge[0] * x + edge[1] * y + edge[2];
+}
+
+/** Corner offsets from the center, in corrected pixels, always in the same order. */
+function corners(frame: ImageFrame) {
+ return [-1, 1].flatMap((x) =>
+ [-1, 1].map((y) =>
+ correctedOffset(frame, (x * frame.size[0]) / 2, (y * frame.size[1]) / 2),
+ ),
+ );
}
-/** Translation slides independently along source edges. */
+/**
+ * The closest point to `point` inside the half-planes, which hold `inside`: the point itself, its
+ * projection onto an edge, or a corner where two edges meet.
+ */
+function nearest(point: Point, planes: readonly Edge[], inside: Point): Point {
+ const holds = (candidate: Point) =>
+ planes.every((plane) => distance(plane, candidate) >= -1e-7);
+ const candidates = planes.flatMap((plane, i): Point[] => {
+ const away = distance(plane, point);
+ const vertices = planes.slice(i + 1).flatMap((other): Point[] => {
+ const determinant = plane[0] * other[1] - plane[1] * other[0];
+ if (!determinant) return [];
+ return [
+ [
+ (plane[1] * other[2] - other[1] * plane[2]) / determinant,
+ (other[0] * plane[2] - plane[0] * other[2]) / determinant,
+ ],
+ ];
+ });
+ return [
+ [point[0] - away * plane[0], point[1] - away * plane[1]],
+ ...vertices,
+ ];
+ });
+ const length = (candidate: Point) =>
+ Math.hypot(candidate[0] - point[0], candidate[1] - point[1]);
+ return [point, ...candidates]
+ .filter(holds)
+ .reduce(
+ (best, candidate) =>
+ length(candidate) < length(best) ? candidate : best,
+ inside,
+ );
+}
+
+/** Translation slides along the photo's edges, to the nearest center that keeps it covered. */
export function move(
frame: ImageFrame,
dx: number,
dy: number,
source: Point,
): ImageFrame {
- const offset = sourceOffset(frame, dx, dy);
- const half = radius(frame);
+ const offset = correctedOffset(frame, dx, dy);
+ const reach = corners(frame);
+ // Where the center may go: each edge, moved in by the corner that reaches furthest across it.
+ const planes = edges(frame, source).map(
+ ([a, b, c]): Edge => [
+ a,
+ b,
+ c + Math.min(...reach.map(([x, y]) => a * x + b * y)),
+ ],
+ );
return {
...frame,
- center: [
- clamp(frame.center[0] + offset[0], half[0], source[0] - half[0]),
- clamp(frame.center[1] + offset[1], half[1], source[1] - half[1]),
- ],
+ center: nearest(
+ [frame.center[0] + offset[0], frame.center[1] + offset[1]],
+ planes,
+ frame.center,
+ ),
};
}
@@ -49,23 +133,28 @@ export function resize(
)
: Math.max(1, width);
const h = ratio ? w / ratio : Math.max(1, height);
- const offset = sourceOffset(
+ const offset = correctedOffset(
frame,
((w - frame.size[0]) * sx) / 2,
((h - frame.size[1]) * sy) / 2,
);
- const before = radius(frame);
- const after = radius({ ...frame, size: [w, h] });
+ const center: Point = [
+ frame.center[0] + offset[0],
+ frame.center[1] + offset[1],
+ ];
+ const before = corners(frame);
+ const after = corners({ ...frame, size: [w, h] });
+ // Corners move in straight lines, so each edge allows a fraction of the way.
let fraction = 1;
- for (const axis of [0, 1]) {
- for (const sign of [-1, 1]) {
- const start = frame.center[axis] + sign * before[axis];
- const end = frame.center[axis] + offset[axis] + sign * after[axis];
- if (end < -1e-7 || end > source[axis] + 1e-7) {
- fraction = Math.min(
- fraction,
- clamp((clamp(end, 0, source[axis]) - start) / (end - start)),
- );
+ for (const edge of edges(frame, source)) {
+ for (const [i, [x, y]] of before.entries()) {
+ const start = distance(edge, [frame.center[0] + x, frame.center[1] + y]);
+ const end = distance(edge, [
+ center[0] + after[i][0],
+ center[1] + after[i][1],
+ ]);
+ if (end < -1e-7) {
+ fraction = Math.min(fraction, clamp(start / (start - end)));
}
}
}
@@ -80,26 +169,108 @@ export function resize(
};
}
-/** Magnify around the current source center only as much as coverage requires. */
+/** Magnify around the current center only as much as coverage requires. */
+function cover(frame: ImageFrame, source: Point): ImageFrame {
+ const reach = corners(frame);
+ let fit = 1;
+ for (const edge of edges(frame, source)) {
+ const room = distance(edge, frame.center);
+ for (const [x, y] of reach) {
+ const across = edge[0] * x + edge[1] * y;
+ if (across < 0) {
+ fit = Math.min(fit, room / -across);
+ }
+ }
+ }
+ return { ...frame, scale: [frame.scale[0] * fit, frame.scale[1] * fit] };
+}
+
+/** Straightens from the unmagnified frame, so turning back restores it. */
export function rotate(
frame: ImageFrame,
angle: number,
source: Point,
): ImageFrame {
- const next: ImageFrame = {
- ...frame,
- angle,
- scale: [Math.sign(frame.scale[0]), Math.sign(frame.scale[1])],
- };
- const half = radius(next);
- const scale = Math.min(
- 1,
- ...source.map(
- (size, axis) =>
- Math.min(frame.center[axis], size - frame.center[axis]) / half[axis],
- ),
+ return cover(
+ {
+ ...frame,
+ angle,
+ scale: [Math.sign(frame.scale[0]), Math.sign(frame.scale[1])],
+ },
+ source,
+ );
+}
+
+/**
+ * Corrects perspective from the unmagnified frame, keeping the source pixel under the center there,
+ * which keeps the center inside the photo.
+ */
+export function correct(
+ frame: ImageFrame,
+ perspective: Point,
+ source: Point,
+): ImageFrame {
+ const anchor = map(correction(frame, source, true), frame.center);
+ const next = { ...frame, perspective };
+ return rotate(
+ { ...next, center: map(correction(next, source), anchor) },
+ frame.angle,
+ source,
);
- return { ...next, scale: [next.scale[0] * scale, next.scale[1] * scale] };
+}
+
+/** For each quarter turn clockwise, the source axis each displayed axis shows and its direction. */
+const quarters = [
+ [
+ [0, 1],
+ [1, 1],
+ ],
+ [
+ [1, -1],
+ [0, 1],
+ ],
+ [
+ [0, -1],
+ [1, -1],
+ ],
+ [
+ [1, 1],
+ [0, -1],
+ ],
+] as const;
+
+/** The source axis behind each displayed one and its direction, after the frame's turn and flips. */
+function orientation(frame: ImageFrame) {
+ const quarter = ((Math.round(frame.rotation / 90) % 4) + 4) % 4;
+ return quarters[quarter].map(
+ ([axis, sign], i) => [axis, sign * Math.sign(frame.scale[i])] as const,
+ );
+}
+
+function signed(sign: number, value: number) {
+ return sign > 0 ? value : 0 - value;
+}
+
+/** The perspective along the displayed axes, horizontal then vertical, as quarter turns and flips show it. */
+export function shownPerspective(frame: ImageFrame): Point {
+ const [x, y] = orientation(frame);
+ return [
+ signed(x[1], frame.perspective[x[0]]),
+ signed(y[1], frame.perspective[y[0]]),
+ ];
+}
+
+/** Corrects perspective given along the displayed axes. */
+export function correctShown(
+ frame: ImageFrame,
+ shown: Point,
+ source: Point,
+): ImageFrame {
+ const perspective: [number, number] = [0, 0];
+ for (const [i, [axis, sign]] of orientation(frame).entries()) {
+ perspective[axis] = signed(sign, shown[i]);
+ }
+ return correct(frame, perspective, source);
}
export function turn(frame: ImageFrame, direction: number): ImageFrame {
diff --git a/src/features/crop/view.tsx b/src/features/crop/view.tsx
index a915d6d..08d2271 100644
--- a/src/features/crop/view.tsx
+++ b/src/features/crop/view.tsx
@@ -1,12 +1,13 @@
import { Button } from "@roprgm/ui/button";
+import { IconButton } from "@roprgm/ui/icon-button";
import { Select } from "@roprgm/ui/select";
import { Slider } from "@roprgm/ui/slider";
-import { Tooltip, TooltipContent, TooltipTrigger } from "@roprgm/ui/tooltip";
import { type KeyboardEvent, useState } from "react";
import { DockChips, DockControls } from "@/components/editor/dock";
import { Image } from "@/components/editor/image";
import { EditorLayout } from "@/components/editor/layout";
import { PanelBody, PanelHeader } from "@/components/editor/panel";
+import type { Parameter } from "@/components/editor/parameter";
import { useDocument, useEditorSession } from "@/components/editor/session";
import { EditorViewport, ViewportStage } from "@/components/editor/viewport";
import { FlipIcon } from "@/components/icons/flip";
@@ -14,8 +15,16 @@ import { RotateIcon } from "@/components/icons/rotate";
import { Dial } from "@/components/ui/dial";
import { imageFrame, type Point } from "@/core/image/frame";
import { useShortcuts } from "@/hooks/use-shortcuts";
+import { isTyping, typingFields } from "@/lib/dom";
import { applyCrop } from "./edits";
-import { fitRatio, flip, rotate, turn } from "./geometry";
+import {
+ correctShown,
+ fitRatio,
+ flip,
+ rotate,
+ shownPerspective,
+ turn,
+} from "./geometry";
import { CropOverlay } from "./overlay";
const actions = [
@@ -25,6 +34,7 @@ const actions = [
{ label: "Flip vertical", flip: 1, transform: "rotate(90deg)" },
] as const;
+/** A turn or flip as a circle captioned below, the shape of the rotation dial beside it. */
function ActionButton({
action,
onClick,
@@ -32,24 +42,21 @@ function ActionButton({
action: (typeof actions)[number];
onClick: () => void;
}) {
- const Icon = "turn" in action ? RotateIcon : FlipIcon;
+ const turn = "turn" in action;
+ const Icon = turn ? RotateIcon : FlipIcon;
return (
- <Tooltip>
- <TooltipTrigger
- render={
- <Button
- variant="ghost"
- aria-label={action.label}
- className="flex h-9 items-center justify-center gap-1 rounded-md px-1"
- onClick={onClick}
- >
- <Icon style={{ transform: action.transform }} />
- {"turn" in action && <span>90°</span>}
- </Button>
- }
- />
- <TooltipContent>{action.label}</TooltipContent>
- </Tooltip>
+ <div className="flex flex-col items-center gap-1.5">
+ <IconButton
+ label={action.label}
+ className="size-11.5 rounded-full material-field"
+ onClick={onClick}
+ >
+ <Icon style={{ transform: action.transform }} />
+ </IconButton>
+ <span aria-hidden="true" className="whitespace-nowrap text-secondary">
+ {turn ? "90°" : "Flip"}
+ </span>
+ </div>
);
}
@@ -77,12 +84,18 @@ function preset(choice: Choice) {
return presets.find(([name]) => name === choice);
}
-/** Enter on a panel button is its click; elsewhere it applies the crop. */
+/** What the dock's middle row adjusts on a phone. */
+type DockMode = "rotate" | "perspective";
+const dockModes = [
+ ["rotate", "Rotate"],
+ ["perspective", "Perspective"],
+] as const;
+
+/** Enter on a panel button is its click and in a field commits the value; elsewhere it applies the crop. */
function keepEnter(event: KeyboardEvent) {
if (
event.key === "Enter" &&
- event.target instanceof Element &&
- event.target.closest("button")
+ isTyping(event.target, `${typingFields}, button`)
) {
event.stopPropagation();
}
@@ -93,6 +106,7 @@ export function CropEditor({ onClose }: { onClose: () => void }) {
const { camera } = useEditorSession();
const [frame, setFrame] = useState(() => document.scene.getState().frame);
const [reference, setReference] = useState(frame.size);
+ const [mode, setMode] = useState<DockMode>("rotate");
const sourceId = document.scene.getState().layers[0].source;
const [width, height] = document.resources.get(sourceId).image.size;
const source: Point = [width, height];
@@ -169,8 +183,34 @@ export function CropEditor({ onClose }: { onClose: () => void }) {
defaultValue: 0,
onChange: (angle: number) => setFrame(rotate(frame, angle, source)),
};
+ const shown = shownPerspective(frame);
+ /** Perspective along the displayed axes, so Vertical stays vertical through quarter turns and flips. */
+ function keystone(id: string, label: string, axis: 0 | 1): Parameter {
+ return {
+ id,
+ label,
+ min: -100,
+ max: 100,
+ step: 1,
+ value: shown[axis],
+ defaultValue: 0,
+ onChange: (value) =>
+ setFrame(
+ correctShown(
+ frame,
+ axis ? [shown[0], value] : [value, shown[1]],
+ source,
+ ),
+ ),
+ };
+ }
+ const perspective = [
+ keystone("vertical", "Vertical", 1),
+ keystone("horizontal", "Horizontal", 0),
+ ];
const size = `${frame.size.map(Math.round).join(" × ")} px`;
- // On a phone the ratios are chips, rotation a dial between the turns and flips, and the actions a footer.
+ // On a phone the ratios are chips beside a choice of what the row between them and the actions
+ // footer adjusts: rotation between the turns and flips, or perspective's dials.
const dock = (
<fieldset
aria-label="Crop"
@@ -178,20 +218,32 @@ export function CropEditor({ onClose }: { onClose: () => void }) {
onKeyDown={keepEnter}
>
<DockControls
+ parameters={mode === "perspective" ? perspective : undefined}
header={
- <DockChips
- label="Aspect ratio"
- items={ratioOptions.map(
- ({ value, label }) => [value, label] as const,
- )}
- value={choice}
- onChange={changeRatio}
- />
+ <div className="flex min-w-0 items-center gap-2">
+ <div className="shrink-0">
+ <DockChips
+ label="Controls"
+ items={dockModes}
+ value={mode}
+ onChange={setMode}
+ />
+ </div>
+ <DockChips
+ label="Aspect ratio"
+ items={ratioOptions.map(
+ ({ value, label }) => [value, label] as const,
+ )}
+ value={choice}
+ onChange={changeRatio}
+ />
+ </div>
}
>
+ {/* As tall as the dial row that takes its place, so switching keeps the canvas still. */}
<section
aria-label="Rotate and flip image"
- className="flex min-h-0 flex-1 items-center justify-center gap-2 pb-3"
+ className="flex h-21 shrink-0 items-center justify-center gap-3"
>
{actions.slice(0, 2).map((action) => (
<ActionButton
@@ -209,21 +261,21 @@ export function CropEditor({ onClose }: { onClose: () => void }) {
/>
))}
</section>
- <div className="flex items-center gap-1.5 px-2.5">
- <Button variant="ghost" size="sm" onClick={reset}>
- Reset
- </Button>
- <span className="flex-1 truncate text-center text-secondary tabular-nums">
- {size}
- </span>
- <Button variant="ghost" size="sm" onClick={onClose}>
- Cancel
- </Button>
- <Button size="sm" onClick={apply}>
- Apply
- </Button>
- </div>
</DockControls>
+ <div className="flex items-center gap-1.5 px-2.5 pb-3">
+ <Button variant="ghost" size="sm" onClick={reset}>
+ Reset
+ </Button>
+ <span className="flex-1 truncate text-center text-secondary tabular-nums">
+ {size}
+ </span>
+ <Button variant="ghost" size="sm" onClick={onClose}>
+ Cancel
+ </Button>
+ <Button size="sm" onClick={apply}>
+ Apply
+ </Button>
+ </div>
</fieldset>
);
return (
@@ -247,7 +299,15 @@ export function CropEditor({ onClose }: { onClose: () => void }) {
className="flex min-h-0 min-w-0 flex-1 flex-col *:not-last:shadow-[inset_0_-1px_0_var(--color-edge)]"
onKeyDown={keepEnter}
>
- <PanelBody header={<PanelHeader title="Crop" onClose={onClose} />}>
+ <PanelBody
+ header={
+ <PanelHeader title="Crop" onClose={onClose}>
+ <Button variant="ghost" size="sm" onClick={reset}>
+ Reset
+ </Button>
+ </PanelHeader>
+ }
+ >
<section aria-label="Crop tool" className="space-y-5 p-3.5">
<div className="flex items-center justify-between text-secondary">
Aspect ratio
@@ -264,17 +324,9 @@ export function CropEditor({ onClose }: { onClose: () => void }) {
</div>
<section
aria-label="Rotate and flip image"
- className="grid grid-cols-4 items-center gap-2"
+ className="grid grid-cols-4 items-start gap-2"
>
- <h3 className="col-span-3 text-secondary">Rotate & flip</h3>
- <Button
- variant="ghost"
- size="sm"
- className="justify-self-end"
- onClick={reset}
- >
- Reset
- </Button>
+ <h3 className="col-span-4 text-secondary">Rotate & flip</h3>
{actions.map((action) => (
<ActionButton
key={action.label}
@@ -284,6 +336,12 @@ export function CropEditor({ onClose }: { onClose: () => void }) {
))}
</section>
<Slider {...rotation} />
+ <section aria-label="Perspective" className="space-y-3">
+ <h3 className="text-secondary">Perspective</h3>
+ {perspective.map(({ id, ...parameter }) => (
+ <Slider key={id} {...parameter} />
+ ))}
+ </section>
<p className="text-secondary">
Drag edges or corners to crop, inside to move, outside to
rotate. Space + drag to pan; Ctrl/⌘ + scroll to zoom.
diff --git a/src/features/heal/anchor.tsx b/src/features/heal/anchor.tsx
index f26b3c7..43c2c8b 100644
--- a/src/features/heal/anchor.tsx
+++ b/src/features/heal/anchor.tsx
@@ -68,7 +68,10 @@ function DraggedAnchor({
else document.history.cancel();
}
function point(event: PointerEvent<SVGCircleElement>, box: DOMRect) {
- return mapping.toDocument(event.clientX, event.clientY, box);
+ return mapping.toDocument([
+ event.clientX - box.left,
+ event.clientY - box.top,
+ ]);
}
function cancel() {
const active = current.current;
@@ -81,6 +84,7 @@ function DraggedAnchor({
useEffect(() => () => cancel(), []);
function start(event: PointerEvent<SVGCircleElement>) {
const box = camera.ref.current?.getBoundingClientRect();
+ const at = box && point(event, box);
if (
current.current ||
event.button !== 0 ||
@@ -88,7 +92,8 @@ function DraggedAnchor({
camera.panMode ||
event.shiftKey ||
event.altKey ||
- !box
+ !box ||
+ !at
) {
return;
}
@@ -96,7 +101,7 @@ function DraggedAnchor({
current.current = {
pointer: event.pointerId,
box,
- start: point(event, box),
+ start: at,
from: drag.from,
next: drag.from,
unsubscribe: document.history.status.subscribe((state, previous) => {
@@ -117,6 +122,8 @@ function DraggedAnchor({
const active = current.current;
if (!active || active.pointer !== event.pointerId) return;
const at = point(event, active.box);
+ // Past where input stops the anchor waits for the pointer to return.
+ if (!at) return;
active.next = [
active.from[0] + at[0] - active.start[0],
active.from[1] + at[1] - active.start[1],
diff --git a/src/features/heal/outline.tsx b/src/features/heal/outline.tsx
index 86d5f0b..706cbc9 100644
--- a/src/features/heal/outline.tsx
+++ b/src/features/heal/outline.tsx
@@ -1,85 +1,71 @@
-import { memo, type PointerEvent, useId, useState } from "react";
-import { useDocumentMapping } from "@/components/editor/mapping";
+import { useId, useState } from "react";
+import { MappedShape } from "@/components/editor/mapped-shape";
+import {
+ type DocumentMapping,
+ type Shape,
+ 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";
+import { findHealPatch } from "./model";
+/**
+ * A hard stroke as the viewport shows it: the dabs it lays at its points and the rectangles they sweep
+ * between them, exactly as perspective maps their union.
+ */
type Geometry = {
mode: BrushStroke["mode"];
- center: Point;
- d: string;
- first: Point;
- last: Point;
- width: number;
- bounds: { left: number; top: number; right: number; bottom: number };
+ /** Where the stroke starts, unless it is behind the horizon. */
+ first?: Point;
+ sweeps: string;
+ dabs: Shape[];
};
-type Mapping = ReturnType<typeof useDocumentMapping>;
function geometry(
stroke: BrushStroke,
offset: Point,
- mapping: Mapping,
+ mapping: DocumentMapping,
): Geometry {
- const points = stroke.points.map(([x, y]) =>
- mapping.toScreen([x + offset[0], y + offset[1]]),
+ const points = stroke.points.map(
+ ([x, y]): Point => [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);
- }
+ const radius = stroke.size / 2;
+ // Each rectangle runs the same way round, so one path's fill unites them.
+ const sweeps = points.slice(1).map((end, i) => {
+ const start = points[i];
+ const length = Math.hypot(end[0] - start[0], end[1] - start[1]);
+ if (!length) return "";
+ const nx = ((start[1] - end[1]) / length) * radius;
+ const ny = ((end[0] - start[0]) / length) * radius;
+ return mapping.polygon([
+ [start[0] - nx, start[1] - ny],
+ [end[0] - nx, end[1] - ny],
+ [end[0] + nx, end[1] + ny],
+ [start[0] + nx, start[1] + ny],
+ ]);
+ });
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 },
+ first: mapping.toScreen(points[0]),
+ sweeps: sweeps.join(""),
+ dabs: points.flatMap(
+ (center) =>
+ mapping.ellipse({ center, radii: [radius, radius], angle: 0 }) ?? [],
+ ),
};
}
-function StrokeShape({
- shape,
- width,
- color,
-}: {
- shape: Geometry;
- width: number;
- color: string;
-}) {
+function StrokeShape({ shape, color }: { shape: Geometry; color: string }) {
return (
<>
- <path
- d={shape.d}
- fill="none"
- stroke={color}
- strokeWidth={width}
- strokeLinecap="round"
- strokeLinejoin="round"
- />
- <circle
- cx={shape.first[0]}
- cy={shape.first[1]}
- r={width / 2}
- fill={color}
- />
- <circle
- cx={shape.last[0]}
- cy={shape.last[1]}
- r={width / 2}
- fill={color}
- />
+ <path d={shape.sweeps} fill={color} />
+ {shape.dabs.map((dab, index) => (
+ <MappedShape key={index} shape={dab} fill={color} />
+ ))}
</>
);
}
@@ -100,7 +86,6 @@ function CoverageMask({
<StrokeShape
key={index}
shape={shape}
- width={shape.width}
color={shape.mode === "paint" ? "white" : "black"}
/>
))}
@@ -108,6 +93,26 @@ function CoverageMask({
);
}
+/**
+ * Viewport bounds of the dabs, which hold the rectangles between them too; a path, the part of a dab
+ * that reaches the horizon, may run anywhere.
+ */
+function bounds(shapes: readonly Geometry[], viewport: Point) {
+ let [left, top, right, bottom] = [Infinity, Infinity, -Infinity, -Infinity];
+ for (const dab of shapes.flatMap((shape) => shape.dabs)) {
+ if ("path" in dab) {
+ return { left: 0, top: 0, right: viewport[0], bottom: viewport[1] };
+ }
+ const [x, y] = dab.ellipse.center;
+ const reach = Math.max(...dab.ellipse.radii);
+ left = Math.min(left, x - reach);
+ top = Math.min(top, y - reach);
+ right = Math.max(right, x + reach);
+ bottom = Math.max(bottom, y + reach);
+ }
+ return { left, top, right, bottom };
+}
+
function Outline({
shapes,
kind,
@@ -118,23 +123,12 @@ function Outline({
const mask = useId();
const edge = `${mask}-edge`;
const { viewport } = useViewport();
+ const box = bounds(shapes, viewport);
// 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,
- );
+ const left = Math.max(-4, box.left - 4);
+ const top = Math.max(-4, box.top - 4);
+ const right = Math.min(viewport[0] + 4, box.right + 4);
+ const bottom = Math.min(viewport[1] + 4, box.bottom + 4);
if (right <= left || bottom <= top) return null;
const region = { x: left, y: top, width: right - left, height: bottom - top };
return (
@@ -243,72 +237,26 @@ export function HealPatchOutline({
onDrop: moveSource,
}
: undefined;
+ const destinationAnchor = destination[0].first;
+ const sourceAnchor = source ? source[0].first : undefined;
return (
<>
<Outline shapes={destination} kind="destination" />
- <HealAnchor
- kind="destination"
- center={destination[0].center}
- drag={destinationDrag}
- />
+ {destinationAnchor && (
+ <HealAnchor
+ kind="destination"
+ center={destinationAnchor}
+ drag={destinationDrag}
+ />
+ )}
{source && (
<>
<Outline shapes={source} kind="source" />
- <HealAnchor
- kind="source"
- center={source[0].center}
- drag={sourceDrag}
- />
+ {sourceAnchor && (
+ <HealAnchor kind="source" center={sourceAnchor} drag={sourceDrag} />
+ )}
</>
)}
</>
);
}
-
-/** Keeps canvas selection on the painted geometry without adding a visible marker over the result. */
-export const HealPatchHitTarget = memo(function HealPatchHitTarget({
- patch,
- onSelect,
-}: {
- patch: HealPatch;
- onSelect: (id: string) => void;
-}) {
- 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);
- };
- return (
- <g
- data-heal-hit-target={patch.id}
- onPointerDown={select}
- pointerEvents="all"
- >
- {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.tsx
index ace8a21..3e801e5 100644
--- a/src/features/heal/overlay.tsx
+++ b/src/features/heal/overlay.tsx
@@ -1,13 +1,15 @@
-import { useEffect, useRef, useState } from "react";
+import { type PointerEvent, useEffect, useRef, useState } from "react";
import { useGpu } from "vgpu-react";
import { BrushCanvas } from "@/components/editor/brush-canvas";
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 { useViewport } from "@/components/editor/viewport";
import { type BrushStroke, findLayer, type Layer } from "@/core/document";
import type { Point } from "@/core/image/frame";
import { useDisposable } from "@/hooks/use-disposable";
+import { containsTarget } from "@/lib/dom";
import {
addHealPatch,
addHealStroke,
@@ -16,12 +18,8 @@ import {
setHealSource,
} from "./edits";
import { useHealing } from "./mode";
-import { dabTouchesImage, findHealPatch } from "./model";
-import {
- HealPatchHitTarget,
- HealPatchOutline,
- HealStrokePreview,
-} from "./outline";
+import { dabTouchesImage, findHealPatch, patchContains } from "./model";
+import { HealPatchOutline, HealStrokePreview } from "./outline";
import { createHealSearch } from "./source";
function isHealLayer(layer: Layer): layer is Extract<Layer, { kind: "heal" }> {
@@ -38,6 +36,7 @@ export function HealOverlay({
}) {
const document = useDocument();
const renderer = useRenderer();
+ const camera = useViewport();
const mapping = useDocumentMapping();
const gpu = useGpu();
const search = useDisposable(() => createHealSearch(gpu), [gpu]);
@@ -113,164 +112,192 @@ export function HealOverlay({
}
}
const marker = source && mapping.toScreen(source);
+ /**
+ * A press within 8 viewport pixels of another patch selects the topmost one instead of painting; a
+ * second finger belongs to the canvas, which turns a stroke into a pinch.
+ */
+ function selectPatchAt(event: PointerEvent<HTMLDivElement>) {
+ if (
+ !containsTarget(event) ||
+ event.button !== 0 ||
+ !event.isPrimary ||
+ event.shiftKey ||
+ event.altKey ||
+ camera.panMode
+ ) {
+ return;
+ }
+ const box = camera.ref.current?.getBoundingClientRect();
+ const at =
+ box &&
+ mapping.toDocument([event.clientX - box.left, event.clientY - box.top]);
+ const hit =
+ at &&
+ patches.findLast(
+ (patch) =>
+ patch.id !== selectedPatch &&
+ patchContains(patch.strokes, at, 8 / mapping.scale(at)),
+ );
+ if (!hit) return;
+ event.preventDefault();
+ event.stopPropagation();
+ selectPatch(hit.id);
+ }
return (
- <BrushCanvas
- label="Healing canvas"
- erase={false}
- 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];
- const size = document.resources.get(
- 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;
+ <div className="absolute inset-0" onPointerDownCapture={selectPatchAt}>
+ <BrushCanvas
+ label="Healing canvas"
+ erase={false}
+ editOnRelease={({ shift, alt }) =>
+ mode === "remove" || Boolean(selected && (shift || alt))
}
- const offset: Point = source ? [source[0] - x, source[1] - y] : [0, 0];
- const patch = addHealPatch(document, layer.id, painted, offset, mode);
- setDrawingPatch(patch);
- setResolvingSource(source ? undefined : patch);
- selectPatch(patch);
- pending.current = {
- layer: layer.id,
- mode,
- patch,
- automatic: !source,
- };
- return true;
- }}
- onExtend={(points) => {
- const current = pending.current;
- if (!current) return;
- if ("stroke" in current) {
- const stroke = {
- ...current.stroke,
- points: [...current.stroke.points, ...points],
+ onStart={(stroke, { shift, alt }) => {
+ const layer = selectedHealLayer();
+ if (!layer) return false;
+ const [x, y] = stroke.points[0];
+ const size = document.resources.get(
+ 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, painted, offset, mode);
+ setDrawingPatch(patch);
+ setResolvingSource(source ? undefined : patch);
+ selectPatch(patch);
+ pending.current = {
+ layer: layer.id,
+ mode,
+ patch,
+ automatic: !source,
};
- 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),
- );
- }
+ return true;
+ }}
+ onExtend={(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;
}
- }
- setDrawingPatch(undefined);
- if (!committed) setResolvingSource(undefined);
- }}
- 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]
+ 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),
+ );
+ }
}
- />
- </svg>
- )}
- {healLayer && patches.length > 0 && (
- <svg
- aria-hidden="true"
- className="absolute inset-0 size-full overflow-visible"
- style={{ pointerEvents: "none" }}
- >
- {patches.map(
- (patch) =>
- visiblePatch === patch.id &&
- !(draft && drawingPatch === patch.id) && (
- <g key={`outline-${patch.id}`} data-heal-patch={patch.id}>
- <HealPatchOutline
- layer={healLayer.id}
- patch={patch}
- showSource={
- patch.id !== drawingPatch && patch.id !== resolvingSource
- }
- interactive={
- patch.id === selectedPatch && patch.id !== drawingPatch
- }
- />
- </g>
- ),
- )}
- {patches.map(
- (patch) =>
- patch.id !== selectedPatch && (
- <HealPatchHitTarget
- key={`hit-${patch.id}`}
- patch={patch}
- onSelect={selectPatch}
- />
- ),
- )}
- </svg>
- )}
- {marker && mode !== "remove" && (
- <svg
- aria-hidden="true"
- className="pointer-events-none absolute inset-0 size-full overflow-visible"
- >
- <circle
- cx={marker[0]}
- cy={marker[1]}
- r="8"
- fill="none"
- stroke="white"
- />
- <path
- d={`M${marker[0] - 12} ${marker[1]}h24M${marker[0]} ${marker[1] - 12}v24`}
- stroke="white"
- />
- </svg>
- )}
- </BrushCanvas>
+ }
+ setDrawingPatch(undefined);
+ if (!committed) setResolvingSource(undefined);
+ }}
+ 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 && (
+ <svg
+ aria-hidden="true"
+ className="absolute inset-0 size-full overflow-visible"
+ style={{ pointerEvents: "none" }}
+ >
+ {patches.map(
+ (patch) =>
+ visiblePatch === patch.id &&
+ !(draft && drawingPatch === patch.id) && (
+ <g key={`outline-${patch.id}`} data-heal-patch={patch.id}>
+ <HealPatchOutline
+ layer={healLayer.id}
+ patch={patch}
+ showSource={
+ patch.id !== drawingPatch &&
+ patch.id !== resolvingSource
+ }
+ interactive={
+ patch.id === selectedPatch && patch.id !== drawingPatch
+ }
+ />
+ </g>
+ ),
+ )}
+ </svg>
+ )}
+ {marker && mode !== "remove" && (
+ <svg
+ aria-hidden="true"
+ className="pointer-events-none absolute inset-0 size-full overflow-visible"
+ >
+ <circle
+ cx={marker[0]}
+ cy={marker[1]}
+ r="8"
+ fill="none"
+ stroke="white"
+ />
+ <path
+ d={`M${marker[0] - 12} ${marker[1]}h24M${marker[0]} ${marker[1] - 12}v24`}
+ stroke="white"
+ />
+ </svg>
+ )}
+ </BrushCanvas>
+ </div>
);
}
diff --git a/src/features/layers/gradient-guides.tsx b/src/features/layers/gradient-guides.tsx
index 873c68c..31d626a 100644
--- a/src/features/layers/gradient-guides.tsx
+++ b/src/features/layers/gradient-guides.tsx
@@ -1,5 +1,7 @@
+import { MappedShape } from "@/components/editor/mapped-shape";
+import type { DocumentMapping } from "@/components/editor/mapping";
import { rotateCursor } from "@/components/icons/rotate-cursor";
-import type { Gradient, RadialGradient } from "@/core/document";
+import type { Gradient, LinearGradient, RadialGradient } from "@/core/document";
import type { Point } from "@/core/image/frame";
import { type GradientHandle, radialPoint } from "./gradient";
@@ -80,137 +82,201 @@ function Guide({
);
}
+/**
+ * Lines of equal coverage through the start, end, and middle, across the gradient in the photo; they
+ * stay straight as perspective maps them, though no longer parallel.
+ */
function LinearGuides({
- startPoint,
- endPoint,
+ mask,
+ mapping,
extent,
}: {
- startPoint: Point;
- endPoint: Point;
+ mask: LinearGradient;
+ mapping: DocumentMapping;
extent: number;
}) {
- const dx = endPoint[0] - startPoint[0],
- dy = endPoint[1] - startPoint[1];
- const length = Math.max(1, Math.hypot(dx, dy));
- const direction: Point = [(-dy / length) * extent, (dx / length) * extent];
- const center: Point = [
- (startPoint[0] + endPoint[0]) / 2,
- (startPoint[1] + endPoint[1]) / 2,
+ const along: Point = [
+ mask.end[0] - mask.start[0],
+ mask.end[1] - mask.start[1],
];
+ const across: Point = [-along[1], along[0]];
+ /** A guide where its point shows, with the gradient's direction there for its cursor. */
+ function guide(point: Point) {
+ const at = mapping.toScreen(point);
+ if (!at) return undefined;
+ const [dx, dy] = mapping.direction(point, across);
+ const length = Math.max(1, Math.hypot(dx, dy));
+ const direction: Point = [(dx / length) * extent, (dy / length) * extent];
+ const cursor = resizeCursor([0, 0], mapping.direction(point, along));
+ return { at, direction, cursor };
+ }
+ const start = guide(mask.start);
+ const end = guide(mask.end);
+ const middle = guide([
+ (mask.start[0] + mask.end[0]) / 2,
+ (mask.start[1] + mask.end[1]) / 2,
+ ]);
return (
<g>
<title>Linear gradient guides</title>
- <Guide
- point={startPoint}
- direction={direction}
- handle="start"
- cursor={resizeCursor(startPoint, endPoint)}
- />
- <Guide
- point={endPoint}
- direction={direction}
- handle="end"
- cursor={resizeCursor(startPoint, endPoint)}
- />
- <Guide
- point={center}
- direction={direction}
- handle="rotate"
- cursor={rotationCursor}
- />
- <Handle point={center} handle="move" label="Move gradient" />
+ {start && (
+ <Guide
+ point={start.at}
+ direction={start.direction}
+ handle="start"
+ cursor={start.cursor}
+ />
+ )}
+ {end && (
+ <Guide
+ point={end.at}
+ direction={end.direction}
+ handle="end"
+ cursor={end.cursor}
+ />
+ )}
+ {middle && (
+ <>
+ <Guide
+ point={middle.at}
+ direction={middle.direction}
+ handle="rotate"
+ cursor={rotationCursor}
+ />
+ <Handle point={middle.at} handle="move" label="Move gradient" />
+ </>
+ )}
</g>
);
}
function RadialGuides({
mask,
- screen,
+ mapping,
}: {
mask: RadialGradient;
- screen: (point: Point) => Point;
+ mapping: DocumentMapping;
}) {
- const center = screen(mask.center);
- const right = screen(radialPoint(mask, 1, 0));
- const bottom = screen(radialPoint(mask, 0, 1));
- const feather = screen(radialPoint(mask, 1 - mask.feather, 0));
- const showFeather =
- Math.min(
- Math.hypot(feather[0] - center[0], feather[1] - center[1]),
- Math.hypot(feather[0] - right[0], feather[1] - right[1]),
- ) >= 24;
- const rotationRadius =
- 1 +
- 24 / Math.max(1, Math.hypot(bottom[0] - center[0], bottom[1] - center[1]));
- const transform = `matrix(${right[0] - center[0]},${right[1] - center[1]},${bottom[0] - center[0]},${bottom[1] - center[1]},${center[0]},${center[1]})`;
+ const at = (x: number, y: number) => radialPoint(mask, x, y);
+ const screen = (x: number, y: number) => mapping.toScreen(at(x, y));
+ /** The viewport direction, where a point of the ellipse shows, of its radius through that point. */
+ function radius(x: number, y: number) {
+ const [px, py] = at(x, y);
+ return mapping.direction(at(x, y), [
+ px - mask.center[0],
+ py - mask.center[1],
+ ]);
+ }
+ /** A radius handle's cursor, along its radius as shown there. */
+ const resize = (x: number, y: number) => resizeCursor([0, 0], radius(x, y));
+ const center = mapping.toScreen(mask.center);
+ const right = screen(1, 0);
+ const left = screen(-1, 0);
+ const bottom = screen(0, 1);
+ const top = screen(0, -1);
+ const feather = screen(1 - mask.feather, 0);
+ // About 24 viewport pixels past the top.
+ const rotation = screen(
+ 0,
+ -1 - 24 / Math.max(1, Math.hypot(...radius(0, -1))),
+ );
+ const apart = (point: Point, other?: Point) =>
+ !other || Math.hypot(point[0] - other[0], point[1] - other[1]) >= 24;
+ const edge = mapping.ellipse({
+ center: mask.center,
+ radii: mask.radius,
+ angle: mask.angle,
+ });
+ const inner = mapping.ellipse({
+ center: mask.center,
+ radii: [
+ mask.radius[0] * (1 - mask.feather),
+ mask.radius[1] * (1 - mask.feather),
+ ],
+ angle: mask.angle,
+ });
+ // Each part shows where it maps, so a mask reaching past the perspective's horizon keeps the rest.
return (
<g>
<title>Radial gradient guides</title>
- <g transform={transform} fill="none" stroke="white">
- <circle
- r="1"
- fill="transparent"
- stroke="none"
- data-gradient-handle="move"
- aria-label="Move radial gradient"
- className="pointer-events-auto cursor-grab hover:fill-hover"
+ <g fill="none" stroke="white">
+ {edge && (
+ <>
+ <MappedShape
+ shape={edge}
+ fill="transparent"
+ stroke="none"
+ data-gradient-handle="move"
+ aria-label="Move radial gradient"
+ className="pointer-events-auto cursor-grab hover:fill-hover"
+ />
+ <MappedShape
+ shape={edge}
+ stroke="black"
+ strokeOpacity="0.6"
+ strokeWidth="3"
+ />
+ <MappedShape shape={edge} strokeOpacity="0.9" />
+ </>
+ )}
+ {inner && (
+ <MappedShape
+ shape={inner}
+ strokeDasharray="4 3"
+ strokeOpacity="0.8"
+ />
+ )}
+ {top && rotation && (
+ <line x1={top[0]} y1={top[1]} x2={rotation[0]} y2={rotation[1]} />
+ )}
+ </g>
+ {center && <Handle point={center} handle="move" label="Move gradient" />}
+ {right && (
+ <Handle
+ point={right}
+ handle="radius-x"
+ label="Radial right radius"
+ cursor={resize(1, 0)}
/>
- <circle
- r="1"
- stroke="black"
- strokeOpacity="0.6"
- strokeWidth="3"
- vectorEffect="non-scaling-stroke"
+ )}
+ {left && (
+ <Handle
+ point={left}
+ handle="radius-x"
+ label="Radial left radius"
+ cursor={resize(-1, 0)}
/>
- <circle r="1" strokeOpacity="0.9" vectorEffect="non-scaling-stroke" />
- <circle
- r={1 - mask.feather}
- strokeDasharray="4 3"
- strokeOpacity="0.8"
- vectorEffect="non-scaling-stroke"
+ )}
+ {bottom && (
+ <Handle
+ point={bottom}
+ handle="radius-y"
+ label="Radial bottom radius"
+ cursor={resize(0, 1)}
/>
- <path
- d={`M0 -1V${-rotationRadius}`}
- vectorEffect="non-scaling-stroke"
+ )}
+ {top && (
+ <Handle
+ point={top}
+ handle="radius-y"
+ label="Radial top radius"
+ cursor={resize(0, -1)}
/>
- </g>
- <Handle point={center} handle="move" label="Move gradient" />
- <Handle
- point={right}
- handle="radius-x"
- label="Radial right radius"
- cursor={resizeCursor(center, right)}
- />
- <Handle
- point={screen(radialPoint(mask, -1, 0))}
- handle="radius-x"
- label="Radial left radius"
- cursor={resizeCursor(center, right)}
- />
- <Handle
- point={bottom}
- handle="radius-y"
- label="Radial bottom radius"
- cursor={resizeCursor(center, bottom)}
- />
- <Handle
- point={screen(radialPoint(mask, 0, -1))}
- handle="radius-y"
- label="Radial top radius"
- cursor={resizeCursor(center, bottom)}
- />
- <Handle
- point={screen(radialPoint(mask, 0, -rotationRadius))}
- handle="rotate"
- label="Rotate radial gradient"
- cursor={rotationCursor}
- />
- {showFeather && (
+ )}
+ {rotation && (
+ <Handle
+ point={rotation}
+ handle="rotate"
+ label="Rotate radial gradient"
+ cursor={rotationCursor}
+ />
+ )}
+ {feather && apart(feather, center) && apart(feather, right) && (
<Handle
point={feather}
handle="feather"
label="Radial feather"
- cursor={resizeCursor(center, right)}
+ cursor={resize(1, 0)}
/>
)}
</g>
@@ -219,24 +285,20 @@ function RadialGuides({
export function GradientGuides({
mask,
- screen,
+ mapping,
extent,
}: {
mask: Gradient;
- screen: (point: Point) => Point;
+ mapping: DocumentMapping;
extent: number;
}) {
return (
<svg className="pointer-events-none absolute inset-0 size-full overflow-visible">
<title>Gradient guides</title>
{mask.kind === "linear" && (
- <LinearGuides
- startPoint={screen(mask.start)}
- endPoint={screen(mask.end)}
- extent={extent}
- />
+ <LinearGuides mask={mask} mapping={mapping} extent={extent} />
)}
- {mask.kind === "radial" && <RadialGuides mask={mask} screen={screen} />}
+ {mask.kind === "radial" && <RadialGuides mask={mask} mapping={mapping} />}
</svg>
);
}
diff --git a/src/features/layers/gradient-overlay.tsx b/src/features/layers/gradient-overlay.tsx
index 3b4c071..6c64492 100644
--- a/src/features/layers/gradient-overlay.tsx
+++ b/src/features/layers/gradient-overlay.tsx
@@ -77,12 +77,22 @@ export function GradientOverlay({ shape }: { shape: Gradient["kind"] }) {
}
},
});
- function documentPoint(event: PointerEvent, box: DOMRect): Point {
- return mapping.toDocument(event.clientX, event.clientY, box);
+ function documentPoint(event: PointerEvent, box: DOMRect) {
+ return mapping.toDocument([
+ event.clientX - box.left,
+ event.clientY - box.top,
+ ]);
}
function start(event: PointerEvent<HTMLDivElement>) {
const box = camera.ref.current?.getBoundingClientRect();
- if (event.button !== 0 || !event.isPrimary || camera.panMode || !box) {
+ const from = box && documentPoint(event, box);
+ if (
+ event.button !== 0 ||
+ !event.isPrimary ||
+ camera.panMode ||
+ !box ||
+ !from
+ ) {
return;
}
const target =
@@ -94,7 +104,6 @@ export function GradientOverlay({ shape }: { shape: Gradient["kind"] }) {
const handle = gradientHandles.find((handle) => handle === target);
// Guides win over drawing, unless a chosen nesting is waiting for this shape.
const drawing = !handle || !mask || tool.pending?.shape === shape;
- const from = documentPoint(event, box);
if (drawing) {
const location = locateLayer(document.scene.getState().layers, selected);
const group =
@@ -144,6 +153,10 @@ export function GradientOverlay({ shape }: { shape: Gradient["kind"] }) {
return;
}
const point = documentPoint(event, drag.box);
+ // Past where input stops the drag keeps its last shape until the pointer returns.
+ if (!point) {
+ return;
+ }
drag.to = point;
if (drag.handle === "new") {
const next = drawGradient(
@@ -170,11 +183,9 @@ export function GradientOverlay({ shape }: { shape: Gradient["kind"] }) {
}
// Commit the last preview; pointerup must not reinterpret geometry or Shift.
if (drag.handle === "new") {
- const point = drag.to;
- const distance =
- Math.hypot(point[0] - drag.from[0], point[1] - drag.from[1]) *
- camera.scale;
- if (distance >= 3) {
+ const from = mapping.toScreen(drag.from);
+ const to = mapping.toScreen(drag.to);
+ if (from && to && Math.hypot(to[0] - from[0], to[1] - from[1]) >= 3) {
tool.create(drag.mask, drag.nesting);
}
} else {
@@ -208,7 +219,7 @@ export function GradientOverlay({ shape }: { shape: Gradient["kind"] }) {
{visible && !camera.panMode && (
<GradientGuides
mask={visible}
- screen={mapping.toScreen}
+ mapping={mapping}
extent={Math.hypot(...camera.viewport)}
/>
)}
diff --git a/src/features/layers/range-picker.tsx b/src/features/layers/range-picker.tsx
index d380a08..7c144b5 100644
--- a/src/features/layers/range-picker.tsx
+++ b/src/features/layers/range-picker.tsx
@@ -81,11 +81,14 @@ export function RangePicker() {
}
/** Picks the color under the pointer and waits for the previous pick to land. */
function pick(current: Drag, event: PointerEvent) {
- const [x, y] = mapping.toDocument(
- event.clientX,
- event.clientY,
- current.box,
- );
+ const at = mapping.toDocument([
+ event.clientX - current.box.left,
+ event.clientY - current.box.top,
+ ]);
+ if (!at) {
+ return;
+ }
+ const [x, y] = at;
const image = () => renderer.rangeSource(group);
const color = sampler.sample(image, [x / size[0], y / size[1]]);
current.landed = Promise.all([current.landed, color])
diff --git a/src/lib/projective.ts b/src/lib/projective.ts
new file mode 100644
index 0000000..0ac2b3a
--- /dev/null
+++ b/src/lib/projective.ts
@@ -0,0 +1,236 @@
+/** Plane projective geometry on homogeneous coordinates. */
+
+export type Point = readonly [number, number];
+/** A 3×3 matrix in row-major order, acting on homogeneous column vectors. */
+export type Homography = readonly [
+ number,
+ number,
+ number,
+ number,
+ number,
+ number,
+ number,
+ number,
+ number,
+];
+/** An ellipse with its radii along its own axes, turned by `angle` degrees. */
+export type Ellipse = { center: Point; radii: Point; angle: number };
+
+export function multiply(a: Homography, b: Homography): Homography {
+ const at = (row: number, column: number) =>
+ a[row * 3] * b[column] +
+ a[row * 3 + 1] * b[3 + column] +
+ a[row * 3 + 2] * b[6 + column];
+ return [
+ at(0, 0),
+ at(0, 1),
+ at(0, 2),
+ at(1, 0),
+ at(1, 1),
+ at(1, 2),
+ at(2, 0),
+ at(2, 1),
+ at(2, 2),
+ ];
+}
+
+/** The homogeneous image of a point: x and y, and the weight that divides them. */
+export function apply(
+ matrix: Homography,
+ [x, y]: Point,
+): [number, number, number] {
+ return [
+ matrix[0] * x + matrix[1] * y + matrix[2],
+ matrix[3] * x + matrix[4] * y + matrix[5],
+ matrix[6] * x + matrix[7] * y + matrix[8],
+ ];
+}
+
+/** The image of a point in front of the horizon, where the weight is positive. */
+export function project(matrix: Homography, point: Point): Point | undefined {
+ const [x, y, w] = apply(matrix, point);
+ return w > 0 ? [x / w, y / w] : undefined;
+}
+
+/** The map's derivative at a point, by rows: how each image coordinate changes along x and y. */
+export function jacobian(matrix: Homography, point: Point): [Point, Point] {
+ const [x, y, w] = apply(matrix, point);
+ const row = (i: number, at: number): Point => [
+ (matrix[i * 3] - at * matrix[6]) / w,
+ (matrix[i * 3 + 1] - at * matrix[7]) / w,
+ ];
+ return [row(0, x / w), row(1, y / w)];
+}
+
+/** The most a linear map lengthens any direction: its largest singular value. */
+export function stretch([[a, b], [c, d]]: readonly [Point, Point]) {
+ const sum = a * a + b * b + c * c + d * d;
+ const determinant = a * d - b * c;
+ return Math.sqrt(
+ (sum + Math.sqrt(Math.max(0, sum * sum - 4 * determinant ** 2))) / 2,
+ );
+}
+
+function ellipsePoint({ center, radii, angle }: Ellipse, t: number): Point {
+ const turn = (angle * Math.PI) / 180;
+ const x = radii[0] * Math.cos(t);
+ const y = radii[1] * Math.sin(t);
+ return [
+ center[0] + x * Math.cos(turn) - y * Math.sin(turn),
+ center[1] + x * Math.sin(turn) + y * Math.cos(turn),
+ ];
+}
+
+/**
+ * The image of an ellipse, exactly, given `inverse`, the map back to the ellipse's plane: a conic
+ * C maps to inverseᵀ C inverse. Undefined unless the image is an ellipse in front of the horizon;
+ * one that reaches the horizon is unbounded.
+ */
+export function projectEllipse(
+ inverse: Homography,
+ { center, radii, angle }: Ellipse,
+): Ellipse | undefined {
+ const turn = (angle * Math.PI) / 180;
+ const w = inverse.slice(6, 9);
+ const dx = inverse.slice(0, 3).map((value, i) => value - center[0] * w[i]);
+ const dy = inverse.slice(3, 6).map((value, i) => value - center[1] * w[i]);
+ // Rows giving a point's coordinates along the ellipse's axes, over its radii.
+ const u = dx.map(
+ (x, i) => (x * Math.cos(turn) + dy[i] * Math.sin(turn)) / radii[0],
+ );
+ const v = dx.map(
+ (x, i) => (dy[i] * Math.cos(turn) - x * Math.sin(turn)) / radii[1],
+ );
+ const conic = (i: number, j: number) =>
+ u[i] * u[j] + v[i] * v[j] - w[i] * w[j];
+ const [a, b, c] = [conic(0, 0), conic(0, 1), conic(1, 1)];
+ const [d, e, f] = [conic(0, 2), conic(1, 2), conic(2, 2)];
+ const determinant = a * c - b * b;
+ if (!(a > 0 && determinant > 0)) {
+ return undefined;
+ }
+ const cx = (b * e - c * d) / determinant;
+ const cy = (b * d - a * e) / determinant;
+ const level = -(d * cx + e * cy + f);
+ // An ellipse of points behind the horizon is the image of nothing in front of it.
+ if (!(level > 0 && w[0] * cx + w[1] * cy + w[2] > 0)) {
+ return undefined;
+ }
+ const mean = (a + c) / 2;
+ const spread = Math.hypot((a - c) / 2, b);
+ return {
+ center: [cx, cy],
+ radii: [
+ Math.sqrt(level / (mean + spread)),
+ Math.sqrt(level / (mean - spread)),
+ ],
+ angle: (Math.atan2(2 * b, a - c) * 90) / Math.PI,
+ };
+}
+
+/**
+ * Points along a curve's image, within `tolerance` of it: each piece halves until its middle lies
+ * that close to its chord. `point` must be finite over [from, to].
+ */
+function trace(
+ point: (t: number) => Point,
+ from: number,
+ to: number,
+ tolerance: number,
+) {
+ const points = [point(from)];
+ function divide(t0: number, p0: Point, t1: number, p1: Point, depth: number) {
+ const t = (t0 + t1) / 2;
+ const middle = point(t);
+ const chord = Math.hypot(p1[0] - p0[0], p1[1] - p0[1]);
+ const away = chord
+ ? Math.abs(
+ (p1[0] - p0[0]) * (middle[1] - p0[1]) -
+ (p1[1] - p0[1]) * (middle[0] - p0[0]),
+ ) / chord
+ : Math.hypot(middle[0] - p0[0], middle[1] - p0[1]);
+ if (depth < 12 && away > tolerance) {
+ divide(t0, p0, t, middle, depth + 1);
+ divide(t, middle, t1, p1, depth + 1);
+ return;
+ }
+ points.push(p1);
+ }
+ const pieces = 32;
+ for (let i = 0; i < pieces; i++) {
+ const t0 = from + ((to - from) * i) / pieces;
+ const t1 = from + ((to - from) * (i + 1)) / pieces;
+ divide(t0, points[points.length - 1], t1, point(t1), 0);
+ }
+ return points;
+}
+
+function pathData(points: readonly Point[]) {
+ return `${points.map(([x, y], i) => `${i ? "L" : "M"}${x} ${y}`).join("")}Z`;
+}
+
+/**
+ * The part of an ellipse's image where `matrix`'s weight stays at least `floor`, as closed SVG path
+ * data within `tolerance` of the curve: a straight edge closes it where the weight reaches `floor`.
+ * Empty when nothing is left.
+ */
+export function ellipsePath(
+ matrix: Homography,
+ ellipse: Ellipse,
+ floor: number,
+ tolerance = 0.25,
+) {
+ // The weight along the ellipse is level + depth·cos(t − phase).
+ const [x0, y0] = ellipsePoint(ellipse, 0);
+ const [x1, y1] = ellipsePoint(ellipse, Math.PI / 2);
+ const weight = ([x, y]: Point) => matrix[6] * x + matrix[7] * y + matrix[8];
+ const level = weight(ellipse.center);
+ const cosine = weight([x0, y0]) - level;
+ const sine = weight([x1, y1]) - level;
+ const depth = Math.hypot(cosine, sine);
+ if (level + depth < floor) {
+ return "";
+ }
+ const phase = Math.atan2(sine, cosine);
+ const reach =
+ level - depth >= floor ? Math.PI : Math.acos((floor - level) / depth);
+ const point = (t: number) => {
+ const [x, y, w] = apply(matrix, ellipsePoint(ellipse, t));
+ return [x / w, y / w] as const;
+ };
+ return pathData(trace(point, phase - reach, phase + reach, tolerance));
+}
+
+/**
+ * A polygon's image where `matrix`'s weight stays at least `floor`, as closed SVG path data; its
+ * edges stay straight. Empty when nothing is left.
+ */
+export function polygonPath(
+ matrix: Homography,
+ polygon: readonly Point[],
+ floor: number,
+) {
+ const weight = ([x, y]: Point) => matrix[6] * x + matrix[7] * y + matrix[8];
+ const kept: Point[] = [];
+ for (const [i, point] of polygon.entries()) {
+ const next = polygon[(i + 1) % polygon.length];
+ const [a, b] = [weight(point) - floor, weight(next) - floor];
+ if (a >= 0) kept.push(point);
+ if (a >= 0 !== b >= 0) {
+ const t = a / (a - b);
+ kept.push([
+ point[0] + (next[0] - point[0]) * t,
+ point[1] + (next[1] - point[1]) * t,
+ ]);
+ }
+ }
+ if (kept.length < 3) {
+ return "";
+ }
+ return pathData(
+ kept.map((point) => {
+ const [x, y, w] = apply(matrix, point);
+ return [x / w, y / w] as const;
+ }),
+ );
+}
diff --git a/tests/brush-navigation.e2e.ts b/tests/brush-navigation.e2e.ts
index 3445e78..e591853 100644
--- a/tests/brush-navigation.e2e.ts
+++ b/tests/brush-navigation.e2e.ts
@@ -94,7 +94,15 @@ for (const { name, shortcut, presses, label } of [
await size.fill("100");
await size.press("Enter");
const bounds = await box(canvas);
- const cursor = canvas.locator('[data-brush-cursor="true"] circle').first();
+ const cursor = canvas.locator('[data-brush-cursor="true"] ellipse').first();
+ /** The cursor's radii in viewport pixels, a circle without perspective. */
+ const radii = () =>
+ Promise.all(
+ ["rx", "ry"].map(async (name) =>
+ Number(await cursor.getAttribute(name)),
+ ),
+ );
+ const fifty = [expect.closeTo(50, 6), expect.closeTo(50, 6)];
const strokes = () =>
page.evaluate(() => {
const { scene, selectedLayerId } = window.openlight.getState();
@@ -111,14 +119,14 @@ for (const { name, shortcut, presses, label } of [
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 expect.poll(radii).toEqual(fifty);
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.poll(radii).toEqual(fifty);
await expect(size).toHaveValue("100");
await page.mouse.click(x + 180, y);
await expect.poll(async () => (await strokes()).length).toBe(2);
@@ -132,7 +140,7 @@ for (const { name, shortcut, presses, label } of [
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.poll(radii).toEqual(fifty);
await expect(size).toHaveValue("100");
});
}
diff --git a/tests/brush.e2e.ts b/tests/brush.e2e.ts
index 7ede517..8e29061 100644
--- a/tests/brush.e2e.ts
+++ b/tests/brush.e2e.ts
@@ -197,4 +197,69 @@ test.describe("on a touch phone", () => {
history: { editing: false },
});
});
+
+ test("a second finger landing on another patch during a healing stroke pinches instead of selecting it", async ({
+ page,
+ context,
+ }) => {
+ await page.goto("/");
+ await page
+ .locator('input[type="file"]')
+ .setInputFiles("tests/fixtures/photo.svg");
+ await expect(
+ page.getByRole("slider", { name: "Exposure", exact: true }),
+ ).toHaveAttribute("aria-valuetext", "0.00");
+ // A patch right of the photo's center, where the second finger lands.
+ await page.evaluate(() => {
+ const api = window.openlight;
+ api.addHealPatch(
+ api.addLayer("heal"),
+ {
+ mode: "paint",
+ size: 160,
+ feather: 0,
+ flow: 1,
+ points: [[900, 400, 1]],
+ },
+ [-300, 0],
+ );
+ });
+ const before = await page.evaluate(() => window.openlight.getState());
+ await page.getByRole("tab", { name: "Healing", exact: true }).click();
+ const canvas = page.getByLabel("Healing canvas", { exact: true });
+ const bounds = await box(canvas);
+ // The fitted photo's pixels per source pixel, inside the viewport's padding.
+ const scale = Math.min(
+ (bounds.width - 48) / 1200,
+ (bounds.height - 48) / 800,
+ );
+ const zoom = page.getByRole("button", { name: /^\d+%$/ });
+ const fitted = await zoom.innerText();
+ const [x, y] = [bounds.x + bounds.width / 2, bounds.y + bounds.height / 2];
+ const patch = 300 * scale;
+ const touch = await context.newCDPSession(page);
+ const send = (
+ type: "touchStart" | "touchMove" | "touchEnd",
+ at: number[],
+ ) =>
+ touch.send("Input.dispatchTouchEvent", {
+ type,
+ touchPoints: at.map((dx) => ({ x: x + dx, y })),
+ });
+ // The first finger paints left of the patch; the second lands on it, then both spread.
+ await send("touchStart", [-patch]);
+ await send("touchMove", [-patch + 10]);
+ await send("touchStart", [-patch + 10, patch]);
+ for (let step = 1; step <= 6; step++) {
+ await send("touchMove", [-patch + 10 - step * 8, patch + step * 8]);
+ }
+ await send("touchEnd", []);
+ await expect(zoom).not.toHaveText(fitted);
+ const after = await page.evaluate(() => window.openlight.getState());
+ expect(after.scene).toEqual(before.scene);
+ expect(after.history).toEqual(before.history);
+ await expect(
+ canvas.locator('[data-heal-destination-handle="true"]'),
+ ).toHaveCount(0);
+ });
});
diff --git a/tests/crop.e2e.ts b/tests/crop.e2e.ts
index 9774530..71bbaeb 100644
--- a/tests/crop.e2e.ts
+++ b/tests/crop.e2e.ts
@@ -1,7 +1,174 @@
+import type { Page } from "@playwright/test";
import { expect, openPhoto, test } from "./fixtures";
import { readImage } from "./images";
import { box, choose, drag, zoom } from "./pointer";
+/** Where API.md puts the point Vertical −50 sends to infinity: 1 / (0.0045 · 50) half-heights above the center. */
+const vanishing = 400 - 400 / (0.0045 * 50);
+const dot = [698, 520] as const;
+/** Black lines that meet above the photo, as a building's edges do when the camera tilts up, and a blue dot between two. */
+const keystone = Buffer.from(
+ `<svg xmlns="http://www.w3.org/2000/svg" width="1200" height="800">
+ <rect width="1200" height="800" fill="white"/>
+ ${[150, 375, 600, 825, 1050]
+ .map(
+ (x) =>
+ `<line x1="${x}" y1="800" x2="600" y2="${vanishing}" stroke="black" stroke-width="12"/>`,
+ )
+ .join("")}
+ <circle cx="${dot[0]}" cy="${dot[1]}" r="24" fill="blue"/>
+ </svg>`,
+);
+
+async function openKeystone(page: Page) {
+ await page.goto("/");
+ await page.locator('input[type="file"]').setInputFiles({
+ name: "keystone.svg",
+ mimeType: "image/svg+xml",
+ buffer: keystone,
+ });
+ await expect(
+ page.getByRole("textbox", { name: "Exposure", exact: true }),
+ ).toHaveValue("0.00");
+}
+
+/** Sets Vertical or Horizontal in the Crop panel and applies it. */
+async function correct(
+ page: Page,
+ name: "Vertical" | "Horizontal",
+ value: string,
+) {
+ const crop = page.getByRole("group", { name: "Crop" });
+ const field = crop.getByRole("textbox", { name, exact: true });
+ await page.keyboard.press("c");
+ await field.fill(value);
+ await field.press("Tab");
+ await expect(field).toHaveValue(value);
+ await crop.getByRole("button", { name: "Apply crop" }).click();
+ await expect(crop).toBeHidden();
+}
+
+/** Centers of the dark lines across exported rows, or down columns with `axis` 1, at fractions of the image. */
+async function lines(page: Page, fractions: readonly number[], axis = 0) {
+ return page.evaluate(
+ async ([fractions, axis]) => {
+ const image = await createImageBitmap(
+ await window.openlight.exportImage(),
+ );
+ const canvas = new OffscreenCanvas(image.width, image.height);
+ const context = canvas.getContext("2d");
+ if (!context) throw new Error("Cannot read image pixels.");
+ context.drawImage(image, 0, 0);
+ const length = axis ? image.height : image.width;
+ return fractions.map((fraction) => {
+ const at = Math.round(fraction * (axis ? image.width : image.height));
+ const { data } = axis
+ ? context.getImageData(at, 0, 1, length)
+ : context.getImageData(0, at, length, 1);
+ const centers: number[] = [];
+ let start = -1;
+ for (let i = 0; i <= length; i++) {
+ const dark = i < length && data[i * 4] < 128 && data[i * 4 + 2] < 128;
+ if (dark && start < 0) start = i;
+ if (!dark && start >= 0) {
+ centers.push((start + i - 1) / 2);
+ start = -1;
+ }
+ }
+ return centers;
+ });
+ },
+ [fractions, axis] as const,
+ );
+}
+
+function expectParallel([first, second]: number[][]) {
+ expect(first.length).toBeGreaterThanOrEqual(3);
+ expect(second).toHaveLength(first.length);
+ for (const [i, center] of first.entries()) {
+ expect(Math.abs(center - second[i])).toBeLessThan(1.5);
+ }
+}
+
+/** The blue dot's center on the page, as the canvas shows it. */
+async function dotOnPage(page: Page) {
+ const canvas = page
+ .getByRole("region", { name: "Image canvas" })
+ .locator("canvas");
+ const origin = await box(canvas);
+ const bytes = await canvas.screenshot();
+ const [x, y] = await page.evaluate(
+ async (bytes) => {
+ const image = await createImageBitmap(new Blob([new Uint8Array(bytes)]));
+ const canvas = new OffscreenCanvas(image.width, image.height);
+ const context = canvas.getContext("2d");
+ if (!context) throw new Error("Cannot read preview pixels.");
+ context.drawImage(image, 0, 0);
+ const { data } = context.getImageData(0, 0, image.width, image.height);
+ let [sx, sy, count] = [0, 0, 0];
+ for (let i = 0; i < data.length; i += 4) {
+ if (data[i + 2] > 200 && data[i] < 80 && data[i + 1] < 80) {
+ sx += ((i / 4) % image.width) + 0.5;
+ sy += Math.floor(i / 4 / image.width) + 0.5;
+ count++;
+ }
+ }
+ return [sx / count, sy / count];
+ },
+ [...bytes],
+ );
+ return [origin.x + x, origin.y + y] as const;
+}
+
+/** In the export, the blue dot's center and that of the dark paint within the dot's bounds. */
+async function paintOnDot(page: Page) {
+ return page.evaluate(async () => {
+ const image = await createImageBitmap(await window.openlight.exportImage());
+ const canvas = new OffscreenCanvas(image.width, image.height);
+ const context = canvas.getContext("2d");
+ if (!context) throw new Error("Cannot read image pixels.");
+ context.drawImage(image, 0, 0);
+ const { data, width, height } = context.getImageData(
+ 0,
+ 0,
+ image.width,
+ image.height,
+ );
+ function centroid(
+ matches: (i: number) => boolean,
+ [left, top, right, bottom]: readonly number[] = [0, 0, width, height],
+ ) {
+ let [sx, sy, count] = [0, 0, 0];
+ const bounds = [width, height, 0, 0];
+ for (let y = top; y < bottom; y++) {
+ for (let x = left; x < right; x++) {
+ if (!matches((y * width + x) * 4)) continue;
+ sx += x + 0.5;
+ sy += y + 0.5;
+ count++;
+ bounds[0] = Math.min(bounds[0], x);
+ bounds[1] = Math.min(bounds[1], y);
+ bounds[2] = Math.max(bounds[2], x + 1);
+ bounds[3] = Math.max(bounds[3], y + 1);
+ }
+ }
+ return { center: [sx / count, sy / count], bounds };
+ }
+ const blue = centroid(
+ (i) => data[i + 2] > 200 && data[i] < 80 && data[i + 1] < 80,
+ );
+ const paint = centroid(
+ (i) => data[i] < 80 && data[i + 1] < 80 && data[i + 2] < 80,
+ blue.bounds,
+ );
+ return { dot: blue.center, paint: paint.center };
+ });
+}
+
+function distance(a: readonly number[], b: readonly number[]) {
+ return Math.hypot(a[0] - b[0], a[1] - b[1]);
+}
+
test("crop, rotate, flip and straighten the photo, then undo", async ({
page,
}) => {
@@ -12,7 +179,9 @@ test("crop, rotate, flip and straighten the photo, then undo", async ({
const panel = page.getByRole("region", { name: "Crop tool" });
const selection = page.getByRole("application", { name: "Crop selection" });
const apply = page.getByRole("button", { name: "Apply crop" });
- const reset = panel.getByRole("button", { name: "Reset", exact: true });
+ const reset = page
+ .getByRole("group", { name: "Crop" })
+ .getByRole("button", { name: "Reset", exact: true });
const aspect = panel.getByRole("combobox", { name: "Aspect ratio" });
await test.step("Escape discards a draft", async () => {
@@ -86,3 +255,245 @@ test("crop, rotate, flip and straighten the photo, then undo", async ({
expect((await state()).frame).toEqual(initial.frame);
});
});
+
+/** The stored draft's frame, read straight from IndexedDB. */
+function savedFrame(page: Page) {
+ return page.evaluate(
+ () =>
+ new Promise<unknown>((resolve, reject) => {
+ const opening = indexedDB.open("openlight", 1);
+ opening.onerror = () => reject(opening.error);
+ opening.onsuccess = () => {
+ const database = opening.result;
+ const request = database
+ .transaction("draft")
+ .objectStore("draft")
+ .get("latest");
+ request.onsuccess = () => {
+ database.close();
+ resolve(request.result?.scene.scene.frame);
+ };
+ };
+ }),
+ );
+}
+
+test("perspective makes converging lines parallel and stays with the photo through undo, turns, flips, scene files, and drafts", async ({
+ page,
+}) => {
+ test.setTimeout(120_000);
+ const state = () => page.evaluate(() => window.openlight.getState());
+ await openKeystone(page);
+ const initial = await state();
+ const crop = page.getByRole("group", { name: "Crop" });
+ const vertical = crop.getByRole("textbox", { name: "Vertical", exact: true });
+ const horizontal = crop.getByRole("textbox", {
+ name: "Horizontal",
+ exact: true,
+ });
+
+ await test.step("the lines converge toward the top", async () => {
+ const [top, bottom] = await lines(page, [0.25, 0.75]);
+ expect(top).toHaveLength(5);
+ expect(bottom).toHaveLength(5);
+ expect(top[0] - bottom[0]).toBeGreaterThan(30);
+ });
+
+ await test.step("Escape discards a correction without an edit", async () => {
+ await page.keyboard.press("c");
+ await vertical.fill("-50");
+ await vertical.press("Tab");
+ await page.keyboard.press("Escape");
+ await expect(crop).toBeHidden();
+ expect(await state()).toEqual(initial);
+ });
+
+ await test.step("Enter commits a typed Vertical and keeps Crop open; Enter again applies it", async () => {
+ await page.keyboard.press("c");
+ await vertical.fill("-50");
+ await vertical.press("Enter");
+ await expect(vertical).toHaveValue("-50");
+ await expect(crop).toBeVisible();
+ expect((await state()).frame).toEqual(initial.frame);
+ await page.keyboard.press("Enter");
+ await expect(crop).toBeHidden();
+ });
+
+ await test.step("Vertical −50 makes the lines parallel as one edit, at the photo's size", async () => {
+ const { frame, history } = await state();
+ expect(frame?.perspective).toEqual([0, -50]);
+ expect(history.undoCount).toBe(1);
+ expect((await readImage(page)).size).toEqual([1200, 800]);
+ expectParallel(await lines(page, [0.25, 0.75]));
+ });
+
+ await test.step("undo restores the photo's frame, and redo the correction", async () => {
+ await page.keyboard.press("ControlOrMeta+z");
+ expect((await state()).frame).toEqual(initial.frame);
+ await page.keyboard.press("ControlOrMeta+Shift+z");
+ expect((await state()).frame?.perspective).toEqual([0, -50]);
+ });
+
+ await test.step("a quarter turn and a flip carry the correction, shown along the displayed axes", async () => {
+ await page.keyboard.press("c");
+ await expect(vertical).toHaveValue("-50");
+ await crop.getByRole("button", { name: "Rotate clockwise" }).click();
+ // The photo's top, which the correction enlarges, now shows on the right, then on the left.
+ await expect(vertical).toHaveValue("0");
+ await expect(horizontal).toHaveValue("50");
+ await crop.getByRole("button", { name: "Flip horizontal" }).click();
+ await expect(horizontal).toHaveValue("-50");
+ await crop.getByRole("button", { name: "Apply crop" }).click();
+ expect((await state()).frame?.perspective).toEqual([0, -50]);
+ expect((await readImage(page)).size).toEqual([800, 1200]);
+ expectParallel(await lines(page, [0.25, 0.75], 1));
+ });
+
+ await test.step("a scene file and the draft reopen with the correction", async () => {
+ const corrected = await state();
+ const shown = await lines(page, [0.25, 0.75], 1);
+ await page.evaluate(async () => {
+ const api = window.openlight;
+ await api.loadScene(await api.exportScene());
+ });
+ const reopened = await state();
+ expect(reopened.documentId).not.toBe(corrected.documentId);
+ expect(reopened.frame).toEqual(corrected.frame);
+ expect(await lines(page, [0.25, 0.75], 1)).toEqual(shown);
+ await expect.poll(() => savedFrame(page)).toEqual(corrected.frame);
+ await page.reload();
+ await page.getByRole("button", { name: "Recover", exact: true }).click();
+ await expect
+ .poll(async () => (await state()).frame)
+ .toEqual(corrected.frame);
+ });
+});
+
+test("brush, retouch, and color picks land where the corrected photo shows them, and paint stays there", async ({
+ page,
+}) => {
+ test.setTimeout(120_000);
+ const state = () => page.evaluate(() => window.openlight.getState());
+ const layers = async () => (await state()).scene?.layers ?? [];
+ await openKeystone(page);
+ await correct(page, "Vertical", "-50");
+
+ await test.step("a color range picks the dot's blue", async () => {
+ const at = await dotOnPage(page);
+ await page.getByRole("button", { name: "Add effect" }).click();
+ await page.getByRole("menuitem", { name: "Color Range" }).click();
+ await page.mouse.click(at[0], at[1]);
+ const color = page.getByLabel("Range color");
+ await expect(color).toHaveValue(/^#[0-9a-f]{6}$/);
+ const hex = await color.inputValue();
+ const [red, green, blue] = [1, 3, 5].map((i) =>
+ Number.parseInt(hex.slice(i, i + 2), 16),
+ );
+ expect(Math.max(red, green)).toBeLessThan(8);
+ expect(blue).toBeGreaterThan(247);
+ // Leaving the picker, undo takes back the pick and the mask.
+ await page.keyboard.press("Escape");
+ await page.keyboard.press("ControlOrMeta+z");
+ await page.keyboard.press("ControlOrMeta+z");
+ expect(await layers()).toHaveLength(1);
+ });
+
+ await test.step("a brush dab on the dot paints it, and stays on it as the correction changes", async () => {
+ const at = await dotOnPage(page);
+ await page.keyboard.press("b");
+ const options = page.getByRole("group", { name: "Layer options" });
+ await options.getByRole("button", { name: "Color", exact: true }).click();
+ const size = options.getByRole("textbox", { name: "Size", exact: true });
+ await size.fill("8");
+ await size.press("Enter");
+ await page.mouse.click(at[0], at[1]);
+ await expect.poll(async () => (await layers()).at(-1)?.kind).toBe("paint");
+ const paint = (await layers()).at(-1);
+ if (paint?.kind !== "paint") throw Error("Missing paint layer.");
+ expect(paint.strokes).toHaveLength(1);
+ expect(distance(paint.strokes[0].points[0], dot)).toBeLessThan(2);
+ const painted = await paintOnDot(page);
+ expect(distance(painted.paint, painted.dot)).toBeLessThan(1.5);
+ await correct(page, "Vertical", "-20");
+ expect((await state()).frame?.perspective).toEqual([0, -20]);
+ const kept = await paintOnDot(page);
+ expect(distance(kept.paint, kept.dot)).toBeLessThan(1.5);
+ });
+
+ await test.step("a Remove patch starts on the dot, its contour's anchor under the click", async () => {
+ const at = await dotOnPage(page);
+ await page.keyboard.press("h");
+ const canvas = page.getByLabel("Healing canvas", { exact: true });
+ await expect(canvas).toBeVisible();
+ const size = page.getByRole("textbox", { name: "Size", exact: true });
+ await size.fill("30");
+ await size.press("Enter");
+ await page.mouse.click(at[0], at[1]);
+ const patches = async () => {
+ const heal = (await layers()).find((layer) => layer.kind === "heal");
+ return heal?.kind === "heal" ? heal.patches : [];
+ };
+ await expect.poll(async () => (await patches()).length).toBe(1);
+ const [patch] = await patches();
+ expect(distance(patch.strokes[0].points[0], dot)).toBeLessThan(2);
+ const handle = canvas.locator('[data-heal-destination-handle="true"]');
+ const origin = await box(canvas);
+ const [cx, cy] = await Promise.all(
+ ["cx", "cy"].map(async (name) => Number(await handle.getAttribute(name))),
+ );
+ expect(distance([origin.x + cx, origin.y + cy], at)).toBeLessThan(1);
+ });
+});
+
+test("gradient guides reaching past the correction's horizon keep the parts the photo shows", async ({
+ page,
+}) => {
+ await openPhoto(page);
+ // The strongest Horizontal puts the horizon 1933 source pixels from the photo's left edge.
+ await correct(page, "Horizontal", "100");
+ const guide = (label: string) => page.getByLabel(label, { exact: true });
+ const mask = await page.evaluate(() => {
+ const api = window.openlight;
+ const id = api.addLayer("mask");
+ api.setLayerMask(id, {
+ kind: "radial",
+ center: [1000, 400],
+ radius: [1000, 200],
+ angle: 0,
+ feather: 0.5,
+ });
+ return id;
+ });
+ await page.keyboard.press("r");
+ // The right edge lies past the horizon; the contour, the other handles, and rotation remain.
+ await expect(guide("Move radial gradient")).toHaveCount(1);
+ for (const label of [
+ "Move gradient",
+ "Radial left radius",
+ "Radial top radius",
+ "Radial bottom radius",
+ "Rotate radial gradient",
+ ]) {
+ await expect(guide(label)).toHaveCount(1);
+ }
+ await expect(guide("Radial right radius")).toHaveCount(0);
+
+ await page.evaluate(
+ (id) =>
+ window.openlight.setLayerMask(id, {
+ kind: "linear",
+ start: [600, 400],
+ end: [2400, 400],
+ }),
+ mask,
+ );
+ // The end lies past the horizon; the start and middle guides remain.
+ for (const label of [
+ "Gradient start guide",
+ "Gradient rotate guide",
+ "Move gradient",
+ ]) {
+ await expect(guide(label)).toHaveCount(1);
+ }
+ await expect(guide("Gradient end guide")).toHaveCount(0);
+});
diff --git a/tests/crop.test.ts b/tests/crop.test.ts
index 22b7e32..2c4bc0b 100644
--- a/tests/crop.test.ts
+++ b/tests/crop.test.ts
@@ -7,11 +7,14 @@ import {
validateFrame,
} from "@/core/image/frame";
import {
+ correct,
+ correctShown,
fitRatio,
flip,
move,
resize,
rotate,
+ shownPerspective,
turn,
} from "@/features/crop/geometry";
@@ -19,7 +22,11 @@ const source: Point = [1200, 800];
const corners = [0, 1].flatMap((x) => [0, 1].map((y) => [x, y] as const));
function sample(frame: ImageFrame, x: number, y: number) {
const { origin, xAxis, yAxis } = frameTransform(frame, source);
- return origin.map((value, axis) => value + x * xAxis[axis] + y * yAxis[axis]);
+ const [u, v, w] = origin.map(
+ (value, axis) => value + x * xAxis[axis] + y * yAxis[axis],
+ );
+ expect(w).toBeGreaterThan(0);
+ return [u / w, v / w];
}
function expectSame(actual: number[], expected: number[]) {
for (const [i, value] of actual.entries()) {
@@ -50,45 +57,64 @@ test("crop preserves source coverage, opposite anchors, flips, and sliding along
expect(next.center).toEqual(minimum.center);
expectCovered(next);
}
- for (const rotation of [0, 90, 270]) {
- for (const angle of [-45, 0, 30]) {
- const before = rotate(
- { ...edge, center: [480, 360], rotation },
- angle,
- source,
- );
- for (const axis of [0, 1]) {
- const flipped = flip(before, axis);
- expectCovered(flipped);
- for (const [x, y] of corners) {
- const [fx, fy] = axis === 0 ? [1 - x, y] : [x, 1 - y];
- expectSame(sample(flipped, x, y), sample(before, fx, fy));
- }
- const turned = turn(flipped, 1);
- expectCovered(turned);
- expect(turn(turned, -1)).toEqual(flipped);
- // Each handle anchors the opposite corner or edge midpoint.
- for (const [handle, x, y] of [
- ["nw", 1, 1],
- ["se", 0, 0],
- ["n", 0.5, 1],
- ["e", 0, 0.5],
- ] as const) {
- for (const delta of [-1200, 1200]) {
- for (const ratio of [before.size[0] / before.size[1], null]) {
- const next = resize(flipped, handle, delta, delta, ratio, source);
- expectCovered(next);
- expectSame(sample(next, x, y), sample(flipped, x, y));
- expect(next.scale).toBe(flipped.scale);
- if (ratio) {
- expect(next.size[0] / next.size[1]).toBeCloseTo(ratio, 8);
- } else if (x === 0.5) {
- expect(next.size[0]).toBe(flipped.size[0]);
- } else if (y === 0.5) {
- expect(next.size[1]).toBe(flipped.size[1]);
+ // Perspective keeps the photo's center; the strongest settings keep every corner covered.
+ for (const perspective of [
+ [0, 0],
+ [60, -80],
+ [-100, 100],
+ ] as const) {
+ for (const rotation of [0, 90, 270]) {
+ for (const angle of [-45, 0, 30]) {
+ const before = rotate(
+ correct(
+ { ...edge, center: [480, 360], rotation },
+ perspective,
+ source,
+ ),
+ angle,
+ source,
+ );
+ expect(before.perspective).toEqual(perspective);
+ for (const axis of [0, 1]) {
+ const flipped = flip(before, axis);
+ expectCovered(flipped);
+ for (const [x, y] of corners) {
+ const [fx, fy] = axis === 0 ? [1 - x, y] : [x, 1 - y];
+ expectSame(sample(flipped, x, y), sample(before, fx, fy));
+ }
+ const turned = turn(flipped, 1);
+ expectCovered(turned);
+ expect(turn(turned, -1)).toEqual(flipped);
+ // Each handle anchors the opposite corner or edge midpoint.
+ for (const [handle, x, y] of [
+ ["nw", 1, 1],
+ ["se", 0, 0],
+ ["n", 0.5, 1],
+ ["e", 0, 0.5],
+ ] as const) {
+ for (const delta of [-1200, 1200]) {
+ for (const ratio of [before.size[0] / before.size[1], null]) {
+ const next = resize(
+ flipped,
+ handle,
+ delta,
+ delta,
+ ratio,
+ source,
+ );
+ expectCovered(next);
+ expectSame(sample(next, x, y), sample(flipped, x, y));
+ expect(next.scale).toBe(flipped.scale);
+ if (ratio) {
+ expect(next.size[0] / next.size[1]).toBeCloseTo(ratio, 8);
+ } else if (x === 0.5) {
+ expect(next.size[0]).toBe(flipped.size[0]);
+ } else if (y === 0.5) {
+ expect(next.size[1]).toBe(flipped.size[1]);
+ }
+ expectCovered(move(next, delta, delta, source));
+ expectCovered(rotate(next, -angle, source));
}
- expectCovered(move(next, delta, delta, source));
- expectCovered(rotate(next, -angle, source));
}
}
}
@@ -96,3 +122,40 @@ test("crop preserves source coverage, opposite anchors, flips, and sliding along
}
}
});
+
+test("perspective keeps the source under the crop's center, undoes exactly, and follows the displayed axes", () => {
+ const frame: ImageFrame = {
+ ...imageFrame(source),
+ center: [620, 380],
+ size: [1100, 700],
+ };
+ const corrected = correct(frame, [40, -70], source);
+ expectCovered(corrected);
+ expectSame(sample(corrected, 0.5, 0.5), sample(frame, 0.5, 0.5));
+ expect(Math.abs(corrected.scale[0])).toBeLessThan(1);
+ const restored = correct(corrected, [0, 0], source);
+ expectSame([...restored.center, ...restored.scale], [...frame.center, 1, 1]);
+
+ // Vertical stays vertical as displayed through every quarter turn and flip.
+ const length = (a: number[], b: number[]) =>
+ Math.hypot((a[0] - b[0]) * source[0], (a[1] - b[1]) * source[1]);
+ for (const turns of [0, 1, 2, 3]) {
+ for (const flips of [[], [0], [1], [0, 1]]) {
+ let oriented: ImageFrame = { ...imageFrame(source), size: [500, 300] };
+ for (let i = 0; i < turns; i++) oriented = turn(oriented, 1);
+ for (const axis of flips) oriented = flip(oriented, axis);
+ const shown = correctShown(oriented, [25, -60], source);
+ expect(shownPerspective(shown)).toEqual([25, -60]);
+ const vertical = correctShown(oriented, [0, 80], source);
+ const [nw, ne, sw, se] = [
+ [0, 0],
+ [1, 0],
+ [0, 1],
+ [1, 1],
+ ].map(([x, y]) => sample(vertical, x, y));
+ // More Vertical enlarges the displayed bottom, so less of the photo fills it; the sides match.
+ expect(length(sw, se)).toBeLessThan(length(nw, ne));
+ expect(length(nw, sw)).toBeCloseTo(length(ne, se), 6);
+ }
+ }
+});
diff --git a/tests/draft.test.ts b/tests/draft.test.ts
index b8ae01d..0230002 100644
--- a/tests/draft.test.ts
+++ b/tests/draft.test.ts
@@ -38,7 +38,12 @@ test("a draft survives storage's structured clone, reopens under its source ID,
const draft = snapshotDraft(document, "photo.png");
document.dispose();
const stored = structuredClone(await draft);
- expect(stored.record).toMatchObject({ version: 1, name: "photo.png" });
+ // Without perspective the scene stays version 2, which an OpenLight from before it still opens.
+ expect(stored.record).toMatchObject({
+ version: 1,
+ name: "photo.png",
+ scene: { version: 2 },
+ });
expect(await stored.files.get(source)?.bytes()).toEqual(bytes);
const recovered = await openDraft(stored, decode);
expect(recovered.scene.getState()).toEqual(edited);
diff --git a/tests/mapping.test.ts b/tests/mapping.test.ts
new file mode 100644
index 0000000..0613c32
--- /dev/null
+++ b/tests/mapping.test.ts
@@ -0,0 +1,89 @@
+import { expect, test } from "bun:test";
+import { createMapping } from "@/components/editor/mapping";
+import { type ImageFrame, imageFrame, type Point } from "@/core/image/frame";
+
+const source: Point = [1200, 800];
+// The strongest Horizontal on a thin crop in a wide viewport, which reaches past the horizon on the left.
+const frame: ImageFrame = {
+ ...imageFrame(source),
+ size: [200, 800],
+ perspective: [100, 0],
+};
+const viewport: Point = [4000, 800];
+const mapping = createMapping(
+ frame,
+ source,
+ { zoom: 1, pan: [0, 0] },
+ 1,
+ viewport,
+);
+const inside = (point: Point) =>
+ point[0] >= 0 &&
+ point[0] <= source[0] &&
+ point[1] >= 0 &&
+ point[1] <= source[1];
+
+test("pointer input maps exactly into the corrected photo and stops short of the horizon", () => {
+ for (const point of [
+ [2000, 400],
+ [1950, 100],
+ [2090, 790],
+ [1800, 300],
+ ] as const) {
+ const at = mapping.toDocument(point);
+ if (!at) throw Error("Missing document point.");
+ const back = mapping.toScreen(at);
+ expect(back?.[0]).toBeCloseTo(point[0], 6);
+ expect(back?.[1]).toBeCloseTo(point[1], 6);
+ }
+ // Left of the photo the viewport crosses where input stops, then the horizon itself.
+ expect(mapping.toDocument([100, 400])).toBeUndefined();
+
+ // A sparse stroke leaves the photo past where input stops and comes back elsewhere: it joins
+ // along that line, far outside the photo, instead of bridging across it.
+ const photo: Point = [2000, 300];
+ const away: Point = [100, 400];
+ const back: Point = [2050, 600];
+ const leaving = mapping.trail(photo, away);
+ const returning = mapping.trail(away, back);
+ expect(leaving).toHaveLength(1);
+ expect(returning).toHaveLength(2);
+ const [exit] = leaving;
+ const [entry, end] = returning;
+ expect(inside(end)).toBe(true);
+ expect(exit[0]).toBeCloseTo(entry[0], 6);
+ expect(exit[0]).toBeLessThan(-source[0]);
+ expect(mapping.trail(away, [50, 600])).toEqual([]);
+});
+
+test("a dab's outline is its exact image, and an oversized one keeps the part the viewport shows", () => {
+ const center: Point = [1000, 300];
+ const radius = 120;
+ const dab = mapping.ellipse({ center, radii: [radius, radius], angle: 0 });
+ if (!dab || !("ellipse" in dab)) throw Error("Expected a bounded ellipse.");
+ const { ellipse } = dab;
+ const turn = (ellipse.angle * Math.PI) / 180;
+ for (let i = 0; i < 16; i++) {
+ const t = (i / 16) * 2 * Math.PI;
+ const point = mapping.toScreen([
+ center[0] + radius * Math.cos(t),
+ center[1] + radius * Math.sin(t),
+ ]);
+ if (!point) throw Error("Missing screen point.");
+ const dx = point[0] - ellipse.center[0];
+ const dy = point[1] - ellipse.center[1];
+ const u = (dx * Math.cos(turn) + dy * Math.sin(turn)) / ellipse.radii[0];
+ const v = (dy * Math.cos(turn) - dx * Math.sin(turn)) / ellipse.radii[1];
+ expect(Math.hypot(u, v)).toBeCloseTo(1, 9);
+ }
+ // Its image is wider toward the enlarged right edge than a circle of the same area.
+ expect(ellipse.radii[0]).not.toBeCloseTo(ellipse.radii[1], 3);
+
+ // A brush larger than the photo reaches past the correction's horizon: no ellipse, but a path.
+ const huge = mapping.ellipse({ center, radii: [5000, 5000], angle: 0 });
+ if (!huge || !("path" in huge)) throw Error("Expected a clipped path.");
+ expect(huge.path).not.toContain("NaN");
+ expect(huge.path).not.toContain("Infinity");
+ const coordinates = huge.path.match(/-?[\d.e+-]+/g)?.map(Number) ?? [];
+ expect(coordinates.length).toBeGreaterThan(8);
+});
diff --git a/tests/perspective-gpu.ts b/tests/perspective-gpu.ts
new file mode 100644
index 0000000..7f96b92
--- /dev/null
+++ b/tests/perspective-gpu.ts
@@ -0,0 +1,74 @@
+import { effect, frame, init, target } from "vgpu";
+import { createImageLayer } from "@/app/editor/layers";
+import { createEditorRenderer } from "@/app/editor/renderer";
+import type { Scene } from "@/core/document";
+import { createImageSource } from "@/core/image";
+import { imageFrame } from "@/core/image/frame";
+import { correct } from "@/features/crop/geometry";
+
+/**
+ * Renders a smooth chart through strong perspective twice, at full size and as the half-size proxy an
+ * open gesture shows, and returns how far each proxy texel strays from the full pixels it stands for.
+ */
+export async function perspectiveProxy() {
+ const gpu = await init();
+ const size: [number, number] = [480, 320];
+ const image = target(gpu, { size, format: "rgba16float" });
+ const errors: string[] = [];
+ gpu.onError((error) => errors.push(error.message));
+ frame(gpu, (pass) =>
+ pass.pass(
+ image,
+ effect(
+ gpu,
+ `@fragment fn fs_main(@builtin(position) p: vec4f) -> @location(0) vec4f {
+ let uv = p.xy / vec2f(${size[0]}.0, ${size[1]}.0);
+ return vec4f(uv, 0.5, 1.0);
+ }`,
+ ),
+ ),
+ );
+ const source = createImageSource(image);
+ const renderer = createEditorRenderer(gpu, source);
+ const scene: Scene = {
+ frame: correct(imageFrame(size), [70, -90], size),
+ layers: [createImageLayer("chart", "Chart")],
+ };
+ try {
+ await renderer.update(scene);
+ const full = await renderer.outputImage().readFloats();
+ renderer.setDisplayScale(0.5);
+ await renderer.update({ ...scene }, undefined, true);
+ const reduced = renderer.outputImage();
+ const proxy = await reduced.readFloats();
+ let stray = 0;
+ for (let y = 0; y < reduced.size[1]; y++) {
+ for (let x = 0; x < reduced.size[0]; x++) {
+ for (let channel = 0; channel < 2; channel++) {
+ let block = 0;
+ for (const [dx, dy] of [
+ [0, 0],
+ [1, 0],
+ [0, 1],
+ [1, 1],
+ ]) {
+ block +=
+ full[((2 * y + dy) * size[0] + 2 * x + dx) * 4 + channel] / 4;
+ }
+ const texel = proxy[(y * reduced.size[0] + x) * 4 + channel];
+ // Sampling clamps within a texel of the chart's edge, where the crop may touch it.
+ if (block > 0.01 && block < 0.99) {
+ stray = Math.max(stray, Math.abs(texel - block));
+ }
+ }
+ }
+ }
+ // Without correction the output's corners would show the chart's own corners.
+ const corner = [...full.slice(0, 2)];
+ return { proxySize: reduced.size, stray, corner, errors };
+ } finally {
+ renderer.dispose();
+ source.dispose();
+ gpu.dispose();
+ }
+}
diff --git a/tests/rendering-benchmark.ts b/tests/rendering-benchmark.ts
index eb39215..ce14463 100644
--- a/tests/rendering-benchmark.ts
+++ b/tests/rendering-benchmark.ts
@@ -14,6 +14,7 @@ import type {
import { createImageSource } from "@/core/image";
import { imageFrame } from "@/core/image/frame";
import { defaultAdjustments } from "@/features/adjustments/model";
+import { correct } from "@/features/crop/geometry";
import { createHistogram } from "@/features/histogram/histogram";
import { defaultCurve } from "@/features/tone-curves/curve";
@@ -40,7 +41,8 @@ export type Workload =
| "remove"
| "heal-empty"
| "heal-proxy"
- | "pipeline-proxy";
+ | "pipeline-proxy"
+ | "perspective";
function summarize(values: number[]) {
const sorted = values.toSorted((a, b) => a - b);
@@ -314,7 +316,11 @@ export async function benchmarkRendering(
}
const scene: Scene = {
- frame: imageFrame(size),
+ // The neutral photo through a perspective correction, which the final transform resamples once.
+ frame:
+ workload === "perspective"
+ ? correct(imageFrame(size), [40, -60], size)
+ : imageFrame(size),
layers: [
{
...createImageLayer("benchmark", "Benchmark"),
diff --git a/tests/rendering.bench.ts b/tests/rendering.bench.ts
index 9b90006..f5b7c24 100644
--- a/tests/rendering.bench.ts
+++ b/tests/rendering.bench.ts
@@ -28,6 +28,7 @@ for (const workload of [
"heal-empty",
"heal-proxy",
"pipeline-proxy",
+ "perspective",
] as const) {
test(`rendering ${workload}`, async ({ page, browser }, info) => {
await page.goto("/tests/gpu.html");
diff --git a/tests/rendering.e2e.ts b/tests/rendering.e2e.ts
index 542c16d..b9ada31 100644
--- a/tests/rendering.e2e.ts
+++ b/tests/rendering.e2e.ts
@@ -125,6 +125,25 @@ test("tone adjustments preserve ramps, colors and alpha, compose, and reset", as
expect(result.resetMatches).toBe(true);
});
+test("a perspective-corrected proxy shows the full render it stands for", async ({
+ page,
+}) => {
+ await page.goto("/tests/gpu.html");
+ const result = await page.evaluate(async () => {
+ const path = "/tests/perspective-gpu.ts";
+ const { perspectiveProxy } = (await import(
+ path
+ )) as typeof import("./perspective-gpu");
+ return perspectiveProxy();
+ });
+ expect(result.errors).toEqual([]);
+ expect(result.proxySize).toEqual([240, 160]);
+ // The correction moved the output's corner into the chart, and the proxy follows it texel by texel.
+ expect(Math.max(...result.corner)).toBeGreaterThan(0.02);
+ // Less than the 2e-3 a half-texel shift of the proxy would make, beyond half-float rounding.
+ expect(result.stray).toBeLessThan(1.5e-3);
+});
+
test("a gradient recovers +3 EV photo exposure without clipping between layers", async ({
page,
}) => {
diff --git a/tests/scene-file.test.ts b/tests/scene-file.test.ts
index 03d6dab..3806a6e 100644
--- a/tests/scene-file.test.ts
+++ b/tests/scene-file.test.ts
@@ -48,7 +48,12 @@ test("scene files reopen the photo with every layer for further editing", async
{ x: 0.5, y: 0.6 },
{ x: 1, y: 1 },
]);
- api.setFrame({ ...imageFrame(size), angle: 5, scale: [-0.8, 0.8] });
+ api.setFrame({
+ ...imageFrame(size),
+ angle: 5,
+ scale: [-0.4, 0.4],
+ perspective: [-30, 45],
+ });
const mask = api.addLayer("mask");
const stroke: BrushStroke = {
mode: "paint",
@@ -147,9 +152,10 @@ test("scene files reopen the photo with every layer for further editing", async
.flatMap((layer) => (layer.kind === "heal" ? layer.patches : []))
.find((patch) => patch.mode === "remove");
if (removal?.mode !== "remove") throw Error("Missing Remove patch.");
+ // Perspective makes the file version 3, so an OpenLight from before it refuses rather than drops it.
expect(json).toMatchObject({
format: "openlight",
- version: 2,
+ version: 3,
sources: { [source]: { name: "photo.nef", type: "image/x-nikon-nef" } },
fields: { [removal.field]: {} },
scene: edited,
@@ -179,7 +185,7 @@ test("scene files reopen the photo with every layer for further editing", async
const invalid: [Blob, string][] = [
[new Blob(["not a zip"]), "Not a ZIP archive."],
[await archive({ ...json, format: "other" }), "an OpenLight scene"],
- [await archive({ ...json, version: 3 }), "needs a newer version"],
+ [await archive({ ...json, version: 4 }), "needs a newer version"],
[await archive({ ...json, fields: {} }), "Remove fields are missing"],
[
await archive({
@@ -200,6 +206,16 @@ test("scene files reopen the photo with every layer for further editing", async
}),
"Remove fields are missing",
],
+ [
+ await archive({
+ ...json,
+ scene: {
+ ...json.scene,
+ frame: { ...json.scene.frame, perspective: [0, 101] },
+ },
+ }),
+ "frame.perspective.1",
+ ],
[await archive(json, new Map()), "The scene's image is missing."],
[await adding(saved[1]), "IDs must be unique"],
[await adding({ id: "new", kind: "text" }), "Unknown layer kind: text."],
@@ -229,6 +245,7 @@ test("scene files reopen the photo with every layer for further editing", async
);
delete vignette.vignette.softness;
delete saved[0].whiteBalance;
+ delete json.scene.frame.perspective;
const healing = json.scene.layers.find(
(layer: { kind: string }) => layer.kind === "heal",
);
@@ -238,7 +255,7 @@ test("scene files reopen the photo with every layer for further editing", async
patch.stroke = patch.strokes[0];
delete patch.strokes;
}
- // Version 1 wrote a Remove field inside its patch, with how many strokes it covered.
+ // Version 1 wrote a Remove field inside its patch, with how many strokes it covered, and no perspective.
const lattice = { origin: [40, 12], scale: 1, size: [16, 16] } as const;
healing.patches[2].field = { texels: "inline", strokes: 1, ...lattice };
const texels = new Blob(["texels"]);
@@ -250,6 +267,7 @@ test("scene files reopen the photo with every layer for further editing", async
decode,
);
const loaded = older.scene.getState();
+ expect(loaded.frame).toEqual({ ...edited.frame, perspective: [0, 0] });
expect(loaded.layers[0].whiteBalance).toEqual(asShot);
expect(loaded.layers.find((layer) => layer.kind === "heal")).toMatchObject({
patches: [