From 5df852a41df689dc7782d4ee374e1e2b5e537da4 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 30 Dec 2016 21:32:07 +1100 Subject: [PATCH] Use correct parallel options --- src/ImageSharp/Image/PixelAccessor{TColor}.cs | 8 ++++++ src/ImageSharp/Quantizers/Quantize.cs | 22 +++++++++++++-- src/ImageSharp/Quantizers/QuantizedImage.cs | 28 ------------------- src/ImageSharp/Quantizers/Wu/WuQuantizer.cs | 2 +- 4 files changed, 28 insertions(+), 32 deletions(-) diff --git a/src/ImageSharp/Image/PixelAccessor{TColor}.cs b/src/ImageSharp/Image/PixelAccessor{TColor}.cs index 991be5aec..dc8c33dbe 100644 --- a/src/ImageSharp/Image/PixelAccessor{TColor}.cs +++ b/src/ImageSharp/Image/PixelAccessor{TColor}.cs @@ -9,6 +9,7 @@ namespace ImageSharp using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; + using System.Threading.Tasks; /// /// Provides per-pixel access to generic pixels. @@ -60,6 +61,7 @@ namespace ImageSharp this.pixelsBase = (byte*)this.dataPointer.ToPointer(); this.PixelSize = Unsafe.SizeOf(); this.RowStride = this.Width * this.PixelSize; + this.ParallelOptions = image.Bootstrapper.ParallelOptions; } /// @@ -86,6 +88,7 @@ namespace ImageSharp this.pixelsBase = (byte*)this.dataPointer.ToPointer(); this.PixelSize = Unsafe.SizeOf(); this.RowStride = this.Width * this.PixelSize; + this.ParallelOptions = Bootstrapper.Default.ParallelOptions; } /// @@ -121,6 +124,11 @@ namespace ImageSharp /// public int Height { get; } + /// + /// Gets the global parallel options for processing tasks in parallel. + /// + public ParallelOptions ParallelOptions { get; } + /// /// Gets or sets the pixel at the specified position. /// diff --git a/src/ImageSharp/Quantizers/Quantize.cs b/src/ImageSharp/Quantizers/Quantize.cs index f060f18b9..b34398448 100644 --- a/src/ImageSharp/Quantizers/Quantize.cs +++ b/src/ImageSharp/Quantizers/Quantize.cs @@ -6,6 +6,7 @@ namespace ImageSharp { using System; + using System.Threading.Tasks; using ImageSharp.Quantizers; @@ -55,9 +56,24 @@ namespace ImageSharp public static Image Quantize(this Image source, IQuantizer quantizer, int maxColors) where TColor : struct, IPackedPixel, IEquatable { - QuantizedImage quantizedImage = quantizer.Quantize(source, maxColors); - source.SetPixels(source.Width, source.Height, quantizedImage.ToImage().Pixels); + QuantizedImage quantized = quantizer.Quantize(source, maxColors); + + int pixelCount = quantized.Pixels.Length; + int palleteCount = quantized.Palette.Length - 1; + TColor[] pixels = new TColor[pixelCount]; + + Parallel.For( + 0, + pixelCount, + source.Bootstrapper.ParallelOptions, + i => + { + TColor color = quantized.Palette[Math.Min(palleteCount, quantized.Pixels[i])]; + pixels[i] = color; + }); + + source.SetPixels(source.Width, source.Height, pixels); return source; } } -} +} \ No newline at end of file diff --git a/src/ImageSharp/Quantizers/QuantizedImage.cs b/src/ImageSharp/Quantizers/QuantizedImage.cs index 4f2c4b149..cc740f9be 100644 --- a/src/ImageSharp/Quantizers/QuantizedImage.cs +++ b/src/ImageSharp/Quantizers/QuantizedImage.cs @@ -59,33 +59,5 @@ namespace ImageSharp.Quantizers /// Gets the pixels of this . /// public byte[] Pixels { get; } - - /// - /// Converts this quantized image to a normal image. - /// - /// - /// The - /// - public Image ToImage() - { - Image image = new Image(this.Width, this.Height); - - int pixelCount = this.Pixels.Length; - int palleteCount = this.Palette.Length - 1; - TColor[] pixels = new TColor[pixelCount]; - - Parallel.For( - 0, - pixelCount, - image.Bootstrapper.ParallelOptions, - i => - { - TColor color = this.Palette[Math.Min(palleteCount, this.Pixels[i])]; - pixels[i] = color; - }); - - image.SetPixels(this.Width, this.Height, pixels); - return image; - } } } \ No newline at end of file diff --git a/src/ImageSharp/Quantizers/Wu/WuQuantizer.cs b/src/ImageSharp/Quantizers/Wu/WuQuantizer.cs index 3035b1098..55743f81e 100644 --- a/src/ImageSharp/Quantizers/Wu/WuQuantizer.cs +++ b/src/ImageSharp/Quantizers/Wu/WuQuantizer.cs @@ -773,7 +773,7 @@ namespace ImageSharp.Quantizers Parallel.For( 0, height, - Bootstrapper.Default.ParallelOptions, + imagePixels.ParallelOptions, y => { byte[] rgba = ArrayPool.Shared.Rent(4);