|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using System.Collections.Concurrent; |
| 5 | +using System.Numerics; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using System.Runtime.Intrinsics; |
| 9 | +using System.Runtime.Intrinsics.X86; |
| 10 | + |
| 11 | +namespace SixLabors.ImageSharp.ColorProfiles.Companding; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Companding utilities that allow the accelerated compression-expansion of color channels. |
| 15 | +/// </summary> |
| 16 | +public static class CompandingUtilities |
| 17 | +{ |
| 18 | + private const int Length = Scale + 2; // 256kb @ 16bit precision. |
| 19 | + private const int Scale = (1 << 16) - 1; |
| 20 | + private static readonly ConcurrentDictionary<(Type, double), Lazy<float[]>> LookupTables = new(); |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Lazily creates and stores a companding lookup table using the given function and modifier. |
| 24 | + /// </summary> |
| 25 | + /// <typeparam name="T">The type of companding function.</typeparam> |
| 26 | + /// <param name="compandingFunction">The companding function.</param> |
| 27 | + /// <param name="modifier">A modifier to pass to the function.</param> |
| 28 | + /// <returns>The <see cref="float"/> array.</returns> |
| 29 | + public static Lazy<float[]> GetLookupTable<T>(Func<double, double, double> compandingFunction, double modifier = 0) |
| 30 | + => LookupTables.GetOrAdd((typeof(T), modifier), args => new(() => CreateLookupTableImpl(compandingFunction, args.Item2), true)); |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Creates a companding lookup table using the given function. |
| 34 | + /// </summary> |
| 35 | + /// <param name="compandingFunction">The companding function.</param> |
| 36 | + /// <param name="modifier">A modifier to pass to the function.</param> |
| 37 | + /// <returns>The <see cref="float"/> array.</returns> |
| 38 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 39 | + private static float[] CreateLookupTableImpl(Func<double, double, double> compandingFunction, double modifier = 0) |
| 40 | + { |
| 41 | + float[] result = new float[Length]; |
| 42 | + |
| 43 | + for (int i = 0; i < result.Length; i++) |
| 44 | + { |
| 45 | + double d = (double)i / Scale; |
| 46 | + d = compandingFunction(d, modifier); |
| 47 | + result[i] = (float)d; |
| 48 | + } |
| 49 | + |
| 50 | + return result; |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Performs the companding operation on the given vectors using the given table. |
| 55 | + /// </summary> |
| 56 | + /// <param name="vectors">The span of vectors.</param> |
| 57 | + /// <param name="table">The lookup table.</param> |
| 58 | + public static void Compand(Span<Vector4> vectors, float[] table) |
| 59 | + { |
| 60 | + DebugGuard.MustBeGreaterThanOrEqualTo(table.Length, Length, nameof(table)); |
| 61 | + |
| 62 | + if (Avx2.IsSupported && vectors.Length >= 2) |
| 63 | + { |
| 64 | + CompandAvx2(vectors, table); |
| 65 | + |
| 66 | + if (Numerics.Modulo2(vectors.Length) != 0) |
| 67 | + { |
| 68 | + // Vector4 fits neatly in pairs. Any overlap has to be equal to 1. |
| 69 | + ref Vector4 last = ref MemoryMarshal.GetReference(vectors[^1..]); |
| 70 | + last = Compand(last, table); |
| 71 | + } |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + CompandScalar(vectors, table); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Performs the companding operation on the given vector using the given table. |
| 81 | + /// </summary> |
| 82 | + /// <param name="vector">The vector.</param> |
| 83 | + /// <param name="table">The lookup table.</param> |
| 84 | + /// <returns>The <see cref="Vector4"/></returns> |
| 85 | + public static Vector4 Compand(Vector4 vector, float[] table) |
| 86 | + { |
| 87 | + DebugGuard.MustBeGreaterThanOrEqualTo(table.Length, Length, nameof(table)); |
| 88 | + |
| 89 | + Vector4 zero = Vector4.Zero; |
| 90 | + Vector4 scale = new(Scale); |
| 91 | + |
| 92 | + Vector4 multiplied = Numerics.Clamp(vector * Scale, zero, scale); |
| 93 | + |
| 94 | + float f0 = multiplied.X; |
| 95 | + float f1 = multiplied.Y; |
| 96 | + float f2 = multiplied.Z; |
| 97 | + |
| 98 | + uint i0 = (uint)f0; |
| 99 | + uint i1 = (uint)f1; |
| 100 | + uint i2 = (uint)f2; |
| 101 | + |
| 102 | + // Alpha is already a linear representation of opacity so we do not want to convert it. |
| 103 | + vector.X = Numerics.Lerp(table[i0], table[i0 + 1], f0 - (int)i0); |
| 104 | + vector.Y = Numerics.Lerp(table[i1], table[i1 + 1], f1 - (int)i1); |
| 105 | + vector.Z = Numerics.Lerp(table[i2], table[i2 + 1], f2 - (int)i2); |
| 106 | + |
| 107 | + return vector; |
| 108 | + } |
| 109 | + |
| 110 | + private static unsafe void CompandAvx2(Span<Vector4> vectors, float[] table) |
| 111 | + { |
| 112 | + fixed (float* tablePointer = &MemoryMarshal.GetArrayDataReference(table)) |
| 113 | + { |
| 114 | + Vector256<float> scale = Vector256.Create((float)Scale); |
| 115 | + Vector256<float> zero = Vector256<float>.Zero; |
| 116 | + Vector256<int> offset = Vector256.Create(1); |
| 117 | + |
| 118 | + // Divide by 2 as 4 elements per Vector4 and 8 per Vector256<float> |
| 119 | + ref Vector256<float> vectorsBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(vectors)); |
| 120 | + ref Vector256<float> vectorsLast = ref Unsafe.Add(ref vectorsBase, (uint)vectors.Length / 2u); |
| 121 | + |
| 122 | + while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsLast)) |
| 123 | + { |
| 124 | + Vector256<float> multiplied = Avx.Multiply(scale, vectorsBase); |
| 125 | + multiplied = Avx.Min(Avx.Max(zero, multiplied), scale); |
| 126 | + |
| 127 | + Vector256<int> truncated = Avx.ConvertToVector256Int32WithTruncation(multiplied); |
| 128 | + Vector256<float> truncatedF = Avx.ConvertToVector256Single(truncated); |
| 129 | + |
| 130 | + Vector256<float> low = Avx2.GatherVector256(tablePointer, truncated, sizeof(float)); |
| 131 | + Vector256<float> high = Avx2.GatherVector256(tablePointer, Avx2.Add(truncated, offset), sizeof(float)); |
| 132 | + |
| 133 | + // Alpha is already a linear representation of opacity so we do not want to convert it. |
| 134 | + Vector256<float> companded = Numerics.Lerp(low, high, Avx.Subtract(multiplied, truncatedF)); |
| 135 | + vectorsBase = Avx.Blend(companded, vectorsBase, Numerics.BlendAlphaControl); |
| 136 | + vectorsBase = ref Unsafe.Add(ref vectorsBase, 1); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static unsafe void CompandScalar(Span<Vector4> vectors, float[] table) |
| 142 | + { |
| 143 | + fixed (float* tablePointer = &MemoryMarshal.GetArrayDataReference(table)) |
| 144 | + { |
| 145 | + Vector4 zero = Vector4.Zero; |
| 146 | + Vector4 scale = new(Scale); |
| 147 | + ref Vector4 vectorsBase = ref MemoryMarshal.GetReference(vectors); |
| 148 | + ref Vector4 vectorsLast = ref Unsafe.Add(ref vectorsBase, (uint)vectors.Length); |
| 149 | + |
| 150 | + while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsLast)) |
| 151 | + { |
| 152 | + Vector4 multiplied = Numerics.Clamp(vectorsBase * Scale, zero, scale); |
| 153 | + |
| 154 | + float f0 = multiplied.X; |
| 155 | + float f1 = multiplied.Y; |
| 156 | + float f2 = multiplied.Z; |
| 157 | + |
| 158 | + uint i0 = (uint)f0; |
| 159 | + uint i1 = (uint)f1; |
| 160 | + uint i2 = (uint)f2; |
| 161 | + |
| 162 | + // Alpha is already a linear representation of opacity so we do not want to convert it. |
| 163 | + vectorsBase.X = Numerics.Lerp(tablePointer[i0], tablePointer[i0 + 1], f0 - (int)i0); |
| 164 | + vectorsBase.Y = Numerics.Lerp(tablePointer[i1], tablePointer[i1 + 1], f1 - (int)i1); |
| 165 | + vectorsBase.Z = Numerics.Lerp(tablePointer[i2], tablePointer[i2 + 1], f2 - (int)i2); |
| 166 | + |
| 167 | + vectorsBase = ref Unsafe.Add(ref vectorsBase, 1); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments