using Chronos.Core.Imaging.Geometry; using Chronos.Testing; namespace Chronos.Core.Tests.Geometry; public sealed class CropFrameTests { /// 9:16 on a 6000×4000 view, in normalised units: 0.5625 × 4000 / 6000. private const double Portrait = 0.375; [Test] public void TheLargestBoxIsCentredAndLocked() { AssertBox(new RectD(0.3125, 0, 0.375, 1), CropFrame.Largest(Portrait)); AssertBox(new RectD(0, 0, 1, 1), CropFrame.Largest(1)); AssertBox(new RectD(0, 0.25, 1, 0.5), CropFrame.Largest(2)); Assert.Throws(() => CropFrame.Largest(0)); Assert.Throws(() => CropFrame.Largest(double.NaN)); } [Test] public void StoredCropsReadAsTheRenderReadsThem() { AssertBox(new RectD(0, 0, 1, 1), CropFrame.Of(null)); AssertBox(new RectD(0, 0, 1, 1), CropFrame.Of([0.1, 0.2])); AssertBox(new RectD(0, 0, 1, 1), CropFrame.Of([0.1, double.NaN, 0.3, 0.3])); AssertBox(new RectD(0, 0, 1, 1), CropFrame.Of([0.5, 0.5, 0.0001, 0.5])); AssertBox(new RectD(0.5, 0, 0.5, 0.25), CropFrame.Of([0.5, -1, 0.8, 0.25])); Assert.True(CropFrame.IsWhole(null)); Assert.True(CropFrame.IsWhole([0, 0, 1, 1])); Assert.False(CropFrame.IsWhole([0, 0, 0.9, 1])); Assert.Null(CropFrame.ToCrop(new RectD(0, 0, 1, 1))); Assert.SequenceEqual([0.25, 0.125, 0.5, 0.5], CropFrame.ToCrop(new RectD(0.25, 0.125, 0.5, 0.5))!); // Written on a 12-decimal grid, never −0. Assert.SequenceEqual([0.333333333333, 0, 0.5, 0.5], CropFrame.ToCrop(new RectD(1 / 3.0, -0.0, 0.5, 0.5))!); Assert.False(double.IsNegative(CropFrame.ToCrop(new RectD(-0.0, 0.2, 0.5, 0.5))![0])); } [Test] public void LockedBakesTheOffsetsOfALegacyCrop() { // A free crop of the whole view with the box pushed right: the render's FitCrop, normalised. AssertBox(new RectD(0.625, 0, 0.375, 1), CropFrame.Locked(null, Portrait, offsetX: 1)); AssertBox(new RectD(0.3125, 0, 0.375, 1), CropFrame.Locked(null, Portrait, double.NaN, double.NaN)); var box = CropFrame.Locked([0.2, 0.1, 0.4, 0.8], Portrait, -1, 0); AssertBox(new RectD(0.2, 0.1, 0.3, 0.8), box); // A box the editor wrote comes back unchanged. AssertBox(box, CropFrame.Locked(CropFrame.ToCrop(box), Portrait)); } [Test] public void MovingStaysInsideAndKeepsTheSize() { var box = new RectD(0.3, 0.2, 0.3, 0.4); AssertBox(new RectD(0.4, 0.15, 0.3, 0.4), CropFrame.Move(box, 0.1, -0.05)); AssertBox(new RectD(0.7, 0, 0.3, 0.4), CropFrame.Move(box, 2, -1)); AssertBox(new RectD(0, 0.6, 0.3, 0.4), CropFrame.Move(box, -5, 5)); } [Theory] [Case(CropHandle.BottomRight, 0.9, 0.95, false)] [Case(CropHandle.TopLeft, 0.0, 0.0, false)] [Case(CropHandle.TopRight, 2.0, -3.0, false)] [Case(CropHandle.Left, 0.31, 0.5, false)] [Case(CropHandle.Top, 0.5, -1.0, false)] [Case(CropHandle.Right, 0.4, 0.5, false)] [Case(CropHandle.Bottom, 0.5, 0.3, false)] [Case(CropHandle.BottomLeft, 0.1, 0.9, true)] [Case(CropHandle.Right, 5.0, 0.5, true)] [Case(CropHandle.TopLeft, 0.45, 0.45, true)] public void ResizingKeepsTheRatioTheMinimumAndTheFrame(CropHandle handle, double x, double y, bool aboutCentre) { var start = new RectD(0.35, 0.3, 0.15, 0.4); var box = CropFrame.ResizeFrom(start, handle, x, y, Portrait, aboutCentre); Assert.Approximately(Portrait, box.Width / box.Height, 1e-12, "the ratio stays locked"); Assert.True(box.X >= -1e-12 && box.Y >= -1e-12 && box.Right <= 1 + 1e-12 && box.Bottom <= 1 + 1e-12, $"{box} lies inside the view"); Assert.GreaterOrEqual(box.Height, CropFrame.Largest(Portrait).Height * CropFrame.MinimumShare - 1e-12); if (aboutCentre) { Assert.Approximately(start.CenterX, box.CenterX, 1e-12); Assert.Approximately(start.CenterY, box.CenterY, 1e-12); return; } // The opposite corner or edge stays where it was; an edge handle keeps the box centred on the other axis. switch (handle) { case CropHandle.BottomRight: Assert.Within(start.X, start.Y, box.X, box.Y, 1e-12); break; case CropHandle.TopLeft: Assert.Within(start.Right, start.Bottom, box.Right, box.Bottom, 1e-12); break; case CropHandle.TopRight: Assert.Within(start.X, start.Bottom, box.X, box.Bottom, 1e-12); break; case CropHandle.Left: Assert.Within(start.Right, start.CenterY, box.Right, box.CenterY, 1e-12); break; case CropHandle.Right: Assert.Within(start.X, start.CenterY, box.X, box.CenterY, 1e-12); break; case CropHandle.Top: Assert.Within(start.CenterX, start.Bottom, box.CenterX, box.Bottom, 1e-12); break; case CropHandle.Bottom: Assert.Within(start.CenterX, start.Y, box.CenterX, box.Y, 1e-12); break; } } [Test] public void ResizingFollowsThePointer() { var start = new RectD(0.35, 0.3, 0.15, 0.4); // Bottom-right to (0.5375, 0.8): both axes ask for a height of 0.5 (0.1875 / 0.375 and 0.8 − 0.3). AssertBox(new RectD(0.35, 0.3, 0.1875, 0.5), CropFrame.ResizeFrom(start, CropHandle.BottomRight, 0.5375, 0.8, Portrait)); // Dragged past the anchor: the minimum box. var least = CropFrame.ResizeFrom(start, CropHandle.BottomRight, 0, 0, Portrait); Assert.Approximately(0.1, least.Height, 1e-12); // Far out: as large as fits from the anchor (0.7 of height below it). Assert.Approximately(0.7, CropFrame.ResizeFrom(start, CropHandle.BottomRight, 1, 1, Portrait).Height, 1e-12); // The inside and no handle leave the box. Assert.Equal(start, CropFrame.ResizeFrom(start, CropHandle.Inside, 0.9, 0.9, Portrait)); Assert.Equal(start, CropFrame.ResizeFrom(start, CropHandle.None, 0.9, 0.9, Portrait)); } [Test] public void ClampAndRefitKeepTheCentre() { // A 16:9 box (in units of a square view) refitted to 1:1 keeps its centre and its area. var wide = new RectD(0.2, 0.3, 0.64, 0.36); var square = CropFrame.Refit(wide, 1); Assert.Approximately(square.Width, square.Height, 1e-12); Assert.Within(wide.CenterX, wide.CenterY, square.CenterX, square.CenterY, 1e-12); Assert.Approximately(wide.Width * wide.Height, square.Width * square.Height, 1e-12); // Near an edge the refitted box moves inside rather than leaving the view. var edge = CropFrame.Refit(new RectD(0.8, 0.1, 0.2, 0.8), 1); Assert.Approximately(1, edge.Right, 1e-12); Assert.Approximately(edge.Width, edge.Height, 1e-12); // Too large or too small: the largest box, the minimum. AssertBox(CropFrame.Largest(Portrait), CropFrame.Clamp(new RectD(-1, -1, 3, 3), Portrait)); Assert.Approximately(0.1, CropFrame.Clamp(new RectD(0.5, 0.5, 0.001, 0.001), Portrait).Height, 1e-12); AssertBox(CropFrame.Largest(Portrait), CropFrame.Clamp(new RectD(double.NaN, 0, 0, 0), Portrait)); } [Test] public void RowsRoundTrip() { foreach (var (size, x, y) in new[] { (1.0, 0.0, 0.0), (0.5, -1.0, 1.0), (0.8, 0.25, -0.5), (0.1, 1.0, 0.3) }) { var box = CropFrame.FromRows(size, x, y, Portrait); Assert.Approximately(Portrait, box.Width / box.Height, 1e-12); var rows = CropFrame.ToRows(box, Portrait); Assert.Approximately(size, rows.Size, 1e-12); Assert.Approximately(x, rows.X, 1e-12); // The largest portrait box fills the height: no vertical slack, so y reads 0. Assert.Approximately(size == 1 ? 0 : y, rows.Y, 1e-12); AssertBox(box, CropFrame.FromRows(rows.Size, rows.X, rows.Y, Portrait)); } AssertBox(CropFrame.Largest(Portrait), CropFrame.FromRows(double.NaN, double.NaN, 0, Portrait)); Assert.True(CropFrame.HasSlack(CropFrame.Largest(Portrait), horizontal: true)); Assert.False(CropFrame.HasSlack(CropFrame.Largest(Portrait), horizontal: false)); } private static void AssertBox(RectD expected, RectD actual) { Assert.Approximately(expected.X, actual.X, 1e-12, $"x of {actual}"); Assert.Approximately(expected.Y, actual.Y, 1e-12, $"y of {actual}"); Assert.Approximately(expected.Width, actual.Width, 1e-12, $"width of {actual}"); Assert.Approximately(expected.Height, actual.Height, 1e-12, $"height of {actual}"); } }