Skip to content

Commit 57614df

Browse files
Port RGB converter
1 parent 80fee7f commit 57614df

14 files changed

+254
-426
lines changed

src/ImageSharp/Common/Helpers/SimdUtils.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@ internal static Vector4 PseudoRound(this Vector4 v)
4040
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4141
internal static Vector<float> RoundToNearestInteger(this Vector<float> v)
4242
{
43-
if (Avx512F.IsSupported && Vector<float>.Count == Vector512<float>.Count)
44-
{
45-
ref Vector512<float> v512 = ref Unsafe.As<Vector<float>, Vector512<float>>(ref v);
46-
47-
// imm8 = 0b1000:
48-
// imm8[7:4] = 0b0000 -> preserve 0 fractional bits (round to whole numbers)
49-
// imm8[3:0] = 0b1000 -> _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC (round to nearest even, suppress exceptions)
50-
Vector512<float> vRound = Avx512F.RoundScale(v512, 0b0000_1000);
51-
return Unsafe.As<Vector512<float>, Vector<float>>(ref vRound);
52-
}
53-
5443
if (Avx2.IsSupported && Vector<float>.Count == Vector256<float>.Count)
5544
{
5645
ref Vector256<float> v256 = ref Unsafe.As<Vector<float>, Vector256<float>>(ref v);

src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.CmykScalar.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ public CmykScalar(int precision)
1414

1515
/// <inheritdoc/>
1616
public override void ConvertToRgbInPlace(in ComponentValues values) =>
17-
ConvertToRgbInplace(values, this.MaximumValue);
17+
ConvertToRgbInPlace(values, this.MaximumValue);
1818

1919
/// <inheritdoc/>
2020
public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane)
2121
=> ConvertFromRgb(values, this.MaximumValue, rLane, gLane, bLane);
2222

23-
public static void ConvertToRgbInplace(in ComponentValues values, float maxValue)
23+
public static void ConvertToRgbInPlace(in ComponentValues values, float maxValue)
2424
{
2525
Span<float> c0 = values.Component0;
2626
Span<float> c1 = values.Component1;

src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.CmykVector.cs

Lines changed: 0 additions & 104 deletions
This file was deleted.

src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.CmykVector512.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected override void ConvertFromRgbVectorized(in ComponentValues values, Span
5252

5353
/// <inheritdoc/>
5454
protected override void ConvertToRgbInPlaceScalarRemainder(in ComponentValues values)
55-
=> CmykScalar.ConvertToRgbInplace(values, this.MaximumValue);
55+
=> CmykScalar.ConvertToRgbInPlace(values, this.MaximumValue);
5656

5757
/// <inheritdoc/>
5858
protected override void ConvertFromRgbScalarRemainder(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane)

src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.GrayScaleVector.cs

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.RgbVector.cs

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using System.Runtime.CompilerServices;
5+
using System.Runtime.InteropServices;
6+
using System.Runtime.Intrinsics;
7+
8+
namespace SixLabors.ImageSharp.Formats.Jpeg.Components;
9+
10+
internal abstract partial class JpegColorConverterBase
11+
{
12+
internal sealed class RgbVector128 : JpegColorConverterVector128
13+
{
14+
public RgbVector128(int precision)
15+
: base(JpegColorSpace.RGB, precision)
16+
{
17+
}
18+
19+
/// <inheritdoc/>
20+
public override void ConvertToRgbInPlace(in ComponentValues values)
21+
{
22+
ref Vector128<float> rBase =
23+
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component0));
24+
ref Vector128<float> gBase =
25+
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component1));
26+
ref Vector128<float> bBase =
27+
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component2));
28+
29+
// Used for the color conversion
30+
Vector128<float> scale = Vector128.Create(1 / this.MaximumValue);
31+
nuint n = values.Component0.Vector128Count<float>();
32+
for (nuint i = 0; i < n; i++)
33+
{
34+
ref Vector128<float> r = ref Unsafe.Add(ref rBase, i);
35+
ref Vector128<float> g = ref Unsafe.Add(ref gBase, i);
36+
ref Vector128<float> b = ref Unsafe.Add(ref bBase, i);
37+
r *= scale;
38+
g *= scale;
39+
b *= scale;
40+
}
41+
}
42+
43+
/// <inheritdoc/>
44+
public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane)
45+
{
46+
rLane.CopyTo(values.Component0);
47+
gLane.CopyTo(values.Component1);
48+
bLane.CopyTo(values.Component2);
49+
}
50+
}
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using System.Runtime.CompilerServices;
5+
using System.Runtime.InteropServices;
6+
using System.Runtime.Intrinsics;
7+
8+
namespace SixLabors.ImageSharp.Formats.Jpeg.Components;
9+
10+
internal abstract partial class JpegColorConverterBase
11+
{
12+
internal sealed class RgbVector256 : JpegColorConverterVector256
13+
{
14+
public RgbVector256(int precision)
15+
: base(JpegColorSpace.RGB, precision)
16+
{
17+
}
18+
19+
/// <inheritdoc/>
20+
public override void ConvertToRgbInPlace(in ComponentValues values)
21+
{
22+
ref Vector256<float> rBase =
23+
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component0));
24+
ref Vector256<float> gBase =
25+
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component1));
26+
ref Vector256<float> bBase =
27+
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component2));
28+
29+
// Used for the color conversion
30+
Vector256<float> scale = Vector256.Create(1 / this.MaximumValue);
31+
nuint n = values.Component0.Vector256Count<float>();
32+
for (nuint i = 0; i < n; i++)
33+
{
34+
ref Vector256<float> r = ref Unsafe.Add(ref rBase, i);
35+
ref Vector256<float> g = ref Unsafe.Add(ref gBase, i);
36+
ref Vector256<float> b = ref Unsafe.Add(ref bBase, i);
37+
r *= scale;
38+
g *= scale;
39+
b *= scale;
40+
}
41+
}
42+
43+
/// <inheritdoc/>
44+
public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane)
45+
{
46+
rLane.CopyTo(values.Component0);
47+
gLane.CopyTo(values.Component1);
48+
bLane.CopyTo(values.Component2);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)