Browse Source

Port YccK and use alias

pull/2917/head
James Jackson-South 9 months ago
parent
commit
80fee7fa12
  1. 21
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.CmykVector512.cs
  2. 4
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.GrayScaleVector256.cs
  3. 8
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.GrayScaleVector512.cs
  4. 20
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YCbCrVector128.cs
  5. 20
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YCbCrVector256.cs
  6. 20
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YCbCrVector512.cs
  7. 8
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YccKScalar.cs
  8. 2
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YccKVector.cs
  9. 130
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YccKVector128.cs
  10. 130
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YccKVector256.cs
  11. 144
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YccKVector512.cs
  12. 14
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverterBase.cs
  13. 28
      tests/ImageSharp.Benchmarks/Codecs/Jpeg/ColorConversion/YccKColorConverter.cs
  14. 81
      tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs

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

@ -48,6 +48,17 @@ internal abstract partial class JpegColorConverterBase
/// <inheritdoc/>
protected override void ConvertFromRgbVectorized(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane)
=> ConvertFromRgbVectorized(in values, this.MaximumValue, rLane, gLane, bLane);
/// <inheritdoc/>
protected override void ConvertToRgbInPlaceScalarRemainder(in ComponentValues values)
=> CmykScalar.ConvertToRgbInplace(values, this.MaximumValue);
/// <inheritdoc/>
protected override void ConvertFromRgbScalarRemainder(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane)
=> CmykScalar.ConvertFromRgb(values, this.MaximumValue, rLane, gLane, bLane);
internal static void ConvertFromRgbVectorized(in ComponentValues values, float maxValue, Span<float> rLane, Span<float> gLane, Span<float> bLane)
{
ref Vector512<float> destC =
ref Unsafe.As<float, Vector512<float>>(ref MemoryMarshal.GetReference(values.Component0));
@ -65,7 +76,7 @@ internal abstract partial class JpegColorConverterBase
ref Vector512<float> srcB =
ref Unsafe.As<float, Vector512<float>>(ref MemoryMarshal.GetReference(bLane));
Vector512<float> scale = Vector512.Create(this.MaximumValue);
Vector512<float> scale = Vector512.Create(maxValue);
nuint n = values.Component0.Vector512Count<float>();
for (nuint i = 0; i < n; i++)
@ -86,13 +97,5 @@ internal abstract partial class JpegColorConverterBase
Unsafe.Add(ref destK, i) = scale - ktmp;
}
}
/// <inheritdoc/>
protected override void ConvertToRgbInPlaceScalarRemainder(in ComponentValues values)
=> CmykScalar.ConvertToRgbInplace(values, this.MaximumValue);
/// <inheritdoc/>
protected override void ConvertFromRgbScalarRemainder(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane)
=> CmykScalar.ConvertFromRgb(values, this.MaximumValue, rLane, gLane, bLane);
}
}

4
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.GrayScaleVector256.cs

@ -4,7 +4,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using SixLabors.ImageSharp.Common.Helpers;
using Vector256_ = SixLabors.ImageSharp.Common.Helpers.Vector256Utilities;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components;
@ -60,7 +60,7 @@ internal abstract partial class JpegColorConverterBase
ref Vector256<float> b = ref Unsafe.Add(ref srcBlue, i);
// luminosity = (0.299 * r) + (0.587 * g) + (0.114 * b)
Unsafe.Add(ref destLuminance, i) = Vector256Utilities.MultiplyAdd(Vector256Utilities.MultiplyAdd(f0114 * b, f0587, g), f0299, r);
Unsafe.Add(ref destLuminance, i) = Vector256_.MultiplyAdd(Vector256_.MultiplyAdd(f0114 * b, f0587, g), f0299, r);
}
}
}

8
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.GrayScaleVector512.cs

@ -4,7 +4,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using SixLabors.ImageSharp.Common.Helpers;
using Vector512_ = SixLabors.ImageSharp.Common.Helpers.Vector512Utilities;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components;
@ -60,7 +60,7 @@ internal abstract partial class JpegColorConverterBase
ref Vector512<float> b = ref Unsafe.Add(ref srcBlue, i);
// luminosity = (0.299 * r) + (0.587 * g) + (0.114 * b)
Unsafe.Add(ref destLuminance, i) = Vector512Utilities.MultiplyAdd(Vector512Utilities.MultiplyAdd(f0114 * b, f0587, g), f0299, r);
Unsafe.Add(ref destLuminance, i) = Vector512_.MultiplyAdd(Vector512_.MultiplyAdd(f0114 * b, f0587, g), f0299, r);
}
}
@ -69,7 +69,7 @@ internal abstract partial class JpegColorConverterBase
=> GrayScaleScalar.ConvertToRgbInPlace(values.Component0, this.MaximumValue);
/// <inheritdoc/>
protected override void ConvertFromRgbScalarRemainder(in ComponentValues values, Span<float> r, Span<float> g, Span<float> b)
=> GrayScaleScalar.ConvertFromRgbScalar(values, r, g, b);
protected override void ConvertFromRgbScalarRemainder(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane)
=> GrayScaleScalar.ConvertFromRgbScalar(values, rLane, gLane, bLane);
}
}

