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

+31
View File
@@ -0,0 +1,31 @@
using System.Windows;
using System.Windows.Input;
using Chronos.Ui.Visuals;
namespace Chronos.App.Editor;
/// <summary>
/// Runs a command after every finished edit of the crop box on a <see cref="PreviewViewport"/> (a drag released, a key
/// press), with the <see cref="CropEditEventArgs"/> as its parameter (owner review 1, R5).
/// </summary>
public static class CropCommit
{
public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
"Command", typeof(ICommand), typeof(CropCommit), new PropertyMetadata(null, OnCommandChanged));
public static ICommand? GetCommand(DependencyObject d) => (ICommand?)d.GetValue(CommandProperty);
public static void SetCommand(DependencyObject d, ICommand? value) => d.SetValue(CommandProperty, value);
private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not PreviewViewport viewport) return;
viewport.CropEditCompleted -= OnEditCompleted;
if (e.NewValue is not null) viewport.CropEditCompleted += OnEditCompleted;
}
private static void OnEditCompleted(object sender, CropEditEventArgs e)
{
if (GetCommand((DependencyObject)sender) is { } command && command.CanExecute(e)) command.Execute(e);
}
}
+148 -5
View File
@@ -13,6 +13,7 @@ using Chronos.Core.Advice;
using Chronos.Core.Analysis;
using Chronos.Core.Diagnostics;
using Chronos.Core.Export;
using Chronos.Core.Imaging.Geometry;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
@@ -64,9 +65,18 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
private bool _showRegions;
private bool _showCropMask;
private bool _exportCropMask = true;
/// <summary>The Camera step's Crop mask, on by default: the crop box is edited there (owner review 1, R5).</summary>
private bool _cameraCropMask = true;
private CropMaskMode _cropMaskMode = CropMaskMode.ShowLoss;
private PreviewZoomMode _zoomMode = PreviewZoomMode.Fit;
private Rect _cropRect = new(0, 0, 1, 1);
private Rect _editCrop = new(0, 0, 1, 1);
private double _cropLockRatio = 1;
private bool _isStraightening;
/// <summary>The orientation of the camera the picture on screen was framed with (the crop box maps onto that view frame).</summary>
private (int Rotate, bool Mirror, bool Flip, double Straighten) _shownOrientation;
private IReadOnlyList<Point>? _regionBoundary;
private IReadOnlyList<MotionVector>? _motionVectors;
private int _previewEdge = DefaultPreviewEdge;
@@ -126,6 +136,7 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
{
if (StepFor(p) is { } step) SelectedTab = step;
});
CommitCropCommand = new RelayCommand(CommitCrop, _ => CropEditable);
Analysis = new AnalysisTab(this);
Frames = new FramesTab(this);
@@ -144,6 +155,8 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
// Until the link is read (AttachJobsAsync), a sequence of this PC cannot send its renders.
_syncGate = session.IsRemote ? ExportSyncGate.Remote : ExportSyncGate.SignedOut;
RenderChoices = [.. ExportPresets.BuiltIn.Select(p => new Choice(p.Id, p.NameKey))];
_shownOrientation = OrientationOf(session.Project.Camera);
UpdateCropBox();
session.ProjectChanged += OnProjectChanged;
session.ContentChanged += OnContentChanged;
@@ -277,6 +290,9 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
/// <summary>The Export step swaps the preview tools for compare, crop mask and a 10-second preview (Export.html).</summary>
public bool IsExportTab => SelectedTab is ExportTab;
/// <summary>The Camera step: its crop mask shows the crop box itself, edited on the picture (owner review 1, R5).</summary>
public bool IsCameraTab => SelectedTab is CameraTab;
/// <summary>The measuring area takes more of the height than the preview (the Analysis findings, the Export preset cards).</summary>
public bool IsTallMeasure => SelectedTab.PrefersTallMeasure;
@@ -364,21 +380,72 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
}
}
/// <summary>The Crop mask tool of the current tab: the Export tab has its own, on by default (Export.html).</summary>
/// <summary>
/// The Crop mask tool of the current tab: the Export tab has its own, on by default (Export.html), and so has the Camera
/// step, where the crop box is edited (owner review 1, R5).
/// </summary>
public bool ShowCropMask
{
get => IsExportTab ? _exportCropMask : _showCropMask;
get => IsExportTab ? _exportCropMask : IsCameraTab ? _cameraCropMask : _showCropMask;
set
{
if (value == ShowCropMask) return;
if (IsExportTab) _exportCropMask = value;
else if (IsCameraTab) _cameraCropMask = value;
else _showCropMask = value;
OnPropertyChanged();
OnPropertyChanged(nameof(CropModeToolTip));
OnCropEditableChanged();
RequestPreview(immediate: true);
}
}
/// <summary>
/// The crop box is moved and resized on the preview: the Camera step with its mask on, while the project can change
/// (not read-only, in a newer format, outdated or busy).
/// </summary>
public bool CropEditable => IsCameraTab && ShowCropMask && IsEditable && !Session.Project.IsNewerFormat && FrameCount > 0;
/// <summary>
/// The crop box the Camera step shows and edits (<see cref="RenderGeometry.EditableCrop"/>), normalised in the view frame
/// the Source picture shows; the preview sets it to each finished edit before <see cref="CommitCropCommand"/> runs.
/// </summary>
public Rect EditCrop
{
get => _editCrop;
set
{
if (!Set(ref _editCrop, value)) return;
if (IsCameraTab) OnPropertyChanged(nameof(CropRect));
}
}
/// <summary>The crop box's width over its height in normalised units (<see cref="RenderGeometry.LockRatio"/>): the output proportions on the view frame.</summary>
public double CropLockRatio { get => _cropLockRatio; private set => Set(ref _cropLockRatio, value); }
/// <summary>
/// Writes a finished edit of the crop box (<see cref="CropEditEventArgs"/>, or a normalised <see cref="Rect"/>) as the
/// camera crop, locked and with the offsets at 0 (owner review 1, R5): one drag is one undo step, a run of key presses
/// merges into one ("Camera.Crop") that the key release ends.
/// </summary>
public ICommand CommitCropCommand { get; }
/// <summary>The Straighten slider is dragged or has the keyboard: the preview shows a fine grid to level the horizon against.</summary>
public bool IsStraightening
{
get => _isStraightening;
set
{
if (Set(ref _isStraightening, value)) OnPropertyChanged(nameof(ShowStraightenGrid));
}
}
/// <summary>The straighten grid over the preview (Camera step, while straightening).</summary>
public bool ShowStraightenGrid => IsStraightening && IsCameraTab;
/// <summary>What a drag on the preview does: the crop box's moves and keys while it is edited there, else compare, pan and zoom.</summary>
public string PreviewToolTip => Loc.L(CropEditable ? "Tooltips.Editor.Preview.CropEdit" : "Tooltips.Editor.Preview.View");
/// <summary>Overlays the viewport draws: only tools that have a button on the current tab.</summary>
public bool ViewVectors => ShowVectors && !IsExportTab;
@@ -422,7 +489,20 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
set => ZoomMode = value ? PreviewZoomMode.Actual : PreviewZoomMode.Fit;
}
public Rect CropRect { get => _cropRect; private set => Set(ref _cropRect, value); }
/// <summary>
/// The crop mask: on the Camera step the crop box (<see cref="EditCrop"/>), elsewhere the footprint of the frame's output
/// (stabilisation and camera pose included) on the Source picture.
/// </summary>
public Rect CropRect
{
get => IsCameraTab ? _editCrop : _cropRect;
private set
{
if (_cropRect == value) return;
_cropRect = value;
if (!IsCameraTab) OnPropertyChanged();
}
}
public IReadOnlyList<Point>? RegionBoundary { get => _regionBoundary; private set => Set(ref _regionBoundary, value); }
@@ -800,6 +880,10 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
PreviewImage = r.Corrected;
OriginalImage = r.Original;
CropRect = r.Crop;
// The crop box of the picture now on screen: its view frame, its proportions.
_shownOrientation = OrientationOf(request.Project.Camera);
CropLockRatio = r.LockRatio;
EditCrop = r.EditCrop;
RegionBoundary = ViewRegions ? r.Horizon : null;
MotionVectors = ViewVectors ? r.Vectors : null;
WaveformImage = r.Waveform;
@@ -983,14 +1067,69 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
private void OnToolsChanged()
{
OnPropertyChanged(nameof(IsCameraTab));
OnPropertyChanged(nameof(ShowCropMask));
OnPropertyChanged(nameof(CropModeToolTip));
OnPropertyChanged(nameof(ViewVectors));
OnPropertyChanged(nameof(ViewRegions));
OnPropertyChanged(nameof(CropRect));
OnPropertyChanged(nameof(ShowStraightenGrid));
OnCropEditableChanged();
}
/// <summary>Whether the crop box is edited on the preview, and what a drag there does.</summary>
private void OnCropEditableChanged()
{
OnPropertyChanged(nameof(CropEditable));
OnPropertyChanged(nameof(PreviewToolTip));
}
/// <summary>The rendered orientation members of <paramref name="camera"/>: the view frame the crop box is normalised in.</summary>
private static (int, bool, bool, double) OrientationOf(CameraSettings camera) =>
(FrameOrientation.TurnsOf(camera.Rotate), camera.Mirror, camera.Flip, FrameOrientation.StraightenOf(camera.Straighten));
/// <summary>
/// The crop box and its proportions for the project now, at once, while the picture on screen shows the same view frame
/// (a crop row, an aspect or preset change: no frame to wait for); a turn, mirror, flip or straighten waits for the
/// picture it applies to.
/// </summary>
private void UpdateCropBox()
{
var project = Session.Project;
if (OrientationOf(project.Camera) != _shownOrientation || Session.Content.SourceWidth <= 0 || Session.Content.SourceHeight <= 0) return;
try
{
var geometry = RenderGeometry.Create(project.Camera, ExportResolver.Resolve(project.Export), Session.Content.SourceWidth, Session.Content.SourceHeight);
CropLockRatio = geometry.LockRatio;
var box = geometry.EditableCrop;
EditCrop = new Rect(box.X, box.Y, box.Width, box.Height);
}
catch (Exception ex) when (ex is ArgumentException or InvalidOperationException or RenderException)
{
Log.Warning("editor", "The crop box could not be worked out", ex);
}
}
private void CommitCrop(object? parameter)
{
var (crop, nudge) = parameter switch
{
CropEditEventArgs edit => (edit.Crop, edit.IsNudge),
Rect rect => (rect, false),
_ => (Rect.Empty, false),
};
if (crop.IsEmpty || !CropEditable) return;
// The box itself, locked to the proportions on screen, with the offsets at 0: the render's FitCrop returns it unchanged.
var box = CropFrame.Clamp(new RectD(crop.X, crop.Y, crop.Width, crop.Height), CropLockRatio);
EditCrop = new Rect(box.X, box.Y, box.Width, box.Height);
Session.Edit(p => p with { Camera = p.Camera with { Crop = CropFrame.ToCrop(box), OffsetX = 0, OffsetY = 0 } }, "Camera.Crop");
if (!nudge) Session.EndEditRun();
}
private void OnProjectChanged()
{
UpdateCropBox();
OnCropEditableChanged();
foreach (var tab in Tabs) tab.Refresh();
UpdateMarkers();
RefreshSteps();
@@ -1009,6 +1148,7 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
Planner = new ExportPlanner(Session.Content, _encoders, _host);
if (Thumbnails.Count != FrameCount) Thumbnails = new EditorThumbnails(Session.Source, FrameCount);
Corrections = null;
UpdateCropBox();
_rendered = new RenderedSettings(Session.Content.Renders);
foreach (var tab in Tabs) tab.OnContentChanged();
OnPropertyChanged(nameof(Summary));
@@ -1347,6 +1487,7 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
OnPropertyChanged(nameof(IsAnalysing));
OnPropertyChanged(nameof(IsLocked));
OnPropertyChanged(nameof(IsEditable));
OnCropEditableChanged();
OnPropertyChanged(nameof(LockedToolTip));
OnPropertyChanged(nameof(AnalyseToolTip));
OnPropertyChanged(nameof(OptimiseAllToolTip));
@@ -1377,13 +1518,15 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
}
/// <summary>A rendered preview as frozen bitmaps and overlays in picture pixels (built off the UI thread).</summary>
private sealed record PreviewResult(ImageSource Corrected, ImageSource Original, Rect Crop, IReadOnlyList<Point> Horizon, IReadOnlyList<MotionVector> Vectors,
ImageSource? Waveform)
private sealed record PreviewResult(ImageSource Corrected, ImageSource Original, Rect Crop, Rect EditCrop, double LockRatio, IReadOnlyList<Point> Horizon,
IReadOnlyList<MotionVector> Vectors, ImageSource? Waveform)
{
public static PreviewResult From(PreviewFrame frame) => new(
EditorImages.ToBitmap(frame.Corrected),
EditorImages.ToBitmap(frame.Original),
new Rect(frame.Crop.X, frame.Crop.Y, frame.Crop.Width, frame.Crop.Height),
new Rect(frame.EditCrop.X, frame.EditCrop.Y, frame.EditCrop.Width, frame.EditCrop.Height),
frame.LockRatio,
[.. frame.Horizon.Select(p => new Point(p.X, p.Y))],
[.. frame.Vectors.Select(v => new MotionVector(v.X, v.Y, v.Dx, v.Dy))],
frame.Waveform is { } waveform ? EditorImages.ToMask(waveform) : null);
@@ -15,7 +15,8 @@
</Window.Resources>
<!-- Full screen at native resolution (BRIEF §12.3) with collapsible shot data; Esc closes, arrows step, space plays. -->
<v:PreviewViewport x:Name="Viewport" Source="{Binding PreviewImage}" OriginalSource="{Binding OriginalImage}" IsCompareEnabled="{Binding IsCompare}"
WipeLabel="{Binding WipeLabel}" ZoomMode="Actual" InfoText="{Binding InfoText}" AutomationProperties.Name="{l:Loc Editor.Preview}">
WipeLabel="{Binding WipeLabel}" ZoomMode="Actual" InfoText="{Binding InfoText}" ToolTip="{l:Loc Tooltips.Editor.Preview.View}"
AutomationProperties.Name="{l:Loc Editor.Preview}">
<v:PreviewViewport.ToolsContent>
<!-- The tools' 6 px bottom margin is for a second line in the editor; one line takes it back. -->
<StackPanel Orientation="Horizontal" Margin="0,0,0,-6">
+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;
}
+4 -1
View File
@@ -5,6 +5,7 @@
xmlns:v="clr-namespace:Chronos.Ui.Visuals;assembly=Chronos.Ui"
xmlns:l="clr-namespace:Chronos.Ui.Localization;assembly=Chronos.Ui"
xmlns:infra="clr-namespace:Chronos.App.Infrastructure"
xmlns:e="clr-namespace:Chronos.App.Editor"
xmlns:sections="clr-namespace:Chronos.App.Sections"
KeyDown="OnKeyDown" Loaded="OnLoaded">
<UserControl.Resources>
@@ -223,7 +224,9 @@
WipeLabel="{Binding WipeLabel}" ZoomMode="{Binding ZoomMode, Mode=TwoWay}" ShowVectors="{Binding ViewVectors}"
MotionVectors="{Binding MotionVectors}" VectorScale="{Binding VectorScale}" ShowRegions="{Binding ViewRegions}"
RegionBoundary="{Binding RegionBoundary}" ShowCropMask="{Binding ShowCropMask}" CropRect="{Binding CropRect}"
CropMaskMode="{Binding CropMaskMode}" InfoText="{Binding InfoText}" ToolTip="{l:Loc Tooltips.Editor.Preview.View}"
CropMaskMode="{Binding CropMaskMode}" CropEditable="{Binding CropEditable}" CropLockRatio="{Binding CropLockRatio}"
EditCrop="{Binding EditCrop}" ShowGrid="{Binding ShowStraightenGrid}" e:CropCommit.Command="{Binding CommitCropCommand}"
InfoText="{Binding InfoText}" ToolTip="{Binding PreviewToolTip}"
AutomationProperties.Name="{l:Loc Editor.Preview}" SizeChanged="OnPreviewSizeChanged">
<v:PreviewViewport.ToolsContent>
<!-- Tools give the keyboard back to the preview after a click, so Space keeps playing (OnToolClicked). They take a second
@@ -11,7 +11,7 @@ internal sealed class XamlLintCommand : ICommand
private static readonly HashSet<string> Actionable = new(StringComparer.Ordinal)
{
"Button", "ToggleButton", "RepeatButton", "Slider", "ComboBox", "CheckBox", "RadioButton", "MenuItem",
"AutoSlider", "IconButton", "SplitButton",
"AutoSlider", "IconButton", "SplitButton", "PreviewViewport",
};
private static readonly string[] TextAttributes = ["Content", "Text", "Header", "ToolTip", "Title", "Tag"];
+62 -5
View File
@@ -1,6 +1,7 @@
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Media;
using Chronos.Core.Imaging.Geometry;
namespace Chronos.Ui.Visuals;
@@ -18,6 +19,13 @@ public sealed class PreviewSurface : DrawingElement
private const double LabelSize = 11;
/// <summary>Crop handles: squares of this many view units (DIPs) whatever the zoom, and their stroke.</summary>
private const double HandleSize = 8;
private const double HandleStroke = 1.5;
/// <summary>Cells per side of the straighten grid.</summary>
private const int GridCells = 8;
private readonly VisualCollection _layers;
private readonly DrawingVisual _corrected = new();
private readonly DrawingVisual _original = new();
@@ -82,13 +90,14 @@ public sealed class PreviewSurface : DrawingElement
if (view.Scale != _overlayScale) RefreshOverlay();
}
/// <summary>Rebuilds the overlays (crop mask, region boundary, motion vectors) in image coordinates.</summary>
/// <summary>Rebuilds the overlays (straighten grid, crop mask, region boundary, motion vectors) in image coordinates.</summary>
internal void RefreshOverlay()
{
_overlayScale = _view.Matrix.M11;
using var dc = _overlay.RenderOpen();
if (Owner is not { } owner || _imageSize.IsEmpty || !(_overlayScale > 0)) return;
var unit = 1 / _overlayScale;
if (owner.ShowGrid) DrawGrid(dc, unit);
if (owner.ShowCropMask) DrawCropMask(dc, owner, unit);
if (owner.ShowRegions && owner.RegionBoundary is { Count: > 1 } boundary) DrawRegion(dc, boundary, unit);
if (owner.ShowVectors && owner.MotionVectors is { Count: > 0 } vectors) DrawVectors(dc, vectors, owner.VectorScale, unit);
@@ -139,18 +148,66 @@ public sealed class PreviewSurface : DrawingElement
_wipeClip.Rect = visible ? new Rect(0, 0, Math.Max(0, Owner!.WipePosition * ActualWidth), Math.Max(0, ActualHeight)) : Rect.Empty;
}
/// <summary>Everything outside the crop: dimmed with the scrim ("what I lose") or hidden ("how it will look").</summary>
/// <summary>
/// Everything outside the crop: dimmed with the scrim ("what I lose") or hidden ("how it will look"). An editable box
/// (owner review 1, R5) has an Accent outline and 8 handles in both modes, and rule-of-thirds lines while dragged.
/// </summary>
private void DrawCropMask(DrawingContext dc, PreviewViewport owner, double unit)
{
var image = new Rect(_imageSize);
var n = owner.CropRect;
var crop = Rect.Intersect(image, new Rect(n.X * image.Width, n.Y * image.Height, n.Width * image.Width, n.Height * image.Height));
var n = owner.ShownCrop;
var crop = n.IsEmpty ? Rect.Empty : Rect.Intersect(image, new Rect(n.X * image.Width, n.Y * image.Height, n.Width * image.Width, n.Height * image.Height));
if (crop.IsEmpty) crop = new Rect(image.TopLeft, new Size(0, 0));
var outside = new CombinedGeometry(GeometryCombineMode.Exclude, new RectangleGeometry(image), new RectangleGeometry(crop));
outside.Freeze();
var loss = owner.CropMaskMode == CropMaskMode.ShowLoss;
dc.DrawGeometry(loss ? GetBrush("Brush.Scrim") : owner.Background ?? GetBrush("Brush.Preview"), null, outside);
if (loss && !crop.IsEmpty) dc.DrawRectangle(null, CreatePen("Brush.TextMuted", unit), crop);
if (crop.Width <= 0 || crop.Height <= 0) return;
if (!owner.CropEditable)
{
if (loss) dc.DrawRectangle(null, CreatePen("Brush.TextMuted", unit), crop);
return;
}
if (owner.IsCropDragging) DrawThirds(dc, crop, unit);
dc.DrawRectangle(null, CreatePen("Brush.Accent", HandleStroke * unit), crop);
var (fill, stroke, half) = (GetBrush("Brush.Card"), CreatePen("Brush.Accent", HandleStroke * unit), HandleSize * unit / 2);
var box = new RectD(crop.X, crop.Y, crop.Width, crop.Height);
foreach (var handle in ViewportMath.CropHandles)
{
var (x, y) = ViewportMath.HandleAt(box, handle);
dc.DrawRectangle(fill, stroke, new Rect(x - half, y - half, 2 * half, 2 * half));
}
}
/// <summary>The rule-of-thirds lines inside the dragged box (1 px Text at 50 %).</summary>
private void DrawThirds(DrawingContext dc, Rect crop, double unit)
{
var pen = CreatePen("Brush.Text", unit);
dc.PushOpacity(0.5);
for (var k = 1; k < 3; k++)
{
var x = crop.X + crop.Width * k / 3;
var y = crop.Y + crop.Height * k / 3;
dc.DrawLine(pen, new Point(x, crop.Y), new Point(x, crop.Bottom));
dc.DrawLine(pen, new Point(crop.X, y), new Point(crop.Right, y));
}
dc.Pop();
}
/// <summary>The straighten grid: 8 × 8 cells over the whole picture (1 px Text at 30 %), to level the horizon against.</summary>
private void DrawGrid(DrawingContext dc, double unit)
{
var pen = CreatePen("Brush.Text", unit);
var (width, height) = (_imageSize.Width, _imageSize.Height);
dc.PushOpacity(0.3);
for (var k = 1; k < GridCells; k++)
{
var x = width * k / GridCells;
var y = height * k / GridCells;
dc.DrawLine(pen, new Point(x, 0), new Point(x, height));
dc.DrawLine(pen, new Point(0, y), new Point(width, y));
}
dc.Pop();
}
private void DrawRegion(DrawingContext dc, IReadOnlyList<Point> boundary, double unit)
+322 -17
View File
@@ -1,10 +1,13 @@
using System.Collections;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Chronos.Core.Imaging.Geometry;
using Chronos.Core.Localization;
namespace Chronos.Ui.Visuals;
@@ -35,6 +38,18 @@ public enum CropMaskMode
/// <summary>A motion-field sample in image pixels: origin (X, Y) and displacement (Dx, Dy).</summary>
public readonly record struct MotionVector(float X, float Y, float Dx, float Dy);
/// <summary>A finished edit of the crop box (<see cref="PreviewViewport.CropEditCompletedEvent"/>).</summary>
/// <param name="crop">The box, normalised in the image, locked to <see cref="PreviewViewport.CropLockRatio"/> and inside [0,1]².</param>
/// <param name="isNudge">A key press (arrows, + and −): a run of them is one undo step; a drag is one on its own.</param>
public sealed class CropEditEventArgs(RoutedEvent routedEvent, Rect crop, bool isNudge) : RoutedEventArgs(routedEvent)
{
public Rect Crop { get; } = crop;
public bool IsNudge { get; } = isNudge;
}
public delegate void CropEditEventHandler(object sender, CropEditEventArgs e);
/// <summary>
/// Editor preview (BRIEF §12.3): the corrected image with an optional original shown left of a draggable wipe
/// divider (label from <see cref="WipeLabel"/>), zoom to fit / 1:1 (raw pixels) / wheel zoom around the cursor,
@@ -45,6 +60,16 @@ public readonly record struct MotionVector(float X, float Y, float Dx, float Dy)
/// Fit and 1:1; Ctrl+0 fits, Ctrl+Shift+1 shows 1:1 (Ctrl+1 to Ctrl+6 switch the client's sections). Screen readers
/// see an image (named by AutomationProperties.Name) whose status is the zoom and whose help text is the shot data,
/// holding the tools.
/// <para>
/// While <see cref="CropEditable"/> (owner review 1, R5) the crop box <see cref="EditCrop"/> has 8 handles and is edited
/// on the picture, locked to <see cref="CropLockRatio"/> (<see cref="CropFrame"/>). A press takes, in order, a handle
/// within 6 px (resize: a corner anchored at the opposite corner, an edge at the opposite edge; Alt about the centre),
/// the wipe divider, the inside of the box (move), else pans. Only the overlay follows the pointer;
/// <see cref="CropEditCompletedEvent"/> fires once when the button is released, and Esc cancels the drag. A press on the
/// box (or Enter) gives it the keyboard: arrows move it 1 % (Shift 10 %), + and − resize it 2 % about its centre, each
/// firing the event; Esc or a press outside gives the arrows back to the editor (frame steps). Screen readers read the
/// box as the image's value ("x 12 %, y 5 %, size 80 %").
/// </para>
/// </summary>
[TemplatePart(Name = PartSurface, Type = typeof(FrameworkElement))]
[TemplatePart(Name = PartTools, Type = typeof(FrameworkElement))]
@@ -106,6 +131,22 @@ public class PreviewViewport : Control
public static readonly DependencyProperty CropMaskModeProperty = DependencyProperty.Register(
nameof(CropMaskMode), typeof(CropMaskMode), typeof(PreviewViewport), new PropertyMetadata(CropMaskMode.ShowLoss, OnOverlayChanged));
public static readonly DependencyProperty CropEditableProperty = DependencyProperty.Register(
nameof(CropEditable), typeof(bool), typeof(PreviewViewport), new PropertyMetadata(false, OnCropEditableChanged));
public static readonly DependencyProperty CropLockRatioProperty = DependencyProperty.Register(
nameof(CropLockRatio), typeof(double), typeof(PreviewViewport), new PropertyMetadata(1.0, OnOverlayChanged, CoerceRatio));
public static readonly DependencyProperty EditCropProperty = DependencyProperty.Register(
nameof(EditCrop), typeof(Rect), typeof(PreviewViewport),
new FrameworkPropertyMetadata(new Rect(0, 0, 1, 1), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnOverlayChanged));
public static readonly DependencyProperty ShowGridProperty = DependencyProperty.Register(
nameof(ShowGrid), typeof(bool), typeof(PreviewViewport), new PropertyMetadata(false, OnOverlayChanged));
public static readonly RoutedEvent CropEditCompletedEvent = EventManager.RegisterRoutedEvent(
nameof(CropEditCompleted), RoutingStrategy.Bubble, typeof(CropEditEventHandler), typeof(PreviewViewport));
public static readonly DependencyProperty InfoTextProperty = DependencyProperty.Register(nameof(InfoText), typeof(string), typeof(PreviewViewport));
public static readonly DependencyProperty ToolsContentProperty = DependencyProperty.Register(
@@ -115,6 +156,15 @@ public class PreviewViewport : Control
private const double MaxZoom = 32;
private const double DividerGrip = 6;
/// <summary>How near a crop handle a press takes it, in view units (DIPs).</summary>
private const double HandleGrip = 6;
/// <summary>The arrows move the box by this share of the view (Shift: ten times).</summary>
private const double NudgeStep = 0.01;
/// <summary>+ and − resize the box by this share of the largest box.</summary>
private const double ResizeStep = 0.02;
private PreviewSurface? _surface;
private FrameworkElement? _tools;
private FrameworkElement? _info;
@@ -125,6 +175,17 @@ public class PreviewViewport : Control
private DragMode _drag;
private Point _dragStart;
private ViewTransform _dragOrigin;
private CropHandle _handle;
private RectD _cropStart;
private RectD? _cropDraft;
/// <summary>The pointer's offset from the dragged handle at the press, normalised (the handle does not jump to the pointer).</summary>
private (double X, double Y) _grab;
private Point _lastPointer;
/// <summary>The box has the keyboard: the arrows move it instead of stepping frames.</summary>
private bool _cropSelected;
static PreviewViewport()
{
@@ -138,11 +199,21 @@ public class PreviewViewport : Control
CommandBindings.Add(new CommandBinding(ActualSizeCommand, (_, _) => SetCurrentValue(ZoomModeProperty, PreviewZoomMode.Actual)));
}
private enum DragMode
/// <summary>Raised once per finished crop edit: a drag released (not cancelled), or a key press while the box has the keyboard.</summary>
public event CropEditEventHandler CropEditCompleted
{
add => AddHandler(CropEditCompletedEvent, value);
remove => RemoveHandler(CropEditCompletedEvent, value);
}
/// <summary>What the left button is dragging.</summary>
internal enum DragMode
{
None,
Pan,
Wipe,
MoveCrop,
ResizeCrop,
}
/// <summary>Corrected image (the output look).</summary>
@@ -185,6 +256,30 @@ public class PreviewViewport : Control
public CropMaskMode CropMaskMode { get => (CropMaskMode)GetValue(CropMaskModeProperty); set => SetValue(CropMaskModeProperty, value); }
/// <summary>The crop box is edited on the picture (handles, move, resize, keyboard): the mask shows <see cref="EditCrop"/>.</summary>
public bool CropEditable { get => (bool)GetValue(CropEditableProperty); set => SetValue(CropEditableProperty, value); }
/// <summary>The box's width over its height in normalised image units (<see cref="CropFrame"/>'s lock ratio).</summary>
public double CropLockRatio { get => (double)GetValue(CropLockRatioProperty); set => SetValue(CropLockRatioProperty, value); }
/// <summary>The edited crop box, normalised (0…1); set to the finished edit before <see cref="CropEditCompleted"/> fires (two-way).</summary>
public Rect EditCrop { get => (Rect)GetValue(EditCropProperty); set => SetValue(EditCropProperty, value); }
/// <summary>A fine 8 × 8 grid over the whole picture (while the straighten is set).</summary>
public bool ShowGrid { get => (bool)GetValue(ShowGridProperty); set => SetValue(ShowGridProperty, value); }
/// <summary>The crop the mask draws: the box being dragged, the edited box, or <see cref="CropRect"/>.</summary>
internal Rect ShownCrop => _cropDraft is { } draft ? new Rect(draft.X, draft.Y, draft.Width, draft.Height) : CropEditable ? EditCrop : CropRect;
/// <summary>A crop drag is under way (the rule-of-thirds lines show).</summary>
internal bool IsCropDragging => _drag is DragMode.MoveCrop or DragMode.ResizeCrop;
/// <summary>The box has the keyboard (arrows, + and −).</summary>
internal bool IsCropSelected => _cropSelected;
/// <summary>What the left button is dragging now.</summary>
internal DragMode Dragging => _drag;
/// <summary>Shot data shown in the bottom-right pill.</summary>
public string? InfoText { get => (string?)GetValue(InfoTextProperty); set => SetValue(InfoTextProperty, value); }
@@ -250,14 +345,13 @@ public class PreviewViewport : Control
var p = e.GetPosition(_surface);
if (e.ClickCount == 2)
{
CancelDrag();
ToggleZoom(p);
e.Handled = true;
return;
}
_drag = IsNearDivider(p) ? DragMode.Wipe : DragMode.Pan;
_dragStart = p;
_dragOrigin = _view;
if (!CaptureMouse()) _drag = DragMode.None;
Press(p);
if (!CaptureMouse()) EndDrag(commit: false);
e.Handled = true;
}
@@ -265,15 +359,7 @@ public class PreviewViewport : Control
{
base.OnMouseMove(e);
if (_drag == DragMode.None || _surface is null) return;
var p = e.GetPosition(_surface);
if (_drag == DragMode.Wipe)
{
var width = ViewSize.Width;
if (width > 0) SetCurrentValue(WipePositionProperty, p.X / width);
return;
}
UpdateView(ViewportMath.Clamp(ViewportMath.Pan(_dragOrigin, p.X - _dragStart.X, p.Y - _dragStart.Y),
_imageSize.Width, _imageSize.Height, ViewSize.Width, ViewSize.Height));
DragTo(e.GetPosition(_surface), (Keyboard.Modifiers & ModifierKeys.Alt) != 0);
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
@@ -285,7 +371,54 @@ public class PreviewViewport : Control
protected override void OnLostMouseCapture(MouseEventArgs e)
{
base.OnLostMouseCapture(e);
_drag = DragMode.None;
EndDrag(commit: true);
}
/// <summary>
/// Esc cancels a crop drag (or gives the arrows back); with the box selected, arrows move it and + and − resize it;
/// Alt, pressed or released during a resize, switches it about the centre and back.
/// </summary>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.Handled) return;
var key = e.Key == Key.System ? e.SystemKey : e.Key;
if (key is Key.LeftAlt or Key.RightAlt && _drag == DragMode.ResizeCrop)
{
DragTo(_lastPointer, aboutCentre: true);
e.Handled = true;
return;
}
if (key == Key.Escape && (IsCropDragging || _cropSelected))
{
if (IsCropDragging) CancelDrag();
else _cropSelected = false;
e.Handled = true;
return;
}
if (!CropEditable || _drag != DragMode.None) return;
if (key == Key.Enter && Keyboard.Modifiers == ModifierKeys.None && !_cropSelected)
{
_cropSelected = true;
e.Handled = true;
return;
}
if (_cropSelected && Nudge(key, Keyboard.Modifiers)) e.Handled = true;
}
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
var key = e.Key == Key.System ? e.SystemKey : e.Key;
if (key is not (Key.LeftAlt or Key.RightAlt) || _drag != DragMode.ResizeCrop) return;
DragTo(_lastPointer, aboutCentre: false);
e.Handled = true;
}
protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
base.OnLostKeyboardFocus(e);
if (!IsKeyboardFocusWithin) _cropSelected = false;
}
protected override void OnMouseWheel(MouseWheelEventArgs e)
@@ -305,12 +438,148 @@ public class PreviewViewport : Control
base.OnQueryCursor(e);
if (e.Handled || _surface is null || !ReferenceEquals(e.OriginalSource, _surface)) return;
var p = e.GetPosition(_surface);
if (_drag == DragMode.Wipe || IsNearDivider(p)) e.Cursor = Cursors.SizeWE;
var handle = _drag switch
{
DragMode.ResizeCrop => _handle,
DragMode.MoveCrop => CropHandle.Inside,
DragMode.None => HitCrop(p),
_ => CropHandle.None,
};
// The same order as a press: a handle, the divider, the inside of the box, then pan.
var onDivider = _drag == DragMode.Wipe || (_drag == DragMode.None && handle is CropHandle.None or CropHandle.Inside && IsNearDivider(p));
if (onDivider) e.Cursor = Cursors.SizeWE;
else if (CursorOf(handle) is { } cursor) e.Cursor = cursor;
else if (IsPannable()) e.Cursor = Cursors.SizeAll;
else return;
e.Handled = true;
}
/// <summary>
/// A press at <paramref name="p"/> (surface DIPs): a crop handle, the wipe divider, inside the crop box, else pan, in
/// that order. A press on the box gives it the keyboard, a press outside gives the arrows back.
/// </summary>
internal void Press(Point p)
{
var handle = HitCrop(p);
_drag = handle is not (CropHandle.None or CropHandle.Inside) ? DragMode.ResizeCrop
: IsNearDivider(p) ? DragMode.Wipe
: handle == CropHandle.Inside ? DragMode.MoveCrop
: DragMode.Pan;
_cropSelected = IsCropDragging;
_dragStart = p;
_lastPointer = p;
_dragOrigin = _view;
if (!IsCropDragging) return;
_handle = handle;
_cropStart = Normalised(EditCrop);
_cropDraft = _cropStart;
var (hx, hy) = ViewportMath.HandleAt(_cropStart, handle);
var (nx, ny) = ToNormalised(p);
_grab = (nx - hx, ny - hy);
_surface?.RefreshOverlay();
}
/// <summary>The pointer moved to <paramref name="p"/> during a drag; <paramref name="aboutCentre"/> (Alt) resizes about the box's centre.</summary>
internal void DragTo(Point p, bool aboutCentre)
{
_lastPointer = p;
switch (_drag)
{
case DragMode.Wipe:
var width = ViewSize.Width;
if (width > 0) SetCurrentValue(WipePositionProperty, p.X / width);
return;
case DragMode.Pan:
UpdateView(ViewportMath.Clamp(ViewportMath.Pan(_dragOrigin, p.X - _dragStart.X, p.Y - _dragStart.Y),
_imageSize.Width, _imageSize.Height, ViewSize.Width, ViewSize.Height));
return;
case DragMode.MoveCrop:
var (x0, y0) = ToNormalised(_dragStart);
var (x1, y1) = ToNormalised(p);
_cropDraft = CropFrame.Move(_cropStart, x1 - x0, y1 - y0);
break;
case DragMode.ResizeCrop:
var (nx, ny) = ToNormalised(p);
_cropDraft = CropFrame.ResizeFrom(_cropStart, _handle, nx - _grab.X, ny - _grab.Y, CropLockRatio, aboutCentre);
break;
default:
return;
}
_surface?.RefreshOverlay();
}
/// <summary>The drag ends (button released, capture lost): a changed crop box becomes <see cref="EditCrop"/> and is reported once.</summary>
internal void EndDrag(bool commit)
{
var (dragged, draft, start) = (IsCropDragging, _cropDraft, _cropStart);
_drag = DragMode.None;
_handle = CropHandle.None;
_cropDraft = null;
if (!dragged) return;
_surface?.RefreshOverlay();
if (commit && draft is { } box && box != start) Complete(box, isNudge: false);
}
/// <summary>Esc: the drag ends without a change.</summary>
internal void CancelDrag()
{
if (_drag == DragMode.None) return;
EndDrag(commit: false);
if (IsMouseCaptured) ReleaseMouseCapture();
}
/// <summary>A key press on the selected box: arrows move it 1 % (Shift 10 %), + and − resize it 2 % about its centre.</summary>
internal bool Nudge(Key key, ModifierKeys modifiers)
{
if (modifiers is not (ModifierKeys.None or ModifierKeys.Shift)) return false;
var step = modifiers == ModifierKeys.Shift ? 10 * NudgeStep : NudgeStep;
var box = Normalised(EditCrop);
RectD? next = key switch
{
Key.Left => CropFrame.Move(box, -step, 0),
Key.Right => CropFrame.Move(box, step, 0),
Key.Up => CropFrame.Move(box, 0, -step),
Key.Down => CropFrame.Move(box, 0, step),
Key.OemPlus or Key.Add => ViewportMath.ResizeCrop(box, ResizeStep, CropLockRatio),
Key.OemMinus or Key.Subtract => ViewportMath.ResizeCrop(box, -ResizeStep, CropLockRatio),
_ => null,
};
if (next is not { } moved) return false;
if (moved != box) Complete(moved, isNudge: true);
return true;
}
private static Cursor? CursorOf(CropHandle handle) => handle switch
{
CropHandle.TopLeft or CropHandle.BottomRight => Cursors.SizeNWSE,
CropHandle.TopRight or CropHandle.BottomLeft => Cursors.SizeNESW,
CropHandle.Left or CropHandle.Right => Cursors.SizeWE,
CropHandle.Top or CropHandle.Bottom => Cursors.SizeNS,
CropHandle.Inside => Cursors.SizeAll,
_ => null,
};
private static RectD Normalised(Rect rect) => rect.IsEmpty ? new RectD(0, 0, 1, 1) : new RectD(rect.X, rect.Y, rect.Width, rect.Height);
private void Complete(RectD box, bool isNudge)
{
var rect = new Rect(box.X, box.Y, box.Width, box.Height);
SetCurrentValue(EditCropProperty, rect);
RaiseEvent(new CropEditEventArgs(CropEditCompletedEvent, rect, isNudge));
}
/// <summary>The crop handle or the inside of the box under <paramref name="p"/>; none while the crop is not editable.</summary>
private CropHandle HitCrop(Point p) => CropEditable && !_imageSize.IsEmpty
? ViewportMath.HitCrop(_view, _imageSize.Width, _imageSize.Height, Normalised(EditCrop), p.X, p.Y, HandleGrip)
: CropHandle.None;
private (double X, double Y) ToNormalised(Point p)
{
if (_imageSize.IsEmpty || !(_view.Scale > 0)) return (0, 0);
var (x, y) = _view.ToImage(p.X, p.Y);
return (x / _imageSize.Width, y / _imageSize.Height);
}
protected override void OnDpiChanged(DpiScale oldDpi, DpiScale newDpi)
{
base.OnDpiChanged(oldDpi, newDpi);
@@ -321,6 +590,19 @@ public class PreviewViewport : Control
private static object CoerceUnit(DependencyObject d, object value) => double.IsNaN((double)value) ? 0.5 : Math.Clamp((double)value, 0, 1);
private static object CoerceRatio(DependencyObject d, object value) => (double)value is > 0 and var ratio && double.IsFinite(ratio) ? ratio : 1.0;
private static void OnCropEditableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var viewport = (PreviewViewport)d;
if (e.NewValue is false)
{
viewport._cropSelected = false;
viewport.CancelDrag();
}
viewport._surface?.RefreshOverlay();
}
private static object CoerceZoom(DependencyObject d, object value) =>
double.IsFinite((double)value) ? Math.Clamp((double)value, MinZoom, MaxZoom) : 1.0;
@@ -434,8 +716,29 @@ public class PreviewViewport : Control
}
/// <summary>The preview seen by UI Automation: an image whose status gives the zoom ("50 %") and whose help text is the shot data.</summary>
public sealed class PreviewViewportAutomationPeer(PreviewViewport owner) : FrameworkElementAutomationPeer(owner)
public sealed class PreviewViewportAutomationPeer(PreviewViewport owner) : FrameworkElementAutomationPeer(owner), IValueProvider
{
/// <summary>The crop box while it is edited on the picture: "x 12 %, y 5 %, size 80 %" (left edge, top edge, share of the largest box).</summary>
public string Value
{
get
{
var viewport = (PreviewViewport)Owner;
var crop = viewport.EditCrop;
if (crop.IsEmpty) return string.Empty;
var (size, _, _) = CropFrame.ToRows(new RectD(crop.X, crop.Y, crop.Width, crop.Height), viewport.CropLockRatio);
return Loc.L("Editor.Preview.CropValue", Percent(crop.X), Percent(crop.Y), Percent(size));
}
}
/// <summary>The box is moved and resized with the pointer and the keys, not typed.</summary>
public bool IsReadOnly => true;
public void SetValue(string value) => throw new ElementNotEnabledException();
public override object GetPattern(PatternInterface patternInterface) =>
patternInterface == PatternInterface.Value && ((PreviewViewport)Owner).CropEditable ? this : base.GetPattern(patternInterface);
protected override AutomationControlType GetAutomationControlTypeCore() => AutomationControlType.Image;
protected override string GetClassNameCore() => nameof(PreviewViewport);
@@ -451,4 +754,6 @@ public sealed class PreviewViewportAutomationPeer(PreviewViewport owner) : Frame
var help = base.GetHelpTextCore();
return string.IsNullOrEmpty(help) ? ((PreviewViewport)Owner).InfoText ?? string.Empty : help;
}
private static string Percent(double share) => Loc.L("Common.Percent", Math.Round(share * 100));
}
+61
View File
@@ -1,3 +1,5 @@
using Chronos.Core.Imaging.Geometry;
namespace Chronos.Ui.Visuals;
/// <summary>Uniform scale and translation from image pixels to view units: view = image × <see cref="Scale"/> + offset.</summary>
@@ -62,6 +64,65 @@ public static class ViewportMath
public static double WheelZoom(double scale, double notches, double minScale, double maxScale) =>
Math.Clamp(scale * Math.Pow(1.25, notches), minScale, maxScale);
/// <summary>The crop box's handles (owner review 1, R5): the four corners, then the four edge midpoints.</summary>
public static IReadOnlyList<CropHandle> CropHandles { get; } =
[
CropHandle.TopLeft, CropHandle.TopRight, CropHandle.BottomRight, CropHandle.BottomLeft,
CropHandle.Top, CropHandle.Right, CropHandle.Bottom, CropHandle.Left,
];
/// <summary>Where <paramref name="handle"/> sits on <paramref name="crop"/>, in the crop's units; its centre for the others.</summary>
public static (double X, double Y) HandleAt(RectD crop, CropHandle handle) => handle switch
{
CropHandle.TopLeft => (crop.X, crop.Y),
CropHandle.Top => (crop.CenterX, crop.Y),
CropHandle.TopRight => (crop.Right, crop.Y),
CropHandle.Right => (crop.Right, crop.CenterY),
CropHandle.BottomRight => (crop.Right, crop.Bottom),
CropHandle.Bottom => (crop.CenterX, crop.Bottom),
CropHandle.BottomLeft => (crop.X, crop.Bottom),
CropHandle.Left => (crop.X, crop.CenterY),
_ => (crop.CenterX, crop.CenterY),
};
/// <summary>
/// What a press at view point (<paramref name="viewX"/>, <paramref name="viewY"/>) takes of the normalised
/// <paramref name="crop"/> over an image of <paramref name="imageWidth"/>×<paramref name="imageHeight"/> pixels shown
/// through <paramref name="view"/>: the nearest handle within <paramref name="grip"/> view units (corners first on a
/// tie), else <see cref="CropHandle.Inside"/> inside the box, else <see cref="CropHandle.None"/>. The grip is in view
/// units, so it is the same on screen whatever the zoom.
/// </summary>
public static CropHandle HitCrop(ViewTransform view, double imageWidth, double imageHeight, RectD crop, double viewX, double viewY, double grip)
{
if (!(view.Scale > 0) || !(imageWidth > 0) || !(imageHeight > 0)) return CropHandle.None;
var best = CropHandle.None;
var nearest = double.MaxValue;
foreach (var handle in CropHandles)
{
var (nx, ny) = HandleAt(crop, handle);
var (x, y) = view.ToView(nx * imageWidth, ny * imageHeight);
var distance = double.Max(double.Abs(x - viewX), double.Abs(y - viewY));
if (distance > grip || distance >= nearest) continue;
(best, nearest) = (handle, distance);
}
if (best != CropHandle.None) return best;
var (left, top) = view.ToView(crop.X * imageWidth, crop.Y * imageHeight);
var (right, bottom) = view.ToView(crop.Right * imageWidth, crop.Bottom * imageHeight);
return viewX >= left && viewX <= right && viewY >= top && viewY <= bottom ? CropHandle.Inside : CropHandle.None;
}
/// <summary>
/// <paramref name="crop"/> resized by <paramref name="step"/> of the largest box of <paramref name="lockRatio"/> (+ grows,
/// − shrinks) about its centre, locked and kept inside the view (the + and − keys).
/// </summary>
public static RectD ResizeCrop(RectD crop, double step, double lockRatio)
{
var largest = CropFrame.Largest(lockRatio);
var height = double.Clamp(crop.Height + step * largest.Height, largest.Height * CropFrame.MinimumShare, largest.Height);
var width = height * lockRatio;
return CropFrame.Clamp(new RectD(crop.CenterX - width / 2, crop.CenterY - height / 2, width, height), lockRatio);
}
private static double ClampAxis(double offset, double size, double view)
{
if (size <= view) return (view - size) / 2;
@@ -4,6 +4,7 @@ using System.IO;
using System.Net.Http;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
@@ -19,6 +20,7 @@ using Chronos.Core.Export;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Core.Render;
using Chronos.Testing;
using Chronos.Ui.Controls;
using Chronos.Ui.Theming;
@@ -424,6 +426,126 @@ public sealed class EditorSmokeTests
}
});
/// <summary>
/// The Camera step edits the crop box on the preview (owner review 1, R5): its mask is on by default and shows the box the
/// render fits, a drag on the picture is one undo step, a run of key presses another, the box is locked to the output
/// proportions with the offsets at 0, and a quarter turn shows the turned view frame.
/// </summary>
[Test]
public Task TheCameraStepEditsTheCropBoxOnThePreview() => ScreenHost.RunAsync(async () =>
{
await using var app = await ScreenSmokeTests.SampleApp.StartAsync();
app.Window.Width = 1200;
app.Window.Height = 788;
var id = await app.Sample.FillEditorAsync();
app.Shell.OpenSequence(id);
await app.Shell.Sequence.OpenAsync(id);
var editor = Assert.NotNull(app.Shell.Sequence.Editor);
try
{
editor.SelectedTab = editor.Exposure;
Assert.False(editor.ShowCropMask, "the other steps keep their mask off");
Assert.False(editor.CropEditable);
Assert.Equal(Loc.L("Tooltips.Editor.Preview.View"), editor.PreviewToolTip);
editor.SelectedTab = editor.Tabs.OfType<CameraTab>().Single();
Assert.True(editor.ShowCropMask, "on by default on the Camera step");
Assert.True(editor.CropEditable);
Assert.Equal(Loc.L("Tooltips.Editor.Preview.CropEdit"), editor.PreviewToolTip);
Assert.True(await editor.WhenIdleAsync(TimeSpan.FromSeconds(60)));
await SettleAsync(app.Window);
var (width, height) = (editor.Session.Content.SourceWidth, editor.Session.Content.SourceHeight);
RenderGeometry Geometry() => RenderGeometry.Create(editor.Session.Project.Camera, ExportResolver.Resolve(editor.Session.Project.Export), width, height);
static Rect RectOf(Chronos.Core.Imaging.Geometry.RectD r) => new(r.X, r.Y, r.Width, r.Height);
Assert.Equal(RectOf(Geometry().EditableCrop), editor.EditCrop, "the mask is the box the render fits");
Assert.Equal(editor.EditCrop, editor.CropRect);
var viewport = Find<PreviewViewport>(app.Window).Single(v => v.IsVisible);
Assert.True(viewport.CropEditable);
Assert.Equal(editor.EditCrop, viewport.EditCrop);
Assert.Equal(editor.CropLockRatio, viewport.CropLockRatio);
Assert.Equal(editor.PreviewToolTip, viewport.ToolTip);
// The bottom-right handle dragged in: one undo step, the box locked, the offsets at 0.
Point At(double nx, double ny)
{
var (x, y) = viewport.View.ToView(nx * viewport.ImageSize.Width, ny * viewport.ImageSize.Height);
return new Point(x, y);
}
var before = editor.Session.Project.Camera;
var box = viewport.EditCrop;
viewport.Press(At(box.Right, box.Bottom));
viewport.DragTo(At(box.X + box.Width * 0.7, box.Y + box.Height * 0.7), aboutCentre: false);
Assert.Same(before, editor.Session.Project.Camera, "nothing changes while dragging");
viewport.EndDrag(commit: true);
var cropped = editor.Session.Project.Camera;
var crop = Assert.NotNull(cropped.Crop);
Assert.Approximately(0, cropped.OffsetX, 0);
Assert.Approximately(0, cropped.OffsetY, 0);
Assert.Approximately(editor.CropLockRatio, crop[2] / crop[3], 1e-9, "locked to the proportions");
Assert.Less(crop[3], box.Height);
Assert.Approximately(box.X, crop[0], 1e-9, "anchored at the opposite corner");
Assert.Equal(new Rect(crop[0], crop[1], crop[2], crop[3]), editor.EditCrop);
// Two nudges are one more step.
Assert.True(viewport.IsCropSelected, "the press took the box");
Assert.True(viewport.Nudge(Key.Right, ModifierKeys.None));
Assert.True(viewport.Nudge(Key.Down, ModifierKeys.None));
editor.Session.EndEditRun();
var nudged = Assert.NotNull(editor.Session.Project.Camera.Crop);
Assert.Approximately(crop[0] + 0.01, nudged[0], 1e-9);
Assert.Approximately(crop[1] + 0.01, nudged[1], 1e-9);
editor.UndoCommand.Execute(null);
Assert.SequenceEqual(crop, editor.Session.Project.Camera.Crop!, "one undo takes both nudges back");
editor.UndoCommand.Execute(null);
Assert.SequenceEqual(before.Crop ?? [], editor.Session.Project.Camera.Crop ?? [], "and one more the drag");
editor.RedoCommand.Execute(null);
Assert.SequenceEqual(crop, editor.Session.Project.Camera.Crop!);
// The straighten grid shows while the slider is used, on the Camera step only.
editor.IsStraightening = true;
await SettleAsync(app.Window);
Assert.True(viewport.ShowGrid);
editor.IsStraightening = false;
Assert.False(viewport.ShowGrid);
// A quarter turn: the box waits for the turned picture, then maps onto it.
var ratio = editor.CropLockRatio;
editor.Session.Edit(p => p with { Camera = p.Camera with { Rotate = 90, Crop = null } });
Assert.Equal(ratio, editor.CropLockRatio, "the box of the picture on screen until the turned one comes");
Assert.True(await editor.WhenIdleAsync(TimeSpan.FromSeconds(60)));
var picture = (System.Windows.Media.Imaging.BitmapSource)editor.PreviewImage!;
Assert.Less(picture.PixelWidth, picture.PixelHeight, "the landscape source shows turned");
Assert.Equal(Geometry().LockRatio, editor.CropLockRatio);
Assert.Equal(RectOf(Geometry().EditableCrop), editor.EditCrop);
editor.UndoCommand.Execute(null);
await editor.Session.FlushAsync();
Assert.True(await editor.WhenIdleAsync(TimeSpan.FromSeconds(60)));
// Read-only and newer-format projects are drawn, never edited.
var content = EditorFixture.Content(EditorFixture.Sunset);
foreach (var (source, reason) in new[]
{
(new FakeSequenceSource(EditorFixture.Sunset, content) { IsReadOnly = true }, "read-only"),
(new FakeSequenceSource(EditorFixture.Sunset, content with { Project = content.Project with { Version = ProjectSettings.CurrentVersion + 1 } }), "newer format"),
})
{
var locked = new EditorViewModel(app.Shell, new EditorSession(source, source.Content, []), [], EncoderHost.Client);
await using (locked)
{
locked.SelectedTab = locked.Tabs.OfType<CameraTab>().Single();
Assert.True(locked.ShowCropMask, reason);
Assert.False(locked.CropEditable, reason);
Assert.False(locked.CommitCropCommand.CanExecute(new Rect(0.1, 0.1, 0.5, 0.5)), reason);
Assert.Equal(Loc.L("Tooltips.Editor.Preview.View"), locked.PreviewToolTip);
}
}
}
finally
{
await app.Shell.Sequence.CloseAsync();
}
});
/// <summary>
/// At the 1200 px minimum beside the 200 px rail (owner review 1, R1), a server sequence's job keeps its Cancel whole in every
/// language, also read-only (the longest place line): the title and the summary give way first (BRIEF §11.5, cancel always stays).
@@ -1,6 +1,12 @@
using System.IO;
using Chronos.App.Editor;
using Chronos.Core.Export;
using Chronos.Core.Imaging;
using Chronos.Core.Project;
using Chronos.Core.Render;
using Chronos.Core.Render.Diagnostics;
using Chronos.Core.Video;
using Chronos.Core.Video.Managed;
using Chronos.Testing;
namespace Chronos.App.Tests.Editor;
@@ -183,6 +189,133 @@ public sealed class PreviewRendererTests
Assert.True(waveform.Density.Any(d => d > 0));
}
[Test]
public async Task OutputFramingFollowsChosenPortraitProportions()
{
// The chosen proportions win (owner review 1, R5): the square preset's box takes a 9:16 picture.
var (renderer, _) = Create();
var project = new ProjectSettings();
project = project with
{
Export = project.Export with { Preset = project.Export.Preset.WithManual("square-1440") },
Camera = project.Camera with { Aspect = project.Camera.Aspect.WithManual("9:16") },
};
using var output = await renderer.RenderAsync(new PreviewRequest(0, project, 80, PreviewFraming.Output), null, CancellationToken.None);
Assert.Equal(45, output!.Corrected.Width, "810 × 1440 at the preview's long edge");
Assert.Equal(80, output.Corrected.Height);
using var source = await renderer.RenderAsync(new PreviewRequest(0, project, 96, PreviewFraming.Source), null, CancellationToken.None);
Assert.Equal(96, source!.Corrected.Width, "the whole frame");
Assert.Approximately(9.0 / 16 * 64 / 96, source.LockRatio, 1e-12, "the box's ratio in normalised units");
Assert.Approximately(1, source.EditCrop.Height, 1e-9, "the largest 9:16 box of the 3:2 frame");
Assert.Approximately(source.LockRatio, source.EditCrop.Width, 1e-9);
Assert.Approximately(source.EditCrop.Width, source.Crop.Width, 0.02, "the output's footprint is the box");
Assert.Approximately(source.EditCrop.X, source.Crop.X, 0.02);
}
[Test]
public async Task SourceFramingShowsTheTurnedViewFrame()
{
var (renderer, _) = Create();
var plain = new ProjectSettings();
var turned = plain with { Camera = plain.Camera with { Rotate = 90 } };
using var frame = await renderer.RenderAsync(new PreviewRequest(5, plain, 96, PreviewFraming.Source), null, CancellationToken.None);
using var view = await renderer.RenderAsync(new PreviewRequest(5, turned, 96, PreviewFraming.Source), null, CancellationToken.None);
Assert.Equal((96, 64), (frame!.Corrected.Width, frame.Corrected.Height));
Assert.Equal((64, 96), (view!.Corrected.Width, view.Corrected.Height), "a quarter turn swaps the size");
Assert.Equal((64, 96), (view.Original.Width, view.Original.Height));
Assert.Equal((64, 96), view.ViewSize);
Assert.Equal((96, 64), frame.ViewSize);
// Turned a quarter clockwise, exactly: the view's (x, y) is the frame's (y, H − 1 − x).
for (var c = 0; c < 3; c++)
{
var a = frame.Original.Plane(c);
var b = view.Original.Plane(c);
for (var y = 0; y < 96; y += 7)
{
for (var x = 0; x < 64; x += 5) Assert.Approximately(a[(63 - x) * 96 + y], b[y * 64 + x], 1e-6, $"channel {c} at ({x}, {y})");
}
}
}
[Test]
public async Task TheMaskIsTheEditableCropOfTheViewFrame()
{
var (renderer, _) = Create();
var project = new ProjectSettings();
project = project with
{
Camera = project.Camera with
{
Aspect = project.Camera.Aspect.WithManual("1:1"),
Crop = [0.1, 0.05, 0.6, 0.8],
OffsetX = 0.5,
Rotate = 270,
Straighten = 4,
},
};
using var frame = await renderer.RenderAsync(new PreviewRequest(2, project, 96, PreviewFraming.Source), null, CancellationToken.None);
var geometry = RenderGeometry.Create(project.Camera, ExportResolver.Resolve(project.Export), EditorFixture.Sunset.Options.Width, EditorFixture.Sunset.Options.Height);
Assert.Equal(geometry.EditableCrop, frame!.EditCrop, "the box the render fits, the same on every frame");
Assert.Equal(geometry.LockRatio, frame.LockRatio);
Assert.Approximately(frame.EditCrop.Width / frame.EditCrop.Height * frame.Corrected.Width / frame.Corrected.Height, 1, 0.03, "square on the picture");
Assert.Approximately(frame.EditCrop.X, frame.Crop.X, 0.02, "without stabilisation or pose, the output's footprint is the box");
Assert.Approximately(frame.EditCrop.Width, frame.Crop.Width, 0.02);
Assert.Approximately(frame.EditCrop.Height, frame.Crop.Height, 0.02);
Assert.Equal((64, 96), (frame.Corrected.Width, frame.Corrected.Height));
}
[Test]
public async Task ALookChangesOnlyTheCorrectedPicture()
{
var (renderer, _) = Create();
var plain = Deflickered() with { Exposure = new ProjectSettings().Exposure with { HighlightKnee = null } };
var look = new LookSettings { Filter = "sepia", Strength = 0.6 };
foreach (var framing in new[] { PreviewFraming.Output, PreviewFraming.Source })
{
using var before = await renderer.RenderAsync(new PreviewRequest(4, plain, 64, framing), null, CancellationToken.None);
using var after = await renderer.RenderAsync(new PreviewRequest(4, plain with { Look = look }, 64, framing), null, CancellationToken.None);
Assert.True(before!.Original.Data.SequenceEqual(after!.Original.Data), $"{framing}: the original side has no look");
Assert.False(before.Corrected.Data.SequenceEqual(after.Corrected.Data), $"{framing}: the corrected side has it");
FrameFinish.Apply(before.Corrected, look, null);
Assert.True(before.Corrected.Data.SequenceEqual(after.Corrected.Data), $"{framing}: the render's finish");
}
}
[Test]
public async Task OutputFramingMatchesTheRenderTurnedStraightenedAndWithALook()
{
var project = new ProjectSettings();
project = project with
{
Exposure = project.Exposure with { HighlightKnee = null },
Export = project.Export with { Preset = project.Export.Preset.WithManual("png-sequence") },
Camera = project.Camera with { Rotate = 90, Straighten = 3 },
Look = new LookSettings { Filter = "sepia", Strength = 0.6 },
};
var (renderer, _) = Create();
using var preview = await renderer.RenderAsync(new PreviewRequest(0, project, 96, PreviewFraming.Output), null, CancellationToken.None);
Assert.Equal((64, 96), (preview!.Corrected.Width, preview.Corrected.Height), "the turned frame at the source's size");
var capture = new CapturingEncoder();
var pixels = new SyntheticFrameSource(EditorFixture.Sunset);
var folder = Path.Combine(Path.GetTempPath(), "chronos-preview-" + Guid.NewGuid().ToString("N"));
try
{
var input = new RenderInput { Source = pixels, Frames = pixels.Metadata(), Project = project };
await new RenderPipeline([capture, new ManagedEncoderProvider()]).RenderAsync(input, new RenderTarget(ExportResolver.Resolve(project.Export), folder));
}
finally
{
if (Directory.Exists(folder)) Directory.Delete(folder, recursive: true);
}
var rendered = capture.Frames[0];
Assert.Equal((preview.Corrected.Width, preview.Corrected.Height), (rendered.Width, rendered.Height));
Assert.Greater(Psnr(rendered, preview.Corrected), 40, "the preview is the render's frame");
foreach (var frame in capture.Frames) frame.Dispose();
}
/// <summary>A sunset project as the advisor leaves it: deflicker, holy grail, white balance and stabilisation on.</summary>
private static ProjectSettings Sunset()
{
@@ -207,6 +340,49 @@ public sealed class PreviewRendererTests
return Math.Log2(sum / n);
}
/// <summary>Peak signal-to-noise ratio of the 8-bit sRGB encodings.</summary>
private static double Psnr(ImageBuffer expected, ImageBuffer actual)
{
var a = PixelConvert.ToInterleaved8(expected, 3);
var b = PixelConvert.ToInterleaved8(actual, 3);
double sum = 0;
for (var i = 0; i < a.Length; i++) sum += (a[i] - b[i]) * (a[i] - b[i]);
return sum == 0 ? double.PositiveInfinity : 10 * Math.Log10(255.0 * 255 / (sum / a.Length));
}
/// <summary>A PNG-sequence encoder that keeps a copy of every frame instead of writing it.</summary>
private sealed class CapturingEncoder : IVideoEncoderProvider
{
private static readonly EncoderCapability Capability = new(VideoCodec.PngSequence, "Capture", false, 8192, 8192, true);
public string Name => "capture";
public IReadOnlyList<EncoderCapability> Capabilities { get; } = [Capability];
public List<ImageBuffer> Frames { get; } = [];
public bool CanEncode(EncodeSettings settings) => settings.Codec == VideoCodec.PngSequence;
public IVideoEncoderSession Open(EncodeSettings settings) => new Session(this);
private sealed class Session(CapturingEncoder owner) : IVideoEncoderSession
{
public string EncoderName => Capability.Name;
public bool IsHardware => false;
public void WriteFrame(ImageBuffer frame, long duration90k) => owner.Frames.Add(frame.Clone());
public void Complete()
{
}
public void Dispose()
{
}
}
}
/// <summary>Standard deviation of the frame-to-frame differences (flicker, not the slow ramp).</summary>
private static double Spread(List<double> values)
{
@@ -1,3 +1,4 @@
using Chronos.Core.Imaging.Geometry;
using Chronos.Testing;
using Chronos.Ui.Visuals;
@@ -69,4 +70,75 @@ public sealed class ViewportMathTests
Assert.Approximately(Math.Round(10.4 * 1.5), t.OffsetX * 1.5, 1e-9);
Assert.Approximately(Math.Round(-3.1 * 1.5), t.OffsetY * 1.5, 1e-9);
}
[Test]
public void ACropPressTakesAHandleWithinTheGripThenTheInsideAtEveryZoom()
{
// A 400×300 image with the box [0.25, 0.2, 0.5, 0.6]: the grip is 6 view units whatever the zoom.
var crop = new RectD(0.25, 0.2, 0.5, 0.6);
foreach (var view in new[] { new ViewTransform(0.25, 40, 10), new ViewTransform(1, -30, 5), new ViewTransform(4, -500, -300) })
{
CropHandle Hit(double nx, double ny, double dx, double dy)
{
var (x, y) = view.ToView(nx * 400, ny * 300);
return ViewportMath.HitCrop(view, 400, 300, crop, x + dx, y + dy, 6);
}
var zoom = $"scale {view.Scale}";
Assert.Equal(CropHandle.TopLeft, Hit(0.25, 0.2, -5, 4), zoom);
Assert.Equal(CropHandle.BottomRight, Hit(0.75, 0.8, 6, -6), zoom);
Assert.Equal(CropHandle.Top, Hit(0.5, 0.2, 3, -2), zoom);
Assert.Equal(CropHandle.Left, Hit(0.25, 0.5, 2, 0), zoom);
Assert.Equal(CropHandle.Inside, Hit(0.25, 0.5, 0, 7), "beside a handle, inside the box: a move");
Assert.Equal(CropHandle.None, Hit(0.25, 0.5, -7, 0), "outside the box and the grip: a pan");
Assert.Equal(CropHandle.Inside, Hit(0.5, 0.5, 0, 0), zoom);
Assert.Equal(CropHandle.None, Hit(0.1, 0.1, 0, 0), zoom);
}
Assert.Equal(CropHandle.None, ViewportMath.HitCrop(new ViewTransform(0, 0, 0), 400, 300, crop, 0, 0, 6), "no view yet");
}
[Test]
public void TheNearestHandleWinsOnATinyBox()
{
// At 1/4 zoom a box of 40×30 image pixels is 10×7.5 view units: every handle is within the grip of the centre.
var view = new ViewTransform(0.25, 0, 0);
var crop = new RectD(0.45, 0.45, 0.1, 0.1);
var (x, y) = view.ToView(0.55 * 400, 0.55 * 300);
Assert.Equal(CropHandle.BottomRight, ViewportMath.HitCrop(view, 400, 300, crop, x - 1, y - 1, 6));
var (cx, cy) = view.ToView(0.5 * 400, 0.45 * 300);
Assert.Equal(CropHandle.Top, ViewportMath.HitCrop(view, 400, 300, crop, cx, cy + 0.5, 6));
}
[Test]
public void TheHandlesSitOnTheCornersAndTheEdgeMidpoints()
{
var crop = new RectD(0.1, 0.2, 0.4, 0.6);
Assert.Count(8, ViewportMath.CropHandles);
Assert.Within(0.1, 0.2, ViewportMath.HandleAt(crop, CropHandle.TopLeft).X, ViewportMath.HandleAt(crop, CropHandle.TopLeft).Y, 1e-12);
Assert.Within(0.3, 0.8, ViewportMath.HandleAt(crop, CropHandle.Bottom).X, ViewportMath.HandleAt(crop, CropHandle.Bottom).Y, 1e-12);
Assert.Within(0.5, 0.5, ViewportMath.HandleAt(crop, CropHandle.Right).X, ViewportMath.HandleAt(crop, CropHandle.Right).Y, 1e-12);
Assert.Within(0.3, 0.5, ViewportMath.HandleAt(crop, CropHandle.Inside).X, ViewportMath.HandleAt(crop, CropHandle.Inside).Y, 1e-12);
}
[Test]
public void PlusAndMinusResizeAboutTheCentreLockedAndWithinTheLimits()
{
const double ratio = 0.5;
var largest = CropFrame.Largest(ratio);
var box = CropFrame.FromRows(0.5, 0, 0, ratio);
var grown = ViewportMath.ResizeCrop(box, 0.02, ratio);
Assert.Approximately(box.Height + 0.02 * largest.Height, grown.Height, 1e-12);
Assert.Approximately(ratio, grown.Width / grown.Height, 1e-12);
Assert.Within(box.CenterX, box.CenterY, grown.CenterX, grown.CenterY, 1e-12);
var smallest = CropFrame.FromRows(CropFrame.MinimumShare, 0, 0, ratio);
Assert.Approximately(smallest.Height, ViewportMath.ResizeCrop(smallest, -0.02, ratio).Height, 1e-12, "not below the minimum");
var whole = ViewportMath.ResizeCrop(largest, 0.02, ratio);
Assert.Approximately(largest.Height, whole.Height, 1e-12, "not beyond the largest box");
// At an edge the grown box moves back inside the view.
var corner = CropFrame.FromRows(0.9, 1, 1, ratio);
var pushed = ViewportMath.ResizeCrop(corner, 0.08, ratio);
Assert.LessOrEqual(pushed.Right, 1 + 1e-12);
Assert.LessOrEqual(pushed.Bottom, 1 + 1e-12);
}
}
@@ -278,6 +278,217 @@ public sealed class VisualsSmokeTests
}
});
[Test]
public void ACropDragMovesThenResizesLockedAndIsReportedOncePerDrag() => WpfHost.Run(() =>
{
var preview = CropPreview(new Rect(0.3, 0.2, 0.3, 0.6), ratio: 0.5);
Render(preview, 800, 450);
var edits = new List<CropEditEventArgs>();
preview.CropEditCompleted += (_, e) => edits.Add(e);
// Inside the box: a move; only the overlay follows the pointer until the button is released.
preview.Press(At(preview, 0.45, 0.5));
Assert.Equal(PreviewViewport.DragMode.MoveCrop, preview.Dragging);
preview.DragTo(At(preview, 0.55, 0.55), aboutCentre: false);
Assert.Empty(edits, "nothing is reported while dragging");
Assert.Approximately(0.4, preview.ShownCrop.X, 1e-6, "the overlay follows");
Assert.Approximately(0.3, preview.EditCrop.X, 0, "the box itself waits for the release");
preview.EndDrag(commit: true);
var moved = Assert.Contains(edits, _ => true).Crop;
Assert.Within(0.4, 0.25, moved.X, moved.Y, 1e-6);
Assert.False(edits[0].IsNudge);
Assert.Equal(moved, preview.EditCrop, "the finished box is the edited one");
// The bottom-right handle, dragged far outside: anchored at the top-left corner, locked, clamped to the view.
preview.Press(At(preview, 0.7, 0.85));
Assert.Equal(PreviewViewport.DragMode.ResizeCrop, preview.Dragging);
preview.DragTo(At(preview, 1.5, 1.5), aboutCentre: false);
preview.EndDrag(commit: true);
Assert.Count(2, edits);
var resized = edits[1].Crop;
Assert.Within(0.4, 0.25, resized.X, resized.Y, 1e-9, "anchored at the opposite corner");
Assert.Approximately(0.5, resized.Width / resized.Height, 1e-9, "the ratio stays locked");
Assert.Approximately(1, resized.Bottom, 1e-9, "as large as the view lets it be");
Assert.LessOrEqual(resized.Right, 1);
// Alt resizes about the centre.
preview.Press(At(preview, resized.Right, resized.Bottom));
preview.DragTo(At(preview, resized.Right - 0.05, resized.Bottom - 0.1), aboutCentre: true);
preview.EndDrag(commit: true);
var centred = edits[2].Crop;
Assert.Within(resized.X + resized.Width / 2, resized.Y + resized.Height / 2, centred.X + centred.Width / 2, centred.Y + centred.Height / 2, 1e-9);
Assert.Approximately(0.5, centred.Width / centred.Height, 1e-9);
// A press released where it started, and a pan outside the box, report nothing.
preview.Press(At(preview, centred.X + centred.Width / 2, centred.Y + centred.Height / 2));
preview.EndDrag(commit: true);
preview.Press(At(preview, 0.05, 0.05));
Assert.Equal(PreviewViewport.DragMode.Pan, preview.Dragging);
preview.DragTo(At(preview, 0.1, 0.1), aboutCentre: false);
preview.EndDrag(commit: true);
Assert.Count(3, edits);
});
[Test]
public void APressTakesAHandleThenTheDividerThenTheBoxThenPansAtEveryZoom() => WpfHost.Run(() =>
{
var preview = CropPreview(new Rect(0.4, 0.4, 0.2, 0.2), ratio: 1);
preview.OriginalSource = Bitmap(2000, 1500);
preview.IsCompareEnabled = true;
preview.WipePosition = 0.5;
Render(preview, 800, 450);
var surface = Assert.NotNull(FindChild<PreviewSurface>(preview));
foreach (var zoom in new double?[] { null, 1, 2 })
{
if (zoom is { } z) preview.Zoom = z;
var label = zoom is null ? "fit" : $"zoom {zoom}";
// The image column under the divider, and a box of 0.06 × 0.12 around it (corner handles well away).
var divider = surface.ActualWidth / 2;
var nx = preview.View.ToImage(divider, 0).X / 2000;
var y = At(preview, 0, 0.5).Y;
PreviewViewport.DragMode Take(Rect box, double x)
{
preview.EditCrop = box;
preview.Press(new Point(x, y));
var mode = preview.Dragging;
preview.EndDrag(commit: false);
return mode;
}
Assert.Equal(PreviewViewport.DragMode.ResizeCrop, Take(new Rect(nx, 0.44, 0.06, 0.12), divider), $"{label}: a handle on the divider");
Assert.Equal(PreviewViewport.DragMode.Wipe, Take(new Rect(nx - 0.03, 0.44, 0.06, 0.12), divider), $"{label}: the divider inside the box");
Assert.Equal(PreviewViewport.DragMode.MoveCrop, Take(new Rect(nx - 0.03, 0.44, 0.06, 0.12), At(preview, nx + 0.012, 0.5).X), $"{label}: inside");
Assert.Equal(PreviewViewport.DragMode.Pan, Take(new Rect(nx - 0.03, 0.44, 0.06, 0.12), At(preview, nx + 0.06, 0.5).X), $"{label}: outside");
}
// Not editable: no handle and no box to take, the divider and pan only.
preview.CropEditable = false;
preview.EditCrop = new Rect(0.4, 0.4, 0.2, 0.2);
preview.Press(At(preview, 0.4, 0.4));
Assert.Equal(PreviewViewport.DragMode.Pan, preview.Dragging);
preview.EndDrag(commit: false);
});
[Test]
public void EscCancelsADragAndTheKeysNudgeTheBoxOnceItIsTaken() => WpfHost.Run(() =>
{
var preview = CropPreview(new Rect(0.3, 0.2, 0.3, 0.6), ratio: 0.5);
var window = OffScreen(preview, 800, 450);
try
{
var edits = new List<CropEditEventArgs>();
preview.CropEditCompleted += (_, e) => edits.Add(e);
Assert.False(Press(preview, Key.Right), "until the box is taken, the arrows step frames");
// Esc cancels a drag: the box stays, nothing is reported.
preview.Press(At(preview, 0.45, 0.5));
preview.DragTo(At(preview, 0.6, 0.6), aboutCentre: false);
Assert.True(Press(preview, Key.Escape));
Assert.Equal(PreviewViewport.DragMode.None, preview.Dragging);
Assert.Equal(new Rect(0.3, 0.2, 0.3, 0.6), preview.EditCrop);
Assert.Equal(new Rect(0.3, 0.2, 0.3, 0.6), preview.ShownCrop);
Assert.Empty(edits);
// The press took the box: the arrows move it 1 %, + and − resize it about its centre, each reported as a nudge.
Assert.True(preview.IsCropSelected);
Assert.True(Press(preview, Key.Right));
Assert.Count(1, edits);
Assert.True(edits[0].IsNudge);
Assert.Approximately(0.31, preview.EditCrop.X, 1e-9);
Assert.True(preview.Nudge(Key.Down, ModifierKeys.Shift), "Shift moves 10 %");
Assert.Approximately(0.3, preview.EditCrop.Y, 1e-9);
var before = preview.EditCrop;
Assert.True(Press(preview, Key.OemMinus));
var shrunk = preview.EditCrop;
Assert.Less(shrunk.Height, before.Height);
Assert.Approximately(0.5, shrunk.Width / shrunk.Height, 1e-9);
Assert.Within(before.X + before.Width / 2, before.Y + before.Height / 2, shrunk.X + shrunk.Width / 2, shrunk.Y + shrunk.Height / 2, 1e-9);
Assert.Count(3, edits);
// Esc gives the arrows back; Enter takes the box from the keyboard.
Assert.True(Press(preview, Key.Escape));
Assert.False(preview.IsCropSelected);
Assert.False(Press(preview, Key.Left));
Assert.True(Press(preview, Key.Enter));
Assert.True(Press(preview, Key.Left));
Assert.Count(4, edits);
// A box that can no longer change gives the keys back.
preview.CropEditable = false;
Assert.False(preview.IsCropSelected);
Assert.False(Press(preview, Key.Right));
}
finally
{
window.Close();
}
});
[Test]
public void TheEditableBoxDrawsItsHandlesAndScreenReadersReadIt() => WpfHost.Run(() =>
{
var card = ColorOf("Brush.Card");
var preview = CropPreview(new Rect(0.25, 0.2, 0.5, 0.6), ratio: 0.5 / 0.6);
Render(preview, 800, 450);
var surface = Assert.NotNull(FindChild<PreviewSurface>(preview));
Color HandlePixel(double nx, double ny)
{
var bitmap = Render(preview, 800, 450);
var (x, y) = preview.View.ToView(nx * 2000, ny * 1500);
var p = surface.TranslatePoint(new Point(x, y), preview);
return PixelAt(bitmap, (int)Math.Round(p.X), (int)Math.Round(p.Y));
}
foreach (var (nx, ny) in new[] { (0.25, 0.2), (0.75, 0.8), (0.5, 0.2), (0.75, 0.5) })
Assert.True(Near(HandlePixel(nx, ny), card, 8), $"a Card handle at ({nx}, {ny})");
preview.CropMaskMode = CropMaskMode.ShowResult;
Assert.True(Near(HandlePixel(0.25, 0.8), card, 8), "the handles draw in either mask mode");
var peer = UIElementAutomationPeer.CreatePeerForElement(preview);
var value = Assert.NotNull(peer.GetPattern(PatternInterface.Value) as System.Windows.Automation.Provider.IValueProvider);
Assert.Equal(Loc.L("Editor.Preview.CropValue", Loc.L("Common.Percent", 25), Loc.L("Common.Percent", 20), Loc.L("Common.Percent", 60)), value.Value);
Assert.True(value.IsReadOnly);
preview.CropMaskMode = CropMaskMode.ShowLoss;
preview.CropEditable = false;
Assert.False(Near(HandlePixel(0.25, 0.2), card, 8), "no handles while the box cannot change");
Assert.Null(peer.GetPattern(PatternInterface.Value), "and no value to read");
});
private static PreviewViewport CropPreview(Rect crop, double ratio) => new()
{
Source = Bitmap(2000, 1500),
ShowCropMask = true,
CropEditable = true,
CropLockRatio = ratio,
EditCrop = crop,
};
/// <summary>The surface point of the normalised image point (<paramref name="nx"/>, <paramref name="ny"/>) of a 2000 × 1500 preview.</summary>
private static Point At(PreviewViewport preview, double nx, double ny)
{
var (x, y) = preview.View.ToView(nx * 2000, ny * 1500);
return new Point(x, y);
}
/// <summary>A window off screen around <paramref name="element"/>, so it has a presentation source for key events.</summary>
private static Window OffScreen(FrameworkElement element, double width, double height)
{
var window = new Window
{
Content = new Border { Child = element },
Width = width,
Height = height,
WindowStyle = WindowStyle.None,
WindowStartupLocation = WindowStartupLocation.Manual,
Left = -20000,
Top = -20000,
ShowActivated = false,
ShowInTaskbar = false,
};
window.Show();
window.UpdateLayout();
return window;
}
private static IEnumerable<(string Name, FrameworkElement Element, double Width, double Height)> Samples()
{
var n = 120_000;
@@ -342,6 +553,15 @@ public sealed class VisualsSmokeTests
ShowCropMask = true,
CropRect = new Rect(0.1, 0.1, 0.8, 0.8),
}, 800, 450);
yield return ("PreviewViewport.CropEdit", new PreviewViewport
{
Source = Bitmap(2000, 1500),
ShowCropMask = true,
CropEditable = true,
CropLockRatio = 0.5,
EditCrop = new Rect(0.3, 0.2, 0.3, 0.6),
ShowGrid = true,
}, 800, 450);
yield return ("SequenceCard", SampleCard(), 236, 208);
yield return ("SegmentedControl", SampleSegments(), 120, 32);
@@ -438,8 +658,13 @@ public sealed class VisualsSmokeTests
return false;
}
private static void Press(UIElement target, Key key) =>
target.RaiseEvent(new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(target)!, 0, key) { RoutedEvent = Keyboard.KeyDownEvent });
/// <summary>Raises a key press on <paramref name="target"/>; true when something handled it.</summary>
private static bool Press(UIElement target, Key key)
{
var args = new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(target)!, 0, key) { RoutedEvent = Keyboard.KeyDownEvent };
target.RaiseEvent(args);
return args.Handled;
}
private static RenderTargetBitmap Render(FrameworkElement element, double width, double height)
{