|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +#pragma warning disable SA1117 // Parameters should be on same line or separate lines |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.Numerics; |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | + |
| 9 | +namespace SixLabors.ImageSharp; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// A structure encapsulating a 5x4 matrix used for transforming the color and alpha components of an image. |
| 13 | +/// </summary> |
| 14 | +public partial struct ColorMatrix |
| 15 | +{ |
| 16 | + [UnscopedRef] |
| 17 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 18 | + internal ref Impl AsImpl() => ref Unsafe.As<ColorMatrix, Impl>(ref this); |
| 19 | + |
| 20 | + [UnscopedRef] |
| 21 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 22 | + internal readonly ref readonly Impl AsROImpl() => ref Unsafe.As<ColorMatrix, Impl>(ref Unsafe.AsRef(in this)); |
| 23 | + |
| 24 | + internal struct Impl : IEquatable<Impl> |
| 25 | + { |
| 26 | + public Vector4 X; |
| 27 | + public Vector4 Y; |
| 28 | + public Vector4 Z; |
| 29 | + public Vector4 W; |
| 30 | + public Vector4 V; |
| 31 | + |
| 32 | + public static Impl Identity |
| 33 | + { |
| 34 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 35 | + get |
| 36 | + { |
| 37 | + Impl result; |
| 38 | + |
| 39 | + result.X = Vector4.UnitX; |
| 40 | + result.Y = Vector4.UnitY; |
| 41 | + result.Z = Vector4.UnitZ; |
| 42 | + result.W = Vector4.UnitW; |
| 43 | + result.V = Vector4.Zero; |
| 44 | + |
| 45 | + return result; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public readonly bool IsIdentity |
| 50 | + { |
| 51 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 52 | + get => |
| 53 | + (this.X == Vector4.UnitX) |
| 54 | + && (this.Y == Vector4.UnitY) |
| 55 | + && (this.Z == Vector4.UnitZ) |
| 56 | + && (this.W == Vector4.UnitW) |
| 57 | + && (this.V == Vector4.Zero); |
| 58 | + } |
| 59 | + |
| 60 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 61 | + public static Impl operator +(in Impl left, in Impl right) |
| 62 | + { |
| 63 | + Impl result; |
| 64 | + |
| 65 | + result.X = left.X + right.X; |
| 66 | + result.Y = left.Y + right.Y; |
| 67 | + result.Z = left.Z + right.Z; |
| 68 | + result.W = left.W + right.W; |
| 69 | + result.V = left.V + right.V; |
| 70 | + |
| 71 | + return result; |
| 72 | + } |
| 73 | + |
| 74 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 75 | + public static Impl operator -(in Impl left, in Impl right) |
| 76 | + { |
| 77 | + Impl result; |
| 78 | + |
| 79 | + result.X = left.X - right.X; |
| 80 | + result.Y = left.Y - right.Y; |
| 81 | + result.Z = left.Z - right.Z; |
| 82 | + result.W = left.W - right.W; |
| 83 | + result.V = left.V - right.V; |
| 84 | + |
| 85 | + return result; |
| 86 | + } |
| 87 | + |
| 88 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 89 | + public static Impl operator -(in Impl value) |
| 90 | + { |
| 91 | + Impl result; |
| 92 | + |
| 93 | + result.X = -value.X; |
| 94 | + result.Y = -value.Y; |
| 95 | + result.Z = -value.Z; |
| 96 | + result.W = -value.W; |
| 97 | + result.V = -value.V; |
| 98 | + |
| 99 | + return result; |
| 100 | + } |
| 101 | + |
| 102 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 103 | + public static Impl operator *(in Impl left, in Impl right) |
| 104 | + { |
| 105 | + Impl result; |
| 106 | + |
| 107 | + // result.X = Transform(left.X, in right); |
| 108 | + result.X = right.X * left.X.X; |
| 109 | + result.X += right.Y * left.X.Y; |
| 110 | + result.X += right.Z * left.X.Z; |
| 111 | + result.X += right.W * left.X.W; |
| 112 | + |
| 113 | + // result.Y = Transform(left.Y, in right); |
| 114 | + result.Y = right.X * left.Y.X; |
| 115 | + result.Y += right.Y * left.Y.Y; |
| 116 | + result.Y += right.Z * left.Y.Z; |
| 117 | + result.Y += right.W * left.Y.W; |
| 118 | + |
| 119 | + // result.Z = Transform(left.Z, in right); |
| 120 | + result.Z = right.X * left.Z.X; |
| 121 | + result.Z += right.Y * left.Z.Y; |
| 122 | + result.Z += right.Z * left.Z.Z; |
| 123 | + result.Z += right.W * left.Z.W; |
| 124 | + |
| 125 | + // result.W = Transform(left.W, in right); |
| 126 | + result.W = right.X * left.W.X; |
| 127 | + result.W += right.Y * left.W.Y; |
| 128 | + result.W += right.Z * left.W.Z; |
| 129 | + result.W += right.W * left.W.W; |
| 130 | + |
| 131 | + // result.V = Transform(left.V, in right); |
| 132 | + result.V = right.X * left.V.X; |
| 133 | + result.V += right.Y * left.V.Y; |
| 134 | + result.V += right.Z * left.V.Z; |
| 135 | + result.V += right.W * left.V.W; |
| 136 | + |
| 137 | + result.V += right.V; |
| 138 | + |
| 139 | + return result; |
| 140 | + } |
| 141 | + |
| 142 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 143 | + public static Impl operator *(in Impl left, float right) |
| 144 | + { |
| 145 | + Impl result; |
| 146 | + |
| 147 | + result.X = left.X * right; |
| 148 | + result.Y = left.Y * right; |
| 149 | + result.Z = left.Z * right; |
| 150 | + result.W = left.W * right; |
| 151 | + result.V = left.V * right; |
| 152 | + |
| 153 | + return result; |
| 154 | + } |
| 155 | + |
| 156 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 157 | + public static bool operator ==(in Impl left, in Impl right) => |
| 158 | + (left.X == right.X) |
| 159 | + && (left.Y == right.Y) |
| 160 | + && (left.Z == right.Z) |
| 161 | + && (left.W == right.W) |
| 162 | + && (left.V == right.V); |
| 163 | + |
| 164 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 165 | + public static bool operator !=(in Impl left, in Impl right) => |
| 166 | + (left.X != right.X) |
| 167 | + && (left.Y != right.Y) |
| 168 | + && (left.Z != right.Z) |
| 169 | + && (left.W != right.W) |
| 170 | + && (left.V != right.V); |
| 171 | + |
| 172 | + [UnscopedRef] |
| 173 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 174 | + public ref ColorMatrix AsColorMatrix() => ref Unsafe.As<Impl, ColorMatrix>(ref this); |
| 175 | + |
| 176 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 177 | + public void Init( |
| 178 | + float m11, float m12, float m13, float m14, |
| 179 | + float m21, float m22, float m23, float m24, |
| 180 | + float m31, float m32, float m33, float m34, |
| 181 | + float m41, float m42, float m43, float m44, |
| 182 | + float m51, float m52, float m53, float m54) |
| 183 | + { |
| 184 | + this.X = new Vector4(m11, m12, m13, m14); |
| 185 | + this.Y = new Vector4(m21, m22, m23, m24); |
| 186 | + this.Z = new Vector4(m31, m32, m33, m34); |
| 187 | + this.W = new Vector4(m41, m42, m43, m44); |
| 188 | + this.V = new Vector4(m51, m52, m53, m54); |
| 189 | + } |
| 190 | + |
| 191 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 192 | + public override readonly bool Equals([NotNullWhen(true)] object? obj) |
| 193 | + => (obj is ColorMatrix other) && this.Equals(in other.AsImpl()); |
| 194 | + |
| 195 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 196 | + public readonly bool Equals(in Impl other) => |
| 197 | + this.X.Equals(other.X) |
| 198 | + && this.Y.Equals(other.Y) |
| 199 | + && this.Z.Equals(other.Z) |
| 200 | + && this.W.Equals(other.W) |
| 201 | + && this.V.Equals(other.V); |
| 202 | + |
| 203 | + bool IEquatable<Impl>.Equals(Impl other) => this.Equals(in other); |
| 204 | + |
| 205 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 206 | + public override readonly int GetHashCode() => HashCode.Combine(this.X, this.Y, this.Z, this.W, this.V); |
| 207 | + } |
| 208 | +} |
0 commit comments