Browse Source

using GetPixelSpan instead of GetPixelRowSpan

af/merge-core
popow 8 years ago
parent
commit
ce96775d33
  1. 41
      src/ImageSharp/Processing/Normalization/HistogramEqualizationProcessor.cs

41
src/ImageSharp/Processing/Normalization/HistogramEqualizationProcessor.cs

@ -29,15 +29,12 @@ namespace SixLabors.ImageSharp.Processing.Normalization
// build the histogram of the grayscale levels // build the histogram of the grayscale levels
int luminanceLevels = is16bitPerChannel ? 65536 : 256; int luminanceLevels = is16bitPerChannel ? 65536 : 256;
Span<int> histogram = memoryAllocator.Allocate<int>(luminanceLevels, clear: true).GetSpan(); Span<int> histogram = memoryAllocator.Allocate<int>(luminanceLevels, clear: true).GetSpan();
for (int y = 0; y < source.Height; y++) Span<TPixel> pixels = source.GetPixelSpan();
for (int i = 0; i < pixels.Length; i++)
{ {
Span<TPixel> row = source.GetPixelRowSpan(y); TPixel sourcePixel = pixels[i];
for (int x = 0; x < source.Width; x++) int luminance = this.GetLuminance(sourcePixel, is16bitPerChannel, ref rgb24, ref rgb48);
{ histogram[luminance]++;
TPixel sourcePixel = row[x];
int luminance = this.GetLuminance(sourcePixel, is16bitPerChannel, ref rgb24, ref rgb48);
histogram[luminance]++;
}
} }
// calculate the cumulative distribution function (which will be the cumulative histogram) // calculate the cumulative distribution function (which will be the cumulative histogram)
@ -69,25 +66,21 @@ namespace SixLabors.ImageSharp.Processing.Normalization
// apply the cdf to each pixel of the image // apply the cdf to each pixel of the image
double numberOfPixelsMinusCdfMin = (double)(numberOfPixels - cdfMin); double numberOfPixelsMinusCdfMin = (double)(numberOfPixels - cdfMin);
int luminanceLevelsMinusOne = luminanceLevels - 1; int luminanceLevelsMinusOne = luminanceLevels - 1;
for (int y = 0; y < source.Height; y++) for (int i = 0; i < pixels.Length; i++)
{ {
Span<TPixel> row = source.GetPixelRowSpan(y); TPixel sourcePixel = pixels[i];
for (int x = 0; x < source.Width; x++)
{
TPixel sourcePixel = row[x];
int luminance = this.GetLuminance(sourcePixel, is16bitPerChannel, ref rgb24, ref rgb48); int luminance = this.GetLuminance(sourcePixel, is16bitPerChannel, ref rgb24, ref rgb48);
double luminanceEqualized = (cdf[luminance] / numberOfPixelsMinusCdfMin) * luminanceLevelsMinusOne; double luminanceEqualized = (cdf[luminance] / numberOfPixelsMinusCdfMin) * luminanceLevelsMinusOne;
luminanceEqualized = Math.Round(luminanceEqualized); luminanceEqualized = Math.Round(luminanceEqualized);
if (is16bitPerChannel) if (is16bitPerChannel)
{ {
row[x].PackFromRgb48(new Rgb48((ushort)luminanceEqualized, (ushort)luminanceEqualized, (ushort)luminanceEqualized)); pixels[i].PackFromRgb48(new Rgb48((ushort)luminanceEqualized, (ushort)luminanceEqualized, (ushort)luminanceEqualized));
} }
else else
{ {
row[x].PackFromRgba32(new Rgba32((byte)luminanceEqualized, (byte)luminanceEqualized, (byte)luminanceEqualized)); pixels[i].PackFromRgba32(new Rgba32((byte)luminanceEqualized, (byte)luminanceEqualized, (byte)luminanceEqualized));
}
} }
} }
} }

Loading…
Cancel
Save