mirror of https://github.com/SixLabors/ImageSharp
29 changed files with 99 additions and 1467 deletions
@ -1,95 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Six Labors Split License.
|
|
||||
|
|
||||
using System.Runtime.CompilerServices; |
|
||||
using System.Runtime.InteropServices; |
|
||||
using System.Runtime.Intrinsics; |
|
||||
using System.Runtime.Intrinsics.Arm; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
|
||||
|
|
||||
internal abstract partial class JpegColorConverterBase |
|
||||
{ |
|
||||
internal sealed class CmykArm64 : JpegColorConverterArm64 |
|
||||
{ |
|
||||
public CmykArm64(int precision) |
|
||||
: base(JpegColorSpace.Cmyk, 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> c3Base = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component3)); |
|
||||
|
|
||||
// Used for the color conversion
|
|
||||
var scale = Vector128.Create(1 / (this.MaximumValue * this.MaximumValue)); |
|
||||
|
|
||||
nint n = (nint)(uint)values.Component0.Length / Vector128<float>.Count; |
|
||||
for (nint i = 0; i < n; i++) |
|
||||
{ |
|
||||
ref Vector128<float> c = ref Unsafe.Add(ref c0Base, i); |
|
||||
ref Vector128<float> m = ref Unsafe.Add(ref c1Base, i); |
|
||||
ref Vector128<float> y = ref Unsafe.Add(ref c2Base, i); |
|
||||
Vector128<float> k = Unsafe.Add(ref c3Base, i); |
|
||||
|
|
||||
k = AdvSimd.Multiply(k, scale); |
|
||||
c = AdvSimd.Multiply(c, k); |
|
||||
m = AdvSimd.Multiply(m, k); |
|
||||
y = AdvSimd.Multiply(y, k); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane) |
|
||||
=> ConvertFromRgb(in values, this.MaximumValue, rLane, gLane, bLane); |
|
||||
|
|
||||
public static void ConvertFromRgb(in ComponentValues values, float maxValue, Span<float> rLane, Span<float> gLane, Span<float> bLane) |
|
||||
{ |
|
||||
ref Vector128<float> destC = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component0)); |
|
||||
ref Vector128<float> destM = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component1)); |
|
||||
ref Vector128<float> destY = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component2)); |
|
||||
ref Vector128<float> destK = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component3)); |
|
||||
|
|
||||
ref Vector128<float> srcR = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(rLane)); |
|
||||
ref Vector128<float> srcG = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(gLane)); |
|
||||
ref Vector128<float> srcB = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(bLane)); |
|
||||
|
|
||||
var scale = Vector128.Create(maxValue); |
|
||||
|
|
||||
nint n = (nint)(uint)values.Component0.Length / Vector128<float>.Count; |
|
||||
for (nint i = 0; i < n; i++) |
|
||||
{ |
|
||||
Vector128<float> ctmp = AdvSimd.Subtract(scale, Unsafe.Add(ref srcR, i)); |
|
||||
Vector128<float> mtmp = AdvSimd.Subtract(scale, Unsafe.Add(ref srcG, i)); |
|
||||
Vector128<float> ytmp = AdvSimd.Subtract(scale, Unsafe.Add(ref srcB, i)); |
|
||||
Vector128<float> ktmp = AdvSimd.Min(ctmp, AdvSimd.Min(mtmp, ytmp)); |
|
||||
|
|
||||
Vector128<float> kMask = AdvSimd.Not(AdvSimd.CompareEqual(ktmp, scale)); |
|
||||
|
|
||||
ctmp = AdvSimd.And(AdvSimd.Arm64.Divide(AdvSimd.Subtract(ctmp, ktmp), AdvSimd.Subtract(scale, ktmp)), kMask); |
|
||||
mtmp = AdvSimd.And(AdvSimd.Arm64.Divide(AdvSimd.Subtract(mtmp, ktmp), AdvSimd.Subtract(scale, ktmp)), kMask); |
|
||||
ytmp = AdvSimd.And(AdvSimd.Arm64.Divide(AdvSimd.Subtract(ytmp, ktmp), AdvSimd.Subtract(scale, ktmp)), kMask); |
|
||||
|
|
||||
Unsafe.Add(ref destC, i) = AdvSimd.Subtract(scale, AdvSimd.Multiply(ctmp, scale)); |
|
||||
Unsafe.Add(ref destM, i) = AdvSimd.Subtract(scale, AdvSimd.Multiply(mtmp, scale)); |
|
||||
Unsafe.Add(ref destY, i) = AdvSimd.Subtract(scale, AdvSimd.Multiply(ytmp, scale)); |
|
||||
Unsafe.Add(ref destK, i) = AdvSimd.Subtract(scale, ktmp); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,95 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Six Labors Split License.
|
|
||||
|
|
||||
using System.Runtime.CompilerServices; |
|
||||
using System.Runtime.InteropServices; |
|
||||
using System.Runtime.Intrinsics; |
|
||||
using System.Runtime.Intrinsics.X86; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
|
||||
|
|
||||
internal abstract partial class JpegColorConverterBase |
|
||||
{ |
|
||||
internal sealed class CmykAvx : JpegColorConverterAvx |
|
||||
{ |
|
||||
public CmykAvx(int precision) |
|
||||
: base(JpegColorSpace.Cmyk, 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> c3Base = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component3)); |
|
||||
|
|
||||
// Used for the color conversion
|
|
||||
var scale = Vector256.Create(1 / (this.MaximumValue * this.MaximumValue)); |
|
||||
|
|
||||
nuint n = values.Component0.Vector256Count<float>(); |
|
||||
for (nuint i = 0; i < n; i++) |
|
||||
{ |
|
||||
ref Vector256<float> c = ref Unsafe.Add(ref c0Base, i); |
|
||||
ref Vector256<float> m = ref Unsafe.Add(ref c1Base, i); |
|
||||
ref Vector256<float> y = ref Unsafe.Add(ref c2Base, i); |
|
||||
Vector256<float> k = Unsafe.Add(ref c3Base, i); |
|
||||
|
|
||||
k = Avx.Multiply(k, scale); |
|
||||
c = Avx.Multiply(c, k); |
|
||||
m = Avx.Multiply(m, k); |
|
||||
y = Avx.Multiply(y, k); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane) |
|
||||
=> ConvertFromRgb(in values, this.MaximumValue, rLane, gLane, bLane); |
|
||||
|
|
||||
public static void ConvertFromRgb(in ComponentValues values, float maxValue, Span<float> rLane, Span<float> gLane, Span<float> bLane) |
|
||||
{ |
|
||||
ref Vector256<float> destC = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component0)); |
|
||||
ref Vector256<float> destM = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component1)); |
|
||||
ref Vector256<float> destY = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component2)); |
|
||||
ref Vector256<float> destK = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component3)); |
|
||||
|
|
||||
ref Vector256<float> srcR = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(rLane)); |
|
||||
ref Vector256<float> srcG = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(gLane)); |
|
||||
ref Vector256<float> srcB = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(bLane)); |
|
||||
|
|
||||
var scale = Vector256.Create(maxValue); |
|
||||
|
|
||||
nuint n = values.Component0.Vector256Count<float>(); |
|
||||
for (nuint i = 0; i < n; i++) |
|
||||
{ |
|
||||
Vector256<float> ctmp = Avx.Subtract(scale, Unsafe.Add(ref srcR, i)); |
|
||||
Vector256<float> mtmp = Avx.Subtract(scale, Unsafe.Add(ref srcG, i)); |
|
||||
Vector256<float> ytmp = Avx.Subtract(scale, Unsafe.Add(ref srcB, i)); |
|
||||
Vector256<float> ktmp = Avx.Min(ctmp, Avx.Min(mtmp, ytmp)); |
|
||||
|
|
||||
Vector256<float> kMask = Avx.CompareNotEqual(ktmp, scale); |
|
||||
|
|
||||
ctmp = Avx.And(Avx.Divide(Avx.Subtract(ctmp, ktmp), Avx.Subtract(scale, ktmp)), kMask); |
|
||||
mtmp = Avx.And(Avx.Divide(Avx.Subtract(mtmp, ktmp), Avx.Subtract(scale, ktmp)), kMask); |
|
||||
ytmp = Avx.And(Avx.Divide(Avx.Subtract(ytmp, ktmp), Avx.Subtract(scale, ktmp)), kMask); |
|
||||
|
|
||||
Unsafe.Add(ref destC, i) = Avx.Subtract(scale, Avx.Multiply(ctmp, scale)); |
|
||||
Unsafe.Add(ref destM, i) = Avx.Subtract(scale, Avx.Multiply(mtmp, scale)); |
|
||||
Unsafe.Add(ref destY, i) = Avx.Subtract(scale, Avx.Multiply(ytmp, scale)); |
|
||||
Unsafe.Add(ref destK, i) = Avx.Subtract(scale, ktmp); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,68 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Six Labors Split License.
|
|
||||
|
|
||||
using System.Runtime.CompilerServices; |
|
||||
using System.Runtime.InteropServices; |
|
||||
using System.Runtime.Intrinsics; |
|
||||
using System.Runtime.Intrinsics.Arm; |
|
||||
using static SixLabors.ImageSharp.SimdUtils; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
|
||||
|
|
||||
internal abstract partial class JpegColorConverterBase |
|
||||
{ |
|
||||
internal sealed class GrayscaleArm : JpegColorConverterArm |
|
||||
{ |
|
||||
public GrayscaleArm(int precision) |
|
||||
: base(JpegColorSpace.Grayscale, precision) |
|
||||
{ |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public override void ConvertToRgbInplace(in ComponentValues values) |
|
||||
{ |
|
||||
ref Vector128<float> c0Base = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component0)); |
|
||||
|
|
||||
// Used for the color conversion
|
|
||||
var scale = Vector128.Create(1 / this.MaximumValue); |
|
||||
|
|
||||
nuint n = values.Component0.Vector128Count<float>(); |
|
||||
for (nuint i = 0; i < n; i++) |
|
||||
{ |
|
||||
ref Vector128<float> c0 = ref Unsafe.Add(ref c0Base, i); |
|
||||
c0 = AdvSimd.Multiply(c0, scale); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane) |
|
||||
{ |
|
||||
ref Vector128<float> destLuminance = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component0)); |
|
||||
|
|
||||
ref Vector128<float> srcRed = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(rLane)); |
|
||||
ref Vector128<float> srcGreen = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(gLane)); |
|
||||
ref Vector128<float> srcBlue = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(bLane)); |
|
||||
|
|
||||
// Used for the color conversion
|
|
||||
var f0299 = Vector128.Create(0.299f); |
|
||||
var f0587 = Vector128.Create(0.587f); |
|
||||
var f0114 = Vector128.Create(0.114f); |
|
||||
|
|
||||
nuint n = values.Component0.Vector128Count<float>(); |
|
||||
for (nuint i = 0; i < n; i++) |
|
||||
{ |
|
||||
ref Vector128<float> r = ref Unsafe.Add(ref srcRed, i); |
|
||||
ref Vector128<float> g = ref Unsafe.Add(ref srcGreen, i); |
|
||||
ref Vector128<float> b = ref Unsafe.Add(ref srcBlue, i); |
|
||||
|
|
||||
// luminocity = (0.299 * r) + (0.587 * g) + (0.114 * b)
|
|
||||
Unsafe.Add(ref destLuminance, i) = HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(AdvSimd.Multiply(f0114, b), f0587, g), f0299, r); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,68 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Six Labors Split License.
|
|
||||
|
|
||||
using System.Runtime.CompilerServices; |
|
||||
using System.Runtime.InteropServices; |
|
||||
using System.Runtime.Intrinsics; |
|
||||
using System.Runtime.Intrinsics.X86; |
|
||||
using static SixLabors.ImageSharp.SimdUtils; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
|
||||
|
|
||||
internal abstract partial class JpegColorConverterBase |
|
||||
{ |
|
||||
internal sealed class GrayscaleAvx : JpegColorConverterAvx |
|
||||
{ |
|
||||
public GrayscaleAvx(int precision) |
|
||||
: base(JpegColorSpace.Grayscale, precision) |
|
||||
{ |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public override void ConvertToRgbInplace(in ComponentValues values) |
|
||||
{ |
|
||||
ref Vector256<float> c0Base = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component0)); |
|
||||
|
|
||||
// Used for the color conversion
|
|
||||
var scale = Vector256.Create(1 / this.MaximumValue); |
|
||||
|
|
||||
nuint n = values.Component0.Vector256Count<float>(); |
|
||||
for (nuint i = 0; i < n; i++) |
|
||||
{ |
|
||||
ref Vector256<float> c0 = ref Unsafe.Add(ref c0Base, i); |
|
||||
c0 = Avx.Multiply(c0, scale); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane) |
|
||||
{ |
|
||||
ref Vector256<float> destLuminance = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component0)); |
|
||||
|
|
||||
ref Vector256<float> srcRed = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(rLane)); |
|
||||
ref Vector256<float> srcGreen = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(gLane)); |
|
||||
ref Vector256<float> srcBlue = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(bLane)); |
|
||||
|
|
||||
// Used for the color conversion
|
|
||||
var f0299 = Vector256.Create(0.299f); |
|
||||
var f0587 = Vector256.Create(0.587f); |
|
||||
var f0114 = Vector256.Create(0.114f); |
|
||||
|
|
||||
nuint n = values.Component0.Vector256Count<float>(); |
|
||||
for (nuint i = 0; i < n; i++) |
|
||||
{ |
|
||||
ref Vector256<float> r = ref Unsafe.Add(ref srcRed, i); |
|
||||
ref Vector256<float> g = ref Unsafe.Add(ref srcGreen, i); |
|
||||
ref Vector256<float> b = ref Unsafe.Add(ref srcBlue, i); |
|
||||
|
|
||||
// luminocity = (0.299 * r) + (0.587 * g) + (0.114 * b)
|
|
||||
Unsafe.Add(ref destLuminance, i) = HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(Avx.Multiply(f0114, b), f0587, g), f0299, r); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,53 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Six Labors Split License.
|
|
||||
|
|
||||
using System.Runtime.CompilerServices; |
|
||||
using System.Runtime.InteropServices; |
|
||||
using System.Runtime.Intrinsics; |
|
||||
using System.Runtime.Intrinsics.Arm; |
|
||||
using System.Runtime.Intrinsics.X86; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
|
||||
|
|
||||
internal abstract partial class JpegColorConverterBase |
|
||||
{ |
|
||||
internal sealed class RgbArm : JpegColorConverterArm |
|
||||
{ |
|
||||
public RgbArm(int precision) |
|
||||
: base(JpegColorSpace.RGB, precision) |
|
||||
{ |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public override void ConvertToRgbInplace(in ComponentValues values) |
|
||||
{ |
|
||||
ref Vector128<float> rBase = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component0)); |
|
||||
ref Vector128<float> gBase = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component1)); |
|
||||
ref Vector128<float> bBase = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component2)); |
|
||||
|
|
||||
// Used for the color conversion
|
|
||||
var scale = Vector128.Create(1 / this.MaximumValue); |
|
||||
nuint n = values.Component0.Vector128Count<float>(); |
|
||||
for (nuint i = 0; i < n; i++) |
|
||||
{ |
|
||||
ref Vector128<float> r = ref Unsafe.Add(ref rBase, i); |
|
||||
ref Vector128<float> g = ref Unsafe.Add(ref gBase, i); |
|
||||
ref Vector128<float> b = ref Unsafe.Add(ref bBase, i); |
|
||||
r = AdvSimd.Multiply(r, scale); |
|
||||
g = AdvSimd.Multiply(g, scale); |
|
||||
b = AdvSimd.Multiply(b, scale); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane) |
|
||||
{ |
|
||||
rLane.CopyTo(values.Component0); |
|
||||
gLane.CopyTo(values.Component1); |
|
||||
bLane.CopyTo(values.Component2); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,52 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Six Labors Split License.
|
|
||||
|
|
||||
using System.Runtime.CompilerServices; |
|
||||
using System.Runtime.InteropServices; |
|
||||
using System.Runtime.Intrinsics; |
|
||||
using System.Runtime.Intrinsics.X86; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
|
||||
|
|
||||
internal abstract partial class JpegColorConverterBase |
|
||||
{ |
|
||||
internal sealed class RgbAvx : JpegColorConverterAvx |
|
||||
{ |
|
||||
public RgbAvx(int precision) |
|
||||
: base(JpegColorSpace.RGB, precision) |
|
||||
{ |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public override void ConvertToRgbInplace(in ComponentValues values) |
|
||||
{ |
|
||||
ref Vector256<float> rBase = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component0)); |
|
||||
ref Vector256<float> gBase = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component1)); |
|
||||
ref Vector256<float> bBase = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component2)); |
|
||||
|
|
||||
// Used for the color conversion
|
|
||||
var scale = Vector256.Create(1 / this.MaximumValue); |
|
||||
nuint n = values.Component0.Vector256Count<float>(); |
|
||||
for (nuint i = 0; i < n; i++) |
|
||||
{ |
|
||||
ref Vector256<float> r = ref Unsafe.Add(ref rBase, i); |
|
||||
ref Vector256<float> g = ref Unsafe.Add(ref gBase, i); |
|
||||
ref Vector256<float> b = ref Unsafe.Add(ref bBase, i); |
|
||||
r = Avx.Multiply(r, scale); |
|
||||
g = Avx.Multiply(g, scale); |
|
||||
b = Avx.Multiply(b, scale); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane) |
|
||||
{ |
|
||||
rLane.CopyTo(values.Component0); |
|
||||
gLane.CopyTo(values.Component1); |
|
||||
bLane.CopyTo(values.Component2); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,122 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Six Labors Split License.
|
|
||||
|
|
||||
using System.Runtime.CompilerServices; |
|
||||
using System.Runtime.InteropServices; |
|
||||
using System.Runtime.Intrinsics; |
|
||||
using System.Runtime.Intrinsics.Arm; |
|
||||
using System.Runtime.Intrinsics.X86; |
|
||||
using static SixLabors.ImageSharp.SimdUtils; |
|
||||
|
|
||||
// ReSharper disable ImpureMethodCallOnReadonlyValueField
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
|
||||
|
|
||||
internal abstract partial class JpegColorConverterBase |
|
||||
{ |
|
||||
internal sealed class YCbCrArm : JpegColorConverterArm |
|
||||
{ |
|
||||
public YCbCrArm(int precision) |
|
||||
: base(JpegColorSpace.YCbCr, 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)); |
|
||||
|
|
||||
// Used for the color conversion
|
|
||||
var chromaOffset = Vector128.Create(-this.HalfValue); |
|
||||
var scale = Vector128.Create(1 / this.MaximumValue); |
|
||||
var rCrMult = Vector128.Create(YCbCrScalar.RCrMult); |
|
||||
var gCbMult = Vector128.Create(-YCbCrScalar.GCbMult); |
|
||||
var gCrMult = Vector128.Create(-YCbCrScalar.GCrMult); |
|
||||
var bCbMult = Vector128.Create(YCbCrScalar.BCbMult); |
|
||||
|
|
||||
// Walking 8 elements at one step:
|
|
||||
nuint n = (uint)values.Component0.Length / (uint)Vector128<float>.Count; |
|
||||
for (nuint i = 0; i < n; i++) |
|
||||
{ |
|
||||
// y = yVals[i];
|
|
||||
// cb = cbVals[i] - 128F;
|
|
||||
// cr = crVals[i] - 128F;
|
|
||||
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 = AdvSimd.Add(c1, chromaOffset); |
|
||||
Vector128<float> cr = AdvSimd.Add(c2, chromaOffset); |
|
||||
|
|
||||
// r = y + (1.402F * cr);
|
|
||||
// g = y - (0.344136F * cb) - (0.714136F * cr);
|
|
||||
// b = y + (1.772F * cb);
|
|
||||
Vector128<float> r = HwIntrinsics.MultiplyAdd(y, cr, rCrMult); |
|
||||
Vector128<float> g = HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(y, cb, gCbMult), cr, gCrMult); |
|
||||
Vector128<float> b = HwIntrinsics.MultiplyAdd(y, cb, bCbMult); |
|
||||
|
|
||||
r = AdvSimd.Multiply(AdvSimd.RoundToNearest(r), scale); |
|
||||
g = AdvSimd.Multiply(AdvSimd.RoundToNearest(g), scale); |
|
||||
b = AdvSimd.Multiply(AdvSimd.RoundToNearest(b), scale); |
|
||||
|
|
||||
c0 = r; |
|
||||
c1 = g; |
|
||||
c2 = b; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane) |
|
||||
{ |
|
||||
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 Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(rLane)); |
|
||||
ref Vector128<float> srcG = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(gLane)); |
|
||||
ref Vector128<float> srcB = |
|
||||
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(bLane)); |
|
||||
|
|
||||
// Used for the color conversion
|
|
||||
var chromaOffset = Vector128.Create(this.HalfValue); |
|
||||
|
|
||||
var f0299 = Vector128.Create(0.299f); |
|
||||
var f0587 = Vector128.Create(0.587f); |
|
||||
var f0114 = Vector128.Create(0.114f); |
|
||||
var fn0168736 = Vector128.Create(-0.168736f); |
|
||||
var fn0331264 = Vector128.Create(-0.331264f); |
|
||||
var fn0418688 = Vector128.Create(-0.418688f); |
|
||||
var fn0081312F = Vector128.Create(-0.081312F); |
|
||||
var f05 = Vector128.Create(0.5f); |
|
||||
|
|
||||
nuint n = (uint)values.Component0.Length / (uint)Vector128<float>.Count; |
|
||||
for (nuint i = 0; i < n; i++) |
|
||||
{ |
|
||||
Vector128<float> r = Unsafe.Add(ref srcR, i); |
|
||||
Vector128<float> g = Unsafe.Add(ref srcG, i); |
|
||||
Vector128<float> b = 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 = HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(AdvSimd.Multiply(f0114, b), f0587, g), f0299, r); |
|
||||
Vector128<float> cb = AdvSimd.Add(chromaOffset, HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(AdvSimd.Multiply(f05, b), fn0331264, g), fn0168736, r)); |
|
||||
Vector128<float> cr = AdvSimd.Add(chromaOffset, HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(AdvSimd.Multiply(fn0081312F, b), fn0418688, g), f05, r)); |
|
||||
|
|
||||
Unsafe.Add(ref destY, i) = y; |
|
||||
Unsafe.Add(ref destCb, i) = cb; |
|
||||
Unsafe.Add(ref destCr, i) = cr; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,121 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Six Labors Split License.
|
|
||||
|
|
||||
using System.Runtime.CompilerServices; |
|
||||
using System.Runtime.InteropServices; |
|
||||
using System.Runtime.Intrinsics; |
|
||||
using System.Runtime.Intrinsics.X86; |
|
||||
using static SixLabors.ImageSharp.SimdUtils; |
|
||||
|
|
||||
// ReSharper disable ImpureMethodCallOnReadonlyValueField
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
|
||||
|
|
||||
internal abstract partial class JpegColorConverterBase |
|
||||
{ |
|
||||
internal sealed class YCbCrAvx : JpegColorConverterAvx |
|
||||
{ |
|
||||
public YCbCrAvx(int precision) |
|
||||
: base(JpegColorSpace.YCbCr, 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)); |
|
||||
|
|
||||
// Used for the color conversion
|
|
||||
var chromaOffset = Vector256.Create(-this.HalfValue); |
|
||||
var scale = Vector256.Create(1 / this.MaximumValue); |
|
||||
var rCrMult = Vector256.Create(YCbCrScalar.RCrMult); |
|
||||
var gCbMult = Vector256.Create(-YCbCrScalar.GCbMult); |
|
||||
var gCrMult = Vector256.Create(-YCbCrScalar.GCrMult); |
|
||||
var 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;
|
|
||||
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 = Avx.Add(c1, chromaOffset); |
|
||||
Vector256<float> cr = Avx.Add(c2, chromaOffset); |
|
||||
|
|
||||
// r = y + (1.402F * cr);
|
|
||||
// g = y - (0.344136F * cb) - (0.714136F * cr);
|
|
||||
// b = y + (1.772F * cb);
|
|
||||
Vector256<float> r = HwIntrinsics.MultiplyAdd(y, cr, rCrMult); |
|
||||
Vector256<float> g = HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(y, cb, gCbMult), cr, gCrMult); |
|
||||
Vector256<float> b = HwIntrinsics.MultiplyAdd(y, cb, bCbMult); |
|
||||
|
|
||||
r = Avx.Multiply(Avx.RoundToNearestInteger(r), scale); |
|
||||
g = Avx.Multiply(Avx.RoundToNearestInteger(g), scale); |
|
||||
b = Avx.Multiply(Avx.RoundToNearestInteger(b), scale); |
|
||||
|
|
||||
c0 = r; |
|
||||
c1 = g; |
|
||||
c2 = b; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane) |
|
||||
{ |
|
||||
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 Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(rLane)); |
|
||||
ref Vector256<float> srcG = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(gLane)); |
|
||||
ref Vector256<float> srcB = |
|
||||
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(bLane)); |
|
||||
|
|
||||
// Used for the color conversion
|
|
||||
var chromaOffset = Vector256.Create(this.HalfValue); |
|
||||
|
|
||||
var f0299 = Vector256.Create(0.299f); |
|
||||
var f0587 = Vector256.Create(0.587f); |
|
||||
var f0114 = Vector256.Create(0.114f); |
|
||||
var fn0168736 = Vector256.Create(-0.168736f); |
|
||||
var fn0331264 = Vector256.Create(-0.331264f); |
|
||||
var fn0418688 = Vector256.Create(-0.418688f); |
|
||||
var fn0081312F = Vector256.Create(-0.081312F); |
|
||||
var f05 = Vector256.Create(0.5f); |
|
||||
|
|
||||
nuint n = values.Component0.Vector256Count<float>(); |
|
||||
for (nuint i = 0; i < n; i++) |
|
||||
{ |
|
||||
Vector256<float> r = Unsafe.Add(ref srcR, i); |
|
||||
Vector256<float> g = Unsafe.Add(ref srcG, i); |
|
||||
Vector256<float> b = 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 = HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(Avx.Multiply(f0114, b), f0587, g), f0299, r); |
|
||||
Vector256<float> cb = Avx.Add(chromaOffset, HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(Avx.Multiply(f05, b), fn0331264, g), fn0168736, r)); |
|
||||
Vector256<float> cr = Avx.Add(chromaOffset, HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(Avx.Multiply(fn0081312F, b), fn0418688, g), f05, r)); |
|
||||
|
|
||||
Unsafe.Add(ref destY, i) = y; |
|
||||
Unsafe.Add(ref destCb, i) = cb; |
|
||||
Unsafe.Add(ref destCr, i) = cr; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,133 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Six Labors Split License.
|
|
||||
|
|
||||
using System.Runtime.CompilerServices; |
|
||||
using System.Runtime.InteropServices; |
|
||||
using System.Runtime.Intrinsics; |
|
||||
using System.Runtime.Intrinsics.Arm; |
|
||||
using System.Runtime.Intrinsics.X86; |
|
||||
using static SixLabors.ImageSharp.SimdUtils; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
|
||||
|
|
||||
internal abstract partial class JpegColorConverterBase |
|
||||
{ |
|
||||
internal sealed class YccKArm64 : JpegColorConverterArm64 |
|
||||
{ |
|
||||
public YccKArm64(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
|
|
||||
var chromaOffset = Vector128.Create(-this.HalfValue); |
|
||||
var scale = Vector128.Create(1 / (this.MaximumValue * this.MaximumValue)); |
|
||||
var max = Vector128.Create(this.MaximumValue); |
|
||||
var rCrMult = Vector128.Create(YCbCrScalar.RCrMult); |
|
||||
var gCbMult = Vector128.Create(-YCbCrScalar.GCbMult); |
|
||||
var gCrMult = Vector128.Create(-YCbCrScalar.GCrMult); |
|
||||
var bCbMult = Vector128.Create(YCbCrScalar.BCbMult); |
|
||||
|
|
||||
// Walking 8 elements at one step:
|
|
||||
nuint n = (uint)values.Component0.Length / (uint)Vector128<float>.Count; |
|
||||
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 = AdvSimd.Add(c1, chromaOffset); |
|
||||
Vector128<float> cr = AdvSimd.Add(c2, chromaOffset); |
|
||||
Vector128<float> scaledK = AdvSimd.Multiply(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 = HwIntrinsics.MultiplyAdd(y, cr, rCrMult); |
|
||||
Vector128<float> g = |
|
||||
HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(y, cb, gCbMult), cr, gCrMult); |
|
||||
Vector128<float> b = HwIntrinsics.MultiplyAdd(y, cb, bCbMult); |
|
||||
|
|
||||
r = AdvSimd.Subtract(max, AdvSimd.RoundToNearest(r)); |
|
||||
g = AdvSimd.Subtract(max, AdvSimd.RoundToNearest(g)); |
|
||||
b = AdvSimd.Subtract(max, AdvSimd.RoundToNearest(b)); |
|
||||
|
|
||||
r = AdvSimd.Multiply(r, scaledK); |
|
||||
g = AdvSimd.Multiply(g, scaledK); |
|
||||
b = AdvSimd.Multiply(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
|
|
||||
CmykArm64.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
|
|
||||
var maxSampleValue = Vector128.Create(this.MaximumValue); |
|
||||
|
|
||||
var chromaOffset = Vector128.Create(this.HalfValue); |
|
||||
|
|
||||
var f0299 = Vector128.Create(0.299f); |
|
||||
var f0587 = Vector128.Create(0.587f); |
|
||||
var f0114 = Vector128.Create(0.114f); |
|
||||
var fn0168736 = Vector128.Create(-0.168736f); |
|
||||
var fn0331264 = Vector128.Create(-0.331264f); |
|
||||
var fn0418688 = Vector128.Create(-0.418688f); |
|
||||
var fn0081312F = Vector128.Create(-0.081312F); |
|
||||
var f05 = Vector128.Create(0.5f); |
|
||||
|
|
||||
nuint n = (uint)values.Component0.Length / (uint)Vector128<float>.Count; |
|
||||
for (nuint i = 0; i < n; i++) |
|
||||
{ |
|
||||
Vector128<float> r = AdvSimd.Subtract(maxSampleValue, Unsafe.Add(ref srcR, i)); |
|
||||
Vector128<float> g = AdvSimd.Subtract(maxSampleValue, Unsafe.Add(ref srcG, i)); |
|
||||
Vector128<float> b = AdvSimd.Subtract(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 = HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(AdvSimd.Multiply(f0114, b), f0587, g), f0299, r); |
|
||||
Vector128<float> cb = AdvSimd.Add(chromaOffset, HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(AdvSimd.Multiply(f05, b), fn0331264, g), fn0168736, r)); |
|
||||
Vector128<float> cr = AdvSimd.Add(chromaOffset, HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(AdvSimd.Multiply(fn0081312F, b), fn0418688, g), f05, r)); |
|
||||
|
|
||||
Unsafe.Add(ref destY, i) = y; |
|
||||
Unsafe.Add(ref destCb, i) = cb; |
|
||||
Unsafe.Add(ref destCr, i) = cr; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,132 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Six Labors Split License.
|
|
||||
|
|
||||
using System.Runtime.CompilerServices; |
|
||||
using System.Runtime.InteropServices; |
|
||||
using System.Runtime.Intrinsics; |
|
||||
using System.Runtime.Intrinsics.X86; |
|
||||
using static SixLabors.ImageSharp.SimdUtils; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
|
||||
|
|
||||
internal abstract partial class JpegColorConverterBase |
|
||||
{ |
|
||||
internal sealed class YccKAvx : JpegColorConverterAvx |
|
||||
{ |
|
||||
public YccKAvx(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
|
|
||||
var chromaOffset = Vector256.Create(-this.HalfValue); |
|
||||
var scale = Vector256.Create(1 / (this.MaximumValue * this.MaximumValue)); |
|
||||
var max = Vector256.Create(this.MaximumValue); |
|
||||
var rCrMult = Vector256.Create(YCbCrScalar.RCrMult); |
|
||||
var gCbMult = Vector256.Create(-YCbCrScalar.GCbMult); |
|
||||
var gCrMult = Vector256.Create(-YCbCrScalar.GCrMult); |
|
||||
var 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 = Avx.Add(c1, chromaOffset); |
|
||||
Vector256<float> cr = Avx.Add(c2, chromaOffset); |
|
||||
Vector256<float> scaledK = Avx.Multiply(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 = HwIntrinsics.MultiplyAdd(y, cr, rCrMult); |
|
||||
Vector256<float> g = |
|
||||
HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(y, cb, gCbMult), cr, gCrMult); |
|
||||
Vector256<float> b = HwIntrinsics.MultiplyAdd(y, cb, bCbMult); |
|
||||
|
|
||||
r = Avx.Subtract(max, Avx.RoundToNearestInteger(r)); |
|
||||
g = Avx.Subtract(max, Avx.RoundToNearestInteger(g)); |
|
||||
b = Avx.Subtract(max, Avx.RoundToNearestInteger(b)); |
|
||||
|
|
||||
r = Avx.Multiply(r, scaledK); |
|
||||
g = Avx.Multiply(g, scaledK); |
|
||||
b = Avx.Multiply(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
|
|
||||
CmykAvx.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
|
|
||||
var maxSampleValue = Vector256.Create(this.MaximumValue); |
|
||||
|
|
||||
var chromaOffset = Vector256.Create(this.HalfValue); |
|
||||
|
|
||||
var f0299 = Vector256.Create(0.299f); |
|
||||
var f0587 = Vector256.Create(0.587f); |
|
||||
var f0114 = Vector256.Create(0.114f); |
|
||||
var fn0168736 = Vector256.Create(-0.168736f); |
|
||||
var fn0331264 = Vector256.Create(-0.331264f); |
|
||||
var fn0418688 = Vector256.Create(-0.418688f); |
|
||||
var fn0081312F = Vector256.Create(-0.081312F); |
|
||||
var f05 = Vector256.Create(0.5f); |
|
||||
|
|
||||
nuint n = values.Component0.Vector256Count<float>(); |
|
||||
for (nuint i = 0; i < n; i++) |
|
||||
{ |
|
||||
Vector256<float> r = Avx.Subtract(maxSampleValue, Unsafe.Add(ref srcR, i)); |
|
||||
Vector256<float> g = Avx.Subtract(maxSampleValue, Unsafe.Add(ref srcG, i)); |
|
||||
Vector256<float> b = Avx.Subtract(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 = HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(Avx.Multiply(f0114, b), f0587, g), f0299, r); |
|
||||
Vector256<float> cb = Avx.Add(chromaOffset, HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(Avx.Multiply(f05, b), fn0331264, g), fn0168736, r)); |
|
||||
Vector256<float> cr = Avx.Add(chromaOffset, HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(Avx.Multiply(fn0081312F, b), fn0418688, g), f05, r)); |
|
||||
|
|
||||
Unsafe.Add(ref destY, i) = y; |
|
||||
Unsafe.Add(ref destCb, i) = cb; |
|
||||
Unsafe.Add(ref destCr, i) = cr; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,35 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Six Labors Split License.
|
|
||||
using System.Runtime.Intrinsics; |
|
||||
using System.Runtime.Intrinsics.Arm; |
|
||||
using System.Runtime.Intrinsics.X86; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
|
||||
|
|
||||
internal abstract partial class JpegColorConverterBase |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// <see cref="JpegColorConverterBase"/> abstract base for implementations
|
|
||||
/// based on <see cref="Avx"/> instructions.
|
|
||||
/// </summary>
|
|
||||
/// <remarks>
|
|
||||
/// Converters of this family would expect input buffers lengths to be
|
|
||||
/// divisible by 8 without a remainder.
|
|
||||
/// This is guaranteed by real-life data as jpeg stores pixels via 8x8 blocks.
|
|
||||
/// DO NOT pass test data of invalid size to these converters as they
|
|
||||
/// potentially won't do a bound check and return a false positive result.
|
|
||||
/// </remarks>
|
|
||||
internal abstract class JpegColorConverterArm : JpegColorConverterBase |
|
||||
{ |
|
||||
protected JpegColorConverterArm(JpegColorSpace colorSpace, int precision) |
|
||||
: base(colorSpace, precision) |
|
||||
{ |
|
||||
} |
|
||||
|
|
||||
public static bool IsSupported => AdvSimd.IsSupported; |
|
||||
|
|
||||
public sealed override bool IsAvailable => IsSupported; |
|
||||
|
|
||||
public sealed override int ElementsPerBatch => Vector128<float>.Count; |
|
||||
} |
|
||||
} |
|
||||
@ -1,35 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Six Labors Split License.
|
|
||||
using System.Runtime.Intrinsics; |
|
||||
using System.Runtime.Intrinsics.Arm; |
|
||||
using System.Runtime.Intrinsics.X86; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
|
||||
|
|
||||
internal abstract partial class JpegColorConverterBase |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// <see cref="JpegColorConverterBase"/> abstract base for implementations
|
|
||||
/// based on <see cref="Avx"/> instructions.
|
|
||||
/// </summary>
|
|
||||
/// <remarks>
|
|
||||
/// Converters of this family would expect input buffers lengths to be
|
|
||||
/// divisible by 8 without a remainder.
|
|
||||
/// This is guaranteed by real-life data as jpeg stores pixels via 8x8 blocks.
|
|
||||
/// DO NOT pass test data of invalid size to these converters as they
|
|
||||
/// potentially won't do a bound check and return a false positive result.
|
|
||||
/// </remarks>
|
|
||||
internal abstract class JpegColorConverterArm64 : JpegColorConverterBase |
|
||||
{ |
|
||||
protected JpegColorConverterArm64(JpegColorSpace colorSpace, int precision) |
|
||||
: base(colorSpace, precision) |
|
||||
{ |
|
||||
} |
|
||||
|
|
||||
public static bool IsSupported => AdvSimd.Arm64.IsSupported; |
|
||||
|
|
||||
public sealed override bool IsAvailable => IsSupported; |
|
||||
|
|
||||
public sealed override int ElementsPerBatch => Vector128<float>.Count; |
|
||||
} |
|
||||
} |
|
||||
@ -1,34 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Six Labors Split License.
|
|
||||
using System.Runtime.Intrinsics; |
|
||||
using System.Runtime.Intrinsics.X86; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
|
||||
|
|
||||
internal abstract partial class JpegColorConverterBase |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// <see cref="JpegColorConverterBase"/> abstract base for implementations
|
|
||||
/// based on <see cref="Avx"/> instructions.
|
|
||||
/// </summary>
|
|
||||
/// <remarks>
|
|
||||
/// Converters of this family would expect input buffers lengths to be
|
|
||||
/// divisible by 8 without a remainder.
|
|
||||
/// This is guaranteed by real-life data as jpeg stores pixels via 8x8 blocks.
|
|
||||
/// DO NOT pass test data of invalid size to these converters as they
|
|
||||
/// potentially won't do a bound check and return a false positive result.
|
|
||||
/// </remarks>
|
|
||||
internal abstract class JpegColorConverterAvx : JpegColorConverterBase |
|
||||
{ |
|
||||
protected JpegColorConverterAvx(JpegColorSpace colorSpace, int precision) |
|
||||
: base(colorSpace, precision) |
|
||||
{ |
|
||||
} |
|
||||
|
|
||||
public static bool IsSupported => Avx.IsSupported; |
|
||||
|
|
||||
public sealed override bool IsAvailable => IsSupported; |
|
||||
|
|
||||
public sealed override int ElementsPerBatch => Vector256<float>.Count; |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue