|
| 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 | + |
| 9 | +namespace SixLabors.ImageSharp.Formats.Jpeg.Components; |
| 10 | + |
| 11 | +internal abstract partial class JpegColorConverterBase |
| 12 | +{ |
| 13 | + internal sealed class CmykArm64 : JpegColorConverterArm64 |
| 14 | + { |
| 15 | + public CmykArm64(int precision) |
| 16 | + : base(JpegColorSpace.Cmyk, precision) |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + /// <inheritdoc/> |
| 21 | + public override void ConvertToRgbInplace(in ComponentValues values) |
| 22 | + { |
| 23 | + ref Vector128<float> c0Base = |
| 24 | + ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component0)); |
| 25 | + ref Vector128<float> c1Base = |
| 26 | + ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component1)); |
| 27 | + ref Vector128<float> c2Base = |
| 28 | + ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component2)); |
| 29 | + ref Vector128<float> c3Base = |
| 30 | + ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component3)); |
| 31 | + |
| 32 | + // Used for the color conversion |
| 33 | + var scale = Vector128.Create(1 / (this.MaximumValue * this.MaximumValue)); |
| 34 | + |
| 35 | + nint n = (nint)(uint)values.Component0.Length / Vector128<float>.Count; |
| 36 | + for (nint i = 0; i < n; i++) |
| 37 | + { |
| 38 | + ref Vector128<float> c = ref Unsafe.Add(ref c0Base, i); |
| 39 | + ref Vector128<float> m = ref Unsafe.Add(ref c1Base, i); |
| 40 | + ref Vector128<float> y = ref Unsafe.Add(ref c2Base, i); |
| 41 | + Vector128<float> k = Unsafe.Add(ref c3Base, i); |
| 42 | + |
| 43 | + k = AdvSimd.Multiply(k, scale); |
| 44 | + c = AdvSimd.Multiply(c, k); |
| 45 | + m = AdvSimd.Multiply(m, k); |
| 46 | + y = AdvSimd.Multiply(y, k); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /// <inheritdoc/> |
| 51 | + public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane, Span<float> gLane, Span<float> bLane) |
| 52 | + => ConvertFromRgb(in values, this.MaximumValue, rLane, gLane, bLane); |
| 53 | + |
| 54 | + public static void ConvertFromRgb(in ComponentValues values, float maxValue, Span<float> rLane, Span<float> gLane, Span<float> bLane) |
| 55 | + { |
| 56 | + ref Vector128<float> destC = |
| 57 | + ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component0)); |
| 58 | + ref Vector128<float> destM = |
| 59 | + ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component1)); |
| 60 | + ref Vector128<float> destY = |
| 61 | + ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component2)); |
| 62 | + ref Vector128<float> destK = |
| 63 | + ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(values.Component3)); |
| 64 | + |
| 65 | + ref Vector128<float> srcR = |
| 66 | + ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(rLane)); |
| 67 | + ref Vector128<float> srcG = |
| 68 | + ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(gLane)); |
| 69 | + ref Vector128<float> srcB = |
| 70 | + ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(bLane)); |
| 71 | + |
| 72 | + var scale = Vector128.Create(maxValue); |
| 73 | + |
| 74 | + nint n = (nint)(uint)values.Component0.Length / Vector128<float>.Count; |
| 75 | + for (nint i = 0; i < n; i++) |
| 76 | + { |
| 77 | + Vector128<float> ctmp = AdvSimd.Subtract(scale, Unsafe.Add(ref srcR, i)); |
| 78 | + Vector128<float> mtmp = AdvSimd.Subtract(scale, Unsafe.Add(ref srcG, i)); |
| 79 | + Vector128<float> ytmp = AdvSimd.Subtract(scale, Unsafe.Add(ref srcB, i)); |
| 80 | + Vector128<float> ktmp = AdvSimd.Min(ctmp, AdvSimd.Min(mtmp, ytmp)); |
| 81 | + |
| 82 | + Vector128<float> kMask = AdvSimd.Not(AdvSimd.CompareEqual(ktmp, scale)); |
| 83 | + |
| 84 | + ctmp = AdvSimd.And(AdvSimd.Arm64.Divide(AdvSimd.Subtract(ctmp, ktmp), AdvSimd.Subtract(scale, ktmp)), kMask); |
| 85 | + mtmp = AdvSimd.And(AdvSimd.Arm64.Divide(AdvSimd.Subtract(mtmp, ktmp), AdvSimd.Subtract(scale, ktmp)), kMask); |
| 86 | + ytmp = AdvSimd.And(AdvSimd.Arm64.Divide(AdvSimd.Subtract(ytmp, ktmp), AdvSimd.Subtract(scale, ktmp)), kMask); |
| 87 | + |
| 88 | + Unsafe.Add(ref destC, i) = AdvSimd.Subtract(scale, AdvSimd.Multiply(ctmp, scale)); |
| 89 | + Unsafe.Add(ref destM, i) = AdvSimd.Subtract(scale, AdvSimd.Multiply(mtmp, scale)); |
| 90 | + Unsafe.Add(ref destY, i) = AdvSimd.Subtract(scale, AdvSimd.Multiply(ytmp, scale)); |
| 91 | + Unsafe.Add(ref destK, i) = AdvSimd.Subtract(scale, ktmp); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments