|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
| 3 | + |
| 4 | +using System.Numerics; |
| 5 | +using SixLabors.ImageSharp.Metadata.Profiles.Exif; |
| 6 | +using SixLabors.ImageSharp.Processing; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace SixLabors.ImageSharp.Web.Tests.Helpers |
| 10 | +{ |
| 11 | + public class ExifOrientationUtilitiesTests |
| 12 | + { |
| 13 | + // 150 |
| 14 | + // ┌─────┬─────┬─────┬─────┬─────┬─────┐ |
| 15 | + // │ │ │ │ │ │ │ |
| 16 | + // │ │ FFFFFFFFFFFFFFF │ │ |
| 17 | + // ├─────X────F┌─────┬─────┬─────┼─────┤ |
| 18 | + // │ │ F│ │ │ │ │ |
| 19 | + // │ │ FFFFFFFFFFFFFFF │ │ |
| 20 | + // ├─────┼────F┌─────┬─────┬─────┼─────┤ 100 |
| 21 | + // │ │ F│ │ │ │ │ |
| 22 | + // │ │ F│ │ │ │ │ |
| 23 | + // ├─────┼────F├─────┼─────┼─────┼─────┤ |
| 24 | + // │ │ F│ │ │ │ │ |
| 25 | + // │ │ │ │ │ │ │ |
| 26 | + // └─────┴─────┴─────┴─────┴─────┴─────┘ |
| 27 | + public static TheoryData<Vector2, Vector2, Vector2, ushort, Vector2> TransformVectorData = |
| 28 | + new() |
| 29 | + { |
| 30 | + { new Vector2(25F, 25F), Vector2.Zero, new Vector2(150, 100), ExifOrientationMode.Unknown, new Vector2(25F, 25F) }, |
| 31 | + { new Vector2(25F, 25F), Vector2.Zero, new Vector2(150, 100), ExifOrientationMode.TopLeft, new Vector2(25F, 25F) }, |
| 32 | + { new Vector2(25F, 25F), Vector2.Zero, new Vector2(150, 100), ExifOrientationMode.TopRight, new Vector2(125F, 25F) }, |
| 33 | + { new Vector2(25F, 25F), Vector2.Zero, new Vector2(150, 100), ExifOrientationMode.BottomRight, new Vector2(125F, 75F) }, |
| 34 | + { new Vector2(25F, 25F), Vector2.Zero, new Vector2(150, 100), ExifOrientationMode.BottomLeft, new Vector2(25F, 75F) }, |
| 35 | + { new Vector2(25F, 25F), Vector2.Zero, new Vector2(150, 100), ExifOrientationMode.LeftTop, new Vector2(25F, 25F) }, |
| 36 | + { new Vector2(25F, 25F), Vector2.Zero, new Vector2(150, 100), ExifOrientationMode.RightTop, new Vector2(25F, 125F) }, |
| 37 | + { new Vector2(25F, 25F), Vector2.Zero, new Vector2(150, 100), ExifOrientationMode.RightBottom, new Vector2(75F, 125F) }, |
| 38 | + { new Vector2(25F, 25F), Vector2.Zero, new Vector2(150, 100), ExifOrientationMode.LeftBottom, new Vector2(75F, 25F) } |
| 39 | + }; |
| 40 | + |
| 41 | + [Theory] |
| 42 | + [MemberData(nameof(TransformVectorData))] |
| 43 | + public void CanTransformVector(Vector2 position, Vector2 min, Vector2 max, ushort orientation, Vector2 expected) |
| 44 | + { |
| 45 | + Vector2 actual = ExifOrientationUtilities.Transform(position, min, max, orientation); |
| 46 | + |
| 47 | + Assert.Equal(expected.X, actual.X, 4); |
| 48 | + Assert.Equal(expected.Y, actual.Y, 4); |
| 49 | + } |
| 50 | + |
| 51 | + public static TheoryData<Size, ushort, Size> TransformSizeData = |
| 52 | + new() |
| 53 | + { |
| 54 | + { new Size(150, 100), ExifOrientationMode.Unknown, new Size(150, 100) }, |
| 55 | + { new Size(150, 100), ExifOrientationMode.TopLeft, new Size(150, 100) }, |
| 56 | + { new Size(150, 100), ExifOrientationMode.TopRight, new Size(150, 100) }, |
| 57 | + { new Size(150, 100), ExifOrientationMode.BottomRight, new Size(150, 100) }, |
| 58 | + { new Size(150, 100), ExifOrientationMode.BottomLeft, new Size(150, 100) }, |
| 59 | + { new Size(150, 100), ExifOrientationMode.LeftTop, new Size(100, 150) }, |
| 60 | + { new Size(150, 100), ExifOrientationMode.RightTop, new Size(100, 150) }, |
| 61 | + { new Size(150, 100), ExifOrientationMode.RightBottom, new Size(100, 150) }, |
| 62 | + { new Size(150, 100), ExifOrientationMode.LeftBottom, new Size(100, 150) }, |
| 63 | + }; |
| 64 | + |
| 65 | + [Theory] |
| 66 | + [MemberData(nameof(TransformSizeData))] |
| 67 | + public void CanTransformSize(Size size, ushort orientation, Size expected) |
| 68 | + { |
| 69 | + Size actual = ExifOrientationUtilities.Transform(size, orientation); |
| 70 | + |
| 71 | + Assert.Equal(expected, actual); |
| 72 | + } |
| 73 | + |
| 74 | + public static TheoryData<AnchorPositionMode, ushort, AnchorPositionMode> TransformAnchorData = |
| 75 | + new() |
| 76 | + { |
| 77 | + { AnchorPositionMode.Center, ExifOrientationMode.TopLeft, AnchorPositionMode.Center }, |
| 78 | + { AnchorPositionMode.Center, ExifOrientationMode.BottomLeft, AnchorPositionMode.Center }, |
| 79 | + { AnchorPositionMode.Center, ExifOrientationMode.LeftBottom, AnchorPositionMode.Center }, |
| 80 | + { AnchorPositionMode.Top, ExifOrientationMode.BottomLeft, AnchorPositionMode.Bottom }, |
| 81 | + { AnchorPositionMode.Top, ExifOrientationMode.BottomRight, AnchorPositionMode.Bottom }, |
| 82 | + { AnchorPositionMode.Top, ExifOrientationMode.LeftTop, AnchorPositionMode.Left }, |
| 83 | + { AnchorPositionMode.Top, ExifOrientationMode.RightTop, AnchorPositionMode.Left }, |
| 84 | + { AnchorPositionMode.Top, ExifOrientationMode.LeftBottom, AnchorPositionMode.Right }, |
| 85 | + { AnchorPositionMode.Top, ExifOrientationMode.RightBottom, AnchorPositionMode.Right }, |
| 86 | + { AnchorPositionMode.Bottom, ExifOrientationMode.BottomLeft, AnchorPositionMode.Top }, |
| 87 | + { AnchorPositionMode.Bottom, ExifOrientationMode.BottomRight, AnchorPositionMode.Top }, |
| 88 | + { AnchorPositionMode.Bottom, ExifOrientationMode.LeftTop, AnchorPositionMode.Right }, |
| 89 | + { AnchorPositionMode.Bottom, ExifOrientationMode.RightTop, AnchorPositionMode.Right }, |
| 90 | + { AnchorPositionMode.Bottom, ExifOrientationMode.LeftBottom, AnchorPositionMode.Left }, |
| 91 | + { AnchorPositionMode.Bottom, ExifOrientationMode.RightBottom, AnchorPositionMode.Left }, |
| 92 | + { AnchorPositionMode.Left, ExifOrientationMode.TopRight, AnchorPositionMode.Right }, |
| 93 | + { AnchorPositionMode.Left, ExifOrientationMode.BottomRight, AnchorPositionMode.Right }, |
| 94 | + { AnchorPositionMode.Left, ExifOrientationMode.LeftTop, AnchorPositionMode.Top }, |
| 95 | + { AnchorPositionMode.Left, ExifOrientationMode.LeftBottom, AnchorPositionMode.Top }, |
| 96 | + { AnchorPositionMode.Left, ExifOrientationMode.RightTop, AnchorPositionMode.Bottom }, |
| 97 | + { AnchorPositionMode.Left, ExifOrientationMode.RightBottom, AnchorPositionMode.Bottom }, |
| 98 | + { AnchorPositionMode.Right, ExifOrientationMode.TopRight, AnchorPositionMode.Left }, |
| 99 | + { AnchorPositionMode.Right, ExifOrientationMode.BottomRight, AnchorPositionMode.Left }, |
| 100 | + { AnchorPositionMode.Right, ExifOrientationMode.LeftTop, AnchorPositionMode.Bottom }, |
| 101 | + { AnchorPositionMode.Right, ExifOrientationMode.LeftBottom, AnchorPositionMode.Bottom }, |
| 102 | + { AnchorPositionMode.Right, ExifOrientationMode.RightTop, AnchorPositionMode.Top }, |
| 103 | + { AnchorPositionMode.Right, ExifOrientationMode.RightBottom, AnchorPositionMode.Top }, |
| 104 | + { AnchorPositionMode.TopLeft, ExifOrientationMode.TopRight, AnchorPositionMode.TopRight }, |
| 105 | + { AnchorPositionMode.TopLeft, ExifOrientationMode.LeftBottom, AnchorPositionMode.TopRight }, |
| 106 | + { AnchorPositionMode.TopLeft, ExifOrientationMode.BottomRight, AnchorPositionMode.BottomLeft }, |
| 107 | + { AnchorPositionMode.TopLeft, ExifOrientationMode.RightTop, AnchorPositionMode.BottomLeft }, |
| 108 | + { AnchorPositionMode.TopLeft, ExifOrientationMode.BottomLeft, AnchorPositionMode.BottomRight }, |
| 109 | + { AnchorPositionMode.TopLeft, ExifOrientationMode.RightBottom, AnchorPositionMode.BottomRight }, |
| 110 | + { AnchorPositionMode.TopRight, ExifOrientationMode.TopRight, AnchorPositionMode.TopLeft }, |
| 111 | + { AnchorPositionMode.TopRight, ExifOrientationMode.RightTop, AnchorPositionMode.TopLeft }, |
| 112 | + { AnchorPositionMode.TopRight, ExifOrientationMode.BottomLeft, AnchorPositionMode.BottomRight }, |
| 113 | + { AnchorPositionMode.TopRight, ExifOrientationMode.LeftBottom, AnchorPositionMode.BottomRight }, |
| 114 | + { AnchorPositionMode.TopRight, ExifOrientationMode.BottomRight, AnchorPositionMode.BottomLeft }, |
| 115 | + { AnchorPositionMode.TopRight, ExifOrientationMode.LeftTop, AnchorPositionMode.BottomLeft }, |
| 116 | + { AnchorPositionMode.BottomRight, ExifOrientationMode.TopRight, AnchorPositionMode.BottomLeft }, |
| 117 | + { AnchorPositionMode.BottomRight, ExifOrientationMode.LeftBottom, AnchorPositionMode.BottomLeft }, |
| 118 | + { AnchorPositionMode.BottomRight, ExifOrientationMode.BottomLeft, AnchorPositionMode.TopRight }, |
| 119 | + { AnchorPositionMode.BottomRight, ExifOrientationMode.RightTop, AnchorPositionMode.TopRight }, |
| 120 | + { AnchorPositionMode.BottomRight, ExifOrientationMode.BottomRight, AnchorPositionMode.TopLeft }, |
| 121 | + { AnchorPositionMode.BottomRight, ExifOrientationMode.RightBottom, AnchorPositionMode.TopLeft }, |
| 122 | + { AnchorPositionMode.BottomLeft, ExifOrientationMode.TopRight, AnchorPositionMode.BottomRight }, |
| 123 | + { AnchorPositionMode.BottomLeft, ExifOrientationMode.RightTop, AnchorPositionMode.BottomRight }, |
| 124 | + { AnchorPositionMode.BottomLeft, ExifOrientationMode.BottomLeft, AnchorPositionMode.TopLeft }, |
| 125 | + { AnchorPositionMode.BottomLeft, ExifOrientationMode.LeftBottom, AnchorPositionMode.TopLeft }, |
| 126 | + { AnchorPositionMode.BottomLeft, ExifOrientationMode.BottomRight, AnchorPositionMode.TopRight }, |
| 127 | + { AnchorPositionMode.BottomLeft, ExifOrientationMode.LeftTop, AnchorPositionMode.TopRight }, |
| 128 | + }; |
| 129 | + |
| 130 | + [Theory] |
| 131 | + [MemberData(nameof(TransformAnchorData))] |
| 132 | + public void CanTransformAnchor(AnchorPositionMode anchor, ushort orientation, AnchorPositionMode expected) |
| 133 | + { |
| 134 | + AnchorPositionMode actual = ExifOrientationUtilities.Transform(anchor, orientation); |
| 135 | + |
| 136 | + Assert.Equal(expected, actual); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments