Browse Source

Merge branch 'master' into bp/tiff24bit

pull/1724/head
Brian Popow 5 years ago
committed by GitHub
parent
commit
85368ccc43
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      src/ImageSharp/Processing/Processors/Dithering/ErrorDither.cs
  2. 16
      src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs
  3. 4
      src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs
  4. 3
      src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs
  5. 6
      src/ImageSharp/Processing/Processors/Quantization/WebSafePaletteQuantizer.cs
  6. 4
      src/ImageSharp/Processing/Processors/Quantization/WernerPaletteQuantizer.cs
  7. 4
      src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs
  8. 20
      tests/ImageSharp.Tests/Processing/Processors/Dithering/DitherTests.cs
  9. 23
      tests/ImageSharp.Tests/Processing/Processors/Quantization/QuantizerTests.cs

16
src/ImageSharp/Processing/Processors/Dithering/ErrorDither.cs

@ -27,6 +27,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public ErrorDither(in DenseMatrix<float> matrix, int offset) public ErrorDither(in DenseMatrix<float> matrix, int offset)
{ {
Guard.MustBeGreaterThan(offset, 0, nameof(offset));
this.matrix = matrix; this.matrix = matrix;
this.offset = offset; this.offset = offset;
} }
@ -95,6 +97,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
where TFrameQuantizer : struct, IQuantizer<TPixel> where TFrameQuantizer : struct, IQuantizer<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
if (this == default)
{
ThrowDefaultInstance();
}
int offsetY = bounds.Top; int offsetY = bounds.Top;
int offsetX = bounds.Left; int offsetX = bounds.Left;
float scale = quantizer.Options.DitherScale; float scale = quantizer.Options.DitherScale;
@ -122,6 +129,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
where TPaletteDitherImageProcessor : struct, IPaletteDitherImageProcessor<TPixel> where TPaletteDitherImageProcessor : struct, IPaletteDitherImageProcessor<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
if (this == default)
{
ThrowDefaultInstance();
}
float scale = processor.DitherScale; float scale = processor.DitherScale;
for (int y = bounds.Top; y < bounds.Bottom; y++) for (int y = bounds.Top; y < bounds.Bottom; y++)
{ {
@ -210,5 +222,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
/// <inheritdoc/> /// <inheritdoc/>
public override int GetHashCode() public override int GetHashCode()
=> HashCode.Combine(this.offset, this.matrix); => HashCode.Combine(this.offset, this.matrix);
[MethodImpl(InliningOptions.ColdPath)]
private static void ThrowDefaultInstance()
=> throw new ImageProcessingException("Cannot use the default value type instance to dither.");
} }
} }

16
src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs

@ -24,6 +24,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public OrderedDither(uint length) public OrderedDither(uint length)
{ {
Guard.MustBeGreaterThan(length, 0, nameof(length));
DenseMatrix<uint> ditherMatrix = OrderedDitherFactory.CreateDitherMatrix(length); DenseMatrix<uint> ditherMatrix = OrderedDitherFactory.CreateDitherMatrix(length);
// Create a new matrix to run against, that pre-thresholds the values. // Create a new matrix to run against, that pre-thresholds the values.
@ -109,6 +111,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
where TFrameQuantizer : struct, IQuantizer<TPixel> where TFrameQuantizer : struct, IQuantizer<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
if (this == default)
{
ThrowDefaultInstance();
}
int spread = CalculatePaletteSpread(destination.Palette.Length); int spread = CalculatePaletteSpread(destination.Palette.Length);
float scale = quantizer.Options.DitherScale; float scale = quantizer.Options.DitherScale;
@ -134,6 +141,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
where TPaletteDitherImageProcessor : struct, IPaletteDitherImageProcessor<TPixel> where TPaletteDitherImageProcessor : struct, IPaletteDitherImageProcessor<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
if (this == default)
{
ThrowDefaultInstance();
}
int spread = CalculatePaletteSpread(processor.Palette.Length); int spread = CalculatePaletteSpread(processor.Palette.Length);
float scale = processor.DitherScale; float scale = processor.DitherScale;
@ -201,5 +213,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public override int GetHashCode() public override int GetHashCode()
=> HashCode.Combine(this.thresholdMatrix, this.modulusX, this.modulusY); => HashCode.Combine(this.thresholdMatrix, this.modulusX, this.modulusY);
[MethodImpl(InliningOptions.ColdPath)]
private static void ThrowDefaultInstance()
=> throw new ImageProcessingException("Cannot use the default value type instance to dither.");
} }
} }

4
src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs

