Browse Source

Refactoring the rest to use a const epsilon.

af/merge-core
Olivia 9 years ago
parent
commit
61b7d3aedb
  1. 5
      src/ImageSharp/Drawing/Processors/FillShapeProcessor.cs
  2. 8
      src/ImageSharp/Filters/Processors/Effects/BackgroundColorProcessor.cs
  3. 14
      src/ImageSharp/Filters/Processors/Transforms/RotateProcessor.cs
  4. 12
      src/ImageSharp/Quantizers/Wu/WuQuantizer.cs

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

@ -8,6 +8,7 @@ namespace ImageSharp.Drawing.Processors
using System; using System;
using System.Numerics; using System.Numerics;
using System.Threading.Tasks; using System.Threading.Tasks;
using Common;
using Drawing; using Drawing;
using ImageSharp.Processors; using ImageSharp.Processors;
using Shapes; using Shapes;
@ -21,8 +22,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 +94,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();

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

@ -8,6 +8,7 @@ namespace ImageSharp.Processors
using System; using System;
using System.Numerics; using System.Numerics;
using System.Threading.Tasks; using System.Threading.Tasks;
using Common;
/// <summary> /// <summary>
/// Sets the background color of the image. /// Sets the background color of the image.
@ -16,11 +17,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 +78,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;
} }

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

@ -8,6 +8,7 @@ namespace ImageSharp.Processors
using System; using System;
using System.Numerics; using System.Numerics;
using System.Threading.Tasks; using System.Threading.Tasks;
using Common;
/// <summary> /// <summary>
/// Provides methods that allow the rotating of images. /// Provides methods that allow the rotating of images.
@ -70,9 +71,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 +90,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;

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

@ -9,6 +9,7 @@ namespace ImageSharp.Quantizers
using System.Buffers; using System.Buffers;
using System.Numerics; using System.Numerics;
using System.Threading.Tasks; using System.Threading.Tasks;
using Common;
/// <summary> /// <summary>
/// An implementation of Wu's color quantizer with alpha channel. /// An implementation of Wu's color quantizer with alpha channel.
@ -33,11 +34,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 +538,7 @@ namespace ImageSharp.Quantizers
double temp; double temp;
if (Math.Abs(halfW) < Epsilon) if (Math.Abs(halfW) < Constants.Epsilon)
{ {
continue; continue;
} }
@ -555,7 +551,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 +758,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);

Loading…
Cancel
Save