Browse Source

Add sanitation for dithering methods.

pull/1721/head
James Jackson-South 5 years ago
parent
commit
8e8123c598
  1. 5
      src/ImageSharp/Processing/Processors/Dithering/ErrorDither.cs
  2. 5
      src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs
  3. 20
      tests/ImageSharp.Tests/Processing/Processors/Dithering/DitherTests.cs
  4. 8
      tests/ImageSharp.Tests/Processing/Processors/Quantization/QuantizerTests.cs

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

@ -129,6 +129,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
where TPaletteDitherImageProcessor : struct, IPaletteDitherImageProcessor<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
if (this == default)
{
ThrowDefaultInstance();
}
float scale = processor.DitherScale;
for (int y = bounds.Top; y < bounds.Bottom; y++)
{

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

@ -141,6 +141,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
where TPaletteDitherImageProcessor : struct, IPaletteDitherImageProcessor<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
if (this == default)
{
ThrowDefaultInstance();
}
int spread = CalculatePaletteSpread(processor.Palette.Length);
float scale = processor.DitherScale;

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) }
};
public static readonly TheoryData<IDither> DefaultInstanceDitherers
= new TheoryData<IDither>
{
default(ErrorDither),
default(OrderedDither)
};
private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.05f);
private static IDither DefaultDitherer => KnownDitherings.Bayer4x4;
@ -175,5 +182,18 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Dithering
c => c.Dither(dither),
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);
}
}
}

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

@ -155,10 +155,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Quantization
public static readonly TheoryData<IDither> DefaultInstanceDitherers
= new TheoryData<IDither>
{
default(ErrorDither),
default(OrderedDither)
};
{
default(ErrorDither),
default(OrderedDither)
};
private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.05F);

Loading…
Cancel
Save