Skip to content

Commit b06cb32

Browse files
Merge pull request #1343 from SixLabors/js/fix-1342
Use interest for target bounds. Fixes #1342
2 parents c9e4d05 + 6c41d2f commit b06cb32

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ private static void ApplyNNResizeFrameTransform(
147147
var operation = new NNRowOperation(
148148
sourceRectangle,
149149
destinationRectangle,
150+
interest,
150151
widthFactor,
151152
heightFactor,
152153
source,
@@ -197,6 +198,7 @@ private static void ApplyResizeFrameTransform(
197198
{
198199
private readonly Rectangle sourceBounds;
199200
private readonly Rectangle destinationBounds;
201+
private readonly Rectangle interest;
200202
private readonly float widthFactor;
201203
private readonly float heightFactor;
202204
private readonly ImageFrame<TPixel> source;
@@ -206,13 +208,15 @@ private static void ApplyResizeFrameTransform(
206208
public NNRowOperation(
207209
Rectangle sourceBounds,
208210
Rectangle destinationBounds,
211+
Rectangle interest,
209212
float widthFactor,
210213
float heightFactor,
211214
ImageFrame<TPixel> source,
212215
ImageFrame<TPixel> destination)
213216
{
214217
this.sourceBounds = sourceBounds;
215218
this.destinationBounds = destinationBounds;
219+
this.interest = interest;
216220
this.widthFactor = widthFactor;
217221
this.heightFactor = heightFactor;
218222
this.source = source;
@@ -224,19 +228,19 @@ public void Invoke(int y)
224228
{
225229
int sourceX = this.sourceBounds.X;
226230
int sourceY = this.sourceBounds.Y;
227-
int destX = this.destinationBounds.X;
228-
int destY = this.destinationBounds.Y;
229-
int destLeft = this.destinationBounds.Left;
230-
int destRight = this.destinationBounds.Right;
231+
int destOriginX = this.destinationBounds.X;
232+
int destOriginY = this.destinationBounds.Y;
233+
int destLeft = this.interest.Left;
234+
int destRight = this.interest.Right;
231235

232236
// Y coordinates of source points
233-
Span<TPixel> sourceRow = this.source.GetPixelRowSpan((int)(((y - destY) * this.heightFactor) + sourceY));
237+
Span<TPixel> sourceRow = this.source.GetPixelRowSpan((int)(((y - destOriginY) * this.heightFactor) + sourceY));
234238
Span<TPixel> targetRow = this.destination.GetPixelRowSpan(y);
235239

236240
for (int x = destLeft; x < destRight; x++)
237241
{
238242
// X coordinates of source points
239-
targetRow[x] = sourceRow[(int)(((x - destX) * this.widthFactor) + sourceX)];
243+
targetRow[x] = sourceRow[(int)(((x - destOriginX) * this.widthFactor) + sourceX)];
240244
}
241245
}
242246
}

tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,5 +621,29 @@ public void Issue1195()
621621
}));
622622
}
623623
}
624+
625+
[Theory]
626+
[InlineData(1, 1)]
627+
[InlineData(4, 6)]
628+
[InlineData(2, 10)]
629+
[InlineData(8, 1)]
630+
[InlineData(3, 7)]
631+
public void Issue1342(int width, int height)
632+
{
633+
using (var image = new Image<Rgba32>(1, 1))
634+
{
635+
var size = new Size(width, height);
636+
image.Mutate(x => x
637+
.Resize(
638+
new ResizeOptions
639+
{
640+
Size = size,
641+
Sampler = KnownResamplers.NearestNeighbor
642+
}));
643+
644+
Assert.Equal(width, image.Width);
645+
Assert.Equal(height, image.Height);
646+
}
647+
}
624648
}
625649
}

0 commit comments

Comments
 (0)