diff --git a/tests/ImageSharp.Tests/Processing/Transforms/TaperTransform.cs b/src/ImageSharp/Processing/Transforms/TaperTransform.cs similarity index 62% rename from tests/ImageSharp.Tests/Processing/Transforms/TaperTransform.cs rename to src/ImageSharp/Processing/Transforms/TaperTransform.cs index 74d1d42c7d..f6f331e320 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/TaperTransform.cs +++ b/src/ImageSharp/Processing/Transforms/TaperTransform.cs @@ -1,15 +1,73 @@ -using System.Numerics; +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Numerics; using SixLabors.Primitives; -namespace SixLabors.ImageSharp.Tests.Processing.Transforms +namespace SixLabors.ImageSharp.Processing.Transforms { - public enum TaperSide { Left, Top, Right, Bottom } + /// + /// Enumerates the various options which determine which side to taper + /// + public enum TaperSide + { + /// + /// Taper the left side + /// + Left, + + /// + /// Taper the top side + /// + Top, + + /// + /// Taper the right side + /// + Right, + + /// + /// Taper the bottom side + /// + Bottom + } - public enum TaperCorner { LeftOrTop, RightOrBottom, Both } + /// + /// Enumerates the various options which determine how to taper corners + /// + public enum TaperCorner + { + /// + /// Taper the left or top corner + /// + LeftOrTop, + + /// + /// Taper the right or bottom corner + /// + RightOrBottom, + + /// + /// Taper the both sets of corners + /// + Both + } + /// + /// Provides methods for the creation of generalized tapering projective transforms. + /// + /// public static class TaperTransform { - public static Matrix4x4 Make(Size size, TaperSide taperSide, TaperCorner taperCorner, float taperFraction) + /// + /// Creates a matrix that performs a tapering projective transform. + /// + /// The resultant size of the tapered output + /// The taper side option + /// The taper corner option + /// The amount to taper + /// The + public static Matrix4x4 Create(Size size, TaperSide taperSide, TaperCorner taperCorner, float taperFraction) { Matrix4x4 matrix = Matrix4x4.Identity; @@ -35,6 +93,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms matrix.M32 = size.Height * (1 - taperFraction) / 2; break; } + break; case TaperSide.Top: @@ -57,6 +116,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms matrix.M31 = size.Width * (1 - taperFraction) / 2; break; } + break; case TaperSide.Right: @@ -76,6 +136,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms matrix.M12 = (size.Height / 2) * matrix.M13; break; } + break; case TaperSide.Bottom: @@ -95,9 +156,11 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms matrix.M21 = (size.Width / 2) * matrix.M23; break; } + break; } + return matrix; } } -} +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs index c9354049d1..9380d4e185 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs @@ -39,25 +39,24 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms { 0, 1f, 2f, 0, 0 }, }; - public static readonly TheoryData ResamplerNames = - new TheoryData - { - nameof(KnownResamplers.Bicubic), - nameof(KnownResamplers.Box), - nameof(KnownResamplers.CatmullRom), - nameof(KnownResamplers.Hermite), - nameof(KnownResamplers.Lanczos2), - nameof(KnownResamplers.Lanczos3), - nameof(KnownResamplers.Lanczos5), - nameof(KnownResamplers.Lanczos8), - nameof(KnownResamplers.MitchellNetravali), - nameof(KnownResamplers.NearestNeighbor), - nameof(KnownResamplers.Robidoux), - nameof(KnownResamplers.RobidouxSharp), - nameof(KnownResamplers.Spline), - nameof(KnownResamplers.Triangle), - nameof(KnownResamplers.Welch), - }; + public static readonly TheoryData ResamplerNames = new TheoryData + { + nameof(KnownResamplers.Bicubic), + nameof(KnownResamplers.Box), + nameof(KnownResamplers.CatmullRom), + nameof(KnownResamplers.Hermite), + nameof(KnownResamplers.Lanczos2), + nameof(KnownResamplers.Lanczos3), + nameof(KnownResamplers.Lanczos5), + nameof(KnownResamplers.Lanczos8), + nameof(KnownResamplers.MitchellNetravali), + nameof(KnownResamplers.NearestNeighbor), + nameof(KnownResamplers.Robidoux), + nameof(KnownResamplers.RobidouxSharp), + nameof(KnownResamplers.Spline), + nameof(KnownResamplers.Triangle), + nameof(KnownResamplers.Welch), + }; public static readonly TheoryData Transform_DoesNotCreateEdgeArtifacts_ResamplerNames = new TheoryData @@ -124,7 +123,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms image.CompareToReferenceOutput(ValidatorComparer, provider, testOutputDetails); } } - + [Theory] [WithTestPatternImages(96, 96, PixelTypes.Rgba32, 50, 0.8f)] public void Transform_RotateScale_ManuallyCentered(TestImageProvider provider, float angleDeg, float s) @@ -166,7 +165,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms using (Image image = provider.GetImage()) { var m = Matrix3x2.CreateScale(2.0F, 1.5F); - + image.Mutate(i => i.Transform(m, KnownResamplers.Spline, rectangle)); image.DebugSave(provider); diff --git a/tests/ImageSharp.Tests/Processing/Transforms/ProjectiveTransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/ProjectiveTransformTests.cs index ea90415e46..743b04a0ef 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/ProjectiveTransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/ProjectiveTransformTests.cs @@ -1,15 +1,76 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System; +using System.Numerics; +using System.Reflection; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; +using SixLabors.ImageSharp.Processing.Transforms; +using SixLabors.ImageSharp.Processing.Transforms.Resamplers; using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; +using Xunit; using Xunit.Abstractions; namespace SixLabors.ImageSharp.Tests.Processing.Transforms { public class ProjectiveTransformTests { - private readonly ITestOutputHelper Output; + // private readonly ITestOutputHelper Output; private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.005f, 3); + + public static readonly TheoryData ResamplerNames = new TheoryData + { + nameof(KnownResamplers.Bicubic), + nameof(KnownResamplers.Box), + nameof(KnownResamplers.CatmullRom), + nameof(KnownResamplers.Hermite), + nameof(KnownResamplers.Lanczos2), + nameof(KnownResamplers.Lanczos3), + nameof(KnownResamplers.Lanczos5), + nameof(KnownResamplers.Lanczos8), + nameof(KnownResamplers.MitchellNetravali), + nameof(KnownResamplers.NearestNeighbor), + nameof(KnownResamplers.Robidoux), + nameof(KnownResamplers.RobidouxSharp), + nameof(KnownResamplers.Spline), + nameof(KnownResamplers.Triangle), + nameof(KnownResamplers.Welch), + }; + + [Theory] + [WithTestPatternImages(nameof(ResamplerNames), 150, 150, PixelTypes.Rgba32)] + public void Transform_WithSampler(TestImageProvider provider, string resamplerName) + where TPixel : struct, IPixel + { + IResampler sampler = GetResampler(resamplerName); + using (Image image = provider.GetImage()) + { + Matrix4x4 m = TaperTransform.Create(image.Size(), TaperSide.Right, TaperCorner.Both, .5F); + + image.Mutate(i => + { + i.Transform(m, sampler); + }); + + image.DebugSave(provider, resamplerName); + + // TODO: Enable and add more tests. + // image.CompareToReferenceOutput(ValidatorComparer, provider, resamplerName); + } + } + + private static IResampler GetResampler(string name) + { + PropertyInfo property = typeof(KnownResamplers).GetTypeInfo().GetProperty(name); + + if (property == null) + { + throw new Exception("Invalid property name!"); + } + + return (IResampler)property.GetValue(null); + } } }