From ff5ba9334dfd9535cf92f88f8cfa768e4dc98b81 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 16 Dec 2016 00:59:01 +1100 Subject: [PATCH] Speed up palette quantizer --- .../Quantizers/Palette/PaletteQuantizer.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Quantizers/Palette/PaletteQuantizer.cs b/src/ImageSharp/Quantizers/Palette/PaletteQuantizer.cs index d8d5d0549..dbab61733 100644 --- a/src/ImageSharp/Quantizers/Palette/PaletteQuantizer.cs +++ b/src/ImageSharp/Quantizers/Palette/PaletteQuantizer.cs @@ -6,7 +6,7 @@ namespace ImageSharp.Quantizers { using System; - using System.Collections.Concurrent; + using System.Collections.Generic; /// /// Encapsulates methods to create a quantized image based upon the given palette. @@ -26,7 +26,7 @@ namespace ImageSharp.Quantizers /// /// A lookup table for colors /// - private readonly ConcurrentDictionary colorMap = new ConcurrentDictionary(); + private readonly Dictionary colorMap = new Dictionary(); /// /// List of all colors in the palette @@ -50,10 +50,9 @@ namespace ImageSharp.Quantizers for (int i = 0; i < constants.Length; i++) { - Color c = constants[i]; - c.ToBytes(this.pixelBuffer, 0, ComponentOrder.XYZW); + constants[i].ToBytes(this.pixelBuffer, 0, ComponentOrder.XYZW); TColor packed = default(TColor); - packed.PackFromBytes(this.pixelBuffer[0], this.pixelBuffer[0], this.pixelBuffer[0], this.pixelBuffer[0]); + packed.PackFromBytes(this.pixelBuffer[0], this.pixelBuffer[1], this.pixelBuffer[2], this.pixelBuffer[3]); safe[i] = packed; } @@ -76,7 +75,7 @@ namespace ImageSharp.Quantizers protected override byte QuantizePixel(TColor pixel) { byte colorIndex = 0; - string colorHash = pixel.ToString(); + int colorHash = pixel.GetHashCode(); // Check if the color is in the lookup table if (this.colorMap.ContainsKey(colorHash)) @@ -120,7 +119,7 @@ namespace ImageSharp.Quantizers } // Now I have the color, pop it into the cache for next time - this.colorMap.TryAdd(colorHash, colorIndex); + this.colorMap.Add(colorHash, colorIndex); } return colorIndex;