|
| 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 | +using System.Runtime.Intrinsics.Arm; |
| 8 | +using static SixLabors.ImageSharp.SimdUtils; |
| 9 | + |
| 10 | +namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
| 11 | + |
| 12 | +internal abstract partial class JpegColorConverterBase |
| 13 | +{ |
| 14 | + internal sealed class GrayscaleArm : JpegColorConverterArm |
| 15 | + { |
| 16 | + public GrayscaleArm(int precision) |
| 17 | + : base(JpegColorSpace.Grayscale, precision) |
| 18 | + { |
| 19 | + } |
| 20 | + |
| 21 | + /// <inheritdoc/> |
| 22 | + public override void ConvertToRgbInplace(in ComponentValues values) |
| 23 | + { |
| 24 | + ref Vector128<float> c0Base = |
| 25 | + ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component0)); |
| 26 | + |
| 27 | + // Used for the color conversion |
| 28 | + var scale = Vector128.Create(1 / this.MaximumValue); |
| 29 | + |
| 30 | + nuint n = (uint)values.Component0.Length / (uint)Vector128<float>.Count; |
| 31 | + for (nuint i = 0; i < n; i++) |
| 32 | + { |
| 33 | + ref Vector128<float> c0 = ref Unsafe.Add(ref c0Base, i); |
| 34 | + c0 = AdvSimd.Multiply(c0, scale); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /// <inheritdoc/> |
| 39 | + public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane) |
| 40 | + { |
| 41 | + ref Vector128<float> destLuminance = |
| 42 | + ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component0)); |
| 43 | + |
| 44 | + ref Vector128<float> srcRed = |
| 45 | + ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(rLane)); |
| 46 | + ref Vector128<float> srcGreen = |
| 47 | + ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(gLane)); |
| 48 | + ref Vector128<float> srcBlue = |
| 49 | + ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(bLane)); |
| 50 | + |
| 51 | + // Used for the color conversion |
| 52 | + var f0299 = Vector128.Create(0.299f); |
| 53 | + var f0587 = Vector128.Create(0.587f); |
| 54 | + var f0114 = Vector128.Create(0.114f); |
| 55 | + |
| 56 | + nuint n = (uint)values.Component0.Length / (uint)Vector128<float>.Count; |
| 57 | + for (nuint i = 0; i < n; i++) |
| 58 | + { |
| 59 | + ref Vector128<float> r = ref Unsafe.Add(ref srcRed, i); |
| 60 | + ref Vector128<float> g = ref Unsafe.Add(ref srcGreen, i); |
| 61 | + ref Vector128<float> b = ref Unsafe.Add(ref srcBlue, i); |
| 62 | + |
| 63 | + // luminocity = (0.299 * r) + (0.587 * g) + (0.114 * b) |
| 64 | + Unsafe.Add(ref destLuminance, i) = HwIntrinsics.MultiplyAdd(HwIntrinsics.MultiplyAdd(AdvSimd.Multiply(f0114, b), f0587, g), f0299, r); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments