Browse Source

Use Vector2

af/merge-core
James Jackson-South 10 years ago
parent
commit
75775a6974
  1. 22
      src/ImageSharp/Quantizers/Palette/PaletteQuantizer.cs

22
src/ImageSharp/Quantizers/Palette/PaletteQuantizer.cs

@ -7,6 +7,7 @@ namespace ImageSharp.Quantizers
{ {
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics;
/// <summary> /// <summary>
/// Encapsulates methods to create a quantized image based upon the given palette. /// Encapsulates methods to create a quantized image based upon the given palette.
@ -85,25 +86,12 @@ namespace ImageSharp.Quantizers
else else
{ {
// Not found - loop through the palette and find the nearest match. // Not found - loop through the palette and find the nearest match.
pixel.ToBytes(this.pixelBuffer, 0, ComponentOrder.XYZW); float leastDistance = int.MaxValue;
int leastDistance = int.MaxValue; Vector4 vector = pixel.ToVector4();
int red = this.pixelBuffer[0];
int green = this.pixelBuffer[1];
int blue = this.pixelBuffer[2];
int alpha = this.pixelBuffer[3];
for (int index = 0; index < this.colors.Length; index++) for (int index = 0; index < this.colors.Length; index++)
{ {
this.colors[index].ToBytes(this.pixelBuffer, 0, ComponentOrder.XYZW); float distance = Vector4.Distance(vector, this.colors[index].ToVector4());
int redDistance = this.pixelBuffer[0] - red;
int greenDistance = this.pixelBuffer[1] - green;
int blueDistance = this.pixelBuffer[2] - blue;
int alphaDistance = this.pixelBuffer[3] - alpha;
int distance = (redDistance * redDistance) +
(greenDistance * greenDistance) +
(blueDistance * blueDistance) +
(alphaDistance * alphaDistance);
if (distance < leastDistance) if (distance < leastDistance)
{ {
@ -111,7 +99,7 @@ namespace ImageSharp.Quantizers
leastDistance = distance; leastDistance = distance;
// And if it's an exact match, exit the loop // And if it's an exact match, exit the loop
if (distance == 0) if (Math.Abs(distance) < .0001F)
{ {
break; break;
} }

Loading…
Cancel
Save