diff --git a/src/ImageSharp/Processing/Normalization/HistogramEqualizationProcessor.cs b/src/ImageSharp/Processing/Normalization/HistogramEqualizationProcessor.cs index 633761352b..6558f48895 100644 --- a/src/ImageSharp/Processing/Normalization/HistogramEqualizationProcessor.cs +++ b/src/ImageSharp/Processing/Normalization/HistogramEqualizationProcessor.cs @@ -26,19 +26,57 @@ namespace SixLabors.ImageSharp.Processing.Normalization int numberOfPixels = source.Width * source.Height; bool is16bitPerChannel = typeof(TPixel) == typeof(Rgb48) || typeof(TPixel) == typeof(Rgba64); + Span pixels = source.GetPixelSpan(); + // build the histogram of the grayscale levels int luminanceLevels = is16bitPerChannel ? 65536 : 256; - Span histogram = memoryAllocator.Allocate(luminanceLevels, clear: true).GetSpan(); - Span pixels = source.GetPixelSpan(); - for (int i = 0; i < pixels.Length; i++) + using (IBuffer histogramBuffer = memoryAllocator.AllocateClean(luminanceLevels)) + using (IBuffer cdfBuffer = memoryAllocator.AllocateClean(luminanceLevels)) { - TPixel sourcePixel = pixels[i]; - int luminance = this.GetLuminance(sourcePixel, is16bitPerChannel, ref rgb24, ref rgb48); - histogram[luminance]++; + Span histogram = histogramBuffer.GetSpan(); + for (int i = 0; i < pixels.Length; i++) + { + TPixel sourcePixel = pixels[i]; + int luminance = this.GetLuminance(sourcePixel, is16bitPerChannel, ref rgb24, ref rgb48); + histogram[luminance]++; + } + + // calculate the cumulative distribution function, which will map each input pixel to a new value + Span cdf = cdfBuffer.GetSpan(); + int cdfMin = this.CaluclateCdf(cdf, histogram); + + // apply the cdf to each pixel of the image + double numberOfPixelsMinusCdfMin = (double)(numberOfPixels - cdfMin); + int luminanceLevelsMinusOne = luminanceLevels - 1; + for (int i = 0; i < pixels.Length; i++) + { + TPixel sourcePixel = pixels[i]; + + int luminance = this.GetLuminance(sourcePixel, is16bitPerChannel, ref rgb24, ref rgb48); + double luminanceEqualized = (cdf[luminance] / numberOfPixelsMinusCdfMin) * luminanceLevelsMinusOne; + luminanceEqualized = Math.Round(luminanceEqualized); + + if (is16bitPerChannel) + { + pixels[i].PackFromRgb48(new Rgb48((ushort)luminanceEqualized, (ushort)luminanceEqualized, (ushort)luminanceEqualized)); + } + else + { + pixels[i].PackFromRgba32(new Rgba32((byte)luminanceEqualized, (byte)luminanceEqualized, (byte)luminanceEqualized)); + } + } } + } - // calculate the cumulative distribution function (which will be the cumulative histogram) - Span cdf = memoryAllocator.Allocate(luminanceLevels, clear: true).GetSpan(); + /// + /// Calculate the cumulative distribution function + /// + /// The array holding the cdf + /// The histogram of the input image + /// The first none zero value of the cdf + private int CaluclateCdf(Span cdf, Span histogram) + { + // calculate the cumulative histogram int histSum = 0; for (int i = 0; i < histogram.Length; i++) { @@ -50,7 +88,7 @@ namespace SixLabors.ImageSharp.Processing.Normalization int cdfMin = 0; for (int i = 0; i < histogram.Length; i++) { - if (histogram[i] != 0) + if (cdf[i] != 0) { cdfMin = cdf[i]; break; @@ -60,29 +98,10 @@ namespace SixLabors.ImageSharp.Processing.Normalization // creating the lookup table: subtracting cdf min, so we do not need to do that inside the for loop for (int i = 0; i < histogram.Length; i++) { - cdf[i] = cdf[i] - cdfMin; + cdf[i] = Math.Max(0, cdf[i] - cdfMin); } - // apply the cdf to each pixel of the image - double numberOfPixelsMinusCdfMin = (double)(numberOfPixels - cdfMin); - int luminanceLevelsMinusOne = luminanceLevels - 1; - for (int i = 0; i < pixels.Length; i++) - { - TPixel sourcePixel = pixels[i]; - - int luminance = this.GetLuminance(sourcePixel, is16bitPerChannel, ref rgb24, ref rgb48); - double luminanceEqualized = (cdf[luminance] / numberOfPixelsMinusCdfMin) * luminanceLevelsMinusOne; - luminanceEqualized = Math.Round(luminanceEqualized); - - if (is16bitPerChannel) - { - pixels[i].PackFromRgb48(new Rgb48((ushort)luminanceEqualized, (ushort)luminanceEqualized, (ushort)luminanceEqualized)); - } - else - { - pixels[i].PackFromRgba32(new Rgba32((byte)luminanceEqualized, (byte)luminanceEqualized, (byte)luminanceEqualized)); - } - } + return cdfMin; } ///