Browse Source

Add better exception handling.

Former-commit-id: f6ba09bdf9b16351b639490de55c5a0402acd7cc
Former-commit-id: 676fecf193f635d19f74ca3de41b8ced7e667352
Former-commit-id: 00484e51faae841544bdc27a2f59d227c664012c
af/merge-core
James Jackson-South 10 years ago
parent
commit
b5d7d12717
  1. 44
      src/ImageProcessor/Common/Exceptions/ImageProcessingException.cs
  2. 72
      src/ImageProcessor/ParallelImageProcessor.cs

44
src/ImageProcessor/Common/Exceptions/ImageProcessingException.cs

@ -0,0 +1,44 @@
// <copyright file="ImageProcessingException.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageProcessor
{
using System;
/// <summary>
/// The exception that is thrown when an error occurs when applying a process to an image.
/// </summary>
public class ImageProcessingException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="ImageProcessingException"/> class.
/// </summary>
public ImageProcessingException()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ImageProcessingException"/> class with the name of the
/// parameter that causes this exception.
/// </summary>
/// <param name="errorMessage">The error message that explains the reason for this exception.</param>
public ImageProcessingException(string errorMessage)
: base(errorMessage)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ImageProcessingException"/> class with a specified
/// error message and the exception that is the cause of this exception.
/// </summary>
/// <param name="errorMessage">The error message that explains the reason for this exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic)
/// if no inner exception is specified.</param>
public ImageProcessingException(string errorMessage, Exception innerException)
: base(errorMessage, innerException)
{
}
}
}

72
src/ImageProcessor/ParallelImageProcessor.cs

@ -75,50 +75,58 @@ namespace ImageProcessor
/// <inheritdoc/>
public void Apply(ImageBase target, ImageBase source, int width, int height, Rectangle targetRectangle = default(Rectangle), Rectangle sourceRectangle = default(Rectangle))
{
float[] pixels = new float[width * height * 4];
target.SetPixels(width, height, pixels);
if (sourceRectangle == Rectangle.Empty)
try
{
sourceRectangle = source.Bounds;
}
// 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);
float[] pixels = new float[width * height * 4];
target.SetPixels(width, height, pixels);
targetRectangle = target.Bounds;
this.numRowsProcessed = 0;
this.totalRows = targetRectangle.Bottom;
if (sourceRectangle == Rectangle.Empty)
{
sourceRectangle = source.Bounds;
}
if (this.Parallelism > 1)
{
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];
targetRectangle = target.Bounds;
this.numRowsProcessed = 0;
this.totalRows = targetRectangle.Bottom;
for (int p = 0; p < partitionCount; p++)
if (this.Parallelism > 1)
{
int current = p;
tasks[p] = Task.Run(() =>
{
int batchSize = targetRectangle.Bottom / partitionCount;
int yStart = current * batchSize;
int yEnd = current == partitionCount - 1 ? targetRectangle.Bottom : yStart + batchSize;
int partitionCount = this.Parallelism;
this.Apply(target, temp, targetRectangle, sourceRectangle, yStart, yEnd);
});
Task[] tasks = new Task[partitionCount];
for (int p = 0; p < partitionCount; p++)
{
int current = p;
tasks[p] = Task.Run(() =>
{
int batchSize = targetRectangle.Bottom / partitionCount;
int yStart = current * batchSize;
int yEnd = current == partitionCount - 1 ? targetRectangle.Bottom : yStart + batchSize;
this.Apply(target, temp, targetRectangle, sourceRectangle, yStart, yEnd);
});
}
Task.WaitAll(tasks);
}
else
{
this.Apply(target, temp, targetRectangle, sourceRectangle, targetRectangle.Y, targetRectangle.Bottom);
}
Task.WaitAll(tasks);
this.AfterApply(temp, target, target.Bounds, sourceRectangle);
}
else
catch (Exception ex)
{
this.Apply(target, temp, targetRectangle, sourceRectangle, targetRectangle.Y, targetRectangle.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);
}
}
/// <summary>

Loading…
Cancel
Save