Browse Source

Use float to prevent casting.

Former-commit-id: 4d0f0912927e86fb3dda26bc8131c2b61e4913d6
Former-commit-id: 87fc5322e14aa23b36a5edc91e2a539be87e6568
Former-commit-id: ba498a73a37b2550eded45e46a37515e5182d157
pull/1/head
James Jackson-South 10 years ago
parent
commit
2922f76ec3
  1. 36
      src/ImageProcessorCore/Common/Helpers/ImageMaths.cs
  2. 28
      src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs
  3. 18
      src/ImageProcessorCore/Samplers/Resamplers/BicubicResampler.cs
  4. 6
      src/ImageProcessorCore/Samplers/Resamplers/BoxResampler.cs
  5. 8
      src/ImageProcessorCore/Samplers/Resamplers/CatmullRomResampler.cs
  6. 8
      src/ImageProcessorCore/Samplers/Resamplers/HermiteResampler.cs
  7. 6
      src/ImageProcessorCore/Samplers/Resamplers/IResampler.cs
  8. 12
      src/ImageProcessorCore/Samplers/Resamplers/Lanczos3Resampler.cs
  9. 12
      src/ImageProcessorCore/Samplers/Resamplers/Lanczos5Resampler.cs
  10. 12
      src/ImageProcessorCore/Samplers/Resamplers/Lanczos8Resampler.cs
  11. 8
      src/ImageProcessorCore/Samplers/Resamplers/MitchellNetravaliResampler.cs
  12. 4
      src/ImageProcessorCore/Samplers/Resamplers/NearestNeighborResampler.cs
  13. 8
      src/ImageProcessorCore/Samplers/Resamplers/RobidouxResampler.cs
  14. 8
      src/ImageProcessorCore/Samplers/Resamplers/RobidouxSharpResampler.cs
  15. 8
      src/ImageProcessorCore/Samplers/Resamplers/SplineResampler.cs
  16. 12
      src/ImageProcessorCore/Samplers/Resamplers/TriangleResampler.cs
  17. 12
      src/ImageProcessorCore/Samplers/Resamplers/WelchResampler.cs

36
src/ImageProcessorCore/Common/Helpers/ImageMaths.cs

