Skip to content

Commit 216257a

Browse files
Introduce Numerics and migrate ImageMaths methods
1 parent a9e2b4c commit 216257a

28 files changed

Lines changed: 781 additions & 317 deletions

src/ImageSharp/ColorSpaces/Companding/LCompanding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static class LCompanding
2424
/// <returns>The <see cref="float"/> representing the linear channel value.</returns>
2525
[MethodImpl(InliningOptions.ShortMethod)]
2626
public static float Expand(float channel)
27-
=> channel <= 0.08F ? (100F * channel) / CieConstants.Kappa : ImageMaths.Pow3((channel + 0.16F) / 1.16F);
27+
=> channel <= 0.08F ? (100F * channel) / CieConstants.Kappa : Numerics.Pow3((channel + 0.16F) / 1.16F);
2828

2929
/// <summary>
3030
/// Compresses an uncompanded channel (linear) to its nonlinear equivalent.

src/ImageSharp/ColorSpaces/Conversion/Implementation/Converters/CieLabToCieXyzConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public CieXyz Convert(in CieLab input)
2525
float fx = (a / 500F) + fy;
2626
float fz = fy - (b / 200F);
2727

28-
float fx3 = ImageMaths.Pow3(fx);
29-
float fz3 = ImageMaths.Pow3(fz);
28+
float fx3 = Numerics.Pow3(fx);
29+
float fz3 = Numerics.Pow3(fz);
3030

3131
float xr = fx3 > CieConstants.Epsilon ? fx3 : ((116F * fx) - 16F) / CieConstants.Kappa;
32-
float yr = l > CieConstants.Kappa * CieConstants.Epsilon ? ImageMaths.Pow3((l + 16F) / 116F) : l / CieConstants.Kappa;
32+
float yr = l > CieConstants.Kappa * CieConstants.Epsilon ? Numerics.Pow3((l + 16F) / 116F) : l / CieConstants.Kappa;
3333
float zr = fz3 > CieConstants.Epsilon ? fz3 : ((116F * fz) - 16F) / CieConstants.Kappa;
3434

3535
var wxyz = new Vector3(input.WhitePoint.X, input.WhitePoint.Y, input.WhitePoint.Z);

src/ImageSharp/ColorSpaces/Conversion/Implementation/Converters/CieLuvToCieXyzConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System.Runtime.CompilerServices;
@@ -24,7 +24,7 @@ public CieXyz Convert(in CieLuv input)
2424
float v0 = ComputeV0(input.WhitePoint);
2525

2626
float y = l > CieConstants.Kappa * CieConstants.Epsilon
27-
? ImageMaths.Pow3((l + 16) / 116)
27+
? Numerics.Pow3((l + 16) / 116)
2828
: l / CieConstants.Kappa;
2929

3030
float a = ((52 * l / (u + (13 * l * u0))) - 1) / 3;
@@ -71,4 +71,4 @@ private static float ComputeU0(in CieXyz input)
7171
private static float ComputeV0(in CieXyz input)
7272
=> (9 * input.Y) / (input.X + (15 * input.Y) + (3 * input.Z));
7373
}
74-
}
74+
}

src/ImageSharp/ColorSpaces/Conversion/Implementation/Converters/HunterLabToCieXyzConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public CieXyz Convert(in HunterLab input)
2626
float ka = ComputeKa(input.WhitePoint);
2727
float kb = ComputeKb(input.WhitePoint);
2828

29-
float pow = ImageMaths.Pow2(l / 100F);
29+
float pow = Numerics.Pow2(l / 100F);
3030
float sqrtPow = MathF.Sqrt(pow);
3131
float y = pow * yn;
3232

src/ImageSharp/Common/Helpers/ImageMaths.cs

Lines changed: 1 addition & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
using System;
55
using System.Numerics;
66
using System.Runtime.CompilerServices;
7-
87
using SixLabors.ImageSharp.PixelFormats;
98

