From 6c7b59fc06cf8da9af070cad8aebae059cb68e47 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sat, 9 Dec 2023 10:43:21 +0000 Subject: [PATCH] reduce to a single par of clamping operations --- .../DrawImageProcessor{TPixelBg,TPixelFg}.cs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor{TPixelBg,TPixelFg}.cs b/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor{TPixelBg,TPixelFg}.cs index e2aca1d90..031bcbdf7 100644 --- a/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor{TPixelBg,TPixelFg}.cs +++ b/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor{TPixelBg,TPixelFg}.cs @@ -96,19 +96,9 @@ internal class DrawImageProcessor : ImageProcessor top = 0; } - if (left > source.Width - foregroundRectangle.Width) - { - // will overhange, lets trim it down - int diff = (left + foregroundRectangle.Width) - source.Width; - foregroundRectangle.Width -= diff; - } - - if (top > source.Height - foregroundRectangle.Height) - { - // will overhange, lets trim it down - int diff = (top + foregroundRectangle.Height) - source.Height; - foregroundRectangle.Height -= diff; - } + // clamp the height/width to the availible space left to prevent overflowing + foregroundRectangle.Width = Math.Min(source.Width - left, foregroundRectangle.Width); + foregroundRectangle.Height = Math.Min(source.Height - top, foregroundRectangle.Height); int width = foregroundRectangle.Width; int height = foregroundRectangle.Height;