20
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YCbCrVector128.cs

@ -4,7 +4,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using SixLabors.ImageSharp.Common.Helpers;
using Vector128_ = SixLabors.ImageSharp.Common.Helpers.Vector128Utilities;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components;
@ -52,13 +52,13 @@ internal abstract partial class JpegColorConverterBase
// r = y + (1.402F * cr);
// g = y - (0.344136F * cb) - (0.714136F * cr);
// b = y + (1.772F * cb);
Vector128<float> r = Vector128Utilities.MultiplyAdd(y, cr, rCrMult);
Vector128<float> g = Vector128Utilities.MultiplyAdd(Vector128Utilities.MultiplyAdd(y, cb, gCbMult), cr, gCrMult);
Vector128<float> b = Vector128Utilities.MultiplyAdd(y, cb, bCbMult);
Vector128<float> r = Vector128_.MultiplyAdd(y, cr, rCrMult);
Vector128<float> g = Vector128_.MultiplyAdd(Vector128_.MultiplyAdd(y, cb, gCbMult), cr, gCrMult);
Vector128<float> b = Vector128_.MultiplyAdd(y, cb, bCbMult);
r = Vector128Utilities.RoundToNearestInteger(r) * scale;
g = Vector128Utilities.RoundToNearestInteger(g) * scale;
b = Vector128Utilities.RoundToNearestInteger(b) * scale;
r = Vector128_.RoundToNearestInteger(r) * scale;
g = Vector128_.RoundToNearestInteger(g) * scale;
b = Vector128_.RoundToNearestInteger(b) * scale;
c0 = r;
c1 = g;
@ -103,9 +103,9 @@ internal abstract partial class JpegColorConverterBase
// y = 0 + (0.299 * r) + (0.587 * g) + (0.114 * b)
// cb = 128 - (0.168736 * r) - (0.331264 * g) + (0.5 * b)
// cr = 128 + (0.5 * r) - (0.418688 * g) - (0.081312 * b)
Vector128<float> y = Vector128Utilities.MultiplyAdd(Vector128Utilities.MultiplyAdd(f0114 * b, f0587, g), f0299, r);
Vector128<float> cb = chromaOffset + Vector128Utilities.MultiplyAdd(Vector128Utilities.MultiplyAdd(f05 * b, fn0331264, g), fn0168736, r);
Vector128<float> cr = chromaOffset + Vector128Utilities.MultiplyAdd(Vector128Utilities.MultiplyAdd(fn0081312F * b, fn0418688, g), f05, r);
Vector128<float> y = Vector128_.MultiplyAdd(Vector128_.MultiplyAdd(f0114 * b, f0587, g), f0299, r);
Vector128<float> cb = chromaOffset + Vector128_.MultiplyAdd(Vector128_.MultiplyAdd(f05 * b, fn0331264, g), fn0168736, r);
Vector128<float> cr = chromaOffset + Vector128_.MultiplyAdd(Vector128_.MultiplyAdd(fn0081312F * b, fn0418688, g), f05, r);
Unsafe.Add(ref destY, i) = y;
Unsafe.Add(ref destCb, i) = cb;

20
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YCbCrVector256.cs

@ -4,7 +4,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using SixLabors.ImageSharp.Common.Helpers;
using Vector256_ = SixLabors.ImageSharp.Common.Helpers.Vector256Utilities;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components;
@ -52,13 +52,13 @@ internal abstract partial class JpegColorConverterBase
// r = y + (1.402F * cr);
// g = y - (0.344136F * cb) - (0.714136F * cr);
// b = y + (1.772F * cb);
Vector256<float> r = Vector256Utilities.MultiplyAdd(y, cr, rCrMult);
Vector256<float> g = Vector256Utilities.MultiplyAdd(Vector256Utilities.MultiplyAdd(y, cb, gCbMult), cr, gCrMult);
Vector256<float> b = Vector256Utilities.MultiplyAdd(y, cb, bCbMult);
Vector256<float> r = Vector256_.MultiplyAdd(y, cr, rCrMult);
Vector256<float> g = Vector256_.MultiplyAdd(Vector256_.MultiplyAdd(y, cb, gCbMult), cr, gCrMult);
Vector256<float> b = Vector256_.MultiplyAdd(y, cb, bCbMult);
r = Vector256Utilities.RoundToNearestInteger(r) * scale;
g = Vector256Utilities.RoundToNearestInteger(g) * scale;
b = Vector256Utilities.RoundToNearestInteger(b) * scale;
r = Vector256_.RoundToNearestInteger(r) * scale;
g = Vector256_.RoundToNearestInteger(g) * scale;
b = Vector256_.RoundToNearestInteger(b) * scale;
c0 = r;
c1 = g;
@ -103,9 +103,9 @@ internal abstract partial class JpegColorConverterBase
// y = 0 + (0.299 * r) + (0.587 * g) + (0.114 * b)
// cb = 128 - (0.168736 * r) - (0.331264 * g) + (0.5 * b)
// cr = 128 + (0.5 * r) - (0.418688 * g) - (0.081312 * b)
Vector256<float> y = Vector256Utilities.MultiplyAdd(Vector256Utilities.MultiplyAdd(f0114 * b, f0587, g), f0299, r);
Vector256<float> cb = chromaOffset + Vector256Utilities.MultiplyAdd(Vector256Utilities.MultiplyAdd(f05 * b, fn0331264, g), fn0168736, r);
Vector256<float> cr = chromaOffset + Vector256Utilities.MultiplyAdd(Vector256Utilities.MultiplyAdd(fn0081312F * b, fn0418688, g), f05, r);
Vector256<float> y = Vector256_.MultiplyAdd(Vector256_.MultiplyAdd(f0114 * b, f0587, g), f0299, r);
Vector256<float> cb = chromaOffset + Vector256_.MultiplyAdd(Vector256_.MultiplyAdd(f05 * b, fn0331264, g), fn0168736, r);
Vector256<float> cr = chromaOffset + Vector256_.MultiplyAdd(Vector256_.MultiplyAdd(fn0081312F * b, fn0418688, g), f05, r);
Unsafe.Add(ref destY, i) = y;
Unsafe.Add(ref destCb, i) = cb;

