Browse Source

Merge pull request #60 from olivif/olivif/epsilonconst

Refactoring Epsilon const value to remove duplication.
pull/65/head
olivif 9 years ago
committed by GitHub
parent
commit
5d5ea67ac8
  1. 13
      src/ImageSharp/Colors/ColorspaceTransforms.cs
  2. 7
      src/ImageSharp/Colors/Spaces/CieLab.cs
  3. 7
      src/ImageSharp/Colors/Spaces/CieXyz.cs
  4. 9
      src/ImageSharp/Colors/Spaces/Cmyk.cs
  5. 15
      src/ImageSharp/Colors/Spaces/Hsl.cs
  6. 15
      src/ImageSharp/Colors/Spaces/Hsv.cs
  7. 13
      src/ImageSharp/Colors/Vector4BlendTransforms.cs
  8. 18
      src/ImageSharp/Common/Constants.cs
  9. 17
      src/ImageSharp/Common/Helpers/ImageMaths.cs
  10. 4
      src/ImageSharp/Drawing/Processors/DrawPathProcessor.cs
  11. 4
      src/ImageSharp/Drawing/Processors/FillShapeProcessor.cs
  12. 7
      src/ImageSharp/Filters/Processors/Effects/BackgroundColorProcessor.cs
  13. 13
      src/ImageSharp/Filters/Processors/Transforms/RotateProcessor.cs
  14. 11
      src/ImageSharp/Quantizers/Wu/WuQuantizer.cs
  15. 18
      tests/ImageSharp.Tests/Common/ConstantsTests.cs

13
src/ImageSharp/Colors/ColorspaceTransforms.cs

