Skip to content

Commit 32d08ba

Browse files
Update benchmarks and simplify color numeric bulk colormatrix
1 parent 9a48aaa commit 32d08ba

23 files changed

+97
-113
lines changed

src/ImageSharp/Common/Helpers/ColorNumerics.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Numerics;
55
using System.Runtime.CompilerServices;
6-
using System.Runtime.InteropServices;
76

87
namespace SixLabors.ImageSharp;
98

@@ -17,7 +16,7 @@ internal static class ColorNumerics
1716
/// Vector for converting pixel to gray value as specified by
1817
/// ITU-R Recommendation BT.709.
1918
/// </summary>
20-
private static readonly Vector4 Bt709 = new Vector4(.2126f, .7152f, .0722f, 0.0f);
19+
private static readonly Vector4 Bt709 = new(.2126f, .7152f, .0722f, 0.0f);
2120

2221
/// <summary>
2322
/// Convert a pixel value to grayscale using ITU-R Recommendation BT.709.
@@ -167,11 +166,9 @@ public static void Transform(ref Vector4 vector, ref ColorMatrix matrix)
167166
[MethodImpl(MethodImplOptions.AggressiveInlining)]
168167
public static void Transform(Span<Vector4> vectors, ref ColorMatrix matrix)
169168
{
170-
ref Vector4 baseRef = ref MemoryMarshal.GetReference(vectors);
171-
172169
for (int i = 0; i < vectors.Length; i++)
173170
{
174-
ref Vector4 v = ref Unsafe.Add(ref baseRef, i);
171+
ref Vector4 v = ref vectors[i];
175172
Transform(ref v, ref matrix);
176173
}
177174
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using System.Numerics;
5+
using BenchmarkDotNet.Attributes;
6+
using SixLabors.ImageSharp.Processing;
7+
8+
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
9+
10+
public class ColorMatrixTransforms
11+
{
12+
private static readonly Vector4[] Vectors = Vector4Factory.CreateVectors();
13+
14+
[Benchmark(Baseline = true)]
15+
public void Transform()
16+
{
17+
ColorMatrix matrix = KnownFilterMatrices.CreateHueFilter(45F);
18+
for (int i = 0; i < Vectors.Length; i++)
19+
{
20+
ref Vector4 input = ref Vectors[i];
21+
ColorNumerics.Transform(ref input, ref matrix);
22+
}
23+
}
24+
25+
[Benchmark]
26+
public void Transform_Span()
27+
{
28+
ColorMatrix matrix = KnownFilterMatrices.CreateHueFilter(45F);
29+
ColorNumerics.Transform(Vectors.AsSpan(), ref matrix);
30+
}
31+
}

tests/ImageSharp.Benchmarks/Color/Bulk/FromRgba32Bytes.cs renamed to tests/ImageSharp.Benchmarks/Bulk/FromRgba32Bytes.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using SixLabors.ImageSharp.PixelFormats;
99

1010
// ReSharper disable InconsistentNaming
11-
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
11+
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
1212

1313
public abstract class FromRgba32Bytes<TPixel>
1414
where TPixel : unmanaged, IPixel<TPixel>
@@ -64,7 +64,7 @@ public void CommonBulk()
6464
[Benchmark]
6565
public void OptimizedBulk()
6666
{
67-
PixelOperations<TPixel>.Instance.FromRgba32Bytes(this.configuration, this.source.GetSpan(), this.destination.GetSpan(), this.Count);
67+
PixelOperations<TPixel>.Instance.FromRgba32Bytes(this.configuration, this.source.GetSpan(), this.destination.GetSpan(), this.Count);
6868
}
6969
}
7070

tests/ImageSharp.Benchmarks/Color/Bulk/FromVector4.cs renamed to tests/ImageSharp.Benchmarks/Bulk/FromVector4.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
using SixLabors.ImageSharp.PixelFormats;
1313

1414
// ReSharper disable InconsistentNaming
15-
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
15+
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
1616

1717
[Config(typeof(Config.ShortCore31))]
1818
public abstract class FromVector4<TPixel>

