|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Numerics; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | + |
| 9 | +namespace SixLabors.ImageSharp |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Provides optimized static methods for common mathematical functions specific |
| 13 | + /// to color processing. |
| 14 | + /// </summary> |
| 15 | + internal static class ColorNumerics |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Vector for converting pixel to gray value as specified by |
| 19 | + /// ITU-R Recommendation BT.709. |
| 20 | + /// </summary> |
| 21 | + private static readonly Vector4 Bt709 = new Vector4(.2126f, .7152f, .0722f, 0.0f); |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Convert a pixel value to grayscale using ITU-R Recommendation BT.709. |
| 25 | + /// </summary> |
| 26 | + /// <param name="vector">The vector to get the luminance from.</param> |
| 27 | + /// <param name="luminanceLevels"> |
| 28 | + /// The number of luminance levels (256 for 8 bit, 65536 for 16 bit grayscale images). |
| 29 | + /// </param> |
| 30 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 31 | + public static int GetBT709Luminance(ref Vector4 vector, int luminanceLevels) |
| 32 | + => (int)MathF.Round(Vector4.Dot(vector, Bt709) * (luminanceLevels - 1)); |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets the luminance from the rgb components using the formula |
| 36 | + /// as specified by ITU-R Recommendation BT.709. |
| 37 | + /// </summary> |
| 38 | + /// <param name="r">The red component.</param> |
| 39 | + /// <param name="g">The green component.</param> |
| 40 | + /// <param name="b">The blue component.</param> |
| 41 | + /// <returns>The <see cref="byte"/>.</returns> |
| 42 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 43 | + public static byte Get8BitBT709Luminance(byte r, byte g, byte b) |
| 44 | + => (byte)((r * .2126F) + (g * .7152F) + (b * .0722F) + 0.5F); |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Gets the luminance from the rgb components using the formula as |
| 48 | + /// specified by ITU-R Recommendation BT.709. |
| 49 | + /// </summary> |
| 50 | + /// <param name="r">The red component.</param> |
| 51 | + /// <param name="g">The green component.</param> |
| 52 | + /// <param name="b">The blue component.</param> |
| 53 | + /// <returns>The <see cref="ushort"/>.</returns> |
| 54 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 55 | + public static ushort Get16BitBT709Luminance(ushort r, ushort g, ushort b) |
| 56 | + => (ushort)((r * .2126F) + (g * .7152F) + (b * .0722F) + 0.5F); |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Gets the luminance from the rgb components using the formula as specified |
| 60 | + /// by ITU-R Recommendation BT.709. |
| 61 | + /// </summary> |
| 62 | + /// <param name="r">The red component.</param> |
| 63 | + /// <param name="g">The green component.</param> |
| 64 | + /// <param name="b">The blue component.</param> |
| 65 | + /// <returns>The <see cref="ushort"/>.</returns> |
| 66 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 67 | + public static ushort Get16BitBT709Luminance(float r, float g, float b) |
| 68 | + => (ushort)((r * .2126F) + (g * .7152F) + (b * .0722F) + 0.5F); |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Scales a value from a 16 bit <see cref="ushort"/> to an |
| 72 | + /// 8 bit <see cref="byte"/> equivalent. |
| 73 | + /// </summary> |
| 74 | + /// <param name="component">The 8 bit component value.</param> |
| 75 | + /// <returns>The <see cref="byte"/></returns> |
| 76 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 77 | + public static byte DownScaleFrom16BitTo8Bit(ushort component) |
| 78 | + { |
| 79 | + // To scale to 8 bits From a 16-bit value V the required value (from the PNG specification) is: |
| 80 | + // |
| 81 | + // (V * 255) / 65535 |
| 82 | + // |
| 83 | + // This reduces to round(V / 257), or floor((V + 128.5)/257) |
| 84 | + // |
| 85 | + // Represent V as the two byte value vhi.vlo. Make a guess that the |
| 86 | + // result is the top byte of V, vhi, then the correction to this value |
| 87 | + // is: |
| 88 | + // |
| 89 | + // error = floor(((V-vhi.vhi) + 128.5) / 257) |
| 90 | + // = floor(((vlo-vhi) + 128.5) / 257) |
| 91 | + // |
| 92 | + // This can be approximated using integer arithmetic (and a signed |
| 93 | + // shift): |
| 94 | + // |
| 95 | + // error = (vlo-vhi+128) >> 8; |
| 96 | + // |
| 97 | + // The approximate differs from the exact answer only when (vlo-vhi) is |
| 98 | + // 128; it then gives a correction of +1 when the exact correction is |
| 99 | + // 0. This gives 128 errors. The exact answer (correct for all 16-bit |
| 100 | + // input values) is: |
| 101 | + // |
| 102 | + // error = (vlo-vhi+128)*65535 >> 24; |
| 103 | + // |
| 104 | + // An alternative arithmetic calculation which also gives no errors is: |
| 105 | + // |
| 106 | + // (V * 255 + 32895) >> 16 |
| 107 | + return (byte)(((component * 255) + 32895) >> 16); |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Scales a value from an 8 bit <see cref="byte"/> to |
| 112 | + /// an 16 bit <see cref="ushort"/> equivalent. |
| 113 | + /// </summary> |
| 114 | + /// <param name="component">The 8 bit component value.</param> |
| 115 | + /// <returns>The <see cref="ushort"/></returns> |
| 116 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 117 | + public static ushort UpscaleFrom8BitTo16Bit(byte component) |
| 118 | + => (ushort)(component * 257); |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Returns how many bits are required to store the specified number of colors. |
| 122 | + /// Performs a Log2() on the value. |
| 123 | + /// </summary> |
| 124 | + /// <param name="colors">The number of colors.</param> |
| 125 | + /// <returns> |
| 126 | + /// The <see cref="int"/> |
| 127 | + /// </returns> |
| 128 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 129 | + public static int GetBitsNeededForColorDepth(int colors) |
| 130 | + => Math.Max(1, (int)Math.Ceiling(Math.Log(colors, 2))); |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Returns how many colors will be created by the specified number of bits. |
| 134 | + /// </summary> |
| 135 | + /// <param name="bitDepth">The bit depth.</param> |
| 136 | + /// <returns>The <see cref="int"/></returns> |
| 137 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 138 | + public static int GetColorCountForBitDepth(int bitDepth) |
| 139 | + => 1 << bitDepth; |
| 140 | + |
| 141 | + /// <summary> |
| 142 | + /// Transforms a vector by the given color matrix. |
| 143 | + /// </summary> |
| 144 | + /// <param name="vector">The source vector.</param> |
| 145 | + /// <param name="matrix">The transformation color matrix.</param> |
| 146 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 147 | + public static void Transform(ref Vector4 vector, ref ColorMatrix matrix) |
| 148 | + { |
| 149 | + float x = vector.X; |
| 150 | + float y = vector.Y; |
| 151 | + float z = vector.Z; |
| 152 | + float w = vector.W; |
| 153 | + |
| 154 | + vector.X = (x * matrix.M11) + (y * matrix.M21) + (z * matrix.M31) + (w * matrix.M41) + matrix.M51; |
| 155 | + vector.Y = (x * matrix.M12) + (y * matrix.M22) + (z * matrix.M32) + (w * matrix.M42) + matrix.M52; |
| 156 | + vector.Z = (x * matrix.M13) + (y * matrix.M23) + (z * matrix.M33) + (w * matrix.M43) + matrix.M53; |
| 157 | + vector.W = (x * matrix.M14) + (y * matrix.M24) + (z * matrix.M34) + (w * matrix.M44) + matrix.M54; |
| 158 | + } |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Bulk variant of <see cref="Transform(ref Vector4, ref ColorMatrix)"/>. |
| 162 | + /// </summary> |
| 163 | + /// <param name="vectors">The span of vectors</param> |
| 164 | + /// <param name="matrix">The transformation color matrix.</param> |
| 165 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 166 | + public static void Transform(Span<Vector4> vectors, ref ColorMatrix matrix) |
| 167 | + { |
| 168 | + ref Vector4 baseRef = ref MemoryMarshal.GetReference(vectors); |
| 169 | + |
| 170 | + for (int i = 0; i < vectors.Length; i++) |
| 171 | + { |
| 172 | + ref Vector4 v = ref Unsafe.Add(ref baseRef, i); |
| 173 | + Transform(ref v, ref matrix); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments