Skip to content

Commit 499e0bb

Browse files
atruskieJimBobSquarePants
authored andcommitted
Now throws a better excpetion DrawImage source does not overlap target (SixLabors#877)
* No longer throws when DrawImage source does not overlap target Previously, when DrawImage was used to overlay an image, in cases where the source image did not overlap the target image, a very confusing error was reported: "System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: MaxDegreeOfParallelism" Now, when this case happens, the DrawImage method will simply not affect the target image, which is the same way FillRegionProcessor handles such cases. ParallelHelper.IterRows also now does more validation of the input rectangle so that any further cases of this kind of problem throw a more relvant exception. Note I switched from DebugGuard to Guard because IterRows is a public API and thus should always validate its inputs. Fixes SixLabors#875 * Refines DrawImage non-overlap error logic Adresses PR feedback in SixLabors#877. Changes DrawImage shortcut to be less than or equal to. Also changes maxX calculation to use `Right` over `Width`. This is a semantic change that reflects intention better. No actual change because Top,Left for that rectanngle should always be 0,0. And adds more informative argument names to ParallelHelper.IterRows error message. * Non-overlapping DrawImage now throws Adressing PR feedback from SixLabors#877 DrawImage now throws when the source image does not overlap, with a useful error message. Also improved the error messages for IterRows (and added validation to the other IterRows method) * DrawImage overlap test changed to support RELEASE The tests on the CI server are run in RELEASE which wrap the expected exception with an ImageProcessingException. * Adress feedback for DrawImage exception DrawImage throws an ImageProcessor exception which makes it easier to catch. And reverted IterRows to use Guard helpers
1 parent 7771c83 commit 499e0bb

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

src/ImageSharp.Drawing/Processing/Processors/Drawing/DrawImageProcessor.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected override void OnFrameApply(ImageFrame<TPixelDst> source, Rectangle sou
7171
Rectangle bounds = targetImage.Bounds();
7272

7373
int minX = Math.Max(this.Location.X, sourceRectangle.X);
74-
int maxX = Math.Min(this.Location.X + bounds.Width, sourceRectangle.Width);
74+
int maxX = Math.Min(this.Location.X + bounds.Width, sourceRectangle.Right);
7575
int targetX = minX - this.Location.X;
7676

7777
int minY = Math.Max(this.Location.Y, sourceRectangle.Y);
@@ -81,6 +81,12 @@ protected override void OnFrameApply(ImageFrame<TPixelDst> source, Rectangle sou
8181

8282
var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY);
8383

84+
// not a valid operation because rectangle does not overlap with this image.
85+
if (workingRect.Width <= 0 || workingRect.Height <= 0)
86+
{
87+
throw new ImageProcessingException("Cannot draw image because the source image does not overlap the target image.");
88+
}
89+
8490
ParallelHelper.IterateRows(
8591
workingRect,
8692
configuration,

tests/ImageSharp.Tests/Drawing/DrawImageTest.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,48 @@ public void ImageShouldHandlePositiveLocation(TestImageProvider<Rgba32> provider
131131
background.DebugSave(provider, testOutputDetails: "Positive");
132132
}
133133
}
134+
[Theory]
135+
[WithSolidFilledImages(100, 100, 255, 255, 255, PixelTypes.Rgba32)]
136+
public void ImageShouldHandlePositiveLocationTruncatedOverlay(TestImageProvider<Rgba32> provider)
137+
{
138+
using (Image<Rgba32> background = provider.GetImage())
139+
using (var overlay = new Image<Rgba32>(50, 50))
140+
{
141+
overlay.Mutate(x => x.Fill(Rgba32.Black));
142+
143+
const int xy = 75;
144+
Rgba32 backgroundPixel = background[xy - 1, xy - 1];
145+
Rgba32 overlayPixel = overlay[0, 0];
146+
147+
background.Mutate(x => x.DrawImage(overlay, new Point(xy, xy), PixelColorBlendingMode.Normal, 1F));
148+
149+
Assert.Equal(Rgba32.White, backgroundPixel);
150+
Assert.Equal(overlayPixel, background[xy, xy]);
151+
152+
background.DebugSave(provider, testOutputDetails: "PositiveTruncated");
153+
}
154+
}
155+
156+
[Theory]
157+
[WithSolidFilledImages(100, 100, 255, 255, 255, PixelTypes.Rgba32, -30, -30)]
158+
[WithSolidFilledImages(100, 100, 255, 255, 255, PixelTypes.Rgba32, 130, -30)]
159+
[WithSolidFilledImages(100, 100, 255, 255, 255, PixelTypes.Rgba32, 130, 130)]
160+
[WithSolidFilledImages(100, 100, 255, 255, 255, PixelTypes.Rgba32, -30, 130)]
161+
public void NonOverlappingImageThrows(TestImageProvider<Rgba32> provider, int x, int y)
162+
{
163+
using (Image<Rgba32> background = provider.GetImage())
164+
using (var overlay = new Image<Rgba32>(Configuration.Default, 10, 10, Rgba32.Black))
165+
{
166+
ImageProcessingException ex = Assert.Throws<ImageProcessingException>(Test);
167+
168+
Assert.Contains("does not overlap", ex.ToString());
169+
170+
void Test()
171+
{
172+
background.Mutate(context => context.DrawImage(overlay, new Point(x, y), GraphicsOptions.Default));
173+
}
174+
}
175+
}
134176

135177
private static void VerifyImage<TPixel>(
136178
TestImageProvider<TPixel> provider,

0 commit comments

Comments
 (0)