Browse Source

Fix Oil Painting processor

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

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

@ -70,88 +70,88 @@ namespace ImageSharp.Processing.Processors
} }
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(
minY, minY,
maxY, maxY,
this.ParallelOptions, this.ParallelOptions,
y => y =>
{
for (int x = startX; x < endX; x++)
{ {
for (int x = startX; x < endX; x++) int maxIntensity = 0;
int maxIndex = 0;
int[] intensityBin = new int[levels];
float[] redBin = new float[levels];
float[] blueBin = new float[levels];
float[] greenBin = new float[levels];
for (int fy = 0; fy <= radius; fy++)
{ {
int maxIntensity = 0; int fyr = fy - radius;
int maxIndex = 0; int offsetY = y + fyr;
int[] intensityBin = new int[levels]; // Skip the current row
float[] redBin = new float[levels]; if (offsetY < minY)
float[] blueBin = new float[levels]; {
float[] greenBin = new float[levels]; continue;
}
for (int fy = 0; fy <= radius; fy++) // Outwith the current bounds so break.
if (offsetY >= maxY)
{ {
int fyr = fy - radius; break;
int offsetY = y + fyr; }
// Skip the current row for (int fx = 0; fx <= radius; fx++)
if (offsetY < minY) {
{ int fxr = fx - radius;
continue; int offsetX = x + fxr;
}
// Outwith the current bounds so break. // Skip the column
if (offsetY >= maxY) if (offsetX < 0)
{ {
break; continue;
} }
for (int fx = 0; fx <= radius; fx++) if (offsetX < maxX)
{ {
int fxr = fx - radius; // ReSharper disable once AccessToDisposedClosure
int offsetX = x + fxr; Vector4 color = sourcePixels[offsetX, offsetY].ToVector4();
// Skip the column float sourceRed = color.X;
if (offsetX < 0) float sourceBlue = color.Z;
{ float sourceGreen = color.Y;
continue;
}
if (offsetX < maxX)
{
// ReSharper disable once AccessToDisposedClosure
Vector4 color = sourcePixels[offsetX, offsetY].ToVector4();
float sourceRed = color.X; int currentIntensity = (int)Math.Round((sourceBlue + sourceGreen + sourceRed) / 3.0 * (levels - 1));
float sourceBlue = color.Z;
float sourceGreen = color.Y;
int currentIntensity = (int)Math.Round((sourceBlue + sourceGreen + sourceRed) / 3.0 * (levels - 1)); intensityBin[currentIntensity] += 1;
blueBin[currentIntensity] += sourceBlue;
greenBin[currentIntensity] += sourceGreen;
redBin[currentIntensity] += sourceRed;
intensityBin[currentIntensity] += 1; if (intensityBin[currentIntensity] > maxIntensity)
blueBin[currentIntensity] += sourceBlue; {
greenBin[currentIntensity] += sourceGreen; maxIntensity = intensityBin[currentIntensity];
redBin[currentIntensity] += sourceRed; maxIndex = currentIntensity;
if (intensityBin[currentIntensity] > maxIntensity)
{
maxIntensity = intensityBin[currentIntensity];
maxIndex = currentIntensity;
}
} }
} }
}
float red = MathF.Abs(redBin[maxIndex] / maxIntensity); float red = MathF.Abs(redBin[maxIndex] / maxIntensity);
float green = MathF.Abs(greenBin[maxIndex] / maxIntensity); float green = MathF.Abs(greenBin[maxIndex] / maxIntensity);
float blue = MathF.Abs(blueBin[maxIndex] / maxIntensity); float blue = MathF.Abs(blueBin[maxIndex] / maxIntensity);
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