From 6c41d2f9329046900066dfc6a230615271d7989a Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 4 Sep 2020 14:13:42 +0100 Subject: [PATCH] Use interest for target bounds. Fixes #1342 --- .../Resize/ResizeProcessor{TPixel}.cs | 16 ++++++++----- .../Processors/Transforms/ResizeTests.cs | 24 +++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs index 9908d4f799..3c033d89e4 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs @@ -147,6 +147,7 @@ private static void ApplyNNResizeFrameTransform( var operation = new NNRowOperation( sourceRectangle, destinationRectangle, + interest, widthFactor, heightFactor, source, @@ -197,6 +198,7 @@ private static void ApplyResizeFrameTransform( { private readonly Rectangle sourceBounds; private readonly Rectangle destinationBounds; + private readonly Rectangle interest; private readonly float widthFactor; private readonly float heightFactor; private readonly ImageFrame source; @@ -206,6 +208,7 @@ private static void ApplyResizeFrameTransform( public NNRowOperation( Rectangle sourceBounds, Rectangle destinationBounds, + Rectangle interest, float widthFactor, float heightFactor, ImageFrame source, @@ -213,6 +216,7 @@ public NNRowOperation( { this.sourceBounds = sourceBounds; this.destinationBounds = destinationBounds; + this.interest = interest; this.widthFactor = widthFactor; this.heightFactor = heightFactor; this.source = source; @@ -224,19 +228,19 @@ public void Invoke(int y) { int sourceX = this.sourceBounds.X; int sourceY = this.sourceBounds.Y; - int destX = this.destinationBounds.X; - int destY = this.destinationBounds.Y; - int destLeft = this.destinationBounds.Left; - int destRight = this.destinationBounds.Right; + int destOriginX = this.destinationBounds.X; + int destOriginY = this.destinationBounds.Y; + int destLeft = this.interest.Left; + int destRight = this.interest.Right; // Y coordinates of source points - Span sourceRow = this.source.GetPixelRowSpan((int)(((y - destY) * this.heightFactor) + sourceY)); + Span sourceRow = this.source.GetPixelRowSpan((int)(((y - destOriginY) * this.heightFactor) + sourceY)); Span targetRow = this.destination.GetPixelRowSpan(y); for (int x = destLeft; x < destRight; x++) { // X coordinates of source points - targetRow[x] = sourceRow[(int)(((x - destX) * this.widthFactor) + sourceX)]; + targetRow[x] = sourceRow[(int)(((x - destOriginX) * this.widthFactor) + sourceX)]; } } } diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs index 51b8ee0264..f40b8d11a0 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs @@ -621,5 +621,29 @@ public void Issue1195() })); } } + + [Theory] + [InlineData(1, 1)] + [InlineData(4, 6)] + [InlineData(2, 10)] + [InlineData(8, 1)] + [InlineData(3, 7)] + public void Issue1342(int width, int height) + { + using (var image = new Image(1, 1)) + { + var size = new Size(width, height); + image.Mutate(x => x + .Resize( + new ResizeOptions + { + Size = size, + Sampler = KnownResamplers.NearestNeighbor + })); + + Assert.Equal(width, image.Width); + Assert.Equal(height, image.Height); + } + } } }