Browse Source

Improve lookup logic

af/merge-core
James Jackson-South 8 years ago
parent
commit
79cfbacd98
  1. 9
      src/ImageSharp/Common/Constants.cs
  2. 14
      src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs

9
src/ImageSharp/Common/Constants.cs

@ -9,8 +9,13 @@ namespace SixLabors.ImageSharp
internal static class Constants
{
/// <summary>
/// The epsilon for comparing floating point numbers.
/// The epsilon value for comparing floating point numbers.
/// </summary>
public static readonly float Epsilon = 0.001f;
public static readonly float Epsilon = 0.001F;
/// <summary>
/// The epsilon squared value for comparing floating point numbers.
/// </summary>
public static readonly float EpsilonSquared = Epsilon * Epsilon;
}
}

14
src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs

@ -144,20 +144,24 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers
int colorIndex = 0;
float leastDistance = float.MaxValue;
var vector = pixel.ToVector4();
float epsilon = Constants.EpsilonSquared;
for (int index = 0; index < colorPalette.Length; index++)
{
ref TPixel candidate = ref colorPalette[index];
float distance = Vector4.DistanceSquared(vector, candidate.ToVector4());
if (distance < leastDistance)
// Greater... Move on.
if (!(distance < leastDistance))
{
colorIndex = index;
leastDistance = distance;
continue;
}
// If it's an exact match, exit the loop
if (distance == 0)
colorIndex = index;
leastDistance = distance;
// And if it's an exact match, exit the loop
if (distance < epsilon)
{
break;
}

Loading…
Cancel
Save