20
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YCbCrVector512.cs

@ -4,7 +4,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using SixLabors.ImageSharp.Common.Helpers;
using Vector512_ = SixLabors.ImageSharp.Common.Helpers.Vector512Utilities;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components;
@ -51,13 +51,13 @@ internal abstract partial class JpegColorConverterBase
// r = y + (1.402F * cr);
// g = y - (0.344136F * cb) - (0.714136F * cr);
// b = y + (1.772F * cb);
Vector512<float> r = Vector512Utilities.MultiplyAdd(y, cr, rCrMult);
Vector512<float> g = Vector512Utilities.MultiplyAdd(Vector512Utilities.MultiplyAdd(y, cb, gCbMult), cr, gCrMult);
Vector512<float> b = Vector512Utilities.MultiplyAdd(y, cb, bCbMult);
Vector512<float> r = Vector512_.MultiplyAdd(y, cr, rCrMult);
Vector512<float> g = Vector512_.MultiplyAdd(Vector512_.MultiplyAdd(y, cb, gCbMult), cr, gCrMult);
Vector512<float> b = Vector512_.MultiplyAdd(y, cb, bCbMult);
r = Vector512Utilities.RoundToNearestInteger(r) * scale;
g = Vector512Utilities.RoundToNearestInteger(g) * scale;
b = Vector512Utilities.RoundToNearestInteger(b) * scale;
r = Vector512_.RoundToNearestInteger(r) * scale;
g = Vector512_.RoundToNearestInteger(g) * scale;
b = Vector512_.RoundToNearestInteger(b) * scale;
c0 = r;
c1 = g;
@ -102,9 +102,9 @@ internal abstract partial class JpegColorConverterBase
// y = 0 + (0.299 * r) + (0.587 * g) + (0.114 * b)
// cb = 128 - (0.168736 * r) - (0.331264 * g) + (0.5 * b)
// cr = 128 + (0.5 * r) - (0.418688 * g) - (0.081312 * b)
Vector512<float> y = Vector512Utilities.MultiplyAdd(Vector512Utilities.MultiplyAdd(f0114 * b, f0587, g), f0299, r);
Vector512<float> cb = chromaOffset + Vector512Utilities.MultiplyAdd(Vector512Utilities.MultiplyAdd(f05 * b, fn0331264, g), fn0168736, r);
Vector512<float> cr = chromaOffset + Vector512Utilities.MultiplyAdd(Vector512Utilities.MultiplyAdd(fn0081312F * b, fn0418688, g), f05, r);
Vector512<float> y = Vector512_.MultiplyAdd(Vector512_.MultiplyAdd(f0114 * b, f0587, g), f0299, r);
Vector512<float> cb = chromaOffset + Vector512_.MultiplyAdd(Vector512_.MultiplyAdd(f05 * b, fn0331264, g), fn0168736, r);
Vector512<float> cr = chromaOffset + Vector512_.MultiplyAdd(Vector512_.MultiplyAdd(fn0081312F * b, fn0418688, g), f05, r);
Unsafe.Add(ref destY, i) = y;
Unsafe.Add(ref destCb, i) = cb;

8
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YccKScalar.cs

