diff --git a/src/ImageProcessorCore/ParallelImageProcessor.cs b/src/ImageProcessorCore/ParallelImageProcessor.cs index e8c09b196..c0de64800 100644 --- a/src/ImageProcessorCore/ParallelImageProcessor.cs +++ b/src/ImageProcessorCore/ParallelImageProcessor.cs @@ -35,41 +35,49 @@ namespace ImageProcessorCore /// public void Apply(ImageBase target, ImageBase source, Rectangle sourceRectangle) { - // We don't want to affect the original source pixels so we make clone here. - ImageFrame frame = source as ImageFrame; - Image temp = frame != null ? new Image(frame) : new Image((Image)source); - this.OnApply(temp, target, target.Bounds, sourceRectangle); - - this.numRowsProcessed = 0; - this.totalRows = sourceRectangle.Height; - - if (this.Parallelism > 1) + try { - int partitionCount = this.Parallelism; + // We don't want to affect the original source pixels so we make clone here. + ImageFrame frame = source as ImageFrame; + Image temp = frame != null ? new Image(frame) : new Image((Image)source); + this.OnApply(temp, target, target.Bounds, sourceRectangle); - Task[] tasks = new Task[partitionCount]; + this.numRowsProcessed = 0; + this.totalRows = sourceRectangle.Height; - for (int p = 0; p < partitionCount; p++) + if (this.Parallelism > 1) { - int current = p; - tasks[p] = Task.Run(() => + int partitionCount = this.Parallelism; + + Task[] tasks = new Task[partitionCount]; + + for (int p = 0; p < partitionCount; p++) { - int batchSize = sourceRectangle.Height / partitionCount; - int yStart = sourceRectangle.Y + (current * batchSize); - int yEnd = current == partitionCount - 1 ? sourceRectangle.Bottom : yStart + batchSize; + int current = p; + tasks[p] = Task.Run(() => + { + int batchSize = sourceRectangle.Height / partitionCount; + int yStart = sourceRectangle.Y + (current * batchSize); + int yEnd = current == partitionCount - 1 ? sourceRectangle.Bottom : yStart + batchSize; - this.Apply(target, temp, target.Bounds, sourceRectangle, yStart, yEnd); - }); + this.Apply(target, temp, target.Bounds, sourceRectangle, yStart, yEnd); + }); + } + + Task.WaitAll(tasks); + } + else + { + this.Apply(target, temp, target.Bounds, sourceRectangle, sourceRectangle.Y, sourceRectangle.Bottom); } - Task.WaitAll(tasks); + this.AfterApply(temp, target, target.Bounds, sourceRectangle); } - else + catch (Exception ex) { - this.Apply(target, temp, target.Bounds, sourceRectangle, sourceRectangle.Y, sourceRectangle.Bottom); - } - this.AfterApply(temp, target, target.Bounds, sourceRectangle); + throw new ImageProcessingException($"An error occured when processing the image using {this.GetType().Name}. See the inner exception for more detail.", ex); + } } /// diff --git a/src/ImageProcessorCore/Samplers/Crop.cs b/src/ImageProcessorCore/Samplers/Crop.cs index b86685590..5b61aa94e 100644 --- a/src/ImageProcessorCore/Samplers/Crop.cs +++ b/src/ImageProcessorCore/Samplers/Crop.cs @@ -31,6 +31,7 @@ namespace ImageProcessorCore.Samplers { target[x, y] = source[x, y]; } + this.OnRowProcessed(); } });