Browse Source

removed unnecessary loops in CalculateCdf, fixed typo in method name AddPixelsToHistogram

pull/673/head
popow 8 years ago
parent
commit
62b3ee3c49
  1. 7
      src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationProcessor.cs
  2. 30
      src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationProcessor.cs

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

@ -3,7 +3,6 @@
using System; using System;
using System.Numerics; using System.Numerics;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -70,7 +69,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization
for (int dy = -halfGridSize; dy < halfGridSize; dy++) for (int dy = -halfGridSize; dy < halfGridSize; dy++)
{ {
Span<TPixel> rowSpan = this.GetPixelRow(source, (int)x - halfGridSize, dy, this.GridSize); Span<TPixel> 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) if (maxIdx > maxHistIdx)
{ {
maxHistIdx = maxIdx; 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. // Add new bottom row to the histogram, mirroring rows which exceeds the borders.
rowSpan = this.GetPixelRow(source, x - halfGridSize, y + halfGridSize, this.GridSize); 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) if (maxIdx > maxHistIdx)
{ {
maxHistIdx = maxIdx; maxHistIdx = maxIdx;
@ -179,7 +178,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization
/// <param name="histogram">The histogram</param> /// <param name="histogram">The histogram</param>
/// <param name="luminanceLevels">The number of different luminance levels.</param> /// <param name="luminanceLevels">The number of different luminance levels.</param>
/// <returns>The maximum index where a value was changed.</returns> /// <returns>The maximum index where a value was changed.</returns>
private int AddPixelsTooHistogram(Span<TPixel> greyValues, Span<int> histogram, int luminanceLevels) private int AddPixelsToHistogram(Span<TPixel> greyValues, Span<int> histogram, int luminanceLevels)
{ {
int maxIdx = 0; int maxIdx = 0;
for (int idx = 0; idx < greyValues.Length; idx++) for (int idx = 0; idx < greyValues.Length; idx++)

30
src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationProcessor.cs

@ -2,11 +2,6 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.Buffers;
using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Processing.Processors.Normalization namespace SixLabors.ImageSharp.Processing.Processors.Normalization
@ -59,29 +54,20 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization
/// <returns>The first none zero value of the cdf.</returns> /// <returns>The first none zero value of the cdf.</returns>
protected int CalculateCdf(Span<int> cdf, Span<int> histogram, int maxIdx) protected int CalculateCdf(Span<int> cdf, Span<int> histogram, int maxIdx)
{ {
// Calculate the cumulative histogram
int histSum = 0; 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; int cdfMin = 0;
bool cdfMinFound = false;
for (int i = 0; i <= maxIdx; i++) for (int i = 0; i <= maxIdx; i++)
{ {
if (cdf[i] != 0) histSum += histogram[i];
if (!cdfMinFound && histSum != 0)
{ {
cdfMin = cdf[i]; cdfMin = histSum;
break; cdfMinFound = true;
} }
}
// Creating the lookup table: subtracting cdf min, so we do not need to do that inside the for loop // 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, histSum - cdfMin);
{
cdf[i] = Math.Max(0, cdf[i] - cdfMin);
} }
return 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) if (addToEachBin > 0)
{ {
for (int i = 0; i < histogram.Length; i++) for (int i = 0; i < histogram.Length; i++)

Loading…
Cancel
Save