Skip to content

Commit c86677b

Browse files
committed
Adapt changes for ColorNumerics.Transform
1 parent ed71f00 commit c86677b

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/ImageSharp/Common/Helpers/ColorNumerics.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,24 +137,31 @@ public static int GetBitsNeededForColorDepth(int colors)
137137
public static int GetColorCountForBitDepth(int bitDepth)
138138
=> 1 << bitDepth;
139139

140+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
141+
internal static Vector4 Transform(Vector4 vector, in ColorMatrix.Impl matrix)
142+
{
143+
Vector4 result = matrix.X * vector.X;
144+
145+
result += matrix.Y * vector.Y;
146+
result += matrix.Z * vector.Z;
147+
result += matrix.W * vector.W;
148+
149+
result.X += matrix.V.X;
150+
result.Y += matrix.V.Y;
151+
result.Z += matrix.V.Z;
152+
result.W += matrix.V.W;
153+
154+
return result;
155+
}
156+
140157
/// <summary>
141158
/// Transforms a vector by the given color matrix.
142159
/// </summary>
143160
/// <param name="vector">The source vector.</param>
144161
/// <param name="matrix">The transformation color matrix.</param>
145162
[MethodImpl(MethodImplOptions.AggressiveInlining)]
146163
public static void Transform(ref Vector4 vector, ref ColorMatrix matrix)
147-
{
148-
float x = vector.X;
149-
float y = vector.Y;
150-
float z = vector.Z;
151-
float w = vector.W;
152-
153-
vector.X = (x * matrix.M11) + (y * matrix.M21) + (z * matrix.M31) + (w * matrix.M41) + matrix.M51;
154-
vector.Y = (x * matrix.M12) + (y * matrix.M22) + (z * matrix.M32) + (w * matrix.M42) + matrix.M52;
155-
vector.Z = (x * matrix.M13) + (y * matrix.M23) + (z * matrix.M33) + (w * matrix.M43) + matrix.M53;
156-
vector.W = (x * matrix.M14) + (y * matrix.M24) + (z * matrix.M34) + (w * matrix.M44) + matrix.M54;
157-
}
164+
=> vector = Transform(vector, matrix.AsImpl());
158165

159166
/// <summary>
160167
/// Bulk variant of <see cref="Transform(ref Vector4, ref ColorMatrix)"/>.

0 commit comments

Comments
 (0)