Skip to content

Commit abc1573

Browse files
Merge pull request #1418 from SixLabors/bp/Issue1416
Fix for Issue 1416
2 parents b37044f + ae27c04 commit abc1573

File tree

5 files changed

+34
-13
lines changed

5 files changed

+34
-13
lines changed

src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor{TPixel}.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Numerics;
77
using System.Runtime.CompilerServices;
88
using System.Runtime.InteropServices;
9+
using System.Threading;
910
using SixLabors.ImageSharp.Advanced;
1011
using SixLabors.ImageSharp.Memory;
1112
using SixLabors.ImageSharp.PixelFormats;
@@ -51,7 +52,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
5152

5253
using IMemoryOwner<int> histogramBuffer = memoryAllocator.Allocate<int>(this.LuminanceLevels, AllocationOptions.Clean);
5354

54-
// Build the histogram of the grayscale levels
55+
// Build the histogram of the grayscale levels.
5556
var grayscaleOperation = new GrayscaleLevelsRowOperation(interest, histogramBuffer, source, this.LuminanceLevels);
5657
ParallelRowIterator.IterateRows(
5758
this.Configuration,
@@ -123,7 +124,7 @@ public void Invoke(int y)
123124
// TODO: We should bulk convert here.
124125
var vector = Unsafe.Add(ref pixelBase, x).ToVector4();
125126
int luminance = ImageMaths.GetBT709Luminance(ref vector, levels);
126-
Unsafe.Add(ref histogramBase, luminance)++;
127+
Interlocked.Increment(ref Unsafe.Add(ref histogramBase, luminance));
127128
}
128129
}
129130
}

tests/ImageSharp.Tests/Processing/Normalization/HistogramEqualizationTests.cs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class HistogramEqualizationTests
1717
[Theory]
1818
[InlineData(256)]
1919
[InlineData(65536)]
20-
public void HistogramEqualizationTest(int luminanceLevels)
20+
public void GlobalHistogramEqualization_WithDifferentLumanceLevels(int luminanceLevels)
2121
{
2222
// Arrange
2323
var pixels = new byte[]
@@ -45,20 +45,21 @@ public void HistogramEqualizationTest(int luminanceLevels)
4545

4646
var expected = new byte[]
4747
{
48-
0, 12, 53, 32, 146, 53, 174, 53,
49-
57, 32, 12, 227, 219, 202, 32, 154,
50-
65, 85, 93, 239, 251, 227, 65, 158,
51-
73, 146, 146, 247, 255, 235, 154, 130,
52-
97, 166, 117, 231, 243, 210, 117, 117,
53-
117, 190, 36, 190, 178, 93, 20, 170,
54-
130, 202, 73, 20, 12, 53, 85, 194,
55-
146, 206, 130, 117, 85, 166, 182, 215
48+
0, 12, 53, 32, 146, 53, 174, 53,
49+
57, 32, 12, 227, 219, 202, 32, 154,
50+
65, 85, 93, 239, 251, 227, 65, 158,
51+
73, 146, 146, 247, 255, 235, 154, 130,
52+
97, 166, 117, 231, 243, 210, 117, 117,
53+
117, 190, 36, 190, 178, 93, 20, 170,
54+
130, 202, 73, 20, 12, 53, 85, 194,
55+
146, 206, 130, 117, 85, 166, 182, 215
5656
};
5757

5858
// Act
5959
image.Mutate(x => x.HistogramEqualization(new HistogramEqualizationOptions
6060
{
61-
LuminanceLevels = luminanceLevels
61+
LuminanceLevels = luminanceLevels,
62+
Method = HistogramEqualizationMethod.Global
6263
}));
6364

6465
// Assert
@@ -75,6 +76,24 @@ public void HistogramEqualizationTest(int luminanceLevels)
7576
}
7677
}
7778

79+
[Theory]
80+
[WithFile(TestImages.Jpeg.Baseline.HistogramEqImage, PixelTypes.Rgba32)]
81+
public void GlobalHistogramEqualization_CompareToReferenceOutput<TPixel>(TestImageProvider<TPixel> provider)
82+
where TPixel : unmanaged, IPixel<TPixel>
83+
{
84+
using (Image<TPixel> image = provider.GetImage())
85+
{
86+
var options = new HistogramEqualizationOptions
87+
{
88+
Method = HistogramEqualizationMethod.Global,
89+
LuminanceLevels = 256,
90+
};
91+
image.Mutate(x => x.HistogramEqualization(options));
92+
image.DebugSave(provider);
93+
image.CompareToReferenceOutput(ValidatorComparer, provider, extension: "png");
94+
}
95+
}
96+
7897
[Theory]
7998
[WithFile(TestImages.Jpeg.Baseline.LowContrast, PixelTypes.Rgba32)]
8099
public void Adaptive_SlidingWindow_15Tiles_WithClipping<TPixel>(TestImageProvider<TPixel> provider)

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ public static class Bad
196196
public const string YcckSubsample1222 = "Jpg/baseline/ycck-subsample-1222.jpg";
197197
public const string Iptc = "Jpg/baseline/iptc.jpg";
198198
public const string App13WithEmptyIptc = "Jpg/baseline/iptc-psAPP13-wIPTCempty.jpg";
199+
public const string HistogramEqImage = "Jpg/baseline/640px-Unequalized_Hawkes_Bay_NZ.jpg";
199200

200201
public static readonly string[] All =
201202
{
31.7 KB
Loading

0 commit comments

Comments
 (0)