Browse Source

Handle default instances. #1583

pull/1721/head
James Jackson-South 5 years ago
parent
commit
547a90780f
  1. 9
      src/ImageSharp/Processing/Processors/Dithering/ErrorDither.cs
  2. 9
      src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs
  3. 23
      tests/ImageSharp.Tests/Processing/Processors/Quantization/QuantizerTests.cs

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

@ -95,6 +95,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;
@ -210,5 +215,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.");
} }
} }

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

@ -109,6 +109,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;
@ -201,5 +206,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.");
} }
} }

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