Skip to content

Commit d6f4821

Browse files
No more Vector4Utils & ImageMath
1 parent 9216ae0 commit d6f4821

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+763
-770
lines changed

src/ImageSharp/Color/Color.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ namespace SixLabors.ImageSharp
2727
private Color(byte r, byte g, byte b, byte a)
2828
{
2929
this.data = new Rgba64(
30-
ImageMath.UpscaleFrom8BitTo16Bit(r),
31-
ImageMath.UpscaleFrom8BitTo16Bit(g),
32-
ImageMath.UpscaleFrom8BitTo16Bit(b),
33-
ImageMath.UpscaleFrom8BitTo16Bit(a));
30+
ColorNumerics.UpscaleFrom8BitTo16Bit(r),
31+
ColorNumerics.UpscaleFrom8BitTo16Bit(g),
32+
ColorNumerics.UpscaleFrom8BitTo16Bit(b),
33+
ColorNumerics.UpscaleFrom8BitTo16Bit(a));
3434
}
3535

3636
[MethodImpl(InliningOptions.ShortMethod)]
3737
private Color(byte r, byte g, byte b)
3838
{
3939
this.data = new Rgba64(
40-
ImageMath.UpscaleFrom8BitTo16Bit(r),
41-
ImageMath.UpscaleFrom8BitTo16Bit(g),
42-
ImageMath.UpscaleFrom8BitTo16Bit(b),
40+
ColorNumerics.UpscaleFrom8BitTo16Bit(r),
41+
ColorNumerics.UpscaleFrom8BitTo16Bit(g),
42+
ColorNumerics.UpscaleFrom8BitTo16Bit(b),
4343
ushort.MaxValue);
4444
}
4545

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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+
}

src/ImageSharp/Common/Helpers/DenseMatrixUtils.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void Convolve2D3<TPixel>(
5959
ref Vector4 target = ref Unsafe.Add(ref targetRowRef, column);
6060
vector.W = target.W;
6161

62-
Vector4Utils.UnPremultiply(ref vector);
62+
Numerics.UnPremultiply(ref vector);
6363
target = vector;
6464
}
6565

@@ -105,7 +105,7 @@ public static void Convolve2D4<TPixel>(
105105
out Vector4 vector);
106106

107107
ref Vector4 target = ref Unsafe.Add(ref targetRowRef, column);
108-
Vector4Utils.UnPremultiply(ref vector);
108+
Numerics.UnPremultiply(ref vector);
109109
target = vector;
110110
}
111111

@@ -140,7 +140,7 @@ public static void Convolve2DImpl<TPixel>(
140140
{
141141
int offsetX = Numerics.Clamp(sourceOffsetColumnBase + x - radiusX, minColumn, maxColumn);
142142
var currentColor = sourceRowSpan[offsetX].ToVector4();
143-
Vector4Utils.Premultiply(ref currentColor);
143+
Numerics.Premultiply(ref currentColor);
144144

145145
vectorX += matrixX[y, x] * currentColor;
146146
vectorY += matrixY[y, x] * currentColor;
@@ -193,7 +193,7 @@ public static void Convolve3<TPixel>(
193193
ref Vector4 target = ref Unsafe.Add(ref targetRowRef, column);
194194
vector.W = target.W;
195195

196-
Vector4Utils.UnPremultiply(ref vector);
196+
Numerics.UnPremultiply(ref vector);
197197
target = vector;
198198
}
199199

@@ -238,7 +238,7 @@ public static void Convolve4<TPixel>(
238238
ref vector);
239239

240240
ref Vector4 target = ref Unsafe.Add(ref targetRowRef, column);
241-
Vector4Utils.UnPremultiply(ref vector);
241+
Numerics.UnPremultiply(ref vector);
242242
target = vector;
243243
}
244244

@@ -270,7 +270,7 @@ private static void ConvolveImpl<TPixel>(
270270
{
271271
int offsetX = Numerics.Clamp(sourceOffsetColumnBase + x - radiusX, minColumn, maxColumn);
272272
var currentColor = sourceRowSpan[offsetX].ToVector4();
273-
Vector4Utils.Premultiply(ref currentColor);
273+
Numerics.Premultiply(ref currentColor);
274274
vector += matrix[y, x] * currentColor;
275275
}
276276
}

0 commit comments

Comments
 (0)