diff --git a/src/Titano.Core/Imaging/Geometry/Affine2D.cs b/src/Titano.Core/Imaging/Geometry/Affine2D.cs new file mode 100644 index 00000000..70198191 --- /dev/null +++ b/src/Titano.Core/Imaging/Geometry/Affine2D.cs @@ -0,0 +1,82 @@ +namespace Titano.Core.Imaging.Geometry; + +/// +/// 2D affine map x' = M11·x + M12·y + Tx, y' = M21·x + M22·y + Ty. Image transforms use +/// pixel-centre coordinates: pixel (i, j) sits at (i, j) and an image of width W spans [−0.5, W − 0.5]. +/// a * b applies b first, then a. +/// +public readonly record struct Affine2D(double M11, double M12, double M21, double M22, double Tx, double Ty) +{ + public static Affine2D Identity { get; } = new(1, 0, 0, 1, 0, 0); + + public static Affine2D Translation(double tx, double ty) => new(1, 0, 0, 1, tx, ty); + + public static Affine2D Scaling(double sx, double sy) => new(sx, 0, 0, sy, 0, 0); + + /// Counter-clockwise rotation in a y-up frame (clockwise on screen, where y points down). + public static Affine2D Rotation(double radians) + { + var (s, c) = double.SinCos(radians); + return new Affine2D(c, -s, s, c, 0, 0); + } + + /// Similarity x' = scale · R(radians) · x + (tx, ty). + public static Affine2D Similarity(double scale, double radians, double tx, double ty) + { + var (s, c) = double.SinCos(radians); + return new Affine2D(scale * c, -scale * s, scale * s, scale * c, tx, ty); + } + + /// The linear part of applied about the pivot (cx, cy) instead of the origin. + public static Affine2D About(Affine2D map, double cx, double cy) => Translation(cx, cy) * map * Translation(-cx, -cy); + + public double Determinant => M11 * M22 - M12 * M21; + + /// Uniform scale of the linear part, √|det|. + public double Scale => double.Sqrt(double.Abs(Determinant)); + + /// Rotation angle (radians) of the closest similarity. + public double Angle => double.Atan2(M21 - M12, M11 + M22); + + public (double X, double Y) Apply(double x, double y) => (M11 * x + M12 * y + Tx, M21 * x + M22 * y + Ty); + + /// Applies the linear part only (for displacement vectors). + public (double X, double Y) ApplyVector(double x, double y) => (M11 * x + M12 * y, M21 * x + M22 * y); + + /// This map followed by . + public Affine2D Then(Affine2D next) => next * this; + + public static Affine2D operator *(Affine2D outer, Affine2D inner) => new( + outer.M11 * inner.M11 + outer.M12 * inner.M21, + outer.M11 * inner.M12 + outer.M12 * inner.M22, + outer.M21 * inner.M11 + outer.M22 * inner.M21, + outer.M21 * inner.M12 + outer.M22 * inner.M22, + outer.M11 * inner.Tx + outer.M12 * inner.Ty + outer.Tx, + outer.M21 * inner.Tx + outer.M22 * inner.Ty + outer.Ty); + + public bool TryInvert(out Affine2D inverse) + { + var det = Determinant; + var norm = double.Abs(M11) + double.Abs(M12) + double.Abs(M21) + double.Abs(M22); + if (!(double.Abs(det) > 1e-14 * norm * norm)) + { + inverse = default; + return false; + } + var r = 1 / det; + var a = M22 * r; + var b = -M12 * r; + var c = -M21 * r; + var d = M11 * r; + inverse = new Affine2D(a, b, c, d, -(a * Tx + b * Ty), -(c * Tx + d * Ty)); + return true; + } + + /// Inverse map; throws for a singular matrix. + public Affine2D Invert() => TryInvert(out var inverse) ? inverse : throw new InvalidOperationException("The affine map is singular."); + + /// True when every coefficient is within of the identity. + public bool IsIdentity(double tolerance = 1e-12) => + double.Abs(M11 - 1) <= tolerance && double.Abs(M12) <= tolerance && double.Abs(M21) <= tolerance + && double.Abs(M22 - 1) <= tolerance && double.Abs(Tx) <= tolerance && double.Abs(Ty) <= tolerance; +} diff --git a/src/Titano.Core/Imaging/Geometry/AspectRatio.cs b/src/Titano.Core/Imaging/Geometry/AspectRatio.cs new file mode 100644 index 00000000..ae92435f --- /dev/null +++ b/src/Titano.Core/Imaging/Geometry/AspectRatio.cs @@ -0,0 +1,117 @@ +using System.Globalization; + +namespace Titano.Core.Imaging.Geometry; + +/// Axis-aligned rectangle in edge coordinates (a W×H image is (0, 0, W, H)). +public readonly record struct RectD(double X, double Y, double Width, double Height) +{ + public double Right => X + Width; + + public double Bottom => Y + Height; + + public double CenterX => X + Width / 2; + + public double CenterY => Y + Height / 2; + + public static RectD FromSize(double width, double height) => new(0, 0, width, height); +} + +/// +/// Output aspect ratio: a named preset, a custom W:H, or (the source frame's own ratio). +/// +public readonly record struct AspectRatio +{ + private AspectRatio(int width, int height) + { + Width = width; + Height = height; + } + + /// Ratio numerator; 0 for . + public int Width { get; } + + /// Ratio denominator; 0 for . + public int Height { get; } + + public bool IsSource => Width == 0; + + public static AspectRatio Source => default; + + public static AspectRatio Widescreen { get; } = new(16, 9); + + public static AspectRatio Cinema { get; } = new(21, 9); + + public static AspectRatio Standard { get; } = new(4, 3); + + public static AspectRatio Photo { get; } = new(3, 2); + + public static AspectRatio Square { get; } = new(1, 1); + + public static AspectRatio Vertical { get; } = new(9, 16); + + public static AspectRatio Portrait { get; } = new(4, 5); + + /// The presets offered to users, first. + public static IReadOnlyList Presets { get; } = [Source, Widescreen, Cinema, Standard, Photo, Square, Vertical, Portrait]; + + /// A custom W:H ratio (reduced to lowest terms). + public static AspectRatio Custom(int width, int height) + { + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(width); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(height); + var g = Gcd(width, height); + return new AspectRatio(width / g, height / g); + } + + /// Width / height, using the source size when this is . + public double Resolve(double sourceWidth, double sourceHeight) => IsSource ? sourceWidth / sourceHeight : Width / (double)Height; + + /// Parses "16:9", "16/9" or "source". + public static bool TryParse(string? text, out AspectRatio ratio) + { + ratio = Source; + if (string.IsNullOrWhiteSpace(text)) return false; + text = text.Trim(); + if (text.Equals("source", StringComparison.OrdinalIgnoreCase)) return true; + var separator = text.IndexOfAny([':', '/', 'x']); + if (separator <= 0 + || !int.TryParse(text.AsSpan(0, separator), NumberStyles.None, CultureInfo.InvariantCulture, out var w) + || !int.TryParse(text.AsSpan(separator + 1), NumberStyles.None, CultureInfo.InvariantCulture, out var h) + || w <= 0 || h <= 0) + return false; + ratio = Custom(w, h); + return true; + } + + public override string ToString() => IsSource ? "source" : string.Create(CultureInfo.InvariantCulture, $"{Width}:{Height}"); + + /// + /// Largest crop of aspect (width / height) inside . + /// The offsets place it within the leftover slack: −1 = left/top, 0 = centred, +1 = right/bottom (clamped). + /// + public static RectD FitCrop(RectD bounds, double ratio, double offsetX = 0, double offsetY = 0) + { + if (!(ratio > 0) || double.IsInfinity(ratio)) throw new ArgumentOutOfRangeException(nameof(ratio)); + if (!(bounds.Width > 0 && bounds.Height > 0)) throw new ArgumentOutOfRangeException(nameof(bounds)); + double width, height; + if (bounds.Width >= bounds.Height * ratio) + { + height = bounds.Height; + width = height * ratio; + } + else + { + width = bounds.Width; + height = width / ratio; + } + var fx = (1 + double.Clamp(offsetX, -1, 1)) / 2; + var fy = (1 + double.Clamp(offsetY, -1, 1)) / 2; + return new RectD(bounds.X + (bounds.Width - width) * fx, bounds.Y + (bounds.Height - height) * fy, width, height); + } + + private static int Gcd(int a, int b) + { + while (b != 0) (a, b) = (b, a % b); + return a; + } +} diff --git a/src/Titano.Core/Imaging/Geometry/GeometryStage.cs b/src/Titano.Core/Imaging/Geometry/GeometryStage.cs new file mode 100644 index 00000000..c9e70682 --- /dev/null +++ b/src/Titano.Core/Imaging/Geometry/GeometryStage.cs @@ -0,0 +1,320 @@ +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Intrinsics; + +namespace Titano.Core.Imaging.Geometry; + +/// How samples that fall outside the source are treated. +public enum BorderMode +{ + /// Replicate the nearest edge pixel. + Clamp, + + /// Outside is transparent black; the destination carries premultiplied colour and alpha coverage. + Transparent, +} + +/// Crop, aspect, output size and warp of one frame, applied in a single resampling pass. +public sealed record GeometrySettings +{ + /// Region of the working space to show (edge coordinates); null = the whole frame. + public RectD? Crop { get; init; } + + /// + /// Output aspect; the largest crop of this aspect inside is used. + /// takes the ratio of the output size when both dimensions are given, else that of the source frame. + /// + public AspectRatio Aspect { get; init; } = AspectRatio.Source; + + /// Placement of the fitted crop within the slack, −1..1 per axis (0 = centred). + public double OffsetX { get; init; } + + public double OffsetY { get; init; } + + /// Output width in pixels; 0 derives it from the height and aspect, or from the crop. + public int OutputWidth { get; init; } + + /// Output height in pixels; 0 derives it from the width and aspect, or from the crop. + public int OutputHeight { get; init; } + + /// Maps working-space pixel-centre coordinates to source pixel-centre coordinates + /// (e.g. virtual camera composed with the inverse stabilisation correction). + public Affine2D Warp { get; init; } = Affine2D.Identity; + + public BorderMode Border { get; init; } = BorderMode.Clamp; +} + +/// Resolved geometry: output size and the output→source pixel-centre mapping. +public readonly record struct GeometryPlan(int OutputWidth, int OutputHeight, RectD Crop, Affine2D OutputToSource, BorderMode Border); + +/// +/// Geometry stage: crop rectangle, aspect fit, output size and affine warp composed into one output→source +/// map and resampled once with Catmull-Rom (Keys 1981 cubic convolution, a = −0.5). Magnification and 1:1 +/// use the 4×4 kernel (Vector128 rows in the interior); minification widens the kernel by the footprint +/// scale so downsizing does not alias. Rows run in parallel. +/// +public static class GeometryStage +{ + private const int ParallelThreshold = 1 << 16; + + /// Resolves crop, aspect and output size against a source of the given size. + public static GeometryPlan Plan(int sourceWidth, int sourceHeight, GeometrySettings settings) + { + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(sourceWidth); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(sourceHeight); + ArgumentOutOfRangeException.ThrowIfNegative(settings.OutputWidth); + ArgumentOutOfRangeException.ThrowIfNegative(settings.OutputHeight); + var bounds = settings.Crop ?? RectD.FromSize(sourceWidth, sourceHeight); + var ratio = settings.Aspect.IsSource && settings.OutputWidth > 0 && settings.OutputHeight > 0 + ? settings.OutputWidth / (double)settings.OutputHeight + : settings.Aspect.Resolve(sourceWidth, sourceHeight); + var crop = AspectRatio.FitCrop(bounds, ratio, settings.OffsetX, settings.OffsetY); + + int width = settings.OutputWidth, height = settings.OutputHeight; + if (width == 0 && height == 0) + { + width = (int)double.Round(crop.Width); + height = (int)double.Round(crop.Height); + } + else if (height == 0) + { + height = (int)double.Round(width / ratio); + } + else if (width == 0) + { + width = (int)double.Round(height * ratio); + } + width = int.Max(1, width); + height = int.Max(1, height); + + // Output pixel centre u ↦ crop edge coordinate X + (u + ½)·Wc/Wout ↦ working pixel centre (−½). + var sx = crop.Width / width; + var sy = crop.Height / height; + var toWorking = new Affine2D(sx, 0, 0, sy, crop.X - 0.5 + 0.5 * sx, crop.Y - 0.5 + 0.5 * sy); + return new GeometryPlan(width, height, crop, settings.Warp * toWorking, settings.Border); + } + + /// Plans and renders into a new buffer (4 channels when the border is transparent). + public static ImageBuffer Apply(ImageBuffer source, GeometrySettings settings, CancellationToken cancellationToken = default) + { + var plan = Plan(source.Width, source.Height, settings); + var channels = plan.Border == BorderMode.Transparent ? 4 : source.Channels; + var destination = new ImageBuffer(plan.OutputWidth, plan.OutputHeight, channels, clear: false); + try + { + Warp(source, destination, plan.OutputToSource, plan.Border, cancellationToken); + return destination; + } + catch + { + destination.Dispose(); + throw; + } + } + + /// + /// Fills by sampling at + /// (x, y) for every destination pixel centre. With + /// both buffers have the same channel count; with + /// the destination has 4 channels (alpha last, colour premultiplied) + /// and the source 3 (opaque) or 4 (premultiplied). + /// + public static void Warp(ImageBuffer source, ImageBuffer destination, Affine2D outputToSource, BorderMode border = BorderMode.Clamp, CancellationToken cancellationToken = default) + { + var transparent = border == BorderMode.Transparent; + if (transparent ? destination.Channels != 4 || source.Channels < 3 : destination.Channels != source.Channels) + throw new ArgumentException("Destination channels must match the source (Clamp) or be 4 with a colour source (Transparent).", nameof(destination)); + + // Source-axis footprint of one output pixel; > 1 means minification and a widened kernel. + var footprintX = double.Max(1, double.Hypot(outputToSource.M11, outputToSource.M12)); + var footprintY = double.Max(1, double.Hypot(outputToSource.M21, outputToSource.M22)); + var job = new WarpJob(source, destination, outputToSource, transparent, footprintX, footprintY); + var rows = destination.Height; + if ((long)destination.Width * rows < ParallelThreshold) + { + for (var y = 0; y < rows; y++) + { + cancellationToken.ThrowIfCancellationRequested(); + job.Row(y); + } + } + else + { + Parallel.For(0, rows, new ParallelOptions { CancellationToken = cancellationToken }, job.Row); + } + } + + /// Catmull-Rom weights for taps at offsets −1, 0, 1, 2 from floor(x), with t = x − floor(x). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static Vector128 CatmullRomWeights(float t) + { + var t2 = t * t; + var t3 = t2 * t; + return Vector128.Create( + 0.5f * (-t3 + 2 * t2 - t), + 0.5f * (3 * t3 - 5 * t2 + 2), + 0.5f * (-3 * t3 + 4 * t2 + t), + 0.5f * (t3 - t2)); + } + + /// Catmull-Rom kernel value at distance x (support ±2). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static double CatmullRom(double x) + { + x = double.Abs(x); + if (x < 1) return (1.5 * x - 2.5) * x * x + 1; + if (x < 2) return ((-0.5 * x + 2.5) * x - 4) * x + 2; + return 0; + } + + private sealed class WarpJob(ImageBuffer source, ImageBuffer destination, Affine2D map, bool transparent, double footprintX, double footprintY) + { + private readonly int _sw = source.Width; + private readonly int _sh = source.Height; + private readonly int _colourChannels = transparent ? 3 : source.Channels; + private readonly bool _sourceAlpha = transparent && source.Channels == 4; + private readonly bool _wide = footprintX > 1 || footprintY > 1; + + public void Row(int y) + { + if (_wide) WideRow(y); + else CubicRow(y); + } + + private void CubicRow(int y) + { + var src = source.Storage; + var plane = source.PlaneLength; + var dst = destination.Storage; + var dplane = destination.PlaneLength; + var w = _sw; + var h = _sh; + var outWidth = destination.Width; + var channels = transparent ? 4 : _colourChannels; + Span values = stackalloc float[4]; + for (var x = 0; x < outWidth; x++) + { + var (sx, sy) = map.Apply(x, y); + var fx = double.Floor(sx); + var fy = double.Floor(sy); + var o = y * outWidth + x; + if (!(double.Abs(fx) < 1e9 && double.Abs(fy) < 1e9)) + { + WriteOutside(dst, dplane, o, channels); + continue; + } + var ix = (int)fx; + var iy = (int)fy; + var wx = CatmullRomWeights((float)(sx - fx)); + var wy = CatmullRomWeights((float)(sy - fy)); + if (ix >= 1 && iy >= 1 && ix + 2 < w && iy + 2 < h) + { + ref var s0 = ref MemoryMarshal.GetArrayDataReference(src); + var offset = (nuint)((iy - 1) * w + ix - 1); + var stride = (nuint)w; + for (var c = 0; c < channels; c++) + { + if (transparent && c == 3 && !_sourceAlpha) + { + dst[c * dplane + o] = 1f; + continue; + } + var p = offset + (nuint)(c * plane); + var acc = Vector128.LoadUnsafe(ref s0, p) * wy.GetElement(0) + + Vector128.LoadUnsafe(ref s0, p + stride) * wy.GetElement(1) + + Vector128.LoadUnsafe(ref s0, p + 2 * stride) * wy.GetElement(2) + + Vector128.LoadUnsafe(ref s0, p + 3 * stride) * wy.GetElement(3); + dst[c * dplane + o] = Vector128.Dot(acc, wx); + } + continue; + } + + // Border: clamp indices, or drop outside taps and accumulate coverage. + values.Clear(); + float coverage = 0; + for (var j = 0; j < 4; j++) + { + var yy = iy - 1 + j; + var weightY = wy.GetElement(j); + if (transparent && (uint)yy >= (uint)h) continue; + yy = int.Clamp(yy, 0, h - 1); + for (var i = 0; i < 4; i++) + { + var xx = ix - 1 + i; + if (transparent && (uint)xx >= (uint)w) continue; + xx = int.Clamp(xx, 0, w - 1); + var weight = weightY * wx.GetElement(i); + var s = yy * w + xx; + for (var c = 0; c < _colourChannels; c++) values[c] += weight * src[c * plane + s]; + if (_sourceAlpha) values[3] += weight * src[3 * plane + s]; + coverage += weight; + } + } + for (var c = 0; c < _colourChannels; c++) dst[c * dplane + o] = values[c]; + if (transparent) dst[3 * dplane + o] = float.Clamp(_sourceAlpha ? values[3] : coverage, 0f, 1f); + } + } + + private void WideRow(int y) + { + var src = source.Storage; + var plane = source.PlaneLength; + var dst = destination.Storage; + var dplane = destination.PlaneLength; + var w = _sw; + var h = _sh; + var outWidth = destination.Width; + var channels = transparent ? 4 : _colourChannels; + var maxX = (int)double.Ceiling(4 * footprintX) + 2; + var maxY = (int)double.Ceiling(4 * footprintY) + 2; + Span wxs = maxX <= 256 ? stackalloc double[maxX] : new double[maxX]; + Span wys = maxY <= 256 ? stackalloc double[maxY] : new double[maxY]; + Span values = stackalloc double[4]; + for (var x = 0; x < outWidth; x++) + { + var (sx, sy) = map.Apply(x, y); + var o = y * outWidth + x; + if (!(double.Abs(sx) < 1e9 && double.Abs(sy) < 1e9)) + { + WriteOutside(dst, dplane, o, channels); + continue; + } + var x0 = (int)double.Ceiling(sx - 2 * footprintX); + var y0 = (int)double.Ceiling(sy - 2 * footprintY); + var nx = int.Min(maxX, (int)double.Floor(sx + 2 * footprintX) - x0 + 1); + var ny = int.Min(maxY, (int)double.Floor(sy + 2 * footprintY) - y0 + 1); + double sumX = 0, sumY = 0; + for (var i = 0; i < nx; i++) sumX += wxs[i] = CatmullRom((x0 + i - sx) / footprintX); + for (var j = 0; j < ny; j++) sumY += wys[j] = CatmullRom((y0 + j - sy) / footprintY); + var norm = 1 / (sumX * sumY); + + values.Clear(); + double coverage = 0; + for (var j = 0; j < ny; j++) + { + var yy = y0 + j; + if (transparent && (uint)yy >= (uint)h) continue; + var row = int.Clamp(yy, 0, h - 1) * w; + for (var i = 0; i < nx; i++) + { + var xx = x0 + i; + if (transparent && (uint)xx >= (uint)w) continue; + var s = row + int.Clamp(xx, 0, w - 1); + var weight = wys[j] * wxs[i]; + for (var c = 0; c < _colourChannels; c++) values[c] += weight * src[c * plane + s]; + if (_sourceAlpha) values[3] += weight * src[3 * plane + s]; + coverage += weight; + } + } + for (var c = 0; c < _colourChannels; c++) dst[c * dplane + o] = (float)(values[c] * norm); + if (transparent) dst[3 * dplane + o] = (float)double.Clamp((_sourceAlpha ? values[3] : coverage) * norm, 0, 1); + } + } + + private void WriteOutside(float[] dst, int dplane, int o, int channels) + { + // Non-finite or absurd coordinates: transparent, or the clamped corner colour for Clamp. + for (var c = 0; c < channels; c++) dst[c * dplane + o] = transparent ? 0f : source.Storage[c * source.PlaneLength]; + } + } +} diff --git a/tests/Titano.Core.Tests/Geometry/Affine2DTests.cs b/tests/Titano.Core.Tests/Geometry/Affine2DTests.cs new file mode 100644 index 00000000..357b1cd9 --- /dev/null +++ b/tests/Titano.Core.Tests/Geometry/Affine2DTests.cs @@ -0,0 +1,56 @@ +using Titano.Core.Imaging.Geometry; +using Titano.Testing; + +namespace Titano.Core.Tests.Geometry; + +public sealed class Affine2DTests +{ + [Test] + public void CompositionAppliesInnerFirst() + { + var scale = Affine2D.Scaling(2, 3); + var move = Affine2D.Translation(5, -1); + var (x, y) = (move * scale).Apply(1, 1); + Assert.Approximately(7, x, 1e-15); + Assert.Approximately(2, y, 1e-15); + Assert.Equal(move * scale, scale.Then(move)); + } + + [Test] + public void InverseUndoesTheMap() + { + var map = Affine2D.Similarity(1.3, 0.4, 12, -7) * Affine2D.Scaling(1, 0.8); + var inverse = map.Invert(); + Assert.True((inverse * map).IsIdentity(1e-12)); + Assert.True((map * inverse).IsIdentity(1e-12)); + var (x, y) = map.Apply(3.5, -2.25); + var (bx, by) = inverse.Apply(x, y); + Assert.Within(3.5, -2.25, bx, by, 1e-12); + } + + [Test] + public void SingularMapsDoNotInvert() + { + Assert.False(new Affine2D(1, 2, 2, 4, 0, 0).TryInvert(out _)); + Assert.Throws(() => Affine2D.Scaling(0, 1).Invert()); + } + + [Test] + public void RotationAboutPivotKeepsThePivot() + { + var map = Affine2D.About(Affine2D.Rotation(0.7), 40, 30); + var (x, y) = map.Apply(40, 30); + Assert.Within(40, 30, x, y, 1e-12); + var (vx, vy) = map.ApplyVector(1, 0); + Assert.Within(double.Cos(0.7), double.Sin(0.7), vx, vy, 1e-15); + } + + [Test] + public void SimilarityDecomposes() + { + var map = Affine2D.Similarity(1.25, -0.3, 4, 5); + Assert.Approximately(1.25, map.Scale, 1e-12); + Assert.Approximately(-0.3, map.Angle, 1e-12); + Assert.Approximately(1.25 * 1.25, map.Determinant, 1e-12); + } +} diff --git a/tests/Titano.Core.Tests/Geometry/AspectRatioTests.cs b/tests/Titano.Core.Tests/Geometry/AspectRatioTests.cs new file mode 100644 index 00000000..797553a6 --- /dev/null +++ b/tests/Titano.Core.Tests/Geometry/AspectRatioTests.cs @@ -0,0 +1,54 @@ +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 _)); + } +} diff --git a/tests/Titano.Core.Tests/Geometry/GeometryStageTests.cs b/tests/Titano.Core.Tests/Geometry/GeometryStageTests.cs new file mode 100644 index 00000000..4d98c6c9 --- /dev/null +++ b/tests/Titano.Core.Tests/Geometry/GeometryStageTests.cs @@ -0,0 +1,133 @@ +using Titano.Core.Imaging; +using Titano.Core.Imaging.Geometry; +using Titano.Core.Tests.Motion; +using Titano.Testing; + +namespace Titano.Core.Tests.Geometry; + +public sealed class GeometryStageTests +{ + private const int Size = 128; + + private readonly TestContext _context; + + public GeometryStageTests(TestContext context) => _context = context; + + [Test] + public void IdentityWarpReproducesTheImage() + { + using var source = Texture(sigma: 1.0); + using var output = GeometryStage.Apply(source, new GeometrySettings()); + Assert.Equal(Size, output.Width); + Assert.Equal(3, output.Channels); + var psnr = SyntheticImages.Psnr(source.Plane(1), output.Plane(1), Size, 0, 0, Size, Size); + _context.Log($"identity PSNR {psnr:F1} dB"); + Assert.GreaterOrEqual(psnr, 60); + Assert.True(source.Data.SequenceEqual(output.Data)); + } + + [Test] + public void HalfPixelShiftAndBackStaysWithinBounds() + { + using var source = Texture(sigma: 2.0); + using var shifted = new ImageBuffer(Size, Size); + using var back = new ImageBuffer(Size, Size); + GeometryStage.Warp(source, shifted, Affine2D.Translation(0.5, 0.5)); + GeometryStage.Warp(shifted, back, Affine2D.Translation(-0.5, -0.5)); + var psnr = SyntheticImages.Psnr(source.Plane(0), back.Plane(0), Size, 4, 4, Size - 4, Size - 4); + _context.Log($"0.5 px round trip PSNR {psnr:F1} dB"); + Assert.GreaterOrEqual(psnr, 40); + } + + [Test] + public void LinearRampsAreReproducedUnderSubPixelWarps() + { + using var ramp = new ImageBuffer(64, 64, 1); + for (var y = 0; y < 64; y++) + for (var x = 0; x < 64; x++) + ramp[0, x, y] = 0.01f * x + 0.005f * y; + using var output = new ImageBuffer(64, 64, 1); + var map = Affine2D.About(Affine2D.Similarity(0.8, 0.2, 0, 0), 32, 32); + GeometryStage.Warp(ramp, output, map); + for (var y = 8; y < 56; y++) + { + for (var x = 8; x < 56; x++) + { + var (sx, sy) = map.Apply(x, y); + Assert.Approximately(0.01 * sx + 0.005 * sy, output[0, x, y], 2e-6); + } + } + } + + [Test] + public void TransparentBordersCarryCoverage() + { + using var source = Texture(sigma: 1.0); + using var output = GeometryStage.Apply(source, new GeometrySettings + { + Warp = Affine2D.Translation(-10, 0), + Border = BorderMode.Transparent, + }); + Assert.Equal(4, output.Channels); + for (var y = 0; y < Size; y += 7) + { + Assert.Equal(0f, output[3, 3, y]); + Assert.Equal(0f, output[0, 3, y]); + Assert.Equal(1f, output[3, 40, y]); + Assert.Equal(source[1, 30, y], output[1, 40, y]); + } + } + + [Test] + public void ClampReplicatesEdges() + { + using var source = Texture(sigma: 1.0); + using var output = GeometryStage.Apply(source, new GeometrySettings { Warp = Affine2D.Translation(-10, 0) }); + for (var y = 0; y < Size; y += 5) Assert.Approximately(source[2, 0, y], output[2, 2, y], 1e-6); + } + + [Test] + public void PlanFitsAspectAndMapsPixelCentres() + { + var plan = GeometryStage.Plan(1920, 1080, new GeometrySettings { Aspect = AspectRatio.Square, OutputWidth = 540 }); + Assert.Equal(540, plan.OutputWidth); + Assert.Equal(540, plan.OutputHeight); + Assert.Equal(new RectD(420, 0, 1080, 1080), plan.Crop); + var (x, y) = plan.OutputToSource.Apply(0, 0); + Assert.Within(420.5, 0.5, x, y, 1e-12); + + var cropped = GeometryStage.Plan(800, 600, new GeometrySettings { Crop = new RectD(100, 50, 200, 100), OutputWidth = 400, OutputHeight = 200 }); + Assert.Equal(new RectD(100, 50, 200, 100), cropped.Crop); + var (cx, cy) = cropped.OutputToSource.Apply(0, 0); + Assert.Within(99.75, 49.75, cx, cy, 1e-12); + + var vertical = GeometryStage.Plan(1920, 1080, new GeometrySettings { Aspect = AspectRatio.Vertical, OutputHeight = 1920 }); + Assert.Equal(1080, vertical.OutputWidth); + } + + [Test] + public void DownscalingDoesNotAlias() + { + using var checker = new ImageBuffer(256, 256, 1); + for (var y = 0; y < 256; y++) + for (var x = 0; x < 256; x++) + checker[0, x, y] = (x + y) % 2 == 0 ? 1f : 0f; + using var small = GeometryStage.Apply(checker, new GeometrySettings { OutputWidth = 64 }); + Assert.Equal(64, small.Height); + for (var y = 2; y < 62; y++) + for (var x = 2; x < 62; x++) + Assert.Approximately(0.5, small[0, x, y], 0.02); + } + + [Test] + public void CancellationStopsTheWarp() + { + using var source = Texture(sigma: 1.0); + using var cts = new CancellationTokenSource(); + cts.Cancel(); + Assert.Throws(() => GeometryStage.Apply(source, new GeometrySettings(), cts.Token)); + } + + private static ImageBuffer Texture(double sigma) => + SyntheticImages.ToRgb(SyntheticImages.Crop(SyntheticImages.Noise(Size, sigma, seed: 21), Size, 0, 0, Size, Size), Size, Size); +}