diff --git a/NuGet.config b/NuGet.config index b2c967cc97..322105d4d4 100644 --- a/NuGet.config +++ b/NuGet.config @@ -1,6 +1,7 @@  + diff --git a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj index eb3c29dd98..3e320dccc7 100644 --- a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj +++ b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj @@ -36,9 +36,9 @@ - - - + + + All diff --git a/src/ImageSharp/Common/Helpers/ImageMaths.cs b/src/ImageSharp/Common/Helpers/ImageMaths.cs index 7d781e77fe..75c9190d24 100644 --- a/src/ImageSharp/Common/Helpers/ImageMaths.cs +++ b/src/ImageSharp/Common/Helpers/ImageMaths.cs @@ -139,27 +139,6 @@ namespace SixLabors.ImageSharp return new Rectangle(topLeft.X, topLeft.Y, bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y); } - /// - /// Gets the bounding from the given matrix. - /// - /// The source rectangle. - /// The transformation matrix. - /// - /// The . - /// - public static Rectangle GetBoundingRectangle(Rectangle rectangle, Matrix3x2 matrix) - { - var leftTop = Vector2.Transform(new Vector2(rectangle.Left, rectangle.Top), matrix); - var rightTop = Vector2.Transform(new Vector2(rectangle.Right, rectangle.Top), matrix); - var leftBottom = Vector2.Transform(new Vector2(rectangle.Left, rectangle.Bottom), matrix); - var rightBottom = Vector2.Transform(new Vector2(rectangle.Right, rectangle.Bottom), matrix); - - Vector2[] allCorners = { leftTop, rightTop, leftBottom, rightBottom }; - float extentX = allCorners.Select(v => v.X).Max() - allCorners.Select(v => v.X).Min(); - float extentY = allCorners.Select(v => v.Y).Max() - allCorners.Select(v => v.Y).Min(); - return new Rectangle(0, 0, (int)extentX, (int)extentY); - } - /// /// Finds the bounding rectangle based on the first instance of any color component other /// than the given one. diff --git a/src/ImageSharp/Image/ImageFrame{TPixel}.cs b/src/ImageSharp/Image/ImageFrame{TPixel}.cs index 45ed5f053a..e39cc1ab2f 100644 --- a/src/ImageSharp/Image/ImageFrame{TPixel}.cs +++ b/src/ImageSharp/Image/ImageFrame{TPixel}.cs @@ -10,6 +10,7 @@ using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.MetaData; using SixLabors.ImageSharp.PixelFormats; +using SixLabors.Primitives; namespace SixLabors.ImageSharp { @@ -53,6 +54,16 @@ namespace SixLabors.ImageSharp this.MetaData = metaData; } + /// + /// Initializes a new instance of the class. + /// + /// The of the frame. + /// The meta data. + internal ImageFrame(Size size, ImageFrameMetaData metaData) + : this(size.Width, size.Height, metaData) + { + } + /// /// Initializes a new instance of the class. /// diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index 1d22e59cb2..9d30030da6 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -34,7 +34,7 @@ - + All diff --git a/src/ImageSharp/Memory/Buffer{T}.cs b/src/ImageSharp/Memory/Buffer{T}.cs index f5c9ed00a1..67af23426a 100644 --- a/src/ImageSharp/Memory/Buffer{T}.cs +++ b/src/ImageSharp/Memory/Buffer{T}.cs @@ -142,7 +142,7 @@ namespace SixLabors.ImageSharp.Memory [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Buffer CreateClean(int count) { - Buffer buffer = new Buffer(count); + var buffer = new Buffer(count); buffer.Clear(); return buffer; } diff --git a/src/ImageSharp/PixelFormats/Rgb24.cs b/src/ImageSharp/PixelFormats/Rgb24.cs index 5a12cff201..d867d9065e 100644 --- a/src/ImageSharp/PixelFormats/Rgb24.cs +++ b/src/ImageSharp/PixelFormats/Rgb24.cs @@ -126,5 +126,11 @@ namespace SixLabors.ImageSharp.PixelFormats dest.B = this.B; dest.A = 255; } + + /// + public override string ToString() + { + return $"({this.R},{this.G},{this.B})"; + } } } \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/Rgba32.cs b/src/ImageSharp/PixelFormats/Rgba32.cs index 51647fc1f9..83a35f1895 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.cs @@ -364,7 +364,7 @@ namespace SixLabors.ImageSharp /// A string representation of the packed vector. public override string ToString() { - return this.ToVector4().ToString(); + return $"({this.R},{this.G},{this.B},{this.A})"; } /// diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs new file mode 100644 index 0000000000..07c247d734 --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs @@ -0,0 +1,245 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Threading.Tasks; +using SixLabors.ImageSharp.Advanced; +using SixLabors.ImageSharp.Helpers; +using SixLabors.ImageSharp.Memory; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.Primitives; + +namespace SixLabors.ImageSharp.Processing.Processors +{ + /// + /// Provides the base methods to perform affine transforms on an image. + /// + /// The pixel format. + internal class AffineTransformProcessor : InterpolatedTransformProcessorBase + where TPixel : struct, IPixel + { + private Size targetDimensions; + + /// + /// Initializes a new instance of the class. + /// + /// The transform matrix + public AffineTransformProcessor(Matrix3x2 matrix) + : this(matrix, KnownResamplers.Bicubic) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The transform matrix + /// The sampler to perform the transform operation. + public AffineTransformProcessor(Matrix3x2 matrix, IResampler sampler) + : this(matrix, sampler, Size.Empty) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The transform matrix + /// The sampler to perform the transform operation. + /// The target dimensions to constrain the transformed image to. + public AffineTransformProcessor(Matrix3x2 matrix, IResampler sampler, Size targetDimensions) + : base(sampler) + { + // Tansforms are inverted else the output is the opposite of the expected. + Matrix3x2.Invert(matrix, out matrix); + this.TransformMatrix = matrix; + this.targetDimensions = targetDimensions; + } + + /// + /// Gets the matrix used to supply the affine transform + /// + public Matrix3x2 TransformMatrix { get; } + + /// + protected override Image CreateDestination(Image source, Rectangle sourceRectangle) + { + if (this.targetDimensions == Size.Empty) + { + // TODO: CreateDestination() should not modify the processors state! (kinda CQRS) + this.targetDimensions = this.GetTransformedDimensions(sourceRectangle.Size, this.TransformMatrix); + } + + // We will always be creating the clone even for mutate because we may need to resize the canvas + IEnumerable> frames = + source.Frames.Select(x => new ImageFrame(this.targetDimensions, x.MetaData.Clone())); + + // Use the overload to prevent an extra frame being added + return new Image(source.GetConfiguration(), source.MetaData.Clone(), frames); + } + + /// + protected override void OnApply( + ImageFrame source, + ImageFrame destination, + Rectangle sourceRectangle, + Configuration configuration) + { + int height = this.targetDimensions.Height; + int width = this.targetDimensions.Width; + + Rectangle sourceBounds = source.Bounds(); + var targetBounds = new Rectangle(0, 0, width, height); + + // Since could potentially be resizing the canvas we might need to re-calculate the matrix + Matrix3x2 matrix = this.GetProcessingMatrix(sourceBounds, targetBounds); + + if (this.Sampler is NearestNeighborResampler) + { + Parallel.For( + 0, + height, + configuration.ParallelOptions, + y => + { + Span destRow = destination.GetPixelRowSpan(y); + + for (int x = 0; x < width; x++) + { + var point = Point.Transform(new Point(x, y), matrix); + if (sourceBounds.Contains(point.X, point.Y)) + { + destRow[x] = source[point.X, point.Y]; + } + } + }); + + return; + } + + int maxSourceX = source.Width - 1; + int maxSourceY = source.Height - 1; + (float radius, float scale, float ratio) xRadiusScale = this.GetSamplingRadius(source.Width, destination.Width); + (float radius, float scale, float ratio) yRadiusScale = this.GetSamplingRadius(source.Height, destination.Height); + float xScale = xRadiusScale.scale; + float yScale = yRadiusScale.scale; + var radius = new Vector2(xRadiusScale.radius, yRadiusScale.radius); + IResampler sampler = this.Sampler; + var maxSource = new Vector4(maxSourceX, maxSourceY, maxSourceX, maxSourceY); + int xLength = (int)MathF.Ceiling((radius.X * 2) + 2); + int yLength = (int)MathF.Ceiling((radius.Y * 2) + 2); + + using (var yBuffer = new Buffer2D(yLength, height)) + using (var xBuffer = new Buffer2D(xLength, height)) + { + Parallel.For( + 0, + height, + configuration.ParallelOptions, + y => + { + Span destRow = destination.GetPixelRowSpan(y); + Span ySpan = yBuffer.GetRowSpan(y); + Span xSpan = xBuffer.GetRowSpan(y); + + for (int x = 0; x < width; x++) + { + // Use the single precision position to calculate correct bounding pixels + // otherwise we get rogue pixels outside of the bounds. + var point = Vector2.Transform(new Vector2(x, y), matrix); + + // Clamp sampling pixel radial extents to the source image edges + Vector2 maxXY = point + radius; + Vector2 minXY = point - radius; + + // max, maxY, minX, minY + var extents = new Vector4( + MathF.Floor(maxXY.X + .5F), + MathF.Floor(maxXY.Y + .5F), + MathF.Ceiling(minXY.X - .5F), + MathF.Ceiling(minXY.Y - .5F)); + + int right = (int)extents.X; + int bottom = (int)extents.Y; + int left = (int)extents.Z; + int top = (int)extents.W; + + extents = Vector4.Clamp(extents, Vector4.Zero, maxSource); + + int maxX = (int)extents.X; + int maxY = (int)extents.Y; + int minX = (int)extents.Z; + int minY = (int)extents.W; + + if (minX == maxX || minY == maxY) + { + continue; + } + + // It appears these have to be calculated on-the-fly. + // Precalulating transformed weights would require prior knowledge of every transformed pixel location + // since they can be at sub-pixel positions on both axis. + // I've optimized where I can but am always open to suggestions. + if (yScale > 1 && xScale > 1) + { + CalculateWeightsDown(top, bottom, minY, maxY, point.Y, sampler, yScale, ySpan); + CalculateWeightsDown(left, right, minX, maxX, point.X, sampler, xScale, xSpan); + } + else + { + CalculateWeightsScaleUp(minY, maxY, point.Y, sampler, ySpan); + CalculateWeightsScaleUp(minX, maxX, point.X, sampler, xSpan); + } + + // Now multiply the results against the offsets + Vector4 sum = Vector4.Zero; + for (int yy = 0, j = minY; j <= maxY; j++, yy++) + { + float yWeight = ySpan[yy]; + + for (int xx = 0, i = minX; i <= maxX; i++, xx++) + { + float xWeight = xSpan[xx]; + var vector = source[i, j].ToVector4(); + + // Values are first premultiplied to prevent darkening of edge pixels + var mupltiplied = new Vector4(new Vector3(vector.X, vector.Y, vector.Z) * vector.W, vector.W); + sum += mupltiplied * xWeight * yWeight; + } + } + + ref TPixel dest = ref destRow[x]; + + // Reverse the premultiplication + dest.PackFromVector4(new Vector4(new Vector3(sum.X, sum.Y, sum.Z) / sum.W, sum.W)); + } + }); + } + } + + /// + /// Gets a transform matrix adjusted for final processing based upon the target image bounds. + /// + /// The source image bounds. + /// The destination image bounds. + /// + /// The . + /// + protected virtual Matrix3x2 GetProcessingMatrix(Rectangle sourceRectangle, Rectangle destinationRectangle) + { + return this.TransformMatrix; + } + + /// + /// Gets the bounding relative to the source for the given transformation matrix. + /// + /// The source rectangle. + /// The transformation matrix. + /// The + protected virtual Size GetTransformedDimensions(Size sourceDimensions, Matrix3x2 matrix) + { + return sourceDimensions; + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/AutoOrientProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AutoOrientProcessor.cs index ab93e0e384..c39311bc33 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AutoOrientProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AutoOrientProcessor.cs @@ -15,13 +15,7 @@ namespace SixLabors.ImageSharp.Processing.Processors internal class AutoOrientProcessor : ImageProcessor where TPixel : struct, IPixel { - /// - /// Initializes a new instance of the class. - /// - public AutoOrientProcessor() - { - } - + /// protected override void BeforeImageApply(Image source, Rectangle sourceRectangle) { Orientation orientation = GetExifOrientation(source); @@ -33,7 +27,7 @@ namespace SixLabors.ImageSharp.Processing.Processors break; case Orientation.BottomRight: - new RotateProcessor() { Angle = (int)RotateType.Rotate180, Expand = false }.Apply(source, sourceRectangle); + new RotateProcessor((int)RotateType.Rotate180).Apply(source, sourceRectangle); break; case Orientation.BottomLeft: @@ -41,21 +35,21 @@ namespace SixLabors.ImageSharp.Processing.Processors break; case Orientation.LeftTop: - new RotateProcessor() { Angle = (int)RotateType.Rotate90, Expand = false }.Apply(source, sourceRectangle); + new RotateProcessor((int)RotateType.Rotate90).Apply(source, sourceRectangle); new FlipProcessor(FlipType.Horizontal).Apply(source, sourceRectangle); break; case Orientation.RightTop: - new RotateProcessor() { Angle = (int)RotateType.Rotate90, Expand = false }.Apply(source, sourceRectangle); + new RotateProcessor((int)RotateType.Rotate90).Apply(source, sourceRectangle); break; case Orientation.RightBottom: new FlipProcessor(FlipType.Vertical).Apply(source, sourceRectangle); - new RotateProcessor() { Angle = (int)RotateType.Rotate270, Expand = false }.Apply(source, sourceRectangle); + new RotateProcessor((int)RotateType.Rotate270).Apply(source, sourceRectangle); break; case Orientation.LeftBottom: - new RotateProcessor() { Angle = (int)RotateType.Rotate270, Expand = false }.Apply(source, sourceRectangle); + new RotateProcessor((int)RotateType.Rotate270).Apply(source, sourceRectangle); break; case Orientation.Unknown: diff --git a/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineTransformProcessor.cs new file mode 100644 index 0000000000..34a0866615 --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineTransformProcessor.cs @@ -0,0 +1,49 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Numerics; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.Primitives; + +namespace SixLabors.ImageSharp.Processing.Processors +{ + /// + /// A base class that provides methods to allow the automatic centering of affine transforms + /// + /// The pixel format. + internal abstract class CenteredAffineTransformProcessor : AffineTransformProcessor + where TPixel : struct, IPixel + { + /// + /// Initializes a new instance of the class. + /// + /// The transform matrix + /// The sampler to perform the transform operation. + protected CenteredAffineTransformProcessor(Matrix3x2 matrix, IResampler sampler) + : base(matrix, sampler) + { + } + + /// + protected override Matrix3x2 GetProcessingMatrix(Rectangle sourceRectangle, Rectangle destinationRectangle) + { + var translationToTargetCenter = Matrix3x2.CreateTranslation(-destinationRectangle.Width * .5F, -destinationRectangle.Height * .5F); + var translateToSourceCenter = Matrix3x2.CreateTranslation(sourceRectangle.Width * .5F, sourceRectangle.Height * .5F); + return translationToTargetCenter * this.TransformMatrix * translateToSourceCenter; + } + + /// + protected override Size GetTransformedDimensions(Size sourceDimensions, Matrix3x2 matrix) + { + var sourceRectangle = new Rectangle(0, 0, sourceDimensions.Width, sourceDimensions.Height); + + if (!Matrix3x2.Invert(this.TransformMatrix, out Matrix3x2 sizeMatrix)) + { + // TODO: Shouldn't we throw an exception instead? + return sourceDimensions; + } + + return TransformHelpers.GetTransformedBoundingRectangle(sourceRectangle, sizeMatrix).Size; + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/CenteredProjectiveTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/CenteredProjectiveTransformProcessor.cs new file mode 100644 index 0000000000..dc2dd28ab1 --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Transforms/CenteredProjectiveTransformProcessor.cs @@ -0,0 +1,43 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Numerics; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.Primitives; + +namespace SixLabors.ImageSharp.Processing.Processors +{ + /// + /// A base class that provides methods to allow the automatic centering of non-affine transforms + /// + /// The pixel format. + internal abstract class CenteredProjectiveTransformProcessor : ProjectiveTransformProcessor + where TPixel : struct, IPixel + { + /// + /// Initializes a new instance of the class. + /// + /// The transform matrix + /// The sampler to perform the transform operation. + protected CenteredProjectiveTransformProcessor(Matrix4x4 matrix, IResampler sampler) + : base(matrix, sampler) + { + } + + /// + protected override Matrix4x4 GetProcessingMatrix(Rectangle sourceRectangle, Rectangle destinationRectangle) + { + var translationToTargetCenter = Matrix4x4.CreateTranslation(-destinationRectangle.Width * .5F, -destinationRectangle.Height * .5F, 0); + var translateToSourceCenter = Matrix4x4.CreateTranslation(sourceRectangle.Width * .5F, sourceRectangle.Height * .5F, 0); + return translationToTargetCenter * this.TransformMatrix * translateToSourceCenter; + } + + /// + protected override Rectangle GetTransformedBoundingRectangle(Rectangle sourceRectangle, Matrix4x4 matrix) + { + return Matrix4x4.Invert(this.TransformMatrix, out Matrix4x4 sizeMatrix) + ? TransformHelpers.GetTransformedBoundingRectangle(sourceRectangle, sizeMatrix) + : sourceRectangle; + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/InterpolatedTransformProcessorBase.cs b/src/ImageSharp/Processing/Processors/Transforms/InterpolatedTransformProcessorBase.cs new file mode 100644 index 0000000000..5c32f044a2 --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Transforms/InterpolatedTransformProcessorBase.cs @@ -0,0 +1,139 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Runtime.CompilerServices; +using SixLabors.ImageSharp.MetaData.Profiles.Exif; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.Primitives; + +namespace SixLabors.ImageSharp.Processing.Processors +{ + /// + /// The base class for performing interpolated affine and non-affine transforms. + /// + /// The pixel format. + internal abstract class InterpolatedTransformProcessorBase : CloningImageProcessor + where TPixel : struct, IPixel + { + /// + /// Initializes a new instance of the class. + /// + /// The sampler to perform the transform operation. + protected InterpolatedTransformProcessorBase(IResampler sampler) + { + this.Sampler = sampler; + } + + /// + /// Gets the sampler to perform interpolation of the transform operation. + /// + public IResampler Sampler { get; } + + /// + /// Calculated the weights for the given point. + /// This method uses more samples than the upscaled version to ensure edge pixels are correctly rendered. + /// Additionally the weights are nomalized. + /// + /// The minimum sampling offset + /// The maximum sampling offset + /// The minimum source bounds + /// The maximum source bounds + /// The transformed point dimension + /// The sampler + /// The transformed image scale relative to the source + /// The collection of weights + [MethodImpl(MethodImplOptions.AggressiveInlining)] + protected static void CalculateWeightsDown(int min, int max, int sourceMin, int sourceMax, float point, IResampler sampler, float scale, Span weights) + { + float sum = 0; + ref float weightsBaseRef = ref weights[0]; + + // Downsampling weights requires more edge sampling plus normalization of the weights + for (int x = 0, i = min; i <= max; i++, x++) + { + int index = i; + if (index < sourceMin) + { + index = sourceMin; + } + + if (index > sourceMax) + { + index = sourceMax; + } + + float weight = sampler.GetValue((index - point) / scale); + sum += weight; + Unsafe.Add(ref weightsBaseRef, x) = weight; + } + + if (sum > 0) + { + for (int i = 0; i < weights.Length; i++) + { + ref float wRef = ref Unsafe.Add(ref weightsBaseRef, i); + wRef = wRef / sum; + } + } + } + + /// + /// Calculated the weights for the given point. + /// + /// The minimum source bounds + /// The maximum source bounds + /// The transformed point dimension + /// The sampler + /// The collection of weights + [MethodImpl(MethodImplOptions.AggressiveInlining)] + protected static void CalculateWeightsScaleUp(int sourceMin, int sourceMax, float point, IResampler sampler, Span weights) + { + ref float weightsBaseRef = ref weights[0]; + for (int x = 0, i = sourceMin; i <= sourceMax; i++, x++) + { + float weight = sampler.GetValue(i - point); + Unsafe.Add(ref weightsBaseRef, x) = weight; + } + } + + /// + /// Calculates the sampling radius for the current sampler + /// + /// The source dimension size + /// The destination dimension size + /// The radius, and scaling factor + protected (float radius, float scale, float ratio) GetSamplingRadius(int sourceSize, int destinationSize) + { + float ratio = (float)sourceSize / destinationSize; + float scale = ratio; + + if (scale < 1F) + { + scale = 1F; + } + + return (MathF.Ceiling(scale * this.Sampler.Radius), scale, ratio); + } + + /// + protected override void AfterImageApply(Image source, Image destination, Rectangle sourceRectangle) + { + ExifProfile profile = destination.MetaData.ExifProfile; + if (profile == null) + { + return; + } + + if (profile.GetValue(ExifTag.PixelXDimension) != null) + { + profile.SetValue(ExifTag.PixelXDimension, destination.Width); + } + + if (profile.GetValue(ExifTag.PixelYDimension) != null) + { + profile.SetValue(ExifTag.PixelYDimension, destination.Height); + } + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/Matrix3x2Processor.cs b/src/ImageSharp/Processing/Processors/Transforms/Matrix3x2Processor.cs deleted file mode 100644 index 4a15254ab2..0000000000 --- a/src/ImageSharp/Processing/Processors/Transforms/Matrix3x2Processor.cs +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System.Numerics; -using SixLabors.ImageSharp.PixelFormats; -using SixLabors.Primitives; - -namespace SixLabors.ImageSharp.Processing.Processors -{ - /// - /// Provides methods to transform an image using a . - /// - /// The pixel format. - internal abstract class Matrix3x2Processor : ImageProcessor - where TPixel : struct, IPixel - { - /// - /// Gets the rectangle designating the target canvas. - /// - protected Rectangle CanvasRectangle { get; private set; } - - /// - /// Creates a new target canvas to contain the results of the matrix transform. - /// - /// The source rectangle. - /// The processing matrix. - protected void CreateNewCanvas(Rectangle sourceRectangle, Matrix3x2 processMatrix) - { - Matrix3x2 sizeMatrix; - this.CanvasRectangle = Matrix3x2.Invert(processMatrix, out sizeMatrix) - ? ImageMaths.GetBoundingRectangle(sourceRectangle, sizeMatrix) - : sourceRectangle; - } - - /// - /// Gets a transform matrix adjusted to center upon the target image bounds. - /// - /// The source image. - /// The transform matrix. - /// - /// The . - /// - protected Matrix3x2 GetCenteredMatrix(ImageFrame source, Matrix3x2 matrix) - { - var translationToTargetCenter = Matrix3x2.CreateTranslation(-this.CanvasRectangle.Width * .5F, -this.CanvasRectangle.Height * .5F); - var translateToSourceCenter = Matrix3x2.CreateTranslation(source.Width * .5F, source.Height * .5F); - return (translationToTargetCenter * matrix) * translateToSourceCenter; - } - } -} diff --git a/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs new file mode 100644 index 0000000000..f53585093b --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs @@ -0,0 +1,240 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Threading.Tasks; +using SixLabors.ImageSharp.Advanced; +using SixLabors.ImageSharp.Helpers; +using SixLabors.ImageSharp.Memory; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.Primitives; + +namespace SixLabors.ImageSharp.Processing.Processors +{ + /// + /// Provides the base methods to perform non-affine transforms on an image. + /// TODO: Doesn't work yet! Implement tests + Finish implementation + Document Matrix4x4 behavior + /// + /// The pixel format. + internal class ProjectiveTransformProcessor : InterpolatedTransformProcessorBase + where TPixel : struct, IPixel + { + // TODO: We should use a Size instead! (See AffineTransformProcessor) + private Rectangle targetRectangle; + + /// + /// Initializes a new instance of the class. + /// + /// The transform matrix + public ProjectiveTransformProcessor(Matrix4x4 matrix) + : this(matrix, KnownResamplers.Bicubic) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The transform matrix + /// The sampler to perform the transform operation. + public ProjectiveTransformProcessor(Matrix4x4 matrix, IResampler sampler) + : this(matrix, sampler, Rectangle.Empty) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The transform matrix + /// The sampler to perform the transform operation. + /// The rectangle to constrain the transformed image to. + public ProjectiveTransformProcessor(Matrix4x4 matrix, IResampler sampler, Rectangle rectangle) + : base(sampler) + { + // Tansforms are inverted else the output is the opposite of the expected. + Matrix4x4.Invert(matrix, out matrix); + this.TransformMatrix = matrix; + this.targetRectangle = rectangle; + } + + /// + /// Gets the matrix used to supply the non-affine transform + /// + public Matrix4x4 TransformMatrix { get; } + + /// + protected override Image CreateDestination(Image source, Rectangle sourceRectangle) + { + if (this.targetRectangle == Rectangle.Empty) + { + this.targetRectangle = this.GetTransformedBoundingRectangle(sourceRectangle, this.TransformMatrix); + } + + // We will always be creating the clone even for mutate because we may need to resize the canvas + IEnumerable> frames = + source.Frames.Select(x => new ImageFrame(this.targetRectangle.Width, this.targetRectangle.Height, x.MetaData.Clone())); + + // Use the overload to prevent an extra frame being added + return new Image(source.GetConfiguration(), source.MetaData.Clone(), frames); + } + + /// + protected override void OnApply(ImageFrame source, ImageFrame destination, Rectangle sourceRectangle, Configuration configuration) + { + int height = this.targetRectangle.Height; + int width = this.targetRectangle.Width; + Rectangle sourceBounds = source.Bounds(); + + // Since could potentially be resizing the canvas we might need to re-calculate the matrix + Matrix4x4 matrix = this.GetProcessingMatrix(sourceBounds, this.targetRectangle); + + if (this.Sampler is NearestNeighborResampler) + { + Parallel.For( + 0, + height, + configuration.ParallelOptions, + y => + { + Span destRow = destination.GetPixelRowSpan(y); + + for (int x = 0; x < width; x++) + { + var point = Point.Round(Vector2.Transform(new Vector2(x, y), matrix)); + if (sourceBounds.Contains(point.X, point.Y)) + { + destRow[x] = source[point.X, point.Y]; + } + } + }); + + return; + } + + int maxSourceX = source.Width - 1; + int maxSourceY = source.Height - 1; + (float radius, float scale, float ratio) xRadiusScale = this.GetSamplingRadius(source.Width, destination.Width); + (float radius, float scale, float ratio) yRadiusScale = this.GetSamplingRadius(source.Height, destination.Height); + float xScale = xRadiusScale.scale; + float yScale = yRadiusScale.scale; + var radius = new Vector2(xRadiusScale.radius, yRadiusScale.radius); + IResampler sampler = this.Sampler; + var maxSource = new Vector4(maxSourceX, maxSourceY, maxSourceX, maxSourceY); + int xLength = (int)MathF.Ceiling((radius.X * 2) + 2); + int yLength = (int)MathF.Ceiling((radius.Y * 2) + 2); + + using (var yBuffer = new Buffer2D(yLength, height)) + using (var xBuffer = new Buffer2D(xLength, height)) + { + Parallel.For( + 0, + height, + configuration.ParallelOptions, + y => + { + Span destRow = destination.GetPixelRowSpan(y); + Span ySpan = yBuffer.GetRowSpan(y); + Span xSpan = xBuffer.GetRowSpan(y); + + for (int x = 0; x < width; x++) + { + // Use the single precision position to calculate correct bounding pixels + // otherwise we get rogue pixels outside of the bounds. + var point = Vector2.Transform(new Vector2(x, y), matrix); + + // Clamp sampling pixel radial extents to the source image edges + Vector2 maxXY = point + radius; + Vector2 minXY = point - radius; + + // max, maxY, minX, minY + var extents = new Vector4( + MathF.Floor(maxXY.X + .5F), + MathF.Floor(maxXY.Y + .5F), + MathF.Ceiling(minXY.X - .5F), + MathF.Ceiling(minXY.Y - .5F)); + + int right = (int)extents.X; + int bottom = (int)extents.Y; + int left = (int)extents.Z; + int top = (int)extents.W; + + extents = Vector4.Clamp(extents, Vector4.Zero, maxSource); + + int maxX = (int)extents.X; + int maxY = (int)extents.Y; + int minX = (int)extents.Z; + int minY = (int)extents.W; + + if (minX == maxX || minY == maxY) + { + continue; + } + + // It appears these have to be calculated on-the-fly. + // Precalulating transformed weights would require prior knowledge of every transformed pixel location + // since they can be at sub-pixel positions on both axis. + // I've optimized where I can but am always open to suggestions. + if (yScale > 1 && xScale > 1) + { + CalculateWeightsDown(top, bottom, minY, maxY, point.Y, sampler, yScale, ySpan); + CalculateWeightsDown(left, right, minX, maxX, point.X, sampler, xScale, xSpan); + } + else + { + CalculateWeightsScaleUp(minY, maxY, point.Y, sampler, ySpan); + CalculateWeightsScaleUp(minX, maxX, point.X, sampler, xSpan); + } + + // Now multiply the results against the offsets + Vector4 sum = Vector4.Zero; + for (int yy = 0, j = minY; j <= maxY; j++, yy++) + { + float yWeight = ySpan[yy]; + + for (int xx = 0, i = minX; i <= maxX; i++, xx++) + { + float xWeight = xSpan[xx]; + var vector = source[i, j].ToVector4(); + + // Values are first premultiplied to prevent darkening of edge pixels + var mupltiplied = new Vector4(new Vector3(vector.X, vector.Y, vector.Z) * vector.W, vector.W); + sum += mupltiplied * xWeight * yWeight; + } + } + + ref TPixel dest = ref destRow[x]; + + // Reverse the premultiplication + dest.PackFromVector4(new Vector4(new Vector3(sum.X, sum.Y, sum.Z) / sum.W, sum.W)); + } + }); + } + } + + /// + /// Gets a transform matrix adjusted for final processing based upon the target image bounds. + /// + /// The source image bounds. + /// The destination image bounds. + /// + /// The . + /// + protected virtual Matrix4x4 GetProcessingMatrix(Rectangle sourceRectangle, Rectangle destinationRectangle) + { + return this.TransformMatrix; + } + + /// + /// Gets the bounding relative to the source for the given transformation matrix. + /// + /// The source rectangle. + /// The transformation matrix. + /// The + protected virtual Rectangle GetTransformedBoundingRectangle(Rectangle sourceRectangle, Matrix4x4 matrix) + { + return sourceRectangle; + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs deleted file mode 100644 index 22a7c90b75..0000000000 --- a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs +++ /dev/null @@ -1,199 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System; -using System.Numerics; -using System.Runtime.CompilerServices; -using SixLabors.ImageSharp.Memory; - -namespace SixLabors.ImageSharp.Processing.Processors -{ - /// - /// Conains the definition of and . - /// - internal abstract partial class ResamplingWeightedProcessor - { - /// - /// Points to a collection of weights allocated in . - /// - internal struct WeightsWindow - { - /// - /// The local left index position - /// - public int Left; - - /// - /// The length of the weights window - /// - public int Length; - - /// - /// The index in the destination buffer - /// - private readonly int flatStartIndex; - - /// - /// The buffer containing the weights values. - /// - private readonly Buffer buffer; - - /// - /// Initializes a new instance of the struct. - /// - /// The destination index in the buffer - /// The local left index - /// The span - /// The length of the window - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal WeightsWindow(int index, int left, Buffer2D buffer, int length) - { - this.flatStartIndex = (index * buffer.Width) + left; - this.Left = left; - this.buffer = buffer; - this.Length = length; - } - - /// - /// Gets a reference to the first item of the window. - /// - /// The reference to the first item of the window - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ref float GetStartReference() - { - return ref this.buffer[this.flatStartIndex]; - } - - /// - /// Gets the span representing the portion of the that this window covers - /// - /// The - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Span GetWindowSpan() => this.buffer.Slice(this.flatStartIndex, this.Length); - - /// - /// Computes the sum of vectors in 'rowSpan' weighted by weight values, pointed by this instance. - /// - /// The input span of vectors - /// The source row position. - /// The weighted sum - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Vector4 ComputeWeightedRowSum(Span rowSpan, int sourceX) - { - ref float horizontalValues = ref this.GetStartReference(); - int left = this.Left; - ref Vector4 vecPtr = ref Unsafe.Add(ref rowSpan.DangerousGetPinnableReference(), left + sourceX); - - // Destination color components - Vector4 result = Vector4.Zero; - - for (int i = 0; i < this.Length; i++) - { - float weight = Unsafe.Add(ref horizontalValues, i); - Vector4 v = Unsafe.Add(ref vecPtr, i); - result += v * weight; - } - - return result; - } - - /// - /// Computes the sum of vectors in 'rowSpan' weighted by weight values, pointed by this instance. - /// Applies to all input vectors. - /// - /// The input span of vectors - /// The source row position. - /// The weighted sum - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Vector4 ComputeExpandedWeightedRowSum(Span rowSpan, int sourceX) - { - ref float horizontalValues = ref this.GetStartReference(); - int left = this.Left; - ref Vector4 vecPtr = ref Unsafe.Add(ref rowSpan.DangerousGetPinnableReference(), left + sourceX); - - // Destination color components - Vector4 result = Vector4.Zero; - - for (int i = 0; i < this.Length; i++) - { - float weight = Unsafe.Add(ref horizontalValues, i); - Vector4 v = Unsafe.Add(ref vecPtr, i); - result += v.Expand() * weight; - } - - return result; - } - - /// - /// Computes the sum of vectors in 'firstPassPixels' at a row pointed by 'x', - /// weighted by weight values, pointed by this instance. - /// - /// The buffer of input vectors in row first order - /// The row position - /// The source column position. - /// The weighted sum - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Vector4 ComputeWeightedColumnSum(Buffer2D firstPassPixels, int x, int sourceY) - { - ref float verticalValues = ref this.GetStartReference(); - int left = this.Left; - - // Destination color components - Vector4 result = Vector4.Zero; - - for (int i = 0; i < this.Length; i++) - { - float yw = Unsafe.Add(ref verticalValues, i); - int index = left + i + sourceY; - result += firstPassPixels[x, index] * yw; - } - - return result; - } - } - - /// - /// Holds the values in an optimized contiguous memory region. - /// - internal class WeightsBuffer : IDisposable - { - private readonly Buffer2D dataBuffer; - - /// - /// Initializes a new instance of the class. - /// - /// The size of the source window - /// The size of the destination window - public WeightsBuffer(int sourceSize, int destinationSize) - { - this.dataBuffer = Buffer2D.CreateClean(sourceSize, destinationSize); - this.Weights = new WeightsWindow[destinationSize]; - } - - /// - /// Gets the calculated values. - /// - public WeightsWindow[] Weights { get; } - - /// - /// Disposes instance releasing it's backing buffer. - /// - public void Dispose() - { - this.dataBuffer.Dispose(); - } - - /// - /// Slices a weights value at the given positions. - /// - /// The index in destination buffer - /// The local left index value - /// The local right index value - /// The weights - public WeightsWindow GetWeightsWindow(int destIdx, int leftIdx, int rightIdx) - { - return new WeightsWindow(destIdx, leftIdx, this.dataBuffer, rightIdx - leftIdx + 1); - } - } - } -} diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.cs index 781f3a01c7..cb65559daa 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.cs @@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// Adapted from /// /// The pixel format. - internal abstract partial class ResamplingWeightedProcessor : CloningImageProcessor + internal abstract class ResamplingWeightedProcessor : CloningImageProcessor where TPixel : struct, IPixel { /// diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs index 17b42c5040..0d8d0d9117 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs @@ -8,6 +8,7 @@ using System.Numerics; using System.Threading.Tasks; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Memory; +using SixLabors.ImageSharp.MetaData.Profiles.Exif; using SixLabors.ImageSharp.PixelFormats; using SixLabors.Primitives; @@ -53,20 +54,16 @@ namespace SixLabors.ImageSharp.Processing.Processors /// protected override Image CreateDestination(Image source, Rectangle sourceRectangle) { - Configuration config = source.GetConfiguration(); + // We will always be creating the clone even for mutate because we may need to resize the canvas + IEnumerable> frames = + source.Frames.Select(x => new ImageFrame(this.Width, this.Height, x.MetaData.Clone())); - // We will always be creating the clone even for mutate because thats the way this base processor works - // ------------ - // For resize we know we are going to populate every pixel with fresh data and we want a different target size so - // let's manually clone an empty set of images at the correct target and then have the base class process them in turn. - IEnumerable> frames = source.Frames.Select(x => new ImageFrame(this.Width, this.Height, x.MetaData.Clone())); // this will create places holders - var image = new Image(config, source.MetaData.Clone(), frames); // base the place holder images in to prevent a extra frame being added - - return image; + // Use the overload to prevent an extra frame being added + return new Image(source.GetConfiguration(), source.MetaData.Clone(), frames); } /// - protected override unsafe void OnApply(ImageFrame source, ImageFrame cloned, Rectangle sourceRectangle, Configuration configuration) + protected override void OnApply(ImageFrame source, ImageFrame cloned, Rectangle sourceRectangle, Configuration configuration) { // Jump out, we'll deal with that later. if (source.Width == cloned.Width && source.Height == cloned.Height && sourceRectangle == this.ResizeRectangle) @@ -194,5 +191,25 @@ namespace SixLabors.ImageSharp.Processing.Processors }); } } + + /// + protected override void AfterImageApply(Image source, Image destination, Rectangle sourceRectangle) + { + ExifProfile profile = destination.MetaData.ExifProfile; + if (profile == null) + { + return; + } + + if (profile.GetValue(ExifTag.PixelXDimension) != null) + { + profile.SetValue(ExifTag.PixelXDimension, destination.Width); + } + + if (profile.GetValue(ExifTag.PixelYDimension) != null) + { + profile.SetValue(ExifTag.PixelYDimension, destination.Height); + } + } } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs index 86a0c73603..b93c869152 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs @@ -2,11 +2,9 @@ // Licensed under the Apache License, Version 2.0. using System; -using System.Numerics; using System.Threading.Tasks; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Helpers; -using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.MetaData.Profiles.Exif; using SixLabors.ImageSharp.PixelFormats; using SixLabors.Primitives; @@ -17,87 +15,55 @@ namespace SixLabors.ImageSharp.Processing.Processors /// Provides methods that allow the rotating of images. /// /// The pixel format. - internal class RotateProcessor : Matrix3x2Processor + internal class RotateProcessor : CenteredAffineTransformProcessor where TPixel : struct, IPixel { /// - /// The transform matrix to apply. + /// Initializes a new instance of the class. /// - private Matrix3x2 processMatrix; + /// The angle of rotation in degrees. + public RotateProcessor(float degrees) + : this(degrees, KnownResamplers.Bicubic) + { + } /// - /// Gets or sets the angle of processMatrix in degrees. + /// Initializes a new instance of the class. /// - public float Angle { get; set; } + /// The angle of rotation in degrees. + /// The sampler to perform the rotating operation. + public RotateProcessor(float degrees, IResampler sampler) + : base(Matrix3x2Extensions.CreateRotationDegrees(degrees, PointF.Empty), sampler) + { + this.Degrees = degrees; + } /// - /// Gets or sets a value indicating whether to expand the canvas to fit the rotated image. + /// Gets the angle of rotation in degrees. /// - public bool Expand { get; set; } = true; - - /// - protected override void OnApply(ImageFrame source, Rectangle sourceRectangle, Configuration configuration) - { - if (this.OptimizedApply(source, configuration)) - { - return; - } - - int height = this.CanvasRectangle.Height; - int width = this.CanvasRectangle.Width; - Matrix3x2 matrix = this.GetCenteredMatrix(source, this.processMatrix); - Rectangle sourceBounds = source.Bounds(); - - using (var targetPixels = new PixelAccessor(width, height)) - { - Parallel.For( - 0, - height, - configuration.ParallelOptions, - y => - { - Span targetRow = targetPixels.GetRowSpan(y); - - for (int x = 0; x < width; x++) - { - var transformedPoint = Point.Rotate(new Point(x, y), matrix); - - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) - { - targetRow[x] = source[transformedPoint.X, transformedPoint.Y]; - } - } - }); - - source.SwapPixelsBuffers(targetPixels); - } - } + public float Degrees { get; } /// - protected override void BeforeApply(ImageFrame source, Rectangle sourceRectangle, Configuration configuration) + protected override void OnApply(ImageFrame source, ImageFrame destination, Rectangle sourceRectangle, Configuration configuration) { - if (MathF.Abs(this.Angle) < Constants.Epsilon || MathF.Abs(this.Angle - 90) < Constants.Epsilon || MathF.Abs(this.Angle - 180) < Constants.Epsilon || MathF.Abs(this.Angle - 270) < Constants.Epsilon) + if (this.OptimizedApply(source, destination, configuration)) { return; } - this.processMatrix = Matrix3x2Extensions.CreateRotationDegrees(-this.Angle, new Point(0, 0)); - if (this.Expand) - { - this.CreateNewCanvas(sourceRectangle, this.processMatrix); - } + base.OnApply(source, destination, sourceRectangle, configuration); } /// - protected override void AfterImageApply(Image source, Rectangle sourceRectangle) + protected override void AfterImageApply(Image source, Image destination, Rectangle sourceRectangle) { - ExifProfile profile = source.MetaData.ExifProfile; + ExifProfile profile = destination.MetaData.ExifProfile; if (profile == null) { return; } - if (MathF.Abs(this.Angle) < Constants.Epsilon) + if (MathF.Abs(WrapDegrees(this.Degrees)) < Constants.Epsilon) { // No need to do anything so return. return; @@ -105,44 +71,62 @@ namespace SixLabors.ImageSharp.Processing.Processors profile.RemoveValue(ExifTag.Orientation); - if (this.Expand && profile.GetValue(ExifTag.PixelXDimension) != null) + base.AfterImageApply(source, destination, sourceRectangle); + } + + /// + /// Wraps a given angle in degrees so that it falls withing the 0-360 degree range + /// + /// The angle of rotation in degrees. + /// The + private static float WrapDegrees(float degrees) + { + degrees = degrees % 360; + + while (degrees < 0) { - profile.SetValue(ExifTag.PixelXDimension, source.Width); - profile.SetValue(ExifTag.PixelYDimension, source.Height); + degrees += 360; } + + return degrees; } /// /// Rotates the images with an optimized method when the angle is 90, 180 or 270 degrees. /// /// The source image. + /// The destination image. /// The configuration. /// /// The /// - private bool OptimizedApply(ImageFrame source, Configuration configuration) + private bool OptimizedApply(ImageFrame source, ImageFrame destination, Configuration configuration) { - if (MathF.Abs(this.Angle) < Constants.Epsilon) + // Wrap the degrees to keep within 0-360 so we can apply optimizations when possible. + float degrees = WrapDegrees(this.Degrees); + + if (MathF.Abs(degrees) < Constants.Epsilon) { - // No need to do anything so return. + // The destination will be blank here so copy all the pixel data over + source.GetPixelSpan().CopyTo(destination.GetPixelSpan()); return true; } - if (MathF.Abs(this.Angle - 90) < Constants.Epsilon) + if (MathF.Abs(degrees - 90) < Constants.Epsilon) { - this.Rotate90(source, configuration); + this.Rotate90(source, destination, configuration); return true; } - if (MathF.Abs(this.Angle - 180) < Constants.Epsilon) + if (MathF.Abs(degrees - 180) < Constants.Epsilon) { - this.Rotate180(source, configuration); + this.Rotate180(source, destination, configuration); return true; } - if (MathF.Abs(this.Angle - 270) < Constants.Epsilon) + if (MathF.Abs(degrees - 270) < Constants.Epsilon) { - this.Rotate270(source, configuration); + this.Rotate270(source, destination, configuration); return true; } @@ -153,95 +137,90 @@ namespace SixLabors.ImageSharp.Processing.Processors /// Rotates the image 270 degrees clockwise at the centre point. /// /// The source image. + /// The destination image. /// The configuration. - private void Rotate270(ImageFrame source, Configuration configuration) + private void Rotate270(ImageFrame source, ImageFrame destination, Configuration configuration) { int width = source.Width; int height = source.Height; + Rectangle destinationBounds = destination.Bounds(); - using (var targetPixels = new PixelAccessor(height, width)) - { - using (PixelAccessor sourcePixels = source.Lock()) + Parallel.For( + 0, + height, + configuration.ParallelOptions, + y => { - Parallel.For( - 0, - height, - configuration.ParallelOptions, - y => + Span sourceRow = source.GetPixelRowSpan(y); + for (int x = 0; x < width; x++) + { + int newX = height - y - 1; + newX = height - newX - 1; + int newY = width - x - 1; + + if (destinationBounds.Contains(newX, newY)) { - for (int x = 0; x < width; x++) - { - int newX = height - y - 1; - newX = height - newX - 1; - int newY = width - x - 1; - targetPixels[newX, newY] = sourcePixels[x, y]; - } - }); - } - - source.SwapPixelsBuffers(targetPixels); - } + destination[newX, newY] = sourceRow[x]; + } + } + }); } /// /// Rotates the image 180 degrees clockwise at the centre point. /// /// The source image. + /// The destination image. /// The configuration. - private void Rotate180(ImageFrame source, Configuration configuration) + private void Rotate180(ImageFrame source, ImageFrame destination, Configuration configuration) { int width = source.Width; int height = source.Height; - using (var targetPixels = new PixelAccessor(width, height)) - { - Parallel.For( - 0, - height, - configuration.ParallelOptions, - y => - { - Span sourceRow = source.GetPixelRowSpan(y); - Span targetRow = targetPixels.GetRowSpan(height - y - 1); - - for (int x = 0; x < width; x++) - { - targetRow[width - x - 1] = sourceRow[x]; - } - }); + Parallel.For( + 0, + height, + configuration.ParallelOptions, + y => + { + Span sourceRow = source.GetPixelRowSpan(y); + Span targetRow = destination.GetPixelRowSpan(height - y - 1); - source.SwapPixelsBuffers(targetPixels); - } + for (int x = 0; x < width; x++) + { + targetRow[width - x - 1] = sourceRow[x]; + } + }); } /// /// Rotates the image 90 degrees clockwise at the centre point. /// /// The source image. + /// The destination image. /// The configuration. - private void Rotate90(ImageFrame source, Configuration configuration) + private void Rotate90(ImageFrame source, ImageFrame destination, Configuration configuration) { int width = source.Width; int height = source.Height; + Rectangle destinationBounds = destination.Bounds(); - using (var targetPixels = new PixelAccessor(height, width)) - { - Parallel.For( - 0, - height, - configuration.ParallelOptions, - y => + Parallel.For( + 0, + height, + configuration.ParallelOptions, + y => + { + Span sourceRow = source.GetPixelRowSpan(y); + int newX = height - y - 1; + for (int x = 0; x < width; x++) { - Span sourceRow = source.GetPixelRowSpan(y); - int newX = height - y - 1; - for (int x = 0; x < width; x++) + if (destinationBounds.Contains(newX, x)) { - targetPixels[newX, x] = sourceRow[x]; + destination[newX, x] = sourceRow[x]; } - }); - - source.SwapPixelsBuffers(targetPixels); - } + } + }); } } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs index 316e2a2af3..8c47b35274 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs @@ -1,12 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; -using System.Numerics; -using System.Threading.Tasks; -using SixLabors.ImageSharp.Advanced; -using SixLabors.ImageSharp.Helpers; -using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; using SixLabors.Primitives; @@ -16,70 +10,40 @@ namespace SixLabors.ImageSharp.Processing.Processors /// Provides methods that allow the skewing of images. /// /// The pixel format. - internal class SkewProcessor : Matrix3x2Processor + internal class SkewProcessor : CenteredAffineTransformProcessor where TPixel : struct, IPixel { /// - /// The transform matrix to apply. + /// Initializes a new instance of the class. /// - private Matrix3x2 processMatrix; + /// The angle in degrees to perform the skew along the x-axis. + /// The angle in degrees to perform the skew along the y-axis. + public SkewProcessor(float degreesX, float degreesY) + : this(degreesX, degreesY, KnownResamplers.Bicubic) + { + } /// - /// Gets or sets the angle of rotation along the x-axis in degrees. + /// Initializes a new instance of the class. /// - public float AngleX { get; set; } + /// The angle in degrees to perform the skew along the x-axis. + /// The angle in degrees to perform the skew along the y-axis. + /// The sampler to perform the skew operation. + public SkewProcessor(float degreesX, float degreesY, IResampler sampler) + : base(Matrix3x2Extensions.CreateSkewDegrees(degreesX, degreesY, PointF.Empty), sampler) + { + this.DegreesX = degreesX; + this.DegreesY = degreesY; + } /// - /// Gets or sets the angle of rotation along the y-axis in degrees. + /// Gets the angle of rotation along the x-axis in degrees. /// - public float AngleY { get; set; } + public float DegreesX { get; } /// - /// Gets or sets a value indicating whether to expand the canvas to fit the skewed image. + /// Gets the angle of rotation along the y-axis in degrees. /// - public bool Expand { get; set; } = true; - - /// - protected override void OnApply(ImageFrame source, Rectangle sourceRectangle, Configuration configuration) - { - int height = this.CanvasRectangle.Height; - int width = this.CanvasRectangle.Width; - Matrix3x2 matrix = this.GetCenteredMatrix(source, this.processMatrix); - Rectangle sourceBounds = source.Bounds(); - - using (var targetPixels = new PixelAccessor(width, height)) - { - Parallel.For( - 0, - height, - configuration.ParallelOptions, - y => - { - Span targetRow = targetPixels.GetRowSpan(y); - - for (int x = 0; x < width; x++) - { - var transformedPoint = Point.Skew(new Point(x, y), matrix); - - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) - { - targetRow[x] = source[transformedPoint.X, transformedPoint.Y]; - } - } - }); - - source.SwapPixelsBuffers(targetPixels); - } - } - - /// - protected override void BeforeApply(ImageFrame source, Rectangle sourceRectangle, Configuration configuration) - { - this.processMatrix = Matrix3x2Extensions.CreateSkewDegrees(-this.AngleX, -this.AngleY, new Point(0, 0)); - if (this.Expand) - { - this.CreateNewCanvas(sourceRectangle, this.processMatrix); - } - } + public float DegreesY { get; } } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/WeightsBuffer.cs b/src/ImageSharp/Processing/Processors/Transforms/WeightsBuffer.cs new file mode 100644 index 0000000000..0e91087f90 --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Transforms/WeightsBuffer.cs @@ -0,0 +1,53 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; + +using SixLabors.ImageSharp.Memory; + +namespace SixLabors.ImageSharp.Processing.Processors +{ + /// + /// Holds the values in an optimized contigous memory region. + /// + internal class WeightsBuffer : IDisposable + { + private readonly Buffer2D dataBuffer; + + /// + /// Initializes a new instance of the class. + /// + /// The size of the source window + /// The size of the destination window + public WeightsBuffer(int sourceSize, int destinationSize) + { + this.dataBuffer = Buffer2D.CreateClean(sourceSize, destinationSize); + this.Weights = new WeightsWindow[destinationSize]; + } + + /// + /// Gets the calculated values. + /// + public WeightsWindow[] Weights { get; } + + /// + /// Disposes instance releasing it's backing buffer. + /// + public void Dispose() + { + this.dataBuffer.Dispose(); + } + + /// + /// Slices a weights value at the given positions. + /// + /// The index in destination buffer + /// The local left index value + /// The local right index value + /// The weights + public WeightsWindow GetWeightsWindow(int destIdx, int leftIdx, int rightIdx) + { + return new WeightsWindow(destIdx, leftIdx, this.dataBuffer, rightIdx - leftIdx + 1); + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/WeightsWindow.cs b/src/ImageSharp/Processing/Processors/Transforms/WeightsWindow.cs new file mode 100644 index 0000000000..d23ee5a060 --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Transforms/WeightsWindow.cs @@ -0,0 +1,150 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Numerics; +using System.Runtime.CompilerServices; + +using SixLabors.ImageSharp.Memory; + +namespace SixLabors.ImageSharp.Processing.Processors +{ + /// + /// Points to a collection of of weights allocated in . + /// + internal struct WeightsWindow + { + /// + /// The local left index position + /// + public int Left; + + /// + /// The length of the weights window + /// + public int Length; + + /// + /// The index in the destination buffer + /// + private readonly int flatStartIndex; + + /// + /// The buffer containing the weights values. + /// + private readonly Buffer buffer; + + /// + /// Initializes a new instance of the struct. + /// + /// The destination index in the buffer + /// The local left index + /// The span + /// The length of the window + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal WeightsWindow(int index, int left, Buffer2D buffer, int length) + { + this.flatStartIndex = (index * buffer.Width) + left; + this.Left = left; + this.buffer = buffer; + this.Length = length; + } + + /// + /// Gets a reference to the first item of the window. + /// + /// The reference to the first item of the window + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ref float GetStartReference() + { + return ref this.buffer[this.flatStartIndex]; + } + + /// + /// Gets the span representing the portion of the that this window covers + /// + /// The + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Span GetWindowSpan() => this.buffer.Slice(this.flatStartIndex, this.Length); + + /// + /// Computes the sum of vectors in 'rowSpan' weighted by weight values, pointed by this instance. + /// + /// The input span of vectors + /// The source row position. + /// The weighted sum + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector4 ComputeWeightedRowSum(Span rowSpan, int sourceX) + { + ref float horizontalValues = ref this.GetStartReference(); + int left = this.Left; + ref Vector4 vecPtr = ref Unsafe.Add(ref rowSpan.DangerousGetPinnableReference(), left + sourceX); + + // Destination color components + Vector4 result = Vector4.Zero; + + for (int i = 0; i < this.Length; i++) + { + float weight = Unsafe.Add(ref horizontalValues, i); + Vector4 v = Unsafe.Add(ref vecPtr, i); + result += v * weight; + } + + return result; + } + + /// + /// Computes the sum of vectors in 'rowSpan' weighted by weight values, pointed by this instance. + /// Applies to all input vectors. + /// + /// The input span of vectors + /// The source row position. + /// The weighted sum + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector4 ComputeExpandedWeightedRowSum(Span rowSpan, int sourceX) + { + ref float horizontalValues = ref this.GetStartReference(); + int left = this.Left; + ref Vector4 vecPtr = ref Unsafe.Add(ref rowSpan.DangerousGetPinnableReference(), left + sourceX); + + // Destination color components + Vector4 result = Vector4.Zero; + + for (int i = 0; i < this.Length; i++) + { + float weight = Unsafe.Add(ref horizontalValues, i); + Vector4 v = Unsafe.Add(ref vecPtr, i); + result += v.Expand() * weight; + } + + return result; + } + + /// + /// Computes the sum of vectors in 'firstPassPixels' at a row pointed by 'x', + /// weighted by weight values, pointed by this instance. + /// + /// The buffer of input vectors in row first order + /// The row position + /// The source column position. + /// The weighted sum + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector4 ComputeWeightedColumnSum(Buffer2D firstPassPixels, int x, int sourceY) + { + ref float verticalValues = ref this.GetStartReference(); + int left = this.Left; + + // Destination color components + Vector4 result = Vector4.Zero; + + for (int i = 0; i < this.Length; i++) + { + float yw = Unsafe.Add(ref verticalValues, i); + int index = left + i + sourceY; + result += firstPassPixels[x, index] * yw; + } + + return result; + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Processing/Transforms/Options/ResizeOptions.cs b/src/ImageSharp/Processing/Transforms/Options/ResizeOptions.cs index 8f2d3db0a9..d20eaefb11 100644 --- a/src/ImageSharp/Processing/Transforms/Options/ResizeOptions.cs +++ b/src/ImageSharp/Processing/Transforms/Options/ResizeOptions.cs @@ -35,7 +35,7 @@ namespace SixLabors.ImageSharp.Processing /// /// Gets or sets the sampler to perform the resize operation. /// - public IResampler Sampler { get; set; } = new BicubicResampler(); + public IResampler Sampler { get; set; } = KnownResamplers.Bicubic; /// /// Gets or sets a value indicating whether to compress diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/KnownResamplers.cs b/src/ImageSharp/Processing/Transforms/Resamplers/KnownResamplers.cs new file mode 100644 index 0000000000..2da98497b5 --- /dev/null +++ b/src/ImageSharp/Processing/Transforms/Resamplers/KnownResamplers.cs @@ -0,0 +1,96 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +namespace SixLabors.ImageSharp.Processing +{ + /// + /// Contains reusable static instances of known resampling algorithms + /// + public static class KnownResamplers + { + /// + /// Gets the Bicubic sampler that implements the bicubic kernel algorithm W(x) + /// + public static IResampler Bicubic { get; } = new BicubicResampler(); + + /// + /// Gets the Box sampler that implements the box algorithm. Similar to nearest neighbor when upscaling. + /// When downscaling the pixels will average, merging pixels together. + /// + public static IResampler Box { get; } = new BoxResampler(); + + /// + /// Gets the Catmull-Rom sampler, a well known standard Cubic Filter often used as a interpolation function + /// + public static IResampler CatmullRom { get; } = new CatmullRomResampler(); + + /// + /// Gets the Hermite sampler. A type of smoothed triangular interpolation filter that rounds off strong edges while + /// preserving flat 'color levels' in the original image. + /// + public static IResampler Hermite { get; } = new HermiteResampler(); + + /// + /// Gets the Lanczos kernel sampler that implements smooth interpolation with a radius of 2 pixels. + /// This algorithm provides sharpened results when compared to others when downsampling. + /// + public static IResampler Lanczos2 { get; } = new Lanczos2Resampler(); + + /// + /// Gets the Lanczos kernel sampler that implements smooth interpolation with a radius of 3 pixels + /// This algorithm provides sharpened results when compared to others when downsampling. + /// + public static IResampler Lanczos3 { get; } = new Lanczos3Resampler(); + + /// + /// Gets the Lanczos kernel sampler that implements smooth interpolation with a radius of 5 pixels + /// This algorithm provides sharpened results when compared to others when downsampling. + /// + public static IResampler Lanczos5 { get; } = new Lanczos5Resampler(); + + /// + /// Gets the Lanczos kernel sampler that implements smooth interpolation with a radius of 8 pixels + /// This algorithm provides sharpened results when compared to others when downsampling. + /// + public static IResampler Lanczos8 { get; } = new Lanczos8Resampler(); + + /// + /// Gets the Mitchell-Netravali sampler. This seperable cubic algorithm yields a very good equilibrium between + /// detail preservation (sharpness) and smoothness. + /// + public static IResampler MitchellNetravali { get; } = new MitchellNetravaliResampler(); + + /// + /// Gets the Nearest-Neighbour sampler that implements the nearest neighbor algorithm. This uses a very fast, unscaled filter + /// which will select the closest pixel to the new pixels position. + /// + public static IResampler NearestNeighbor { get; } = new NearestNeighborResampler(); + + /// + /// Gets the Robidoux sampler. This algorithm developed by Nicolas Robidoux providing a very good equilibrium between + /// detail preservation (sharpness) and smoothness comprable to . + /// + public static IResampler Robidoux { get; } = new RobidouxResampler(); + + /// + /// Gets the Robidoux Sharp sampler. A sharpend form of the sampler + /// + public static IResampler RobidouxSharp { get; } = new RobidouxSharpResampler(); + + /// + /// Gets the Spline sampler. A seperable cubic algorithm similar to but yielding smoother results. + /// + public static IResampler Spline { get; } = new SplineResampler(); + + /// + /// Gets the Triangle sampler, otherwise known as Bilinear. This interpolation algorithm can be used where perfect image transformation + /// with pixel matching is impossible, so that one can calculate and assign appropriate intensity values to pixels + /// + public static IResampler Triangle { get; } = new TriangleResampler(); + + /// + /// Gets the Welch sampler. A high speed algorthm that delivers very sharpened results. + /// + public static IResampler Welch { get; } = new WelchResampler(); + } +} \ No newline at end of file diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/WelchResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/WelchResampler.cs index acb74a8ec4..9e18a24710 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/WelchResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/WelchResampler.cs @@ -28,4 +28,4 @@ namespace SixLabors.ImageSharp.Processing return 0F; } } -} +} \ No newline at end of file diff --git a/src/ImageSharp/Processing/Transforms/Resize.cs b/src/ImageSharp/Processing/Transforms/Resize.cs index 3c7cbca311..832b02dea7 100644 --- a/src/ImageSharp/Processing/Transforms/Resize.cs +++ b/src/ImageSharp/Processing/Transforms/Resize.cs @@ -56,7 +56,7 @@ namespace SixLabors.ImageSharp public static IImageProcessingContext Resize(this IImageProcessingContext source, Size size) where TPixel : struct, IPixel { - return Resize(source, size.Width, size.Height, new BicubicResampler(), false); + return Resize(source, size.Width, size.Height, KnownResamplers.Bicubic, false); } /// @@ -71,7 +71,7 @@ namespace SixLabors.ImageSharp public static IImageProcessingContext Resize(this IImageProcessingContext source, Size size, bool compand) where TPixel : struct, IPixel { - return Resize(source, size.Width, size.Height, new BicubicResampler(), compand); + return Resize(source, size.Width, size.Height, KnownResamplers.Bicubic, compand); } /// @@ -86,7 +86,7 @@ namespace SixLabors.ImageSharp public static IImageProcessingContext Resize(this IImageProcessingContext source, int width, int height) where TPixel : struct, IPixel { - return Resize(source, width, height, new BicubicResampler(), false); + return Resize(source, width, height, KnownResamplers.Bicubic, false); } /// @@ -102,7 +102,7 @@ namespace SixLabors.ImageSharp public static IImageProcessingContext Resize(this IImageProcessingContext source, int width, int height, bool compand) where TPixel : struct, IPixel { - return Resize(source, width, height, new BicubicResampler(), compand); + return Resize(source, width, height, KnownResamplers.Bicubic, compand); } /// diff --git a/src/ImageSharp/Processing/Transforms/Rotate.cs b/src/ImageSharp/Processing/Transforms/Rotate.cs index 7b35a879bc..69fb7ebf0c 100644 --- a/src/ImageSharp/Processing/Transforms/Rotate.cs +++ b/src/ImageSharp/Processing/Transforms/Rotate.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors; @@ -14,39 +13,37 @@ namespace SixLabors.ImageSharp public static partial class ImageExtensions { /// - /// Rotates an image by the given angle in degrees, expanding the image to fit the rotated result. + /// Rotates and flips an image by the given instructions. /// /// The pixel format. /// The image to rotate. - /// The angle in degrees to perform the rotation. + /// The to perform the rotation. /// The - public static IImageProcessingContext Rotate(this IImageProcessingContext source, float degrees) + public static IImageProcessingContext Rotate(this IImageProcessingContext source, RotateType rotateType) where TPixel : struct, IPixel - { - return Rotate(source, degrees, true); - } + => Rotate(source, (float)rotateType); /// - /// Rotates and flips an image by the given instructions. + /// Rotates an image by the given angle in degrees. /// /// The pixel format. /// The image to rotate. - /// The to perform the rotation. + /// The angle in degrees to perform the rotation. /// The - public static IImageProcessingContext Rotate(this IImageProcessingContext source, RotateType rotateType) + public static IImageProcessingContext Rotate(this IImageProcessingContext source, float degrees) where TPixel : struct, IPixel - => Rotate(source, (float)rotateType, false); + => Rotate(source, degrees, KnownResamplers.Bicubic); /// - /// Rotates an image by the given angle in degrees. + /// Rotates an image by the given angle in degrees using the specified sampling algorithm. /// /// The pixel format. /// The image to rotate. /// The angle in degrees to perform the rotation. - /// Whether to expand the image to fit the rotated result. + /// The to perform the resampling. /// The - public static IImageProcessingContext Rotate(this IImageProcessingContext source, float degrees, bool expand) + public static IImageProcessingContext Rotate(this IImageProcessingContext source, float degrees, IResampler sampler) where TPixel : struct, IPixel - => source.ApplyProcessor(new RotateProcessor { Angle = degrees, Expand = expand }); + => source.ApplyProcessor(new RotateProcessor(degrees, sampler)); } -} +} \ No newline at end of file diff --git a/src/ImageSharp/Processing/Transforms/Skew.cs b/src/ImageSharp/Processing/Transforms/Skew.cs index 00411946d9..0613a690b8 100644 --- a/src/ImageSharp/Processing/Transforms/Skew.cs +++ b/src/ImageSharp/Processing/Transforms/Skew.cs @@ -1,8 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors; namespace SixLabors.ImageSharp @@ -13,30 +13,28 @@ namespace SixLabors.ImageSharp public static partial class ImageExtensions { /// - /// Skews an image by the given angles in degrees, expanding the image to fit the skewed result. + /// Skews an image by the given angles in degrees. /// /// The pixel format. /// The image to skew. - /// The angle in degrees to perform the rotation along the x-axis. - /// The angle in degrees to perform the rotation along the y-axis. + /// The angle in degrees to perform the skew along the x-axis. + /// The angle in degrees to perform the skew along the y-axis. /// The public static IImageProcessingContext Skew(this IImageProcessingContext source, float degreesX, float degreesY) where TPixel : struct, IPixel - { - return Skew(source, degreesX, degreesY, true); - } + => Skew(source, degreesX, degreesY, KnownResamplers.Bicubic); /// - /// Skews an image by the given angles in degrees. + /// Skews an image by the given angles in degrees using the specified sampling algorithm. /// /// The pixel format. /// The image to skew. - /// The angle in degrees to perform the rotation along the x-axis. - /// The angle in degrees to perform the rotation along the y-axis. - /// Whether to expand the image to fit the skewed result. + /// The angle in degrees to perform the skew along the x-axis. + /// The angle in degrees to perform the skew along the y-axis. + /// The to perform the resampling. /// The - public static IImageProcessingContext Skew(this IImageProcessingContext source, float degreesX, float degreesY, bool expand) + public static IImageProcessingContext Skew(this IImageProcessingContext source, float degreesX, float degreesY, IResampler sampler) where TPixel : struct, IPixel - => source.ApplyProcessor(new SkewProcessor { AngleX = degreesX, AngleY = degreesY, Expand = expand }); + => source.ApplyProcessor(new SkewProcessor(degreesX, degreesY, sampler)); } -} +} \ No newline at end of file diff --git a/src/ImageSharp/Processing/Transforms/Transform.cs b/src/ImageSharp/Processing/Transforms/Transform.cs new file mode 100644 index 0000000000..e39da8dc0f --- /dev/null +++ b/src/ImageSharp/Processing/Transforms/Transform.cs @@ -0,0 +1,122 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Numerics; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; +using SixLabors.ImageSharp.Processing.Processors; +using SixLabors.Primitives; + +namespace SixLabors.ImageSharp +{ + /// + /// Extension methods for the type. + /// + public static partial class ImageExtensions + { + /// + /// Transforms an image by the given matrix. + /// + /// The pixel format. + /// The image to transform. + /// The transformation matrix. + /// The + public static IImageProcessingContext Transform(this IImageProcessingContext source, Matrix3x2 matrix) + where TPixel : struct, IPixel + => Transform(source, matrix, KnownResamplers.NearestNeighbor); + + /// + /// Transforms an image by the given matrix using the specified sampling algorithm. + /// + /// The pixel format. + /// The image to transform. + /// The transformation matrix. + /// The to perform the resampling. + /// The + public static IImageProcessingContext Transform(this IImageProcessingContext source, Matrix3x2 matrix, IResampler sampler) + where TPixel : struct, IPixel + => Transform(source, matrix, sampler, Size.Empty); + + /// + /// Transforms an image by the given matrix using the specified sampling algorithm + /// and a rectangle defining the transform origin in the source image and the size of the result image. + /// + /// The pixel format. + /// The image to transform. + /// The transformation matrix. + /// The to perform the resampling. + /// + /// The rectangle defining the transform origin in the source image, and the size of the result image. + /// + /// The + public static IImageProcessingContext Transform( + this IImageProcessingContext source, + Matrix3x2 matrix, + IResampler sampler, + Rectangle rectangle) + where TPixel : struct, IPixel + { + var t = Matrix3x2.CreateTranslation(-rectangle.Location); + Matrix3x2 combinedMatrix = t * matrix; + return source.ApplyProcessor(new AffineTransformProcessor(combinedMatrix, sampler, rectangle.Size)); + } + + /// + /// Transforms an image by the given matrix using the specified sampling algorithm, + /// cropping or extending the image according to . + /// + /// The pixel format. + /// The image to transform. + /// The transformation matrix. + /// The to perform the resampling. + /// The size of the destination image. + /// The + public static IImageProcessingContext Transform( + this IImageProcessingContext source, + Matrix3x2 matrix, + IResampler sampler, + Size destinationSize) + where TPixel : struct, IPixel + { + return source.ApplyProcessor(new AffineTransformProcessor(matrix, sampler, destinationSize)); + } + + /// + /// Transforms an image by the given matrix. + /// + /// The pixel format. + /// The image to transform. + /// The transformation matrix. + /// The + internal static IImageProcessingContext Transform(this IImageProcessingContext source, Matrix4x4 matrix) + where TPixel : struct, IPixel + => Transform(source, matrix, KnownResamplers.NearestNeighbor); + + /// + /// Applies a projective transform to the image by the given matrix using the specified sampling algorithm. + /// TODO: Doesn't work yet! Implement tests + Finish implementation + Document Matrix4x4 behavior + /// + /// The pixel format. + /// The image to transform. + /// The transformation matrix. + /// The to perform the resampling. + /// The + internal static IImageProcessingContext Transform(this IImageProcessingContext source, Matrix4x4 matrix, IResampler sampler) + where TPixel : struct, IPixel + => Transform(source, matrix, sampler, Rectangle.Empty); + + /// + /// Applies a projective transform to the image by the given matrix using the specified sampling algorithm. + /// TODO: Doesn't work yet! Implement tests + Finish implementation + Document Matrix4x4 behavior + /// + /// The pixel format. + /// The image to transform. + /// The transformation matrix. + /// The to perform the resampling. + /// The rectangle to constrain the transformed image to. + /// The + internal static IImageProcessingContext Transform(this IImageProcessingContext source, Matrix4x4 matrix, IResampler sampler, Rectangle rectangle) + where TPixel : struct, IPixel + => source.ApplyProcessor(new ProjectiveTransformProcessor(matrix, sampler, rectangle)); + } +} \ No newline at end of file diff --git a/src/ImageSharp/Processing/Transforms/TransformHelpers.cs b/src/ImageSharp/Processing/Transforms/TransformHelpers.cs new file mode 100644 index 0000000000..119fc9eeda --- /dev/null +++ b/src/ImageSharp/Processing/Transforms/TransformHelpers.cs @@ -0,0 +1,68 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Numerics; +using SixLabors.Primitives; + +namespace SixLabors.ImageSharp +{ + /// + /// Contains helper methods for working with affine and non-affine transforms + /// + internal class TransformHelpers + { + /// + /// Returns the bounding relative to the source for the given transformation matrix. + /// + /// The source rectangle. + /// The transformation matrix. + /// + /// The . + /// + public static Rectangle GetTransformedBoundingRectangle(Rectangle rectangle, Matrix3x2 matrix) + { + // Calculate the position of the four corners in world space by applying + // The world matrix to the four corners in object space (0, 0, width, height) + var tl = Vector2.Transform(Vector2.Zero, matrix); + var tr = Vector2.Transform(new Vector2(rectangle.Width, 0), matrix); + var bl = Vector2.Transform(new Vector2(0, rectangle.Height), matrix); + var br = Vector2.Transform(new Vector2(rectangle.Width, rectangle.Height), matrix); + + return GetBoundingRectangle(tl, tr, bl, br); + } + + /// + /// Returns the bounding relative to the source for the given transformation matrix. + /// + /// The source rectangle. + /// The transformation matrix. + /// + /// The . + /// + public static Rectangle GetTransformedBoundingRectangle(Rectangle rectangle, Matrix4x4 matrix) + { + // Calculate the position of the four corners in world space by applying + // The world matrix to the four corners in object space (0, 0, width, height) + var tl = Vector2.Transform(Vector2.Zero, matrix); + var tr = Vector2.Transform(new Vector2(rectangle.Width, 0), matrix); + var bl = Vector2.Transform(new Vector2(0, rectangle.Height), matrix); + var br = Vector2.Transform(new Vector2(rectangle.Width, rectangle.Height), matrix); + + return GetBoundingRectangle(tl, tr, bl, br); + } + + private static Rectangle GetBoundingRectangle(Vector2 tl, Vector2 tr, Vector2 bl, Vector2 br) + { + // Find the minimum and maximum "corners" based on the given vectors + float minX = MathF.Min(tl.X, MathF.Min(tr.X, MathF.Min(bl.X, br.X))); + float maxX = MathF.Max(tl.X, MathF.Max(tr.X, MathF.Max(bl.X, br.X))); + float minY = MathF.Min(tl.Y, MathF.Min(tr.Y, MathF.Min(bl.Y, br.Y))); + float maxY = MathF.Max(tl.Y, MathF.Max(tr.Y, MathF.Max(bl.Y, br.Y))); + float sizeX = maxX - minX + .5F; + float sizeY = maxY - minY + .5F; + + return new Rectangle((int)(MathF.Ceiling(minX) - .5F), (int)(MathF.Ceiling(minY) - .5F), (int)MathF.Floor(sizeX), (int)MathF.Floor(sizeY)); + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Drawing/DrawImageTest.cs b/tests/ImageSharp.Tests/Drawing/DrawImageTest.cs index 2e3a730fcc..6a55d8a561 100644 --- a/tests/ImageSharp.Tests/Drawing/DrawImageTest.cs +++ b/tests/ImageSharp.Tests/Drawing/DrawImageTest.cs @@ -10,6 +10,7 @@ using Xunit; namespace SixLabors.ImageSharp.Tests { using System; + using System.Numerics; public class DrawImageTest : FileTestBase { @@ -45,6 +46,25 @@ namespace SixLabors.ImageSharp.Tests } } + [Theory] + [WithFileCollection(nameof(TestFiles), PixelTypes, PixelBlenderMode.Normal)] + public void ImageShouldDrawTransformedImage(TestImageProvider provider, PixelBlenderMode mode) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage()) + using (Image blend = Image.Load(TestFile.Create(TestImages.Bmp.Car).Bytes)) + { + Matrix3x2 rotate = Matrix3x2Extensions.CreateRotationDegrees(45F); + Matrix3x2 scale = Matrix3x2Extensions.CreateScale(new SizeF(.25F, .25F)); + + blend.Mutate(x => x.Transform(rotate * scale)); + + var position = new Point((image.Width - blend.Width) / 2, (image.Height - blend.Height) / 2); + image.Mutate(x => x.DrawImage(blend, mode, .75F, new Size(blend.Width, blend.Height), position)); + image.DebugSave(provider, new[] { "Transformed" }); + } + } + [Theory] [WithSolidFilledImages(100, 100, 255, 255, 255, PixelTypes.Rgba32)] public void ImageShouldHandleNegativeLocation(TestImageProvider provider) diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index 7e0329ef46..4f214fd85c 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -17,6 +17,9 @@ + + + diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeProfilingBenchmarks.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeProfilingBenchmarks.cs index 963b849d2a..98dbbadaba 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeProfilingBenchmarks.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeProfilingBenchmarks.cs @@ -38,13 +38,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms // [Fact] public void PrintWeightsData() { - var proc = new ResizeProcessor(new BicubicResampler(), 200, 200); + var proc = new ResizeProcessor(KnownResamplers.Bicubic, 200, 200); - ResamplingWeightedProcessor.WeightsBuffer weights = proc.PrecomputeWeights(200, 500); + WeightsBuffer weights = proc.PrecomputeWeights(200, 500); var bld = new StringBuilder(); - foreach (ResamplingWeightedProcessor.WeightsWindow window in weights.Weights) + foreach (WeightsWindow window in weights.Weights) { Span span = window.GetWindowSpan(); for (int i = 0; i < window.Length; i++) diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs index a5e21b8ef3..92f1af58ed 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs @@ -20,19 +20,20 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms public static readonly TheoryData AllReSamplers = new TheoryData { - { "Bicubic", new BicubicResampler() }, - { "Triangle", new TriangleResampler() }, - { "NearestNeighbor", new NearestNeighborResampler() }, - { "Box", new BoxResampler() }, - { "Lanczos3", new Lanczos3Resampler() }, - { "Lanczos5", new Lanczos5Resampler() }, - { "MitchellNetravali", new MitchellNetravaliResampler() }, - { "Lanczos8", new Lanczos8Resampler() }, - { "Hermite", new HermiteResampler() }, - { "Spline", new SplineResampler() }, - { "Robidoux", new RobidouxResampler() }, - { "RobidouxSharp", new RobidouxSharpResampler() }, - { "Welch", new WelchResampler() } + { "Bicubic", KnownResamplers.Bicubic }, + { "Triangle", KnownResamplers.Triangle}, + { "NearestNeighbor", KnownResamplers.NearestNeighbor }, + { "Box", KnownResamplers.Box }, + // { "Lanczos2", KnownResamplers.Lanczos2 }, TODO: Add expected file + { "Lanczos3", KnownResamplers.Lanczos3 }, + { "Lanczos5", KnownResamplers.Lanczos5 }, + { "MitchellNetravali", KnownResamplers.MitchellNetravali }, + { "Lanczos8", KnownResamplers.Lanczos8 }, + { "Hermite", KnownResamplers.Hermite }, + { "Spline", KnownResamplers.Spline }, + { "Robidoux", KnownResamplers.Robidoux }, + { "RobidouxSharp", KnownResamplers.RobidouxSharp }, + { "Welch", KnownResamplers.Welch } }; [Theory] @@ -105,7 +106,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms var sourceRectangle = new Rectangle(image.Width / 8, image.Height / 8, image.Width / 4, image.Height / 4); var destRectangle = new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2); - image.Mutate(x => x.Resize(image.Width, image.Height, new BicubicResampler(), sourceRectangle, destRectangle, false)); + image.Mutate(x => x.Resize(image.Width, image.Height, KnownResamplers.Bicubic, sourceRectangle, destRectangle, false)); image.DebugSave(provider); image.CompareToReferenceOutput(provider); @@ -286,7 +287,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms [InlineData(2, 0)] public static void BicubicWindowOscillatesCorrectly(float x, float expected) { - var sampler = new BicubicResampler(); + var sampler = KnownResamplers.Bicubic; float result = sampler.GetValue(x); Assert.Equal(result, expected); @@ -300,7 +301,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms [InlineData(2, 0)] public static void TriangleWindowOscillatesCorrectly(float x, float expected) { - var sampler = new TriangleResampler(); + var sampler = KnownResamplers.Triangle; float result = sampler.GetValue(x); Assert.Equal(result, expected); @@ -314,7 +315,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms [InlineData(2, 0)] public static void Lanczos3WindowOscillatesCorrectly(float x, float expected) { - var sampler = new Lanczos3Resampler(); + var sampler = KnownResamplers.Lanczos3; float result = sampler.GetValue(x); Assert.Equal(result, expected); @@ -328,7 +329,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms [InlineData(4, 0)] public static void Lanczos5WindowOscillatesCorrectly(float x, float expected) { - var sampler = new Lanczos5Resampler(); + var sampler = KnownResamplers.Lanczos5; float result = sampler.GetValue(x); Assert.Equal(result, expected); diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs index 161af43c91..b5220ea948 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs @@ -8,13 +8,15 @@ using Xunit; // ReSharper disable InconsistentNaming namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms { + using System; + using System.Reflection; + public class RotateTests : FileTestBase { - public static readonly TheoryData RotateFloatValues + public static readonly TheoryData RotateAngles = new TheoryData { - 170, - -170 + 50, -50, 170, -170 }; public static readonly TheoryData RotateEnumValues @@ -25,11 +27,11 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms RotateType.Rotate180, RotateType.Rotate270 }; - + [Theory] - [WithTestPatternImages(nameof(RotateFloatValues), 100, 50, DefaultPixelType)] - [WithTestPatternImages(nameof(RotateFloatValues), 50, 100, DefaultPixelType)] - public void Rotate(TestImageProvider provider, float value) + [WithTestPatternImages(nameof(RotateAngles), 100, 50, DefaultPixelType)] + [WithTestPatternImages(nameof(RotateAngles), 50, 100, DefaultPixelType)] + public void Rotate_WithAngle(TestImageProvider provider, float value) where TPixel : struct, IPixel { using (Image image = provider.GetImage()) @@ -38,7 +40,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms image.DebugSave(provider, value); } } - + [Theory] [WithTestPatternImages(nameof(RotateEnumValues), 100, 50, DefaultPixelType)] [WithTestPatternImages(nameof(RotateEnumValues), 50, 100, DefaultPixelType)] diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/SkewTest.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/SkewTest.cs index 5e83fa620d..17bf3def26 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/SkewTest.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/SkewTest.cs @@ -6,17 +6,43 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms { + using System; + using System.Collections.Generic; + using System.Reflection; + + using SixLabors.ImageSharp.Processing; + public class SkewTest : FileTestBase { public static readonly TheoryData SkewValues - = new TheoryData + = new TheoryData { { 20, 10 }, { -20, -10 } }; + public static readonly List ResamplerNames + = new List + { + 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] - [WithFileCollection(nameof(DefaultFiles), nameof(SkewValues), DefaultPixelType)] + [WithTestPatternImages(nameof(SkewValues), 100, 50, DefaultPixelType)] public void ImageShouldSkew(TestImageProvider provider, float x, float y) where TPixel : struct, IPixel { @@ -26,5 +52,33 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms image.DebugSave(provider, string.Join("_", x, y)); } } + + [Theory] + [WithTestPatternImages(nameof(SkewValues), 100, 50, DefaultPixelType)] + public void ImageShouldSkewWithSampler(TestImageProvider provider, float x, float y) + where TPixel : struct, IPixel + { + foreach (string resamplerName in ResamplerNames) + { + IResampler sampler = GetResampler(resamplerName); + using (Image image = provider.GetImage()) + { + image.Mutate(i => i.Skew(x, y, sampler)); + image.DebugSave(provider, string.Join("_", x, y, 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); + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs new file mode 100644 index 0000000000..ace6cb70d6 --- /dev/null +++ b/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs @@ -0,0 +1,258 @@ +using System; +using System.Numerics; +using System.Reflection; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; +using SixLabors.Primitives; +using Xunit; +using Xunit.Abstractions; +using SixLabors.ImageSharp.Helpers; +// ReSharper disable InconsistentNaming + +namespace SixLabors.ImageSharp.Tests.Processing.Transforms +{ + public class AffineTransformTests + { + private readonly ITestOutputHelper Output; + + /// + /// angleDeg, sx, sy, tx, ty + /// + public static readonly TheoryData TransformValues + = new TheoryData + { + { 0, 1, 1, 0, 0 }, + { 50, 1, 1, 0, 0 }, + { 0, 1, 1, 20, 10 }, + { 50, 1, 1, 20, 10 }, + { 0, 1, 1, -20, -10 }, + { 50, 1, 1, -20, -10 }, + { 50, 1.5f, 1.5f, 0, 0 }, + { 50, 1.1F, 1.3F, 30, -20 }, + { 0, 2f, 1f, 0, 0 }, + { 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 Transform_DoesNotCreateEdgeArtifacts_ResamplerNames = + new TheoryData + { + nameof(KnownResamplers.NearestNeighbor), + nameof(KnownResamplers.Triangle), + nameof(KnownResamplers.Bicubic), + nameof(KnownResamplers.Lanczos8), + }; + + public AffineTransformTests(ITestOutputHelper output) + { + this.Output = output; + } + + /// + /// The output of an "all white" image should be "all white" or transparent, regardless of the transformation and the resampler. + /// + [Theory] + [WithSolidFilledImages(nameof(Transform_DoesNotCreateEdgeArtifacts_ResamplerNames), 5, 5, 255, 255, 255, 255, PixelTypes.Rgba32)] + public void Transform_DoesNotCreateEdgeArtifacts(TestImageProvider provider, string resamplerName) + where TPixel : struct, IPixel + { + IResampler resampler = GetResampler(resamplerName); + using (Image image = provider.GetImage()) + { + var rotate = Matrix3x2.CreateRotation((float)Math.PI / 4F, new Vector2(5 / 2F, 5 / 2F)); + var translate = Matrix3x2.CreateTranslation((7 - 5) / 2F, (7 - 5) / 2F); + + Rectangle sourceRectangle = image.Bounds(); + Matrix3x2 matrix = rotate * translate; + + Rectangle destRectangle = TransformHelpers.GetTransformedBoundingRectangle(sourceRectangle, matrix); + + image.Mutate(c => c.Transform(matrix, resampler, destRectangle)); + image.DebugSave(provider, resamplerName); + + VerifyAllPixelsAreWhiteOrTransparent(image); + } + } + + [Theory] + [WithTestPatternImages(nameof(TransformValues), 100, 50, PixelTypes.Rgba32)] + public void Transform_RotateScaleTranslate( + TestImageProvider provider, + float angleDeg, + float sx, float sy, + float tx, float ty) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage()) + { + Matrix3x2 rotate = Matrix3x2Extensions.CreateRotationDegrees(angleDeg); + var translate = Matrix3x2.CreateTranslation(tx, ty); + var scale = Matrix3x2.CreateScale(sx, sy); + Matrix3x2 m = rotate * scale * translate; + + this.PrintMatrix(m); + + image.Mutate(i => i.Transform(m, KnownResamplers.Bicubic)); + + string testOutputDetails = $"R({angleDeg})_S({sx},{sy})_T({tx},{ty})"; + image.DebugSave(provider, testOutputDetails); + image.CompareToReferenceOutput(provider, testOutputDetails); + } + } + + [Theory] + [WithTestPatternImages(96, 96, PixelTypes.Rgba32, 50, 0.8f)] + public void Transform_RotateScale_ManuallyCentered(TestImageProvider provider, float angleDeg, float s) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage()) + { + Matrix3x2 m = this.MakeManuallyCenteredMatrix(angleDeg, s, image); + + image.Mutate(i => i.Transform(m, KnownResamplers.Bicubic)); + + string testOutputDetails = $"R({angleDeg})_S({s})"; + image.DebugSave(provider, testOutputDetails); + image.CompareToReferenceOutput(provider, testOutputDetails); + } + } + + public static readonly TheoryData Transform_IntoRectangle_Data = + new TheoryData + { + { 0, 0, 10, 10 }, + { 0, 0, 5, 10 }, + { 0, 0, 10, 5 }, + { 5, 0, 5, 10 }, + {-5,-5, 20, 20 } + }; + + /// + /// Testing transforms using custom source rectangles: + /// https://github.com/SixLabors/ImageSharp/pull/386#issuecomment-357104963 + /// + [Theory] + [WithTestPatternImages(96, 48, PixelTypes.Rgba32)] + public void Transform_FromSourceRectangle1(TestImageProvider provider) + where TPixel : struct, IPixel + { + var rectangle = new Rectangle(48, 0, 96, 36); + + 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); + image.CompareToReferenceOutput(provider); + } + } + + [Theory] + [WithTestPatternImages(96, 48, PixelTypes.Rgba32)] + public void Transform_FromSourceRectangle2(TestImageProvider provider) + where TPixel : struct, IPixel + { + var rectangle = new Rectangle(0, 24, 48, 48); + + using (Image image = provider.GetImage()) + { + var m = Matrix3x2.CreateScale(1.0F, 2.0F); + + image.Mutate(i => i.Transform(m, KnownResamplers.Spline, rectangle)); + + image.DebugSave(provider); + image.CompareToReferenceOutput(provider); + } + } + + [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()) + { + Matrix3x2 m = this.MakeManuallyCenteredMatrix(50, 0.6f, image); + + image.Mutate(i => + { + i.Transform(m, sampler); + }); + + image.DebugSave(provider, resamplerName); + image.CompareToReferenceOutput(provider, resamplerName); + } + } + + private Matrix3x2 MakeManuallyCenteredMatrix(float angleDeg, float s, Image image) + where TPixel : struct, IPixel + { + Matrix3x2 rotate = Matrix3x2Extensions.CreateRotationDegrees(angleDeg); + Vector2 toCenter = 0.5f * new Vector2(image.Width, image.Height); + var translate = Matrix3x2.CreateTranslation(-toCenter); + var translateBack = Matrix3x2.CreateTranslation(toCenter); + var scale = Matrix3x2.CreateScale(s); + + Matrix3x2 m = translate * rotate * scale * translateBack; + + this.PrintMatrix(m); + return m; + } + + 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); + } + + private static void VerifyAllPixelsAreWhiteOrTransparent(Image image) + where TPixel : struct, IPixel + { + TPixel[] data = new TPixel[image.Width * image.Height]; + image.Frames.RootFrame.SavePixelData(data); + var rgba = default(Rgba32); + var white = new Rgb24(255, 255, 255); + foreach (TPixel pixel in data) + { + pixel.ToRgba32(ref rgba); + if (rgba.A == 0) continue; + + Assert.Equal(white, rgba.Rgb); + } + } + + private void PrintMatrix(Matrix3x2 a) + { + string s = $"{a.M11:F10},{a.M12:F10},{a.M21:F10},{a.M22:F10},{a.M31:F10},{a.M32:F10}"; + this.Output.WriteLine(s); + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Processing/Transforms/RotateFlipTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/RotateFlipTests.cs index 67602131b0..75d7067702 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/RotateFlipTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/RotateFlipTests.cs @@ -25,14 +25,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms [InlineData(RotateType.Rotate90, FlipType.Vertical, 90)] [InlineData(RotateType.Rotate180, FlipType.Vertical, 180)] [InlineData(RotateType.Rotate270, FlipType.Vertical, 270)] - public void Rotate_degreesFloat_RotateProcessorWithAnglesSetAndExpandTrue(RotateType angle, FlipType flip, float expectedAngle) + public void Rotate_degreesFloat_RotateProcessorWithAnglesSetrue(RotateType angle, FlipType flip, float expectedAngle) { this.operations.RotateFlip(angle, flip); - var rotateProcessor = this.Verify>(0); - var flipProcessor = this.Verify>(1); + RotateProcessor rotateProcessor = this.Verify>(0); + FlipProcessor flipProcessor = this.Verify>(1); - Assert.Equal(expectedAngle, rotateProcessor.Angle); - Assert.False(rotateProcessor.Expand); + Assert.Equal(expectedAngle, rotateProcessor.Degrees); Assert.Equal(flip, flipProcessor.FlipType); } } diff --git a/tests/ImageSharp.Tests/Processing/Transforms/RotateTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/RotateTests.cs index 80eccd00cd..a990fa88ca 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/RotateTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/RotateTests.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors; using Xunit; @@ -10,46 +9,28 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms { public class RotateTests : BaseImageOperationsExtensionTest { - [Theory] [InlineData(85.6f)] [InlineData(21)] - public void Rotate_degreesFloat_RotateProcessorWithAnglesSetAndExpandTrue(float angle) + public void RotateDegreesFloatRotateProcessorWithAnglesSet(float angle) { this.operations.Rotate(angle); - var processor = this.Verify>(); + RotateProcessor processor = this.Verify>(); - Assert.Equal(angle, processor.Angle); - Assert.True(processor.Expand); + Assert.Equal(angle, processor.Degrees); } - [Theory] [InlineData(RotateType.None, 0)] [InlineData(RotateType.Rotate90, 90)] [InlineData(RotateType.Rotate180, 180)] [InlineData(RotateType.Rotate270, 270)] - public void Rotate_RotateType_RotateProcessorWithAnglesConvertedFromEnumAndExpandTrue(RotateType angle, float expectedangle) + public void RotateRotateTypeRotateProcessorWithAnglesConvertedFromEnum(RotateType angle, float expectedangle) { this.operations.Rotate(angle); // is this api needed ??? - var processor = this.Verify>(); - - Assert.Equal(expectedangle, processor.Angle); - Assert.False(processor.Expand); - } - - - [Theory] - [InlineData(85.6f, false)] - [InlineData(21, true)] - [InlineData(21, false)] - public void Rotate_degreesFloat_expand_RotateProcessorWithAnglesSetAndExpandSet(float angle, bool expand) - { - this.operations.Rotate(angle, expand); - var processor = this.Verify>(); + RotateProcessor processor = this.Verify>(); - Assert.Equal(angle, processor.Angle); - Assert.Equal(expand, processor.Expand); + Assert.Equal(expectedangle, processor.Degrees); } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Processing/Transforms/SkewTest.cs b/tests/ImageSharp.Tests/Processing/Transforms/SkewTest.cs index 28a0e0d8f8..d2cc5764ed 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/SkewTest.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/SkewTest.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; using Xunit; @@ -10,25 +9,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms public class SkewTest : BaseImageOperationsExtensionTest { [Fact] - public void Skew_x_y_CreateSkewProcessorWithAnglesSetAndExpandTrue() + public void SkewXYCreateSkewProcessorWithAnglesSet() { this.operations.Skew(10, 20); - var processor = this.Verify>(); + SkewProcessor processor = this.Verify>(); - Assert.Equal(10, processor.AngleX); - Assert.Equal(20, processor.AngleY); - Assert.True(processor.Expand); - } - - [Fact] - public void Skew_x_y_expand_CreateSkewProcessorWithAnglesSetAndExpandTrue() - { - this.operations.Skew(10, 20, false); - var processor = this.Verify>(); - - Assert.Equal(10, processor.AngleX); - Assert.Equal(20, processor.AngleY); - Assert.False(processor.Expand); + Assert.Equal(10, processor.DegreesX); + Assert.Equal(20, processor.DegreesY); } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/Attributes/WithSolidFilledImagesAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithSolidFilledImagesAttribute.cs index f787a35916..991f7108fe 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Attributes/WithSolidFilledImagesAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithSolidFilledImagesAttribute.cs @@ -6,6 +6,8 @@ using System.Reflection; namespace SixLabors.ImageSharp.Tests { + using SixLabors.ImageSharp.PixelFormats; + /// /// Triggers passing instances which produce an image of size width * height filled with the requested color. /// One instance will be passed for each the pixel format defined by the pixelTypes parameter @@ -56,14 +58,88 @@ namespace SixLabors.ImageSharp.Tests byte a, PixelTypes pixelTypes, params object[] additionalParameters) - : base(width, height, pixelTypes, additionalParameters) + : this(null, width, height, r, g, b, a, pixelTypes, additionalParameters) + { + } + + /// + /// Triggers passing instances which produce an image of size width * height filled with the requested color. + /// One instance will be passed for each the pixel format defined by the pixelTypes parameter + /// + /// The member data to apply to theories + /// The width of the requested image + /// The height of the requested image + /// Red + /// Green + /// Blue + /// /// Alpha + /// The requested pixel types + /// Additional theory parameter values + public WithSolidFilledImagesAttribute( + string memberData, + int width, + int height, + byte r, + byte g, + byte b, + byte a, + PixelTypes pixelTypes, + params object[] additionalParameters) + : base(memberData, width, height, pixelTypes, additionalParameters) { this.R = r; this.G = g; this.B = b; this.A = a; } - + + /// + /// Triggers passing instances which produce an image of size width * height filled with the requested color. + /// One instance will be passed for each the pixel format defined by the pixelTypes parameter + /// + /// The width of the requested image + /// The height of the requested image + /// The referenced color name (name of property in + /// The requested pixel types + /// Additional theory parameter values + public WithSolidFilledImagesAttribute( + int width, + int height, + string colorName, + PixelTypes pixelTypes, + params object[] additionalParameters) + : this(null, width, height, colorName, pixelTypes, additionalParameters) + { + } + + /// + /// Triggers passing instances which produce an image of size width * height filled with the requested color. + /// One instance will be passed for each the pixel format defined by the pixelTypes parameter + /// + /// The member data to apply to theories + /// The width of the requested image + /// The height of the requested image + /// The referenced color name (name of property in + /// The requested pixel types + /// Additional theory parameter values + public WithSolidFilledImagesAttribute( + string memberData, + int width, + int height, + string colorName, + PixelTypes pixelTypes, + params object[] additionalParameters) + : base(memberData, width, height, pixelTypes, additionalParameters) + { + Guard.NotNull(colorName, nameof(colorName)); + + var c = (Rgba32)typeof(Rgba32).GetTypeInfo().GetField(colorName).GetValue(null); + this.R = c.R; + this.G = c.G; + this.B = c.B; + this.A = c.A; + } + /// /// Red /// diff --git a/tests/ImageSharp.Tests/TestUtilities/Attributes/WithTestPatternImageAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithTestPatternImageAttribute.cs index 585bb8f066..7c659c64fc 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Attributes/WithTestPatternImageAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithTestPatternImageAttribute.cs @@ -20,7 +20,7 @@ namespace SixLabors.ImageSharp.Tests /// The requested parameter /// Additional theory parameter values public WithTestPatternImagesAttribute(int width, int height, PixelTypes pixelTypes, params object[] additionalParameters) - : this(null, width, height, pixelTypes,additionalParameters) + : this(null, width, height, pixelTypes, additionalParameters) { }