diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs index ce4fbdd71..fb07eb023 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 34eabba9b..34a086661 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