Files
Chronos/tests/Titano.Core.Tests/Geometry/AspectRatioTests.cs
T
Claude 0b459bd108 feat(geometry): add affine maps, aspect crop-fit and Catmull-Rom geometry stage
Affine2D (compose, invert, apply, similarity decomposition), RectD and
AspectRatio presets with largest-centred crop fitting and offsets, and a
single-pass geometry stage composing crop, aspect, output size and warp,
resampled with Keys cubic convolution (a = -0.5): Vector128 4x4 interior
path, widened kernel when minifying, clamp or transparent borders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 09:22:36 +00:00

55 lines
2.2 KiB
C#

using Titano.Core.Imaging.Geometry;
using Titano.Testing;
namespace Titano.Core.Tests.Geometry;
public sealed class AspectRatioTests
{
[Theory]
[Case(1920.0, 1080.0, 1, 1, 420.0, 0.0, 1080.0, 1080.0)]
[Case(1920.0, 1080.0, 9, 16, 656.25, 0.0, 607.5, 1080.0)]
[Case(4000.0, 3000.0, 16, 9, 0.0, 375.0, 4000.0, 2250.0)]
[Case(6000.0, 4000.0, 3, 2, 0.0, 0.0, 6000.0, 4000.0)]
[Case(1080.0, 1920.0, 4, 5, 0.0, 285.0, 1080.0, 1350.0)]
[Case(3840.0, 2160.0, 21, 9, 0.0, 257.142857142857, 3840.0, 1645.71428571429)]
public void FitCropCentresTheLargestCrop(double bw, double bh, int w, int h, double x, double y, double cw, double ch)
{
var crop = AspectRatio.FitCrop(RectD.FromSize(bw, bh), AspectRatio.Custom(w, h).Resolve(bw, bh));
Assert.Approximately(x, crop.X, 1e-9);
Assert.Approximately(y, crop.Y, 1e-9);
Assert.Approximately(cw, crop.Width, 1e-9);
Assert.Approximately(ch, crop.Height, 1e-9);
}
[Theory]
[Case(-1.0, 100.0)]
[Case(0.0, 280.0)]
[Case(1.0, 460.0)]
[Case(5.0, 460.0)]
[Case(-0.5, 190.0)]
public void FitCropOffsetMovesWithinTheSlack(double offset, double expectedX)
{
var crop = AspectRatio.FitCrop(new RectD(100, 50, 720, 360), 1.0, offset, offset);
Assert.Approximately(expectedX, crop.X, 1e-9);
Assert.Approximately(50, crop.Y, 1e-9);
Assert.Approximately(360, crop.Width, 1e-9);
}
[Test]
public void PresetsAndParsing()
{
Assert.Equal(AspectRatio.Widescreen, AspectRatio.Custom(32, 18));
Assert.Equal("16:9", AspectRatio.Widescreen.ToString());
Assert.Equal("9:16", AspectRatio.Vertical.ToString());
Assert.Equal(8, AspectRatio.Presets.Count);
Assert.Approximately(1.5, AspectRatio.Source.Resolve(6000, 4000), 1e-15);
Assert.Approximately(21.0 / 9, AspectRatio.Cinema.Resolve(6000, 4000), 1e-15);
Assert.True(AspectRatio.TryParse("4/5", out var portrait));
Assert.Equal(AspectRatio.Portrait, portrait);
Assert.True(AspectRatio.TryParse("source", out var source));
Assert.True(source.IsSource);
Assert.False(AspectRatio.TryParse("16:0", out _));
Assert.False(AspectRatio.TryParse("wide", out _));
}
}