@ -20,13 +20,13 @@ internal abstract partial class JpegColorConverterBase
/// <inheritdoc/>
public override void ConvertToRgbInPlace(in ComponentValues values)
=> ConvertToRgpInplace(values, this.MaximumValue, this.HalfValue);
=> ConvertToRgpInPlace(values, this.MaximumValue, this.HalfValue);
/// <inheritdoc/>
public override void ConvertFromRgb(in ComponentValues values, Span<float> r, Span<float> g, Span<float> b)
=> ConvertFromRgb(values, this.HalfValue, this.MaximumValue, r, g, b);
public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane)
=> ConvertFromRgb(values, this.HalfValue, this.MaximumValue, rLane, gLane, bLane);
public static void ConvertToRgpInplace(in ComponentValues values, float maxValue, float halfValue)
public static void ConvertToRgpInPlace(in ComponentValues values, float maxValue, float halfValue)
{
Span<float> c0 = values.Component0;
Span<float> c1 = values.Component1;

2
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YccKVector.cs

@ -71,7 +71,7 @@ internal abstract partial class JpegColorConverterBase
/// <inheritdoc/>
protected override void ConvertToRgbInPlaceScalarRemainder(in ComponentValues values)
=> YccKScalar.ConvertToRgpInplace(values, this.MaximumValue, this.HalfValue);
=> YccKScalar.ConvertToRgpInPlace(values, this.MaximumValue, this.HalfValue);
/// <inheritdoc/>
protected override void ConvertFromRgbVectorized(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane)

130
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YccKVector128.cs

@ -0,0 +1,130 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using Vector128_ = SixLabors.ImageSharp.Common.Helpers.Vector128Utilities;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components;
internal abstract partial class JpegColorConverterBase
{
internal sealed class YccKVector128 : JpegColorConverterVector128
{
public YccKVector128(int precision)
: base(JpegColorSpace.Ycck, precision)
{
}
/// <inheritdoc/>
public override void ConvertToRgbInPlace(in ComponentValues values)
{
ref Vector128<float> c0Base =
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component0));
ref Vector128<float> c1Base =
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component1));
ref Vector128<float> c2Base =
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component2));
ref Vector128<float> kBase =
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component3));
// Used for the color conversion
Vector128<float> chromaOffset = Vector128.Create(-this.HalfValue);
Vector128<float> scale = Vector128.Create(1 / (this.MaximumValue * this.MaximumValue));
Vector128<float> max = Vector128.Create(this.MaximumValue);
Vector128<float> rCrMult = Vector128.Create(YCbCrScalar.RCrMult);
Vector128<float> gCbMult = Vector128.Create(-YCbCrScalar.GCbMult);
Vector128<float> gCrMult = Vector128.Create(-YCbCrScalar.GCrMult);
Vector128<float> bCbMult = Vector128.Create(YCbCrScalar.BCbMult);
// Walking 8 elements at one step:
nuint n = values.Component0.Vector128Count<float>();
for (nuint i = 0; i < n; i++)
{
// y = yVals[i];
// cb = cbVals[i] - 128F;
// cr = crVals[i] - 128F;
// k = kVals[i] / 256F;
ref Vector128<float> c0 = ref Unsafe.Add(ref c0Base, i);
ref Vector128<float> c1 = ref Unsafe.Add(ref c1Base, i);
ref Vector128<float> c2 = ref Unsafe.Add(ref c2Base, i);
Vector128<float> y = c0;
Vector128<float> cb = c1 + chromaOffset;
Vector128<float> cr = c2 + chromaOffset;
Vector128<float> scaledK = Unsafe.Add(ref kBase, i) * scale;
// r = y + (1.402F * cr);
// g = y - (0.344136F * cb) - (0.714136F * cr);
// b = y + (1.772F * cb);
Vector128<float> r = Vector128_.MultiplyAdd(y, cr, rCrMult);
Vector128<float> g = Vector128_.MultiplyAdd(Vector128_.MultiplyAdd(y, cb, gCbMult), cr, gCrMult);
Vector128<float> b = Vector128_.MultiplyAdd(y, cb, bCbMult);
r = max - Vector128_.RoundToNearestInteger(r);
g = max - Vector128_.RoundToNearestInteger(g);
b = max - Vector128_.RoundToNearestInteger(b);
r *= scaledK;
g *= scaledK;
b *= scaledK;
c0 = r;
c1 = g;
c2 = b;
}
}
/// <inheritdoc/>
public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane)
{
// rgb -> cmyk
CmykVector128.ConvertFromRgb(in values, this.MaximumValue, rLane, gLane, bLane);
// cmyk -> ycck
ref Vector128<float> destY =
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component0));
ref Vector128<float> destCb =
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component1));
ref Vector128<float> destCr =
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component2));
ref Vector128<float> srcR = ref destY;
ref Vector128<float> srcG = ref destCb;
ref Vector128<float> srcB = ref destCr;
// Used for the color conversion
Vector128<float> maxSampleValue = Vector128.Create(this.MaximumValue);
Vector128<float> chromaOffset = Vector128.Create(this.HalfValue);
Vector128<float> f0299 = Vector128.Create(0.299f);
Vector128<float> f0587 = Vector128.Create(0.587f);
Vector128<float> f0114 = Vector128.Create(0.114f);
Vector128<float> fn0168736 = Vector128.Create(-0.168736f);
Vector128<float> fn0331264 = Vector128.Create(-0.331264f);
Vector128<float> fn0418688 = Vector128.Create(-0.418688f);
Vector128<float> fn0081312F = Vector128.Create(-0.081312F);
Vector128<float> f05 = Vector128.Create(0.5f);
nuint n = values.Component0.Vector128Count<float>();
for (nuint i = 0; i < n; i++)
{
Vector128<float> r = maxSampleValue - Unsafe.Add(ref srcR, i);
Vector128<float> g = maxSampleValue - Unsafe.Add(ref srcG, i);
Vector128<float> b = maxSampleValue - Unsafe.Add(ref srcB, i);
// y = 0 + (0.299 * r) + (0.587 * g) + (0.114 * b)
// cb = 128 - (0.168736 * r) - (0.331264 * g) + (0.5 * b)
// cr = 128 + (0.5 * r) - (0.418688 * g) - (0.081312 * b)
Vector128<float> y = Vector128_.MultiplyAdd(Vector128_.MultiplyAdd(f0114 * b, f0587, g), f0299, r);
Vector128<float> cb = chromaOffset + Vector128_.MultiplyAdd(Vector128_.MultiplyAdd(f05 * b, fn0331264, g), fn0168736, r);
Vector128<float> cr = chromaOffset + Vector128_.MultiplyAdd(Vector128_.MultiplyAdd(fn0081312F * b, fn0418688, g), f05, r);
Unsafe.Add(ref destY, i) = y;
Unsafe.Add(ref destCb, i) = cb;
Unsafe.Add(ref destCr, i) = cr;
}
}
}
}

