Browse Source

Merge pull request #60 from olivif/olivif/epsilonconst

Refactoring Epsilon const value to remove duplication.
af/merge-core
olivif 10 years ago
committed by GitHub
parent
commit
0b02d8f5fa
  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> /// </remarks>
public partial struct Color public partial struct Color
{ {
/// <summary>
/// The epsilon for comparing floating point numbers.
/// </summary>
private const float Epsilon = 0.001F;
/// <summary> /// <summary>
/// Allows the implicit conversion of an instance of <see cref="Color"/> to a /// Allows the implicit conversion of an instance of <see cref="Color"/> to a
/// <see cref="Bgra32"/>. /// <see cref="Bgra32"/>.
@ -110,12 +105,12 @@ namespace ImageSharp
float s = color.S; float s = color.S;
float v = color.V; float v = color.V;
if (Math.Abs(s) < Epsilon) if (Math.Abs(s) < Constants.Epsilon)
{ {
return new Color(v, v, v, 1); 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); int i = (int)Math.Truncate(h);
float f = h - i; float f = h - i;
@ -183,9 +178,9 @@ namespace ImageSharp
float s = color.S; float s = color.S;
float l = color.L; 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; r = g = b = l;
} }

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

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

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

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

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

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

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

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

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

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

13
src/ImageSharp/Colors/Vector4BlendTransforms.cs

@ -14,11 +14,6 @@ namespace ImageSharp
/// </summary> /// </summary>
public class Vector4BlendTransforms public class Vector4BlendTransforms
{ {
/// <summary>
/// The epsilon for comparing floating point numbers.
/// </summary>
private const float Epsilon = 0.0001F;
/// <summary> /// <summary>
/// The blending formula simply selects the source vector. /// The blending formula simply selects the source vector.
/// </summary> /// </summary>
@ -203,13 +198,13 @@ namespace ImageSharp
amount = amount.Clamp(0, 1); amount = amount.Clamp(0, 1);
// Santize on zero alpha // Santize on zero alpha
if (Math.Abs(backdrop.W) < Epsilon) if (Math.Abs(backdrop.W) < Constants.Epsilon)
{ {
source.W *= amount; source.W *= amount;
return source; return source;
} }
if (Math.Abs(source.W) < Epsilon) if (Math.Abs(source.W) < Constants.Epsilon)
{ {
return backdrop; return backdrop;
} }
@ -266,7 +261,7 @@ namespace ImageSharp
/// </returns> /// </returns>
private static float BlendDodge(float b, float s) 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> /// <summary>
@ -279,7 +274,7 @@ namespace ImageSharp
/// </returns> /// </returns>
private static float BlendBurn(float b, float s) 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> /// <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> /// </returns>
public static float SinC(float x) public static float SinC(float x)
{ {
const float Epsilon = .00001F; if (Math.Abs(x) > Constants.Epsilon)
if (Math.Abs(x) > Epsilon)
{ {
x *= (float)Math.PI; x *= (float)Math.PI;
return Clean((float)Math.Sin(x) / x); 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) public static Rectangle GetFilteredBoundingRectangle<TColor>(ImageBase<TColor> bitmap, float componentValue, RgbaComponent channel = RgbaComponent.B)
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {
const float Epsilon = .00001f;
int width = bitmap.Width; int width = bitmap.Width;
int height = bitmap.Height; int height = bitmap.Height;
Point topLeft = default(Point); Point topLeft = default(Point);
@ -178,19 +175,19 @@ namespace ImageSharp
switch (channel) switch (channel)
{ {
case RgbaComponent.R: 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; break;
case RgbaComponent.G: 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; break;
case RgbaComponent.B: 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; break;
default: 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; break;
} }
@ -278,9 +275,7 @@ namespace ImageSharp
/// </returns>. /// </returns>.
private static float Clean(float x) private static float Clean(float x)
{ {
const float Epsilon = .00001F; if (Math.Abs(x) < Constants.Epsilon)
if (Math.Abs(x) < Epsilon)
{ {
return 0F; return 0F;
} }

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

@ -9,7 +9,6 @@ namespace ImageSharp.Drawing.Processors
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using System.Threading.Tasks; using System.Threading.Tasks;
using ImageSharp.Processors; using ImageSharp.Processors;
using Paths; using Paths;
using Pens; using Pens;
@ -27,7 +26,6 @@ namespace ImageSharp.Drawing.Processors
{ {
private const float AntialiasFactor = 1f; private const float AntialiasFactor = 1f;
private const int PaddingFactor = 1; // needs to been the same or greater than AntialiasFactor 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 IPen<TColor> pen;
private readonly IPath[] paths; private readonly IPath[] paths;
@ -138,7 +136,7 @@ namespace ImageSharp.Drawing.Processors
var opacity = this.Opacity(color.DistanceFromElement); var opacity = this.Opacity(color.DistanceFromElement);
if (opacity > Epsilon) if (opacity > Constants.Epsilon)
{ {
int offsetColorX = x - minX; 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> public class FillShapeProcessor<TColor> : ImageFilteringProcessor<TColor>
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {
private const float Epsilon = 0.001f;
private const float AntialiasFactor = 1f; private const float AntialiasFactor = 1f;
private const int DrawPadding = 1; private const int DrawPadding = 1;
private readonly IBrush<TColor> fillColor; private readonly IBrush<TColor> fillColor;
@ -95,7 +93,7 @@ namespace ImageSharp.Drawing.Processors
float dist = this.poly.Distance(currentPoint); float dist = this.poly.Distance(currentPoint);
float opacity = this.Opacity(dist); float opacity = this.Opacity(dist);
if (opacity > Epsilon) if (opacity > Constants.Epsilon)
{ {
Vector4 backgroundVector = sourcePixels[offsetX, offsetY].ToVector4(); Vector4 backgroundVector = sourcePixels[offsetX, offsetY].ToVector4();
Vector4 sourceVector = applicator.GetColor(currentPoint).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> public class BackgroundColorProcessor<TColor> : ImageFilteringProcessor<TColor>
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {
/// <summary>
/// The epsilon for comparing floating point numbers.
/// </summary>
private const float Epsilon = 0.001f;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="BackgroundColorProcessor{TColor}"/> class. /// Initializes a new instance of the <see cref="BackgroundColorProcessor{TColor}"/> class.
/// </summary> /// </summary>
@ -82,7 +77,7 @@ namespace ImageSharp.Processors
color = Vector4BlendTransforms.PremultipliedLerp(backgroundColor, color, .5F); color = Vector4BlendTransforms.PremultipliedLerp(backgroundColor, color, .5F);
} }
if (Math.Abs(a) < Epsilon) if (Math.Abs(a) < Constants.Epsilon)
{ {
color = backgroundColor; color = backgroundColor;
} }

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

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

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

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