Browse Source

Fix ConvolutionProcessor

pull/195/head
James Jackson-South 9 years ago
parent
commit
0e229e2bc8
  1. 88
      src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs

88
src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs

@ -46,51 +46,51 @@ namespace ImageSharp.Processing.Processors
int maxX = endX - 1; int maxX = endX - 1;
using (PixelAccessor<TPixel> targetPixels = new PixelAccessor<TPixel>(source.Width, source.Height)) using (PixelAccessor<TPixel> targetPixels = new PixelAccessor<TPixel>(source.Width, source.Height))
using (PixelAccessor<TPixel> sourcePixels = source.Lock())
{ {
using (PixelAccessor<TPixel> sourcePixels = source.Lock()) sourcePixels.CopyTo(targetPixels);
{
Parallel.For( Parallel.For(
startY, startY,
endY, endY,
this.ParallelOptions, this.ParallelOptions,
y => y =>
{ {
for (int x = startX; x < endX; x++) for (int x = startX; x < endX; x++)
{ {
float red = 0; float red = 0;
float green = 0; float green = 0;
float blue = 0; float blue = 0;
// Apply each matrix multiplier to the color components for each pixel. // Apply each matrix multiplier to the color components for each pixel.
for (int fy = 0; fy < kernelLength; fy++) for (int fy = 0; fy < kernelLength; fy++)
{ {
int fyr = fy - radius; int fyr = fy - radius;
int offsetY = y + fyr; int offsetY = y + fyr;
offsetY = offsetY.Clamp(0, maxY); offsetY = offsetY.Clamp(0, maxY);
for (int fx = 0; fx < kernelLength; fx++) for (int fx = 0; fx < kernelLength; fx++)
{ {
int fxr = fx - radius; int fxr = fx - radius;
int offsetX = x + fxr; int offsetX = x + fxr;
offsetX = offsetX.Clamp(0, maxX); offsetX = offsetX.Clamp(0, maxX);
Vector4 currentColor = sourcePixels[offsetX, offsetY].ToVector4(); Vector4 currentColor = sourcePixels[offsetX, offsetY].ToVector4();
currentColor *= this.KernelXY[fy, fx]; currentColor *= this.KernelXY[fy, fx];
red += currentColor.X; red += currentColor.X;
green += currentColor.Y; green += currentColor.Y;
blue += currentColor.Z; blue += currentColor.Z;
} }
} }
TPixel packed = default(TPixel); TPixel packed = default(TPixel);
packed.PackFromVector4(new Vector4(red, green, blue, sourcePixels[x, y].ToVector4().W)); packed.PackFromVector4(new Vector4(red, green, blue, sourcePixels[x, y].ToVector4().W));
targetPixels[x, y] = packed; targetPixels[x, y] = packed;
} }
}); });
}
source.SwapPixelsBuffers(targetPixels); source.SwapPixelsBuffers(targetPixels);
} }

Loading…
Cancel
Save