Files
Alby96andClaude Opus 5.5 188b2c2e55
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
feat(editor): visual crop, rotate/mirror/flip/straighten, colour looks (owner review 1 phase 4) (#26)
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>
2026-11-05 01:09:09 +01:00

238 lines
11 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Geometry;
using Chronos.Core.Project;
using Chronos.Core.Render;
using Chronos.Core.Tests.Render;
using Chronos.Testing;
namespace Chronos.Core.Tests.Geometry;
public sealed class FrameOrientationTests
{
private const int Width = 48;
private const int Height = 32;
[Theory]
[Case(90, false, false)]
[Case(180, false, false)]
[Case(270, false, false)]
[Case(-90, false, false)]
[Case(0, true, false)]
[Case(0, false, true)]
[Case(90, true, false)]
[Case(270, false, true)]
[Case(180, true, true)]
public void TurnsMirrorsAndFlipsPermuteThePixelsExactly(int rotate, bool mirror, bool flip)
{
using var frame = Texture(Width, Height, seed: 7);
var camera = new CameraSettings { Rotate = rotate, Mirror = mirror, Flip = flip };
var geometry = RenderGeometry.Create(camera, RenderFixture.Preset("png-sequence", 0, 0), Width, Height);
var turns = FrameOrientation.TurnsOf(rotate);
Assert.Equal(turns % 2 == 1 ? (Height, Width) : (Width, Height), (geometry.OutputWidth, geometry.OutputHeight));
Assert.Equal((geometry.OutputWidth, geometry.OutputHeight), (geometry.ViewWidth, geometry.ViewHeight));
Assert.Empty(geometry.Warnings);
using var view = GeometryStage.Apply(frame, geometry.For(Width, Height, 0, Affine2D.Identity));
using var expected = Permuted(frame, turns, mirror, flip);
Assert.Equal((expected.Width, expected.Height), (view.Width, view.Height));
Assert.True(expected.Data.SequenceEqual(view.Data), "the view is an exact permutation of the frame");
// The editor's Source framing (the whole view frame) is the same permutation.
using var whole = GeometryStage.Apply(frame, geometry.ViewFor(Width, Height, view.Width, view.Height));
Assert.True(expected.Data.SequenceEqual(whole.Data));
}
[Test]
public void QuarterTurnsAreExactIntegerMaps()
{
Assert.Equal(new Affine2D(0, -1, 1, 0, Height - 1, 0), FrameOrientation.Of(90, false, false, 0, Width, Height).FrameToView);
Assert.Equal(new Affine2D(-1, 0, 0, -1, Width - 1, Height - 1), FrameOrientation.Of(180, false, false, 0, Width, Height).FrameToView);
Assert.Equal(new Affine2D(0, 1, -1, 0, 0, Width - 1), FrameOrientation.Of(270, false, false, 0, Width, Height).FrameToView);
foreach (var rotate in new[] { 0, 90, 180, 270 })
{
foreach (var (mirror, flip) in new[] { (false, false), (true, false), (false, true), (true, true) })
{
var o = FrameOrientation.Of(rotate, mirror, flip, 0, Width, Height);
Assert.Equal(Affine2D.Identity, o.ViewToFrame * o.FrameToView);
Assert.Equal(Affine2D.Identity, o.FrameToView * o.ViewToFrame);
}
}
}
[Test]
public void OrientationAlgebraHolds()
{
// mirror² = flip² = I.
var mirror = FrameOrientation.Of(0, true, false, 0, Width, Height).FrameToView;
var flip = FrameOrientation.Of(0, false, true, 0, Width, Height).FrameToView;
Assert.Equal(Affine2D.Identity, mirror * mirror);
Assert.Equal(Affine2D.Identity, flip * flip);
// turn⁴ = I: each quarter turn takes the previous view (its size swapped) as its frame.
var turn = Affine2D.Identity;
var (w, h) = (Width, Height);
for (var k = 0; k < 4; k++)
{
turn = FrameOrientation.Of(90, false, false, 0, w, h).FrameToView * turn;
(w, h) = (h, w);
if (k < 3) Assert.NotEqual(Affine2D.Identity, turn);
}
Assert.Equal(Affine2D.Identity, turn);
// Parity: a turn right then a mirror is a turn left then a flip, the transpose; mirror and flip together are a half turn.
var transpose = new Affine2D(0, 1, 1, 0, 0, 0);
Assert.Equal(transpose, FrameOrientation.Of(90, true, false, 0, Width, Height).FrameToView);
Assert.Equal(transpose, FrameOrientation.Of(270, false, true, 0, Width, Height).FrameToView);
Assert.Equal(FrameOrientation.Of(180, false, false, 0, Width, Height).FrameToView, FrameOrientation.Of(0, true, true, 0, Width, Height).FrameToView);
Assert.True(FrameOrientation.Of(360, false, false, 0, Width, Height).IsIdentity);
}
[Theory]
[Case(7.5, 0)]
[Case(-12.0, 0)]
[Case(15.0, 90)]
public void StraightenMatchesTheAnalyticRamp(double degrees, int rotate)
{
using var ramp = new ImageBuffer(96, 64, 1);
for (var y = 0; y < 64; y++)
for (var x = 0; x < 96; x++)
ramp[0, x, y] = 0.1f + 0.01f * x + 0.005f * y;
var orientation = FrameOrientation.Of(rotate, false, false, degrees, 96, 64);
using var view = GeometryStage.Apply(ramp, new GeometrySettings
{
Crop = RectD.FromSize(orientation.ViewWidth, orientation.ViewHeight),
OutputWidth = orientation.ViewWidth,
OutputHeight = orientation.ViewHeight,
Warp = orientation.ViewToFrame,
});
Assert.Equal((orientation.ViewWidth, orientation.ViewHeight), (view.Width, view.Height));
var checkedPixels = 0;
for (var v = 0; v < view.Height; v++)
{
for (var u = 0; u < view.Width; u++)
{
var (sx, sy) = orientation.ViewToFrame.Apply(u, v);
// Every sample lies inside the frame's pixel centres (the cover zoom), within rounding.
Assert.True(sx >= -1e-9 && sx <= 95 + 1e-9 && sy >= -1e-9 && sy <= 63 + 1e-9, $"view ({u}, {v}) samples ({sx}, {sy})");
if (sx < 1 || sx >= 93 || sy < 1 || sy >= 61) continue; // all 4×4 taps inside: the ramp is reproduced exactly
Assert.Approximately(0.1 + 0.01 * sx + 0.005 * sy, view[0, u, v], 2e-6);
checkedPixels++;
}
}
Assert.Greater(checkedPixels, view.Width * view.Height / 2);
}
[Theory]
[Case(15.0, 0)]
[Case(-15.0, 0)]
[Case(15.0, 90)]
[Case(-15.0, 270)]
[Case(0.3, 0)]
public void StraightenNeverShowsABorder(double degrees, int rotate)
{
using var frame = Texture(96, 64, seed: 3);
var camera = new CameraSettings { Rotate = rotate, Straighten = degrees };
var geometry = RenderGeometry.Create(camera, RenderFixture.Preset("png-sequence", 0, 0), 96, 64);
var settings = geometry.For(96, 64, 0, Affine2D.Identity) with { Border = BorderMode.Transparent };
using var view = GeometryStage.Apply(frame, settings);
var alpha = view.Plane(3).ToArray();
Assert.GreaterOrEqual(alpha.Min(), 1 - 1e-6, "every output pixel is fully covered");
}
[Test]
public void CoverZoomFollowsTheFormula()
{
Assert.Equal(1.0, FrameOrientation.CoverZoomOf(0, 1920, 1080));
var (sin, cos) = double.SinCos(15 * double.Pi / 180);
Assert.Approximately(cos + 16 / 9.0 * sin, FrameOrientation.CoverZoomOf(15, 1920, 1080), 1e-12);
Assert.Approximately(cos + 16 / 9.0 * sin, FrameOrientation.CoverZoomOf(-15, 1080, 1920), 1e-12);
// On the pixel-centre extents of the view.
var o = FrameOrientation.Of(0, false, false, 15, 1920, 1080);
Assert.Approximately(cos + 1919 / 1079.0 * sin, o.CoverZoom, 1e-12);
Assert.Equal(o.CoverZoom, FrameOrientation.Of(90, false, false, 15, 1080, 1920).CoverZoom);
Assert.Equal(1.0, FrameOrientation.Of(90, true, false, 0, 1920, 1080).CoverZoom);
}
[Test]
public void ValuesAreNormalisedAndResizedKeepsThem()
{
Assert.Equal(3, FrameOrientation.TurnsOf(-90));
Assert.Equal(1, FrameOrientation.TurnsOf(450));
Assert.Equal(0, FrameOrientation.TurnsOf(45));
Assert.Equal(15.0, FrameOrientation.StraightenOf(40));
Assert.Equal(-15.0, FrameOrientation.StraightenOf(-15.5));
Assert.Equal(0.0, FrameOrientation.StraightenOf(double.NaN));
Assert.False(FrameOrientation.IsValidStraighten(double.PositiveInfinity));
var full = FrameOrientation.Of(-90, true, false, 4, 6000, 4000);
var small = full.Resized(600, 400);
Assert.Equal((3, true, false, 4.0), (small.Turns, small.Mirror, small.Flip, small.Straighten));
Assert.Equal((400, 600), (small.ViewWidth, small.ViewHeight));
Assert.Same(full, full.Resized(6000, 4000));
Assert.True(FrameOrientation.Identity(10, 10).IsIdentity);
}
[Test]
public void InvalidValuesRenderAsNoneWithAWarning()
{
var export = RenderFixture.Preset("png-sequence", 0, 0);
var skewed = RenderGeometry.Create(new CameraSettings { Rotate = 45 }, export, 96, 64);
Assert.True(skewed.Orientation.IsIdentity);
Assert.Equal((96, 64), (skewed.OutputWidth, skewed.OutputHeight));
Assert.Equal("Export.Render.RotationIgnored", skewed.Warnings.Single().Key);
Assert.SequenceEqual(["45"], skewed.Warnings.Single().Args);
var steep = RenderGeometry.Create(new CameraSettings { Straighten = 20 }, export, 96, 64);
Assert.Equal(15.0, steep.Orientation.Straighten);
Assert.Equal("Export.Render.StraightenClamped", steep.Warnings.Single().Key);
Assert.SequenceEqual(["20", "15"], steep.Warnings.Single().Args);
var broken = RenderGeometry.Create(new CameraSettings { Straighten = double.NaN }, export, 96, 64);
Assert.True(broken.Orientation.IsIdentity);
Assert.SequenceEqual(["NaN", "0"], broken.Warnings.Single().Args);
}
/// <summary>A seeded RGB texture.</summary>
internal static ImageBuffer Texture(int width, int height, int seed)
{
var random = new Random(seed);
var image = new ImageBuffer(width, height, 3);
for (var i = 0; i < image.Data.Length; i++) image.Data[i] = (float)random.NextDouble();
return image;
}
/// <summary>The frame turned right <paramref name="turns"/> times, then mirrored and flipped, by moving pixels.</summary>
internal static ImageBuffer Permuted(ImageBuffer frame, int turns, bool mirror, bool flip)
{
var image = frame.Clone();
for (var k = 0; k < turns; k++) image = Replace(image, Move(image, image.Height, image.Width, (src, u, v) => (v, src.Height - 1 - u)));
if (mirror) image = Replace(image, Move(image, image.Width, image.Height, (src, u, v) => (src.Width - 1 - u, v)));
if (flip) image = Replace(image, Move(image, image.Width, image.Height, (src, u, v) => (u, src.Height - 1 - v)));
return image;
}
private static ImageBuffer Replace(ImageBuffer old, ImageBuffer next)
{
old.Dispose();
return next;
}
/// <summary>A new image whose pixel (u, v) is the source pixel <paramref name="from"/>(u, v).</summary>
private static ImageBuffer Move(ImageBuffer src, int width, int height, Func<ImageBuffer, int, int, (int X, int Y)> from)
{
var dst = new ImageBuffer(width, height, src.Channels);
for (var c = 0; c < src.Channels; c++)
{
for (var v = 0; v < height; v++)
{
for (var u = 0; u < width; u++)
{
var (x, y) = from(src, u, v);
dst[c, u, v] = src[c, x, y];
}
}
}
return dst;
}
}