From 0b4e7615ed9871d899d0e3aeb7671673cef0939a Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 15 Apr 2017 13:25:27 +1000 Subject: [PATCH] Fix quantization altering the original image. --- src/ImageSharp/Quantizers/Quantizer.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/ImageSharp/Quantizers/Quantizer.cs b/src/ImageSharp/Quantizers/Quantizer.cs index bb856ccc5..492ec5f2b 100644 --- a/src/ImageSharp/Quantizers/Quantizer.cs +++ b/src/ImageSharp/Quantizers/Quantizer.cs @@ -68,7 +68,20 @@ namespace ImageSharp.Quantizers // Collect the palette. Required before the second pass runs. colorPalette = this.GetPalette(); - this.SecondPass(pixels, quantizedPixels, width, height); + + if (this.Dither) + { + // We clone the image as we don't want to alter the original. + using (Image clone = new Image(image)) + using (PixelAccessor clonedPixels = clone.Lock()) + { + this.SecondPass(clonedPixels, quantizedPixels, width, height); + } + } + else + { + this.SecondPass(pixels, quantizedPixels, width, height); + } } return new QuantizedImage(width, height, colorPalette, quantizedPixels);