Skip to content

Commit b0ff7cf

Browse files
committed
copy companding refactor from #847
1 parent 877f314 commit b0ff7cf

File tree

5 files changed

+68
-26
lines changed

5 files changed

+68
-26
lines changed

src/ImageSharp/ColorSpaces/Companding/SRgbCompanding.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ public static void Expand(Span<Vector4> vectors)
3030
for (int i = 0; i < vectors.Length; i++)
3131
{
3232
ref Vector4 v = ref Unsafe.Add(ref baseRef, i);
33-
v.X = Expand(v.X);
34-
v.Y = Expand(v.Y);
35-
v.Z = Expand(v.Z);
33+
Expand(ref v);
3634
}
3735
}
3836

@@ -48,27 +46,33 @@ public static void Compress(Span<Vector4> vectors)
4846
for (int i = 0; i < vectors.Length; i++)
4947
{
5048
ref Vector4 v = ref Unsafe.Add(ref baseRef, i);
51-
v.X = Compress(v.X);
52-
v.Y = Compress(v.Y);
53-
v.Z = Compress(v.Z);
49+
Compress(ref v);
5450
}
5551
}
5652

5753
/// <summary>
5854
/// Expands a companded vector to its linear equivalent with respect to the energy.
5955
/// </summary>
6056
/// <param name="vector">The vector.</param>
61-
/// <returns>The <see cref="Vector4"/> representing the linear channel values.</returns>
6257
[MethodImpl(InliningOptions.ShortMethod)]
63-
public static Vector4 Expand(Vector4 vector) => new Vector4(Expand(vector.X), Expand(vector.Y), Expand(vector.Z), vector.W);
58+
public static void Expand(ref Vector4 vector)
59+
{
60+
vector.X = Expand(vector.X);
61+
vector.Y = Expand(vector.Y);
62+
vector.Z = Expand(vector.Z);
63+
}
6464

6565
/// <summary>
6666
/// Compresses an uncompanded vector (linear) to its nonlinear equivalent.
6767
/// </summary>
6868
/// <param name="vector">The vector.</param>
69-
/// <returns>The <see cref="Vector4"/> representing the nonlinear channel values.</returns>
7069
[MethodImpl(InliningOptions.ShortMethod)]
71-
public static Vector4 Compress(Vector4 vector) => new Vector4(Compress(vector.X), Compress(vector.Y), Compress(vector.Z), vector.W);
70+
public static void Compress(ref Vector4 vector)
71+
{
72+
vector.X = Compress(vector.X);
73+
vector.Y = Compress(vector.Y);
74+
vector.Z = Compress(vector.Z);
75+
}
7276

7377
/// <summary>
7478
/// Expands a companded channel to its linear equivalent with respect to the energy.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System;
5+
6+
namespace SixLabors.ImageSharp.PixelFormats
7+
{
8+
[Flags]
9+
internal enum PixelConversionModifiers
10+
{
11+
None = 0,
12+
Scale = 1 << 0,
13+
Premultiply = 1 << 1,
14+
SRgbCompand = 1 << 2,
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.Runtime.CompilerServices;
5+
6+
namespace SixLabors.ImageSharp.PixelFormats
7+
{
8+
internal static class PixelConversionModifiersExtensions
9+
{
10+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11+
public static bool IsDefined(this PixelConversionModifiers modifiers, PixelConversionModifiers expected) =>
12+
(modifiers & expected) == expected;
13+
14+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
15+
public static PixelConversionModifiers Remove(
16+
this PixelConversionModifiers modifiers,
17+
PixelConversionModifiers removeThis) =>
18+
modifiers & ~removeThis;
19+
}
20+
}

src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,6 @@
99

1010
namespace SixLabors.ImageSharp.PixelFormats
1111
{
12-
internal enum PixelConversionModifiers
13-
{
14-
None = 0,
15-
Scale = 1 << 0,
16-
Premultiply = 1 << 1,
17-
SRgbCompand = 1 << 2,
18-
}
19-
20-
internal static class PixelConversionModifiersExtensions
21-
{
22-
public static bool IsDefined(this PixelConversionModifiers modifiers, PixelConversionModifiers expected) =>
23-
(modifiers & expected) == expected;
24-
}
25-
2612
/// <summary>
2713
/// A stateless class implementing Strategy Pattern for batched pixel-data conversion operations
2814
/// for pixel buffers of type <typeparamref name="TPixel"/>.

tests/ImageSharp.Tests/Colorspaces/Companding/CompandingTests.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@ public void SRgbCompanding_Expand_VectorSpan(int length)
5353
{
5454
var rnd = new Random(42);
5555
Vector4[] source = rnd.GenerateRandomVectorArray(length, 0, 1);
56-
Vector4[] expected = source.Select(v => SRgbCompanding.Expand(v)).ToArray();
56+
var expected = new Vector4[source.Length];
57+
58+
for (int i = 0; i < source.Length; i++)
59+
{
60+
Vector4 s = source[i];
61+
ref Vector4 e = ref expected[i];
62+
SRgbCompanding.Expand(ref s);
63+
e = s;
64+
}
5765

5866
SRgbCompanding.Expand(source);
5967

@@ -68,7 +76,15 @@ public void SRgbCompanding_Compress_VectorSpan(int length)
6876
{
6977
var rnd = new Random(42);
7078
Vector4[] source = rnd.GenerateRandomVectorArray(length, 0, 1);
71-
Vector4[] expected = source.Select(v => SRgbCompanding.Compress(v)).ToArray();
79+
var expected = new Vector4[source.Length];
80+
81+
for (int i = 0; i < source.Length; i++)
82+
{
83+
Vector4 s = source[i];
84+
ref Vector4 e = ref expected[i];
85+
SRgbCompanding.Compress(ref s);
86+
e = s;
87+
}
7288

7389
SRgbCompanding.Compress(source);
7490

0 commit comments

Comments
 (0)