130
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YccKVector256.cs

@ -0,0 +1,130 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using Vector256_ = SixLabors.ImageSharp.Common.Helpers.Vector256Utilities;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components;
internal abstract partial class JpegColorConverterBase
{
internal sealed class YccKVector256 : JpegColorConverterVector256
{
public YccKVector256(int precision)
: base(JpegColorSpace.Ycck, precision)
{
}
/// <inheritdoc/>
public override void ConvertToRgbInPlace(in ComponentValues values)
{
ref Vector256<float> c0Base =
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component0));
ref Vector256<float> c1Base =
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component1));
ref Vector256<float> c2Base =
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component2));
ref Vector256<float> kBase =
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component3));
// Used for the color conversion
Vector256<float> chromaOffset = Vector256.Create(-this.HalfValue);
Vector256<float> scale = Vector256.Create(1 / (this.MaximumValue * this.MaximumValue));
Vector256<float> max = Vector256.Create(this.MaximumValue);
Vector256<float> rCrMult = Vector256.Create(YCbCrScalar.RCrMult);
Vector256<float> gCbMult = Vector256.Create(-YCbCrScalar.GCbMult);
Vector256<float> gCrMult = Vector256.Create(-YCbCrScalar.GCrMult);
Vector256<float> bCbMult = Vector256.Create(YCbCrScalar.BCbMult);
// Walking 8 elements at one step:
nuint n = values.Component0.Vector256Count<float>();
for (nuint i = 0; i < n; i++)
{
// y = yVals[i];
// cb = cbVals[i] - 128F;
// cr = crVals[i] - 128F;
// k = kVals[i] / 256F;
ref Vector256<float> c0 = ref Unsafe.Add(ref c0Base, i);
ref Vector256<float> c1 = ref Unsafe.Add(ref c1Base, i);
ref Vector256<float> c2 = ref Unsafe.Add(ref c2Base, i);
Vector256<float> y = c0;
Vector256<float> cb = c1 + chromaOffset;
Vector256<float> cr = c2 + chromaOffset;
Vector256<float> scaledK = Unsafe.Add(ref kBase, i) * scale;
// r = y + (1.402F * cr);
// g = y - (0.344136F * cb) - (0.714136F * cr);
// b = y + (1.772F * cb);
Vector256<float> r = Vector256_.MultiplyAdd(y, cr, rCrMult);
Vector256<float> g = Vector256_.MultiplyAdd(Vector256_.MultiplyAdd(y, cb, gCbMult), cr, gCrMult);
Vector256<float> b = Vector256_.MultiplyAdd(y, cb, bCbMult);
r = max - Vector256_.RoundToNearestInteger(r);
g = max - Vector256_.RoundToNearestInteger(g);
b = max - Vector256_.RoundToNearestInteger(b);
r *= scaledK;
g *= scaledK;
b *= scaledK;
c0 = r;
c1 = g;
c2 = b;
}
}
/// <inheritdoc/>
public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane)
{
// rgb -> cmyk
CmykVector256.ConvertFromRgb(in values, this.MaximumValue, rLane, gLane, bLane);
// cmyk -> ycck
ref Vector256<float> destY =
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component0));
ref Vector256<float> destCb =
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component1));
ref Vector256<float> destCr =
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component2));
ref Vector256<float> srcR = ref destY;
ref Vector256<float> srcG = ref destCb;
ref Vector256<float> srcB = ref destCr;
// Used for the color conversion
Vector256<float> maxSampleValue = Vector256.Create(this.MaximumValue);
Vector256<float> chromaOffset = Vector256.Create(this.HalfValue);
Vector256<float> f0299 = Vector256.Create(0.299f);
Vector256<float> f0587 = Vector256.Create(0.587f);
Vector256<float> f0114 = Vector256.Create(0.114f);
Vector256<float> fn0168736 = Vector256.Create(-0.168736f);
Vector256<float> fn0331264 = Vector256.Create(-0.331264f);
Vector256<float> fn0418688 = Vector256.Create(-0.418688f);
Vector256<float> fn0081312F = Vector256.Create(-0.081312F);
Vector256<float> f05 = Vector256.Create(0.5f);
nuint n = values.Component0.Vector256Count<float>();
for (nuint i = 0; i < n; i++)
{
Vector256<float> r = maxSampleValue - Unsafe.Add(ref srcR, i);
Vector256<float> g = maxSampleValue - Unsafe.Add(ref srcG, i);
Vector256<float> b = maxSampleValue - Unsafe.Add(ref srcB, i);
// y = 0 + (0.299 * r) + (0.587 * g) + (0.114 * b)
// cb = 128 - (0.168736 * r) - (0.331264 * g) + (0.5 * b)
// cr = 128 + (0.5 * r) - (0.418688 * g) - (0.081312 * b)
Vector256<float> y = Vector256_.MultiplyAdd(Vector256_.MultiplyAdd(f0114 * b, f0587, g), f0299, r);
Vector256<float> cb = chromaOffset + Vector256_.MultiplyAdd(Vector256_.MultiplyAdd(f05 * b, fn0331264, g), fn0168736, r);
Vector256<float> cr = chromaOffset + Vector256_.MultiplyAdd(Vector256_.MultiplyAdd(fn0081312F * b, fn0418688, g), f05, r);
Unsafe.Add(ref destY, i) = y;
Unsafe.Add(ref destCb, i) = cb;
Unsafe.Add(ref destCr, i) = cr;
}
}
}
}

