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:
1 parent
aaf9c0bba7
commit
6f15717463
13 files changed
+1298
-65
No files matched your search
@@ -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)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user