From fa1f0367204cc72790af373beaa318099534d987 Mon Sep 17 00:00:00 2001 From: popow Date: Mon, 10 Dec 2018 20:46:04 +0100 Subject: [PATCH] alpha channel from the original image is now preserved --- .../Normalization/AdaptiveHistEqualizationProcessor.cs | 10 +++++----- .../AdaptiveHistEqualizationSWProcessor.cs | 9 ++++----- .../GlobalHistogramEqualizationProcessor.cs | 2 +- .../Normalization/HistogramEqualizationOptions.cs | 2 +- .../Normalization/HistogramEqualizationProcessor.cs | 8 ++++---- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationProcessor.cs b/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationProcessor.cs index 6e55ec8472..f9190a92b6 100644 --- a/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationProcessor.cs @@ -27,7 +27,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization /// The number of different luminance levels. Typical values are 256 for 8-bit grayscale images /// or 65536 for 16-bit grayscale images. /// Indicating whether to clip the histogram bins at a specific value. - /// Histogram clip limit in percent of the total pixels in the grid. Histogram bins which exceed this limit, will be capped at this value. + /// Histogram clip limit in percent of the total pixels in the tile. Histogram bins which exceed this limit, will be capped at this value. /// The number of tiles the image is split into (horizontal and vertically). Minimum value is 2. public AdaptiveHistEqualizationProcessor(int luminanceLevels, bool clipHistogram, float clipLimitPercentage, int tiles) : base(luminanceLevels, clipHistogram, clipLimitPercentage) @@ -83,7 +83,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization for (int dx = x; dx < xEnd; dx++) { float luminanceEqualized = this.InterpolateBetweenFourTiles(source[dx, dy], cdfData, tileX, tileY, cdfX, tileYStartPosition.cdfY, tileWidth, tileHeight, pixelsInTile); - pixelRow[dx].FromVector4(new Vector4(luminanceEqualized)); + pixelRow[dx].FromVector4(new Vector4(luminanceEqualized, luminanceEqualized, luminanceEqualized, pixelRow[dx].ToVector4().W)); tileX++; } @@ -141,7 +141,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization for (int dx = xStart; dx < xEnd; dx++) { float luminanceEqualized = cdfData.RemapGreyValue(this.GetLuminance(source[dx, dy], this.LuminanceLevels), pixelsInTile); - pixels[(dy * source.Width) + dx].FromVector4(new Vector4(luminanceEqualized)); + pixels[(dy * source.Width) + dx].FromVector4(new Vector4(luminanceEqualized, luminanceEqualized, luminanceEqualized, source[dx, dy].ToVector4().W)); } } } @@ -174,7 +174,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization for (int dx = xStart; dx < xEnd; dx++) { float luminanceEqualized = this.InterpolateBetweenTwoTiles(source[dx, dy], cdfData[cdfX, cdfY], cdfData[cdfX, cdfY + 1], tileY, tileHeight, pixelsInTile); - pixels[(dy * source.Width) + dx].FromVector4(new Vector4(luminanceEqualized)); + pixels[(dy * source.Width) + dx].FromVector4(new Vector4(luminanceEqualized, luminanceEqualized, luminanceEqualized, source[dx, dy].ToVector4().W)); tileX++; } @@ -213,7 +213,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization for (int dx = x; dx < xLimit; dx++) { float luminanceEqualized = this.InterpolateBetweenTwoTiles(source[dx, dy], cdfData[cdfX, cdfY], cdfData[cdfX + 1, cdfY], tileX, tileWidth, pixelsInTile); - pixels[(dy * source.Width) + dx].FromVector4(new Vector4(luminanceEqualized)); + pixels[(dy * source.Width) + dx].FromVector4(new Vector4(luminanceEqualized, luminanceEqualized, luminanceEqualized, source[dx, dy].ToVector4().W)); tileX++; } diff --git a/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationSWProcessor.cs b/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationSWProcessor.cs index a9ec21d2d1..7d82b413fd 100644 --- a/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationSWProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationSWProcessor.cs @@ -6,7 +6,6 @@ using System.Numerics; using System.Threading.Tasks; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Memory; -using SixLabors.ImageSharp.ParallelUtils; using SixLabors.ImageSharp.PixelFormats; using SixLabors.Memory; using SixLabors.Primitives; @@ -26,7 +25,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization /// The number of different luminance levels. Typical values are 256 for 8-bit grayscale images /// or 65536 for 16-bit grayscale images. /// Indicating whether to clip the histogram bins at a specific value. - /// Histogram clip limit in percent of the total pixels in the grid. Histogram bins which exceed this limit, will be capped at this value. + /// Histogram clip limit in percent of the total pixels in the tile. Histogram bins which exceed this limit, will be capped at this value. /// The number of tiles the image is split into (horizontal and vertically). Minimum value is 2. public AdaptiveHistEqualizationSWProcessor(int luminanceLevels, bool clipHistogram, float clipLimitPercentage, int tiles) : base(luminanceLevels, clipHistogram, clipLimitPercentage) @@ -71,7 +70,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization Span pixelRow = pixelRowBuffer.GetSpan(); int maxHistIdx = 0; - // Build the histogram of grayscale values for the current grid. + // Build the histogram of grayscale values for the current tile. for (int dy = -halfTileWith; dy < halfTileWith; dy++) { Span rowSpan = this.GetPixelRow(source, pixelRow, (int)x - halfTileWith, dy, tileWidth); @@ -91,14 +90,14 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization this.ClipHistogram(histogramCopy, this.ClipLimitPercentage, pixeInTile); } - // Calculate the cumulative distribution function, which will map each input pixel in the current grid to a new value. + // Calculate the cumulative distribution function, which will map each input pixel in the current tile to a new value. int cdfMin = this.ClipHistogramEnabled ? this.CalculateCdf(cdf, histogramCopy, maxHistIdx) : this.CalculateCdf(cdf, histogram, maxHistIdx); float numberOfPixelsMinusCdfMin = pixeInTile - cdfMin; // Map the current pixel to the new equalized value int luminance = this.GetLuminance(source[x, y], this.LuminanceLevels); float luminanceEqualized = cdf[luminance] / numberOfPixelsMinusCdfMin; - targetPixels[x, y].FromVector4(new Vector4(luminanceEqualized)); + targetPixels[x, y].FromVector4(new Vector4(luminanceEqualized, luminanceEqualized, luminanceEqualized, source[x, y].ToVector4().W)); // Remove top most row from the histogram, mirroring rows which exceeds the borders. Span rowSpan = this.GetPixelRow(source, pixelRow, x - halfTileWith, y - halfTileWith, tileWidth); diff --git a/src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor.cs b/src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor.cs index a4de02cc7e..5cad9e60a7 100644 --- a/src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor.cs @@ -67,7 +67,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization int luminance = this.GetLuminance(sourcePixel, this.LuminanceLevels); float luminanceEqualized = cdf[luminance] / numberOfPixelsMinusCdfMin; - pixels[i].FromVector4(new Vector4(luminanceEqualized)); + pixels[i].FromVector4(new Vector4(luminanceEqualized, luminanceEqualized, luminanceEqualized, sourcePixel.ToVector4().W)); } } } diff --git a/src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationOptions.cs b/src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationOptions.cs index 190c664436..6c0938c4ec 100644 --- a/src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationOptions.cs +++ b/src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationOptions.cs @@ -26,7 +26,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization /// /// Gets or sets the histogram clip limit in percent of the total pixels in a tile. Histogram bins which exceed this limit, will be capped at this value. - /// Defaults to 0.35. + /// Defaults to 0.035f. /// public float ClipLimitPercentage { get; set; } = 0.035f; diff --git a/src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationProcessor.cs b/src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationProcessor.cs index e52aff1e76..2763fcddd8 100644 --- a/src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationProcessor.cs @@ -19,7 +19,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization /// The number of different luminance levels. Typical values are 256 for 8-bit grayscale images /// or 65536 for 16-bit grayscale images. /// Indicates, if histogram bins should be clipped. - /// Histogram clip limit in percent of the total pixels in the grid. Histogram bins which exceed this limit, will be capped at this value. + /// Histogram clip limit in percent of the total pixels in the tile. Histogram bins which exceed this limit, will be capped at this value. protected HistogramEqualizationProcessor(int luminanceLevels, bool clipHistogram, float clipLimitPercentage) { Guard.MustBeGreaterThan(luminanceLevels, 0, nameof(luminanceLevels)); @@ -41,7 +41,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization public bool ClipHistogramEnabled { get; } /// - /// Gets the histogram clip limit in percent of the total pixels in the grid. Histogram bins which exceed this limit, will be capped at this value. + /// Gets the histogram clip limit in percent of the total pixels in the tile. Histogram bins which exceed this limit, will be capped at this value. /// public float ClipLimitPercentage { get; } @@ -79,8 +79,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization /// the values over the clip limit to all other bins equally. /// /// The histogram to apply the clipping. - /// Histogram clip limit in percent of the total pixels in the grid. Histogram bins which exceed this limit, will be capped at this value. - /// The numbers of pixels inside the grid. + /// Histogram clip limit in percent of the total pixels in the tile. Histogram bins which exceed this limit, will be capped at this value. + /// The numbers of pixels inside the tile. protected void ClipHistogram(Span histogram, float clipLimitPercentage, int pixelCount) { int clipLimit = Convert.ToInt32(pixelCount * clipLimitPercentage);