144
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YccKVector512.cs

@ -0,0 +1,144 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using Vector512_ = SixLabors.ImageSharp.Common.Helpers.Vector512Utilities;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components;
internal abstract partial class JpegColorConverterBase
{
internal sealed class YccKVector512 : JpegColorConverterVector512
{
public YccKVector512(int precision)
: base(JpegColorSpace.Ycck, precision)
{
}
/// <inheritdoc/>
protected override void ConvertToRgbInPlaceVectorized(in ComponentValues values)
{
ref Vector512<float> c0Base =
ref Unsafe.As<float, Vector512<float>>(ref MemoryMarshal.GetReference(values.Component0));
ref Vector512<float> c1Base =
ref Unsafe.As<float, Vector512<float>>(ref MemoryMarshal.GetReference(values.Component1));
ref Vector512<float> c2Base =
ref Unsafe.As<float, Vector512<float>>(ref MemoryMarshal.GetReference(values.Component2));
ref Vector512<float> kBase =
ref Unsafe.As<float, Vector512<float>>(ref MemoryMarshal.GetReference(values.Component3));
// Used for the color conversion
Vector512<float> chromaOffset = Vector512.Create(-this.HalfValue);
Vector512<float> scale = Vector512.Create(1 / (this.MaximumValue * this.MaximumValue));
Vector512<float> max = Vector512.Create(this.MaximumValue);
Vector512<float> rCrMult = Vector512.Create(YCbCrScalar.RCrMult);
Vector512<float> gCbMult = Vector512.Create(-YCbCrScalar.GCbMult);
Vector512<float> gCrMult = Vector512.Create(-YCbCrScalar.GCrMult);
Vector512<float> bCbMult = Vector512.Create(YCbCrScalar.BCbMult);
// Walking 8 elements at one step:
nuint n = values.Component0.Vector512Count<float>();
for (nuint i = 0; i < n; i++)
{
// y = yVals[i];
// cb = cbVals[i] - 128F;
// cr = crVals[i] - 128F;
// k = kVals[i] / 256F;
ref Vector512<float> c0 = ref Unsafe.Add(ref c0Base, i);
ref Vector512<float> c1 = ref Unsafe.Add(ref c1Base, i);
ref Vector512<float> c2 = ref Unsafe.Add(ref c2Base, i);
Vector512<float> y = c0;
Vector512<float> cb = c1 + chromaOffset;
Vector512<float> cr = c2 + chromaOffset;
Vector512<float> scaledK = Unsafe.Add(ref kBase, i) * scale;
// r = y + (1.402F * cr);
// g = y - (0.344136F * cb) - (0.714136F * cr);
// b = y + (1.772F * cb);
Vector512<float> r = Vector512_.MultiplyAdd(y, cr, rCrMult);
Vector512<float> g = Vector512_.MultiplyAdd(Vector512_.MultiplyAdd(y, cb, gCbMult), cr, gCrMult);
Vector512<float> b = Vector512_.MultiplyAdd(y, cb, bCbMult);
r = max - Vector512_.RoundToNearestInteger(r);
g = max - Vector512_.RoundToNearestInteger(g);
b = max - Vector512_.RoundToNearestInteger(b);
r *= scaledK;
g *= scaledK;
b *= scaledK;
c0 = r;
c1 = g;
c2 = b;
}
}
/// <inheritdoc/>
protected override void ConvertToRgbInPlaceScalarRemainder(in ComponentValues values)
=> YccKScalar.ConvertToRgpInPlace(values, this.MaximumValue, this.HalfValue);
/// <inheritdoc/>
protected override void ConvertFromRgbVectorized(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane)
{
// rgb -> cmyk
CmykVector512.ConvertFromRgbVectorized(in values, this.MaximumValue, rLane, gLane, bLane);
// cmyk -> ycck
ref Vector512<float> destY =
ref Unsafe.As<float, Vector512<float>>(ref MemoryMarshal.GetReference(values.Component0));
ref Vector512<float> destCb =
ref Unsafe.As<float, Vector512<float>>(ref MemoryMarshal.GetReference(values.Component1));
ref Vector512<float> destCr =
ref Unsafe.As<float, Vector512<float>>(ref MemoryMarshal.GetReference(values.Component2));
ref Vector512<float> srcR = ref destY;
ref Vector512<float> srcG = ref destCb;
ref Vector512<float> srcB = ref destCr;
// Used for the color conversion
Vector512<float> maxSampleValue = Vector512.Create(this.MaximumValue);
Vector512<float> chromaOffset = Vector512.Create(this.HalfValue);
Vector512<float> f0299 = Vector512.Create(0.299f);
Vector512<float> f0587 = Vector512.Create(0.587f);
Vector512<float> f0114 = Vector512.Create(0.114f);
Vector512<float> fn0168736 = Vector512.Create(-0.168736f);
Vector512<float> fn0331264 = Vector512.Create(-0.331264f);
Vector512<float> fn0418688 = Vector512.Create(-0.418688f);
Vector512<float> fn0081312F = Vector512.Create(-0.081312F);
Vector512<float> f05 = Vector512.Create(0.5f);
nuint n = values.Component0.Vector512Count<float>();
for (nuint i = 0; i < n; i++)
{
Vector512<float> r = maxSampleValue - Unsafe.Add(ref srcR, i);
Vector512<float> g = maxSampleValue - Unsafe.Add(ref srcG, i);
Vector512<float> b = maxSampleValue - Unsafe.Add(ref srcB, i);
// y = 0 + (0.299 * r) + (0.587 * g) + (0.114 * b)
// cb = 128 - (0.168736 * r) - (0.331264 * g) + (0.5 * b)
// cr = 128 + (0.5 * r) - (0.418688 * g) - (0.081312 * b)
Vector512<float> y = Vector512_.MultiplyAdd(Vector512_.MultiplyAdd(f0114 * b, f0587, g), f0299, r);
Vector512<float> cb = chromaOffset + Vector512_.MultiplyAdd(Vector512_.MultiplyAdd(f05 * b, fn0331264, g), fn0168736, r);
Vector512<float> cr = chromaOffset + Vector512_.MultiplyAdd(Vector512_.MultiplyAdd(fn0081312F * b, fn0418688, g), f05, r);
Unsafe.Add(ref destY, i) = y;
Unsafe.Add(ref destCb, i) = cb;
Unsafe.Add(ref destCr, i) = cr;
}
}
/// <inheritdoc/>
protected override void ConvertFromRgbScalarRemainder(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane)
{
// rgb -> cmyk
CmykScalar.ConvertFromRgb(in values, this.MaximumValue, rLane, gLane, bLane);
// cmyk -> ycck
YccKScalar.ConvertFromRgb(in values, this.HalfValue, this.MaximumValue, rLane, gLane, bLane);
}
}
}

