using Titano.Core.Windows.Wic; using Titano.Testing; namespace Titano.Core.Windows.Tests.Wic; public sealed class OrientationMapTests { [Theory] [Case(1)] [Case(2)] [Case(3)] [Case(4)] [Case(5)] [Case(6)] [Case(7)] [Case(8)] public void MatchesTheExifDefinition(int orientation) { const int width = 5, height = 3; var map = OrientationMap.Create(orientation, width, height); var swapped = OrientationMap.SwapsAxes(orientation); Assert.Equal(swapped ? height : width, map.Width); Assert.Equal(swapped ? width : height, map.Height); var seen = new HashSet(); for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { var (dx, dy) = Reference(orientation, x, y, width, height); Assert.Equal((dy * map.Width) + dx, map.IndexOf(x, y), $"orientation {orientation}, pixel ({x},{y})"); seen.Add(map.IndexOf(x, y)); } } Assert.Equal(width * height, seen.Count); } [Test] public void UnknownValuesActAsIdentity() => Assert.Equal(OrientationMap.Create(1, 4, 2), OrientationMap.Create(0, 4, 2)); [Theory] [Case(4000u, 3000u, 1920, 1920u, 1440u)] [Case(400u, 100u, 100, 100u, 25u)] [Case(3000u, 4000u, 1000, 750u, 1000u)] [Case(640u, 480u, 0, 640u, 480u)] [Case(640u, 480u, 2000, 640u, 480u)] [Case(2u, 1000u, 10, 1u, 10u)] public void TargetSizeFitsTheLongEdge(uint width, uint height, int maxLongEdge, uint expectedWidth, uint expectedHeight) { var (w, h) = WicDecoderProvider.TargetSize(width, height, maxLongEdge); Assert.Equal(expectedWidth, w); Assert.Equal(expectedHeight, h); } /// Displayed position of stored pixel (x, y), straight from the Exif 2.3 orientation table. private static (int X, int Y) Reference(int orientation, int x, int y, int w, int h) => orientation switch { 2 => (w - 1 - x, y), 3 => (w - 1 - x, h - 1 - y), 4 => (x, h - 1 - y), 5 => (y, x), 6 => (h - 1 - y, x), 7 => (h - 1 - y, w - 1 - x), 8 => (y, w - 1 - x), _ => (x, y), }; }