Browse Source

Merge pull request #1418 from SixLabors/bp/Issue1416

Fix for Issue 1416
js/color-alpha-handling
James Jackson-South 6 years ago
committed by GitHub
parent
commit
3b4149d43e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor{TPixel}.cs
  2. 39
      tests/ImageSharp.Tests/Processing/Normalization/HistogramEqualizationTests.cs
  3. 1
      tests/ImageSharp.Tests/TestImages.cs
  4. 2
      tests/Images/External
  5. 3
      tests/Images/Input/Jpg/baseline/640px-Unequalized_Hawkes_Bay_NZ.jpg

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

@ -6,6 +6,7 @@ using System.Buffers;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading;
using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -51,7 +52,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization
using IMemoryOwner<int> histogramBuffer = memoryAllocator.Allocate<int>(this.LuminanceLevels, AllocationOptions.Clean); using IMemoryOwner<int> histogramBuffer = memoryAllocator.Allocate<int>(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); var grayscaleOperation = new GrayscaleLevelsRowOperation(interest, histogramBuffer, source, this.LuminanceLevels);
ParallelRowIterator.IterateRows( ParallelRowIterator.IterateRows(
this.Configuration, this.Configuration,
@ -123,7 +124,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization
// TODO: We should bulk convert here. // TODO: We should bulk convert here.
var vector = Unsafe.Add(ref pixelBase, x).ToVector4(); var vector = Unsafe.Add(ref pixelBase, x).ToVector4();
int luminance = ImageMaths.GetBT709Luminance(ref vector, levels); int luminance = ImageMaths.GetBT709Luminance(ref vector, levels);
Unsafe.Add(ref histogramBase, luminance)++; Interlocked.Increment(ref Unsafe.Add(ref histogramBase, luminance));
} }
} }
} }

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

@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Normalization
[Theory] [Theory]
[InlineData(256)] [InlineData(256)]
[InlineData(65536)] [InlineData(65536)]
public void HistogramEqualizationTest(int luminanceLevels) public void GlobalHistogramEqualization_WithDifferentLumanceLevels(int luminanceLevels)
{ {
// Arrange // Arrange
var pixels = new byte[] var pixels = new byte[]
@ -45,20 +45,21 @@ namespace SixLabors.ImageSharp.Tests.Processing.Normalization
var expected = new byte[] var expected = new byte[]
{ {
0, 12, 53, 32, 146, 53, 174, 53, 0, 12, 53, 32, 146, 53, 174, 53,
57, 32, 12, 227, 219, 202, 32, 154, 57, 32, 12, 227, 219, 202, 32, 154,
65, 85, 93, 239, 251, 227, 65, 158, 65, 85, 93, 239, 251, 227, 65, 158,
73, 146, 146, 247, 255, 235, 154, 130, 73, 146, 146, 247, 255, 235, 154, 130,
97, 166, 117, 231, 243, 210, 117, 117, 97, 166, 117, 231, 243, 210, 117, 117,
117, 190, 36, 190, 178, 93, 20, 170, 117, 190, 36, 190, 178, 93, 20, 170,
130, 202, 73, 20, 12, 53, 85, 194, 130, 202, 73, 20, 12, 53, 85, 194,
146, 206, 130, 117, 85, 166, 182, 215 146, 206, 130, 117, 85, 166, 182, 215
}; };
// Act // Act
image.Mutate(x => x.HistogramEqualization(new HistogramEqualizationOptions image.Mutate(x => x.HistogramEqualization(new HistogramEqualizationOptions
{ {
LuminanceLevels = luminanceLevels LuminanceLevels = luminanceLevels,
Method = HistogramEqualizationMethod.Global
})); }));
// Assert // Assert
@ -75,6 +76,24 @@ namespace SixLabors.ImageSharp.Tests.Processing.Normalization
} }
} }
[Theory]
[WithFile(TestImages.Jpeg.Baseline.HistogramEqImage, PixelTypes.Rgba32)]
public void GlobalHistogramEqualization_CompareToReferenceOutput<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var options = new HistogramEqualizationOptions
{
Method = HistogramEqualizationMethod.Global,
LuminanceLevels = 256,
};
image.Mutate(x => x.HistogramEqualization(options));
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider, extension: "png");
}
}
[Theory] [Theory]
[WithFile(TestImages.Jpeg.Baseline.LowContrast, PixelTypes.Rgba32)] [WithFile(TestImages.Jpeg.Baseline.LowContrast, PixelTypes.Rgba32)]
public void Adaptive_SlidingWindow_15Tiles_WithClipping<TPixel>(TestImageProvider<TPixel> provider) public void Adaptive_SlidingWindow_15Tiles_WithClipping<TPixel>(TestImageProvider<TPixel> provider)

1
tests/ImageSharp.Tests/TestImages.cs

@ -196,6 +196,7 @@ namespace SixLabors.ImageSharp.Tests
public const string YcckSubsample1222 = "Jpg/baseline/ycck-subsample-1222.jpg"; public const string YcckSubsample1222 = "Jpg/baseline/ycck-subsample-1222.jpg";
public const string Iptc = "Jpg/baseline/iptc.jpg"; public const string Iptc = "Jpg/baseline/iptc.jpg";
public const string App13WithEmptyIptc = "Jpg/baseline/iptc-psAPP13-wIPTCempty.jpg"; public const string App13WithEmptyIptc = "Jpg/baseline/iptc-psAPP13-wIPTCempty.jpg";
public const string HistogramEqImage = "Jpg/baseline/640px-Unequalized_Hawkes_Bay_NZ.jpg";
public static readonly string[] All = public static readonly string[] All =
{ {

2
tests/Images/External

@ -1 +1 @@
Subproject commit cc6465910d092319ef9bf4e99698a0649996d3c5 Subproject commit 8b43d14d21ce9b436af3d12a70d38402cdba176b

3
tests/Images/Input/Jpg/baseline/640px-Unequalized_Hawkes_Bay_NZ.jpg

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d1fafc61231325c42d94fe163486a6c5144fb6211ccdceb902d5cb4ddebda9e1
size 32428
Loading…
Cancel
Save