Browse Source

better FlipProcessor implementation

af/merge-core
Anton Firszov 8 years ago
parent
commit
c267876a0e
  1. 2
      src/ImageSharp/Common/ParallelUtils/ParallelExecutionSettings.cs
  2. 67
      src/ImageSharp/Processing/Processors/Transforms/FlipProcessor.cs

2
src/ImageSharp/Common/ParallelUtils/ParallelExecutionSettings.cs

@ -15,7 +15,7 @@ namespace SixLabors.ImageSharp.ParallelUtils
/// <summary> /// <summary>
/// Default value for <see cref="MinimumPixelsProcessedPerTask"/>. /// Default value for <see cref="MinimumPixelsProcessedPerTask"/>.
/// </summary> /// </summary>
public const int DefaultMinimumPixelsProcessedPerTask = 2048; public const int DefaultMinimumPixelsProcessedPerTask = 4096;
public ParallelExecutionSettings( public ParallelExecutionSettings(
int maxDegreeOfParallelism, int maxDegreeOfParallelism,

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

@ -2,9 +2,11 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.Buffers;
using System.Threading.Tasks; using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.ParallelUtils;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory; using SixLabors.Memory;
using SixLabors.Primitives; using SixLabors.Primitives;
@ -55,30 +57,20 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
private void FlipX(ImageFrame<TPixel> source, Configuration configuration) private void FlipX(ImageFrame<TPixel> source, Configuration configuration)
{ {
int height = source.Height; int height = source.Height;
int halfHeight = (int)Math.Ceiling(source.Height * .5F);
using (IMemoryOwner<TPixel> tempBuffer = configuration.MemoryAllocator.Allocate<TPixel>(source.Width))
using (Buffer2D<TPixel> targetPixels = configuration.MemoryAllocator.Allocate2D<TPixel>(source.Size()))
{ {
Span<TPixel> temp = tempBuffer.Memory.Span;
for (int yTop = 0; yTop < height / 2; yTop++)
ParallelFor.WithConfiguration( {
0, int yBottom = height - yTop - 1;
halfHeight, Span<TPixel> topRow = source.GetPixelRowSpan(yBottom);
configuration, Span<TPixel> bottomRow = source.GetPixelRowSpan(yTop);
y => topRow.CopyTo(temp);
{ bottomRow.CopyTo(topRow);
int newY = height - y - 1; temp.CopyTo(bottomRow);
Span<TPixel> sourceRow = source.GetPixelRowSpan(y); }
Span<TPixel> altSourceRow = source.GetPixelRowSpan(newY);
Span<TPixel> targetRow = targetPixels.GetRowSpan(y);
Span<TPixel> altTargetRow = targetPixels.GetRowSpan(newY);
sourceRow.CopyTo(altTargetRow);
altSourceRow.CopyTo(targetRow);
});
Buffer2D<TPixel>.SwapOrCopyContent(source.PixelBuffer, targetPixels);
} }
} }
@ -89,31 +81,16 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
/// <param name="configuration">The configuration.</param> /// <param name="configuration">The configuration.</param>
private void FlipY(ImageFrame<TPixel> source, Configuration configuration) private void FlipY(ImageFrame<TPixel> source, Configuration configuration)
{ {
int width = source.Width; ParallelHelper.IterateRows(
int height = source.Height; source.Bounds(),
int halfWidth = (int)Math.Ceiling(width * .5F); configuration,
rows =>
using (Buffer2D<TPixel> targetPixels = configuration.MemoryAllocator.Allocate2D<TPixel>(source.Size())) {
{ for (int y = rows.Min; y < rows.Max; y++)
ParallelFor.WithConfiguration(
0,
height,
configuration,
y =>
{ {
Span<TPixel> sourceRow = source.GetPixelRowSpan(y); source.GetPixelRowSpan(y).Reverse();
Span<TPixel> targetRow = targetPixels.GetRowSpan(y); }
});
for (int x = 0; x < halfWidth; x++)
{
int newX = width - x - 1;
targetRow[x] = sourceRow[newX];
targetRow[newX] = sourceRow[x];
}
});
Buffer2D<TPixel>.SwapOrCopyContent(source.PixelBuffer, targetPixels);
}
} }
} }
} }
Loading…
Cancel
Save