From f3fb47ba379d17252f1d0c3314b83ad269db2f86 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 22 Nov 2017 00:55:40 +1100 Subject: [PATCH 01/51] Refactor affine classes to inherit from weighted --- .../Processors/Transforms/AffineProcessor.cs | 95 ++++++++ .../Transforms/Matrix3x2Processor.cs | 50 ---- .../Processors/Transforms/RotateProcessor.cs | 218 +++++++++--------- .../Processors/Transforms/SkewProcessor.cs | 79 +++---- .../Processing/Transforms/Rotate.cs | 3 +- src/ImageSharp/Processing/Transforms/Skew.cs | 4 +- 6 files changed, 249 insertions(+), 200 deletions(-) create mode 100644 src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs delete mode 100644 src/ImageSharp/Processing/Processors/Transforms/Matrix3x2Processor.cs diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs new file mode 100644 index 0000000000..4d70fba84f --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -0,0 +1,95 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using SixLabors.ImageSharp.Advanced; +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 abstract class AffineProcessor : ResamplingWeightedProcessor + where TPixel : struct, IPixel + { + // TODO: Move to constants somewhere else to prevent generic type duplication. + private static readonly Rectangle DefaultRectangle = new Rectangle(0, 0, 1, 1); + + /// + /// Initializes a new instance of the class. + /// + /// The sampler to perform the resize operation. + protected AffineProcessor(IResampler sampler) + : base(sampler, 1, 1, DefaultRectangle) // Hack to prevent Guard throwing in base, we always set the canvas + { + } + + /// + /// Gets or sets a value indicating whether to expand the canvas to fit the skewed image. + /// + public bool Expand { get; set; } = true; + + /// + /// Returns the processing matrix used for transforming the image. + /// + /// The + protected abstract Matrix3x2 CreateProcessingMatrix(); + + /// + /// Creates a new target canvas to contain the results of the matrix transform. + /// + /// The source rectangle. + protected virtual void CreateNewCanvas(Rectangle sourceRectangle) + { + if (this.ResizeRectangle == DefaultRectangle) + { + if (this.Expand) + { + this.ResizeRectangle = Matrix3x2.Invert(this.CreateProcessingMatrix(), out Matrix3x2 sizeMatrix) + ? ImageMaths.GetBoundingRectangle(sourceRectangle, sizeMatrix) + : sourceRectangle; + } + else + { + this.ResizeRectangle = sourceRectangle; + } + } + + this.Width = this.ResizeRectangle.Width; + this.Height = this.ResizeRectangle.Height; + } + + /// + protected override Image CreateDestination(Image source, Rectangle sourceRectangle) + { + this.CreateNewCanvas(sourceRectangle); + + // 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())); + + // Use the overload to prevent an extra frame being added + return new Image(source.GetConfiguration(), source.MetaData.Clone(), frames); + } + + /// + /// 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.ResizeRectangle.Width * .5F, -this.ResizeRectangle.Height * .5F); + var translateToSourceCenter = Matrix3x2.CreateTranslation(source.Width * .5F, source.Height * .5F); + return (translationToTargetCenter * matrix) * translateToSourceCenter; + } + } +} \ 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/RotateProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs index 86a0c73603..f47d483596 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs @@ -6,7 +6,6 @@ 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,46 +16,84 @@ namespace SixLabors.ImageSharp.Processing.Processors /// Provides methods that allow the rotating of images. /// /// The pixel format. - internal class RotateProcessor : Matrix3x2Processor + internal class RotateProcessor : AffineProcessor where TPixel : struct, IPixel { + private Matrix3x2 transformMatrix; + /// - /// The transform matrix to apply. + /// Initializes a new instance of the class. /// - private Matrix3x2 processMatrix; + public RotateProcessor() + : base(new BicubicResampler()) + { + } /// - /// Gets or sets the angle of processMatrix in degrees. + /// Initializes a new instance of the class. /// - public float Angle { get; set; } + /// The sampler to perform the resize operation. + public RotateProcessor(IResampler sampler) + : base(sampler) + { + } /// - /// Gets or sets a value indicating whether to expand the canvas to fit the rotated image. + /// Gets or sets the angle of processMatrix in degrees. /// - public bool Expand { get; set; } = true; + public float Angle { get; set; } /// - protected override void OnApply(ImageFrame source, Rectangle sourceRectangle, Configuration configuration) + protected override Matrix3x2 CreateProcessingMatrix() { - if (this.OptimizedApply(source, configuration)) + if (this.transformMatrix == default(Matrix3x2)) + { + this.transformMatrix = Matrix3x2Extensions.CreateRotationDegrees(-this.Angle, new Point(0, 0)); + } + + return this.transformMatrix; + } + + /// + protected override void CreateNewCanvas(Rectangle sourceRectangle) + { + if (MathF.Abs(this.Angle) < Constants.Epsilon || + MathF.Abs(this.Angle - 180) < Constants.Epsilon) + { + this.ResizeRectangle = sourceRectangle; + } + + if (MathF.Abs(this.Angle - 90) < Constants.Epsilon || + MathF.Abs(this.Angle - 270) < Constants.Epsilon) + { + // We always expand enumerated rectangle values + this.ResizeRectangle = new Rectangle(0, 0, sourceRectangle.Height, sourceRectangle.Width); + } + + base.CreateNewCanvas(sourceRectangle); + } + + /// + protected override void OnApply(ImageFrame source, ImageFrame destination, Rectangle sourceRectangle, Configuration configuration) + { + if (this.OptimizedApply(source, destination, configuration)) { return; } - int height = this.CanvasRectangle.Height; - int width = this.CanvasRectangle.Width; - Matrix3x2 matrix = this.GetCenteredMatrix(source, this.processMatrix); + int height = this.ResizeRectangle.Height; + int width = this.ResizeRectangle.Width; + Matrix3x2 matrix = this.GetCenteredMatrix(source, this.CreateProcessingMatrix()); Rectangle sourceBounds = source.Bounds(); - using (var targetPixels = new PixelAccessor(width, height)) - { - Parallel.For( - 0, - height, - configuration.ParallelOptions, - y => + // TODO: Use our new weights functionality to resample on transform + Parallel.For( + 0, + height, + configuration.ParallelOptions, + y => { - Span targetRow = targetPixels.GetRowSpan(y); + Span destRow = destination.GetPixelRowSpan(y); for (int x = 0; x < width; x++) { @@ -64,34 +101,16 @@ namespace SixLabors.ImageSharp.Processing.Processors if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) { - targetRow[x] = source[transformedPoint.X, transformedPoint.Y]; + destRow[x] = source[transformedPoint.X, transformedPoint.Y]; } } }); - - source.SwapPixelsBuffers(targetPixels); - } - } - - /// - protected override void BeforeApply(ImageFrame source, 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) - { - return; - } - - this.processMatrix = Matrix3x2Extensions.CreateRotationDegrees(-this.Angle, new Point(0, 0)); - if (this.Expand) - { - this.CreateNewCanvas(sourceRectangle, this.processMatrix); - } } /// - 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; @@ -116,33 +135,35 @@ namespace SixLabors.ImageSharp.Processing.Processors /// 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) { - // 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) { - this.Rotate90(source, configuration); + this.Rotate90(source, destination, configuration); return true; } if (MathF.Abs(this.Angle - 180) < Constants.Epsilon) { - this.Rotate180(source, configuration); + this.Rotate180(source, destination, configuration); return true; } if (MathF.Abs(this.Angle - 270) < Constants.Epsilon) { - this.Rotate270(source, configuration); + this.Rotate270(source, destination, configuration); return true; } @@ -153,95 +174,82 @@ 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; - 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 => - { - 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]; - } - }); - } + 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; - 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; - 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++) - { - targetPixels[newX, x] = sourceRow[x]; - } - }); - - source.SwapPixelsBuffers(targetPixels); - } + destination[newX, x] = sourceRow[x]; + } + }); } } } \ 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..ba84eab9e8 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs @@ -6,7 +6,6 @@ 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,13 +15,19 @@ namespace SixLabors.ImageSharp.Processing.Processors /// Provides methods that allow the skewing of images. /// /// The pixel format. - internal class SkewProcessor : Matrix3x2Processor + internal class SkewProcessor : AffineProcessor where TPixel : struct, IPixel { + private Matrix3x2 transformMatrix; + /// - /// The transform matrix to apply. + /// Initializes a new instance of the class. /// - private Matrix3x2 processMatrix; + /// The sampler to perform the skew operation. + public SkewProcessor(IResampler sampler) + : base(sampler) + { + } /// /// Gets or sets the angle of rotation along the x-axis in degrees. @@ -34,52 +39,44 @@ namespace SixLabors.ImageSharp.Processing.Processors /// public float AngleY { get; set; } - /// - /// Gets or sets a value indicating whether to expand the canvas to fit the skewed image. - /// - public bool Expand { get; set; } = true; + /// + protected override Matrix3x2 CreateProcessingMatrix() + { + if (this.transformMatrix == default(Matrix3x2)) + { + this.transformMatrix = Matrix3x2Extensions.CreateSkewDegrees(-this.AngleX, -this.AngleY, new Point(0, 0)); + } + + return this.transformMatrix; + } /// - protected override void OnApply(ImageFrame source, Rectangle sourceRectangle, Configuration configuration) + protected override void OnApply(ImageFrame source, ImageFrame destination, Rectangle sourceRectangle, Configuration configuration) { - int height = this.CanvasRectangle.Height; - int width = this.CanvasRectangle.Width; - Matrix3x2 matrix = this.GetCenteredMatrix(source, this.processMatrix); + int height = this.ResizeRectangle.Height; + int width = this.ResizeRectangle.Width; + Matrix3x2 matrix = this.GetCenteredMatrix(source, this.CreateProcessingMatrix()); Rectangle sourceBounds = source.Bounds(); - using (var targetPixels = new PixelAccessor(width, height)) - { - Parallel.For( - 0, - height, - configuration.ParallelOptions, - y => + // TODO: Use our new weights functionality to resample on transform + Parallel.For( + 0, + height, + configuration.ParallelOptions, + y => + { + Span destRow = destination.GetPixelRowSpan(y); + + for (int x = 0; x < width; x++) { - Span targetRow = targetPixels.GetRowSpan(y); + var transformedPoint = Point.Skew(new Point(x, y), matrix); - for (int x = 0; x < width; x++) + if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) { - var transformedPoint = Point.Skew(new Point(x, y), matrix); - - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) - { - targetRow[x] = source[transformedPoint.X, transformedPoint.Y]; - } + destRow[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); - } + } + }); } } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Transforms/Rotate.cs b/src/ImageSharp/Processing/Transforms/Rotate.cs index 7b35a879bc..436aa3cd98 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; @@ -47,6 +46,6 @@ namespace SixLabors.ImageSharp /// The public static IImageProcessingContext Rotate(this IImageProcessingContext source, float degrees, bool expand) where TPixel : struct, IPixel - => source.ApplyProcessor(new RotateProcessor { Angle = degrees, Expand = expand }); + => source.ApplyProcessor(new RotateProcessor(new BicubicResampler()) { Angle = degrees, Expand = expand }); } } diff --git a/src/ImageSharp/Processing/Transforms/Skew.cs b/src/ImageSharp/Processing/Transforms/Skew.cs index 00411946d9..873dbe5100 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 @@ -37,6 +37,6 @@ namespace SixLabors.ImageSharp /// The public static IImageProcessingContext Skew(this IImageProcessingContext source, float degreesX, float degreesY, bool expand) where TPixel : struct, IPixel - => source.ApplyProcessor(new SkewProcessor { AngleX = degreesX, AngleY = degreesY, Expand = expand }); + => source.ApplyProcessor(new SkewProcessor(new BicubicResampler()) { AngleX = degreesX, AngleY = degreesY, Expand = expand }); } } From 6fb1def28deab7631b74f4a0fbbd1379b1aaa3e3 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 22 Nov 2017 20:15:02 +1100 Subject: [PATCH 02/51] Add broken implementation. Help needed! --- .../Processors/Transforms/AffineProcessor.cs | 44 +++++++++++++++++++ .../Processors/Transforms/RotateProcessor.cs | 36 +++++++++++++-- .../Processors/Transforms/SkewProcessor.cs | 37 ++++++++++++++-- 3 files changed, 109 insertions(+), 8 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index 4d70fba84f..87f28045c8 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Numerics; +using System.Runtime.CompilerServices; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.PixelFormats; using SixLabors.Primitives; @@ -91,5 +92,48 @@ namespace SixLabors.ImageSharp.Processing.Processors var translateToSourceCenter = Matrix3x2.CreateTranslation(source.Width * .5F, source.Height * .5F); return (translationToTargetCenter * matrix) * translateToSourceCenter; } + + /// + /// Computes the weighted sum at the given XY position + /// + /// The source image + /// The maximum x value + /// The maximum y value + /// The horizontal weights + /// The vertical weights + /// The transformed position + /// The + [MethodImpl(MethodImplOptions.AggressiveInlining)] + protected Vector4 ComputeWeightedSumAtPosition(ImageFrame source, int maxX, int maxY, ref WeightsWindow windowX, ref WeightsWindow windowY, ref Point point) + { + ref float horizontalValues = ref windowX.GetStartReference(); + ref float verticalValues = ref windowY.GetStartReference(); + int xLeft = windowX.Left; + int yLeft = windowY.Left; + int xLength = windowX.Length; + int yLength = windowY.Length; + Vector4 result = Vector4.Zero; + + // TODO: Fix this. + // Currently the output image is the separable values duplicated with half the transform applied + // and not the combined values as it should be. I must be sampling the wrong values. + for (int i = 0; i < xLength; i++) + { + int offsetX = xLeft + i + point.X; + offsetX = offsetX.Clamp(0, maxX); + float weight = Unsafe.Add(ref horizontalValues, i); + result += source[offsetX, point.Y].ToVector4() * weight; + } + + for (int i = 0; i < yLength; i++) + { + int offsetY = yLeft + i + point.Y; + offsetY = offsetY.Clamp(0, maxY); + float weight = Unsafe.Add(ref verticalValues, i); + result += source[point.X, offsetY].ToVector4() * weight; + } + + return result; + } } } \ 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 f47d483596..50b28a8312 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs @@ -86,7 +86,33 @@ namespace SixLabors.ImageSharp.Processing.Processors Matrix3x2 matrix = this.GetCenteredMatrix(source, this.CreateProcessingMatrix()); Rectangle sourceBounds = source.Bounds(); - // TODO: Use our new weights functionality to resample on transform + 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 transformedPoint = Point.Rotate(new Point(x, y), matrix); + + if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) + { + destRow[x] = source[transformedPoint.X, transformedPoint.Y]; + } + } + }); + + return; + } + + int maxX = source.Height - 1; + int maxY = source.Width - 1; + Parallel.For( 0, height, @@ -94,14 +120,16 @@ namespace SixLabors.ImageSharp.Processing.Processors y => { Span destRow = destination.GetPixelRowSpan(y); - for (int x = 0; x < width; x++) { var transformedPoint = Point.Rotate(new Point(x, y), matrix); - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) { - destRow[x] = source[transformedPoint.X, transformedPoint.Y]; + WeightsWindow windowX = this.HorizontalWeights.Weights[transformedPoint.X]; + WeightsWindow windowY = this.VerticalWeights.Weights[transformedPoint.Y]; + Vector4 dXY = this.ComputeWeightedSumAtPosition(source, maxX, maxY, ref windowX, ref windowY, ref transformedPoint); + ref TPixel dest = ref destRow[x]; + dest.PackFromVector4(dXY); } } }); diff --git a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs index ba84eab9e8..2330559951 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs @@ -58,7 +58,33 @@ namespace SixLabors.ImageSharp.Processing.Processors Matrix3x2 matrix = this.GetCenteredMatrix(source, this.CreateProcessingMatrix()); Rectangle sourceBounds = source.Bounds(); - // TODO: Use our new weights functionality to resample on transform + 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 transformedPoint = Point.Skew(new Point(x, y), matrix); + + if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) + { + destRow[x] = source[transformedPoint.X, transformedPoint.Y]; + } + } + }); + + return; + } + + int maxX = source.Height - 1; + int maxY = source.Width - 1; + Parallel.For( 0, height, @@ -66,14 +92,17 @@ namespace SixLabors.ImageSharp.Processing.Processors y => { Span destRow = destination.GetPixelRowSpan(y); - for (int x = 0; x < width; x++) { var transformedPoint = Point.Skew(new Point(x, y), matrix); - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) { - destRow[x] = source[transformedPoint.X, transformedPoint.Y]; + WeightsWindow windowX = this.HorizontalWeights.Weights[transformedPoint.X]; + WeightsWindow windowY = this.VerticalWeights.Weights[transformedPoint.Y]; + + Vector4 dXY = this.ComputeWeightedSumAtPosition(source, maxX, maxY, ref windowX, ref windowY, ref transformedPoint); + ref TPixel dest = ref destRow[x]; + dest.PackFromVector4(dXY); } } }); From 23974da9fc525c2d1c1b6af02a5f643107c26973 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 22 Nov 2017 22:05:48 +1100 Subject: [PATCH 03/51] Fix max values --- .../Processing/Processors/Transforms/RotateProcessor.cs | 4 ++-- .../Processing/Processors/Transforms/SkewProcessor.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs index 50b28a8312..1bcd26d8d4 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs @@ -110,8 +110,8 @@ namespace SixLabors.ImageSharp.Processing.Processors return; } - int maxX = source.Height - 1; - int maxY = source.Width - 1; + int maxX = source.Width - 1; + int maxY = source.Height - 1; Parallel.For( 0, diff --git a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs index 2330559951..ba156c6743 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs @@ -82,8 +82,8 @@ namespace SixLabors.ImageSharp.Processing.Processors return; } - int maxX = source.Height - 1; - int maxY = source.Width - 1; + int maxX = source.Width - 1; + int maxY = source.Height - 1; Parallel.For( 0, From 90f90671ccc28b00bcb902e5b5439f4d6d9ff8e2 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 22 Nov 2017 23:47:09 +1100 Subject: [PATCH 04/51] Better but still not correct --- .../Processors/Transforms/AffineProcessor.cs | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index 87f28045c8..93a97267ce 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -72,7 +72,7 @@ namespace SixLabors.ImageSharp.Processing.Processors // 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())); + source.Frames.Select(x => new ImageFrame(this.ResizeRectangle.Width, this.ResizeRectangle.Height, x.MetaData.Clone())); // Use the overload to prevent an extra frame being added return new Image(source.GetConfiguration(), source.MetaData.Clone(), frames); @@ -115,22 +115,23 @@ namespace SixLabors.ImageSharp.Processing.Processors Vector4 result = Vector4.Zero; // TODO: Fix this. - // Currently the output image is the separable values duplicated with half the transform applied - // and not the combined values as it should be. I must be sampling the wrong values. - for (int i = 0; i < xLength; i++) + // The output for skew is shrunken, offset, with right/bottom banding. + // For rotate values are offset + for (int y = 0; y < yLength; y++) { - int offsetX = xLeft + i + point.X; - offsetX = offsetX.Clamp(0, maxX); - float weight = Unsafe.Add(ref horizontalValues, i); - result += source[offsetX, point.Y].ToVector4() * weight; - } - - for (int i = 0; i < yLength; i++) - { - int offsetY = yLeft + i + point.Y; + float yweight = Unsafe.Add(ref verticalValues, y); + int offsetY = yLeft + y + point.Y; offsetY = offsetY.Clamp(0, maxY); - float weight = Unsafe.Add(ref verticalValues, i); - result += source[point.X, offsetY].ToVector4() * weight; + + for (int x = 0; x < xLength; x++) + { + float xweight = Unsafe.Add(ref horizontalValues, x); + int offsetX = xLeft + x + point.X; + offsetX = offsetX.Clamp(0, maxX); + float weight = yweight * xweight; + + result += source[offsetX, offsetY].ToVector4() * weight; + } } return result; From 37370aa089a144eb7be47dfb55e2268f02fb43d1 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 23 Nov 2017 12:11:02 +1100 Subject: [PATCH 05/51] Fix affine transforms and cleanup --- .../Processors/Transforms/AffineProcessor.cs | 32 ++++--- .../Processors/Transforms/ResizeProcessor.cs | 14 +-- .../Processors/Transforms/RotateProcessor.cs | 10 +- .../Processors/Transforms/SkewProcessor.cs | 5 +- .../Transforms/Options/ResizeOptions.cs | 2 +- .../Transforms/Resamplers/KnownResamplers.cs | 96 +++++++++++++++++++ .../Processing/Transforms/Resize.cs | 8 +- .../Processing/Transforms/Rotate.cs | 31 +++++- src/ImageSharp/Processing/Transforms/Skew.cs | 33 ++++++- .../Transforms/ResizeProfilingBenchmarks.cs | 2 +- .../Processors/Transforms/ResizeTests.cs | 37 +++---- .../Processors/Transforms/RotateTests.cs | 13 +++ .../Processors/Transforms/SkewTest.cs | 14 +++ 13 files changed, 233 insertions(+), 64 deletions(-) create mode 100644 src/ImageSharp/Processing/Transforms/Resamplers/KnownResamplers.cs diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index 93a97267ce..9fcc3f4e43 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Numerics; using System.Runtime.CompilerServices; @@ -18,15 +19,12 @@ namespace SixLabors.ImageSharp.Processing.Processors internal abstract class AffineProcessor : ResamplingWeightedProcessor where TPixel : struct, IPixel { - // TODO: Move to constants somewhere else to prevent generic type duplication. - private static readonly Rectangle DefaultRectangle = new Rectangle(0, 0, 1, 1); - /// /// Initializes a new instance of the class. /// /// The sampler to perform the resize operation. protected AffineProcessor(IResampler sampler) - : base(sampler, 1, 1, DefaultRectangle) // Hack to prevent Guard throwing in base, we always set the canvas + : base(sampler, 1, 1, Rectangles.DefaultRectangle) // Hack to prevent Guard throwing in base, we always set the canvas { } @@ -47,7 +45,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// The source rectangle. protected virtual void CreateNewCanvas(Rectangle sourceRectangle) { - if (this.ResizeRectangle == DefaultRectangle) + if (this.ResizeRectangle == Rectangles.DefaultRectangle) { if (this.Expand) { @@ -82,15 +80,14 @@ namespace SixLabors.ImageSharp.Processing.Processors /// 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) + protected Matrix3x2 GetCenteredMatrix(ImageFrame source) { var translationToTargetCenter = Matrix3x2.CreateTranslation(-this.ResizeRectangle.Width * .5F, -this.ResizeRectangle.Height * .5F); var translateToSourceCenter = Matrix3x2.CreateTranslation(source.Width * .5F, source.Height * .5F); - return (translationToTargetCenter * matrix) * translateToSourceCenter; + return (translationToTargetCenter * this.CreateProcessingMatrix()) * translateToSourceCenter; } /// @@ -108,25 +105,20 @@ namespace SixLabors.ImageSharp.Processing.Processors { ref float horizontalValues = ref windowX.GetStartReference(); ref float verticalValues = ref windowY.GetStartReference(); - int xLeft = windowX.Left; - int yLeft = windowY.Left; int xLength = windowX.Length; int yLength = windowY.Length; Vector4 result = Vector4.Zero; - // TODO: Fix this. - // The output for skew is shrunken, offset, with right/bottom banding. - // For rotate values are offset for (int y = 0; y < yLength; y++) { float yweight = Unsafe.Add(ref verticalValues, y); - int offsetY = yLeft + y + point.Y; + int offsetY = y + point.Y; offsetY = offsetY.Clamp(0, maxY); for (int x = 0; x < xLength; x++) { float xweight = Unsafe.Add(ref horizontalValues, x); - int offsetX = xLeft + x + point.X; + int offsetX = x + point.X; offsetX = offsetX.Clamp(0, maxX); float weight = yweight * xweight; @@ -137,4 +129,14 @@ namespace SixLabors.ImageSharp.Processing.Processors return result; } } + + /// + /// Contains a static rectangle used for comparison when creating a new canvas. + /// We do this so we can inherit from the resampling weights class and pass the guard in the constructor and also avoid creating a new rectangle each time. + /// + [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleType", Justification = "I'm using this only here to prevent duplication in generic types.")] + internal static class Rectangles + { + public static Rectangle DefaultRectangle { get; } = new Rectangle(0, 0, 1, 1); + } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs index a4fdb1a1b4..ecfcc7dd20 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs @@ -53,16 +53,12 @@ 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 processs 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 prevet 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); } /// diff --git a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs index 1bcd26d8d4..35ce8ce630 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs @@ -25,14 +25,14 @@ namespace SixLabors.ImageSharp.Processing.Processors /// Initializes a new instance of the class. /// public RotateProcessor() - : base(new BicubicResampler()) + : base(KnownResamplers.NearestNeighbor) { } /// /// Initializes a new instance of the class. /// - /// The sampler to perform the resize operation. + /// The sampler to perform the rotating operation. public RotateProcessor(IResampler sampler) : base(sampler) { @@ -48,7 +48,7 @@ namespace SixLabors.ImageSharp.Processing.Processors { if (this.transformMatrix == default(Matrix3x2)) { - this.transformMatrix = Matrix3x2Extensions.CreateRotationDegrees(-this.Angle, new Point(0, 0)); + this.transformMatrix = Matrix3x2Extensions.CreateRotationDegrees(-this.Angle, PointF.Empty); } return this.transformMatrix; @@ -83,7 +83,7 @@ namespace SixLabors.ImageSharp.Processing.Processors int height = this.ResizeRectangle.Height; int width = this.ResizeRectangle.Width; - Matrix3x2 matrix = this.GetCenteredMatrix(source, this.CreateProcessingMatrix()); + Matrix3x2 matrix = this.GetCenteredMatrix(source); Rectangle sourceBounds = source.Bounds(); if (this.Sampler is NearestNeighborResampler) @@ -99,7 +99,6 @@ namespace SixLabors.ImageSharp.Processing.Processors for (int x = 0; x < width; x++) { var transformedPoint = Point.Rotate(new Point(x, y), matrix); - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) { destRow[x] = source[transformedPoint.X, transformedPoint.Y]; @@ -127,6 +126,7 @@ namespace SixLabors.ImageSharp.Processing.Processors { WeightsWindow windowX = this.HorizontalWeights.Weights[transformedPoint.X]; WeightsWindow windowY = this.VerticalWeights.Weights[transformedPoint.Y]; + Vector4 dXY = this.ComputeWeightedSumAtPosition(source, maxX, maxY, ref windowX, ref windowY, ref transformedPoint); ref TPixel dest = ref destRow[x]; dest.PackFromVector4(dXY); diff --git a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs index ba156c6743..0daf6acdd8 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs @@ -44,7 +44,7 @@ namespace SixLabors.ImageSharp.Processing.Processors { if (this.transformMatrix == default(Matrix3x2)) { - this.transformMatrix = Matrix3x2Extensions.CreateSkewDegrees(-this.AngleX, -this.AngleY, new Point(0, 0)); + this.transformMatrix = Matrix3x2Extensions.CreateSkewDegrees(-this.AngleX, -this.AngleY, PointF.Empty); } return this.transformMatrix; @@ -55,7 +55,7 @@ namespace SixLabors.ImageSharp.Processing.Processors { int height = this.ResizeRectangle.Height; int width = this.ResizeRectangle.Width; - Matrix3x2 matrix = this.GetCenteredMatrix(source, this.CreateProcessingMatrix()); + Matrix3x2 matrix = this.GetCenteredMatrix(source); Rectangle sourceBounds = source.Bounds(); if (this.Sampler is NearestNeighborResampler) @@ -71,7 +71,6 @@ namespace SixLabors.ImageSharp.Processing.Processors for (int x = 0; x < width; x++) { var transformedPoint = Point.Skew(new Point(x, y), matrix); - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) { destRow[x] = source[transformedPoint.X, transformedPoint.Y]; 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..b4a9b648ab --- /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 RobidouxResampler(); + + /// + /// 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/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 436aa3cd98..2ec1385bb9 100644 --- a/src/ImageSharp/Processing/Transforms/Rotate.cs +++ b/src/ImageSharp/Processing/Transforms/Rotate.cs @@ -21,9 +21,19 @@ namespace SixLabors.ImageSharp /// The public static IImageProcessingContext Rotate(this IImageProcessingContext source, float degrees) where TPixel : struct, IPixel - { - return Rotate(source, degrees, true); - } + => Rotate(source, degrees, true); + + /// + /// Rotates an image by the given angle in degrees, expanding the image to fit the rotated result. + /// + /// The pixel format. + /// The image to rotate. + /// The angle in degrees to perform the rotation. + /// The to perform the resampling. + /// The + public static IImageProcessingContext Rotate(this IImageProcessingContext source, float degrees, IResampler sampler) + where TPixel : struct, IPixel + => Rotate(source, degrees, sampler, true); /// /// Rotates and flips an image by the given instructions. @@ -46,6 +56,19 @@ namespace SixLabors.ImageSharp /// The public static IImageProcessingContext Rotate(this IImageProcessingContext source, float degrees, bool expand) where TPixel : struct, IPixel - => source.ApplyProcessor(new RotateProcessor(new BicubicResampler()) { Angle = degrees, Expand = expand }); + => Rotate(source, degrees, KnownResamplers.NearestNeighbor, expand); + + /// + /// 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. + /// The to perform the resampling. + /// Whether to expand the image to fit the rotated result. + /// The + public static IImageProcessingContext Rotate(this IImageProcessingContext source, float degrees, IResampler sampler, bool expand) + where TPixel : struct, IPixel + => source.ApplyProcessor(new RotateProcessor(sampler) { Angle = degrees, Expand = expand }); } } diff --git a/src/ImageSharp/Processing/Transforms/Skew.cs b/src/ImageSharp/Processing/Transforms/Skew.cs index 873dbe5100..f03e60e3da 100644 --- a/src/ImageSharp/Processing/Transforms/Skew.cs +++ b/src/ImageSharp/Processing/Transforms/Skew.cs @@ -22,9 +22,20 @@ namespace SixLabors.ImageSharp /// 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, true); + + /// + /// Skews an image by the given angles in degrees using the given sampler, expanding the image to fit the skewed result. + /// + /// 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 to perform the resampling. + /// The + public static IImageProcessingContext Skew(this IImageProcessingContext source, float degreesX, float degreesY, IResampler sampler) + where TPixel : struct, IPixel + => Skew(source, degreesX, degreesY, sampler, true); /// /// Skews an image by the given angles in degrees. @@ -37,6 +48,20 @@ namespace SixLabors.ImageSharp /// The public static IImageProcessingContext Skew(this IImageProcessingContext source, float degreesX, float degreesY, bool expand) where TPixel : struct, IPixel - => source.ApplyProcessor(new SkewProcessor(new BicubicResampler()) { AngleX = degreesX, AngleY = degreesY, Expand = expand }); + => Skew(source, degreesX, degreesY, KnownResamplers.NearestNeighbor, expand); + + /// + /// 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. + /// The to perform the resampling. + /// Whether to expand the image to fit the skewed result. + /// The + public static IImageProcessingContext Skew(this IImageProcessingContext source, float degreesX, float degreesY, IResampler sampler, bool expand) + where TPixel : struct, IPixel + => source.ApplyProcessor(new SkewProcessor(sampler) { AngleX = degreesX, AngleY = degreesY, Expand = expand }); } } diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeProfilingBenchmarks.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeProfilingBenchmarks.cs index 963b849d2a..6dd6369802 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeProfilingBenchmarks.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeProfilingBenchmarks.cs @@ -38,7 +38,7 @@ 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); diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs index a5e21b8ef3..662d03d9c8 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 }, + { "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..49fe8952be 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs @@ -39,6 +39,19 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms } } + [Theory] + [WithTestPatternImages(nameof(RotateFloatValues), 100, 50, DefaultPixelType)] + [WithTestPatternImages(nameof(RotateFloatValues), 50, 100, DefaultPixelType)] + public void RotateWithSampler(TestImageProvider provider, float value) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage()) + { + image.Mutate(x => x.Rotate(value, KnownResamplers.Triangle)); + image.DebugSave(provider, string.Join("_", value, "triangle")); + } + } + [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..6e0d651496 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/SkewTest.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/SkewTest.cs @@ -6,6 +6,8 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms { + using SixLabors.ImageSharp.Processing; + public class SkewTest : FileTestBase { public static readonly TheoryData SkewValues @@ -26,5 +28,17 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms image.DebugSave(provider, string.Join("_", x, y)); } } + + [Theory] + [WithFileCollection(nameof(DefaultFiles), nameof(SkewValues), DefaultPixelType)] + public void ImageShouldSkewWithSampler(TestImageProvider provider, float x, float y) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage()) + { + image.Mutate(i => i.Skew(x, y, KnownResamplers.Triangle)); + image.DebugSave(provider, string.Join("_", x, y, "triangle")); + } + } } } \ No newline at end of file From ea69d7152e71055897756f7300baf53ba8a7b447 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 23 Nov 2017 12:25:51 +1100 Subject: [PATCH 06/51] Fix KnownResamplers --- .../Processing/Transforms/Resamplers/KnownResamplers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/KnownResamplers.cs b/src/ImageSharp/Processing/Transforms/Resamplers/KnownResamplers.cs index b4a9b648ab..2da98497b5 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/KnownResamplers.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/KnownResamplers.cs @@ -75,7 +75,7 @@ namespace SixLabors.ImageSharp.Processing /// /// Gets the Robidoux Sharp sampler. A sharpend form of the sampler /// - public static IResampler RobidouxSharp { get; } = new RobidouxResampler(); + public static IResampler RobidouxSharp { get; } = new RobidouxSharpResampler(); /// /// Gets the Spline sampler. A seperable cubic algorithm similar to but yielding smoother results. From a9b4e3b27a35696928861adc9568b44d14dc066a Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 23 Nov 2017 12:47:25 +1100 Subject: [PATCH 07/51] Disable Lanczos2 for now, we need test image --- .../Processing/Processors/Transforms/ResizeTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs index 662d03d9c8..92f1af58ed 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs @@ -24,7 +24,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms { "Triangle", KnownResamplers.Triangle}, { "NearestNeighbor", KnownResamplers.NearestNeighbor }, { "Box", KnownResamplers.Box }, - { "Lanczos2", KnownResamplers.Lanczos2 }, + // { "Lanczos2", KnownResamplers.Lanczos2 }, TODO: Add expected file { "Lanczos3", KnownResamplers.Lanczos3 }, { "Lanczos5", KnownResamplers.Lanczos5 }, { "MitchellNetravali", KnownResamplers.MitchellNetravali }, From b8f0432499724a67a4ff63959dffbd723e9ae315 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 23 Nov 2017 13:08:02 +1100 Subject: [PATCH 08/51] Bump Travis dotnet build https://github.com/travis-ci/travis-ci/issues/8793 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index a4f68b1d1b..70501a484b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ matrix: - os: linux # Ubuntu 14.04 dist: trusty sudo: required - dotnet: 1.0.1 + dotnet: 1.0.4 mono: latest # - os: osx # OSX 10.11 # osx_image: xcode7.3.1 From 4b56f23c7caeeb955e100cad38c964329dd4c502 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Fri, 24 Nov 2017 01:42:56 +0100 Subject: [PATCH 09/51] testing with all resamplers --- .../Processors/Transforms/RotateTests.cs | 46 +++++++++++++++++-- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs index 49fe8952be..c30770ada9 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs @@ -8,6 +8,9 @@ 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 @@ -26,6 +29,25 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms RotateType.Rotate270 }; + public static readonly TheoryData ResmplerNames = new TheoryData + { + nameof(KnownResamplers.Bicubic), + nameof(KnownResamplers.Box), + nameof(KnownResamplers.CatmullRom), + nameof(KnownResamplers.Hermite), + nameof(KnownResamplers.Lanczos2), + nameof(KnownResamplers.Lanczos3), + nameof(KnownResamplers.Lanczos5), + nameof(KnownResamplers.Lanczos8), + nameof(KnownResamplers.MitchellNetravali), + nameof(KnownResamplers.NearestNeighbor), + nameof(KnownResamplers.Robidoux), + nameof(KnownResamplers.RobidouxSharp), + nameof(KnownResamplers.Spline), + nameof(KnownResamplers.Triangle), + nameof(KnownResamplers.Welch), + }; + [Theory] [WithTestPatternImages(nameof(RotateFloatValues), 100, 50, DefaultPixelType)] [WithTestPatternImages(nameof(RotateFloatValues), 50, 100, DefaultPixelType)] @@ -40,15 +62,17 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms } [Theory] - [WithTestPatternImages(nameof(RotateFloatValues), 100, 50, DefaultPixelType)] - [WithTestPatternImages(nameof(RotateFloatValues), 50, 100, DefaultPixelType)] - public void RotateWithSampler(TestImageProvider provider, float value) + [WithTestPatternImages(nameof(ResmplerNames), 100, 50, DefaultPixelType)] + [WithTestPatternImages(nameof(ResmplerNames), 50, 100, DefaultPixelType)] + public void RotateWithSampler(TestImageProvider provider, string resamplerName) where TPixel : struct, IPixel { + IResampler resampler = GetResampler(resamplerName); + using (Image image = provider.GetImage()) { - image.Mutate(x => x.Rotate(value, KnownResamplers.Triangle)); - image.DebugSave(provider, string.Join("_", value, "triangle")); + image.Mutate(x => x.Rotate(50, resampler)); + image.DebugSave(provider, resamplerName); } } @@ -64,5 +88,17 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms image.DebugSave(provider, value); } } + + 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 From bce9d38fe231ecc71e25f40bf117edced81fa830 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 24 Nov 2017 15:59:14 +1100 Subject: [PATCH 10/51] Fix sampling offset --- .../Processors/Transforms/AffineProcessor.cs | 51 ++++++++++++++----- .../Processors/Transforms/RotateProcessor.cs | 3 +- .../Processors/Transforms/SkewProcessor.cs | 3 +- .../Processors/Transforms/RotateTests.cs | 43 ++++++++-------- .../Processors/Transforms/SkewTest.cs | 48 +++++++++++++++-- 5 files changed, 108 insertions(+), 40 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index 9fcc3f4e43..4db2aacbac 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -29,7 +29,7 @@ namespace SixLabors.ImageSharp.Processing.Processors } /// - /// Gets or sets a value indicating whether to expand the canvas to fit the skewed image. + /// Gets or sets a value indicating whether to expand the canvas to fit the transformed image. /// public bool Expand { get; set; } = true; @@ -96,33 +96,58 @@ namespace SixLabors.ImageSharp.Processing.Processors /// The source image /// The maximum x value /// The maximum y value + /// The radius of the current sampling window /// The horizontal weights /// The vertical weights /// The transformed position /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - protected Vector4 ComputeWeightedSumAtPosition(ImageFrame source, int maxX, int maxY, ref WeightsWindow windowX, ref WeightsWindow windowY, ref Point point) + protected Vector4 ComputeWeightedSumAtPosition(ImageFrame source, int maxX, int maxY, int radius, ref WeightsWindow windowX, ref WeightsWindow windowY, ref Point point) { ref float horizontalValues = ref windowX.GetStartReference(); ref float verticalValues = ref windowY.GetStartReference(); - int xLength = windowX.Length; - int yLength = windowY.Length; + + int left = point.X - radius; + int right = point.X + radius; + int top = point.Y - radius; + int bottom = point.Y + radius; + + // Faster than clamping + we know we are only looking in one direction + if (left < 0) + { + left = 0; + } + + if (top < 0) + { + top = 0; + } + + if (right > maxX) + { + right = maxX; + } + + if (bottom > maxY) + { + bottom = maxY; + } + Vector4 result = Vector4.Zero; - for (int y = 0; y < yLength; y++) + // We calculate our sample by iterating up-down/left-right from our transformed point. + // Ignoring the weight of outlying pixels is better for shape preservation on transforms such as skew with samplers that use larger radii. + // We don't offset our window index so that the weight compensates for the missing values + for (int y = top, yl = 0; y <= bottom; y++, yl++) { - float yweight = Unsafe.Add(ref verticalValues, y); - int offsetY = y + point.Y; - offsetY = offsetY.Clamp(0, maxY); + float yweight = Unsafe.Add(ref verticalValues, yl); - for (int x = 0; x < xLength; x++) + for (int x = left, xl = 0; x <= right; x++, xl++) { - float xweight = Unsafe.Add(ref horizontalValues, x); - int offsetX = x + point.X; - offsetX = offsetX.Clamp(0, maxX); + float xweight = Unsafe.Add(ref horizontalValues, xl); float weight = yweight * xweight; - result += source[offsetX, offsetY].ToVector4() * weight; + result += source[x, y].ToVector4() * weight; } } diff --git a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs index 35ce8ce630..a1b181e69f 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs @@ -111,6 +111,7 @@ namespace SixLabors.ImageSharp.Processing.Processors int maxX = source.Width - 1; int maxY = source.Height - 1; + int radius = Math.Max((int)this.Sampler.Radius, 1); Parallel.For( 0, @@ -127,7 +128,7 @@ namespace SixLabors.ImageSharp.Processing.Processors WeightsWindow windowX = this.HorizontalWeights.Weights[transformedPoint.X]; WeightsWindow windowY = this.VerticalWeights.Weights[transformedPoint.Y]; - Vector4 dXY = this.ComputeWeightedSumAtPosition(source, maxX, maxY, ref windowX, ref windowY, ref transformedPoint); + Vector4 dXY = this.ComputeWeightedSumAtPosition(source, maxX, maxY, radius, ref windowX, ref windowY, ref transformedPoint); ref TPixel dest = ref destRow[x]; dest.PackFromVector4(dXY); } diff --git a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs index 0daf6acdd8..0f67f724ca 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs @@ -83,6 +83,7 @@ namespace SixLabors.ImageSharp.Processing.Processors int maxX = source.Width - 1; int maxY = source.Height - 1; + int radius = Math.Max((int)this.Sampler.Radius, 1); Parallel.For( 0, @@ -99,7 +100,7 @@ namespace SixLabors.ImageSharp.Processing.Processors WeightsWindow windowX = this.HorizontalWeights.Weights[transformedPoint.X]; WeightsWindow windowY = this.VerticalWeights.Weights[transformedPoint.Y]; - Vector4 dXY = this.ComputeWeightedSumAtPosition(source, maxX, maxY, ref windowX, ref windowY, ref transformedPoint); + Vector4 dXY = this.ComputeWeightedSumAtPosition(source, maxX, maxY, radius, ref windowX, ref windowY, ref transformedPoint); ref TPixel dest = ref destRow[x]; dest.PackFromVector4(dXY); } diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs index c30770ada9..ca29009bc7 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs @@ -29,24 +29,25 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms RotateType.Rotate270 }; - public static readonly TheoryData ResmplerNames = new TheoryData - { - nameof(KnownResamplers.Bicubic), - nameof(KnownResamplers.Box), - nameof(KnownResamplers.CatmullRom), - nameof(KnownResamplers.Hermite), - nameof(KnownResamplers.Lanczos2), - nameof(KnownResamplers.Lanczos3), - nameof(KnownResamplers.Lanczos5), - nameof(KnownResamplers.Lanczos8), - nameof(KnownResamplers.MitchellNetravali), - nameof(KnownResamplers.NearestNeighbor), - nameof(KnownResamplers.Robidoux), - nameof(KnownResamplers.RobidouxSharp), - nameof(KnownResamplers.Spline), - nameof(KnownResamplers.Triangle), - nameof(KnownResamplers.Welch), - }; + public static readonly TheoryData ResamplerNames + = new TheoryData + { + nameof(KnownResamplers.Bicubic), + nameof(KnownResamplers.Box), + nameof(KnownResamplers.CatmullRom), + nameof(KnownResamplers.Hermite), + nameof(KnownResamplers.Lanczos2), + nameof(KnownResamplers.Lanczos3), + nameof(KnownResamplers.Lanczos5), + nameof(KnownResamplers.Lanczos8), + nameof(KnownResamplers.MitchellNetravali), + nameof(KnownResamplers.NearestNeighbor), + nameof(KnownResamplers.Robidoux), + nameof(KnownResamplers.RobidouxSharp), + nameof(KnownResamplers.Spline), + nameof(KnownResamplers.Triangle), + nameof(KnownResamplers.Welch), + }; [Theory] [WithTestPatternImages(nameof(RotateFloatValues), 100, 50, DefaultPixelType)] @@ -62,8 +63,8 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms } [Theory] - [WithTestPatternImages(nameof(ResmplerNames), 100, 50, DefaultPixelType)] - [WithTestPatternImages(nameof(ResmplerNames), 50, 100, DefaultPixelType)] + [WithTestPatternImages(nameof(ResamplerNames), 100, 50, DefaultPixelType)] + [WithTestPatternImages(nameof(ResamplerNames), 50, 100, DefaultPixelType)] public void RotateWithSampler(TestImageProvider provider, string resamplerName) where TPixel : struct, IPixel { @@ -98,7 +99,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms throw new Exception("Invalid property name!"); } - return (IResampler) property.GetValue(null); + return (IResampler)property.GetValue(null); } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/SkewTest.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/SkewTest.cs index 6e0d651496..8e57327be0 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/SkewTest.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/SkewTest.cs @@ -6,17 +6,41 @@ 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)] public void ImageShouldSkew(TestImageProvider provider, float x, float y) @@ -34,11 +58,27 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms public void ImageShouldSkewWithSampler(TestImageProvider provider, float x, float y) where TPixel : struct, IPixel { - using (Image image = provider.GetImage()) + 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) { - image.Mutate(i => i.Skew(x, y, KnownResamplers.Triangle)); - image.DebugSave(provider, string.Join("_", x, y, "triangle")); + throw new Exception("Invalid property name!"); } + + return (IResampler)property.GetValue(null); } } } \ No newline at end of file From 2f4e06cae870f894162b4567080f2ce888240896 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 26 Nov 2017 02:04:28 +1100 Subject: [PATCH 11/51] Update dependencies, remove code duplication --- .../ImageSharp.Drawing.csproj | 6 +- src/ImageSharp/ImageSharp.csproj | 2 +- .../Processors/Transforms/AffineProcessor.cs | 61 ++++++++++++++++++ .../Processors/Transforms/RotateProcessor.cs | 54 +--------------- .../Processors/Transforms/SkewProcessor.cs | 62 ------------------- 5 files changed, 66 insertions(+), 119 deletions(-) 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/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index 8c22237cf7..f1bcb2ecda 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -34,7 +34,7 @@ - + All diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index 4db2aacbac..cffe6674d6 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -1,12 +1,15 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Numerics; using System.Runtime.CompilerServices; +using System.Threading.Tasks; using SixLabors.ImageSharp.Advanced; +using SixLabors.ImageSharp.Helpers; using SixLabors.ImageSharp.PixelFormats; using SixLabors.Primitives; @@ -76,6 +79,64 @@ namespace SixLabors.ImageSharp.Processing.Processors return new Image(source.GetConfiguration(), source.MetaData.Clone(), frames); } + /// + protected override void OnApply(ImageFrame source, ImageFrame destination, Rectangle sourceRectangle, Configuration configuration) + { + int height = this.ResizeRectangle.Height; + int width = this.ResizeRectangle.Width; + Matrix3x2 matrix = this.GetCenteredMatrix(source); + Rectangle sourceBounds = source.Bounds(); + + 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 transformedPoint = Point.Transform(new Point(x, y), matrix); + if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) + { + destRow[x] = source[transformedPoint.X, transformedPoint.Y]; + } + } + }); + + return; + } + + int maxX = source.Width - 1; + int maxY = source.Height - 1; + int radius = Math.Max((int)this.Sampler.Radius, 1); + + Parallel.For( + 0, + height, + configuration.ParallelOptions, + y => + { + Span destRow = destination.GetPixelRowSpan(y); + for (int x = 0; x < width; x++) + { + var transformedPoint = Point.Transform(new Point(x, y), matrix); + if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) + { + WeightsWindow windowX = this.HorizontalWeights.Weights[transformedPoint.X]; + WeightsWindow windowY = this.VerticalWeights.Weights[transformedPoint.Y]; + + Vector4 dXY = this.ComputeWeightedSumAtPosition(source, maxX, maxY, radius, ref windowX, ref windowY, ref transformedPoint); + ref TPixel dest = ref destRow[x]; + dest.PackFromVector4(dXY); + } + } + }); + } + /// /// Gets a transform matrix adjusted to center upon the target image bounds. /// diff --git a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs index a1b181e69f..eb6110fa14 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs @@ -81,59 +81,7 @@ namespace SixLabors.ImageSharp.Processing.Processors return; } - int height = this.ResizeRectangle.Height; - int width = this.ResizeRectangle.Width; - Matrix3x2 matrix = this.GetCenteredMatrix(source); - Rectangle sourceBounds = source.Bounds(); - - 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 transformedPoint = Point.Rotate(new Point(x, y), matrix); - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) - { - destRow[x] = source[transformedPoint.X, transformedPoint.Y]; - } - } - }); - - return; - } - - int maxX = source.Width - 1; - int maxY = source.Height - 1; - int radius = Math.Max((int)this.Sampler.Radius, 1); - - Parallel.For( - 0, - height, - configuration.ParallelOptions, - y => - { - Span destRow = destination.GetPixelRowSpan(y); - for (int x = 0; x < width; x++) - { - var transformedPoint = Point.Rotate(new Point(x, y), matrix); - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) - { - WeightsWindow windowX = this.HorizontalWeights.Weights[transformedPoint.X]; - WeightsWindow windowY = this.VerticalWeights.Weights[transformedPoint.Y]; - - Vector4 dXY = this.ComputeWeightedSumAtPosition(source, maxX, maxY, radius, ref windowX, ref windowY, ref transformedPoint); - ref TPixel dest = ref destRow[x]; - dest.PackFromVector4(dXY); - } - } - }); + base.OnApply(source, destination, sourceRectangle, configuration); } /// diff --git a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs index 0f67f724ca..fc70330e81 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs @@ -1,11 +1,7 @@ // 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.PixelFormats; using SixLabors.Primitives; @@ -49,63 +45,5 @@ namespace SixLabors.ImageSharp.Processing.Processors return this.transformMatrix; } - - /// - protected override void OnApply(ImageFrame source, ImageFrame destination, Rectangle sourceRectangle, Configuration configuration) - { - int height = this.ResizeRectangle.Height; - int width = this.ResizeRectangle.Width; - Matrix3x2 matrix = this.GetCenteredMatrix(source); - Rectangle sourceBounds = source.Bounds(); - - 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 transformedPoint = Point.Skew(new Point(x, y), matrix); - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) - { - destRow[x] = source[transformedPoint.X, transformedPoint.Y]; - } - } - }); - - return; - } - - int maxX = source.Width - 1; - int maxY = source.Height - 1; - int radius = Math.Max((int)this.Sampler.Radius, 1); - - Parallel.For( - 0, - height, - configuration.ParallelOptions, - y => - { - Span destRow = destination.GetPixelRowSpan(y); - for (int x = 0; x < width; x++) - { - var transformedPoint = Point.Skew(new Point(x, y), matrix); - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) - { - WeightsWindow windowX = this.HorizontalWeights.Weights[transformedPoint.X]; - WeightsWindow windowY = this.VerticalWeights.Weights[transformedPoint.Y]; - - Vector4 dXY = this.ComputeWeightedSumAtPosition(source, maxX, maxY, radius, ref windowX, ref windowY, ref transformedPoint); - ref TPixel dest = ref destRow[x]; - dest.PackFromVector4(dXY); - } - } - }); - } } } \ No newline at end of file From 7abcaa20247d08dc233371ab62c2ea6725b902d7 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 26 Nov 2017 02:22:28 +1100 Subject: [PATCH 12/51] Add missing myget source --- NuGet.config | 1 + 1 file changed, 1 insertion(+) diff --git a/NuGet.config b/NuGet.config index b2c967cc97..5c234e5ba6 100644 --- a/NuGet.config +++ b/NuGet.config @@ -1,6 +1,7 @@  + From 0c98e5b3337097c8271839ab87f4476ceb76f2a4 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 26 Nov 2017 02:24:19 +1100 Subject: [PATCH 13/51] Fix nuget.config, Thanks VSCode!! --- NuGet.config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NuGet.config b/NuGet.config index 5c234e5ba6..322105d4d4 100644 --- a/NuGet.config +++ b/NuGet.config @@ -1,7 +1,7 @@  - + From 5201a2238c174768780413b04ba4a208627f7dda Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 27 Nov 2017 19:37:06 +1100 Subject: [PATCH 14/51] Original maths was totally incorrect This, however, is no better if not, worse. --- src/ImageSharp/Common/Helpers/ImageMaths.cs | 2 +- .../Processors/Transforms/AffineProcessor.cs | 93 ++++++++++++------- .../Processors/Transforms/RotateProcessor.cs | 33 +++---- .../Processors/Transforms/SkewProcessor.cs | 2 +- 4 files changed, 71 insertions(+), 59 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/ImageMaths.cs b/src/ImageSharp/Common/Helpers/ImageMaths.cs index 7d781e77fe..fa60f855ee 100644 --- a/src/ImageSharp/Common/Helpers/ImageMaths.cs +++ b/src/ImageSharp/Common/Helpers/ImageMaths.cs @@ -157,7 +157,7 @@ namespace SixLabors.ImageSharp 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); + return new Rectangle(0, 0, (int)MathF.Ceiling(extentX), (int)MathF.Ceiling(extentY)); } /// diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index cffe6674d6..cc48cd91a3 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -39,8 +39,9 @@ namespace SixLabors.ImageSharp.Processing.Processors /// /// Returns the processing matrix used for transforming the image. /// + /// The rectangle bounds /// The - protected abstract Matrix3x2 CreateProcessingMatrix(); + protected abstract Matrix3x2 CreateProcessingMatrix(Rectangle rectangle); /// /// Creates a new target canvas to contain the results of the matrix transform. @@ -48,19 +49,9 @@ namespace SixLabors.ImageSharp.Processing.Processors /// The source rectangle. protected virtual void CreateNewCanvas(Rectangle sourceRectangle) { - if (this.ResizeRectangle == Rectangles.DefaultRectangle) - { - if (this.Expand) - { - this.ResizeRectangle = Matrix3x2.Invert(this.CreateProcessingMatrix(), out Matrix3x2 sizeMatrix) - ? ImageMaths.GetBoundingRectangle(sourceRectangle, sizeMatrix) - : sourceRectangle; - } - else - { - this.ResizeRectangle = sourceRectangle; - } - } + this.ResizeRectangle = Matrix3x2.Invert(this.CreateProcessingMatrix(sourceRectangle), out Matrix3x2 sizeMatrix) + ? ImageMaths.GetBoundingRectangle(sourceRectangle, sizeMatrix) + : sourceRectangle; this.Width = this.ResizeRectangle.Width; this.Height = this.ResizeRectangle.Height; @@ -84,8 +75,8 @@ namespace SixLabors.ImageSharp.Processing.Processors { int height = this.ResizeRectangle.Height; int width = this.ResizeRectangle.Width; - Matrix3x2 matrix = this.GetCenteredMatrix(source); Rectangle sourceBounds = source.Bounds(); + Matrix3x2 matrix = this.GetCenteredMatrix(source); if (this.Sampler is NearestNeighborResampler) { @@ -112,24 +103,24 @@ namespace SixLabors.ImageSharp.Processing.Processors int maxX = source.Width - 1; int maxY = source.Height - 1; - int radius = Math.Max((int)this.Sampler.Radius, 1); Parallel.For( 0, height, - configuration.ParallelOptions, + new ParallelOptions { MaxDegreeOfParallelism = 1 }, y => { Span destRow = destination.GetPixelRowSpan(y); for (int x = 0; x < width; x++) { var transformedPoint = Point.Transform(new Point(x, y), matrix); + if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) { WeightsWindow windowX = this.HorizontalWeights.Weights[transformedPoint.X]; WeightsWindow windowY = this.VerticalWeights.Weights[transformedPoint.Y]; - Vector4 dXY = this.ComputeWeightedSumAtPosition(source, maxX, maxY, radius, ref windowX, ref windowY, ref transformedPoint); + Vector4 dXY = this.ComputeWeightedSumAtPosition(source, maxX, maxY, ref windowX, ref windowY, ref transformedPoint); ref TPixel dest = ref destRow[x]; dest.PackFromVector4(dXY); } @@ -148,7 +139,7 @@ namespace SixLabors.ImageSharp.Processing.Processors { var translationToTargetCenter = Matrix3x2.CreateTranslation(-this.ResizeRectangle.Width * .5F, -this.ResizeRectangle.Height * .5F); var translateToSourceCenter = Matrix3x2.CreateTranslation(source.Width * .5F, source.Height * .5F); - return (translationToTargetCenter * this.CreateProcessingMatrix()) * translateToSourceCenter; + return (translationToTargetCenter * this.CreateProcessingMatrix(this.ResizeRectangle)) * translateToSourceCenter; } /// @@ -157,55 +148,87 @@ namespace SixLabors.ImageSharp.Processing.Processors /// The source image /// The maximum x value /// The maximum y value - /// The radius of the current sampling window /// The horizontal weights /// The vertical weights /// The transformed position /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - protected Vector4 ComputeWeightedSumAtPosition(ImageFrame source, int maxX, int maxY, int radius, ref WeightsWindow windowX, ref WeightsWindow windowY, ref Point point) + protected Vector4 ComputeWeightedSumAtPosition( + ImageFrame source, + int maxX, + int maxY, + ref WeightsWindow windowX, + ref WeightsWindow windowY, + ref Point point) { + // What, in theory, is supposed to happen here is the following... + // + // We identify the maximum possible pixel offsets allowable by the current sampler + // clamping values to ensure that we do not go outwith the bounds of our image. + // + // Then we get the weights of that offset value from our pre-calculated vaues. + // First we grab the weight on the y-axis, then the x-axis and then we multiply + // them together to get the final weight. + // + // Unfortunately this simply does not seem to work! + // The output is rubbish and I cannot see why :( ref float horizontalValues = ref windowX.GetStartReference(); ref float verticalValues = ref windowY.GetStartReference(); - int left = point.X - radius; - int right = point.X + radius; - int top = point.Y - radius; - int bottom = point.Y + radius; + int yLength = windowY.Length; + int xLength = windowX.Length; + int yRadius = (int)MathF.Ceiling((yLength - 1) * .5F); + int xRadius = (int)MathF.Ceiling((xLength - 1) * .5F); + + int left = point.X - xRadius; + int right = point.X + xRadius; + int top = point.Y - yRadius; + int bottom = point.Y + yRadius; + + int yIndex = 0; + int xIndex = 0; // Faster than clamping + we know we are only looking in one direction if (left < 0) { + // Trim the length of our weights iterator across the x-axis. + // Offset our start index across the x-axis. + xIndex = ImageMaths.FastAbs(left); + xLength -= xIndex; left = 0; } if (top < 0) { + // Trim the length of our weights iterator across the y-axis. + // Offset our start index across the y-axis. + yIndex = ImageMaths.FastAbs(top); + yLength -= yIndex; top = 0; } - if (right > maxX) + if (right >= maxX) { - right = maxX; + // Trim the length of our weights iterator across the x-axis. + xLength -= right - maxX; } - if (bottom > maxY) + if (bottom >= maxY) { - bottom = maxY; + // Trim the length of our weights iterator across the y-axis. + yLength -= bottom - maxY; } Vector4 result = Vector4.Zero; // We calculate our sample by iterating up-down/left-right from our transformed point. - // Ignoring the weight of outlying pixels is better for shape preservation on transforms such as skew with samplers that use larger radii. - // We don't offset our window index so that the weight compensates for the missing values - for (int y = top, yl = 0; y <= bottom; y++, yl++) + for (int y = top, yi = yIndex; yi < yLength; y++, yi++) { - float yweight = Unsafe.Add(ref verticalValues, yl); + float yweight = Unsafe.Add(ref verticalValues, yi); - for (int x = left, xl = 0; x <= right; x++, xl++) + for (int x = left, xi = xIndex; xi < xLength; x++, xi++) { - float xweight = Unsafe.Add(ref horizontalValues, xl); + float xweight = Unsafe.Add(ref horizontalValues, xi); float weight = yweight * xweight; result += source[x, y].ToVector4() * weight; diff --git a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs index eb6110fa14..1ad7263929 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs @@ -44,7 +44,7 @@ namespace SixLabors.ImageSharp.Processing.Processors public float Angle { get; set; } /// - protected override Matrix3x2 CreateProcessingMatrix() + protected override Matrix3x2 CreateProcessingMatrix(Rectangle rectangle) { if (this.transformMatrix == default(Matrix3x2)) { @@ -54,25 +54,6 @@ namespace SixLabors.ImageSharp.Processing.Processors return this.transformMatrix; } - /// - protected override void CreateNewCanvas(Rectangle sourceRectangle) - { - if (MathF.Abs(this.Angle) < Constants.Epsilon || - MathF.Abs(this.Angle - 180) < Constants.Epsilon) - { - this.ResizeRectangle = sourceRectangle; - } - - if (MathF.Abs(this.Angle - 90) < Constants.Epsilon || - MathF.Abs(this.Angle - 270) < Constants.Epsilon) - { - // We always expand enumerated rectangle values - this.ResizeRectangle = new Rectangle(0, 0, sourceRectangle.Height, sourceRectangle.Width); - } - - base.CreateNewCanvas(sourceRectangle); - } - /// protected override void OnApply(ImageFrame source, ImageFrame destination, Rectangle sourceRectangle, Configuration configuration) { @@ -157,6 +138,7 @@ namespace SixLabors.ImageSharp.Processing.Processors { int width = source.Width; int height = source.Height; + Rectangle destinationBounds = destination.Bounds(); Parallel.For( 0, @@ -171,7 +153,10 @@ namespace SixLabors.ImageSharp.Processing.Processors newX = height - newX - 1; int newY = width - x - 1; - destination[newX, newY] = sourceRow[x]; + if (destinationBounds.Contains(newX, newY)) + { + destination[newX, newY] = sourceRow[x]; + } } }); } @@ -213,6 +198,7 @@ namespace SixLabors.ImageSharp.Processing.Processors { int width = source.Width; int height = source.Height; + Rectangle destinationBounds = destination.Bounds(); Parallel.For( 0, @@ -224,7 +210,10 @@ namespace SixLabors.ImageSharp.Processing.Processors int newX = height - y - 1; for (int x = 0; x < width; x++) { - destination[newX, x] = sourceRow[x]; + if (destinationBounds.Contains(newX, x)) + { + destination[newX, x] = sourceRow[x]; + } } }); } diff --git a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs index fc70330e81..61526747ea 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs @@ -36,7 +36,7 @@ namespace SixLabors.ImageSharp.Processing.Processors public float AngleY { get; set; } /// - protected override Matrix3x2 CreateProcessingMatrix() + protected override Matrix3x2 CreateProcessingMatrix(Rectangle rectangle) { if (this.transformMatrix == default(Matrix3x2)) { From 069422297ba09a6c23a6f087063d1eb8d9663c51 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 1 Dec 2017 18:27:59 +1100 Subject: [PATCH 15/51] It works!!!! Now we need to refactor and optimize this. --- .../Processors/Transforms/AffineProcessor.cs | 154 ++++++------------ .../Transforms/Resamplers/BicubicResampler.cs | 3 + .../Transforms/Resamplers/BoxResampler.cs | 3 + .../Resamplers/CatmullRomResampler.cs | 3 + .../Transforms/Resamplers/HermiteResampler.cs | 3 + .../Resamplers/Lanczos2Resampler.cs | 3 + .../Resamplers/Lanczos3Resampler.cs | 3 + .../Resamplers/Lanczos5Resampler.cs | 3 + .../Resamplers/Lanczos8Resampler.cs | 3 + .../Resamplers/MitchellNetravaliResampler.cs | 3 + .../Resamplers/NearestNeighborResampler.cs | 3 + .../Resamplers/RobidouxSharpResampler.cs | 3 + .../Transforms/Resamplers/SplineResampler.cs | 3 + .../Resamplers/TriangleResampler.cs | 3 + .../Transforms/Resamplers/WelchResampler.cs | 5 +- 15 files changed, 96 insertions(+), 102 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index cc48cd91a3..cea7f5953a 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -90,10 +90,10 @@ namespace SixLabors.ImageSharp.Processing.Processors for (int x = 0; x < width; x++) { - var transformedPoint = Point.Transform(new Point(x, y), matrix); - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) + var point = Point.Transform(new Point(x, y), matrix); + if (sourceBounds.Contains(point.X, point.Y)) { - destRow[x] = source[transformedPoint.X, transformedPoint.Y]; + destRow[x] = source[point.X, point.Y]; } } }); @@ -101,29 +101,58 @@ namespace SixLabors.ImageSharp.Processing.Processors return; } - int maxX = source.Width - 1; - int maxY = source.Height - 1; + int maxSourceX = source.Width - 1; + int maxSourceY = source.Height - 1; + (float radius, float scale) xRadiusScale = this.GetSamplingRadius(source.Width, destination.Width); + (float radius, float scale) yRadiusScale = this.GetSamplingRadius(source.Height, destination.Height); + float xScale = xRadiusScale.scale; + float yScale = yRadiusScale.scale; + float xRadius = xRadiusScale.radius; + float yRadius = yRadiusScale.radius; Parallel.For( 0, height, - new ParallelOptions { MaxDegreeOfParallelism = 1 }, + configuration.ParallelOptions, y => { Span destRow = destination.GetPixelRowSpan(y); for (int x = 0; x < width; x++) { - var transformedPoint = Point.Transform(new Point(x, y), matrix); - - if (sourceBounds.Contains(transformedPoint.X, transformedPoint.Y)) + // Use the single precision position to calculate correct bounding pixels + // otherwise we get rogue pixels outside of the bounds. + var point = PointF.Transform(new PointF(x, y), matrix); + int maxX = (int)MathF.Ceiling(point.X + xRadius); + int maxY = (int)MathF.Ceiling(point.Y + yRadius); + int minX = (int)MathF.Floor(point.X - xRadius); + int minY = (int)MathF.Floor(point.Y - yRadius); + + // Clamp sampling pixels to the source image edge + maxX = maxX.Clamp(0, maxSourceX); + minX = minX.Clamp(0, maxSourceX); + maxY = maxY.Clamp(0, maxSourceY); + minY = minY.Clamp(0, maxSourceY); + + if (minX == maxX || minY == maxY) { - WeightsWindow windowX = this.HorizontalWeights.Weights[transformedPoint.X]; - WeightsWindow windowY = this.VerticalWeights.Weights[transformedPoint.Y]; + continue; + } - Vector4 dXY = this.ComputeWeightedSumAtPosition(source, maxX, maxY, ref windowX, ref windowY, ref transformedPoint); - ref TPixel dest = ref destRow[x]; - dest.PackFromVector4(dXY); + // It appears these have to be calculated manually. + // Using the precalculated weights give the wrong values. + // TODO: Find a way to speed this up. + Vector4 sum = Vector4.Zero; + for (int i = minX; i <= maxX; i++) + { + float weightX = this.Sampler.GetValue((i - point.X) / xScale); + for (int j = minY; j <= maxY; j++) + { + float weightY = this.Sampler.GetValue((j - point.Y) / yScale); + sum += source[i, j].ToVector4() * weightX * weightY; + } } + ref TPixel dest = ref destRow[x]; + dest.PackFromVector4(sum); } }); } @@ -143,99 +172,22 @@ namespace SixLabors.ImageSharp.Processing.Processors } /// - /// Computes the weighted sum at the given XY position + /// Calculates the sampling radius for the current sampler /// - /// The source image - /// The maximum x value - /// The maximum y value - /// The horizontal weights - /// The vertical weights - /// The transformed position - /// The - [MethodImpl(MethodImplOptions.AggressiveInlining)] - protected Vector4 ComputeWeightedSumAtPosition( - ImageFrame source, - int maxX, - int maxY, - ref WeightsWindow windowX, - ref WeightsWindow windowY, - ref Point point) + /// The source dimension size + /// The destination dimension size + /// The radius, and scaling factor + private (float radius, float scale) GetSamplingRadius(int sourceSize, int destinationSize) { - // What, in theory, is supposed to happen here is the following... - // - // We identify the maximum possible pixel offsets allowable by the current sampler - // clamping values to ensure that we do not go outwith the bounds of our image. - // - // Then we get the weights of that offset value from our pre-calculated vaues. - // First we grab the weight on the y-axis, then the x-axis and then we multiply - // them together to get the final weight. - // - // Unfortunately this simply does not seem to work! - // The output is rubbish and I cannot see why :( - ref float horizontalValues = ref windowX.GetStartReference(); - ref float verticalValues = ref windowY.GetStartReference(); - - int yLength = windowY.Length; - int xLength = windowX.Length; - int yRadius = (int)MathF.Ceiling((yLength - 1) * .5F); - int xRadius = (int)MathF.Ceiling((xLength - 1) * .5F); - - int left = point.X - xRadius; - int right = point.X + xRadius; - int top = point.Y - yRadius; - int bottom = point.Y + yRadius; - - int yIndex = 0; - int xIndex = 0; - - // Faster than clamping + we know we are only looking in one direction - if (left < 0) - { - // Trim the length of our weights iterator across the x-axis. - // Offset our start index across the x-axis. - xIndex = ImageMaths.FastAbs(left); - xLength -= xIndex; - left = 0; - } - - if (top < 0) - { - // Trim the length of our weights iterator across the y-axis. - // Offset our start index across the y-axis. - yIndex = ImageMaths.FastAbs(top); - yLength -= yIndex; - top = 0; - } + float ratio = (float)sourceSize / destinationSize; + float scale = ratio; - if (right >= maxX) + if (scale < 1F) { - // Trim the length of our weights iterator across the x-axis. - xLength -= right - maxX; - } - - if (bottom >= maxY) - { - // Trim the length of our weights iterator across the y-axis. - yLength -= bottom - maxY; - } - - Vector4 result = Vector4.Zero; - - // We calculate our sample by iterating up-down/left-right from our transformed point. - for (int y = top, yi = yIndex; yi < yLength; y++, yi++) - { - float yweight = Unsafe.Add(ref verticalValues, yi); - - for (int x = left, xi = xIndex; xi < xLength; x++, xi++) - { - float xweight = Unsafe.Add(ref horizontalValues, xi); - float weight = yweight * xweight; - - result += source[x, y].ToVector4() * weight; - } + scale = 1F; } - return result; + return (MathF.Ceiling(scale * this.Sampler.Radius), scale); } } diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/BicubicResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/BicubicResampler.cs index be9de9edaa..9fb1313d9f 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/BicubicResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/BicubicResampler.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Runtime.CompilerServices; + namespace SixLabors.ImageSharp.Processing { /// @@ -14,6 +16,7 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 2; /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { if (x < 0F) diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/BoxResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/BoxResampler.cs index 5aab0d07fa..8255af4fe5 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/BoxResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/BoxResampler.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Runtime.CompilerServices; + namespace SixLabors.ImageSharp.Processing { /// @@ -13,6 +15,7 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 0.5F; /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { if (x > -0.5F && x <= 0.5F) diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/CatmullRomResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/CatmullRomResampler.cs index 1c84676188..19f466287c 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/CatmullRomResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/CatmullRomResampler.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Runtime.CompilerServices; + namespace SixLabors.ImageSharp.Processing { /// @@ -15,6 +17,7 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 2; /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { const float B = 0; diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/HermiteResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/HermiteResampler.cs index 33435059f1..afc1427f48 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/HermiteResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/HermiteResampler.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Runtime.CompilerServices; + namespace SixLabors.ImageSharp.Processing { /// @@ -14,6 +16,7 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 2; /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { const float B = 0F; diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos2Resampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos2Resampler.cs index 29568db021..3d5af528e2 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos2Resampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos2Resampler.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Runtime.CompilerServices; + namespace SixLabors.ImageSharp.Processing { /// @@ -14,6 +16,7 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 2; /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { if (x < 0F) diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos3Resampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos3Resampler.cs index 492ef69e4c..7e46b05f33 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos3Resampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos3Resampler.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Runtime.CompilerServices; + namespace SixLabors.ImageSharp.Processing { /// @@ -14,6 +16,7 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 3; /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { if (x < 0F) diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos5Resampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos5Resampler.cs index cae152a53c..d593dbcf43 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos5Resampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos5Resampler.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Runtime.CompilerServices; + namespace SixLabors.ImageSharp.Processing { /// @@ -14,6 +16,7 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 5; /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { if (x < 0F) diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos8Resampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos8Resampler.cs index b390c55419..5d7c708f2d 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos8Resampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos8Resampler.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Runtime.CompilerServices; + namespace SixLabors.ImageSharp.Processing { /// @@ -14,6 +16,7 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 8; /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { if (x < 0F) diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/MitchellNetravaliResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/MitchellNetravaliResampler.cs index df351d9505..f6e9a9fa09 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/MitchellNetravaliResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/MitchellNetravaliResampler.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Runtime.CompilerServices; + namespace SixLabors.ImageSharp.Processing { /// @@ -13,6 +15,7 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 2; /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { const float B = 0.3333333F; diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/NearestNeighborResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/NearestNeighborResampler.cs index 7a7785be36..b1cc8609e7 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/NearestNeighborResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/NearestNeighborResampler.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Runtime.CompilerServices; + namespace SixLabors.ImageSharp.Processing { /// @@ -13,6 +15,7 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 1; /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { return x; diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/RobidouxSharpResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/RobidouxSharpResampler.cs index a345da3f42..9db4b125f2 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/RobidouxSharpResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/RobidouxSharpResampler.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Runtime.CompilerServices; + namespace SixLabors.ImageSharp.Processing { /// @@ -13,6 +15,7 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 2; /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { const float B = 0.2620145123990142F; diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/SplineResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/SplineResampler.cs index ac5e2dedba..815fd9c3dc 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/SplineResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/SplineResampler.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Runtime.CompilerServices; + namespace SixLabors.ImageSharp.Processing { /// @@ -13,6 +15,7 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 2; /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { const float B = 1F; diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/TriangleResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/TriangleResampler.cs index 842da87e06..4b62c767bc 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/TriangleResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/TriangleResampler.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Runtime.CompilerServices; + namespace SixLabors.ImageSharp.Processing { /// @@ -14,6 +16,7 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 1; /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { if (x < 0F) diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/WelchResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/WelchResampler.cs index acb74a8ec4..9bf19573af 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/WelchResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/WelchResampler.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System.Runtime.CompilerServices; + namespace SixLabors.ImageSharp.Processing { /// @@ -13,6 +15,7 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 3; /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { if (x < 0F) @@ -28,4 +31,4 @@ namespace SixLabors.ImageSharp.Processing return 0F; } } -} +} \ No newline at end of file From b26ae165e58785c727d2309e84d2442c032ca470 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 2 Dec 2017 00:25:02 +1100 Subject: [PATCH 16/51] Performance improvements in loop --- .../Processors/Transforms/AffineProcessor.cs | 38 +++++++++++-------- .../ImageSharp.Tests/ImageSharp.Tests.csproj | 6 +-- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index cea7f5953a..3dbcf796fc 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Numerics; -using System.Runtime.CompilerServices; using System.Threading.Tasks; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Helpers; @@ -109,6 +108,8 @@ namespace SixLabors.ImageSharp.Processing.Processors float yScale = yRadiusScale.scale; float xRadius = xRadiusScale.radius; float yRadius = yRadiusScale.radius; + IResampler sampler = this.Sampler; + var maxSource = new Vector4(maxSourceX, maxSourceY, maxSourceX, maxSourceY); Parallel.For( 0, @@ -121,36 +122,41 @@ namespace SixLabors.ImageSharp.Processing.Processors { // Use the single precision position to calculate correct bounding pixels // otherwise we get rogue pixels outside of the bounds. - var point = PointF.Transform(new PointF(x, y), matrix); - int maxX = (int)MathF.Ceiling(point.X + xRadius); - int maxY = (int)MathF.Ceiling(point.Y + yRadius); - int minX = (int)MathF.Floor(point.X - xRadius); - int minY = (int)MathF.Floor(point.Y - yRadius); - - // Clamp sampling pixels to the source image edge - maxX = maxX.Clamp(0, maxSourceX); - minX = minX.Clamp(0, maxSourceX); - maxY = maxY.Clamp(0, maxSourceY); - minY = minY.Clamp(0, maxSourceY); + var point = Vector2.Transform(new Vector2(x, y), matrix); + + // Clamp sampling pixel radial extents to the source image edges + var extents = new Vector4( + MathF.Ceiling(point.X + xRadius), + MathF.Ceiling(point.Y + yRadius), + MathF.Floor(point.X - xRadius), + MathF.Floor(point.Y - yRadius)); + + 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 manually. + // It appears these have to be calculated on-the-fly. // Using the precalculated weights give the wrong values. - // TODO: Find a way to speed this up. + // TODO: Find a way to speed this up if we can. Vector4 sum = Vector4.Zero; for (int i = minX; i <= maxX; i++) { - float weightX = this.Sampler.GetValue((i - point.X) / xScale); + float weightX = sampler.GetValue((i - point.X) / xScale); for (int j = minY; j <= maxY; j++) { - float weightY = this.Sampler.GetValue((j - point.Y) / yScale); + float weightY = sampler.GetValue((j - point.Y) / yScale); sum += source[i, j].ToVector4() * weightX * weightY; } } + ref TPixel dest = ref destRow[x]; dest.PackFromVector4(sum); } diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index 2f45e4c83a..eb60aa5fe4 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -17,9 +17,9 @@ - - - + + + From 6b182faf809eb273dcb0d47c67e0d5088285d4fc Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 3 Dec 2017 00:01:39 +1100 Subject: [PATCH 17/51] Fix inheritance and cleanup --- src/ImageSharp/Common/Helpers/ImageMaths.cs | 27 ++++--- .../Processors/Transforms/AffineProcessor.cs | 79 +++++++++---------- .../Transforms/AutoOrientProcessor.cs | 18 ++--- .../Processors/Transforms/RotateProcessor.cs | 36 ++++----- .../Processors/Transforms/SkewProcessor.cs | 33 +++++--- .../Transforms/Resamplers/BicubicResampler.cs | 3 - .../Transforms/Resamplers/BoxResampler.cs | 3 - .../Resamplers/CatmullRomResampler.cs | 3 - .../Transforms/Resamplers/HermiteResampler.cs | 3 - .../Resamplers/Lanczos2Resampler.cs | 3 - .../Resamplers/Lanczos3Resampler.cs | 3 - .../Resamplers/Lanczos5Resampler.cs | 3 - .../Resamplers/Lanczos8Resampler.cs | 3 - .../Resamplers/MitchellNetravaliResampler.cs | 3 - .../Resamplers/NearestNeighborResampler.cs | 3 - .../Resamplers/RobidouxSharpResampler.cs | 3 - .../Transforms/Resamplers/SplineResampler.cs | 3 - .../Resamplers/TriangleResampler.cs | 3 - .../Transforms/Resamplers/WelchResampler.cs | 3 - .../Processing/Transforms/Rotate.cs | 37 ++------- src/ImageSharp/Processing/Transforms/Skew.cs | 45 +++-------- .../Processing/Transforms/RotateFlipTests.cs | 9 +-- .../Processing/Transforms/RotateTests.cs | 31 ++------ .../Processing/Transforms/SkewTest.cs | 21 +---- 24 files changed, 125 insertions(+), 253 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/ImageMaths.cs b/src/ImageSharp/Common/Helpers/ImageMaths.cs index fa60f855ee..74d705d53a 100644 --- a/src/ImageSharp/Common/Helpers/ImageMaths.cs +++ b/src/ImageSharp/Common/Helpers/ImageMaths.cs @@ -147,17 +147,26 @@ namespace SixLabors.ImageSharp /// /// The . /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] 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)MathF.Ceiling(extentX), (int)MathF.Ceiling(extentY)); + // 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); + + // Find the minimum and maximum "corners" based on the ones above + 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))); + var min = new Vector2(minX, minY); + var max = new Vector2(maxX, maxY); + Vector2 size = max - min; + + return new Rectangle((int)MathF.Floor(minX), (int)MathF.Floor(minY), (int)MathF.Ceiling(size.X), (int)MathF.Ceiling(size.Y)); } /// diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index 3dbcf796fc..843d07f4b5 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Numerics; using System.Threading.Tasks; @@ -18,52 +17,40 @@ namespace SixLabors.ImageSharp.Processing.Processors /// Provides the base methods to perform affine transforms on an image. /// /// The pixel format. - internal abstract class AffineProcessor : ResamplingWeightedProcessor + internal abstract class AffineProcessor : CloningImageProcessor where TPixel : struct, IPixel { + private Rectangle targetRectangle; + private Matrix3x2 transformMatrix; + /// /// Initializes a new instance of the class. /// /// The sampler to perform the resize operation. protected AffineProcessor(IResampler sampler) - : base(sampler, 1, 1, Rectangles.DefaultRectangle) // Hack to prevent Guard throwing in base, we always set the canvas { + this.Sampler = sampler; } /// - /// Gets or sets a value indicating whether to expand the canvas to fit the transformed image. + /// Gets the sampler to perform interpolation of the transform operation. /// - public bool Expand { get; set; } = true; + public IResampler Sampler { get; } /// /// Returns the processing matrix used for transforming the image. /// - /// The rectangle bounds /// The - protected abstract Matrix3x2 CreateProcessingMatrix(Rectangle rectangle); - - /// - /// Creates a new target canvas to contain the results of the matrix transform. - /// - /// The source rectangle. - protected virtual void CreateNewCanvas(Rectangle sourceRectangle) - { - this.ResizeRectangle = Matrix3x2.Invert(this.CreateProcessingMatrix(sourceRectangle), out Matrix3x2 sizeMatrix) - ? ImageMaths.GetBoundingRectangle(sourceRectangle, sizeMatrix) - : sourceRectangle; - - this.Width = this.ResizeRectangle.Width; - this.Height = this.ResizeRectangle.Height; - } + protected abstract Matrix3x2 GetTransformMatrix(); /// protected override Image CreateDestination(Image source, Rectangle sourceRectangle) { - this.CreateNewCanvas(sourceRectangle); + this.ResizeCanvas(sourceRectangle); // 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.ResizeRectangle.Width, this.ResizeRectangle.Height, x.MetaData.Clone())); + 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); @@ -72,9 +59,11 @@ namespace SixLabors.ImageSharp.Processing.Processors /// protected override void OnApply(ImageFrame source, ImageFrame destination, Rectangle sourceRectangle, Configuration configuration) { - int height = this.ResizeRectangle.Height; - int width = this.ResizeRectangle.Width; + int height = this.targetRectangle.Height; + int width = this.targetRectangle.Width; Rectangle sourceBounds = source.Bounds(); + + // Since could potentially be resizing the canvas we need to recenter the matrix Matrix3x2 matrix = this.GetCenteredMatrix(source); if (this.Sampler is NearestNeighborResampler) @@ -106,8 +95,7 @@ namespace SixLabors.ImageSharp.Processing.Processors (float radius, float scale) yRadiusScale = this.GetSamplingRadius(source.Height, destination.Height); float xScale = xRadiusScale.scale; float yScale = yRadiusScale.scale; - float xRadius = xRadiusScale.radius; - float yRadius = yRadiusScale.radius; + var radius = new Vector2(xRadiusScale.radius, yRadiusScale.radius); IResampler sampler = this.Sampler; var maxSource = new Vector4(maxSourceX, maxSourceY, maxSourceX, maxSourceY); @@ -125,11 +113,14 @@ namespace SixLabors.ImageSharp.Processing.Processors 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; + var extents = new Vector4( - MathF.Ceiling(point.X + xRadius), - MathF.Ceiling(point.Y + yRadius), - MathF.Floor(point.X - xRadius), - MathF.Floor(point.Y - yRadius)); + MathF.Ceiling(maxXY.X), + MathF.Ceiling(maxXY.Y), + MathF.Floor(minXY.X), + MathF.Floor(minXY.Y)); extents = Vector4.Clamp(extents, Vector4.Zero, maxSource); @@ -172,9 +163,21 @@ namespace SixLabors.ImageSharp.Processing.Processors /// protected Matrix3x2 GetCenteredMatrix(ImageFrame source) { - var translationToTargetCenter = Matrix3x2.CreateTranslation(-this.ResizeRectangle.Width * .5F, -this.ResizeRectangle.Height * .5F); + var translationToTargetCenter = Matrix3x2.CreateTranslation(-this.targetRectangle.Width * .5F, -this.targetRectangle.Height * .5F); var translateToSourceCenter = Matrix3x2.CreateTranslation(source.Width * .5F, source.Height * .5F); - return (translationToTargetCenter * this.CreateProcessingMatrix(this.ResizeRectangle)) * translateToSourceCenter; + return translationToTargetCenter * this.transformMatrix * translateToSourceCenter; + } + + /// + /// Creates a new target canvas to contain the results of the matrix transform. + /// + /// The source rectangle. + private void ResizeCanvas(Rectangle sourceRectangle) + { + this.transformMatrix = this.GetTransformMatrix(); + this.targetRectangle = Matrix3x2.Invert(this.transformMatrix, out Matrix3x2 sizeMatrix) + ? ImageMaths.GetBoundingRectangle(sourceRectangle, sizeMatrix) + : sourceRectangle; } /// @@ -196,14 +199,4 @@ namespace SixLabors.ImageSharp.Processing.Processors return (MathF.Ceiling(scale * this.Sampler.Radius), scale); } } - - /// - /// Contains a static rectangle used for comparison when creating a new canvas. - /// We do this so we can inherit from the resampling weights class and pass the guard in the constructor and also avoid creating a new rectangle each time. - /// - [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleType", Justification = "I'm using this only here to prevent duplication in generic types.")] - internal static class Rectangles - { - public static Rectangle DefaultRectangle { get; } = new Rectangle(0, 0, 1, 1); - } } \ 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/RotateProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs index 1ad7263929..be49ec321b 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs @@ -19,39 +19,35 @@ namespace SixLabors.ImageSharp.Processing.Processors internal class RotateProcessor : AffineProcessor where TPixel : struct, IPixel { - private Matrix3x2 transformMatrix; - /// /// Initializes a new instance of the class. /// - public RotateProcessor() - : base(KnownResamplers.NearestNeighbor) + /// The angle of rotation in degrees. + public RotateProcessor(float angle) + : this(angle, KnownResamplers.NearestNeighbor) { } /// /// Initializes a new instance of the class. /// + /// The angle of rotation in degrees. /// The sampler to perform the rotating operation. - public RotateProcessor(IResampler sampler) + public RotateProcessor(float degrees, IResampler sampler) : base(sampler) { + this.Degrees = degrees; } /// - /// Gets or sets the angle of processMatrix in degrees. + /// Gets the angle of rotation in degrees. /// - public float Angle { get; set; } + public float Degrees { get; } /// - protected override Matrix3x2 CreateProcessingMatrix(Rectangle rectangle) + protected override Matrix3x2 GetTransformMatrix() { - if (this.transformMatrix == default(Matrix3x2)) - { - this.transformMatrix = Matrix3x2Extensions.CreateRotationDegrees(-this.Angle, PointF.Empty); - } - - return this.transformMatrix; + return Matrix3x2Extensions.CreateRotationDegrees(-this.Degrees, PointF.Empty); } /// @@ -74,7 +70,7 @@ namespace SixLabors.ImageSharp.Processing.Processors return; } - if (MathF.Abs(this.Angle) < Constants.Epsilon) + if (MathF.Abs(this.Degrees) < Constants.Epsilon) { // No need to do anything so return. return; @@ -82,7 +78,7 @@ namespace SixLabors.ImageSharp.Processing.Processors profile.RemoveValue(ExifTag.Orientation); - if (this.Expand && profile.GetValue(ExifTag.PixelXDimension) != null) + if (profile.GetValue(ExifTag.PixelXDimension) != null) { profile.SetValue(ExifTag.PixelXDimension, source.Width); profile.SetValue(ExifTag.PixelYDimension, source.Height); @@ -100,26 +96,26 @@ namespace SixLabors.ImageSharp.Processing.Processors /// private bool OptimizedApply(ImageFrame source, ImageFrame destination, Configuration configuration) { - if (MathF.Abs(this.Angle) < Constants.Epsilon) + if (MathF.Abs(this.Degrees) < Constants.Epsilon) { // 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(this.Degrees - 90) < Constants.Epsilon) { this.Rotate90(source, destination, configuration); return true; } - if (MathF.Abs(this.Angle - 180) < Constants.Epsilon) + if (MathF.Abs(this.Degrees - 180) < Constants.Epsilon) { this.Rotate180(source, destination, configuration); return true; } - if (MathF.Abs(this.Angle - 270) < Constants.Epsilon) + if (MathF.Abs(this.Degrees - 270) < Constants.Epsilon) { this.Rotate270(source, destination, configuration); return true; diff --git a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs index 61526747ea..8da8b1e57b 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs @@ -14,36 +14,43 @@ namespace SixLabors.ImageSharp.Processing.Processors internal class SkewProcessor : AffineProcessor where TPixel : struct, IPixel { - private Matrix3x2 transformMatrix; + /// + /// Initializes a new instance of the class. + /// + /// 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.NearestNeighbor) + { + } /// /// Initializes a new instance of the class. /// + /// 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(IResampler sampler) + public SkewProcessor(float degreesX, float degreesY, IResampler sampler) : base(sampler) { + this.DegreesX = degreesX; + this.DegreesY = degreesY; } /// - /// Gets or sets the angle of rotation along the x-axis in degrees. + /// Gets the angle of rotation along the x-axis in degrees. /// - public float AngleX { get; set; } + public float DegreesX { get; } /// - /// Gets or sets the angle of rotation along the y-axis in degrees. + /// Gets the angle of rotation along the y-axis in degrees. /// - public float AngleY { get; set; } + public float DegreesY { get; } /// - protected override Matrix3x2 CreateProcessingMatrix(Rectangle rectangle) + protected override Matrix3x2 GetTransformMatrix() { - if (this.transformMatrix == default(Matrix3x2)) - { - this.transformMatrix = Matrix3x2Extensions.CreateSkewDegrees(-this.AngleX, -this.AngleY, PointF.Empty); - } - - return this.transformMatrix; + return Matrix3x2Extensions.CreateSkewDegrees(-this.DegreesX, -this.DegreesY, PointF.Empty); } } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/BicubicResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/BicubicResampler.cs index 9fb1313d9f..be9de9edaa 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/BicubicResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/BicubicResampler.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Runtime.CompilerServices; - namespace SixLabors.ImageSharp.Processing { /// @@ -16,7 +14,6 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 2; /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { if (x < 0F) diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/BoxResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/BoxResampler.cs index 8255af4fe5..5aab0d07fa 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/BoxResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/BoxResampler.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Runtime.CompilerServices; - namespace SixLabors.ImageSharp.Processing { /// @@ -15,7 +13,6 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 0.5F; /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { if (x > -0.5F && x <= 0.5F) diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/CatmullRomResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/CatmullRomResampler.cs index 19f466287c..1c84676188 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/CatmullRomResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/CatmullRomResampler.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Runtime.CompilerServices; - namespace SixLabors.ImageSharp.Processing { /// @@ -17,7 +15,6 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 2; /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { const float B = 0; diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/HermiteResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/HermiteResampler.cs index afc1427f48..33435059f1 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/HermiteResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/HermiteResampler.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Runtime.CompilerServices; - namespace SixLabors.ImageSharp.Processing { /// @@ -16,7 +14,6 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 2; /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { const float B = 0F; diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos2Resampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos2Resampler.cs index 3d5af528e2..29568db021 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos2Resampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos2Resampler.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Runtime.CompilerServices; - namespace SixLabors.ImageSharp.Processing { /// @@ -16,7 +14,6 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 2; /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { if (x < 0F) diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos3Resampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos3Resampler.cs index 7e46b05f33..492ef69e4c 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos3Resampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos3Resampler.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Runtime.CompilerServices; - namespace SixLabors.ImageSharp.Processing { /// @@ -16,7 +14,6 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 3; /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { if (x < 0F) diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos5Resampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos5Resampler.cs index d593dbcf43..cae152a53c 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos5Resampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos5Resampler.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Runtime.CompilerServices; - namespace SixLabors.ImageSharp.Processing { /// @@ -16,7 +14,6 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 5; /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { if (x < 0F) diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos8Resampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos8Resampler.cs index 5d7c708f2d..b390c55419 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos8Resampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/Lanczos8Resampler.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Runtime.CompilerServices; - namespace SixLabors.ImageSharp.Processing { /// @@ -16,7 +14,6 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 8; /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { if (x < 0F) diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/MitchellNetravaliResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/MitchellNetravaliResampler.cs index f6e9a9fa09..df351d9505 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/MitchellNetravaliResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/MitchellNetravaliResampler.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Runtime.CompilerServices; - namespace SixLabors.ImageSharp.Processing { /// @@ -15,7 +13,6 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 2; /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { const float B = 0.3333333F; diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/NearestNeighborResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/NearestNeighborResampler.cs index b1cc8609e7..7a7785be36 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/NearestNeighborResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/NearestNeighborResampler.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Runtime.CompilerServices; - namespace SixLabors.ImageSharp.Processing { /// @@ -15,7 +13,6 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 1; /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { return x; diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/RobidouxSharpResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/RobidouxSharpResampler.cs index 9db4b125f2..a345da3f42 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/RobidouxSharpResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/RobidouxSharpResampler.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Runtime.CompilerServices; - namespace SixLabors.ImageSharp.Processing { /// @@ -15,7 +13,6 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 2; /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { const float B = 0.2620145123990142F; diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/SplineResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/SplineResampler.cs index 815fd9c3dc..ac5e2dedba 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/SplineResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/SplineResampler.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Runtime.CompilerServices; - namespace SixLabors.ImageSharp.Processing { /// @@ -15,7 +13,6 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 2; /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { const float B = 1F; diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/TriangleResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/TriangleResampler.cs index 4b62c767bc..842da87e06 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/TriangleResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/TriangleResampler.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Runtime.CompilerServices; - namespace SixLabors.ImageSharp.Processing { /// @@ -16,7 +14,6 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 1; /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { if (x < 0F) diff --git a/src/ImageSharp/Processing/Transforms/Resamplers/WelchResampler.cs b/src/ImageSharp/Processing/Transforms/Resamplers/WelchResampler.cs index 9bf19573af..9e18a24710 100644 --- a/src/ImageSharp/Processing/Transforms/Resamplers/WelchResampler.cs +++ b/src/ImageSharp/Processing/Transforms/Resamplers/WelchResampler.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Runtime.CompilerServices; - namespace SixLabors.ImageSharp.Processing { /// @@ -15,7 +13,6 @@ namespace SixLabors.ImageSharp.Processing public float Radius => 3; /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public float GetValue(float x) { if (x < 0F) diff --git a/src/ImageSharp/Processing/Transforms/Rotate.cs b/src/ImageSharp/Processing/Transforms/Rotate.cs index 2ec1385bb9..e9ae4fcf32 100644 --- a/src/ImageSharp/Processing/Transforms/Rotate.cs +++ b/src/ImageSharp/Processing/Transforms/Rotate.cs @@ -12,29 +12,6 @@ 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. - /// - /// The pixel format. - /// The image to rotate. - /// The angle in degrees to perform the rotation. - /// The - public static IImageProcessingContext Rotate(this IImageProcessingContext source, float degrees) - where TPixel : struct, IPixel - => Rotate(source, degrees, true); - - /// - /// Rotates an image by the given angle in degrees, expanding the image to fit the rotated result. - /// - /// The pixel format. - /// The image to rotate. - /// The angle in degrees to perform the rotation. - /// The to perform the resampling. - /// The - public static IImageProcessingContext Rotate(this IImageProcessingContext source, float degrees, IResampler sampler) - where TPixel : struct, IPixel - => Rotate(source, degrees, sampler, true); - /// /// Rotates and flips an image by the given instructions. /// @@ -44,7 +21,7 @@ namespace SixLabors.ImageSharp /// The public static IImageProcessingContext Rotate(this IImageProcessingContext source, RotateType rotateType) where TPixel : struct, IPixel - => Rotate(source, (float)rotateType, false); + => Rotate(source, (float)rotateType); /// /// Rotates an image by the given angle in degrees. @@ -52,11 +29,10 @@ namespace SixLabors.ImageSharp /// 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 - public static IImageProcessingContext Rotate(this IImageProcessingContext source, float degrees, bool expand) + public static IImageProcessingContext Rotate(this IImageProcessingContext source, float degrees) where TPixel : struct, IPixel - => Rotate(source, degrees, KnownResamplers.NearestNeighbor, expand); + => Rotate(source, degrees, KnownResamplers.NearestNeighbor); /// /// Rotates an image by the given angle in degrees using the specified sampling algorithm. @@ -65,10 +41,9 @@ namespace SixLabors.ImageSharp /// The image to rotate. /// The angle in degrees to perform the rotation. /// The to perform the resampling. - /// Whether to expand the image to fit the rotated result. /// The - public static IImageProcessingContext Rotate(this IImageProcessingContext source, float degrees, IResampler sampler, bool expand) + public static IImageProcessingContext Rotate(this IImageProcessingContext source, float degrees, IResampler sampler) where TPixel : struct, IPixel - => source.ApplyProcessor(new RotateProcessor(sampler) { 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 f03e60e3da..b7a431cce4 100644 --- a/src/ImageSharp/Processing/Transforms/Skew.cs +++ b/src/ImageSharp/Processing/Transforms/Skew.cs @@ -12,56 +12,29 @@ 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. - /// - /// 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 - public static IImageProcessingContext Skew(this IImageProcessingContext source, float degreesX, float degreesY) - where TPixel : struct, IPixel - => Skew(source, degreesX, degreesY, true); - - /// - /// Skews an image by the given angles in degrees using the given sampler, expanding the image to fit the skewed result. - /// - /// 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 to perform the resampling. - /// The - public static IImageProcessingContext Skew(this IImageProcessingContext source, float degreesX, float degreesY, IResampler sampler) - where TPixel : struct, IPixel - => Skew(source, degreesX, degreesY, sampler, true); - /// /// 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. - /// 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 - public static IImageProcessingContext Skew(this IImageProcessingContext source, float degreesX, float degreesY, bool expand) + public static IImageProcessingContext Skew(this IImageProcessingContext source, float degreesX, float degreesY) where TPixel : struct, IPixel - => Skew(source, degreesX, degreesY, KnownResamplers.NearestNeighbor, expand); + => Skew(source, degreesX, degreesY, KnownResamplers.NearestNeighbor); /// /// 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. + /// 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. - /// Whether to expand the image to fit the skewed result. /// The - public static IImageProcessingContext Skew(this IImageProcessingContext source, float degreesX, float degreesY, IResampler sampler, bool expand) + public static IImageProcessingContext Skew(this IImageProcessingContext source, float degreesX, float degreesY, IResampler sampler) where TPixel : struct, IPixel - => source.ApplyProcessor(new SkewProcessor(sampler) { AngleX = degreesX, AngleY = degreesY, Expand = expand }); + => source.ApplyProcessor(new SkewProcessor(degreesX, degreesY, sampler)); } -} +} \ 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 From 36c3a4b9f213e1f965c23419b7294f59fe8fc5b4 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 3 Dec 2017 03:36:49 +1100 Subject: [PATCH 18/51] WIP make this work with scaled-down transforms. If we normalize the weights to make this work when the output is scaled down we break the edge pixel output. Somehow fix this. --- .../Processors/Transforms/AffineProcessor.cs | 125 ++++++++++++------ .../Transforms/TransformProcessor.cs | 47 +++++++ .../Processing/Transforms/Transform.cs | 39 ++++++ .../Processing/Transforms/TransformTests.cs | 79 +++++++++++ 4 files changed, 249 insertions(+), 41 deletions(-) create mode 100644 src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs create mode 100644 src/ImageSharp/Processing/Transforms/Transform.cs create mode 100644 tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index 843d07f4b5..26b8e3a124 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -8,6 +8,7 @@ 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; @@ -26,7 +27,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// /// Initializes a new instance of the class. /// - /// The sampler to perform the resize operation. + /// The sampler to perform the transform operation. protected AffineProcessor(IResampler sampler) { this.Sampler = sampler; @@ -91,13 +92,15 @@ namespace SixLabors.ImageSharp.Processing.Processors int maxSourceX = source.Width - 1; int maxSourceY = source.Height - 1; - (float radius, float scale) xRadiusScale = this.GetSamplingRadius(source.Width, destination.Width); - (float radius, float scale) yRadiusScale = this.GetSamplingRadius(source.Height, destination.Height); + (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); Parallel.For( 0, @@ -106,50 +109,90 @@ namespace SixLabors.ImageSharp.Processing.Processors y => { Span destRow = destination.GetPixelRowSpan(y); - for (int x = 0; x < width; x++) + using (var yBuffer = new Buffer(yLength)) + using (var xBuffer = new Buffer(xLength)) { - // 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; - - var extents = new Vector4( - MathF.Ceiling(maxXY.X), - MathF.Ceiling(maxXY.Y), - MathF.Floor(minXY.X), - MathF.Floor(minXY.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; + + var extents = new Vector4( + MathF.Ceiling(maxXY.X), + MathF.Ceiling(maxXY.Y), + MathF.Floor(minXY.X), + MathF.Floor(minXY.Y)); + + 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; + + // TODO: Find a way to speed this up if we can we precalculated weights!!! + // It appears these have to be calculated on-the-fly. + // Check with Anton to figure out why indexing from the precalculated weights was wrong. + // + // Create and normalize the y-weights + float ySum = 0; + for (int yy = 0, i = minY; i <= maxY; i++, yy++) + { + float weight = sampler.GetValue((i - point.Y) / yScale); + ySum += weight; + yBuffer[yy] = weight; + } - extents = Vector4.Clamp(extents, Vector4.Zero, maxSource); + // TODO: + // Normalizing the weights fixes scaled transfrom where we scale down but breaks edge pixel belnding + // We end up with too much weight on pixels that should be blended. + // We could, maybe, move the division into the loop and not divide when we hit 0 or maxN but that seems clunky. + if (ySum > 0) + { + for (int i = 0; i < yBuffer.Length; i++) + { + yBuffer[i] = yBuffer[i] / ySum; + } + } - int maxX = (int)extents.X; - int maxY = (int)extents.Y; - int minX = (int)extents.Z; - int minY = (int)extents.W; + // Create and normalize the x-weights + float xSum = 0; + for (int xx = 0, i = minX; i <= maxX; i++, xx++) + { + float weight = sampler.GetValue((i - point.X) / xScale); + xSum += weight; + xBuffer[xx] = weight; + } - if (minX == maxX || minY == maxY) - { - continue; - } + if (xSum > 0) + { + for (int i = 0; i < xBuffer.Length; i++) + { + xBuffer[i] = xBuffer[i] / xSum; + } + } - // It appears these have to be calculated on-the-fly. - // Using the precalculated weights give the wrong values. - // TODO: Find a way to speed this up if we can. - Vector4 sum = Vector4.Zero; - for (int i = minX; i <= maxX; i++) - { - float weightX = sampler.GetValue((i - point.X) / xScale); - for (int j = minY; j <= maxY; j++) + // Now multiply the normalized results against the offsets + Vector4 sum = Vector4.Zero; + for (int yy = 0, j = minY; j <= maxY; j++, yy++) { - float weightY = sampler.GetValue((j - point.Y) / yScale); - sum += source[i, j].ToVector4() * weightX * weightY; + float yWeight = yBuffer[yy]; + + for (int xx = 0, i = minX; i <= maxX; i++, xx++) + { + float xWeight = xBuffer[xx]; + sum += source[i, j].ToVector4() * xWeight * yWeight; + } } - } - ref TPixel dest = ref destRow[x]; - dest.PackFromVector4(sum); + ref TPixel dest = ref destRow[x]; + dest.PackFromVector4(sum); + } } }); } @@ -186,7 +229,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// The source dimension size /// The destination dimension size /// The radius, and scaling factor - private (float radius, float scale) GetSamplingRadius(int sourceSize, int destinationSize) + private (float radius, float scale, float ratio) GetSamplingRadius(int sourceSize, int destinationSize) { float ratio = (float)sourceSize / destinationSize; float scale = ratio; @@ -196,7 +239,7 @@ namespace SixLabors.ImageSharp.Processing.Processors scale = 1F; } - return (MathF.Ceiling(scale * this.Sampler.Radius), scale); + return (MathF.Ceiling(scale * this.Sampler.Radius), scale, ratio); } } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs new file mode 100644 index 0000000000..62bdc4a1eb --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs @@ -0,0 +1,47 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Numerics; +using SixLabors.ImageSharp.PixelFormats; + +namespace SixLabors.ImageSharp.Processing.Processors +{ + /// + /// Provides methods that allow the tranformation of images using various algorithms. + /// + /// The pixel format. + internal class TransformProcessor : AffineProcessor + where TPixel : struct, IPixel + { + /// + /// Initializes a new instance of the class. + /// + /// The transformation matrix + public TransformProcessor(Matrix3x2 matrix) + : this(matrix, KnownResamplers.NearestNeighbor) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The transformation matrix + /// The sampler to perform the transform operation. + public TransformProcessor(Matrix3x2 matrix, IResampler sampler) + : base(sampler) + { + this.TransformMatrix = matrix; + } + + /// + /// Gets the transform matrix + /// + public Matrix3x2 TransformMatrix { get; } + + /// + protected override Matrix3x2 GetTransformMatrix() + { + return this.TransformMatrix; + } + } +} \ 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..f3478e32d5 --- /dev/null +++ b/src/ImageSharp/Processing/Transforms/Transform.cs @@ -0,0 +1,39 @@ +// 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; + +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 skew. + /// 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 skew. + /// The transformation matrix. + /// The to perform the resampling. + /// The + public static IImageProcessingContext Transform(this IImageProcessingContext source, Matrix3x2 matrix, IResampler sampler) + where TPixel : struct, IPixel + => source.ApplyProcessor(new TransformProcessor(matrix, sampler)); + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs new file mode 100644 index 0000000000..30cb626155 --- /dev/null +++ b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; + +namespace SixLabors.ImageSharp.Tests.Processing.Transforms +{ + using System.Numerics; + using System.Reflection; + + using SixLabors.ImageSharp.PixelFormats; + using SixLabors.ImageSharp.Processing; + using SixLabors.Primitives; + + using Xunit; + + public class TransformTests : FileTestBase + { + public static readonly TheoryData TransformValues + = new TheoryData + { + { 20, 10, 50 }, + { -20, -10, 50 } + }; + + 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(TransformValues), DefaultPixelType)] + public void ImageShouldTransformWithSampler(TestImageProvider provider, float x, float y, float z) + where TPixel : struct, IPixel + { + + foreach (string resamplerName in ResamplerNames) + { + IResampler sampler = GetResampler(resamplerName); + using (Image image = provider.GetImage()) + { + Matrix3x2 rotate = Matrix3x2Extensions.CreateRotationDegrees(-z); + + // TODO, how does scale work? 2 means half just now, + Matrix3x2 scale = Matrix3x2Extensions.CreateScale(new SizeF(2F, 2F)); + + + image.Mutate(i => i.Transform(scale * rotate, 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 From be2c2e2742793ab2a59a64f52575ea08049b3443 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 4 Dec 2017 12:37:33 +1100 Subject: [PATCH 19/51] Handle downsized transforms --- .../Processors/Transforms/AffineProcessor.cs | 98 +++++++++++++------ 1 file changed, 70 insertions(+), 28 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index 26b8e3a124..f7ba651778 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Numerics; +using System.Runtime.CompilerServices; using System.Threading.Tasks; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Helpers; @@ -128,6 +129,11 @@ namespace SixLabors.ImageSharp.Processing.Processors MathF.Floor(minXY.X), MathF.Floor(minXY.Y)); + 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; @@ -135,46 +141,34 @@ namespace SixLabors.ImageSharp.Processing.Processors int minX = (int)extents.Z; int minY = (int)extents.W; + if (minX == maxX || minY == maxY) + { + continue; + } + // TODO: Find a way to speed this up if we can we precalculated weights!!! // It appears these have to be calculated on-the-fly. // Check with Anton to figure out why indexing from the precalculated weights was wrong. + // It might not be possible to do so with the resizer weights but perhaps we can fashion something similar for here. // // Create and normalize the y-weights - float ySum = 0; - for (int yy = 0, i = minY; i <= maxY; i++, yy++) + if (yScale > 1) { - float weight = sampler.GetValue((i - point.Y) / yScale); - ySum += weight; - yBuffer[yy] = weight; + CalculateWeightsDown(top, bottom, minY, maxY, point.Y, sampler, yScale, yBuffer); } - - // TODO: - // Normalizing the weights fixes scaled transfrom where we scale down but breaks edge pixel belnding - // We end up with too much weight on pixels that should be blended. - // We could, maybe, move the division into the loop and not divide when we hit 0 or maxN but that seems clunky. - if (ySum > 0) + else { - for (int i = 0; i < yBuffer.Length; i++) - { - yBuffer[i] = yBuffer[i] / ySum; - } + CalculateWeightsScaleUp(minY, maxY, point.Y, sampler, yBuffer); } // Create and normalize the x-weights - float xSum = 0; - for (int xx = 0, i = minX; i <= maxX; i++, xx++) + if (xScale > 1) { - float weight = sampler.GetValue((i - point.X) / xScale); - xSum += weight; - xBuffer[xx] = weight; + CalculateWeightsDown(left, right, minX, maxX, point.X, sampler, xScale, xBuffer); } - - if (xSum > 0) + else { - for (int i = 0; i < xBuffer.Length; i++) - { - xBuffer[i] = xBuffer[i] / xSum; - } + CalculateWeightsScaleUp(minX, maxX, point.X, sampler, xBuffer); } // Now multiply the normalized results against the offsets @@ -211,6 +205,52 @@ namespace SixLabors.ImageSharp.Processing.Processors return translationToTargetCenter * this.transformMatrix * translateToSourceCenter; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void CalculateWeightsDown(int top, int bottom, int min, int max, float point, IResampler sampler, float scale, Buffer 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 = top; i <= bottom; i++, x++) + { + int index = i; + if (index < min) + { + index = min; + } + + if (index > max) + { + index = max; + } + + 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; + } + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void CalculateWeightsScaleUp(int min, int max, float point, IResampler sampler, Buffer weights) + { + ref float weightsBaseRef = ref weights[0]; + for (int x = 0, i = min; i <= max; i++, x++) + { + float weight = sampler.GetValue(i - point); + Unsafe.Add(ref weightsBaseRef, x) = weight; + } + } + /// /// Creates a new target canvas to contain the results of the matrix transform. /// @@ -218,9 +258,11 @@ namespace SixLabors.ImageSharp.Processing.Processors private void ResizeCanvas(Rectangle sourceRectangle) { this.transformMatrix = this.GetTransformMatrix(); + + // this.targetRectangle = ImageMaths.GetBoundingRectangle(sourceRectangle, this.transformMatrix); this.targetRectangle = Matrix3x2.Invert(this.transformMatrix, out Matrix3x2 sizeMatrix) - ? ImageMaths.GetBoundingRectangle(sourceRectangle, sizeMatrix) - : sourceRectangle; + ? ImageMaths.GetBoundingRectangle(sourceRectangle, sizeMatrix) + : sourceRectangle; } /// From af819aba07a6581c950c41d4106d583db165b601 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 4 Dec 2017 15:33:10 +1100 Subject: [PATCH 20/51] Normalize matrix transforms across methods Methods now match w3c expected results. --- .../Processors/Transforms/AffineProcessor.cs | 52 +++-- .../ResamplingWeightedProcessor.Weights.cs | 199 ------------------ .../Transforms/ResamplingWeightedProcessor.cs | 2 +- .../Processors/Transforms/RotateProcessor.cs | 4 +- .../Processors/Transforms/SkewProcessor.cs | 4 +- .../Transforms/TransformProcessor.cs | 1 + .../Processors/Transforms/WeightsBuffer.cs | 53 +++++ .../Processors/Transforms/WeightsWindow.cs | 150 +++++++++++++ .../Transforms/ResizeProfilingBenchmarks.cs | 4 +- .../Processing/Transforms/TransformTests.cs | 11 +- 10 files changed, 253 insertions(+), 227 deletions(-) delete mode 100644 src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs create mode 100644 src/ImageSharp/Processing/Processors/Transforms/WeightsBuffer.cs create mode 100644 src/ImageSharp/Processing/Processors/Transforms/WeightsWindow.cs diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index f7ba651778..767c6feed1 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -65,7 +65,7 @@ namespace SixLabors.ImageSharp.Processing.Processors int width = this.targetRectangle.Width; Rectangle sourceBounds = source.Bounds(); - // Since could potentially be resizing the canvas we need to recenter the matrix + // Since could potentially be resizing the canvas we need to re-center the matrix Matrix3x2 matrix = this.GetCenteredMatrix(source); if (this.Sampler is NearestNeighborResampler) @@ -146,10 +146,10 @@ namespace SixLabors.ImageSharp.Processing.Processors continue; } - // TODO: Find a way to speed this up if we can we precalculated weights!!! // It appears these have to be calculated on-the-fly. - // Check with Anton to figure out why indexing from the precalculated weights was wrong. - // It might not be possible to do so with the resizer weights but perhaps we can fashion something similar for here. + // Precalulating transformed weights would require prior knowledge of every transformed pixel location + // since they can be at sub-pixel positions. + // I've optimized where I can but am always open to suggestions. // // Create and normalize the y-weights if (yScale > 1) @@ -171,7 +171,7 @@ namespace SixLabors.ImageSharp.Processing.Processors CalculateWeightsScaleUp(minX, maxX, point.X, sampler, xBuffer); } - // Now multiply the normalized results against the offsets + // Now multiply the results against the offsets Vector4 sum = Vector4.Zero; for (int yy = 0, j = minY; j <= maxY; j++, yy++) { @@ -205,24 +205,36 @@ namespace SixLabors.ImageSharp.Processing.Processors return translationToTargetCenter * this.transformMatrix * translateToSourceCenter; } + /// + /// 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)] - private static void CalculateWeightsDown(int top, int bottom, int min, int max, float point, IResampler sampler, float scale, Buffer weights) + private static void CalculateWeightsDown(int min, int max, int sourceMin, int sourceMax, float point, IResampler sampler, float scale, Buffer 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 = top; i <= bottom; i++, x++) + for (int x = 0, i = min; i <= max; i++, x++) { int index = i; - if (index < min) + if (index < sourceMin) { - index = min; + index = sourceMin; } - if (index > max) + if (index > sourceMax) { - index = max; + index = sourceMax; } float weight = sampler.GetValue((index - point) / scale); @@ -240,11 +252,20 @@ namespace SixLabors.ImageSharp.Processing.Processors } } + /// + /// 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 source bounds + /// The maximum source bounds + /// The transformed point dimension + /// The sampler + /// The collection of weights [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void CalculateWeightsScaleUp(int min, int max, float point, IResampler sampler, Buffer weights) + private static void CalculateWeightsScaleUp(int sourceMin, int sourceMax, float point, IResampler sampler, Buffer weights) { ref float weightsBaseRef = ref weights[0]; - for (int x = 0, i = min; i <= max; i++, x++) + for (int x = 0, i = sourceMin; i <= sourceMax; i++, x++) { float weight = sampler.GetValue(i - point); Unsafe.Add(ref weightsBaseRef, x) = weight; @@ -259,10 +280,9 @@ namespace SixLabors.ImageSharp.Processing.Processors { this.transformMatrix = this.GetTransformMatrix(); - // this.targetRectangle = ImageMaths.GetBoundingRectangle(sourceRectangle, this.transformMatrix); this.targetRectangle = Matrix3x2.Invert(this.transformMatrix, out Matrix3x2 sizeMatrix) - ? ImageMaths.GetBoundingRectangle(sourceRectangle, sizeMatrix) - : sourceRectangle; + ? ImageMaths.GetBoundingRectangle(sourceRectangle, sizeMatrix) + : sourceRectangle; } /// 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 1169d2eadc..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 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 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/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/RotateProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs index be49ec321b..e3fd36ce69 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs @@ -47,7 +47,9 @@ namespace SixLabors.ImageSharp.Processing.Processors /// protected override Matrix3x2 GetTransformMatrix() { - return Matrix3x2Extensions.CreateRotationDegrees(-this.Degrees, PointF.Empty); + Matrix3x2 matrix = Matrix3x2Extensions.CreateRotationDegrees(this.Degrees, PointF.Empty); + Matrix3x2.Invert(matrix, out matrix); + return matrix; } /// diff --git a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs index 8da8b1e57b..eb9f068db0 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs @@ -50,7 +50,9 @@ namespace SixLabors.ImageSharp.Processing.Processors /// protected override Matrix3x2 GetTransformMatrix() { - return Matrix3x2Extensions.CreateSkewDegrees(-this.DegreesX, -this.DegreesY, PointF.Empty); + Matrix3x2 matrix = Matrix3x2Extensions.CreateSkewDegrees(this.DegreesX, this.DegreesY, PointF.Empty); + Matrix3x2.Invert(matrix, out matrix); + return matrix; } } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs index 62bdc4a1eb..45e9d4dd5e 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs @@ -30,6 +30,7 @@ namespace SixLabors.ImageSharp.Processing.Processors public TransformProcessor(Matrix3x2 matrix, IResampler sampler) : base(sampler) { + Matrix3x2.Invert(matrix, out matrix); this.TransformMatrix = matrix; } 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/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeProfilingBenchmarks.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeProfilingBenchmarks.cs index 6dd6369802..98dbbadaba 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeProfilingBenchmarks.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeProfilingBenchmarks.cs @@ -40,11 +40,11 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms { 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/Transforms/TransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs index 30cb626155..4562d7de73 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs @@ -17,8 +17,8 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms public static readonly TheoryData TransformValues = new TheoryData { - { 20, 10, 50 }, - { -20, -10, 50 } + { 20, 10, 45 }, + { -20, -10, 45 } }; public static readonly List ResamplerNames @@ -52,13 +52,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms IResampler sampler = GetResampler(resamplerName); using (Image image = provider.GetImage()) { - Matrix3x2 rotate = Matrix3x2Extensions.CreateRotationDegrees(-z); - - // TODO, how does scale work? 2 means half just now, + Matrix3x2 rotate = Matrix3x2Extensions.CreateRotationDegrees(z); Matrix3x2 scale = Matrix3x2Extensions.CreateScale(new SizeF(2F, 2F)); - - image.Mutate(i => i.Transform(scale * rotate, sampler)); + image.Mutate(i => i.Transform(rotate * scale, sampler)); image.DebugSave(provider, string.Join("_", x, y, resamplerName)); } } From e0e0cf189d1053a4b2e887cfa11659d5d20b283f Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 4 Dec 2017 16:06:13 +1100 Subject: [PATCH 21/51] Span!! --- src/ImageSharp/Memory/Buffer{T}.cs | 2 +- .../Processors/Transforms/AffineProcessor.cs | 157 +++++++++--------- 2 files changed, 81 insertions(+), 78 deletions(-) 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/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index 767c6feed1..f2e74abd1b 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -103,92 +103,95 @@ namespace SixLabors.ImageSharp.Processing.Processors int xLength = (int)MathF.Ceiling((radius.X * 2) + 2); int yLength = (int)MathF.Ceiling((radius.Y * 2) + 2); - Parallel.For( - 0, - height, - configuration.ParallelOptions, - y => - { - Span destRow = destination.GetPixelRowSpan(y); - using (var yBuffer = new Buffer(yLength)) - using (var xBuffer = new Buffer(xLength)) - { - for (int x = 0; x < width; x++) + using (var yBuffer = new Buffer2D(yLength, height)) + using (var xBuffer = new Buffer2D(xLength, height)) + { + Parallel.For( + 0, + height, + configuration.ParallelOptions, + y => { - // 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); + Span destRow = destination.GetPixelRowSpan(y); + Span ySpan = yBuffer.GetRowSpan(y); + Span xSpan = xBuffer.GetRowSpan(y); - // Clamp sampling pixel radial extents to the source image edges - Vector2 maxXY = point + radius; - Vector2 minXY = point - radius; + 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); - var extents = new Vector4( - MathF.Ceiling(maxXY.X), - MathF.Ceiling(maxXY.Y), - MathF.Floor(minXY.X), - MathF.Floor(minXY.Y)); + // Clamp sampling pixel radial extents to the source image edges + Vector2 maxXY = point + radius; + Vector2 minXY = point - radius; - int right = (int)extents.X; - int bottom = (int)extents.Y; - int left = (int)extents.Z; - int top = (int)extents.W; + var extents = new Vector4( + MathF.Ceiling(maxXY.X), + MathF.Ceiling(maxXY.Y), + MathF.Floor(minXY.X), + MathF.Floor(minXY.Y)); - extents = Vector4.Clamp(extents, Vector4.Zero, maxSource); + int right = (int)extents.X; + int bottom = (int)extents.Y; + int left = (int)extents.Z; + int top = (int)extents.W; - int maxX = (int)extents.X; - int maxY = (int)extents.Y; - int minX = (int)extents.Z; - int minY = (int)extents.W; + extents = Vector4.Clamp(extents, Vector4.Zero, maxSource); - if (minX == maxX || minY == maxY) - { - continue; - } + int maxX = (int)extents.X; + int maxY = (int)extents.Y; + int minX = (int)extents.Z; + int minY = (int)extents.W; - // 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. - // I've optimized where I can but am always open to suggestions. - // - // Create and normalize the y-weights - if (yScale > 1) - { - CalculateWeightsDown(top, bottom, minY, maxY, point.Y, sampler, yScale, yBuffer); - } - else - { - CalculateWeightsScaleUp(minY, maxY, point.Y, sampler, yBuffer); - } + if (minX == maxX || minY == maxY) + { + continue; + } - // Create and normalize the x-weights - if (xScale > 1) - { - CalculateWeightsDown(left, right, minX, maxX, point.X, sampler, xScale, xBuffer); - } - else - { - CalculateWeightsScaleUp(minX, maxX, point.X, sampler, xBuffer); - } + // 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. + // + // Create and normalize the y-weights + if (yScale > 1) + { + CalculateWeightsDown(top, bottom, minY, maxY, point.Y, sampler, yScale, ySpan); + } + else + { + CalculateWeightsScaleUp(minY, maxY, point.Y, sampler, ySpan); + } - // Now multiply the results against the offsets - Vector4 sum = Vector4.Zero; - for (int yy = 0, j = minY; j <= maxY; j++, yy++) - { - float yWeight = yBuffer[yy]; + // Create and normalize the x-weights + if (xScale > 1) + { + CalculateWeightsDown(left, right, minX, maxX, point.X, sampler, xScale, xSpan); + } + else + { + CalculateWeightsScaleUp(minX, maxX, point.X, sampler, xSpan); + } - for (int xx = 0, i = minX; i <= maxX; i++, xx++) + // Now multiply the results against the offsets + Vector4 sum = Vector4.Zero; + for (int yy = 0, j = minY; j <= maxY; j++, yy++) { - float xWeight = xBuffer[xx]; - sum += source[i, j].ToVector4() * xWeight * yWeight; + float yWeight = ySpan[yy]; + + for (int xx = 0, i = minX; i <= maxX; i++, xx++) + { + float xWeight = xSpan[xx]; + sum += source[i, j].ToVector4() * xWeight * yWeight; + } } - } - ref TPixel dest = ref destRow[x]; - dest.PackFromVector4(sum); - } - } - }); + ref TPixel dest = ref destRow[x]; + dest.PackFromVector4(sum); + } + }); + } } /// @@ -206,7 +209,8 @@ namespace SixLabors.ImageSharp.Processing.Processors } /// - /// Calculated the weights for the given point. This method uses more samples than the upscaled version to ensure edge pixels are correctly rendered. + /// 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 @@ -218,7 +222,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// The transformed image scale relative to the source /// The collection of weights [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void CalculateWeightsDown(int min, int max, int sourceMin, int sourceMax, float point, IResampler sampler, float scale, Buffer weights) + private 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]; @@ -253,8 +257,7 @@ namespace SixLabors.ImageSharp.Processing.Processors } /// - /// 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. + /// Calculated the weights for the given point. /// /// The minimum source bounds /// The maximum source bounds @@ -262,7 +265,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// The sampler /// The collection of weights [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void CalculateWeightsScaleUp(int sourceMin, int sourceMax, float point, IResampler sampler, Buffer weights) + private 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++) From c2d874711d376ebb6bc1f022d752232b151fecfd Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 4 Dec 2017 17:04:06 +1100 Subject: [PATCH 22/51] Add new transform draw test --- .../ImageSharp.Tests/Drawing/DrawImageTest.cs | 20 +++++++++++++++++++ .../Processing/Transforms/TransformTests.cs | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) 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/Processing/Transforms/TransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs index 4562d7de73..8dad6de412 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs @@ -53,7 +53,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms using (Image image = provider.GetImage()) { Matrix3x2 rotate = Matrix3x2Extensions.CreateRotationDegrees(z); - Matrix3x2 scale = Matrix3x2Extensions.CreateScale(new SizeF(2F, 2F)); + Matrix3x2 scale = Matrix3x2Extensions.CreateScale(new SizeF(.5F, .5F)); image.Mutate(i => i.Transform(rotate * scale, sampler)); image.DebugSave(provider, string.Join("_", x, y, resamplerName)); From 531c41ece051d0575146dca175889321924496f5 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 7 Dec 2017 12:12:40 +1100 Subject: [PATCH 23/51] Add comments --- .../Processing/Processors/Transforms/RotateProcessor.cs | 1 + .../Processing/Processors/Transforms/SkewProcessor.cs | 1 + .../Processing/Processors/Transforms/TransformProcessor.cs | 2 ++ 3 files changed, 4 insertions(+) diff --git a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs index e3fd36ce69..b59918cea4 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs @@ -47,6 +47,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// protected override Matrix3x2 GetTransformMatrix() { + // Tansforms are inverted else the output is the opposite of the expected. Matrix3x2 matrix = Matrix3x2Extensions.CreateRotationDegrees(this.Degrees, PointF.Empty); Matrix3x2.Invert(matrix, out matrix); return matrix; diff --git a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs index eb9f068db0..321a6abe16 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs @@ -50,6 +50,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// protected override Matrix3x2 GetTransformMatrix() { + // Tansforms are inverted else the output is the opposite of the expected. Matrix3x2 matrix = Matrix3x2Extensions.CreateSkewDegrees(this.DegreesX, this.DegreesY, PointF.Empty); Matrix3x2.Invert(matrix, out matrix); return matrix; diff --git a/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs index 45e9d4dd5e..6ed8c31dd4 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs @@ -30,6 +30,8 @@ namespace SixLabors.ImageSharp.Processing.Processors public TransformProcessor(Matrix3x2 matrix, IResampler sampler) : base(sampler) { + + // Tansforms are inverted else the output is the opposite of the expected. Matrix3x2.Invert(matrix, out matrix); this.TransformMatrix = matrix; } From 2221912c62f6686ca75b9c55b0f2980cf2c2483b Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 7 Dec 2017 12:33:13 +1100 Subject: [PATCH 24/51] Fix build --- .../Processing/Processors/Transforms/TransformProcessor.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs index 6ed8c31dd4..e2dbed7655 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs @@ -30,7 +30,6 @@ namespace SixLabors.ImageSharp.Processing.Processors public TransformProcessor(Matrix3x2 matrix, IResampler sampler) : base(sampler) { - // Tansforms are inverted else the output is the opposite of the expected. Matrix3x2.Invert(matrix, out matrix); this.TransformMatrix = matrix; From 85049afb9429cf0f3eaba5c611fdb9fbcc3107ea Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 11 Dec 2017 10:19:07 +1100 Subject: [PATCH 25/51] No need for Vector2 here. --- src/ImageSharp/Common/Helpers/ImageMaths.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/ImageMaths.cs b/src/ImageSharp/Common/Helpers/ImageMaths.cs index 74d705d53a..554de10bec 100644 --- a/src/ImageSharp/Common/Helpers/ImageMaths.cs +++ b/src/ImageSharp/Common/Helpers/ImageMaths.cs @@ -162,11 +162,10 @@ namespace SixLabors.ImageSharp 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))); - var min = new Vector2(minX, minY); - var max = new Vector2(maxX, maxY); - Vector2 size = max - min; + float sizeX = maxX - minX; + float sizeY = maxY - minY; - return new Rectangle((int)MathF.Floor(minX), (int)MathF.Floor(minY), (int)MathF.Ceiling(size.X), (int)MathF.Ceiling(size.Y)); + return new Rectangle((int)MathF.Floor(minX), (int)MathF.Floor(minY), (int)MathF.Ceiling(sizeX), (int)MathF.Ceiling(sizeY)); } /// From 812fe6c884c627fcf8472aa08b48581d6005a0ff Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Mon, 11 Dec 2017 02:38:17 +0100 Subject: [PATCH 26/51] better TransformTests --- .../Processing/Transforms/TransformTests.cs | 82 ++++++++++++------- 1 file changed, 51 insertions(+), 31 deletions(-) diff --git a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs index 8dad6de412..129fe1be3b 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +// ReSharper disable InconsistentNaming namespace SixLabors.ImageSharp.Tests.Processing.Transforms { @@ -14,50 +15,69 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms public class TransformTests : FileTestBase { - public static readonly TheoryData TransformValues - = new TheoryData + public static readonly TheoryData TransformValues + = new TheoryData { - { 20, 10, 45 }, - { -20, -10, 45 } + { 45, 1, 1, 20, 10 }, + { 45, 1, 1, -20, -10 }, + { 45, 1.5f, 1.5f, 0, 0 }, + { 0, 2f, 1f, 0, 0 }, + { 0, 1f, 2f, 0, 0 }, }; - public static readonly List ResamplerNames - = new List + 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), + 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(TransformValues), DefaultPixelType)] - public void ImageShouldTransformWithSampler(TestImageProvider provider, float x, float y, float z) + [WithTestPatternImages(nameof(TransformValues), 100, 50, DefaultPixelType)] + 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); + Matrix3x2 translate = Matrix3x2Extensions.CreateTranslation(new PointF(tx, ty)); + Matrix3x2 scale = Matrix3x2Extensions.CreateScale(new SizeF(sx, sy)); + + image.Mutate(i => i.Transform(rotate * scale * translate)); + image.DebugSave(provider, $"R({angleDeg})_S({sx},{sy})_T({tx},{ty})"); + } + } - foreach (string resamplerName in ResamplerNames) + [Theory] + [WithTestPatternImages(nameof(ResamplerNames), 100, 200, DefaultPixelType)] + public void Transform_WithSampler(TestImageProvider provider, string resamplerName) + where TPixel : struct, IPixel + { + IResampler sampler = GetResampler(resamplerName); + using (Image image = provider.GetImage()) { - IResampler sampler = GetResampler(resamplerName); - using (Image image = provider.GetImage()) - { - Matrix3x2 rotate = Matrix3x2Extensions.CreateRotationDegrees(z); - Matrix3x2 scale = Matrix3x2Extensions.CreateScale(new SizeF(.5F, .5F)); + Matrix3x2 rotate = Matrix3x2Extensions.CreateRotationDegrees(45); + Matrix3x2 scale = Matrix3x2Extensions.CreateScale(new SizeF(.5F, .5F)); - image.Mutate(i => i.Transform(rotate * scale, sampler)); - image.DebugSave(provider, string.Join("_", x, y, resamplerName)); - } + image.Mutate(i => i.Transform(rotate * scale, sampler)); + image.DebugSave(provider, resamplerName); } } From 4e12604761082aaf3f96ae1a1cc5ca6f3a412450 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 11 Dec 2017 16:07:32 +1100 Subject: [PATCH 27/51] Fix Lancsoz banding + lighter skew tests --- src/ImageSharp/Common/Helpers/ImageMaths.cs | 6 ++--- .../Processors/Transforms/AffineProcessor.cs | 24 +++++++------------ .../Processors/Transforms/SkewTest.cs | 4 ++-- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/ImageMaths.cs b/src/ImageSharp/Common/Helpers/ImageMaths.cs index 554de10bec..a1c83415bf 100644 --- a/src/ImageSharp/Common/Helpers/ImageMaths.cs +++ b/src/ImageSharp/Common/Helpers/ImageMaths.cs @@ -162,10 +162,10 @@ namespace SixLabors.ImageSharp 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; - float sizeY = maxY - minY; + float sizeX = maxX - minX + .5F; + float sizeY = maxY - minY + .5F; - return new Rectangle((int)MathF.Floor(minX), (int)MathF.Floor(minY), (int)MathF.Ceiling(sizeX), (int)MathF.Ceiling(sizeY)); + return new Rectangle((int)(MathF.Ceiling(minX) - .5F), (int)(MathF.Ceiling(minY) - .5F), (int)MathF.Floor(sizeX), (int)MathF.Floor(sizeY)); } /// diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index f2e74abd1b..c9a4b2aeaf 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -127,10 +127,10 @@ namespace SixLabors.ImageSharp.Processing.Processors Vector2 minXY = point - radius; var extents = new Vector4( - MathF.Ceiling(maxXY.X), - MathF.Ceiling(maxXY.Y), - MathF.Floor(minXY.X), - MathF.Floor(minXY.Y)); + 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; @@ -154,23 +154,17 @@ namespace SixLabors.ImageSharp.Processing.Processors // since they can be at sub-pixel positions on both axis. // I've optimized where I can but am always open to suggestions. // - // Create and normalize the y-weights - if (yScale > 1) + // TODO: If we can somehow improve edge pixel handling that would be most beneficial. + // Currently the interpolated edge non-alpha components are altered which causes slight darkening of edges. + // Ideally the edge pixels should represent the nearest sample with altered alpha component only. + if (yScale > 1 && xScale > 1) { CalculateWeightsDown(top, bottom, minY, maxY, point.Y, sampler, yScale, ySpan); - } - else - { - CalculateWeightsScaleUp(minY, maxY, point.Y, sampler, ySpan); - } - - // Create and normalize the x-weights - if (xScale > 1) - { 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); } diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/SkewTest.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/SkewTest.cs index 8e57327be0..17bf3def26 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/SkewTest.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/SkewTest.cs @@ -42,7 +42,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms }; [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 { @@ -54,7 +54,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms } [Theory] - [WithFileCollection(nameof(DefaultFiles), nameof(SkewValues), DefaultPixelType)] + [WithTestPatternImages(nameof(SkewValues), 100, 50, DefaultPixelType)] public void ImageShouldSkewWithSampler(TestImageProvider provider, float x, float y) where TPixel : struct, IPixel { From f991b8f7c01ec7abc5b4ea8ab65f3961bd0e4115 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 12 Dec 2017 12:10:15 +0100 Subject: [PATCH 28/51] failing test for edge artifacts, minimalistic RotateTests, better Rgba32.ToString() --- src/ImageSharp/PixelFormats/Rgb24.cs | 6 ++ src/ImageSharp/PixelFormats/Rgba32.cs | 2 +- .../Processors/Transforms/RotateTests.cs | 62 ++------------- .../Processing/Transforms/TransformTests.cs | 79 +++++++++++++++---- 4 files changed, 77 insertions(+), 72 deletions(-) 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/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs index ca29009bc7..b5220ea948 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateTests.cs @@ -13,11 +13,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms 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 @@ -28,31 +27,11 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms RotateType.Rotate180, RotateType.Rotate270 }; - - public static readonly TheoryData ResamplerNames - = new TheoryData - { - nameof(KnownResamplers.Bicubic), - nameof(KnownResamplers.Box), - nameof(KnownResamplers.CatmullRom), - nameof(KnownResamplers.Hermite), - nameof(KnownResamplers.Lanczos2), - nameof(KnownResamplers.Lanczos3), - nameof(KnownResamplers.Lanczos5), - nameof(KnownResamplers.Lanczos8), - nameof(KnownResamplers.MitchellNetravali), - nameof(KnownResamplers.NearestNeighbor), - nameof(KnownResamplers.Robidoux), - nameof(KnownResamplers.RobidouxSharp), - nameof(KnownResamplers.Spline), - nameof(KnownResamplers.Triangle), - nameof(KnownResamplers.Welch), - }; - + [Theory] - [WithTestPatternImages(nameof(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()) @@ -61,22 +40,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms image.DebugSave(provider, value); } } - - [Theory] - [WithTestPatternImages(nameof(ResamplerNames), 100, 50, DefaultPixelType)] - [WithTestPatternImages(nameof(ResamplerNames), 50, 100, DefaultPixelType)] - public void RotateWithSampler(TestImageProvider provider, string resamplerName) - where TPixel : struct, IPixel - { - IResampler resampler = GetResampler(resamplerName); - - using (Image image = provider.GetImage()) - { - image.Mutate(x => x.Rotate(50, resampler)); - image.DebugSave(provider, resamplerName); - } - } - + [Theory] [WithTestPatternImages(nameof(RotateEnumValues), 100, 50, DefaultPixelType)] [WithTestPatternImages(nameof(RotateEnumValues), 50, 100, DefaultPixelType)] @@ -89,17 +53,5 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms image.DebugSave(provider, value); } } - - 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/TransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs index 129fe1be3b..c4a9437d51 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs @@ -1,20 +1,19 @@ using System; -using System.Collections.Generic; +using System.Numerics; +using System.Reflection; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; +using SixLabors.Primitives; +using Xunit; +using Xunit.Abstractions; // ReSharper disable InconsistentNaming namespace SixLabors.ImageSharp.Tests.Processing.Transforms { - using System.Numerics; - using System.Reflection; - - using SixLabors.ImageSharp.PixelFormats; - using SixLabors.ImageSharp.Processing; - using SixLabors.Primitives; - - using Xunit; - - public class TransformTests : FileTestBase + public class TransformTests { + private readonly ITestOutputHelper Output; + public static readonly TheoryData TransformValues = new TheoryData { @@ -45,8 +44,37 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms nameof(KnownResamplers.Welch), }; + public TransformTests(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(5, 5, 255, 255, 255, 255, PixelTypes.Rgba32, nameof(KnownResamplers.NearestNeighbor))] + [WithSolidFilledImages(5, 5, 255, 255, 255, 255, PixelTypes.Rgba32, nameof(KnownResamplers.Triangle))] + [WithSolidFilledImages(5, 5, 255, 255, 255, 255, PixelTypes.Rgba32, nameof(KnownResamplers.Bicubic))] + [WithSolidFilledImages(5, 5, 255, 255, 255, 255, PixelTypes.Rgba32, nameof(KnownResamplers.Lanczos8))] + public void Transform_DoesNotCreateEdgeArtifacts(TestImageProvider provider, string resamplerName) + where TPixel : struct, IPixel + { + IResampler resampler = GetResampler(resamplerName); + using (Image image = provider.GetImage()) + { + // TODO: Modify this matrix if we change our origin-concept + var rotate = Matrix3x2.CreateRotation((float)Math.PI/4f); + + image.Mutate(c => c.Transform(rotate, resampler)); + image.DebugSave(provider, resamplerName); + + VerifyAllPixelsAreWhiteOrTransparent(image); + } + } + [Theory] - [WithTestPatternImages(nameof(TransformValues), 100, 50, DefaultPixelType)] + [WithTestPatternImages(nameof(TransformValues), 100, 50, PixelTypes.Rgba32)] public void Transform_RotateScaleTranslate( TestImageProvider provider, float angleDeg, @@ -57,16 +85,19 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms using (Image image = provider.GetImage()) { Matrix3x2 rotate = Matrix3x2Extensions.CreateRotationDegrees(angleDeg); - Matrix3x2 translate = Matrix3x2Extensions.CreateTranslation(new PointF(tx, ty)); - Matrix3x2 scale = Matrix3x2Extensions.CreateScale(new SizeF(sx, sy)); + var translate = Matrix3x2.CreateTranslation(tx, ty); + var scale = Matrix3x2.CreateScale(sx, sy); + Matrix3x2 m = rotate * scale * translate; + + this.Output.WriteLine(m.ToString()); - image.Mutate(i => i.Transform(rotate * scale * translate)); + image.Mutate(i => i.Transform(m)); image.DebugSave(provider, $"R({angleDeg})_S({sx},{sy})_T({tx},{ty})"); } } [Theory] - [WithTestPatternImages(nameof(ResamplerNames), 100, 200, DefaultPixelType)] + [WithTestPatternImages(nameof(ResamplerNames), 100, 200, PixelTypes.Rgba32)] public void Transform_WithSampler(TestImageProvider provider, string resamplerName) where TPixel : struct, IPixel { @@ -92,5 +123,21 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms 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); + } + } } } \ No newline at end of file From 6d08237859bd33187f7cf628cd904ccacd1d80ca Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 13 Dec 2017 00:06:10 +1100 Subject: [PATCH 29/51] Refactor transforms to allow freeform + rectangle --- .../Processors/Transforms/AffineProcessor.cs | 84 ++++++++++++------- .../Transforms/CenteredAffineProcessor.cs | 31 +++++++ .../Processors/Transforms/RotateProcessor.cs | 52 +++++++----- .../Processors/Transforms/SkewProcessor.cs | 14 +--- .../Transforms/TransformProcessor.cs | 18 ++-- .../Processing/Transforms/Transform.cs | 16 +++- 6 files changed, 141 insertions(+), 74 deletions(-) create mode 100644 src/ImageSharp/Processing/Processors/Transforms/CenteredAffineProcessor.cs diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index c9a4b2aeaf..b331201cc9 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -10,6 +10,7 @@ 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; @@ -23,32 +24,53 @@ namespace SixLabors.ImageSharp.Processing.Processors where TPixel : struct, IPixel { private Rectangle targetRectangle; - private Matrix3x2 transformMatrix; /// /// Initializes a new instance of the class. /// + /// The transform matrix /// The sampler to perform the transform operation. - protected AffineProcessor(IResampler sampler) + protected AffineProcessor(Matrix3x2 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. + protected AffineProcessor(Matrix3x2 matrix, IResampler sampler, Rectangle rectangle) + { + // Tansforms are inverted else the output is the opposite of the expected. + Matrix3x2.Invert(matrix, out matrix); + this.TransformMatrix = matrix; + this.Sampler = sampler; + + this.targetRectangle = rectangle; } /// - /// Gets the sampler to perform interpolation of the transform operation. + /// Gets the matrix used to supply the affine transform /// - public IResampler Sampler { get; } + public Matrix3x2 TransformMatrix { get; } /// - /// Returns the processing matrix used for transforming the image. + /// Gets the sampler to perform interpolation of the transform operation. /// - /// The - protected abstract Matrix3x2 GetTransformMatrix(); + public IResampler Sampler { get; } /// protected override Image CreateDestination(Image source, Rectangle sourceRectangle) { - this.ResizeCanvas(sourceRectangle); + if (this.targetRectangle == Rectangle.Empty) + { + this.targetRectangle = Matrix3x2.Invert(this.TransformMatrix, out Matrix3x2 sizeMatrix) + ? ImageMaths.GetBoundingRectangle(sourceRectangle, sizeMatrix) + : sourceRectangle; + } // We will always be creating the clone even for mutate because we may need to resize the canvas IEnumerable> frames = @@ -66,7 +88,7 @@ namespace SixLabors.ImageSharp.Processing.Processors Rectangle sourceBounds = source.Bounds(); // Since could potentially be resizing the canvas we need to re-center the matrix - Matrix3x2 matrix = this.GetCenteredMatrix(source); + Matrix3x2 matrix = this.GetProcessingMatrix(sourceBounds, this.targetRectangle); if (this.Sampler is NearestNeighborResampler) { @@ -188,18 +210,37 @@ 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); + } + } + /// - /// Gets a transform matrix adjusted to center upon the target image bounds. + /// Gets a transform matrix adjusted for final processing based upon the target image bounds. /// - /// The source image. + /// The source image bounds. + /// The destination image bounds. /// /// The . /// - protected Matrix3x2 GetCenteredMatrix(ImageFrame source) + protected virtual Matrix3x2 GetProcessingMatrix(Rectangle sourceRectangle, Rectangle destinationRectangle) { - var translationToTargetCenter = Matrix3x2.CreateTranslation(-this.targetRectangle.Width * .5F, -this.targetRectangle.Height * .5F); - var translateToSourceCenter = Matrix3x2.CreateTranslation(source.Width * .5F, source.Height * .5F); - return translationToTargetCenter * this.transformMatrix * translateToSourceCenter; + return this.TransformMatrix; } /// @@ -269,19 +310,6 @@ namespace SixLabors.ImageSharp.Processing.Processors } } - /// - /// Creates a new target canvas to contain the results of the matrix transform. - /// - /// The source rectangle. - private void ResizeCanvas(Rectangle sourceRectangle) - { - this.transformMatrix = this.GetTransformMatrix(); - - this.targetRectangle = Matrix3x2.Invert(this.transformMatrix, out Matrix3x2 sizeMatrix) - ? ImageMaths.GetBoundingRectangle(sourceRectangle, sizeMatrix) - : sourceRectangle; - } - /// /// Calculates the sampling radius for the current sampler /// diff --git a/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineProcessor.cs new file mode 100644 index 0000000000..5f03f94e27 --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineProcessor.cs @@ -0,0 +1,31 @@ +// 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 +{ + internal abstract class CenteredAffineProcessor : AffineProcessor + where TPixel : struct, IPixel + { + /// + /// Initializes a new instance of the class. + /// + /// The transform matrix + /// The sampler to perform the transform operation. + protected CenteredAffineProcessor(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; + } + } +} \ 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 b59918cea4..7af1c68f12 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs @@ -2,7 +2,6 @@ // 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; @@ -16,15 +15,15 @@ namespace SixLabors.ImageSharp.Processing.Processors /// Provides methods that allow the rotating of images. /// /// The pixel format. - internal class RotateProcessor : AffineProcessor + internal class RotateProcessor : CenteredAffineProcessor where TPixel : struct, IPixel { /// /// Initializes a new instance of the class. /// - /// The angle of rotation in degrees. - public RotateProcessor(float angle) - : this(angle, KnownResamplers.NearestNeighbor) + /// The angle of rotation in degrees. + public RotateProcessor(float degrees) + : this(degrees, KnownResamplers.NearestNeighbor) { } @@ -34,7 +33,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// The angle of rotation in degrees. /// The sampler to perform the rotating operation. public RotateProcessor(float degrees, IResampler sampler) - : base(sampler) + : base(Matrix3x2Extensions.CreateRotationDegrees(degrees, PointF.Empty), sampler) { this.Degrees = degrees; } @@ -44,15 +43,6 @@ namespace SixLabors.ImageSharp.Processing.Processors /// public float Degrees { get; } - /// - protected override Matrix3x2 GetTransformMatrix() - { - // Tansforms are inverted else the output is the opposite of the expected. - Matrix3x2 matrix = Matrix3x2Extensions.CreateRotationDegrees(this.Degrees, PointF.Empty); - Matrix3x2.Invert(matrix, out matrix); - return matrix; - } - /// protected override void OnApply(ImageFrame source, ImageFrame destination, Rectangle sourceRectangle, Configuration configuration) { @@ -73,7 +63,7 @@ namespace SixLabors.ImageSharp.Processing.Processors return; } - if (MathF.Abs(this.Degrees) < Constants.Epsilon) + if (MathF.Abs(WrapDegrees(this.Degrees)) < Constants.Epsilon) { // No need to do anything so return. return; @@ -81,11 +71,24 @@ namespace SixLabors.ImageSharp.Processing.Processors profile.RemoveValue(ExifTag.Orientation); - if (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; + + if (degrees < 0) { - profile.SetValue(ExifTag.PixelXDimension, source.Width); - profile.SetValue(ExifTag.PixelYDimension, source.Height); + degrees += 360; } + + return degrees; } /// @@ -99,26 +102,29 @@ namespace SixLabors.ImageSharp.Processing.Processors /// private bool OptimizedApply(ImageFrame source, ImageFrame destination, Configuration configuration) { - if (MathF.Abs(this.Degrees) < 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) { // The destination will be blank here so copy all the pixel data over source.GetPixelSpan().CopyTo(destination.GetPixelSpan()); return true; } - if (MathF.Abs(this.Degrees - 90) < Constants.Epsilon) + if (MathF.Abs(degrees - 90) < Constants.Epsilon) { this.Rotate90(source, destination, configuration); return true; } - if (MathF.Abs(this.Degrees - 180) < Constants.Epsilon) + if (MathF.Abs(degrees - 180) < Constants.Epsilon) { this.Rotate180(source, destination, configuration); return true; } - if (MathF.Abs(this.Degrees - 270) < Constants.Epsilon) + if (MathF.Abs(degrees - 270) < Constants.Epsilon) { this.Rotate270(source, destination, configuration); return true; diff --git a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs index 321a6abe16..07f082838c 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Numerics; using SixLabors.ImageSharp.PixelFormats; using SixLabors.Primitives; @@ -11,7 +10,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// Provides methods that allow the skewing of images. /// /// The pixel format. - internal class SkewProcessor : AffineProcessor + internal class SkewProcessor : CenteredAffineProcessor where TPixel : struct, IPixel { /// @@ -31,7 +30,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// 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(sampler) + : base(Matrix3x2Extensions.CreateSkewDegrees(degreesX, degreesY, PointF.Empty), sampler) { this.DegreesX = degreesX; this.DegreesY = degreesY; @@ -46,14 +45,5 @@ namespace SixLabors.ImageSharp.Processing.Processors /// Gets the angle of rotation along the y-axis in degrees. /// public float DegreesY { get; } - - /// - protected override Matrix3x2 GetTransformMatrix() - { - // Tansforms are inverted else the output is the opposite of the expected. - Matrix3x2 matrix = Matrix3x2Extensions.CreateSkewDegrees(this.DegreesX, this.DegreesY, PointF.Empty); - Matrix3x2.Invert(matrix, out matrix); - return matrix; - } } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs index e2dbed7655..577691cbb5 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs @@ -3,6 +3,7 @@ using System.Numerics; using SixLabors.ImageSharp.PixelFormats; +using SixLabors.Primitives; namespace SixLabors.ImageSharp.Processing.Processors { @@ -28,22 +29,19 @@ namespace SixLabors.ImageSharp.Processing.Processors /// The transformation matrix /// The sampler to perform the transform operation. public TransformProcessor(Matrix3x2 matrix, IResampler sampler) - : base(sampler) + : base(matrix, sampler) { - // Tansforms are inverted else the output is the opposite of the expected. - Matrix3x2.Invert(matrix, out matrix); - this.TransformMatrix = matrix; } /// - /// Gets the transform matrix + /// Initializes a new instance of the class. /// - public Matrix3x2 TransformMatrix { get; } - - /// - protected override Matrix3x2 GetTransformMatrix() + /// The transform matrix + /// The sampler to perform the transform operation. + /// The rectangle to constrain the transformed image to. + public TransformProcessor(Matrix3x2 matrix, IResampler sampler, Rectangle rectangle) + : base(matrix, sampler, rectangle) { - return this.TransformMatrix; } } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Transforms/Transform.cs b/src/ImageSharp/Processing/Transforms/Transform.cs index f3478e32d5..74f91fa701 100644 --- a/src/ImageSharp/Processing/Transforms/Transform.cs +++ b/src/ImageSharp/Processing/Transforms/Transform.cs @@ -5,6 +5,7 @@ using System.Numerics; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors; +using SixLabors.Primitives; namespace SixLabors.ImageSharp { @@ -34,6 +35,19 @@ namespace SixLabors.ImageSharp /// The public static IImageProcessingContext Transform(this IImageProcessingContext source, Matrix3x2 matrix, IResampler sampler) where TPixel : struct, IPixel - => source.ApplyProcessor(new TransformProcessor(matrix, sampler)); + => Transform(source, matrix, sampler, Rectangle.Empty); + + /// + /// Transforms an image by the given matrix using the specified sampling algorithm. + /// + /// The pixel format. + /// The image to skew. + /// The transformation matrix. + /// The to perform the resampling. + /// The rectangle to constrain the transformed image to. + /// The + public static IImageProcessingContext Transform(this IImageProcessingContext source, Matrix3x2 matrix, IResampler sampler, Rectangle rectangle) + where TPixel : struct, IPixel + => source.ApplyProcessor(new TransformProcessor(matrix, sampler, rectangle)); } } \ No newline at end of file From 8503ea580c0543d36dd428619e91f273fb1fe5c8 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 13 Dec 2017 01:14:58 +1100 Subject: [PATCH 30/51] Premultiply FTW! --- .../Processors/Transforms/AffineProcessor.cs | 6 ++++-- .../Processing/Transforms/TransformTests.cs | 20 ++++++++++--------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index b331201cc9..6fdf061b86 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -199,12 +199,14 @@ namespace SixLabors.ImageSharp.Processing.Processors for (int xx = 0, i = minX; i <= maxX; i++, xx++) { float xWeight = xSpan[xx]; - sum += source[i, j].ToVector4() * xWeight * yWeight; + var vector = source[i, j].ToVector4(); + 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]; - dest.PackFromVector4(sum); + dest.PackFromVector4(new Vector4(new Vector3(sum.X, sum.Y, sum.Z) / sum.W, sum.W)); } }); } diff --git a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs index c4a9437d51..b2e183de79 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs @@ -17,14 +17,14 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms public static readonly TheoryData TransformValues = new TheoryData { - { 45, 1, 1, 20, 10 }, - { 45, 1, 1, -20, -10 }, - { 45, 1.5f, 1.5f, 0, 0 }, + { 50, 1, 1, 20, 10 }, + { 50, 1, 1, -20, -10 }, + { 50, 1.5f, 1.5f, 0, 0 }, { 0, 2f, 1f, 0, 0 }, { 0, 1f, 2f, 0, 0 }, }; - public static readonly TheoryData ResamplerNames = + public static readonly TheoryData ResamplerNames = new TheoryData { nameof(KnownResamplers.Bicubic), @@ -64,7 +64,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms using (Image image = provider.GetImage()) { // TODO: Modify this matrix if we change our origin-concept - var rotate = Matrix3x2.CreateRotation((float)Math.PI/4f); + var rotate = Matrix3x2.CreateRotation((float)Math.PI / 4f); image.Mutate(c => c.Transform(rotate, resampler)); image.DebugSave(provider, resamplerName); @@ -88,9 +88,9 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms var translate = Matrix3x2.CreateTranslation(tx, ty); var scale = Matrix3x2.CreateScale(sx, sy); Matrix3x2 m = rotate * scale * translate; - + this.Output.WriteLine(m.ToString()); - + image.Mutate(i => i.Transform(m)); image.DebugSave(provider, $"R({angleDeg})_S({sx},{sy})_T({tx},{ty})"); } @@ -104,10 +104,12 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms IResampler sampler = GetResampler(resamplerName); using (Image image = provider.GetImage()) { - Matrix3x2 rotate = Matrix3x2Extensions.CreateRotationDegrees(45); + Matrix3x2 rotate = Matrix3x2Extensions.CreateRotationDegrees(50); Matrix3x2 scale = Matrix3x2Extensions.CreateScale(new SizeF(.5F, .5F)); + var translate = Matrix3x2.CreateTranslation(75, 0); + - image.Mutate(i => i.Transform(rotate * scale, sampler)); + image.Mutate(i => i.Transform(rotate * scale * translate, sampler)); image.DebugSave(provider, resamplerName); } } From 95be7e6aa700c1d8b04342c36ae28376aa720d7d Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 13 Dec 2017 10:22:25 +1100 Subject: [PATCH 31/51] Cleanup + update tests --- .../Processors/Transforms/AffineProcessor.cs | 8 +++---- .../Processors/Transforms/ResizeProcessor.cs | 23 ++++++++++++++++++- .../Processing/Transforms/TransformTests.cs | 6 ++--- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index 6fdf061b86..ad8221b88b 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -175,10 +175,6 @@ namespace SixLabors.ImageSharp.Processing.Processors // 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. - // - // TODO: If we can somehow improve edge pixel handling that would be most beneficial. - // Currently the interpolated edge non-alpha components are altered which causes slight darkening of edges. - // Ideally the edge pixels should represent the nearest sample with altered alpha component only. if (yScale > 1 && xScale > 1) { CalculateWeightsDown(top, bottom, minY, maxY, point.Y, sampler, yScale, ySpan); @@ -200,12 +196,16 @@ namespace SixLabors.ImageSharp.Processing.Processors { 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)); } }); diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs index ecfcc7dd20..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; @@ -62,7 +63,7 @@ namespace SixLabors.ImageSharp.Processing.Processors } /// - 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) @@ -190,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/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs index b2e183de79..54a3af36fd 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs @@ -63,10 +63,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms IResampler resampler = GetResampler(resamplerName); using (Image image = provider.GetImage()) { - // TODO: Modify this matrix if we change our origin-concept - var rotate = Matrix3x2.CreateRotation((float)Math.PI / 4f); + var rotate = Matrix3x2.CreateRotation((float)Math.PI / 4F, new Vector2(5 / 2F, 5 / 2F)); + var translate = Matrix3x2.CreateTranslation((7 - 5) / 2F, (7 - 5) / 2F); - image.Mutate(c => c.Transform(rotate, resampler)); + image.Mutate(c => c.Transform(rotate * translate, resampler)); image.DebugSave(provider, resamplerName); VerifyAllPixelsAreWhiteOrTransparent(image); From 201e4f43bddf180c87e91d80933823d162795270 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Wed, 20 Dec 2017 18:35:30 +0100 Subject: [PATCH 32/51] more/better TransformTests --- .../Processing/Transforms/TransformTests.cs | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs index 54a3af36fd..59a16eb78d 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs @@ -10,6 +10,8 @@ using Xunit.Abstractions; namespace SixLabors.ImageSharp.Tests.Processing.Transforms { + using SixLabors.ImageSharp.Helpers; + public class TransformTests { private readonly ITestOutputHelper Output; @@ -17,7 +19,11 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms 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 }, { 0, 2f, 1f, 0, 0 }, @@ -75,7 +81,30 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms [Theory] [WithTestPatternImages(nameof(TransformValues), 100, 50, PixelTypes.Rgba32)] - public void Transform_RotateScaleTranslate( + public void Transform_RotateScaleTranslate_AutoDestRectangle( + 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)); + image.DebugSave(provider, $"R({angleDeg})_S({sx},{sy})_T({tx},{ty})"); + } + } + + [Theory] + [WithTestPatternImages(nameof(TransformValues), 100, 50, PixelTypes.Rgba32)] + public void Transform_RotateScaleTranslate_SameDestRectangle( TestImageProvider provider, float angleDeg, float sx, float sy, @@ -89,13 +118,15 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms var scale = Matrix3x2.CreateScale(sx, sy); Matrix3x2 m = rotate * scale * translate; - this.Output.WriteLine(m.ToString()); + this.PrintMatrix(m); - image.Mutate(i => i.Transform(m)); + Rectangle destBounds = image.Bounds(); + image.Mutate(i => i.Transform(m, KnownResamplers.Bicubic, destBounds)); image.DebugSave(provider, $"R({angleDeg})_S({sx},{sy})_T({tx},{ty})"); } } + [Theory] [WithTestPatternImages(nameof(ResamplerNames), 100, 200, PixelTypes.Rgba32)] public void Transform_WithSampler(TestImageProvider provider, string resamplerName) @@ -141,5 +172,11 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms 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 From 02f97c93b3443d3964ca505aff68d1cbab80fae0 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 21 Dec 2017 12:21:30 +1100 Subject: [PATCH 33/51] Update default behaviours --- src/ImageSharp/Common/Helpers/ImageMaths.cs | 29 ------------- .../Processors/Transforms/AffineProcessor.cs | 16 +++++-- .../Transforms/CenteredAffineProcessor.cs | 8 ++++ .../Processors/Transforms/RotateProcessor.cs | 4 +- .../Processors/Transforms/SkewProcessor.cs | 2 +- .../Processing/Transforms/Rotate.cs | 2 +- src/ImageSharp/Processing/Transforms/Skew.cs | 2 +- .../Processing/Transforms/TransformHelpers.cs | 43 +++++++++++++++++++ .../Processing/Transforms/TransformTests.cs | 9 +++- 9 files changed, 76 insertions(+), 39 deletions(-) create mode 100644 src/ImageSharp/Processing/Transforms/TransformHelpers.cs diff --git a/src/ImageSharp/Common/Helpers/ImageMaths.cs b/src/ImageSharp/Common/Helpers/ImageMaths.cs index a1c83415bf..75c9190d24 100644 --- a/src/ImageSharp/Common/Helpers/ImageMaths.cs +++ b/src/ImageSharp/Common/Helpers/ImageMaths.cs @@ -139,35 +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 . - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Rectangle GetBoundingRectangle(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); - - // Find the minimum and maximum "corners" based on the ones above - 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)); - } - /// /// Finds the bounding rectangle based on the first instance of any color component other /// than the given one. diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs index ad8221b88b..59b8442639 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs @@ -67,9 +67,7 @@ namespace SixLabors.ImageSharp.Processing.Processors { if (this.targetRectangle == Rectangle.Empty) { - this.targetRectangle = Matrix3x2.Invert(this.TransformMatrix, out Matrix3x2 sizeMatrix) - ? ImageMaths.GetBoundingRectangle(sourceRectangle, sizeMatrix) - : sourceRectangle; + 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 @@ -148,6 +146,7 @@ namespace SixLabors.ImageSharp.Processing.Processors 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), @@ -245,6 +244,17 @@ namespace SixLabors.ImageSharp.Processing.Processors 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, Matrix3x2 matrix) + { + return sourceRectangle; + } + /// /// Calculated the weights for the given point. /// This method uses more samples than the upscaled version to ensure edge pixels are correctly rendered. diff --git a/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineProcessor.cs index 5f03f94e27..5631af3aa0 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineProcessor.cs @@ -27,5 +27,13 @@ namespace SixLabors.ImageSharp.Processing.Processors var translateToSourceCenter = Matrix3x2.CreateTranslation(sourceRectangle.Width * .5F, sourceRectangle.Height * .5F); return translationToTargetCenter * this.TransformMatrix * translateToSourceCenter; } + + /// + protected override Rectangle GetTransformedBoundingRectangle(Rectangle sourceRectangle, Matrix3x2 matrix) + { + return Matrix3x2.Invert(this.TransformMatrix, out Matrix3x2 sizeMatrix) + ? TransformHelpers.GetTransformedBoundingRectangle(sourceRectangle, sizeMatrix) + : sourceRectangle; + } } } \ 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 7af1c68f12..fa47dadaaa 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs @@ -23,7 +23,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// /// The angle of rotation in degrees. public RotateProcessor(float degrees) - : this(degrees, KnownResamplers.NearestNeighbor) + : this(degrees, KnownResamplers.Bicubic) { } @@ -83,7 +83,7 @@ namespace SixLabors.ImageSharp.Processing.Processors { degrees = degrees % 360; - if (degrees < 0) + while (degrees < 0) { degrees += 360; } diff --git a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs index 07f082838c..b123a309be 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs @@ -19,7 +19,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// 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.NearestNeighbor) + : this(degreesX, degreesY, KnownResamplers.Bicubic) { } diff --git a/src/ImageSharp/Processing/Transforms/Rotate.cs b/src/ImageSharp/Processing/Transforms/Rotate.cs index e9ae4fcf32..69fb7ebf0c 100644 --- a/src/ImageSharp/Processing/Transforms/Rotate.cs +++ b/src/ImageSharp/Processing/Transforms/Rotate.cs @@ -32,7 +32,7 @@ namespace SixLabors.ImageSharp /// The public static IImageProcessingContext Rotate(this IImageProcessingContext source, float degrees) where TPixel : struct, IPixel - => Rotate(source, degrees, KnownResamplers.NearestNeighbor); + => Rotate(source, degrees, KnownResamplers.Bicubic); /// /// Rotates an image by the given angle in degrees using the specified sampling algorithm. diff --git a/src/ImageSharp/Processing/Transforms/Skew.cs b/src/ImageSharp/Processing/Transforms/Skew.cs index b7a431cce4..0613a690b8 100644 --- a/src/ImageSharp/Processing/Transforms/Skew.cs +++ b/src/ImageSharp/Processing/Transforms/Skew.cs @@ -22,7 +22,7 @@ namespace SixLabors.ImageSharp /// The public static IImageProcessingContext Skew(this IImageProcessingContext source, float degreesX, float degreesY) where TPixel : struct, IPixel - => Skew(source, degreesX, degreesY, KnownResamplers.NearestNeighbor); + => Skew(source, degreesX, degreesY, KnownResamplers.Bicubic); /// /// Skews an image by the given angles in degrees using the specified sampling algorithm. diff --git a/src/ImageSharp/Processing/Transforms/TransformHelpers.cs b/src/ImageSharp/Processing/Transforms/TransformHelpers.cs new file mode 100644 index 0000000000..6f9560a9fa --- /dev/null +++ b/src/ImageSharp/Processing/Transforms/TransformHelpers.cs @@ -0,0 +1,43 @@ +// 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 transforms + /// + public 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); + + // Find the minimum and maximum "corners" based on the ones above + 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/Processing/Transforms/TransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs index 59a16eb78d..79a1fd4454 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs @@ -72,7 +72,12 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms var rotate = Matrix3x2.CreateRotation((float)Math.PI / 4F, new Vector2(5 / 2F, 5 / 2F)); var translate = Matrix3x2.CreateTranslation((7 - 5) / 2F, (7 - 5) / 2F); - image.Mutate(c => c.Transform(rotate * translate, resampler)); + 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); @@ -96,7 +101,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms Matrix3x2 m = rotate * scale * translate; this.PrintMatrix(m); - + image.Mutate(i => i.Transform(m, KnownResamplers.Bicubic)); image.DebugSave(provider, $"R({angleDeg})_S({sx},{sy})_T({tx},{ty})"); } From 5542840459261feb084571965432b2775f9e88a1 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 21 Dec 2017 12:23:08 +1100 Subject: [PATCH 34/51] Use Bicubic for TransformProcessor --- .../Processing/Processors/Transforms/TransformProcessor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs index 577691cbb5..140fd7ec6a 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs @@ -19,7 +19,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// /// The transformation matrix public TransformProcessor(Matrix3x2 matrix) - : this(matrix, KnownResamplers.NearestNeighbor) + : this(matrix, KnownResamplers.Bicubic) { } From 0b9e0a51e0f474abc60b2fcaf2fc179e477e916a Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 21 Dec 2017 02:45:39 +0100 Subject: [PATCH 35/51] Transform_RotateScale_ManuallyCentered --- .../Processing/Transforms/TransformTests.cs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs index 59a16eb78d..cfc59870ea 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs @@ -26,6 +26,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms { 0, 1, 1, -20, -10 }, { 50, 1, 1, -20, -10 }, { 50, 1.5f, 1.5f, 0, 0 }, + { 50, 1.1F, 1.2F, 20, 10 }, { 0, 2f, 1f, 0, 0 }, { 0, 1f, 2f, 0, 0 }, }; @@ -126,6 +127,28 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms } } + [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 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); + + Rectangle destBounds = image.Bounds(); + image.Mutate(i => i.Transform(m, KnownResamplers.Bicubic, destBounds)); + image.DebugSave(provider, $"R({angleDeg})_S({s})"); + } + } [Theory] [WithTestPatternImages(nameof(ResamplerNames), 100, 200, PixelTypes.Rgba32)] From 53534346a57fa9f5beef893c1048c85c877d4107 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 21 Dec 2017 02:53:12 +0100 Subject: [PATCH 36/51] drop unnecessary "AutoDestRectangle" tests --- .../Processing/Transforms/TransformTests.cs | 28 ++----------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs index d7220f72ca..9cb7890fe2 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs @@ -87,7 +87,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms [Theory] [WithTestPatternImages(nameof(TransformValues), 100, 50, PixelTypes.Rgba32)] - public void Transform_RotateScaleTranslate_AutoDestRectangle( + public void Transform_RotateScaleTranslate( TestImageProvider provider, float angleDeg, float sx, float sy, @@ -107,31 +107,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms image.DebugSave(provider, $"R({angleDeg})_S({sx},{sy})_T({tx},{ty})"); } } - - [Theory] - [WithTestPatternImages(nameof(TransformValues), 100, 50, PixelTypes.Rgba32)] - public void Transform_RotateScaleTranslate_SameDestRectangle( - 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); - - Rectangle destBounds = image.Bounds(); - image.Mutate(i => i.Transform(m, KnownResamplers.Bicubic, destBounds)); - image.DebugSave(provider, $"R({angleDeg})_S({sx},{sy})_T({tx},{ty})"); - } - } - + [Theory] [WithTestPatternImages(96, 96, PixelTypes.Rgba32, 50, 0.8f)] public void Transform_RotateScale_ManuallyCentered(TestImageProvider provider, float angleDeg, float s) From a05853de39c2e4708f45bca166c1515979336f16 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 23 Dec 2017 23:56:23 +0100 Subject: [PATCH 37/51] Transform_IntoRectangle --- .../Processing/Transforms/TransformTests.cs | 72 +++++++++++++---- .../WithSolidFilledImagesAttribute.cs | 80 ++++++++++++++++++- .../WithTestPatternImageAttribute.cs | 2 +- 3 files changed, 135 insertions(+), 19 deletions(-) diff --git a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs index 9cb7890fe2..ac1e3812dc 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs @@ -16,6 +16,9 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms { private readonly ITestOutputHelper Output; + /// + /// angleDeg, sx, sy, tx, ty + /// public static readonly TheoryData TransformValues = new TheoryData { @@ -51,6 +54,15 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms 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 TransformTests(ITestOutputHelper output) { this.Output = output; @@ -60,10 +72,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms /// The output of an "all white" image should be "all white" or transparent, regardless of the transformation and the resampler. /// [Theory] - [WithSolidFilledImages(5, 5, 255, 255, 255, 255, PixelTypes.Rgba32, nameof(KnownResamplers.NearestNeighbor))] - [WithSolidFilledImages(5, 5, 255, 255, 255, 255, PixelTypes.Rgba32, nameof(KnownResamplers.Triangle))] - [WithSolidFilledImages(5, 5, 255, 255, 255, 255, PixelTypes.Rgba32, nameof(KnownResamplers.Bicubic))] - [WithSolidFilledImages(5, 5, 255, 255, 255, 255, PixelTypes.Rgba32, nameof(KnownResamplers.Lanczos8))] + [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 { @@ -115,19 +124,36 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms { using (Image image = provider.GetImage()) { - 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 = this.MakeManuallyCenteredMatrix(angleDeg, s, image); - Matrix3x2 m = translate * rotate * scale * translateBack; + image.Mutate(i => i.Transform(m, KnownResamplers.Bicubic)); + image.DebugSave(provider, $"R({angleDeg})_S({s})"); + } + } - this.PrintMatrix(m); + public static readonly TheoryData Transform_IntoRectangle_Data = + new TheoryData + { + { 0, 0, 10, 10 }, + { 0, 0, 5, 10 }, + { 0, 0, 10, 5 }, + {-5,-5, 15, 15 } + }; - Rectangle destBounds = image.Bounds(); - image.Mutate(i => i.Transform(m, KnownResamplers.Bicubic, destBounds)); - image.DebugSave(provider, $"R({angleDeg})_S({s})"); + [Theory] + [WithSolidFilledImages(nameof(Transform_IntoRectangle_Data), 10, 10, nameof(Rgba32.Red), PixelTypes.Rgba32)] + public void Transform_IntoRectangle(TestImageProvider provider, int x0, int y0, int w, int h) + where TPixel : struct, IPixel + { + var rectangle = new Rectangle(x0, y0, w, h); + + using (Image image = provider.GetImage()) + { + Matrix3x2 m = this.MakeManuallyCenteredMatrix(45, 0.8f, image); + + image.Mutate(i => i.Transform(m, KnownResamplers.Spline, rectangle)); + string testDetails = $"({x0},{y0}-W{w},H{h})"; + image.DebugSave(provider, testDetails); } } @@ -142,13 +168,27 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms Matrix3x2 rotate = Matrix3x2Extensions.CreateRotationDegrees(50); Matrix3x2 scale = Matrix3x2Extensions.CreateScale(new SizeF(.5F, .5F)); var translate = Matrix3x2.CreateTranslation(75, 0); - - + image.Mutate(i => i.Transform(rotate * scale * translate, sampler)); image.DebugSave(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); 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) { } From 0d9bdba3f160381c42dba83a66b4c992d6ea4e04 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 24 Dec 2017 16:42:27 +1100 Subject: [PATCH 38/51] Make TransformHelpers internal --- src/ImageSharp/Processing/Transforms/TransformHelpers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Processing/Transforms/TransformHelpers.cs b/src/ImageSharp/Processing/Transforms/TransformHelpers.cs index 6f9560a9fa..419c1c13d6 100644 --- a/src/ImageSharp/Processing/Transforms/TransformHelpers.cs +++ b/src/ImageSharp/Processing/Transforms/TransformHelpers.cs @@ -10,7 +10,7 @@ namespace SixLabors.ImageSharp /// /// Contains helper methods for working with affine transforms /// - public class TransformHelpers + internal class TransformHelpers { /// /// Returns the bounding relative to the source for the given transformation matrix. From 831794e3a62b1a9649f5c7894e8785ef603d1c47 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Mon, 25 Dec 2017 00:26:12 +0100 Subject: [PATCH 39/51] yet another test case for TransformTests --- tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs index ac1e3812dc..d5e1f144a1 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs @@ -137,6 +137,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms { 0, 0, 10, 10 }, { 0, 0, 5, 10 }, { 0, 0, 10, 5 }, + { 5, 0, 5, 10 }, {-5,-5, 15, 15 } }; From d0ee1e100e48779299d3410d647ea429e74925f9 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 19 Jan 2018 21:44:18 +1100 Subject: [PATCH 40/51] Add internal non-affine transform methods --- ImageSharp.sln.DotSettings | 10 + ...ocessor.cs => AffineTransformProcessor.cs} | 138 ++-------- ...cs => CenteredAffineTransformProcessor.cs} | 10 +- .../CenteredNonAffineTransformProcessor.cs | 43 ++++ .../InterpolatedTransformProcessorBase.cs | 139 ++++++++++ .../Transforms/NonAffineTransformProcessor.cs | 238 ++++++++++++++++++ .../Processors/Transforms/RotateProcessor.cs | 2 +- .../Processors/Transforms/SkewProcessor.cs | 2 +- .../Transforms/TransformProcessor.cs | 47 ---- .../Processing/Transforms/Transform.cs | 46 +++- .../Processing/Transforms/TransformHelpers.cs | 29 ++- 11 files changed, 523 insertions(+), 181 deletions(-) rename src/ImageSharp/Processing/Processors/Transforms/{AffineProcessor.cs => AffineTransformProcessor.cs} (67%) rename src/ImageSharp/Processing/Processors/Transforms/{CenteredAffineProcessor.cs => CenteredAffineTransformProcessor.cs} (77%) create mode 100644 src/ImageSharp/Processing/Processors/Transforms/CenteredNonAffineTransformProcessor.cs create mode 100644 src/ImageSharp/Processing/Processors/Transforms/InterpolatedTransformProcessorBase.cs create mode 100644 src/ImageSharp/Processing/Processors/Transforms/NonAffineTransformProcessor.cs delete mode 100644 src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs diff --git a/ImageSharp.sln.DotSettings b/ImageSharp.sln.DotSettings index 1839bf2f8d..435aad73bf 100644 --- a/ImageSharp.sln.DotSettings +++ b/ImageSharp.sln.DotSettings @@ -38,10 +38,15 @@ NEXT_LINE_SHIFTED_2 1 1 + False + False False + NEVER False False + NEVER False + ALWAYS False True ON_SINGLE_LINE @@ -370,8 +375,13 @@ <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> <Policy Inspect="True" Prefix="T" Suffix="" Style="AaBb" /> <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + True + True + True + True True True + True True True \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs similarity index 67% rename from src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs rename to src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs index 59b8442639..ce4fbdd712 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs @@ -5,12 +5,10 @@ using System; using System.Collections.Generic; using System.Linq; using System.Numerics; -using System.Runtime.CompilerServices; 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; @@ -20,35 +18,42 @@ namespace SixLabors.ImageSharp.Processing.Processors /// Provides the base methods to perform affine transforms on an image. /// /// The pixel format. - internal abstract class AffineProcessor : CloningImageProcessor + internal class AffineTransformProcessor : InterpolatedTransformProcessorBase where TPixel : struct, IPixel { private Rectangle targetRectangle; /// - /// Initializes a new instance of the class. + /// 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. - protected AffineProcessor(Matrix3x2 matrix, IResampler sampler) + public AffineTransformProcessor(Matrix3x2 matrix, IResampler sampler) : this(matrix, sampler, Rectangle.Empty) { } /// - /// Initializes a new instance of the class. + /// 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. - protected AffineProcessor(Matrix3x2 matrix, IResampler sampler, Rectangle rectangle) + public AffineTransformProcessor(Matrix3x2 matrix, IResampler sampler, Rectangle rectangle) + : base(sampler) { // Tansforms are inverted else the output is the opposite of the expected. Matrix3x2.Invert(matrix, out matrix); this.TransformMatrix = matrix; - - this.Sampler = sampler; - this.targetRectangle = rectangle; } @@ -57,11 +62,6 @@ namespace SixLabors.ImageSharp.Processing.Processors /// public Matrix3x2 TransformMatrix { get; } - /// - /// Gets the sampler to perform interpolation of the transform operation. - /// - public IResampler Sampler { get; } - /// protected override Image CreateDestination(Image source, Rectangle sourceRectangle) { @@ -85,7 +85,7 @@ namespace SixLabors.ImageSharp.Processing.Processors int width = this.targetRectangle.Width; Rectangle sourceBounds = source.Bounds(); - // Since could potentially be resizing the canvas we need to re-center the matrix + // Since could potentially be resizing the canvas we might need to re-calculate the matrix Matrix3x2 matrix = this.GetProcessingMatrix(sourceBounds, this.targetRectangle); if (this.Sampler is NearestNeighborResampler) @@ -211,26 +211,6 @@ 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); - } - } - /// /// Gets a transform matrix adjusted for final processing based upon the target image bounds. /// @@ -254,91 +234,5 @@ namespace SixLabors.ImageSharp.Processing.Processors { return sourceRectangle; } - - /// - /// 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)] - private 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)] - private 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 - private (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); - } } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineTransformProcessor.cs similarity index 77% rename from src/ImageSharp/Processing/Processors/Transforms/CenteredAffineProcessor.cs rename to src/ImageSharp/Processing/Processors/Transforms/CenteredAffineTransformProcessor.cs index 5631af3aa0..34eabba9b8 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineTransformProcessor.cs @@ -7,15 +7,19 @@ using SixLabors.Primitives; namespace SixLabors.ImageSharp.Processing.Processors { - internal abstract class CenteredAffineProcessor : AffineProcessor + /// + /// 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. + /// Initializes a new instance of the class. /// /// The transform matrix /// The sampler to perform the transform operation. - protected CenteredAffineProcessor(Matrix3x2 matrix, IResampler sampler) + protected CenteredAffineTransformProcessor(Matrix3x2 matrix, IResampler sampler) : base(matrix, sampler) { } diff --git a/src/ImageSharp/Processing/Processors/Transforms/CenteredNonAffineTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/CenteredNonAffineTransformProcessor.cs new file mode 100644 index 0000000000..bb1505c807 --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Transforms/CenteredNonAffineTransformProcessor.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 CenteredNonAffineTransformProcessor : NonAffineTransformProcessor + where TPixel : struct, IPixel + { + /// + /// Initializes a new instance of the class. + /// + /// The transform matrix + /// The sampler to perform the transform operation. + protected CenteredNonAffineTransformProcessor(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/NonAffineTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/NonAffineTransformProcessor.cs new file mode 100644 index 0000000000..caaf812b07 --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Transforms/NonAffineTransformProcessor.cs @@ -0,0 +1,238 @@ +// 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. + /// + /// The pixel format. + internal class NonAffineTransformProcessor : InterpolatedTransformProcessorBase + where TPixel : struct, IPixel + { + private Rectangle targetRectangle; + + /// + /// Initializes a new instance of the class. + /// + /// The transform matrix + public NonAffineTransformProcessor(Matrix4x4 matrix) + : this(matrix, KnownResamplers.Bicubic) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The transform matrix + /// The sampler to perform the transform operation. + public NonAffineTransformProcessor(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 NonAffineTransformProcessor(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/RotateProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs index fa47dadaaa..b93c869152 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs @@ -15,7 +15,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// Provides methods that allow the rotating of images. /// /// The pixel format. - internal class RotateProcessor : CenteredAffineProcessor + internal class RotateProcessor : CenteredAffineTransformProcessor where TPixel : struct, IPixel { /// diff --git a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs index b123a309be..8c47b35274 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/SkewProcessor.cs @@ -10,7 +10,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// Provides methods that allow the skewing of images. /// /// The pixel format. - internal class SkewProcessor : CenteredAffineProcessor + internal class SkewProcessor : CenteredAffineTransformProcessor where TPixel : struct, IPixel { /// diff --git a/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs deleted file mode 100644 index 140fd7ec6a..0000000000 --- a/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor.cs +++ /dev/null @@ -1,47 +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 that allow the tranformation of images using various algorithms. - /// - /// The pixel format. - internal class TransformProcessor : AffineProcessor - where TPixel : struct, IPixel - { - /// - /// Initializes a new instance of the class. - /// - /// The transformation matrix - public TransformProcessor(Matrix3x2 matrix) - : this(matrix, KnownResamplers.Bicubic) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The transformation matrix - /// The sampler to perform the transform operation. - public TransformProcessor(Matrix3x2 matrix, IResampler sampler) - : base(matrix, sampler) - { - } - - /// - /// 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 TransformProcessor(Matrix3x2 matrix, IResampler sampler, Rectangle rectangle) - : base(matrix, sampler, rectangle) - { - } - } -} \ No newline at end of file diff --git a/src/ImageSharp/Processing/Transforms/Transform.cs b/src/ImageSharp/Processing/Transforms/Transform.cs index 74f91fa701..cbfe6da187 100644 --- a/src/ImageSharp/Processing/Transforms/Transform.cs +++ b/src/ImageSharp/Processing/Transforms/Transform.cs @@ -18,18 +18,18 @@ namespace SixLabors.ImageSharp /// Transforms an image by the given matrix. /// /// The pixel format. - /// The image to skew. + /// 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); + => Transform(source, matrix, KnownResamplers.NearestNeighbor); /// /// Transforms an image by the given matrix using the specified sampling algorithm. /// /// The pixel format. - /// The image to skew. + /// The image to transform. /// The transformation matrix. /// The to perform the resampling. /// The @@ -41,13 +41,49 @@ namespace SixLabors.ImageSharp /// Transforms an image by the given matrix using the specified sampling algorithm. /// /// The pixel format. - /// The image to skew. + /// The image to transform. /// The transformation matrix. /// The to perform the resampling. /// The rectangle to constrain the transformed image to. /// The public static IImageProcessingContext Transform(this IImageProcessingContext source, Matrix3x2 matrix, IResampler sampler, Rectangle rectangle) where TPixel : struct, IPixel - => source.ApplyProcessor(new TransformProcessor(matrix, sampler, rectangle)); + => source.ApplyProcessor(new AffineTransformProcessor(matrix, sampler, rectangle)); + + /// + /// 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); + + /// + /// 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 + internal static IImageProcessingContext Transform(this IImageProcessingContext source, Matrix4x4 matrix, IResampler sampler) + where TPixel : struct, IPixel + => Transform(source, matrix, sampler, Rectangle.Empty); + + /// + /// 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 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 NonAffineTransformProcessor(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 index 419c1c13d6..119fc9eeda 100644 --- a/src/ImageSharp/Processing/Transforms/TransformHelpers.cs +++ b/src/ImageSharp/Processing/Transforms/TransformHelpers.cs @@ -8,7 +8,7 @@ using SixLabors.Primitives; namespace SixLabors.ImageSharp { /// - /// Contains helper methods for working with affine transforms + /// Contains helper methods for working with affine and non-affine transforms /// internal class TransformHelpers { @@ -29,7 +29,32 @@ namespace SixLabors.ImageSharp var bl = Vector2.Transform(new Vector2(0, rectangle.Height), matrix); var br = Vector2.Transform(new Vector2(rectangle.Width, rectangle.Height), matrix); - // Find the minimum and maximum "corners" based on the ones above + 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))); From 9e5bd1b8b405eb7b169a124b40ae652bb282ee97 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 20 Jan 2018 21:54:18 +0100 Subject: [PATCH 41/51] fix merge tool madness --- src/ImageSharp/ImageSharp.csproj | 120 +------------------------------ 1 file changed, 1 insertion(+), 119 deletions(-) diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index 82282fb81c..9d30030da6 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -1,122 +1,4 @@  - - A cross-platform library for the processing of image files; written in C# - SixLabors.ImageSharp - $(packageversion) - 0.0.1 - Six Labors and contributors - netstandard1.1;netstandard1.3;netstandard2.0 - true - true - SixLabors.ImageSharp - SixLabors.ImageSharp - Image Resize Crop Gif Jpg Jpeg Bitmap Png Core - https://raw.githubusercontent.com/SixLabors/ImageSharp/master/build/icons/imagesharp-logo-128.png - https://github.com/SixLabors/ImageSharp - http://www.apache.org/licenses/LICENSE-2.0 - git - https://github.com/SixLabors/ImageSharp - false - false - false - false - false - false - false - false - false - full - portable - True - IOperation - - - - - - - - - All - - - - - - - - - - - - - ..\..\ImageSharp.ruleset - SixLabors.ImageSharp - - - true - - - - TextTemplatingFileGenerator - Block8x8F.Generated.cs - - - TextTemplatingFileGenerator - Block8x8F.Generated.cs - - - TextTemplatingFileGenerator - PixelOperations{TPixel}.Generated.cs - - - TextTemplatingFileGenerator - Rgba32.PixelOperations.Generated.cs - - - PorterDuffFunctions.Generated.cs - TextTemplatingFileGenerator - - - DefaultPixelBlenders.Generated.cs - TextTemplatingFileGenerator - - - - - - - - True - True - Block8x8F.Generated.tt - - - True - True - Block8x8F.Generated.tt - - - True - True - PixelOperations{TPixel}.Generated.tt - - - True - True - Rgba32.PixelOperations.Generated.tt - - - True - True - DefaultPixelBlenders.Generated.tt - - - True - True - PorterDuffFunctions.Generated.tt - - A cross-platform library for the processing of image files; written in C# SixLabors.ImageSharp @@ -152,7 +34,7 @@ - + All From f9f69f57037d37eaac634121e29c69f92f0b52d9 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 21 Jan 2018 01:13:29 +0100 Subject: [PATCH 42/51] better TransformTests --- .../Processing/Transforms/TransformTests.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs index d5e1f144a1..43ae664184 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs @@ -29,7 +29,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms { 0, 1, 1, -20, -10 }, { 50, 1, 1, -20, -10 }, { 50, 1.5f, 1.5f, 0, 0 }, - { 50, 1.1F, 1.2F, 20, 10 }, + { 50, 1.1F, 1.3F, 30, -20 }, { 0, 2f, 1f, 0, 0 }, { 0, 1f, 2f, 0, 0 }, }; @@ -113,7 +113,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms this.PrintMatrix(m); image.Mutate(i => i.Transform(m, KnownResamplers.Bicubic)); - image.DebugSave(provider, $"R({angleDeg})_S({sx},{sy})_T({tx},{ty})"); + + string testOutputDetails = $"R({angleDeg})_S({sx},{sy})_T({tx},{ty})"; + image.DebugSave(provider, testOutputDetails); + image.CompareToReferenceOutput(provider, testOutputDetails); } } From 102a12dc16ea364ff16b950413b430c0a9c1d4ce Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 21 Jan 2018 01:18:14 +0100 Subject: [PATCH 43/51] TransformTests -> AffineTransformTests + update submodule to use new reference images --- .../Processing/Transforms/TransformTests.cs | 7 +++---- tests/Images/External | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs index 43ae664184..574a5c3bab 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs @@ -6,13 +6,12 @@ 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 { - using SixLabors.ImageSharp.Helpers; - - public class TransformTests + public class AffineTransformTests { private readonly ITestOutputHelper Output; @@ -63,7 +62,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms nameof(KnownResamplers.Lanczos8), }; - public TransformTests(ITestOutputHelper output) + public AffineTransformTests(ITestOutputHelper output) { this.Output = output; } diff --git a/tests/Images/External b/tests/Images/External index dc5479d00b..0141b2af86 160000 --- a/tests/Images/External +++ b/tests/Images/External @@ -1 +1 @@ -Subproject commit dc5479d00b2312f691e6249b9f7765e2316d4a30 +Subproject commit 0141b2af864fa3eaa63b09127dc99fb09374f235 From 8edeb51763cb1906fb9c1bf3ebf23c9616fd3941 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 21 Jan 2018 01:37:35 +0100 Subject: [PATCH 44/51] AffineTransformProcessor.targetRectangle replaced by targetDimensions --- .../Transforms/AffineTransformProcessor.cs | 34 ++++++++++++------- .../CenteredAffineTransformProcessor.cs | 14 +++++--- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs index ce4fbdd712..fb07eb0238 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs @@ -21,7 +21,7 @@ namespace SixLabors.ImageSharp.Processing.Processors internal class AffineTransformProcessor : InterpolatedTransformProcessorBase where TPixel : struct, IPixel { - private Rectangle targetRectangle; + private Size? targetDimensions; /// /// Initializes a new instance of the class. @@ -54,7 +54,7 @@ namespace SixLabors.ImageSharp.Processing.Processors // Tansforms are inverted else the output is the opposite of the expected. Matrix3x2.Invert(matrix, out matrix); this.TransformMatrix = matrix; - this.targetRectangle = rectangle; + this.targetDimensions = rectangle == Rectangle.Empty ?(Size?)null : rectangle.Size; } /// @@ -65,28 +65,38 @@ namespace SixLabors.ImageSharp.Processing.Processors /// protected override Image CreateDestination(Image source, Rectangle sourceRectangle) { - if (this.targetRectangle == Rectangle.Empty) + if (!this.targetDimensions.HasValue) { - this.targetRectangle = this.GetTransformedBoundingRectangle(sourceRectangle, this.TransformMatrix); + // TODO: CreateDestination() should not modify the processors state! (kinda CQRS) + this.targetDimensions = this.GetTransformedDimensions(sourceRectangle.Size, this.TransformMatrix); } + Size targetDims = this.targetDimensions.Value; + // 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())); + source.Frames.Select(x => new ImageFrame(targetDims.Width, targetDims.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) + protected override void OnApply( + ImageFrame source, + ImageFrame destination, + Rectangle sourceRectangle, + Configuration configuration) { - int height = this.targetRectangle.Height; - int width = this.targetRectangle.Width; + int height = this.targetDimensions.Value.Height; + int width = this.targetDimensions.Value.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, this.targetRectangle); + + Matrix3x2 matrix = this.GetProcessingMatrix(sourceBounds, targetBounds); if (this.Sampler is NearestNeighborResampler) { @@ -227,12 +237,12 @@ namespace SixLabors.ImageSharp.Processing.Processors /// /// Gets the bounding relative to the source for the given transformation matrix. /// - /// The source rectangle. + /// The source rectangle. /// The transformation matrix. /// The - protected virtual Rectangle GetTransformedBoundingRectangle(Rectangle sourceRectangle, Matrix3x2 matrix) + protected virtual Size GetTransformedDimensions(Size sourceDimensions, Matrix3x2 matrix) { - return sourceRectangle; + return sourceDimensions; } } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineTransformProcessor.cs index 34eabba9b8..34a0866615 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineTransformProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/CenteredAffineTransformProcessor.cs @@ -33,11 +33,17 @@ namespace SixLabors.ImageSharp.Processing.Processors } /// - protected override Rectangle GetTransformedBoundingRectangle(Rectangle sourceRectangle, Matrix3x2 matrix) + protected override Size GetTransformedDimensions(Size sourceDimensions, Matrix3x2 matrix) { - return Matrix3x2.Invert(this.TransformMatrix, out Matrix3x2 sizeMatrix) - ? TransformHelpers.GetTransformedBoundingRectangle(sourceRectangle, sizeMatrix) - : sourceRectangle; + 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 From b09f533aa217b7c287e57bf26075150d0863b8f6 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 21 Jan 2018 01:49:54 +0100 Subject: [PATCH 45/51] Transform() extension method overload taking Size --- src/ImageSharp/Image/ImageFrame{TPixel}.cs | 12 ++++++++ .../Transforms/AffineTransformProcessor.cs | 21 ++++++------- .../Processing/Transforms/Transform.cs | 30 +++++++++++++++++-- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/ImageSharp/Image/ImageFrame{TPixel}.cs b/src/ImageSharp/Image/ImageFrame{TPixel}.cs index 45ed5f053a..96f78ef98a 100644 --- a/src/ImageSharp/Image/ImageFrame{TPixel}.cs +++ b/src/ImageSharp/Image/ImageFrame{TPixel}.cs @@ -13,6 +13,8 @@ using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp { + using SixLabors.Primitives; + /// /// Represents a single frame in a animation. /// @@ -53,6 +55,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/Processing/Processors/Transforms/AffineTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs index fb07eb0238..07c247d734 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs @@ -21,7 +21,7 @@ namespace SixLabors.ImageSharp.Processing.Processors internal class AffineTransformProcessor : InterpolatedTransformProcessorBase where TPixel : struct, IPixel { - private Size? targetDimensions; + private Size targetDimensions; /// /// Initializes a new instance of the class. @@ -38,7 +38,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// The transform matrix /// The sampler to perform the transform operation. public AffineTransformProcessor(Matrix3x2 matrix, IResampler sampler) - : this(matrix, sampler, Rectangle.Empty) + : this(matrix, sampler, Size.Empty) { } @@ -47,14 +47,14 @@ namespace SixLabors.ImageSharp.Processing.Processors /// /// The transform matrix /// The sampler to perform the transform operation. - /// The rectangle to constrain the transformed image to. - public AffineTransformProcessor(Matrix3x2 matrix, IResampler sampler, Rectangle rectangle) + /// 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 = rectangle == Rectangle.Empty ?(Size?)null : rectangle.Size; + this.targetDimensions = targetDimensions; } /// @@ -65,17 +65,15 @@ namespace SixLabors.ImageSharp.Processing.Processors /// protected override Image CreateDestination(Image source, Rectangle sourceRectangle) { - if (!this.targetDimensions.HasValue) + if (this.targetDimensions == Size.Empty) { // TODO: CreateDestination() should not modify the processors state! (kinda CQRS) this.targetDimensions = this.GetTransformedDimensions(sourceRectangle.Size, this.TransformMatrix); } - Size targetDims = this.targetDimensions.Value; - // 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(targetDims.Width, targetDims.Height, x.MetaData.Clone())); + 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); @@ -88,14 +86,13 @@ namespace SixLabors.ImageSharp.Processing.Processors Rectangle sourceRectangle, Configuration configuration) { - int height = this.targetDimensions.Value.Height; - int width = this.targetDimensions.Value.Width; + 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) diff --git a/src/ImageSharp/Processing/Transforms/Transform.cs b/src/ImageSharp/Processing/Transforms/Transform.cs index cbfe6da187..87c7306b81 100644 --- a/src/ImageSharp/Processing/Transforms/Transform.cs +++ b/src/ImageSharp/Processing/Transforms/Transform.cs @@ -46,9 +46,35 @@ namespace SixLabors.ImageSharp /// The to perform the resampling. /// The rectangle to constrain the transformed image to. /// The - public static IImageProcessingContext Transform(this IImageProcessingContext source, Matrix3x2 matrix, IResampler sampler, Rectangle rectangle) + public static IImageProcessingContext Transform( + this IImageProcessingContext source, + Matrix3x2 matrix, + IResampler sampler, + Rectangle rectangle) where TPixel : struct, IPixel - => source.ApplyProcessor(new AffineTransformProcessor(matrix, sampler, rectangle)); + { + // TODO: Fixme! + return source.ApplyProcessor(new AffineTransformProcessor(matrix, sampler, rectangle.Size)); + } + + /// + /// 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 dimensions to constrain the transformed image to. + /// 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. From 96975dff7fe32de176ae43f652f9586d21f76ff5 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 21 Jan 2018 01:59:00 +0100 Subject: [PATCH 46/51] Transform_RotateScale_ManuallyCentered uses reference image now --- .../{TransformTests.cs => AffineTransformTests.cs} | 5 ++++- tests/Images/External | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) rename tests/ImageSharp.Tests/Processing/Transforms/{TransformTests.cs => AffineTransformTests.cs} (97%) diff --git a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs similarity index 97% rename from tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs rename to tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs index 574a5c3bab..f6ef365e54 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/TransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs @@ -129,7 +129,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms Matrix3x2 m = this.MakeManuallyCenteredMatrix(angleDeg, s, image); image.Mutate(i => i.Transform(m, KnownResamplers.Bicubic)); - image.DebugSave(provider, $"R({angleDeg})_S({s})"); + + string testOutputDetails = $"R({angleDeg})_S({s})"; + image.DebugSave(provider, testOutputDetails); + image.CompareToReferenceOutput(provider, testOutputDetails); } } diff --git a/tests/Images/External b/tests/Images/External index 0141b2af86..9d4070dab9 160000 --- a/tests/Images/External +++ b/tests/Images/External @@ -1 +1 @@ -Subproject commit 0141b2af864fa3eaa63b09127dc99fb09374f235 +Subproject commit 9d4070dab9e4aefa70dcd41ef98176a5d54e644f From 5c12546100ea4d97dccff3373416b695ad83021e Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 21 Jan 2018 03:34:21 +0100 Subject: [PATCH 47/51] finalized transform with source rectangle + tests --- .../Processing/Transforms/Transform.cs | 11 ++-- .../Transforms/AffineTransformTests.cs | 52 ++++++++++++++----- tests/Images/External | 2 +- 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/src/ImageSharp/Processing/Transforms/Transform.cs b/src/ImageSharp/Processing/Transforms/Transform.cs index 87c7306b81..496e57defc 100644 --- a/src/ImageSharp/Processing/Transforms/Transform.cs +++ b/src/ImageSharp/Processing/Transforms/Transform.cs @@ -35,7 +35,7 @@ namespace SixLabors.ImageSharp /// The public static IImageProcessingContext Transform(this IImageProcessingContext source, Matrix3x2 matrix, IResampler sampler) where TPixel : struct, IPixel - => Transform(source, matrix, sampler, Rectangle.Empty); + => Transform(source, matrix, sampler, Size.Empty); /// /// Transforms an image by the given matrix using the specified sampling algorithm. @@ -44,17 +44,18 @@ namespace SixLabors.ImageSharp /// The image to transform. /// The transformation matrix. /// The to perform the resampling. - /// The rectangle to constrain the transformed image to. + /// The rectangle defining the source pixel area to transform. 'sourceRectangle.Location' becomes the origo of the transformed image. /// The public static IImageProcessingContext Transform( this IImageProcessingContext source, Matrix3x2 matrix, IResampler sampler, - Rectangle rectangle) + Rectangle sourceRectangle) where TPixel : struct, IPixel { - // TODO: Fixme! - return source.ApplyProcessor(new AffineTransformProcessor(matrix, sampler, rectangle.Size)); + var t = Matrix3x2.CreateTranslation(-sourceRectangle.Location); + Matrix3x2 combinedMatrix = t * matrix; + return source.ApplyProcessor(new AffineTransformProcessor(combinedMatrix, sampler, sourceRectangle.Size)); } /// diff --git a/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs index f6ef365e54..ace6cb70d6 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs @@ -143,40 +143,66 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms { 0, 0, 5, 10 }, { 0, 0, 10, 5 }, { 5, 0, 5, 10 }, - {-5,-5, 15, 15 } + {-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] - [WithSolidFilledImages(nameof(Transform_IntoRectangle_Data), 10, 10, nameof(Rgba32.Red), PixelTypes.Rgba32)] - public void Transform_IntoRectangle(TestImageProvider provider, int x0, int y0, int w, int h) + [WithTestPatternImages(96, 48, PixelTypes.Rgba32)] + public void Transform_FromSourceRectangle2(TestImageProvider provider) where TPixel : struct, IPixel { - var rectangle = new Rectangle(x0, y0, w, h); + var rectangle = new Rectangle(0, 24, 48, 48); using (Image image = provider.GetImage()) { - Matrix3x2 m = this.MakeManuallyCenteredMatrix(45, 0.8f, image); + var m = Matrix3x2.CreateScale(1.0F, 2.0F); image.Mutate(i => i.Transform(m, KnownResamplers.Spline, rectangle)); - string testDetails = $"({x0},{y0}-W{w},H{h})"; - image.DebugSave(provider, testDetails); + + image.DebugSave(provider); + image.CompareToReferenceOutput(provider); } } [Theory] - [WithTestPatternImages(nameof(ResamplerNames), 100, 200, PixelTypes.Rgba32)] + [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 rotate = Matrix3x2Extensions.CreateRotationDegrees(50); - Matrix3x2 scale = Matrix3x2Extensions.CreateScale(new SizeF(.5F, .5F)); - var translate = Matrix3x2.CreateTranslation(75, 0); - - image.Mutate(i => i.Transform(rotate * scale * translate, sampler)); + Matrix3x2 m = this.MakeManuallyCenteredMatrix(50, 0.6f, image); + + image.Mutate(i => + { + i.Transform(m, sampler); + }); + image.DebugSave(provider, resamplerName); + image.CompareToReferenceOutput(provider, resamplerName); } } diff --git a/tests/Images/External b/tests/Images/External index 9d4070dab9..02687e772f 160000 --- a/tests/Images/External +++ b/tests/Images/External @@ -1 +1 @@ -Subproject commit 9d4070dab9e4aefa70dcd41ef98176a5d54e644f +Subproject commit 02687e772f84b1d518ab83eacdfafba476f824e6 From d2e7a07374eb40c943e52503a5a9c1ddd2c09bc4 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 21 Jan 2018 03:47:24 +0100 Subject: [PATCH 48/51] IDE-s & analyzers why are you doing this to me? It's 2018! --- src/ImageSharp/Image/ImageFrame{TPixel}.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ImageSharp/Image/ImageFrame{TPixel}.cs b/src/ImageSharp/Image/ImageFrame{TPixel}.cs index 96f78ef98a..e39cc1ab2f 100644 --- a/src/ImageSharp/Image/ImageFrame{TPixel}.cs +++ b/src/ImageSharp/Image/ImageFrame{TPixel}.cs @@ -10,11 +10,10 @@ using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.MetaData; using SixLabors.ImageSharp.PixelFormats; +using SixLabors.Primitives; namespace SixLabors.ImageSharp { - using SixLabors.Primitives; - /// /// Represents a single frame in a animation. /// From 22d212de09cb653281fb0a3c82346311289ede44 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 21 Jan 2018 03:55:11 +0100 Subject: [PATCH 49/51] renamed NonAffineTransformProcessor to ProjectiveTransformProcessor --- ...cs => CenteredProjectiveTransformProcessor.cs} | 6 +++--- ...ocessor.cs => ProjectiveTransformProcessor.cs} | 15 ++++++++------- src/ImageSharp/Processing/Transforms/Transform.cs | 8 +++++--- 3 files changed, 16 insertions(+), 13 deletions(-) rename src/ImageSharp/Processing/Processors/Transforms/{CenteredNonAffineTransformProcessor.cs => CenteredProjectiveTransformProcessor.cs} (87%) rename src/ImageSharp/Processing/Processors/Transforms/{NonAffineTransformProcessor.cs => ProjectiveTransformProcessor.cs} (93%) diff --git a/src/ImageSharp/Processing/Processors/Transforms/CenteredNonAffineTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/CenteredProjectiveTransformProcessor.cs similarity index 87% rename from src/ImageSharp/Processing/Processors/Transforms/CenteredNonAffineTransformProcessor.cs rename to src/ImageSharp/Processing/Processors/Transforms/CenteredProjectiveTransformProcessor.cs index bb1505c807..dc2dd28ab1 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/CenteredNonAffineTransformProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/CenteredProjectiveTransformProcessor.cs @@ -11,15 +11,15 @@ 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 CenteredNonAffineTransformProcessor : NonAffineTransformProcessor + internal abstract class CenteredProjectiveTransformProcessor : ProjectiveTransformProcessor where TPixel : struct, IPixel { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The transform matrix /// The sampler to perform the transform operation. - protected CenteredNonAffineTransformProcessor(Matrix4x4 matrix, IResampler sampler) + protected CenteredProjectiveTransformProcessor(Matrix4x4 matrix, IResampler sampler) : base(matrix, sampler) { } diff --git a/src/ImageSharp/Processing/Processors/Transforms/NonAffineTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs similarity index 93% rename from src/ImageSharp/Processing/Processors/Transforms/NonAffineTransformProcessor.cs rename to src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs index caaf812b07..579a0ac9a2 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/NonAffineTransformProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs @@ -16,39 +16,40 @@ 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 NonAffineTransformProcessor : InterpolatedTransformProcessorBase + internal class ProjectiveTransformProcessor : InterpolatedTransformProcessorBase where TPixel : struct, IPixel { private Rectangle targetRectangle; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The transform matrix - public NonAffineTransformProcessor(Matrix4x4 matrix) + public ProjectiveTransformProcessor(Matrix4x4 matrix) : this(matrix, KnownResamplers.Bicubic) { } /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The transform matrix /// The sampler to perform the transform operation. - public NonAffineTransformProcessor(Matrix4x4 matrix, IResampler sampler) + public ProjectiveTransformProcessor(Matrix4x4 matrix, IResampler sampler) : this(matrix, sampler, Rectangle.Empty) { } /// - /// Initializes a new instance of the class. + /// 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 NonAffineTransformProcessor(Matrix4x4 matrix, IResampler sampler, Rectangle rectangle) + public ProjectiveTransformProcessor(Matrix4x4 matrix, IResampler sampler, Rectangle rectangle) : base(sampler) { // Tansforms are inverted else the output is the opposite of the expected. diff --git a/src/ImageSharp/Processing/Transforms/Transform.cs b/src/ImageSharp/Processing/Transforms/Transform.cs index 496e57defc..21cd7863a5 100644 --- a/src/ImageSharp/Processing/Transforms/Transform.cs +++ b/src/ImageSharp/Processing/Transforms/Transform.cs @@ -89,7 +89,8 @@ namespace SixLabors.ImageSharp => Transform(source, matrix, KnownResamplers.NearestNeighbor); /// - /// Transforms an image by the given matrix using the specified sampling algorithm. + /// 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. @@ -101,7 +102,8 @@ namespace SixLabors.ImageSharp => Transform(source, matrix, sampler, Rectangle.Empty); /// - /// Transforms an image by the given matrix using the specified sampling algorithm. + /// 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. @@ -111,6 +113,6 @@ namespace SixLabors.ImageSharp /// The internal static IImageProcessingContext Transform(this IImageProcessingContext source, Matrix4x4 matrix, IResampler sampler, Rectangle rectangle) where TPixel : struct, IPixel - => source.ApplyProcessor(new NonAffineTransformProcessor(matrix, sampler, rectangle)); + => source.ApplyProcessor(new ProjectiveTransformProcessor(matrix, sampler, rectangle)); } } \ No newline at end of file From f6bf0fef94356ce39d35d811bfad673302e42d2e Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 21 Jan 2018 14:01:45 +0100 Subject: [PATCH 50/51] better variable naming and docs --- .../Transforms/ProjectiveTransformProcessor.cs | 1 + .../Processing/Transforms/Transform.cs | 18 +++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs index 579a0ac9a2..f53585093b 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs @@ -22,6 +22,7 @@ namespace SixLabors.ImageSharp.Processing.Processors internal class ProjectiveTransformProcessor : InterpolatedTransformProcessorBase where TPixel : struct, IPixel { + // TODO: We should use a Size instead! (See AffineTransformProcessor) private Rectangle targetRectangle; /// diff --git a/src/ImageSharp/Processing/Transforms/Transform.cs b/src/ImageSharp/Processing/Transforms/Transform.cs index 21cd7863a5..e39da8dc0f 100644 --- a/src/ImageSharp/Processing/Transforms/Transform.cs +++ b/src/ImageSharp/Processing/Transforms/Transform.cs @@ -38,34 +38,38 @@ namespace SixLabors.ImageSharp => Transform(source, matrix, sampler, Size.Empty); /// - /// Transforms an image by the given matrix using the specified sampling algorithm. + /// 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 source pixel area to transform. 'sourceRectangle.Location' becomes the origo of the transformed image. + /// + /// 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 sourceRectangle) + Rectangle rectangle) where TPixel : struct, IPixel { - var t = Matrix3x2.CreateTranslation(-sourceRectangle.Location); + var t = Matrix3x2.CreateTranslation(-rectangle.Location); Matrix3x2 combinedMatrix = t * matrix; - return source.ApplyProcessor(new AffineTransformProcessor(combinedMatrix, sampler, sourceRectangle.Size)); + return source.ApplyProcessor(new AffineTransformProcessor(combinedMatrix, sampler, rectangle.Size)); } /// - /// Transforms an image by the given matrix using the specified sampling algorithm. + /// 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 dimensions to constrain the transformed image to. + /// The size of the destination image. /// The public static IImageProcessingContext Transform( this IImageProcessingContext source, From b4c0ad44033f51054b92f18e4844d512c330ca96 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 23 Jan 2018 00:21:03 +1100 Subject: [PATCH 51/51] Update submodule --- tests/Images/External | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Images/External b/tests/Images/External index 02687e772f..ddc4045926 160000 --- a/tests/Images/External +++ b/tests/Images/External @@ -1 +1 @@ -Subproject commit 02687e772f84b1d518ab83eacdfafba476f824e6 +Subproject commit ddc4045926bb0331be8c1785d9adf5f76dc4e52e