CI / Build and test (push) Successful in 8m19s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 12m9s
CI / PostgreSQL 16 integration (push) Successful in 7m49s
CI / PostgreSQL 17 integration (push) Successful in 7m19s
CI / Docker image (push) Successful in 46s
Release / Server image (push) Successful in 1m14s
Owner review 1 R5 + R6. Camera rotation, mirror, flip, straighten and crop handles locked to the output; an Exposure Look group; Export output size ("proportions win"); settings format 2 with older-node gates. Also fixes project text and hashes differing between Windows and Linux.
Closes #19
Closes #20
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
247 lines
12 KiB
C#
247 lines
12 KiB
C#
using Chronos.Core.Imaging.Geometry;
|
||
using Chronos.Core.Project;
|
||
using Chronos.Testing;
|
||
|
||
namespace Chronos.Core.Tests.Geometry;
|
||
|
||
public sealed class OrientationEditTests
|
||
{
|
||
private static readonly OrientationOp[] Ops = [OrientationOp.RotateLeft, OrientationOp.RotateRight, OrientationOp.Mirror, OrientationOp.Flip];
|
||
|
||
private static ProjectSettings Framed { get; } = new()
|
||
{
|
||
Camera = new CameraSettings
|
||
{
|
||
Crop = CropFrame.ToCrop(CropFrame.FromRows(0.7, 1 / 3.0, -0.2, 16 / 9.0 * 4000 / 6000)),
|
||
Aspect = new Auto<string>().WithManual("16:9").WithAdvice("3:2", null),
|
||
OffsetX = 0.25,
|
||
OffsetY = -0.5,
|
||
Keyframes = [new CameraKey(0, 0.25, 0.4, 1.5, 3), new CameraKey(9, 0.6, 0.125, 2, -1.5, "Linear")],
|
||
Straighten = 2.5,
|
||
},
|
||
PendingFrames = [new PendingFrame { Sha256 = "ab", Keyframes = [new CameraKey(4, 0.75, 0.5, 1, 2)] }],
|
||
};
|
||
|
||
[Test]
|
||
public void FourRightTurnsGiveBackTheText()
|
||
{
|
||
var text = Framed.ToJson();
|
||
var settings = Framed;
|
||
for (var k = 0; k < 4; k++)
|
||
{
|
||
settings = OrientationEdit.Apply(settings, OrientationOp.RotateRight);
|
||
Assert.Equal((k + 1) % 4 * 90, settings.Camera.Rotate);
|
||
if (k < 3) Assert.NotEqual(text, settings.ToJson());
|
||
}
|
||
Assert.Equal(text, settings.ToJson());
|
||
|
||
// Without a straighten, the turns leave nothing of format 2 behind.
|
||
var plain = new ProjectSettings { Camera = new CameraSettings { Crop = [0.1, 0.2, 0.3, 0.4] } };
|
||
var turned = plain;
|
||
for (var k = 0; k < 4; k++) turned = OrientationEdit.Apply(turned, OrientationOp.RotateRight);
|
||
Assert.Equal(plain.ToJson(), turned.ToJson());
|
||
Assert.Equal(ProjectSettings.BaseVersion, turned.WrittenFormat);
|
||
}
|
||
|
||
[Test]
|
||
public void EveryPressIsUndoneByItsOpposite()
|
||
{
|
||
var text = Framed.ToJson();
|
||
foreach (var start in new[] { Framed, OrientationEdit.Apply(Framed, OrientationOp.Mirror), OrientationEdit.Apply(Framed, OrientationOp.RotateLeft) })
|
||
{
|
||
var before = start.ToJson();
|
||
Assert.Equal(before, OrientationEdit.Apply(OrientationEdit.Apply(start, OrientationOp.RotateRight), OrientationOp.RotateLeft).ToJson());
|
||
Assert.Equal(before, OrientationEdit.Apply(OrientationEdit.Apply(start, OrientationOp.Mirror), OrientationOp.Mirror).ToJson());
|
||
Assert.Equal(before, OrientationEdit.Apply(OrientationEdit.Apply(start, OrientationOp.Flip), OrientationOp.Flip).ToJson());
|
||
}
|
||
Assert.Equal(text, Framed.ToJson());
|
||
}
|
||
|
||
/// <summary>
|
||
/// Values off the 12-decimal grid (a crop or keyframe centre an older Chronos, a hand or another program wrote) cannot
|
||
/// come back bit for bit, as 1 − v drops bits of a v below ½: they land on the grid at the first press, no visible change,
|
||
/// and from then on every net-zero sequence of presses gives back the same text.
|
||
/// </summary>
|
||
[Test]
|
||
public void ValuesOffTheGridLandOnItOnceAndThenStay()
|
||
{
|
||
var legacy = new ProjectSettings
|
||
{
|
||
Camera = new CameraSettings { Crop = [1 / 3.0, 0.2, 0.25, 0.3], Keyframes = [new CameraKey(0, 0.47368421052631576, 1 / 7.0, 1.5, 0)] },
|
||
};
|
||
var once = legacy;
|
||
for (var k = 0; k < 4; k++) once = OrientationEdit.Apply(once, OrientationOp.RotateRight);
|
||
Assert.Approximately(1 / 3.0, once.Camera.Crop![0], 1e-12);
|
||
Assert.Approximately(0.47368421052631576, once.Camera.Keyframes[0].CenterX, 1e-12);
|
||
Assert.Approximately(1 / 7.0, once.Camera.Keyframes[0].CenterY, 1e-12);
|
||
var text = once.ToJson();
|
||
var turned = once;
|
||
for (var k = 0; k < 4; k++) turned = OrientationEdit.Apply(turned, OrientationOp.RotateRight);
|
||
Assert.Equal(text, turned.ToJson(), "four turns of gridded values");
|
||
var mirrored = OrientationEdit.Apply(OrientationEdit.Apply(once, OrientationOp.Mirror), OrientationOp.Mirror);
|
||
Assert.Equal(text, mirrored.ToJson(), "two mirrors of gridded values");
|
||
foreach (var op in Ops)
|
||
{
|
||
var opposite = op switch
|
||
{
|
||
OrientationOp.RotateLeft => OrientationOp.RotateRight,
|
||
OrientationOp.RotateRight => OrientationOp.RotateLeft,
|
||
_ => op,
|
||
};
|
||
Assert.Equal(text, OrientationEdit.Apply(OrientationEdit.Apply(once, op), opposite).ToJson(), $"{op} undone");
|
||
}
|
||
// What the editor writes itself is on the grid already.
|
||
Assert.Equal(0.473684210526, CropFrame.Grid(0.47368421052631576));
|
||
}
|
||
|
||
/// <summary>A crop out of range turns with the content it shows (as it renders, clamped), not with its stored numbers.</summary>
|
||
[Test]
|
||
public void AnOutOfRangeCropTurnsWithTheContentItShows()
|
||
{
|
||
var camera = new CameraSettings { Crop = [0.5, 0, 0.7, 1] };
|
||
Assert.Equal(new RectD(0.5, 0, 0.5, 1), CropFrame.Of(camera.Crop));
|
||
var mirrored = OrientationEdit.Apply(camera, OrientationOp.Mirror);
|
||
Assert.Equal(new RectD(0, 0, 0.5, 1), CropFrame.Of(mirrored.Crop), "the left half: the right half mirrored");
|
||
var turned = OrientationEdit.Apply(camera, OrientationOp.RotateRight);
|
||
Assert.Equal(new RectD(0, 0.5, 1, 0.5), CropFrame.Of(turned.Crop), "the right half turned clockwise is the bottom half");
|
||
// A crop in range stays as stored.
|
||
Assert.SequenceEqual([0.6, 0.2, 0.3, 0.4], OrientationEdit.Apply(new CameraSettings { Crop = [0.1, 0.2, 0.3, 0.4] }, OrientationOp.Mirror).Crop!);
|
||
}
|
||
|
||
/// <summary>A turn transposes the aspect text as written, never reduced: turning back gives the stored text (21:9, not 7:3).</summary>
|
||
[Test]
|
||
public void TurningBackGivesTheStoredAspectText()
|
||
{
|
||
foreach (var aspect in new[] { "21:9", "1920:1080", "16:9", "4/5" })
|
||
{
|
||
var project = new ProjectSettings { Camera = new CameraSettings { Aspect = new Auto<string>().WithManual(aspect) } };
|
||
var right = OrientationEdit.Apply(project, OrientationOp.RotateRight);
|
||
Assert.True(AspectRatio.TryParse(right.Camera.Aspect.Manual, out var transposed));
|
||
Assert.True(AspectRatio.TryParse(aspect, out var stored));
|
||
Assert.Equal((stored.Height, stored.Width), (transposed.Width, transposed.Height));
|
||
Assert.Equal(project.ToJson(), OrientationEdit.Apply(right, OrientationOp.RotateLeft).ToJson(), aspect);
|
||
}
|
||
Assert.Equal("9:21", OrientationEdit.Transposed(new Auto<string>().WithManual("21:9")).Manual);
|
||
}
|
||
|
||
[Test]
|
||
public void MirrorMovesTheCropAndNegatesTheAngles()
|
||
{
|
||
var camera = new CameraSettings
|
||
{
|
||
Crop = [0.1, 0.2, 0.3, 0.4],
|
||
OffsetX = 0.5,
|
||
OffsetY = 0.25,
|
||
Keyframes = [new CameraKey(0, 0.2, 0.7, 2, 5)],
|
||
Straighten = 3,
|
||
};
|
||
var mirrored = OrientationEdit.Apply(camera, OrientationOp.Mirror);
|
||
Assert.True(mirrored.Mirror);
|
||
Assert.SequenceEqual([0.6, 0.2, 0.3, 0.4], mirrored.Crop!);
|
||
Assert.Equal((-0.5, 0.25), (mirrored.OffsetX, mirrored.OffsetY));
|
||
Assert.Equal(new CameraKey(0, 0.8, 0.7, 2, -5), mirrored.Keyframes.Single());
|
||
Assert.Equal(-3.0, mirrored.Straighten);
|
||
Assert.Equal(camera.Aspect, mirrored.Aspect);
|
||
|
||
var flipped = OrientationEdit.Apply(camera, OrientationOp.Flip);
|
||
Assert.True(flipped.Flip);
|
||
Assert.SequenceEqual([0.1, 0.4, 0.3, 0.4], flipped.Crop!);
|
||
Assert.Equal((0.5, -0.25), (flipped.OffsetX, flipped.OffsetY));
|
||
Assert.Equal(new CameraKey(0, 0.2, 0.3, 2, -5), flipped.Keyframes.Single());
|
||
|
||
// A zero stays a zero (JSON would write −0 as "-0").
|
||
Assert.False(double.IsNegative(OrientationEdit.Apply(new CameraSettings(), OrientationOp.Mirror).Straighten));
|
||
}
|
||
|
||
[Test]
|
||
public void AQuarterTurnTransposesTheCropAndTheAspect()
|
||
{
|
||
var camera = new CameraSettings
|
||
{
|
||
Crop = [0.1, 0.2, 0.3, 0.4],
|
||
Aspect = new Auto<string>().WithManual("16:9").WithAdvice("4:3", null),
|
||
OffsetX = 0.5,
|
||
OffsetY = 0.25,
|
||
Keyframes = [new CameraKey(0, 0.2, 0.7, 2, 5)],
|
||
Straighten = 3,
|
||
};
|
||
var right = OrientationEdit.Apply(camera, OrientationOp.RotateRight);
|
||
Assert.Equal(90, right.Rotate);
|
||
Assert.SequenceEqual([0.4, 0.1, 0.4, 0.3], right.Crop!);
|
||
Assert.Equal((-0.25, 0.5), (right.OffsetX, right.OffsetY));
|
||
Assert.Equal(new CameraKey(0, 0.3, 0.2, 2, 5), right.Keyframes.Single());
|
||
Assert.Equal("9:16", right.Aspect.Manual);
|
||
Assert.Equal("3:4", right.Aspect.AutoValue);
|
||
Assert.Equal(3.0, right.Straighten);
|
||
|
||
var left = OrientationEdit.Apply(camera, OrientationOp.RotateLeft);
|
||
Assert.Equal(270, left.Rotate);
|
||
Assert.SequenceEqual([0.2, 0.6, 0.4, 0.3], left.Crop!);
|
||
|
||
// "source", a square and text that cannot be read stay as they are.
|
||
foreach (var aspect in new[] { "source", "1:1", "wide" })
|
||
Assert.Equal(aspect, OrientationEdit.Transposed(new Auto<string>().WithManual(aspect)).Manual);
|
||
Assert.Null(OrientationEdit.Transposed(new Auto<string>()).Manual);
|
||
}
|
||
|
||
[Test]
|
||
public void ATurnSeenThroughOneMirrorIsStoredTheOtherWay()
|
||
{
|
||
Assert.Equal(270, OrientationEdit.Apply(new CameraSettings { Mirror = true }, OrientationOp.RotateRight).Rotate);
|
||
Assert.Equal(90, OrientationEdit.Apply(new CameraSettings { Flip = true }, OrientationOp.RotateLeft).Rotate);
|
||
Assert.Equal(90, OrientationEdit.Apply(new CameraSettings { Mirror = true, Flip = true }, OrientationOp.RotateRight).Rotate);
|
||
Assert.Equal(0, OrientationEdit.Apply(new CameraSettings { Rotate = 90 }, OrientationOp.RotateLeft).Rotate);
|
||
Assert.Equal(0, OrientationEdit.Apply(new CameraSettings { Rotate = -90 }, OrientationOp.RotateRight).Rotate);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Each press moves the view as the user sees it: a frame point shown at normalised (x, y) before the press is shown
|
||
/// at <see cref="OrientationEdit.MapPoint"/>(x, y) after it, whatever the stored turn, mirror, flip and straighten.
|
||
/// </summary>
|
||
[Test]
|
||
public void PressesMoveThePictureAsShown()
|
||
{
|
||
const int width = 60, height = 40;
|
||
foreach (var rotate in new[] { 0, 90, 180, 270 })
|
||
{
|
||
foreach (var (mirror, flip) in new[] { (false, false), (true, false), (false, true), (true, true) })
|
||
{
|
||
foreach (var straighten in new[] { 0.0, 6.5 })
|
||
{
|
||
var camera = new CameraSettings { Rotate = rotate, Mirror = mirror, Flip = flip, Straighten = straighten };
|
||
var before = FrameOrientation.Of(camera, width, height);
|
||
foreach (var op in Ops)
|
||
{
|
||
var after = FrameOrientation.Of(OrientationEdit.Apply(camera, op), width, height);
|
||
foreach (var (px, py) in new[] { (0.0, 0.0), (59.0, 0.0), (12.5, 30.0), (30.0, 20.0), (44.0, 7.25) })
|
||
{
|
||
var (bx, by) = Normalised(before, px, py);
|
||
var (ax, ay) = Normalised(after, px, py);
|
||
var (ex, ey) = OrientationEdit.MapPoint(bx, by, op);
|
||
Assert.Within(ex, ey, ax, ay, 1e-9, $"{op} on rotate {rotate}, mirror {mirror}, flip {flip}, straighten {straighten} at ({px}, {py})");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
[Test]
|
||
public void ParkedKeyframesMoveWithTheProject()
|
||
{
|
||
var turned = OrientationEdit.Apply(Framed, OrientationOp.RotateRight);
|
||
Assert.Equal(new CameraKey(4, 0.5, 0.75, 1, 2), turned.PendingFrames!.Single().Keyframes!.Single());
|
||
Assert.Equal("ab", turned.PendingFrames!.Single().Sha256);
|
||
var mirrored = OrientationEdit.Apply(Framed, OrientationOp.Mirror);
|
||
Assert.Equal(new CameraKey(4, 0.25, 0.5, 1, -2), mirrored.PendingFrames!.Single().Keyframes!.Single());
|
||
Assert.Null(OrientationEdit.Apply(new ProjectSettings(), OrientationOp.Flip).PendingFrames);
|
||
}
|
||
|
||
/// <summary>A frame pixel centre in normalised edge coordinates of the view.</summary>
|
||
private static (double X, double Y) Normalised(FrameOrientation orientation, double x, double y)
|
||
{
|
||
var (vx, vy) = orientation.FrameToView.Apply(x, y);
|
||
return ((vx + 0.5) / orientation.ViewWidth, (vy + 0.5) / orientation.ViewHeight);
|
||
}
|
||
}
|