@ -19,11 +19,6 @@ namespace ImageSharp
/// </remarks>
public partial struct Color
{
/// <summary>
/// The epsilon for comparing floating point numbers.
/// </summary>
private const float Epsilon = 0.001F;
/// <summary>
/// Allows the implicit conversion of an instance of <see cref="Color"/> to a
/// <see cref="Bgra32"/>.
@ -110,12 +105,12 @@ namespace ImageSharp
float s = color.S;
float v = color.V;
if (Math.Abs(s) < Epsilon)
if (Math.Abs(s) < Constants.Epsilon)
{
return new Color(v, v, v, 1);
}
float h = (Math.Abs(color.H - 360) < Epsilon) ? 0 : color.H / 60;
float h = (Math.Abs(color.H - 360) < Constants.Epsilon) ? 0 : color.H / 60;
int i = (int)Math.Truncate(h);
float f = h - i;
@ -183,9 +178,9 @@ namespace ImageSharp
float s = color.S;
float l = color.L;
if (Math.Abs(l) > Epsilon)
if (Math.Abs(l) > Constants.Epsilon)
{
if (Math.Abs(s) < Epsilon)
if (Math.Abs(s) < Constants.Epsilon)
{
r = g = b = l;
}

7
src/ImageSharp/Colors/Spaces/CieLab.cs

@ -20,11 +20,6 @@ namespace ImageSharp.Colors.Spaces
/// </summary>
public static readonly CieLab Empty = default(CieLab);
/// <summary>
/// The epsilon for comparing floating point numbers.
/// </summary>
private const float Epsilon = 0.001f;
/// <summary>
/// The backing vector for SIMD support.
/// </summary>
@ -166,7 +161,7 @@ namespace ImageSharp.Colors.Spaces
/// <inheritdoc/>
public bool Equals(CieLab other)
{
return this.AlmostEquals(other, Epsilon);
return this.AlmostEquals(other, Constants.Epsilon);
}
/// <inheritdoc/>

7
src/ImageSharp/Colors/Spaces/CieXyz.cs

@ -20,11 +20,6 @@ namespace ImageSharp.Colors.Spaces
/// </summary>
public static readonly CieXyz Empty = default(CieXyz);
/// <summary>
/// The epsilon for comparing floating point numbers.
/// </summary>
private const float Epsilon = 0.001f;
/// <summary>
/// The backing vector for SIMD support.
/// </summary>
@ -157,7 +152,7 @@ namespace ImageSharp.Colors.Spaces
/// <inheritdoc/>
public bool Equals(CieXyz other)
{
return this.AlmostEquals(other, Epsilon);
return this.AlmostEquals(other, Constants.Epsilon);
}
/// <inheritdoc/>

9
src/ImageSharp/Colors/Spaces/Cmyk.cs

@ -19,11 +19,6 @@ namespace ImageSharp.Colors.Spaces
/// </summary>
public static readonly Cmyk Empty = default(Cmyk);
/// <summary>
/// The epsilon for comparing floating point numbers.
/// </summary>
private const float Epsilon = 0.001f;
/// <summary>
/// The backing vector for SIMD support.
/// </summary>
@ -90,7 +85,7 @@ namespace ImageSharp.Colors.Spaces
float k = Math.Min(c, Math.Min(m, y));
if (Math.Abs(k - 1.0f) <= Epsilon)
if (Math.Abs(k - 1.0f) <= Constants.Epsilon)
{
return new Cmyk(0, 0, 0, 1);
}
@ -167,7 +162,7 @@ namespace ImageSharp.Colors.Spaces
/// <inheritdoc/>
public bool Equals(Cmyk other)
{
return this.AlmostEquals(other, Epsilon);
return this.AlmostEquals(other, Constants.Epsilon);
}
/// <inheritdoc/>

15
src/ImageSharp/Colors/Spaces/Hsl.cs

@ -19,11 +19,6 @@ namespace ImageSharp.Colors.Spaces
/// </summary>
public static readonly Hsl Empty = default(Hsl);
/// <summary>
/// The epsilon for comparing floating point numbers.
/// </summary>
private const float Epsilon = 0.001F;
/// <summary>
/// The backing vector for SIMD support.
/// </summary>
@ -85,20 +80,20 @@ namespace ImageSharp.Colors.Spaces
float s = 0;
float l = (max + min) / 2;
if (Math.Abs(chroma) < Epsilon)
if (Math.Abs(chroma) < Constants.Epsilon)
{
return new Hsl(0, s, l);
}
if (Math.Abs(r - max) < Epsilon)
if (Math.Abs(r - max) < Constants.Epsilon)
{
h = (g - b) / chroma;
}
else if (Math.Abs(g - max) < Epsilon)
else if (Math.Abs(g - max) < Constants.Epsilon)
{
h = 2 + ((b - r) / chroma);
}
else if (Math.Abs(b - max) < Epsilon)
else if (Math.Abs(b - max) < Constants.Epsilon)
{
h = 4 + ((r - g) / chroma);
}
@ -186,7 +181,7 @@ namespace ImageSharp.Colors.Spaces
/// <inheritdoc/>
public bool Equals(Hsl other)
{
return this.AlmostEquals(other, Epsilon);
return this.AlmostEquals(other, Constants.Epsilon);
}
/// <inheritdoc/>

15
src/ImageSharp/Colors/Spaces/Hsv.cs

@ -19,11 +19,6 @@ namespace ImageSharp.Colors.Spaces
/// </summary>
public static readonly Hsv Empty = default(Hsv);
/// <summary>
/// The epsilon for comparing floating point numbers.
/// </summary>
private const float Epsilon = 0.001F;
/// <summary>
/// The backing vector for SIMD support.
/// </summary>
@ -85,20 +80,20 @@ namespace ImageSharp.Colors.Spaces
float s = 0;
float v = max;
if (Math.Abs(chroma) < Epsilon)
if (Math.Abs(chroma) < Constants.Epsilon)
{
return new Hsv(0, s, v);
}
if (Math.Abs(r - max) < Epsilon)
if (Math.Abs(r - max) < Constants.Epsilon)
{
h = (g - b) / chroma;
}
else if (Math.Abs(g - max) < Epsilon)
else if (Math.Abs(g - max) < Constants.Epsilon)
{
h = 2 + ((b - r) / chroma);
}
else if (Math.Abs(b - max) < Epsilon)
else if (Math.Abs(b - max) < Constants.Epsilon)
{
h = 4 + ((r - g) / chroma);
}
@ -179,7 +174,7 @@ namespace ImageSharp.Colors.Spaces
/// <inheritdoc/>
public bool Equals(Hsv other)
{
return this.AlmostEquals(other, Epsilon);
return this.AlmostEquals(other, Constants.Epsilon);
}
/// <inheritdoc/>

13
src/ImageSharp/Colors/Vector4BlendTransforms.cs

@ -14,11 +14,6 @@ namespace ImageSharp
/// </summary>
public class Vector4BlendTransforms
{
/// <summary>
/// The epsilon for comparing floating point numbers.
/// </summary>
private const float Epsilon = 0.0001F;
/// <summary>
/// The blending formula simply selects the source vector.
/// </summary>
@ -203,13 +198,13 @@ namespace ImageSharp
amount = amount.Clamp(0, 1);
// Santize on zero alpha
if (Math.Abs(backdrop.W) < Epsilon)
if (Math.Abs(backdrop.W) < Constants.Epsilon)
{
source.W *= amount;
return source;
}
if (Math.Abs(source.W) < Epsilon)
if (Math.Abs(source.W) < Constants.Epsilon)
{
return backdrop;
}
@ -266,7 +261,7 @@ namespace ImageSharp
/// </returns>
private static float BlendDodge(float b, float s)
{
return Math.Abs(s - 1F) < Epsilon ? s : Math.Min(b / (1F - s), 1F);
return Math.Abs(s - 1F) < Constants.Epsilon ? s : Math.Min(b / (1F - s), 1F);
}
/// <summary>
@ -279,7 +274,7 @@ namespace ImageSharp
/// </returns>
private static float BlendBurn(float b, float s)
{
return Math.Abs(s) < Epsilon ? s : Math.Max(1F - ((1F - b) / s), 0F);
return Math.Abs(s) < Constants.Epsilon ? s : Math.Max(1F - ((1F - b) / s), 0F);
}
/// <summary>

18
src/ImageSharp/Common/Constants.cs

@ -0,0 +1,18 @@
// <copyright file="Constants.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
{
/// <summary>
/// Common constants used throughout the project
/// </summary>
internal static class Constants
{
/// <summary>
/// The epsilon for comparing floating point numbers.
/// </summary>
public static readonly float Epsilon = 0.001f;
}
}

17
src/ImageSharp/Common/Helpers/ImageMaths.cs

@ -91,9 +91,7 @@ namespace ImageSharp
/// </returns>
public static float SinC(float x)
{
const float Epsilon = .00001F;
if (Math.Abs(x) > Epsilon)
if (Math.Abs(x) > Constants.Epsilon)
{
x *= (float)Math.PI;
return Clean((float)Math.Sin(x) / x);
@ -166,7 +164,6 @@ namespace ImageSharp
public static Rectangle GetFilteredBoundingRectangle<TColor>(ImageBase<TColor> bitmap, float componentValue, RgbaComponent channel = RgbaComponent.B)
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
const float Epsilon = .00001f;
int width = bitmap.Width;
int height = bitmap.Height;
Point topLeft = default(Point);
@ -178,19 +175,19 @@ namespace ImageSharp
switch (channel)
{
case RgbaComponent.R:
delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().X - b) > Epsilon;
delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().X - b) > Constants.Epsilon;
break;
case RgbaComponent.G:
delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().Y - b) > Epsilon;
delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().Y - b) > Constants.Epsilon;
break;
case RgbaComponent.B:
delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().Z - b) > Epsilon;
delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().Z - b) > Constants.Epsilon;
break;
default:
delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().W - b) > Epsilon;
delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().W - b) > Constants.Epsilon;
break;
}
@ -278,9 +275,7 @@ namespace ImageSharp
/// </returns>.
private static float Clean(float x)
{
const float Epsilon = .00001F;
if (Math.Abs(x) < Epsilon)
if (Math.Abs(x) < Constants.Epsilon)
{
return 0F;
}

4
src/ImageSharp/Drawing/Processors/DrawPathProcessor.cs

@ -9,7 +9,6 @@ namespace ImageSharp.Drawing.Processors
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using ImageSharp.Processors;
using Paths;
using Pens;
@ -27,7 +26,6 @@ namespace ImageSharp.Drawing.Processors
{
private const float AntialiasFactor = 1f;
private const int PaddingFactor = 1; // needs to been the same or greater than AntialiasFactor
private const float Epsilon = 0.001f;
private readonly IPen<TColor> pen;
private readonly IPath[] paths;
@ -138,7 +136,7 @@ namespace ImageSharp.Drawing.Processors
var opacity = this.Opacity(color.DistanceFromElement);
if (opacity > Epsilon)
if (opacity > Constants.Epsilon)
{
int offsetColorX = x - minX;

4
src/ImageSharp/Drawing/Processors/FillShapeProcessor.cs

@ -21,8 +21,6 @@ namespace ImageSharp.Drawing.Processors
public class FillShapeProcessor<TColor> : ImageFilteringProcessor<TColor>
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
private const float Epsilon = 0.001f;
private const float AntialiasFactor = 1f;
private const int DrawPadding = 1;
private readonly IBrush<TColor> fillColor;
@ -95,7 +93,7 @@ namespace ImageSharp.Drawing.Processors
float dist = this.poly.Distance(currentPoint);
float opacity = this.Opacity(dist);
if (opacity > Epsilon)
if (opacity > Constants.Epsilon)
{
Vector4 backgroundVector = sourcePixels[offsetX, offsetY].ToVector4();
Vector4 sourceVector = applicator.GetColor(currentPoint).ToVector4();

7
src/ImageSharp/Filters/Processors/Effects/BackgroundColorProcessor.cs

@ -16,11 +16,6 @@ namespace ImageSharp.Processors
public class BackgroundColorProcessor<TColor> : ImageFilteringProcessor<TColor>
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
/// <summary>
/// The epsilon for comparing floating point numbers.
/// </summary>
private const float Epsilon = 0.001f;
/// <summary>
/// Initializes a new instance of the <see cref="BackgroundColorProcessor{TColor}"/> class.
/// </summary>
@ -82,7 +77,7 @@ namespace ImageSharp.Processors
color = Vector4BlendTransforms.PremultipliedLerp(backgroundColor, color, .5F);
}
if (Math.Abs(a) < Epsilon)
if (Math.Abs(a) < Constants.Epsilon)
{
color = backgroundColor;
}

13
src/ImageSharp/Filters/Processors/Transforms/RotateProcessor.cs

@ -70,9 +70,7 @@ namespace ImageSharp.Processors
/// <inheritdoc/>
protected override void BeforeApply(ImageBase<TColor> source, Rectangle sourceRectangle)
{
const float Epsilon = .0001F;
if (Math.Abs(this.Angle) < Epsilon || Math.Abs(this.Angle - 90) < Epsilon || Math.Abs(this.Angle - 180) < Epsilon || Math.Abs(this.Angle - 270) < Epsilon)
if (Math.Abs(this.Angle) < Constants.Epsilon || Math.Abs(this.Angle - 90) < Constants.Epsilon || Math.Abs(this.Angle - 180) < Constants.Epsilon || Math.Abs(this.Angle - 270) < Constants.Epsilon)
{
return;
}
@ -91,26 +89,25 @@ namespace ImageSharp.Processors
/// <returns>The <see cref="bool"/></returns>
private bool OptimizedApply(ImageBase<TColor> source)
{
const float Epsilon = .0001F;
if (Math.Abs(this.Angle) < Epsilon)
if (Math.Abs(this.Angle) < Constants.Epsilon)
{
// No need to do anything so return.
return true;
}
if (Math.Abs(this.Angle - 90) < Epsilon)
if (Math.Abs(this.Angle - 90) < Constants.Epsilon)
{
this.Rotate90(source);
return true;
}
if (Math.Abs(this.Angle - 180) < Epsilon)
if (Math.Abs(this.Angle - 180) < Constants.Epsilon)
{
this.Rotate180(source);
return true;
}
if (Math.Abs(this.Angle - 270) < Epsilon)
if (Math.Abs(this.Angle - 270) < Constants.Epsilon)
{
this.Rotate270(source);
return true;

11
src/ImageSharp/Quantizers/Wu/WuQuantizer.cs

@ -33,11 +33,6 @@ namespace ImageSharp.Quantizers
public sealed class WuQuantizer<TColor> : IQuantizer<TColor>
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
/// <summary>
/// The epsilon for comparing floating point numbers.
/// </summary>
private const float Epsilon = 1e-5F;
/// <summary>
/// The index bits.
/// </summary>
@ -542,7 +537,7 @@ namespace ImageSharp.Quantizers
double temp;
if (Math.Abs(halfW) < Epsilon)
if (Math.Abs(halfW) < Constants.Epsilon)
{
continue;
}
@ -555,7 +550,7 @@ namespace ImageSharp.Quantizers
halfA = wholeA - halfA;
halfW = wholeW - halfW;
if (Math.Abs(halfW) < Epsilon)
if (Math.Abs(halfW) < Constants.Epsilon)
{
continue;
}
@ -762,7 +757,7 @@ namespace ImageSharp.Quantizers
double weight = Volume(cube[k], this.vwt);
if (Math.Abs(weight) > Epsilon)
if (Math.Abs(weight) > Constants.Epsilon)
{
float r = (float)(Volume(cube[k], this.vmr) / weight);
float g = (float)(Volume(cube[k], this.vmg) / weight);

18
tests/ImageSharp.Tests/Common/ConstantsTests.cs

@ -0,0 +1,18 @@
// <copyright file="ConstantsTests.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests.Common
{
using Xunit;
public class ConstantsTests
{
[Fact]
public void Epsilon()
{
Assert.Equal(Constants.Epsilon, 0.001f);
}
}
}
Loading…
Cancel
Save