feat(editor): edit the crop box on the preview, locked to the output proportions

The Camera step now shows its crop box on the Source picture with its mask on
by default, and the box is moved and resized right there (owner review 1, R5).
A press takes a handle within 6 px, then the wipe divider, then the inside of
the box, then pans; a resize stays anchored at the opposite corner or edge and
locked to the ratio, Alt resizes about the centre. Esc cancels a drag, and once
the box is taken (a press on it or Enter) the arrows, + and - nudge it. Cursors
follow the same priority and screen readers read "x, y, size" from a read-only
value pattern.

Each finished drag goes through CropCommit to CommitCropCommand as one undo
step; a run of key presses merges into one ("Camera.Crop"). The commit writes
the locked box with the offsets at 0, so the render keeps it as drawn. Editing
is gated on the Camera step, the mask on, an editable project, a current
format and frames to show.

PreviewRenderer frames Source through ViewFor, so the turned, mirrored and
straightened view frame is what the box is drawn on; the mask comes from
EditableCrop on the Camera step and FootprintInView elsewhere, and the horizon
overlay is mapped into the view. A straighten grid shows while IsStraightening.
xaml-lint now requires a tooltip on PreviewViewport.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-04 20:24:10 +01:00
1 parent aaf9c0bba7
commit 6f15717463
13 files changed
+1298 -65

No files matched your search

