From 4f8ea7f677d2b857aedd87b0644c93dfe8e3592b Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 24 Nov 2023 23:11:40 +1000 Subject: [PATCH] Tweak bounds clamping --- src/ImageSharp/Formats/AnimationUtilities.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Formats/AnimationUtilities.cs b/src/ImageSharp/Formats/AnimationUtilities.cs index b66efd7f5..a3a5292a2 100644 --- a/src/ImageSharp/Formats/AnimationUtilities.cs +++ b/src/ImageSharp/Formats/AnimationUtilities.cs @@ -148,11 +148,12 @@ internal static class AnimationUtilities PixelOperations.Instance.FromVector4Destructive(configuration, result, resultFrame.DangerousGetPixelRowMemory(y).Span, PixelConversionModifiers.Scale); } - Rectangle bounds = Rectangle.FromLTRB( - left = Numerics.Clamp(left, 0, resultFrame.Width - 1), - top = Numerics.Clamp(top, 0, resultFrame.Height - 1), - Numerics.Clamp(right, left + 1, resultFrame.Width), - Numerics.Clamp(bottom, top + 1, resultFrame.Height)); + left = Math.Max(0, Math.Min(left, resultFrame.Width - 1)); + top = Math.Max(0, Math.Min(top, resultFrame.Height - 1)); + right = Math.Max(left + 1, Math.Min(right, resultFrame.Width)); + bottom = Math.Max(top + 1, Math.Min(bottom, resultFrame.Height)); + + Rectangle bounds = Rectangle.FromLTRB(left, top, right, bottom); // Webp requires even bounds if (clampingMode == ClampingMode.Even)