44using System ;
55using System . Numerics ;
66using System . Runtime . CompilerServices ;
7-
87using SixLabors . ImageSharp . PixelFormats ;
98
109namespace 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