Skip to content

Commit 6863127

Browse files
authored
Merge pull request #2610 from SixLabors/issue/2603
Handle case when Foreground image overhangs bottom of background image during DrawImage Call (3.1 target)
2 parents df21451 + a4ce7ca commit 6863127

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor{TPixelBg,TPixelFg}.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ protected override void OnFrameApply(ImageFrame<TPixelBg> source)
9898
top = 0;
9999
}
100100

101+
// clamp the height/width to the availible space left to prevent overflowing
102+
foregroundRectangle.Width = Math.Min(source.Width - left, foregroundRectangle.Width);
103+
foregroundRectangle.Height = Math.Min(source.Height - top, foregroundRectangle.Height);
104+
101105
int width = foregroundRectangle.Width;
102106
int height = foregroundRectangle.Height;
103107
if (width <= 0 || height <= 0)

tests/ImageSharp.Tests/Drawing/DrawImageTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,25 @@ public void Issue2608_NegOffset<TPixel>(TestImageProvider<TPixel> provider)
272272
appendPixelTypeToFileName: false,
273273
appendSourceFileOrDescription: false);
274274
}
275+
276+
[Theory]
277+
[WithFile(TestImages.Png.Issue2447, PixelTypes.Rgba32)]
278+
public void Issue2603<TPixel>(TestImageProvider<TPixel> provider)
279+
where TPixel : unmanaged, IPixel<TPixel>
280+
{
281+
using Image<TPixel> foreground = provider.GetImage();
282+
using Image<Rgba32> background = new(100, 100, new Rgba32(0, 255, 255));
283+
284+
background.Mutate(c => c.DrawImage(foreground, new Point(80, 80), new Rectangle(32, 32, 32, 32), 1F));
285+
286+
background.DebugSave(
287+
provider,
288+
appendPixelTypeToFileName: false,
289+
appendSourceFileOrDescription: false);
290+
291+
background.CompareToReferenceOutput(
292+
provider,
293+
appendPixelTypeToFileName: false,
294+
appendSourceFileOrDescription: false);
295+
}
275296
}
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)