Browse Source

Add missing exception handling. Fix #293

Former-commit-id: 3592ce3d67c70c1bd34ded26c48a0a3472ffd0d9
Former-commit-id: acb1420d8740919a329bba50d93c4f996e66f97d
Former-commit-id: 56dca60cbada6d0929f029b7a6a43570272bc5eb
pull/1/head
James Jackson-South 11 years ago
parent
commit
9bfcbe8f92
  1. 56
      src/ImageProcessorCore/ParallelImageProcessor.cs
  2. 1
      src/ImageProcessorCore/Samplers/Crop.cs

56
src/ImageProcessorCore/ParallelImageProcessor.cs

@ -35,41 +35,49 @@ namespace ImageProcessorCore
/// <inheritdoc/> /// <inheritdoc/>
public void Apply(ImageBase target, ImageBase source, Rectangle sourceRectangle) public void Apply(ImageBase target, ImageBase source, Rectangle sourceRectangle)
{ {
// We don't want to affect the original source pixels so we make clone here. try
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)
{ {
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; int partitionCount = this.Parallelism;
tasks[p] = Task.Run(() =>
Task[] tasks = new Task[partitionCount];
for (int p = 0; p < partitionCount; p++)
{ {
int batchSize = sourceRectangle.Height / partitionCount; int current = p;
int yStart = sourceRectangle.Y + (current * batchSize); tasks[p] = Task.Run(() =>
int yEnd = current == partitionCount - 1 ? sourceRectangle.Bottom : yStart + batchSize; {
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);
}
} }
/// <inheritdoc/> /// <inheritdoc/>

1
src/ImageProcessorCore/Samplers/Crop.cs

@ -31,6 +31,7 @@ namespace ImageProcessorCore.Samplers
{ {
target[x, y] = source[x, y]; target[x, y] = source[x, y];
} }
this.OnRowProcessed(); this.OnRowProcessed();
} }
}); });

Loading…
Cancel
Save