From 63043fdc36eb02e765263b1a869c8e4563d472e9 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 1 Apr 2016 10:27:29 +1100 Subject: [PATCH] Palette Quantizer is now threadsafe. Former-commit-id: 7e9f0265dd832ab739a0579deabae792f351efda Former-commit-id: 8d60a4d0e34f89e2ef638da8c7b1873ac77aee03 Former-commit-id: 7bc7b4acc76f558796b3cf580446d0f517bb15fa --- .../Quantizers/Palette/PaletteQuantizer.cs | 7 +++--- .../Processors/Formats/EncoderDecoderTests.cs | 23 ++++++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/ImageProcessorCore/Quantizers/Palette/PaletteQuantizer.cs b/src/ImageProcessorCore/Quantizers/Palette/PaletteQuantizer.cs index 47f77a877..db2a4c59c 100644 --- a/src/ImageProcessorCore/Quantizers/Palette/PaletteQuantizer.cs +++ b/src/ImageProcessorCore/Quantizers/Palette/PaletteQuantizer.cs @@ -6,6 +6,7 @@ namespace ImageProcessorCore.Quantizers { using System; + using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; @@ -18,7 +19,7 @@ namespace ImageProcessorCore.Quantizers /// /// A lookup table for colors /// - private readonly Dictionary colorMap = new Dictionary(); + private readonly ConcurrentDictionary colorMap = new ConcurrentDictionary(); /// /// List of all colors in the palette @@ -58,7 +59,7 @@ namespace ImageProcessorCore.Quantizers protected override byte QuantizePixel(Bgra32 pixel) { byte colorIndex = 0; - int colorHash = pixel.Bgra; + string colorHash = pixel.ToString(); // Check if the color is in the lookup table if (this.colorMap.ContainsKey(colorHash)) @@ -118,7 +119,7 @@ namespace ImageProcessorCore.Quantizers } // Now I have the color, pop it into the cache for next time - this.colorMap[colorHash] = colorIndex; + this.colorMap.TryAdd(colorHash, colorIndex); } return colorIndex; diff --git a/tests/ImageProcessorCore.Tests/Processors/Formats/EncoderDecoderTests.cs b/tests/ImageProcessorCore.Tests/Processors/Formats/EncoderDecoderTests.cs index 9fbb7af35..f5cf32a4d 100644 --- a/tests/ImageProcessorCore.Tests/Processors/Formats/EncoderDecoderTests.cs +++ b/tests/ImageProcessorCore.Tests/Processors/Formats/EncoderDecoderTests.cs @@ -57,10 +57,27 @@ using (FileStream stream = File.OpenRead(file)) { Image image = new Image(stream); - IQuantizer quantizer = new OctreeQuantizer(); + + IQuantizer quantizer = new OctreeQuantizer(); QuantizedImage quantizedImage = quantizer.Quantize(image, 256); - using (FileStream output = File.OpenWrite($"TestOutput/Quantize/{Path.GetFileName(file)}")) + using (FileStream output = File.OpenWrite($"TestOutput/Quantize/Octree-{Path.GetFileName(file)}")) + { + quantizedImage.ToImage().Save(output, image.CurrentImageFormat); + } + + quantizer = new WuQuantizer(); + quantizedImage = quantizer.Quantize(image, 256); + + using (FileStream output = File.OpenWrite($"TestOutput/Quantize/Wu-{Path.GetFileName(file)}")) + { + quantizedImage.ToImage().Save(output, image.CurrentImageFormat); + } + + quantizer = new PaletteQuantizer(); + quantizedImage = quantizer.Quantize(image, 256); + + using (FileStream output = File.OpenWrite($"TestOutput/Quantize/Palette-{Path.GetFileName(file)}")) { quantizedImage.ToImage().Save(output, image.CurrentImageFormat); } @@ -84,7 +101,7 @@ using (FileStream output = File.OpenWrite($"TestOutput/Indexed/{Path.GetFileNameWithoutExtension(file)}.png")) { - image.Quality = 255; + image.Quality = 256; image.Save(output, new PngFormat()); } }