Browse Source

Optimize Crop

af/merge-core
James Jackson-South 9 years ago
parent
commit
9dc1009d1b
  1. 28
      src/ImageSharp/Processing/Processors/Transforms/CropProcessor.cs

28
src/ImageSharp/Processing/Processors/Transforms/CropProcessor.cs

@ -7,7 +7,7 @@ namespace ImageSharp.Processing.Processors
{ {
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using ImageSharp.Memory;
using ImageSharp.PixelFormats; using ImageSharp.PixelFormats;
/// <summary> /// <summary>
@ -44,22 +44,18 @@ namespace ImageSharp.Processing.Processors
int minX = Math.Max(this.CropRectangle.X, sourceRectangle.X); int minX = Math.Max(this.CropRectangle.X, sourceRectangle.X);
int maxX = Math.Min(this.CropRectangle.Right, sourceRectangle.Right); int maxX = Math.Min(this.CropRectangle.Right, sourceRectangle.Right);
using (PixelAccessor<TPixel> targetPixels = new PixelAccessor<TPixel>(this.CropRectangle.Width, this.CropRectangle.Height)) using (var targetPixels = new PixelAccessor<TPixel>(this.CropRectangle.Width, this.CropRectangle.Height))
{ {
using (PixelAccessor<TPixel> sourcePixels = source.Lock()) Parallel.For(
{ minY,
Parallel.For( maxY,
minY, this.ParallelOptions,
maxY, y =>
this.ParallelOptions, {
y => Span<TPixel> sourceRow = source.GetRowSpan(minX, y);
{ Span<TPixel> targetRow = targetPixels.GetRowSpan(y - minY);
for (int x = minX; x < maxX; x++) SpanHelper.Copy(sourceRow, targetRow, maxX - minX);
{ });
targetPixels[x - minX, y - minY] = sourcePixels[x, y];
}
});
}
source.SwapPixelsBuffers(targetPixels); source.SwapPixelsBuffers(targetPixels);
} }

Loading…
Cancel
Save