Browse Source

Clipping performance improvement

pull/2633/head
Ynse Hoornenborg 2 years ago
parent
commit
ca4d842b75
  1. 30
      src/ImageSharp/Formats/Heif/Av1/Av1Math.cs
  2. 12
      tests/ImageSharp.Tests/Formats/Heif/Av1/Av1MathTests.cs

30
src/ImageSharp/Formats/Heif/Av1/Av1Math.cs

@ -93,35 +93,9 @@ internal static class Av1Math
public static uint Clip1(uint value, int bitDepth) => public static uint Clip1(uint value, int bitDepth) =>
Clip3(0, (1U << bitDepth) - 1, value); Clip3(0, (1U << bitDepth) - 1, value);
public static uint Clip3(uint x, uint y, uint z) public static uint Clip3(uint min, uint max, uint value) => Math.Max(min, Math.Min(max, value));
{
if (z < x)
{
return x;
}
if (z > y)
{
return y;
}
return z;
}
public static int Clip3(int x, int y, int z) public static int Clip3(int min, int max, int value) => Math.Max(min, Math.Min(max, value));
{
if (z < x)
{
return x;
}
if (z > y)
{
return y;
}
return z;
}
public static uint Round2(uint value, int n) public static uint Round2(uint value, int n)
{ {

12
tests/ImageSharp.Tests/Formats/Heif/Av1/Av1MathTests.cs

@ -108,4 +108,16 @@ public class Av1MathTests
Av1Math.SetBit(ref actual, n); Av1Math.SetBit(ref actual, n);
Assert.Equal(expected, actual); 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);
}
} }

Loading…
Cancel
Save