diff --git a/src/ImageSharp/Formats/Heif/Av1/Av1Math.cs b/src/ImageSharp/Formats/Heif/Av1/Av1Math.cs index e3ad3242b5..3da7249611 100644 --- a/src/ImageSharp/Formats/Heif/Av1/Av1Math.cs +++ b/src/ImageSharp/Formats/Heif/Av1/Av1Math.cs @@ -93,35 +93,9 @@ internal static class Av1Math public static uint Clip1(uint value, int bitDepth) => Clip3(0, (1U << bitDepth) - 1, value); - public static uint Clip3(uint x, uint y, uint z) - { - if (z < x) - { - return x; - } - - if (z > y) - { - return y; - } - - return z; - } + public static uint Clip3(uint min, uint max, uint value) => Math.Max(min, Math.Min(max, value)); - public static int Clip3(int x, int y, int z) - { - if (z < x) - { - return x; - } - - if (z > y) - { - return y; - } - - return z; - } + public static int Clip3(int min, int max, int value) => Math.Max(min, Math.Min(max, value)); public static uint Round2(uint value, int n) { diff --git a/tests/ImageSharp.Tests/Formats/Heif/Av1/Av1MathTests.cs b/tests/ImageSharp.Tests/Formats/Heif/Av1/Av1MathTests.cs index 0da23e0f36..a68f3d0367 100644 --- a/tests/ImageSharp.Tests/Formats/Heif/Av1/Av1MathTests.cs +++ b/tests/ImageSharp.Tests/Formats/Heif/Av1/Av1MathTests.cs @@ -108,4 +108,16 @@ public class Av1MathTests Av1Math.SetBit(ref actual, n); Assert.Equal(expected, actual); } + + [Theory] + [InlineData(255, 4, 4)] + [InlineData(255, -1, 0)] + [InlineData(255, 255, 255)] + [InlineData(255, 256, 255)] + [InlineData(255, 1000, 255)] + public void TestClip3(int max, int value, int expected) + { + int actual = Av1Math.Clip3(0, max, value); + Assert.Equal(expected, actual); + } }