Browse Source

Optimize Flip

pull/221/head
James Jackson-South 9 years ago
parent
commit
18f4128b7e
  1. 66
      src/ImageSharp/Processing/Processors/Transforms/FlipProcessor.cs

66
src/ImageSharp/Processing/Processors/Transforms/FlipProcessor.cs

@ -8,6 +8,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>
@ -57,24 +58,23 @@ namespace ImageSharp.Processing.Processors
int height = source.Height; int height = source.Height;
int halfHeight = (int)Math.Ceiling(source.Height * .5F); int halfHeight = (int)Math.Ceiling(source.Height * .5F);
using (PixelAccessor<TPixel> targetPixels = new PixelAccessor<TPixel>(width, height)) using (var targetPixels = new PixelAccessor<TPixel>(width, height))
{ {
using (PixelAccessor<TPixel> sourcePixels = source.Lock()) Parallel.For(
{ 0,
Parallel.For( halfHeight,
0, this.ParallelOptions,
halfHeight, y =>
this.ParallelOptions, {
y => int newY = height - y - 1;
{ Span<TPixel> sourceRow = source.GetRowSpan(y);
for (int x = 0; x < width; x++) Span<TPixel> altSourceRow = source.GetRowSpan(newY);
{ Span<TPixel> targetRow = targetPixels.GetRowSpan(y);
int newY = height - y - 1; Span<TPixel> altTargetRow = targetPixels.GetRowSpan(newY);
targetPixels[x, y] = sourcePixels[x, newY];
targetPixels[x, newY] = sourcePixels[x, y]; sourceRow.CopyTo(altTargetRow);
} altSourceRow.CopyTo(targetRow);
}); });
}
source.SwapPixelsBuffers(targetPixels); source.SwapPixelsBuffers(targetPixels);
} }
@ -91,24 +91,24 @@ namespace ImageSharp.Processing.Processors
int height = source.Height; int height = source.Height;
int halfWidth = (int)Math.Ceiling(width * .5F); int halfWidth = (int)Math.Ceiling(width * .5F);
using (PixelAccessor<TPixel> targetPixels = new PixelAccessor<TPixel>(width, height)) using (var targetPixels = new PixelAccessor<TPixel>(width, height))
{ {
using (PixelAccessor<TPixel> sourcePixels = source.Lock()) Parallel.For(
{ 0,
Parallel.For( height,
0, this.ParallelOptions,
height, y =>
this.ParallelOptions, {
y => Span<TPixel> sourceRow = source.GetRowSpan(y);
Span<TPixel> targetRow = targetPixels.GetRowSpan(y);
for (int x = 0; x < halfWidth; x++)
{ {
for (int x = 0; x < halfWidth; x++) int newX = width - x - 1;
{ targetRow[x] = sourceRow[newX];
int newX = width - x - 1; targetRow[newX] = sourceRow[x];
targetPixels[x, y] = sourcePixels[newX, y]; }
targetPixels[newX, y] = sourcePixels[x, y]; });
}
});
}
source.SwapPixelsBuffers(targetPixels); source.SwapPixelsBuffers(targetPixels);
} }

Loading…
Cancel
Save