@ -55,31 +55,31 @@ namespace ImageProcessorCore
/// <param name="b">The B-Spline curve variable.</param> /// <param name="b">The B-Spline curve variable.</param>
/// <param name="c">The Cardinal curve variable.</param> /// <param name="c">The Cardinal curve variable.</param>
/// <returns> /// <returns>
/// The <see cref="double"/>. /// The <see cref="float"/>.
/// </returns> /// </returns>
public static double GetBcValue(double x, double b, double c) public static float GetBcValue(float x, float b, float c)
{ {
double temp; float temp;
if (x < 0) if (x < 0F)
{ {
x = -x; x = -x;
} }
temp = x * x; temp = x * x;
if (x < 1) if (x < 1F)
{ {
x = ((12 - (9 * b) - (6 * c)) * (x * temp)) + ((-18 + (12 * b) + (6 * c)) * temp) + (6 - (2 * b)); x = ((12 - (9 * b) - (6 * c)) * (x * temp)) + ((-18 + (12 * b) + (6 * c)) * temp) + (6 - (2 * b));
return x / 6; return x / 6F;
} }
if (x < 2) if (x < 2F)
{ {
x = ((-b - (6 * c)) * (x * temp)) + (((6 * b) + (30 * c)) * temp) + (((-12 * b) - (48 * c)) * x) + ((8 * b) + (24 * c)); x = ((-b - (6 * c)) * (x * temp)) + (((6 * b) + (30 * c)) * temp) + (((-12 * b) - (48 * c)) * x) + ((8 * b) + (24 * c));
return x / 6; return x / 6F;
} }
return 0; return 0F;
} }
/// <summary> /// <summary>
@ -87,16 +87,16 @@ namespace ImageProcessorCore
/// </summary> /// </summary>
/// <param name="x">The value to calculate the result for.</param> /// <param name="x">The value to calculate the result for.</param>
/// <returns> /// <returns>
/// The <see cref="double"/>. /// The <see cref="float"/>.
/// </returns> /// </returns>
public static double SinC(double x) public static float SinC(float x)
{ {
const double Epsilon = .00001d; const float Epsilon = .00001F;
if (Math.Abs(x) > Epsilon) if (Math.Abs(x) > Epsilon)
{ {
x *= (double)Math.PI; x *= (float)Math.PI;
return Clean((double)Math.Sin(x) / x); return Clean((float)Math.Sin(x) / x);
} }
return 1.0f; return 1.0f;
@ -272,15 +272,15 @@ namespace ImageProcessorCore
/// </summary> /// </summary>
/// <param name="x">The value to clean.</param> /// <param name="x">The value to clean.</param>
/// <returns> /// <returns>
/// The <see cref="double"/> /// The <see cref="float"/>
/// </returns>. /// </returns>.
private static double Clean(double x) private static float Clean(float x)
{ {
const double Epsilon = .00001d; const float Epsilon = .00001F;
if (Math.Abs(x) < Epsilon) if (Math.Abs(x) < Epsilon)
{ {
return 0f; return 0F;
} }
return x; return x;

28
src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs

@ -141,7 +141,7 @@ namespace ImageProcessorCore.Processors
Weight xw = horizontalValues[i]; Weight xw = horizontalValues[i];
int originX = xw.Index; int originX = xw.Index;
Vector4 sourceColor = sourcePixels[originX, y].ToVector4(); Vector4 sourceColor = sourcePixels[originX, y].ToVector4();
destination += sourceColor * (float)xw.Value; destination += sourceColor * xw.Value;
} }
//if (compand) //if (compand)
@ -179,7 +179,7 @@ namespace ImageProcessorCore.Processors
Weight yw = verticalValues[i]; Weight yw = verticalValues[i];
int originY = yw.Index; int originY = yw.Index;
Vector4 sourceColor = firstPassPixels[x, originY].ToVector4(); Vector4 sourceColor = firstPassPixels[x, originY].ToVector4();
destination += sourceColor * (float)yw.Value; destination += sourceColor * yw.Value;
} }
//if (compand) //if (compand)
@ -219,12 +219,12 @@ namespace ImageProcessorCore.Processors
/// </returns> /// </returns>
protected Weights[] PrecomputeWeights(int destinationSize, int sourceSize) protected Weights[] PrecomputeWeights(int destinationSize, int sourceSize)
{ {
double scale = (double)destinationSize / sourceSize; float scale = (float)destinationSize / sourceSize;
IResampler sampler = this.Sampler; IResampler sampler = this.Sampler;
double radius = sampler.Radius; float radius = sampler.Radius;
double left; double left;
double right; double right;
double weight; float weight;
int index; int index;
int sum; int sum;
@ -234,13 +234,13 @@ namespace ImageProcessorCore.Processors
// visit every source pixel. // visit every source pixel.
if (scale < 1) if (scale < 1)
{ {
double width = radius / scale; float width = radius / scale;
double filterScale = 1 / scale; float filterScale = 1 / scale;
// Make the weights slices, one source for each column or row. // Make the weights slices, one source for each column or row.
for (int i = 0; i < destinationSize; i++) for (int i = 0; i < destinationSize; i++)
{ {
double centre = i / scale; float centre = i / scale;
left = Math.Ceiling(centre - width); left = Math.Ceiling(centre - width);
right = Math.Floor(centre + width); right = Math.Floor(centre + width);
@ -251,7 +251,7 @@ namespace ImageProcessorCore.Processors
for (double j = left; j <= right; j++) for (double j = left; j <= right; j++)
{ {
weight = sampler.GetValue((centre - j) / filterScale) / filterScale; weight = sampler.GetValue((float)((centre - j) / filterScale)) / filterScale;
if (j < 0) if (j < 0)
{ {
index = (int)-j; index = (int)-j;
@ -275,7 +275,7 @@ namespace ImageProcessorCore.Processors
// Make the weights slices, one source for each column or row. // Make the weights slices, one source for each column or row.
for (int i = 0; i < destinationSize; i++) for (int i = 0; i < destinationSize; i++)
{ {
double centre = i / scale; float centre = i / scale;
left = Math.Ceiling(centre - radius); left = Math.Ceiling(centre - radius);
right = Math.Floor(centre + radius); right = Math.Floor(centre + radius);
result[i] = new Weights result[i] = new Weights
@ -285,7 +285,7 @@ namespace ImageProcessorCore.Processors
for (double j = left; j <= right; j++) for (double j = left; j <= right; j++)
{ {
weight = sampler.GetValue(centre - j); weight = sampler.GetValue((float)(centre - j));
if (j < 0) if (j < 0)
{ {
index = (int)-j; index = (int)-j;
@ -318,7 +318,7 @@ namespace ImageProcessorCore.Processors
/// </summary> /// </summary>
/// <param name="index">The index.</param> /// <param name="index">The index.</param>
/// <param name="value">The value.</param> /// <param name="value">The value.</param>
public Weight(int index, double value) public Weight(int index, float value)
{ {
this.Index = index; this.Index = index;
this.Value = value; this.Value = value;
@ -332,7 +332,7 @@ namespace ImageProcessorCore.Processors
/// <summary> /// <summary>
/// Gets the result of the interpolation algorithm. /// Gets the result of the interpolation algorithm.
/// </summary> /// </summary>
public double Value { get; } public float Value { get; }
} }
/// <summary> /// <summary>
@ -348,7 +348,7 @@ namespace ImageProcessorCore.Processors
/// <summary> /// <summary>
/// Gets or sets the sum. /// Gets or sets the sum.
/// </summary> /// </summary>
public double Sum { get; set; } public float Sum { get; set; }
} }
} }
} }

18
src/ImageProcessorCore/Samplers/Resamplers/BicubicResampler.cs

@ -13,28 +13,28 @@ namespace ImageProcessorCore
public class BicubicResampler : IResampler public class BicubicResampler : IResampler
{ {
/// <inheritdoc/> /// <inheritdoc/>
public double Radius => 2; public float Radius => 2;
/// <inheritdoc/> /// <inheritdoc/>
public double GetValue(double x) public float GetValue(float x)
{ {
// The coefficient. // The coefficient.
double a = -0.5d; float a = -0.5F;
if (x < 0) if (x < 0F)
{ {
x = -x; x = -x;
} }
double result = 0; float result = 0;
if (x <= 1) if (x <= 1F)
{ {
result = (((1.5d * x) - 2.5d) * x * x) + 1; result = (((1.5F * x) - 2.5F) * x * x) + 1;
} }
else if (x < 2) else if (x < 2F)
{ {
result = (((((a * x) + 2.5d) * x) - 4) * x) + 2; result = (((((a * x) + 2.5F) * x) - 4) * x) + 2;
} }
return result; return result;

6
src/ImageProcessorCore/Samplers/Resamplers/BoxResampler.cs

@ -12,12 +12,12 @@ namespace ImageProcessorCore
public class BoxResampler : IResampler public class BoxResampler : IResampler
{ {
/// <inheritdoc/> /// <inheritdoc/>
public double Radius => 0.5d; public float Radius => 0.5F;
/// <inheritdoc/> /// <inheritdoc/>
public double GetValue(double x) public float GetValue(float x)
{ {
if (x > -0.5 && x <= 0.5) if (x > -0.5F && x <= 0.5F)
{ {
return 1; return 1;
} }

8
src/ImageProcessorCore/Samplers/Resamplers/CatmullRomResampler.cs

@ -14,13 +14,13 @@ namespace ImageProcessorCore
public class CatmullRomResampler : IResampler public class CatmullRomResampler : IResampler
{ {
/// <inheritdoc/> /// <inheritdoc/>
public double Radius => 2; public float Radius => 2;
/// <inheritdoc/> /// <inheritdoc/>
public double GetValue(double x) public float GetValue(float x)
{ {
const double B = 0; const float B = 0;
const double C = 0.5d; const float C = 0.5F;
return ImageMaths.GetBcValue(x, B, C); return ImageMaths.GetBcValue(x, B, C);
} }

8
src/ImageProcessorCore/Samplers/Resamplers/HermiteResampler.cs

@ -13,13 +13,13 @@ namespace ImageProcessorCore.Processors
public class HermiteResampler : IResampler public class HermiteResampler : IResampler
{ {
/// <inheritdoc/> /// <inheritdoc/>
public double Radius => 2; public float Radius => 2;
/// <inheritdoc/> /// <inheritdoc/>
public double GetValue(double x) public float GetValue(float x)
{ {
const double B = 0; const float B = 0F;
const double C = 0; const float C = 0F;
return ImageMaths.GetBcValue(x, B, C); return ImageMaths.GetBcValue(x, B, C);
} }

6
src/ImageProcessorCore/Samplers/Resamplers/IResampler.cs

@ -13,15 +13,15 @@ namespace ImageProcessorCore
/// <summary> /// <summary>
/// Gets the radius in which to sample pixels. /// Gets the radius in which to sample pixels.
/// </summary> /// </summary>
double Radius { get; } float Radius { get; }
/// <summary> /// <summary>
/// Gets the result of the interpolation algorithm. /// Gets the result of the interpolation algorithm.
/// </summary> /// </summary>
/// <param name="x">The value to process.</param> /// <param name="x">The value to process.</param>
/// <returns> /// <returns>
/// The <see cref="double"/> /// The <see cref="float"/>
/// </returns> /// </returns>
double GetValue(double x); float GetValue(float x);
} }
} }

12
src/ImageProcessorCore/Samplers/Resamplers/Lanczos3Resampler.cs

@ -13,22 +13,22 @@ namespace ImageProcessorCore
public class Lanczos3Resampler : IResampler public class Lanczos3Resampler : IResampler
{ {
/// <inheritdoc/> /// <inheritdoc/>
public double Radius => 3; public float Radius => 3;
/// <inheritdoc/> /// <inheritdoc/>
public double GetValue(double x) public float GetValue(float x)
{ {
if (x < 0) if (x < 0F)
{ {
x = -x; x = -x;
} }
if (x < 3) if (x < 3F)
{ {
return ImageMaths.SinC(x) * ImageMaths.SinC(x / 3d); return ImageMaths.SinC(x) * ImageMaths.SinC(x / 3F);
} }
return 0; return 0F;
} }
} }
} }

12
src/ImageProcessorCore/Samplers/Resamplers/Lanczos5Resampler.cs

@ -13,22 +13,22 @@ namespace ImageProcessorCore
public class Lanczos5Resampler : IResampler public class Lanczos5Resampler : IResampler
{ {
/// <inheritdoc/> /// <inheritdoc/>
public double Radius => 5; public float Radius => 5;
/// <inheritdoc/> /// <inheritdoc/>
public double GetValue(double x) public float GetValue(float x)
{ {
if (x < 0) if (x < 0F)
{ {
x = -x; x = -x;
} }
if (x < 5) if (x < 5F)
{ {
return ImageMaths.SinC(x) * ImageMaths.SinC(x / 5d); return ImageMaths.SinC(x) * ImageMaths.SinC(x / 5F);
} }
return 0; return 0F;
} }
} }
} }

12
src/ImageProcessorCore/Samplers/Resamplers/Lanczos8Resampler.cs

@ -13,22 +13,22 @@ namespace ImageProcessorCore
public class Lanczos8Resampler : IResampler public class Lanczos8Resampler : IResampler
{ {
/// <inheritdoc/> /// <inheritdoc/>
public double Radius => 8; public float Radius => 8;
/// <inheritdoc/> /// <inheritdoc/>
public double GetValue(double x) public float GetValue(float x)
{ {
if (x < 0) if (x < 0F)
{ {
x = -x; x = -x;
} }
if (x < 8) if (x < 8F)
{ {
return ImageMaths.SinC(x) * ImageMaths.SinC(x / 8d); return ImageMaths.SinC(x) * ImageMaths.SinC(x / 8F);
} }
return 0; return 0F;
} }
} }
} }

8
src/ImageProcessorCore/Samplers/Resamplers/MitchellNetravaliResampler.cs

@ -12,13 +12,13 @@ namespace ImageProcessorCore
public class MitchellNetravaliResampler : IResampler public class MitchellNetravaliResampler : IResampler
{ {
/// <inheritdoc/> /// <inheritdoc/>
public double Radius => 2; public float Radius => 2;
/// <inheritdoc/> /// <inheritdoc/>
public double GetValue(double x) public float GetValue(float x)
{ {
const double B = 0.333333333333333D; const float B = 0.3333333F;
const double C = 0.333333333333333D; const float C = 0.3333333F;
return ImageMaths.GetBcValue(x, B, C); return ImageMaths.GetBcValue(x, B, C);
} }

4
src/ImageProcessorCore/Samplers/Resamplers/NearestNeighborResampler.cs

@ -12,10 +12,10 @@ namespace ImageProcessorCore
public class NearestNeighborResampler : IResampler public class NearestNeighborResampler : IResampler
{ {
/// <inheritdoc/> /// <inheritdoc/>
public double Radius => 1; public float Radius => 1;
/// <inheritdoc/> /// <inheritdoc/>
public double GetValue(double x) public float GetValue(float x)
{ {
return x; return x;
} }

8
src/ImageProcessorCore/Samplers/Resamplers/RobidouxResampler.cs

@ -12,13 +12,13 @@ namespace ImageProcessorCore
public class RobidouxResampler : IResampler public class RobidouxResampler : IResampler
{ {
/// <inheritdoc/> /// <inheritdoc/>
public double Radius => 2; public float Radius => 2;
/// <inheritdoc/> /// <inheritdoc/>
public double GetValue(double x) public float GetValue(float x)
{ {
const double B = 0.37821575509399867D; const float B = 0.37821575509399867F;
const double C = 0.31089212245300067D; const float C = 0.31089212245300067F;
return ImageMaths.GetBcValue(x, B, C); return ImageMaths.GetBcValue(x, B, C);
} }

8
src/ImageProcessorCore/Samplers/Resamplers/RobidouxSharpResampler.cs

@ -12,13 +12,13 @@ namespace ImageProcessorCore
public class RobidouxSharpResampler : IResampler public class RobidouxSharpResampler : IResampler
{ {
/// <inheritdoc/> /// <inheritdoc/>
public double Radius => 2; public float Radius => 2;
/// <inheritdoc/> /// <inheritdoc/>
public double GetValue(double x) public float GetValue(float x)
{ {
const double B = 0.2620145123990142D; const float B = 0.2620145123990142F;
const double C = 0.3689927438004929D; const float C = 0.3689927438004929F;
return ImageMaths.GetBcValue(x, B, C); return ImageMaths.GetBcValue(x, B, C);
} }

8
src/ImageProcessorCore/Samplers/Resamplers/SplineResampler.cs

@ -12,13 +12,13 @@ namespace ImageProcessorCore
public class SplineResampler : IResampler public class SplineResampler : IResampler
{ {
/// <inheritdoc/> /// <inheritdoc/>
public double Radius => 2; public float Radius => 2;
/// <inheritdoc/> /// <inheritdoc/>
public double GetValue(double x) public float GetValue(float x)
{ {
const double B = 1; const float B = 1F;
const double C = 0; const float C = 0F;
return ImageMaths.GetBcValue(x, B, C); return ImageMaths.GetBcValue(x, B, C);
} }

12
src/ImageProcessorCore/Samplers/Resamplers/TriangleResampler.cs

@ -13,22 +13,22 @@ namespace ImageProcessorCore
public class TriangleResampler : IResampler public class TriangleResampler : IResampler
{ {
/// <inheritdoc/> /// <inheritdoc/>
public double Radius => 1; public float Radius => 1;
/// <inheritdoc/> /// <inheritdoc/>
public double GetValue(double x) public float GetValue(float x)
{ {
if (x < 0) if (x < 0F)
{ {
x = -x; x = -x;
} }
if (x < 1) if (x < 1F)
{ {
return 1 - x; return 1F - x;
} }
return 0; return 0F;
} }
} }
} }

12
src/ImageProcessorCore/Samplers/Resamplers/WelchResampler.cs

@ -12,22 +12,22 @@ namespace ImageProcessorCore
public class WelchResampler : IResampler public class WelchResampler : IResampler
{ {
/// <inheritdoc/> /// <inheritdoc/>
public double Radius => 3; public float Radius => 3;
/// <inheritdoc/> /// <inheritdoc/>
public double GetValue(double x) public float GetValue(float x)
{ {
if (x < 0) if (x < 0F)
{ {
x = -x; x = -x;
} }
if (x < 3) if (x < 3F)
{ {
return ImageMaths.SinC(x) * (1.0d - (x * x / 9.0d)); return ImageMaths.SinC(x) * (1F - (x * x / 9.0F));
} }
return 0; return 0F;
} }
} }
} }

Loading…
Cancel
Save