@ -11,14 +11,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// </summary> /// </summary>
public class OctreeQuantizer : IQuantizer public class OctreeQuantizer : IQuantizer
{ {
private static readonly QuantizerOptions DefaultOptions = new QuantizerOptions();
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="OctreeQuantizer"/> class /// Initializes a new instance of the <see cref="OctreeQuantizer"/> class
/// using the default <see cref="QuantizerOptions"/>. /// using the default <see cref="QuantizerOptions"/>.
/// </summary> /// </summary>
public OctreeQuantizer() public OctreeQuantizer()
: this(DefaultOptions) : this(new QuantizerOptions())
{ {
} }

3
src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs

@ -11,7 +11,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// </summary> /// </summary>
public class PaletteQuantizer : IQuantizer public class PaletteQuantizer : IQuantizer
{ {
private static readonly QuantizerOptions DefaultOptions = new QuantizerOptions();
private readonly ReadOnlyMemory<Color> colorPalette; private readonly ReadOnlyMemory<Color> colorPalette;
/// <summary> /// <summary>
@ -19,7 +18,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// </summary> /// </summary>
/// <param name="palette">The color palette.</param> /// <param name="palette">The color palette.</param>
public PaletteQuantizer(ReadOnlyMemory<Color> palette) public PaletteQuantizer(ReadOnlyMemory<Color> palette)
: this(palette, DefaultOptions) : this(palette, new QuantizerOptions())
{ {
} }

6
src/ImageSharp/Processing/Processors/Quantization/WebSafePaletteQuantizer.cs

@ -1,8 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.Processing.Processors.Dithering;
namespace SixLabors.ImageSharp.Processing.Processors.Quantization namespace SixLabors.ImageSharp.Processing.Processors.Quantization
{ {
/// <summary> /// <summary>
@ -10,13 +8,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// </summary> /// </summary>
public class WebSafePaletteQuantizer : PaletteQuantizer public class WebSafePaletteQuantizer : PaletteQuantizer
{ {
private static readonly QuantizerOptions DefaultOptions = new QuantizerOptions();
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WebSafePaletteQuantizer" /> class. /// Initializes a new instance of the <see cref="WebSafePaletteQuantizer" /> class.
/// </summary> /// </summary>
public WebSafePaletteQuantizer() public WebSafePaletteQuantizer()
: this(DefaultOptions) : this(new QuantizerOptions())
{ {
} }

4
src/ImageSharp/Processing/Processors/Quantization/WernerPaletteQuantizer.cs

@ -9,13 +9,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// </summary> /// </summary>
public class WernerPaletteQuantizer : PaletteQuantizer public class WernerPaletteQuantizer : PaletteQuantizer
{ {
private static readonly QuantizerOptions DefaultOptions = new QuantizerOptions();
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WernerPaletteQuantizer" /> class. /// Initializes a new instance of the <see cref="WernerPaletteQuantizer" /> class.
/// </summary> /// </summary>
public WernerPaletteQuantizer() public WernerPaletteQuantizer()
: this(DefaultOptions) : this(new QuantizerOptions())
{ {
} }

4
src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs

@ -10,14 +10,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// </summary> /// </summary>
public class WuQuantizer : IQuantizer public class WuQuantizer : IQuantizer
{ {
private static readonly QuantizerOptions DefaultOptions = new QuantizerOptions();
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WuQuantizer"/> class /// Initializes a new instance of the <see cref="WuQuantizer"/> class
/// using the default <see cref="QuantizerOptions"/>. /// using the default <see cref="QuantizerOptions"/>.
/// </summary> /// </summary>
public WuQuantizer() public WuQuantizer()
: this(DefaultOptions) : this(new QuantizerOptions())
{ {
} }

20
tests/ImageSharp.Tests/Processing/Processors/Dithering/DitherTests.cs

@ -43,6 +43,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Dithering
{ KnownDitherings.Ordered3x3, nameof(KnownDitherings.Ordered3x3) } { KnownDitherings.Ordered3x3, nameof(KnownDitherings.Ordered3x3) }
}; };
public static readonly TheoryData<IDither> DefaultInstanceDitherers
= new TheoryData<IDither>
{
default(ErrorDither),
default(OrderedDither)
};
private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.05f); private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.05f);
private static IDither DefaultDitherer => KnownDitherings.Bayer4x4; private static IDither DefaultDitherer => KnownDitherings.Bayer4x4;
@ -175,5 +182,18 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Dithering
c => c.Dither(dither), c => c.Dither(dither),
name); name);
} }
[Theory]
[MemberData(nameof(DefaultInstanceDitherers))]
public void ShouldThrowForDefaultDitherInstance(IDither dither)
{
void Command()
{
using var image = new Image<Rgba32>(10, 10);
image.Mutate(x => x.Dither(dither));
}
Assert.Throws<ImageProcessingException>(Command);
}
} }
} }

23
tests/ImageSharp.Tests/Processing/Processors/Quantization/QuantizerTests.cs

@ -5,6 +5,7 @@ using System;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Dithering;
using SixLabors.ImageSharp.Processing.Processors.Quantization; using SixLabors.ImageSharp.Processing.Processors.Quantization;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using Xunit; using Xunit;
@ -152,6 +153,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Quantization
new WuQuantizer(OrderedDitherOptions), new WuQuantizer(OrderedDitherOptions),
}; };
public static readonly TheoryData<IDither> DefaultInstanceDitherers
= new TheoryData<IDither>
{
default(ErrorDither),
default(OrderedDither)
};
private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.05F); private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.05F);
[Theory] [Theory]
@ -217,5 +225,20 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Quantization
testOutputDetails: testOutputDetails, testOutputDetails: testOutputDetails,
appendPixelTypeToFileName: false); appendPixelTypeToFileName: false);
} }
[Theory]
[MemberData(nameof(DefaultInstanceDitherers))]
public void ShouldThrowForDefaultDitherInstance(IDither dither)
{
void Command()
{
using var image = new Image<Rgba32>(10, 10);
var quantizer = new WebSafePaletteQuantizer();
quantizer.Options.Dither = dither;
image.Mutate(x => x.Quantize(quantizer));
}
Assert.Throws<ImageProcessingException>(Command);
}
} }
} }

Loading…
Cancel
Save