Browse Source

Optimize OilPaint

af/merge-core
James Jackson-South 9 years ago
parent
commit
38f5340165
  1. 27
      src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor.cs

27
src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor.cs

@ -8,7 +8,7 @@ namespace ImageSharp.Processing.Processors
using System;
using System.Numerics;
using System.Threading.Tasks;
using ImageSharp.Memory;
using ImageSharp.PixelFormats;
/// <summary>
@ -69,10 +69,9 @@ namespace ImageSharp.Processing.Processors
startX = 0;
}
using (PixelAccessor<TPixel> targetPixels = new PixelAccessor<TPixel>(source.Width, source.Height))
using (PixelAccessor<TPixel> sourcePixels = source.Lock())
using (var targetPixels = new PixelAccessor<TPixel>(source.Width, source.Height))
{
sourcePixels.CopyTo(targetPixels);
source.CopyTo(targetPixels);
Parallel.For(
minY,
@ -80,6 +79,9 @@ namespace ImageSharp.Processing.Processors
this.ParallelOptions,
y =>
{
Span<TPixel> sourceRow = source.GetRowSpan(y);
Span<TPixel> targetRow = targetPixels.GetRowSpan(y);
for (int x = startX; x < endX; x++)
{
int maxIntensity = 0;
@ -107,6 +109,8 @@ namespace ImageSharp.Processing.Processors
break;
}
Span<TPixel> sourceOffsetRow = source.GetRowSpan(offsetY);
for (int fx = 0; fx <= radius; fx++)
{
int fxr = fx - radius;
@ -121,13 +125,13 @@ namespace ImageSharp.Processing.Processors
if (offsetX < maxX)
{
// ReSharper disable once AccessToDisposedClosure
Vector4 color = sourcePixels[offsetX, offsetY].ToVector4();
var vector = sourceOffsetRow[offsetX].ToVector4();
float sourceRed = color.X;
float sourceBlue = color.Z;
float sourceGreen = color.Y;
float sourceRed = vector.X;
float sourceBlue = vector.Z;
float sourceGreen = vector.Y;
int currentIntensity = (int)Math.Round((sourceBlue + sourceGreen + sourceRed) / 3.0 * (levels - 1));
int currentIntensity = (int)MathF.Round((sourceBlue + sourceGreen + sourceRed) / 3F * (levels - 1));
intensityBin[currentIntensity] += 1;
blueBin[currentIntensity] += sourceBlue;
@ -146,9 +150,8 @@ namespace ImageSharp.Processing.Processors
float green = MathF.Abs(greenBin[maxIndex] / maxIntensity);
float blue = MathF.Abs(blueBin[maxIndex] / maxIntensity);
TPixel packed = default(TPixel);
packed.PackFromVector4(new Vector4(red, green, blue, sourcePixels[x, y].ToVector4().W));
targetPixels[x, y] = packed;
ref TPixel pixel = ref targetRow[x];
pixel.PackFromVector4(new Vector4(red, green, blue, sourceRow[x].ToVector4().W));
}
}
});

Loading…
Cancel
Save