From 62b3ee3c49b38b868b5e2f615045df9030e0e33f Mon Sep 17 00:00:00 2001 From: popow Date: Sun, 12 Aug 2018 17:18:20 +0200 Subject: [PATCH] removed unnecessary loops in CalculateCdf, fixed typo in method name AddPixelsToHistogram --- .../AdaptiveHistEqualizationProcessor.cs | 7 ++--- .../HistogramEqualizationProcessor.cs | 30 +++++-------------- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationProcessor.cs b/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationProcessor.cs index 12c897183f..5e3dea6c6f 100644 --- a/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationProcessor.cs @@ -3,7 +3,6 @@ using System; using System.Numerics; -using System.Threading.Tasks; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -70,7 +69,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization for (int dy = -halfGridSize; dy < halfGridSize; dy++) { Span rowSpan = this.GetPixelRow(source, (int)x - halfGridSize, dy, this.GridSize); - int maxIdx = this.AddPixelsTooHistogram(rowSpan, histogram, this.LuminanceLevels); + int maxIdx = this.AddPixelsToHistogram(rowSpan, histogram, this.LuminanceLevels); if (maxIdx > maxHistIdx) { maxHistIdx = maxIdx; @@ -101,7 +100,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization // Add new bottom row to the histogram, mirroring rows which exceeds the borders. rowSpan = this.GetPixelRow(source, x - halfGridSize, y + halfGridSize, this.GridSize); - int maxIdx = this.AddPixelsTooHistogram(rowSpan, histogram, this.LuminanceLevels); + int maxIdx = this.AddPixelsToHistogram(rowSpan, histogram, this.LuminanceLevels); if (maxIdx > maxHistIdx) { maxHistIdx = maxIdx; @@ -179,7 +178,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization /// The histogram /// The number of different luminance levels. /// The maximum index where a value was changed. - private int AddPixelsTooHistogram(Span greyValues, Span histogram, int luminanceLevels) + private int AddPixelsToHistogram(Span greyValues, Span histogram, int luminanceLevels) { int maxIdx = 0; for (int idx = 0; idx < greyValues.Length; idx++) diff --git a/src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationProcessor.cs b/src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationProcessor.cs index a86c8b6183..e52aff1e76 100644 --- a/src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationProcessor.cs @@ -2,11 +2,6 @@ // Licensed under the Apache License, Version 2.0. using System; -using System.Buffers; -using System.Numerics; -using System.Runtime.CompilerServices; -using SixLabors.ImageSharp.Advanced; -using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Processing.Processors.Normalization @@ -59,29 +54,20 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization /// The first none zero value of the cdf. protected int CalculateCdf(Span cdf, Span histogram, int maxIdx) { - // Calculate the cumulative histogram int histSum = 0; - for (int i = 0; i <= maxIdx; i++) - { - histSum += histogram[i]; - cdf[i] = histSum; - } - - // Get the first none zero value of the cumulative histogram int cdfMin = 0; + bool cdfMinFound = false; for (int i = 0; i <= maxIdx; i++) { - if (cdf[i] != 0) + histSum += histogram[i]; + if (!cdfMinFound && histSum != 0) { - cdfMin = cdf[i]; - break; + cdfMin = histSum; + cdfMinFound = true; } - } - // Creating the lookup table: subtracting cdf min, so we do not need to do that inside the for loop - for (int i = 0; i <= maxIdx; i++) - { - cdf[i] = Math.Max(0, cdf[i] - cdfMin); + // Creating the lookup table: subtracting cdf min, so we do not need to do that inside the for loop + cdf[i] = Math.Max(0, histSum - cdfMin); } return cdfMin; @@ -108,7 +94,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization } } - int addToEachBin = (int)Math.Floor(sumOverClip / (double)this.LuminanceLevels); + int addToEachBin = sumOverClip > 0 ? (int)Math.Floor(sumOverClip / (double)this.LuminanceLevels) : 0; if (addToEachBin > 0) { for (int i = 0; i < histogram.Length; i++)