using Chronos.Core.Export; using Chronos.Core.Imaging; using Chronos.Core.Imaging.Geometry; using Chronos.Core.Project; using Chronos.Core.Render; using Chronos.Core.Tests.Geometry; using Chronos.Testing; namespace Chronos.Core.Tests.Render; public sealed class RenderGeometryTests { [Test] public void TheChosenProportionsWinOverAFixedPreset() { var portrait = RenderGeometry.Create(Camera("9:16"), Export("4k-h264"), 6000, 4000); Assert.Equal((2160, 3840), (portrait.OutputWidth, portrait.OutputHeight)); Assert.Approximately(9 / 16.0, portrait.OutputRatio, 1e-12); Assert.Empty(portrait.Warnings); var source = RenderGeometry.Create(new CameraSettings(), Export("4k-h264"), 6000, 4000); Assert.Equal((3840, 2160), (source.OutputWidth, source.OutputHeight)); Assert.Approximately(16 / 9.0, source.OutputRatio, 1e-12); // Every case agrees with RenderSize, which the server routes jobs with. foreach (var aspect in new[] { "source", "9:16", "1:1", "4:5", "21:9" }) { foreach (var camera in new[] { Camera(aspect), Camera(aspect) with { Rotate = 90 } }) { foreach (var preset in new[] { "4k-h264", "1080p-h264", "vertical-1080x1920", "png-sequence" }) { var geometry = RenderGeometry.Create(camera, Export(preset), 6000, 4000); Assert.Equal(RenderSize.Of(camera, Export(preset), 6000, 4000), (geometry.OutputWidth, geometry.OutputHeight), $"{aspect} {preset} {camera.Rotate}"); } } } } [Test] public void AQuarterTurnSwapsTheViewAndLocksTheCropInIt() { var turned = RenderGeometry.Create(new CameraSettings { Rotate = 90 }, Export("png-sequence"), 6000, 4000); Assert.Equal((4000, 6000), (turned.ViewWidth, turned.ViewHeight)); Assert.Equal((4000, 6000), (turned.OutputWidth, turned.OutputHeight)); Assert.Equal((6000, 4000), (turned.SourceWidth, turned.SourceHeight)); // 1:1 on the turned view: the box is as wide as the view and 4000/6000 of its height, in normalised units. var square = RenderGeometry.Create(Camera("1:1") with { Rotate = 90 }, Export("png-sequence"), 6000, 4000); Assert.Approximately(1.5, square.LockRatio, 1e-12); var crop = square.EditableCrop; Assert.Approximately(1, crop.Width, 1e-12); Assert.Approximately(2 / 3.0, crop.Height, 1e-12); Assert.Approximately(1 / 6.0, crop.Y, 1e-12); Assert.Equal((4000, 4000), (square.OutputWidth, square.OutputHeight)); } [Test] public void TheEditableCropIsTheRenderedCrop() { var camera = Camera("4:5") with { Crop = [0.1, 0.2, 0.5, 0.6], OffsetX = 1, OffsetY = -1, Rotate = 270, Mirror = true }; var geometry = RenderGeometry.Create(camera, Export("png-sequence"), 96, 64); // The Export step's footprint of a frame without pose or stabilisation is the Camera step's box. foreach (var (w, h) in new[] { (96, 64), (48, 32) }) { var plan = GeometryStage.Plan(w, h, geometry.For(w, h, 0, Affine2D.Identity)); var footprint = geometry.FootprintInView(plan, w, h); var box = geometry.EditableCrop; Assert.Approximately(box.X, footprint.X, 1e-9); Assert.Approximately(box.Y, footprint.Y, 1e-9); Assert.Approximately(box.Width, footprint.Width, 1e-9); Assert.Approximately(box.Height, footprint.Height, 1e-9); } Assert.Equal(new RectD(0, 0, 1, 1), geometry.FootprintInView(null, 96, 64)); // The box the editor would write for it renders the same. var written = Camera("4:5") with { Crop = CropFrame.ToCrop(geometry.EditableCrop), Rotate = 270, Mirror = true }; var again = RenderGeometry.Create(written, Export("png-sequence"), 96, 64); Assert.Equal((geometry.OutputWidth, geometry.OutputHeight), (again.OutputWidth, again.OutputHeight)); Assert.Approximately(geometry.EditableCrop.X, again.EditableCrop.X, 1e-12); Assert.Approximately(geometry.EditableCrop.Y, again.EditableCrop.Y, 1e-12); } [Test] public void ViewToFrameMapsTheViewOntoTheDecodedFrame() { var geometry = RenderGeometry.Create(new CameraSettings { Rotate = 180, Flip = true, Straighten = -4 }, Export("png-sequence"), 6000, 4000); var small = geometry.ViewToFrame(600, 400); Assert.Equal(FrameOrientation.Of(180, false, true, -4, 600, 400).ViewToFrame, small); Assert.Equal(geometry.Orientation.ViewToFrame, geometry.ViewToFrame(6000, 4000)); Assert.Equal(Affine2D.Identity, RenderGeometry.Create(new CameraSettings(), Export("png-sequence"), 60, 40).ViewToFrame(60, 40)); // The Source framing shows the whole view at the size asked, and only an identity camera skips the warp. var view = geometry.ViewFor(600, 400, 300, 200); var plan = GeometryStage.Plan(600, 400, view); Assert.Equal((300, 200), (plan.OutputWidth, plan.OutputHeight)); Assert.Equal(new RectD(0, 0, 600, 400), plan.Crop); } [Test] public void TheDecodeGrowsWithTheCoverZoom() { var plain = RenderGeometry.Create(new CameraSettings(), Export("png-sequence", 96, 64), 480, 320); Assert.Equal(96, plain.WorkingLongEdge); foreach (var degrees in new[] { 2.0, 10.0, -15.0 }) { var straightened = RenderGeometry.Create(new CameraSettings { Straighten = degrees }, Export("png-sequence", 96, 64), 480, 320); var zoom = straightened.Orientation.CoverZoom; Assert.Greater(zoom, 1); Assert.Equal((int)double.Ceiling(96 * zoom), straightened.WorkingLongEdge); } // Quarter turns do not change the long edge. Assert.Equal(96, RenderGeometry.Create(new CameraSettings { Rotate = 90 }, Export("png-sequence", 64, 96), 480, 320).WorkingLongEdge); } [Test] public void StabilisationComposesInRawFrameSpace() { // stab⁻¹ · O⁻¹ · V: the correction is undone in the decoded frame, after the view is mapped back into it. var geometry = RenderGeometry.Create(new CameraSettings { Rotate = 90 }, Export("png-sequence"), 96, 64); var shift = Affine2D.Translation(3, -2); var settings = geometry.For(96, 64, 0, shift); var plain = geometry.For(96, 64, 0, Affine2D.Identity); var (x, y) = settings.Warp.Apply(10, 20); var (px, py) = plain.Warp.Apply(10, 20); Assert.Within(px - 3, py + 2, x, y, 1e-12); using var frame = FrameOrientationTests.Texture(96, 64, seed: 11); using var view = GeometryStage.Apply(frame, plain); Assert.Equal((64, 96), (view.Width, view.Height)); } private static CameraSettings Camera(string aspect) => new() { Aspect = new Auto().WithManual(aspect) }; private static ResolvedExport Export(string preset, int width = -1, int height = -1) { var export = ExportResolver.Resolve(new ExportSettings { Preset = new Auto().WithManual(preset) }); return width < 0 ? export : export with { Width = width, Height = height }; } }