Browse Source

make it parallel again!

af/merge-core
Anton Firszov 9 years ago
parent
commit
3502db1f48
  1. 104
      src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs

104
src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs

@ -110,64 +110,76 @@ namespace ImageSharp.Processing.Processors
{
firstPassPixels.Clear();
using (PinnedBuffer<Vector4> tempRowBuffer = new PinnedBuffer<Vector4>(sourcePixels.Width))
{
for (int y = 0; y < sourceRectangle.Bottom; y++)
{
BufferSpan<TColor> sourceRow = sourcePixels.GetRowSpan(y);
Parallel.For(
0,
sourceRectangle.Bottom,
this.ParallelOptions,
y =>
{
// TODO: Without Parallel.For() this buffer object could be reused:
using (PinnedBuffer<Vector4> tempRowBuffer = new PinnedBuffer<Vector4>(sourcePixels.Width))
{
BufferSpan<TColor> sourceRow = sourcePixels.GetRowSpan(y);
BulkPixelOperations<TColor>.Instance.ToVector4(
sourceRow,
tempRowBuffer,
sourceRow.Length);
if (this.Compand)
{
for (int x = minX; x < maxX; x++)
{
WeightsWindow window = this.HorizontalWeights.Weights[x - startX];
firstPassPixels[x, y] = window.ComputeExpandedWeightedRowSum(tempRowBuffer);
}
}
else
{
for (int x = minX; x < maxX; x++)
{
WeightsWindow window = this.HorizontalWeights.Weights[x - startX];
firstPassPixels[x, y] = window.ComputeWeightedRowSum(tempRowBuffer);
}
}
}
});
BulkPixelOperations<TColor>.Instance.ToVector4(sourceRow, tempRowBuffer, sourceRow.Length);
// Now process the rows.
Parallel.For(
minY,
maxY,
this.ParallelOptions,
y =>
{
// Ensure offsets are normalised for cropping and padding.
WeightsWindow window = this.VerticalWeights.Weights[y - startY];
if (this.Compand)
{
for (int x = minX; x < maxX; x++)
for (int x = 0; x < width; x++)
{
WeightsWindow window = this.HorizontalWeights.Weights[x - startX];
firstPassPixels[x, y] = window.ComputeExpandedWeightedRowSum(tempRowBuffer);
// Destination color components
Vector4 destination = window.ComputeWeightedColumnSum(firstPassPixels, x);
destination = destination.Compress();
TColor d = default(TColor);
d.PackFromVector4(destination);
targetPixels[x, y] = d;
}
}
else
{
for (int x = minX; x < maxX; x++)
for (int x = 0; x < width; x++)
{
WeightsWindow window = this.HorizontalWeights.Weights[x - startX];
firstPassPixels[x, y] = window.ComputeWeightedRowSum(tempRowBuffer);
}
}
}
}
// Now process the rows.
for (int y = minY; y < maxY; y++)
{
// Ensure offsets are normalised for cropping and padding.
WeightsWindow window = this.VerticalWeights.Weights[y - startY];
if (this.Compand)
{
for (int x = 0; x < width; x++)
{
// Destination color components
Vector4 destination = window.ComputeWeightedColumnSum(firstPassPixels, x);
destination = destination.Compress();
TColor d = default(TColor);
d.PackFromVector4(destination);
targetPixels[x, y] = d;
}
}
else
{
for (int x = 0; x < width; x++)
{
// Destination color components
Vector4 destination = window.ComputeWeightedColumnSum(firstPassPixels, x);
// Destination color components
Vector4 destination = window.ComputeWeightedColumnSum(firstPassPixels, x);
TColor d = default(TColor);
d.PackFromVector4(destination);
targetPixels[x, y] = d;
TColor d = default(TColor);
d.PackFromVector4(destination);
targetPixels[x, y] = d;
}
}
}
}
});
}
source.SwapPixelsBuffers(targetPixels);

Loading…
Cancel
Save