From bc7ea4f8bffeb21e4f39e9cdebc42b8c67aa39ae Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 8 Nov 2020 12:41:38 +0100 Subject: [PATCH] Use Interlocked.Increment during histogram calculation, fixes issue #1416 --- .../GlobalHistogramEqualizationProcessor{TPixel}.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor{TPixel}.cs index 274376671..0c5a109a6 100644 --- a/src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor{TPixel}.cs @@ -6,6 +6,7 @@ using System.Buffers; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using System.Threading; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -51,7 +52,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization using IMemoryOwner histogramBuffer = memoryAllocator.Allocate(this.LuminanceLevels, AllocationOptions.Clean); - // Build the histogram of the grayscale levels + // Build the histogram of the grayscale levels. var grayscaleOperation = new GrayscaleLevelsRowOperation(interest, histogramBuffer, source, this.LuminanceLevels); ParallelRowIterator.IterateRows( this.Configuration, @@ -114,7 +115,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization #endif public void Invoke(int y) { - ref int histogramBase = ref MemoryMarshal.GetReference(this.histogramBuffer.GetSpan()); ref TPixel pixelBase = ref MemoryMarshal.GetReference(this.source.GetPixelRowSpan(y)); int levels = this.luminanceLevels; @@ -123,7 +123,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization // TODO: We should bulk convert here. var vector = Unsafe.Add(ref pixelBase, x).ToVector4(); int luminance = ImageMaths.GetBT709Luminance(ref vector, levels); - Unsafe.Add(ref histogramBase, luminance)++; + ref int histogramAtLuminance = ref MemoryMarshal.GetReference(this.histogramBuffer.Slice(luminance)); + Interlocked.Increment(ref histogramAtLuminance); } } }