Browse Source

Fully working implementation

pull/1574/head
Simanto Rahman 8 years ago
parent
commit
d89daae13a
  1. 32
      src/ImageSharp/Processing/Processors/Binarization/AdaptiveThresholdProcessor.cs

32
src/ImageSharp/Processing/Processors/Binarization/AdaptiveThresholdProcessor.cs

@ -92,29 +92,22 @@ namespace SixLabors.ImageSharp.Processing.Processors.Binarization
// Using pooled 2d buffer for integer image table and temp memory to hold Rgb24 converted pixel data // Using pooled 2d buffer for integer image table and temp memory to hold Rgb24 converted pixel data
using (Buffer2D<ulong> intImage = configuration.MemoryAllocator.Allocate2D<ulong>(width, height)) using (Buffer2D<ulong> intImage = configuration.MemoryAllocator.Allocate2D<ulong>(width, height))
using (IMemoryOwner<Rgb24> bulkRgbBuf = configuration.MemoryAllocator.Allocate<Rgb24>(width * height)) using (IMemoryOwner<Rgb24> tmpBuffer = configuration.MemoryAllocator.Allocate<Rgb24>(width * height))
{ {
// Defines the rectangle section of the image to work on // Defines the rectangle section of the image to work on
Rectangle workingRectangle = Rectangle.FromLTRB(startX, startY, endX, endY); Rectangle workingRectangle = Rectangle.FromLTRB(startX, startY, endX, endY);
// TPixel span of the original image this.pixelOpInstance.ToRgb24(source.GetPixelSpan(), tmpBuffer.GetSpan());
Span<TPixel> pixelSpan = source.GetPixelSpan();
// RGB24 span of the converted pixel buffer
Span<Rgb24> rgbSpan = bulkRgbBuf.GetSpan();
// Bulk conversion to RGB24
this.pixelOpInstance.ToRgb24(pixelSpan, rgbSpan);
for (int x = startX; x < endX; x++) for (int x = startX; x < endX; x++)
{ {
Span<Rgb24> rgbSpan = tmpBuffer.GetSpan();
ulong sum = 0; ulong sum = 0;
for (int y = startY; y < endY; y++) for (int y = startY; y < endY; y++)
{ {
ref Rgb24 rgb = ref rgbSpan[(width * y) + x]; ref Rgb24 rgb = ref rgbSpan[(width * y) + x];
sum += (ulong)(rgb.R + rgb.G + rgb.B); sum += (ulong)(rgb.R + rgb.G + rgb.G);
if (x != 0) if (x != 0)
{ {
intImage[x - startX, y - startY] = intImage[x - startX - 1, y - startY] + sum; intImage[x - startX, y - startY] = intImage[x - startX - 1, y - startY] + sum;
@ -126,13 +119,19 @@ namespace SixLabors.ImageSharp.Processing.Processors.Binarization
} }
} }
ushort x1, x2, y1, y2; ParallelHelper.IterateRows(
workingRectangle,
configuration,
rows =>
{
Span<Rgb24> rgbSpan = tmpBuffer.GetSpan();
ushort x1, y1, x2, y2;
uint count = 0; uint count = 0;
long sum = 0;
for (int x = startX; x < endX; x++) for (int x = startX; x < endX; x++)
{ {
long sum = 0; for (int y = rows.Min; y < rows.Max; y++)
for (int y = startY; y < endY; y++)
{ {
ref Rgb24 rgb = ref rgbSpan[(width * y) + x]; ref Rgb24 rgb = ref rgbSpan[(width * y) + x];
@ -146,14 +145,15 @@ namespace SixLabors.ImageSharp.Processing.Processors.Binarization
if ((rgb.R + rgb.G + rgb.B) * count <= sum * this.ThresholdLimit) if ((rgb.R + rgb.G + rgb.B) * count <= sum * this.ThresholdLimit)
{ {
pixelSpan[(width * y) + x] = this.Lower; source[x, y] = this.Lower;
} }
else else
{ {
pixelSpan[(width * y) + x] = this.Upper; source[x, y] = this.Upper;
} }
} }
} }
});
} }
} }
} }

Loading…
Cancel
Save