From c8aeebeb1f9a3897212416c6d064c42493281990 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 16 Jul 2016 12:46:36 +1000 Subject: [PATCH] Add parallel options Former-commit-id: a81f9393544d2c2fac2a4ebe247dc83cc27a3934 Former-commit-id: 098166601a68b3b8d34f5e8206aea51ed02f82aa Former-commit-id: c5b423b967d17e571d9bc6b1918f7414eddde44e --- src/ImageProcessorCore/Bootstrapper.cs | 6 ++++++ src/ImageProcessorCore/Image/IImageProcessor.cs | 7 +++++++ src/ImageProcessorCore/ImageProcessor.cs | 4 ++++ .../Samplers/Processors/ResizeProcessor.cs | 3 +++ 4 files changed, 20 insertions(+) diff --git a/src/ImageProcessorCore/Bootstrapper.cs b/src/ImageProcessorCore/Bootstrapper.cs index 39e97e1100..0f47526b3b 100644 --- a/src/ImageProcessorCore/Bootstrapper.cs +++ b/src/ImageProcessorCore/Bootstrapper.cs @@ -10,6 +10,7 @@ namespace ImageProcessorCore using System.Collections.ObjectModel; using ImageProcessorCore.Formats; + using System.Threading.Tasks; /// /// Provides initialization code which allows extending the library. @@ -58,6 +59,11 @@ namespace ImageProcessorCore /// public IReadOnlyCollection ImageFormats => new ReadOnlyCollection(this.imageFormats); + /// + /// Gets or sets the global parallel options for processing tasks in parallel. + /// + public ParallelOptions ParallelOptions { get; set; } = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }; + /// /// Adds a new to the collection of supported image formats. /// diff --git a/src/ImageProcessorCore/Image/IImageProcessor.cs b/src/ImageProcessorCore/Image/IImageProcessor.cs index 4241b88c75..2c3dd50204 100644 --- a/src/ImageProcessorCore/Image/IImageProcessor.cs +++ b/src/ImageProcessorCore/Image/IImageProcessor.cs @@ -3,6 +3,8 @@ // Licensed under the Apache License, Version 2.0. // +using System.Threading.Tasks; + namespace ImageProcessorCore.Processors { /// @@ -26,6 +28,11 @@ namespace ImageProcessorCore.Processors /// event ProgressEventHandler OnProgress; + /// + /// Gets or sets the global parallel options for processing tasks in parallel. + /// + ParallelOptions ParallelOptions { get; set; } + /// /// Applies the process to the specified portion of the specified . /// diff --git a/src/ImageProcessorCore/ImageProcessor.cs b/src/ImageProcessorCore/ImageProcessor.cs index c7c0f4e1b7..211bbd5038 100644 --- a/src/ImageProcessorCore/ImageProcessor.cs +++ b/src/ImageProcessorCore/ImageProcessor.cs @@ -7,6 +7,7 @@ namespace ImageProcessorCore.Processors { using System; using System.Threading; + using System.Threading.Tasks; /// /// Allows the application of processors to images. @@ -26,6 +27,9 @@ namespace ImageProcessorCore.Processors /// private int totalRows; + /// + public virtual ParallelOptions ParallelOptions { get; set; } = Bootstrapper.Instance.ParallelOptions; + /// public void Apply(ImageBase target, ImageBase source, Rectangle sourceRectangle) where T : IPackedVector, new() diff --git a/src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs b/src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs index a01fd92402..3f78ed0bef 100644 --- a/src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs +++ b/src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs @@ -84,6 +84,7 @@ namespace ImageProcessorCore.Processors Parallel.For( startY, endY, + this.ParallelOptions, y => { if (targetY <= y && y < targetBottom) @@ -122,6 +123,7 @@ namespace ImageProcessorCore.Processors Parallel.For( 0, sourceHeight, + this.ParallelOptions, y => { for (int x = startX; x < endX; x++) @@ -160,6 +162,7 @@ namespace ImageProcessorCore.Processors Parallel.For( startY, endY, + this.ParallelOptions, y => { if (y >= 0 && y < height)