Browse Source

Added missing using statement

af/octree-no-pixelmap
Sergio Pedri 6 years ago
parent
commit
d7a2dee255
  1. 99
      src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor{TPixel}.cs

99
src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor{TPixel}.cs

@ -73,73 +73,74 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects
ref Vector4 vectorSpanRef = ref MemoryMarshal.GetReference(vectorSpan);
// Rent the shared buffer only once per parallel item
IMemoryOwner<float> bins = configuration.MemoryAllocator.Allocate<float>(levels * 4);
ref float binsRef = ref bins.GetReference();
ref int intensityBinRef = ref Unsafe.As<float, int>(ref binsRef);
ref float redBinRef = ref Unsafe.Add(ref binsRef, levels);
ref float blueBinRef = ref Unsafe.Add(ref redBinRef, levels);
ref float greenBinRef = ref Unsafe.Add(ref blueBinRef, levels);
for (int y = rows.Min; y < rows.Max; y++)
using (IMemoryOwner<float> bins = configuration.MemoryAllocator.Allocate<float>(levels * 4))
{
Span<TPixel> sourceRow = source.GetPixelRowSpan(y);
Span<TPixel> targetRowSpan = targetPixels.GetRowSpan(y).Slice(startX, length);
PixelOperations<TPixel>.Instance.ToVector4(configuration, targetRowSpan, vectorSpan);
ref float binsRef = ref bins.GetReference();
ref int intensityBinRef = ref Unsafe.As<float, int>(ref binsRef);
ref float redBinRef = ref Unsafe.Add(ref binsRef, levels);
ref float blueBinRef = ref Unsafe.Add(ref redBinRef, levels);
ref float greenBinRef = ref Unsafe.Add(ref blueBinRef, levels);
for (int x = startX; x < endX; x++)
for (int y = rows.Min; y < rows.Max; y++)
{
int maxIntensity = 0;
int maxIndex = 0;
Span<TPixel> sourceRow = source.GetPixelRowSpan(y);
Span<TPixel> targetRowSpan = targetPixels.GetRowSpan(y).Slice(startX, length);
PixelOperations<TPixel>.Instance.ToVector4(configuration, targetRowSpan, vectorSpan);
// Clear the current shared buffer before processing each target pixel
bins.Memory.Span.Clear();
for (int fy = 0; fy <= radius; fy++)
for (int x = startX; x < endX; x++)
{
int fyr = fy - radius;
int offsetY = y + fyr;
offsetY = offsetY.Clamp(0, maxY);
int maxIntensity = 0;
int maxIndex = 0;
Span<TPixel> sourceOffsetRow = source.GetPixelRowSpan(offsetY);
// Clear the current shared buffer before processing each target pixel
bins.Memory.Span.Clear();
for (int fx = 0; fx <= radius; fx++)
for (int fy = 0; fy <= radius; fy++)
{
int fxr = fx - radius;
int offsetX = x + fxr;
offsetX = offsetX.Clamp(0, maxX);
int fyr = fy - radius;
int offsetY = y + fyr;
var vector = sourceOffsetRow[offsetX].ToVector4();
offsetY = offsetY.Clamp(0, maxY);
float sourceRed = vector.X;
float sourceBlue = vector.Z;
float sourceGreen = vector.Y;
Span<TPixel> sourceOffsetRow = source.GetPixelRowSpan(offsetY);
int currentIntensity = (int)MathF.Round((sourceBlue + sourceGreen + sourceRed) / 3F * (levels - 1));
for (int fx = 0; fx <= radius; fx++)
{
int fxr = fx - radius;
int offsetX = x + fxr;
offsetX = offsetX.Clamp(0, maxX);
Unsafe.Add(ref intensityBinRef, currentIntensity)++;
Unsafe.Add(ref redBinRef, currentIntensity) += sourceRed;
Unsafe.Add(ref blueBinRef, currentIntensity) += sourceBlue;
Unsafe.Add(ref greenBinRef, currentIntensity) += sourceGreen;
var vector = sourceOffsetRow[offsetX].ToVector4();
if (Unsafe.Add(ref intensityBinRef, currentIntensity) > maxIntensity)
{
maxIntensity = Unsafe.Add(ref intensityBinRef, currentIntensity);
maxIndex = currentIntensity;
float sourceRed = vector.X;
float sourceBlue = vector.Z;
float sourceGreen = vector.Y;
int currentIntensity = (int)MathF.Round((sourceBlue + sourceGreen + sourceRed) / 3F * (levels - 1));
Unsafe.Add(ref intensityBinRef, currentIntensity)++;
Unsafe.Add(ref redBinRef, currentIntensity) += sourceRed;
Unsafe.Add(ref blueBinRef, currentIntensity) += sourceBlue;
Unsafe.Add(ref greenBinRef, currentIntensity) += sourceGreen;
if (Unsafe.Add(ref intensityBinRef, currentIntensity) > maxIntensity)
{
maxIntensity = Unsafe.Add(ref intensityBinRef, currentIntensity);
maxIndex = currentIntensity;
}
}
}
float red = MathF.Abs(Unsafe.Add(ref redBinRef, maxIndex) / maxIntensity);
float blue = MathF.Abs(Unsafe.Add(ref blueBinRef, maxIndex) / maxIntensity);
float green = MathF.Abs(Unsafe.Add(ref greenBinRef, maxIndex) / maxIntensity);
float alpha = sourceRow[x].ToVector4().W;
float red = MathF.Abs(Unsafe.Add(ref redBinRef, maxIndex) / maxIntensity);
float blue = MathF.Abs(Unsafe.Add(ref blueBinRef, maxIndex) / maxIntensity);
float green = MathF.Abs(Unsafe.Add(ref greenBinRef, maxIndex) / maxIntensity);
float alpha = sourceRow[x].ToVector4().W;
Unsafe.Add(ref vectorSpanRef, x) = new Vector4(red, green, blue, alpha);
Unsafe.Add(ref vectorSpanRef, x) = new Vector4(red, green, blue, alpha);
}
}
}
PixelOperations<TPixel>.Instance.FromVector4Destructive(configuration, vectorSpan, targetRowSpan);
PixelOperations<TPixel>.Instance.FromVector4Destructive(configuration, vectorSpan, targetRowSpan);
}
}
});

Loading…
Cancel
Save