From ace4fbc229e98f29fb84576b4e8e020f793a8550 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Mon, 20 Dec 2021 02:19:13 +0100 Subject: [PATCH] Numerics.IsOutOfRangeZeroToMax --- src/ImageSharp/Common/Helpers/Numerics.cs | 10 ++++++++++ src/ImageSharp/ImageFrame{TPixel}.cs | 4 ++-- src/ImageSharp/Image{TPixel}.cs | 4 ++-- src/ImageSharp/Memory/Buffer2D{T}.cs | 2 +- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/Numerics.cs b/src/ImageSharp/Common/Helpers/Numerics.cs index 8f82476e1..f88c54f0d 100644 --- a/src/ImageSharp/Common/Helpers/Numerics.cs +++ b/src/ImageSharp/Common/Helpers/Numerics.cs @@ -970,7 +970,17 @@ namespace SixLabors.ImageSharp /// Value. /// Mininum value, inclusive. /// Maximum value, inclusive. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsOutOfRange(int value, int min, int max) => (uint)(value - min) > (uint)(max - min); + + /// + /// Tells whether input value is outside of the 0..max range. + /// + /// Value. + /// Maximum value, inclusive. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsOutOfRangeZeroToMax(int value, int max) + => (uint)value > (uint)max; } } diff --git a/src/ImageSharp/ImageFrame{TPixel}.cs b/src/ImageSharp/ImageFrame{TPixel}.cs index 4753e90e0..fe026b3eb 100644 --- a/src/ImageSharp/ImageFrame{TPixel}.cs +++ b/src/ImageSharp/ImageFrame{TPixel}.cs @@ -443,12 +443,12 @@ namespace SixLabors.ImageSharp [MethodImpl(InliningOptions.ShortMethod)] private void VerifyCoords(int x, int y) { - if (x < 0 || x >= this.Width) + if (Numerics.IsOutOfRangeZeroToMax(x, this.Width)) { ThrowArgumentOutOfRangeException(nameof(x)); } - if (y < 0 || y >= this.Height) + if (Numerics.IsOutOfRangeZeroToMax(y, this.Height)) { ThrowArgumentOutOfRangeException(nameof(y)); } diff --git a/src/ImageSharp/Image{TPixel}.cs b/src/ImageSharp/Image{TPixel}.cs index d01706b01..b25fa0d6a 100644 --- a/src/ImageSharp/Image{TPixel}.cs +++ b/src/ImageSharp/Image{TPixel}.cs @@ -452,12 +452,12 @@ namespace SixLabors.ImageSharp [MethodImpl(InliningOptions.ShortMethod)] private void VerifyCoords(int x, int y) { - if (x < 0 || x >= this.Width) + if (Numerics.IsOutOfRangeZeroToMax(x, this.Width)) { ThrowArgumentOutOfRangeException(nameof(x)); } - if (y < 0 || y >= this.Height) + if (Numerics.IsOutOfRangeZeroToMax(y, this.Height)) { ThrowArgumentOutOfRangeException(nameof(y)); } diff --git a/src/ImageSharp/Memory/Buffer2D{T}.cs b/src/ImageSharp/Memory/Buffer2D{T}.cs index 03d708d75..bc97add5c 100644 --- a/src/ImageSharp/Memory/Buffer2D{T}.cs +++ b/src/ImageSharp/Memory/Buffer2D{T}.cs @@ -97,7 +97,7 @@ namespace SixLabors.ImageSharp.Memory [MethodImpl(InliningOptions.ShortMethod)] public Span DangerousGetRowSpan(int y) { - if (y < 0 || y >= this.Height) + if (Numerics.IsOutOfRangeZeroToMax(y, this.Height)) { this.ThrowYOutOfRangeException(y); }