14
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverterBase.cs

@ -140,9 +140,19 @@ internal abstract partial class JpegColorConverterBase
/// <param name="precision">The precision in bits.</param>
private static JpegColorConverterBase GetYccKConverter(int precision)
{
if (JpegColorConverterVector.IsSupported)
if (JpegColorConverterVector512.IsSupported)
{
return new YccKVector512(precision);
}
if (JpegColorConverterVector256.IsSupported)
{
return new YccKVector256(precision);
}
if (JpegColorConverterVector128.IsSupported)
{
return new YccKVector(precision);
return new YccKVector128(precision);
}
return new YccKScalar(precision);

28
tests/ImageSharp.Benchmarks/Codecs/Jpeg/ColorConversion/YccKColorConverter.cs

@ -17,7 +17,7 @@ public class YccKColorConverter : ColorConversionBenchmark
[Benchmark(Baseline = true)]
public void Scalar()
{
var values = new JpegColorConverterBase.ComponentValues(this.Input, 0);
JpegColorConverterBase.ComponentValues values = new(this.Input, 0);
new JpegColorConverterBase.YccKScalar(8).ConvertToRgbInPlace(values);
}
@ -25,8 +25,32 @@ public class YccKColorConverter : ColorConversionBenchmark
[Benchmark]
public void SimdVector8()
{
var values = new JpegColorConverterBase.ComponentValues(this.Input, 0);
JpegColorConverterBase.ComponentValues values = new(this.Input, 0);
new JpegColorConverterBase.YccKVector(8).ConvertToRgbInPlace(values);
}
[Benchmark]
public void SimdVector128()
{
JpegColorConverterBase.ComponentValues values = new(this.Input, 0);
new JpegColorConverterBase.YccKVector128(8).ConvertToRgbInPlace(values);
}
[Benchmark]
public void SimdVector256()
{
JpegColorConverterBase.ComponentValues values = new(this.Input, 0);
new JpegColorConverterBase.YccKVector256(8).ConvertToRgbInPlace(values);
}
[Benchmark]
public void SimdVector512()
{
JpegColorConverterBase.ComponentValues values = new(this.Input, 0);
new JpegColorConverterBase.YccKVector512(8).ConvertToRgbInPlace(values);
}
}

