Browse Source

Numerics.IsOutOfRangeZeroToMax

pull/1901/head
Anton Firszov 4 years ago
parent
commit
ace4fbc229
  1. 10
      src/ImageSharp/Common/Helpers/Numerics.cs
  2. 4
      src/ImageSharp/ImageFrame{TPixel}.cs
  3. 4
      src/ImageSharp/Image{TPixel}.cs
  4. 2
      src/ImageSharp/Memory/Buffer2D{T}.cs

10
src/ImageSharp/Common/Helpers/Numerics.cs

@ -970,7 +970,17 @@ namespace SixLabors.ImageSharp
/// <param name="value">Value.</param>
/// <param name="min">Mininum value, inclusive.</param>
/// <param name="max">Maximum value, inclusive.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsOutOfRange(int value, int min, int max)
=> (uint)(value - min) > (uint)(max - min);
/// <summary>
/// Tells whether input value is outside of the 0..max range.
/// </summary>
/// <param name="value">Value.</param>
/// <param name="max">Maximum value, inclusive.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsOutOfRangeZeroToMax(int value, int max)
=> (uint)value > (uint)max;
}
}

4
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));
}

4
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));
}

2
src/ImageSharp/Memory/Buffer2D{T}.cs

@ -97,7 +97,7 @@ namespace SixLabors.ImageSharp.Memory
[MethodImpl(InliningOptions.ShortMethod)]
public Span<T> DangerousGetRowSpan(int y)
{
if (y < 0 || y >= this.Height)
if (Numerics.IsOutOfRangeZeroToMax(y, this.Height))
{
this.ThrowYOutOfRangeException(y);
}

Loading…
Cancel
Save