+70 -33
View File
@@ -15,7 +15,11 @@ public enum PreviewFraming
/// <summary>Through the render geometry (stabilisation, virtual camera, crop): what the video shows.</summary>
Output,
/// <summary>The whole frame, exposure-corrected; <see cref="PreviewFrame.Crop"/> marks what the output keeps.</summary>
/// <summary>
/// The whole view frame (owner review 1, R6: the frame turned, mirrored, flipped and straightened as the camera says;
/// the decoded frame itself without them), exposure-corrected, without stabilisation, camera pose or crop;
/// <see cref="PreviewFrame.Crop"/> marks what the output keeps and <see cref="PreviewFrame.EditCrop"/> the crop box.
/// </summary>
Source,
}
@@ -36,9 +40,24 @@ public sealed class PreviewFrame(int index, ImageBuffer corrected, ImageBuffer o
public ImageBuffer Original { get; } = original;
/// <summary>The output's part of the picture, normalised (the whole picture in <see cref="PreviewFraming.Output"/>).</summary>
/// <summary>
/// The output's part of the picture on this frame, normalised: the footprint of the frame's output (stabilisation and
/// camera pose included) in the view frame; the whole picture in <see cref="PreviewFraming.Output"/>. The Export step's mask.
/// </summary>
public RectD Crop { get; init; } = new(0, 0, 1, 1);
/// <summary>
/// The crop box the Camera step shows and edits (<see cref="RenderGeometry.EditableCrop"/>), normalised in the view
/// frame, the same on every frame: in <see cref="PreviewFraming.Source"/> it maps 1:1 onto the picture.
/// </summary>
public RectD EditCrop { get; init; } = new(0, 0, 1, 1);
/// <summary>The crop box's width over its height in normalised units (<see cref="RenderGeometry.LockRatio"/>).</summary>
public double LockRatio { get; init; } = 1;
/// <summary>The view frame's size at full resolution (<see cref="RenderGeometry.ViewWidth"/>): the source's, swapped by an odd turn.</summary>
public (int Width, int Height) ViewSize { get; init; }
/// <summary>Sky/land boundary in picture pixels; empty when the analysis found no horizon.</summary>
public IReadOnlyList<(double X, double Y)> Horizon { get; init; } = [];
@@ -59,7 +78,9 @@ public sealed class PreviewFrame(int index, ImageBuffer corrected, ImageBuffer o
/// single-frame entry point, so the per-frame steps of <c>RenderRun</c> are followed here: corrections from
/// <see cref="RenderPreparation"/> exactly as a render prepares them, then each frame through the render's exposure
/// stage, its single geometry warp and its finish (look, then highlight knee). Stacking and motion blur need neighbouring frames and are
/// left to the render.
/// left to the render. The Source framing of an oriented camera warps the corrected and the original picture once each
/// into the whole view frame (<see cref="RenderGeometry.ViewFor"/>), so the crop box maps onto it 1:1; without a turn,
/// mirror, flip or straighten it is the decoded frame itself, at no cost.
/// <para>
/// Preparing is split so a slider never decodes the sequence again: a measuring preparation (luminance, colour means,
/// stabilisation; keyed on the settings that change what is measured) runs once, and every other change re-solves
@@ -144,16 +165,20 @@ public sealed class PreviewRenderer(SequenceContent content, ISequenceSource sou
var (geometry, edge) = setup.Value;
var frame = await PictureAsync(request.Index, request, corrections, geometry, edge, cancellationToken).ConfigureAwait(false);
if (frame is null) return null;
var (corrected, original, plan) = frame.Value;
var (corrected, original, footprint, frameSize) = frame.Value;
try
{
var vectors = request.Vectors && request.Index + 1 < content.Count
? await VectorsAsync(request, corrections, corrected.Width, corrected.Height, cancellationToken).ConfigureAwait(false)
: [];
var source = request.Framing == PreviewFraming.Source;
return new PreviewFrame(request.Index, corrected, original)
{
Crop = request.Framing == PreviewFraming.Source ? CropOf(plan, corrected.Width, corrected.Height) : new RectD(0, 0, 1, 1),
Horizon = request.Framing == PreviewFraming.Source ? HorizonOf(corrected.Width, corrected.Height) : [],
Crop = footprint,
EditCrop = geometry.EditableCrop,
LockRatio = geometry.LockRatio,
ViewSize = (geometry.ViewWidth, geometry.ViewHeight),
Horizon = source ? HorizonOf(geometry, frameSize.Width, frameSize.Height) : [],
Vectors = vectors,
Waveform = request.Waveform ? LumaWaveform.Measure(corrected) : null,
};
@@ -259,8 +284,11 @@ public sealed class PreviewRenderer(SequenceContent content, ISequenceSource sou
return (geometry, int.Max(16, edge));
}
/// <summary>Decodes, corrects and frames one source frame: (corrected, original, output→picture plan).</summary>
private async Task<(ImageBuffer, ImageBuffer, GeometryPlan?)?> PictureAsync(int index, PreviewRequest request, RenderCorrections? corrections,
/// <summary>
/// Decodes, corrects and frames one source frame: (corrected, original, the output's footprint on the picture
/// normalised, the decoded frame's size).
/// </summary>
private async Task<(ImageBuffer, ImageBuffer, RectD, (int Width, int Height))?> PictureAsync(int index, PreviewRequest request, RenderCorrections? corrections,
RenderGeometry geometry, int edge, CancellationToken cancellationToken)
{
var decoded = await source.DecodeAsync(index, edge, DecodePurpose.Proxy, cancellationToken).ConfigureAwait(false);
@@ -271,19 +299,29 @@ public sealed class PreviewRenderer(SequenceContent content, ISequenceSource sou
{
corrected = raw.Clone();
if (corrections is not null && corrections.FrameCount == content.Count) ApplyExposure(corrected, index, corrections);
var frame = (raw.Width, raw.Height);
var settings = geometry.For(raw.Width, raw.Height, index, corrections?.StabilizationAt(index, raw.Width, raw.Height) ?? Affine2D.Identity);
var plan = GeometryStage.Plan(raw.Width, raw.Height, settings);
if (request.Framing == PreviewFraming.Output)
var footprint = new RectD(0, 0, 1, 1);
GeometrySettings? warp = settings;
if (request.Framing == PreviewFraming.Source)
{
var framed = GeometryStage.Apply(corrected, settings, cancellationToken);
footprint = geometry.FootprintInView(GeometryStage.Plan(raw.Width, raw.Height, settings), raw.Width, raw.Height);
// The whole view frame: the same turn, mirror, flip and straighten as the render, without pose or stabilisation.
var orientation = geometry.Orientation.Resized(raw.Width, raw.Height);
warp = orientation.IsIdentity ? null : geometry.ViewFor(raw.Width, raw.Height, orientation.ViewWidth, orientation.ViewHeight);
}
if (warp is not null)
{
var framed = GeometryStage.Apply(corrected, warp, cancellationToken);
corrected.Dispose();
corrected = framed;
framed = GeometryStage.Apply(raw, settings, cancellationToken);
framed = GeometryStage.Apply(raw, warp, cancellationToken);
raw.Dispose();
raw = framed;
}
// The look and the knee change the corrected side only: the wipe compares before and after.
FrameFinish.Apply(corrected, request.Project.Look, request.Project.Exposure.HighlightKnee);
return (corrected, raw, request.Framing == PreviewFraming.Output ? null : plan);
return (corrected, raw, footprint, frame);
}
catch
{
@@ -316,12 +354,12 @@ public sealed class PreviewRenderer(SequenceContent content, ISequenceSource sou
var small = request with { LongEdge = int.Min(request.LongEdge, VectorLongEdge), Vectors = false, Waveform = false };
if (Geometry(small, corrections) is not var (geometry, edge)) return [];
var current = await PictureAsync(request.Index, small, corrections, geometry, edge, cancellationToken).ConfigureAwait(false);
if (current is not var (a, aOriginal, _)) return [];
if (current is not var (a, aOriginal, _, _)) return [];
aOriginal.Dispose();
using (a)
{
var next = await PictureAsync(request.Index + 1, small, corrections, geometry, edge, cancellationToken).ConfigureAwait(false);
if (next is not var (b, bOriginal, _)) return [];
if (next is not var (b, bOriginal, _, _)) return [];
bOriginal.Dispose();
using (b)
{
@@ -344,27 +382,26 @@ public sealed class PreviewRenderer(SequenceContent content, ISequenceSource sou
}
}
/// <summary>The output's footprint on the source picture (bounding box of the warped output corners), normalised.</summary>
private static RectD CropOf(GeometryPlan? plan, int width, int height)
{
if (plan is not { } p) return new RectD(0, 0, 1, 1);
double left = double.MaxValue, top = double.MaxValue, right = double.MinValue, bottom = double.MinValue;
foreach (var (u, v) in new[] { (-0.5, -0.5), (p.OutputWidth - 0.5, -0.5), (-0.5, p.OutputHeight - 0.5), (p.OutputWidth - 0.5, p.OutputHeight - 0.5) })
{
var (x, y) = p.OutputToSource.Apply(u, v);
(left, top, right, bottom) = (double.Min(left, x + 0.5), double.Min(top, y + 0.5), double.Max(right, x + 0.5), double.Max(bottom, y + 0.5));
}
left = double.Clamp(left / width, 0, 1);
top = double.Clamp(top / height, 0, 1);
return new RectD(left, top, double.Clamp(right / width, 0, 1) - left, double.Clamp(bottom / height, 0, 1) - top);
}
/// <summary>The analysed horizon (normalised rows at evenly spaced columns) in pixels of the whole-frame picture.</summary>
private IReadOnlyList<(double X, double Y)> HorizonOf(int width, int height)
/// <summary>
/// The analysed horizon (normalised rows at evenly spaced columns) in pixels of the Source picture: measured on the
/// decoded frame of <paramref name="width"/>×<paramref name="height"/>, mapped into the view frame when the camera turns,
/// mirrors, flips or straightens it (<see cref="RenderGeometry.ViewToFrame"/>⁻¹, pixel-centre coordinates).
/// </summary>
private IReadOnlyList<(double X, double Y)> HorizonOf(RenderGeometry geometry, int width, int height)
{
if (content.Deep?.Regions is not { Horizon: { Length: > 1 } rows }) return [];
var toView = geometry.Orientation.IsIdentity ? (Affine2D?)null : geometry.ViewToFrame(width, height).Invert();
var points = new (double X, double Y)[rows.Length];
for (var k = 0; k < rows.Length; k++) points[k] = ((double)k / (rows.Length - 1) * width, rows[k] * height);
for (var k = 0; k < rows.Length; k++)
{
var (x, y) = ((double)k / (rows.Length - 1) * width, rows[k] * height);
if (toView is { } map)
{
var (vx, vy) = map.Apply(x - 0.5, y - 0.5);
(x, y) = (vx + 0.5, vy + 0.5);
}
points[k] = (x, y);
}
return points;
}