81
tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs

@ -213,17 +213,17 @@ public class JpegColorConverterTests
{
// arrange
Type expectedType = typeof(JpegColorConverterBase.YccKScalar);
if (Avx.IsSupported)
if (JpegColorConverterBase.JpegColorConverterVector512.IsSupported)
{
expectedType = typeof(JpegColorConverterBase.YccKVector);
expectedType = typeof(JpegColorConverterBase.YccKVector512);
}
else if (Sse2.IsSupported)
else if (JpegColorConverterBase.JpegColorConverterVector256.IsSupported)
{
expectedType = typeof(JpegColorConverterBase.YccKVector);
expectedType = typeof(JpegColorConverterBase.YccKVector256);
}
else if (AdvSimd.Arm64.IsSupported)
else if (JpegColorConverterBase.JpegColorConverterVector128.IsSupported)
{
expectedType = typeof(JpegColorConverterBase.YccKVector);
expectedType = typeof(JpegColorConverterBase.YccKVector128);
}
// act
@ -474,29 +474,60 @@ public class JpegColorConverterTests
[Theory]
[MemberData(nameof(Seeds))]
public void FromYccKVector(int seed)
{
JpegColorConverterBase.YccKVector converter = new(8);
public void FromYccKVector512(int seed) =>
this.TestConversionToRgb(
new JpegColorConverterBase.YccKVector512(8),
4,
seed,
new JpegColorConverterBase.YccKScalar(8));
if (!converter.IsAvailable)
{
this.Output.WriteLine(
$"Skipping test - {converter.GetType().Name} is not supported on current hardware.");
return;
}
[Theory]
[MemberData(nameof(Seeds))]
public void FromYccKVector256(int seed) =>
this.TestConversionToRgb(
new JpegColorConverterBase.YccKVector256(8),
4,
seed,
new JpegColorConverterBase.YccKScalar(8));
FeatureTestRunner.RunWithHwIntrinsicsFeature(
RunTest,
[Theory]
[MemberData(nameof(Seeds))]
public void FromYccKVector128(int seed) =>
this.TestConversionToRgb(
new JpegColorConverterBase.YccKVector128(8),
4,
seed,
IntrinsicsConfig);
new JpegColorConverterBase.YccKScalar(8));
static void RunTest(string arg) =>
ValidateConversionToRgb(
new JpegColorConverterBase.YccKVector(8),
4,
FeatureTestRunner.Deserialize<int>(arg),
new JpegColorConverterBase.YccKScalar(8));
}
[Theory]
[MemberData(nameof(Seeds))]
public void FromRgbToYccKVector512(int seed) =>
this.TestConversionFromRgb(
new JpegColorConverterBase.YccKVector512(8),
4,
seed,
new JpegColorConverterBase.YccKScalar(8),
precision: 2);
[Theory]
[MemberData(nameof(Seeds))]
public void FromRgbToYccKVector256(int seed) =>
this.TestConversionFromRgb(
new JpegColorConverterBase.YccKVector256(8),
4,
seed,
new JpegColorConverterBase.YccKScalar(8),
precision: 2);
[Theory]
[MemberData(nameof(Seeds))]
public void FromRgbToYccKVector128(int seed) =>
this.TestConversionFromRgb(
new JpegColorConverterBase.YccKVector128(8),
4,
seed,
new JpegColorConverterBase.YccKScalar(8),
precision: 2);
private void TestConversionToRgb(
JpegColorConverterBase converter,

Loading…
Cancel
Save