Browse Source

using Parallel.For

pull/673/head
popow 8 years ago
parent
commit
f04ac406e3
  1. 18
      src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationProcessor.cs

18
src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationProcessor.cs

@ -4,6 +4,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory; using SixLabors.Memory;
@ -28,6 +29,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization
public AdaptiveHistEqualizationProcessor(int gridSize, int clipLimit, int luminanceLevels) public AdaptiveHistEqualizationProcessor(int gridSize, int clipLimit, int luminanceLevels)
: base(luminanceLevels) : base(luminanceLevels)
{ {
Guard.MustBeGreaterThan(gridSize, 8, nameof(gridSize));
Guard.MustBeGreaterThan(clipLimit, 1, nameof(clipLimit));
this.GridSize = gridSize; this.GridSize = gridSize;
this.ClipLimit = clipLimit; this.ClipLimit = clipLimit;
} }
@ -51,17 +55,22 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization
int pixelsInGrid = this.GridSize * this.GridSize; int pixelsInGrid = this.GridSize * this.GridSize;
int halfGridSize = this.GridSize / 2; int halfGridSize = this.GridSize / 2;
using (Buffer2D<TPixel> targetPixels = configuration.MemoryAllocator.Allocate2D<TPixel>(source.Width, source.Height))
{
Parallel.For(
0,
source.Width,
configuration.ParallelOptions,
x =>
{
using (IBuffer<int> histogramBuffer = memoryAllocator.AllocateClean<int>(this.LuminanceLevels)) using (IBuffer<int> histogramBuffer = memoryAllocator.AllocateClean<int>(this.LuminanceLevels))
using (IBuffer<int> histogramBufferCopy = memoryAllocator.AllocateClean<int>(this.LuminanceLevels)) using (IBuffer<int> histogramBufferCopy = memoryAllocator.AllocateClean<int>(this.LuminanceLevels))
using (IBuffer<int> cdfBuffer = memoryAllocator.AllocateClean<int>(this.LuminanceLevels)) using (IBuffer<int> cdfBuffer = memoryAllocator.AllocateClean<int>(this.LuminanceLevels))
using (Buffer2D<TPixel> targetPixels = configuration.MemoryAllocator.Allocate2D<TPixel>(source.Width, source.Height))
{ {
Span<int> histogram = histogramBuffer.GetSpan(); Span<int> histogram = histogramBuffer.GetSpan();
Span<int> histogramCopy = histogramBufferCopy.GetSpan(); Span<int> histogramCopy = histogramBufferCopy.GetSpan();
Span<int> cdf = cdfBuffer.GetSpan(); Span<int> cdf = cdfBuffer.GetSpan();
for (int x = 0; x < source.Width; x++)
{
histogram.Clear(); histogram.Clear();
// Build the histogram of grayscale values for the current grid. // Build the histogram of grayscale values for the current grid.
@ -81,7 +90,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization
double numberOfPixelsMinusCdfMin = (double)(pixelsInGrid - cdfMin); double numberOfPixelsMinusCdfMin = (double)(pixelsInGrid - cdfMin);
// Map the current pixel to the new equalized value // Map the current pixel to the new equalized value
int luminance = this.GetLuminance(pixels[(y * source.Width) + x], this.LuminanceLevels); int luminance = this.GetLuminance(source[x, y], this.LuminanceLevels);
double luminanceEqualized = cdf[luminance] / numberOfPixelsMinusCdfMin; double luminanceEqualized = cdf[luminance] / numberOfPixelsMinusCdfMin;
targetPixels[x, y].PackFromVector4(new Vector4((float)luminanceEqualized)); targetPixels[x, y].PackFromVector4(new Vector4((float)luminanceEqualized));
@ -94,6 +103,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization
this.AddPixelsTooHistogram(rowSpan, histogram, this.LuminanceLevels); this.AddPixelsTooHistogram(rowSpan, histogram, this.LuminanceLevels);
} }
} }
});
Buffer2D<TPixel>.SwapOrCopyContent(source.PixelBuffer, targetPixels); Buffer2D<TPixel>.SwapOrCopyContent(source.PixelBuffer, targetPixels);
} }

Loading…
Cancel
Save