Browse Source

Optimize Pixellate

pull/221/head
James Jackson-South 9 years ago
parent
commit
ed019c0781
  1. 62
      src/ImageSharp/Processing/Processors/Effects/PixelateProcessor.cs

62
src/ImageSharp/Processing/Processors/Effects/PixelateProcessor.cs

@ -66,47 +66,45 @@ namespace ImageSharp.Processing.Processors
// Get the range on the y-plane to choose from. // Get the range on the y-plane to choose from.
IEnumerable<int> range = EnumerableExtensions.SteppedRange(minY, i => i < maxY, size); IEnumerable<int> range = EnumerableExtensions.SteppedRange(minY, i => i < maxY, size);
using (PixelAccessor<TPixel> sourcePixels = source.Lock()) Parallel.ForEach(
{ range,
Parallel.ForEach( this.ParallelOptions,
range, y =>
this.ParallelOptions, {
y => int offsetY = y - startY;
int offsetPy = offset;
// Make sure that the offset is within the boundary of the image.
while (offsetY + offsetPy >= maxY)
{ {
int offsetY = y - startY; offsetPy--;
int offsetPy = offset; }
for (int x = minX; x < maxX; x += size) Span<TPixel> row = source.GetRowSpan(offsetY + offsetPy);
{
int offsetX = x - startX;
int offsetPx = offset;
// Make sure that the offset is within the boundary of the image. for (int x = minX; x < maxX; x += size)
while (offsetY + offsetPy >= maxY) {
{ int offsetX = x - startX;
offsetPy--; int offsetPx = offset;
}
while (x + offsetPx >= maxX) while (x + offsetPx >= maxX)
{ {
offsetPx--; offsetPx--;
} }
// Get the pixel color in the centre of the soon to be pixelated area. // Get the pixel color in the centre of the soon to be pixelated area.
// ReSharper disable AccessToDisposedClosure TPixel pixel = row[offsetX + offsetPx];
TPixel pixel = sourcePixels[offsetX + offsetPx, offsetY + offsetPy];
// For each pixel in the pixelate size, set it to the centre color. // For each pixel in the pixelate size, set it to the centre color.
for (int l = offsetY; l < offsetY + size && l < maxY; l++) for (int l = offsetY; l < offsetY + size && l < maxY; l++)
{
for (int k = offsetX; k < offsetX + size && k < maxX; k++)
{ {
for (int k = offsetX; k < offsetX + size && k < maxX; k++) source[k, l] = pixel;
{
sourcePixels[k, l] = pixel;
}
} }
} }
}); }
} });
} }
} }
} }
Loading…
Cancel
Save