Browse Source

Palette Quantizer is now threadsafe.

Former-commit-id: 7e9f0265dd832ab739a0579deabae792f351efda
Former-commit-id: 8d60a4d0e34f89e2ef638da8c7b1873ac77aee03
Former-commit-id: 7bc7b4acc76f558796b3cf580446d0f517bb15fa
af/merge-core
James Jackson-South 10 years ago
parent
commit
63043fdc36
  1. 7
      src/ImageProcessorCore/Quantizers/Palette/PaletteQuantizer.cs
  2. 23
      tests/ImageProcessorCore.Tests/Processors/Formats/EncoderDecoderTests.cs

7
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
/// <summary>
/// A lookup table for colors
/// </summary>
private readonly Dictionary<int, byte> colorMap = new Dictionary<int, byte>();
private readonly ConcurrentDictionary<string, byte> colorMap = new ConcurrentDictionary<string, byte>();
/// <summary>
/// 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;

23
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());
}
}

Loading…
Cancel
Save