diff --git a/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationSWProcessor.cs b/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationSWProcessor.cs index 66775520f2..bf6eeeb04e 100644 --- a/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationSWProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationSWProcessor.cs @@ -63,16 +63,18 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization using (System.Buffers.IMemoryOwner histogramBuffer = memoryAllocator.Allocate(this.LuminanceLevels, AllocationOptions.Clean)) using (System.Buffers.IMemoryOwner histogramBufferCopy = memoryAllocator.Allocate(this.LuminanceLevels, AllocationOptions.Clean)) using (System.Buffers.IMemoryOwner cdfBuffer = memoryAllocator.Allocate(this.LuminanceLevels, AllocationOptions.Clean)) + using (System.Buffers.IMemoryOwner pixelRowBuffer = memoryAllocator.Allocate(tileWidth, AllocationOptions.Clean)) { Span histogram = histogramBuffer.GetSpan(); Span histogramCopy = histogramBufferCopy.GetSpan(); Span cdf = cdfBuffer.GetSpan(); + Span pixelRow = pixelRowBuffer.GetSpan(); int maxHistIdx = 0; // Build the histogram of grayscale values for the current grid. for (int dy = -halfTileWith; dy < halfTileWith; dy++) { - Span rowSpan = this.GetPixelRow(source, (int)x - halfTileWith, dy, tileWidth); + Span rowSpan = this.GetPixelRow(source, pixelRow, (int)x - halfTileWith, dy, tileWidth); int maxIdx = this.AddPixelsToHistogram(rowSpan, histogram, this.LuminanceLevels); if (maxIdx > maxHistIdx) { @@ -99,11 +101,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization targetPixels[x, y].FromVector4(new Vector4(luminanceEqualized)); // Remove top most row from the histogram, mirroring rows which exceeds the borders. - Span rowSpan = this.GetPixelRow(source, x - halfTileWith, y - halfTileWith, tileWidth); + Span rowSpan = this.GetPixelRow(source, pixelRow, x - halfTileWith, y - halfTileWith, tileWidth); maxHistIdx = this.RemovePixelsFromHistogram(rowSpan, histogram, this.LuminanceLevels, maxHistIdx); // Add new bottom row to the histogram, mirroring rows which exceeds the borders. - rowSpan = this.GetPixelRow(source, x - halfTileWith, y + halfTileWith, tileWidth); + rowSpan = this.GetPixelRow(source, pixelRow, x - halfTileWith, y + halfTileWith, tileWidth); int maxIdx = this.AddPixelsToHistogram(rowSpan, histogram, this.LuminanceLevels); if (maxIdx > maxHistIdx) { @@ -121,12 +123,15 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization /// Get the a pixel row at a given position with a length of the tile width. Mirrors pixels which exceeds the edges. /// /// The source image. + /// Pre-allocated pixel row span of the size of a tile width. /// The x position. /// The y position. /// The width in pixels of a tile. /// A pixel row of the length of the tile width. - private Span GetPixelRow(ImageFrame source, int x, int y, int tileWidth) + private Span GetPixelRow(ImageFrame source, Span rowPixels, int x, int y, int tileWidth) { + rowPixels.Clear(); + if (y < 0) { y = Math.Abs(y); @@ -140,7 +145,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization // Special cases for the left and the right border where GetPixelRowSpan can not be used if (x < 0) { - var rowPixels = new TPixel[tileWidth]; int idx = 0; for (int dx = x; dx < x + tileWidth; dx++) { @@ -152,7 +156,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization } else if (x + tileWidth > source.Width) { - var rowPixels = new TPixel[tileWidth]; int idx = 0; for (int dx = x; dx < x + tileWidth; dx++) {