mirror of https://github.com/SixLabors/ImageSharp
13 changed files with 339 additions and 74 deletions
@ -0,0 +1,67 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using System.Runtime.CompilerServices; |
|||
using System.Runtime.InteropServices; |
|||
using System.Runtime.Intrinsics; |
|||
using SixLabors.ImageSharp.Common.Helpers; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
|||
|
|||
internal abstract partial class JpegColorConverterBase |
|||
{ |
|||
internal sealed class GrayScaleVector128 : JpegColorConverterVector128 |
|||
{ |
|||
public GrayScaleVector128(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
|
|||
Vector128<float> 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 *= 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
|
|||
Vector128<float> f0299 = Vector128.Create(0.299f); |
|||
Vector128<float> f0587 = Vector128.Create(0.587f); |
|||
Vector128<float> 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); |
|||
|
|||
// luminosity = (0.299 * r) + (0.587 * g) + (0.114 * b)
|
|||
Unsafe.Add(ref destLuminance, i) = Vector128Utilities.MultiplyAdd(Vector128Utilities.MultiplyAdd(f0114 * b, f0587, g), f0299, r); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,67 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using System.Runtime.CompilerServices; |
|||
using System.Runtime.InteropServices; |
|||
using System.Runtime.Intrinsics; |
|||
using SixLabors.ImageSharp.Common.Helpers; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
|||
|
|||
internal abstract partial class JpegColorConverterBase |
|||
{ |
|||
internal sealed class GrayScaleVector256 : JpegColorConverterVector256 |
|||
{ |
|||
public GrayScaleVector256(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
|
|||
Vector256<float> 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 *= 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
|
|||
Vector256<float> f0299 = Vector256.Create(0.299f); |
|||
Vector256<float> f0587 = Vector256.Create(0.587f); |
|||
Vector256<float> 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); |
|||
|
|||
// 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); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,75 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using System.Runtime.CompilerServices; |
|||
using System.Runtime.InteropServices; |
|||
using System.Runtime.Intrinsics; |
|||
using SixLabors.ImageSharp.Common.Helpers; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
|||
|
|||
internal abstract partial class JpegColorConverterBase |
|||
{ |
|||
internal sealed class GrayScaleVector512 : JpegColorConverterVector512 |
|||
{ |
|||
public GrayScaleVector512(int precision) |
|||
: base(JpegColorSpace.Grayscale, precision) |
|||
{ |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override void ConvertToRgbInPlaceVectorized(in ComponentValues values) |
|||
{ |
|||
ref Vector512<float> c0Base = |
|||
ref Unsafe.As<float, Vector512<float>>(ref MemoryMarshal.GetReference(values.Component0)); |
|||
|
|||
// Used for the color conversion
|
|||
Vector512<float> scale = Vector512.Create(1 / this.MaximumValue); |
|||
|
|||
nuint n = values.Component0.Vector512Count<float>(); |
|||
for (nuint i = 0; i < n; i++) |
|||
{ |
|||
ref Vector512<float> c0 = ref Unsafe.Add(ref c0Base, i); |
|||
c0 *= scale; |
|||
} |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override void ConvertFromRgbVectorized(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane) |
|||
{ |
|||
ref Vector512<float> destLuminance = |
|||
ref Unsafe.As<float, Vector512<float>>(ref MemoryMarshal.GetReference(values.Component0)); |
|||
|
|||
ref Vector512<float> srcRed = |
|||
ref Unsafe.As<float, Vector512<float>>(ref MemoryMarshal.GetReference(rLane)); |
|||
ref Vector512<float> srcGreen = |
|||
ref Unsafe.As<float, Vector512<float>>(ref MemoryMarshal.GetReference(gLane)); |
|||
ref Vector512<float> srcBlue = |
|||
ref Unsafe.As<float, Vector512<float>>(ref MemoryMarshal.GetReference(bLane)); |
|||
|
|||
// Used for the color conversion
|
|||
Vector512<float> f0299 = Vector512.Create(0.299f); |
|||
Vector512<float> f0587 = Vector512.Create(0.587f); |
|||
Vector512<float> f0114 = Vector512.Create(0.114f); |
|||
|
|||
nuint n = values.Component0.Vector512Count<float>(); |
|||
for (nuint i = 0; i < n; i++) |
|||
{ |
|||
ref Vector512<float> r = ref Unsafe.Add(ref srcRed, i); |
|||
ref Vector512<float> g = ref Unsafe.Add(ref srcGreen, i); |
|||
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); |
|||
} |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override void ConvertToRgbInPlaceScalarRemainder(in ComponentValues values) |
|||
=> 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); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue