Browse Source

Optimize Flip

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

30
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( Parallel.For(
0, 0,
halfHeight, halfHeight,
this.ParallelOptions, this.ParallelOptions,
y => y =>
{
for (int x = 0; x < width; x++)
{ {
int newY = height - y - 1; int newY = height - y - 1;
targetPixels[x, y] = sourcePixels[x, newY]; Span<TPixel> sourceRow = source.GetRowSpan(y);
targetPixels[x, newY] = sourcePixels[x, y]; Span<TPixel> altSourceRow = source.GetRowSpan(newY);
} Span<TPixel> targetRow = targetPixels.GetRowSpan(y);
Span<TPixel> altTargetRow = targetPixels.GetRowSpan(newY);
sourceRow.CopyTo(altTargetRow);
altSourceRow.CopyTo(targetRow);
}); });
}
source.SwapPixelsBuffers(targetPixels); source.SwapPixelsBuffers(targetPixels);
} }
@ -91,9 +91,7 @@ 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( Parallel.For(
0, 0,
@ -101,14 +99,16 @@ namespace ImageSharp.Processing.Processors
this.ParallelOptions, this.ParallelOptions,
y => 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; int newX = width - x - 1;
targetPixels[x, y] = sourcePixels[newX, y]; targetRow[x] = sourceRow[newX];
targetPixels[newX, y] = sourcePixels[x, y]; targetRow[newX] = sourceRow[x];
} }
}); });
}
source.SwapPixelsBuffers(targetPixels); source.SwapPixelsBuffers(targetPixels);
} }

Loading…
Cancel
Save