109
namespace SixLabors.ImageSharp
1110
{
1211
/// <summary>
13-
/// Provides common mathematical methods.
12+
/// Provides common mathematical methods used for image processing.
1413
/// </summary>
1514
internal static class ImageMaths
1615
{
@@ -108,85 +107,6 @@ public static byte DownScaleFrom16BitTo8Bit(ushort component)
108107
[MethodImpl(InliningOptions.ShortMethod)]
109108
public static ushort UpscaleFrom8BitTo16Bit(byte component) => (ushort)(component * 257);
110109

111-
/// <summary>
112-
/// Determine the Greatest CommonDivisor (GCD) of two numbers.
113-
/// </summary>
114-
public static int GreatestCommonDivisor(int a, int b)
115-
{
116-
while (b != 0)
117-
{
118-
int temp = b;
119-
b = a % b;
120-
a = temp;
121-
}
122-
123-
return a;
124-
}
125-
126-
/// <summary>
127-
/// Determine the Least Common Multiple (LCM) of two numbers.
128-
/// </summary>
129-
public static int LeastCommonMultiple(int a, int b)
130-
{
131-
// https://en.wikipedia.org/wiki/Least_common_multiple#Reduction_by_the_greatest_common_divisor
132-
return (a / GreatestCommonDivisor(a, b)) * b;
133-
}
134-
135-
/// <summary>
136-
/// Calculates <paramref name="x"/> % 2
137-
/// </summary>
138-
[MethodImpl(InliningOptions.ShortMethod)]
139-
public static int Modulo2(int x) => x & 1;
140-
141-
/// <summary>
142-
/// Calculates <paramref name="x"/> % 4
143-
/// </summary>
144-
[MethodImpl(InliningOptions.ShortMethod)]
145-
public static int Modulo4(int x) => x & 3;
146-
147-
/// <summary>
148-
/// Calculates <paramref name="x"/> % 8
149-
/// </summary>
150-
[MethodImpl(InliningOptions.ShortMethod)]
151-
public static int Modulo8(int x) => x & 7;
152-
153-
/// <summary>
154-
/// Fast (x mod m) calculator, with the restriction that
155-
/// <paramref name="m"/> should be power of 2.
156-
/// </summary>
157-
[MethodImpl(InliningOptions.ShortMethod)]
158-
public static int ModuloP2(int x, int m) => x & (m - 1);
159-
160-
/// <summary>
161-
/// Returns the absolute value of a 32-bit signed integer. Uses bit shifting to speed up the operation.
162-
/// </summary>
163-
/// <param name="x">
164-
/// A number that is greater than <see cref="int.MinValue"/>, but less than or equal to <see cref="int.MaxValue"/>
165-
/// </param>
166-
/// <returns>The <see cref="int"/></returns>
167-
[MethodImpl(InliningOptions.ShortMethod)]
168-
public static int FastAbs(int x)
169-
{
170-
int y = x >> 31;
171-
return (x ^ y) - y;
172-
}
173-
174-
/// <summary>
175-
/// Returns a specified number raised to the power of 2
176-
/// </summary>
177-
/// <param name="x">A single-precision floating-point number</param>
178-
/// <returns>The number <paramref name="x" /> raised to the power of 2.</returns>
179-
[MethodImpl(InliningOptions.ShortMethod)]
180-
public static float Pow2(float x) => x * x;
181-
182-
/// <summary>
183-
/// Returns a specified number raised to the power of 3
184-
/// </summary>
185-
/// <param name="x">A single-precision floating-point number</param>
186-
/// <returns>The number <paramref name="x" /> raised to the power of 3.</returns>
187-
[MethodImpl(InliningOptions.ShortMethod)]
188-
public static float Pow3(float x) => x * x * x;
189-
190110
/// <summary>
191111
/// Returns how many bits are required to store the specified number of colors.
192112
/// Performs a Log2() on the value.
@@ -206,48 +126,6 @@ public static int FastAbs(int x)
206126
[MethodImpl(InliningOptions.ShortMethod)]
207127
public static int GetColorCountForBitDepth(int bitDepth) => 1 << bitDepth;
208128

209-
/// <summary>
210-
/// Implementation of 1D Gaussian G(x) function
211-
/// </summary>
212-
/// <param name="x">The x provided to G(x).</param>
213-
/// <param name="sigma">The spread of the blur.</param>
214-
/// <returns>The Gaussian G(x)</returns>
215-
[MethodImpl(InliningOptions.ShortMethod)]
216-
public static float Gaussian(float x, float sigma)
217-
{
218-
const float Numerator = 1.0f;
219-
float denominator = MathF.Sqrt(2 * MathF.PI) * sigma;
220-
221-
float exponentNumerator = -x * x;
222-
float exponentDenominator = 2 * Pow2(sigma);
223-
224-
float left = Numerator / denominator;
225-
float right = MathF.Exp(exponentNumerator / exponentDenominator);
226-
227-
return left * right;
228-
}
229-
230-
/// <summary>
231-
/// Returns the result of a normalized sine cardinal function for the given value.
232-
/// SinC(x) = sin(pi*x)/(pi*x).
233-
/// </summary>
234-
/// <param name="f">A single-precision floating-point number to calculate the result for.</param>
235-
/// <returns>
236-
/// The sine cardinal of <paramref name="f" />.
237-
/// </returns>
238-
[MethodImpl(InliningOptions.ShortMethod)]
239-
public static float SinC(float f)
240-
{
241-
if (MathF.Abs(f) > Constants.Epsilon)
242-
{
243-
f *= MathF.PI;
244-
float result = MathF.Sin(f) / f;
245-
return MathF.Abs(result) < Constants.Epsilon ? 0F : result;
246-
}
247-
248-
return 1F;
249-
}
250-
251129
/// <summary>
252130
/// Gets the bounding <see cref="Rectangle"/> from the given points.
253131
/// </summary>

0 commit comments

Comments
 (0)