tests/ImageSharp.Benchmarks/Color/Bulk/FromVector4_Rgb24.cs renamed to tests/ImageSharp.Benchmarks/Bulk/FromVector4_Rgb24.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using BenchmarkDotNet.Attributes;
55
using SixLabors.ImageSharp.PixelFormats;
66

7-
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
7+
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
88

99
[Config(typeof(Config.ShortMultiFramework))]
1010
public class FromVector4_Rgb24 : FromVector4<Rgb24>

tests/ImageSharp.Benchmarks/Color/Bulk/Pad3Shuffle4Channel.cs renamed to tests/ImageSharp.Benchmarks/Bulk/Pad3Shuffle4Channel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using BenchmarkDotNet.Attributes;
55

6-
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
6+
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
77

88
[Config(typeof(Config.HwIntrinsics_SSE_AVX))]
99
public class Pad3Shuffle4Channel

tests/ImageSharp.Benchmarks/Color/Bulk/PremultiplyVector4.cs renamed to tests/ImageSharp.Benchmarks/Bulk/PremultiplyVector4.cs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
using System.Runtime.InteropServices;
77
using BenchmarkDotNet.Attributes;
88

9-
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
9+
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
1010

1111
public class PremultiplyVector4
1212
{
13-
private static readonly Vector4[] Vectors = CreateVectors();
13+
private static readonly Vector4[] Vectors = Vector4Factory.CreateVectors();
1414

1515
[Benchmark(Baseline = true)]
1616
public void PremultiplyBaseline()
@@ -46,29 +46,4 @@ private static void Premultiply(ref Vector4 source)
4646
source *= w;
4747
source.W = w;
4848
}
49-
50-
private static Vector4[] CreateVectors()
51-
{
52-
Random rnd = new(42);
53-
return GenerateRandomVectorArray(rnd, 2048, 0, 1);
54-
}
55-
56-
private static Vector4[] GenerateRandomVectorArray(Random rnd, int length, float minVal, float maxVal)
57-
{
58-
Vector4[] values = new Vector4[length];
59-
60-
for (int i = 0; i < length; i++)
61-
{
62-
ref Vector4 v = ref values[i];
63-
v.X = GetRandomFloat(rnd, minVal, maxVal);
64-
v.Y = GetRandomFloat(rnd, minVal, maxVal);
65-
v.Z = GetRandomFloat(rnd, minVal, maxVal);
66-
v.W = GetRandomFloat(rnd, minVal, maxVal);
67-
}
68-
69-
return values;
70-
}
71-
72-
private static float GetRandomFloat(Random rnd, float minVal, float maxVal)
73-
=> ((float)rnd.NextDouble() * (maxVal - minVal)) + minVal;
7449
}

tests/ImageSharp.Benchmarks/Color/Bulk/Rgb24Bytes.cs renamed to tests/ImageSharp.Benchmarks/Bulk/Rgb24Bytes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using SixLabors.ImageSharp.PixelFormats;
88

99
// ReSharper disable InconsistentNaming
10-
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
10+
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
1111

1212
public abstract class Rgb24Bytes<TPixel>
1313
where TPixel : unmanaged, IPixel<TPixel>

tests/ImageSharp.Benchmarks/Color/Bulk/Shuffle3Channel.cs renamed to tests/ImageSharp.Benchmarks/Bulk/Shuffle3Channel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using BenchmarkDotNet.Attributes;
55

6-
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
6+
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
77

88
[Config(typeof(Config.HwIntrinsics_SSE_AVX))]
99
public class Shuffle3Channel

tests/ImageSharp.Benchmarks/Color/Bulk/Shuffle4Slice3Channel.cs renamed to tests/ImageSharp.Benchmarks/Bulk/Shuffle4Slice3Channel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using BenchmarkDotNet.Attributes;
55

6-
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
6+
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
77

88
[Config(typeof(Config.HwIntrinsics_SSE_AVX))]
99
public class Shuffle4Slice3Channel

0 commit comments

Comments
 (0)