Skip to content

Commit 8e8123c

Browse files
Add sanitation for dithering methods.
1 parent 8238c8f commit 8e8123c

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ public void ApplyPaletteDither<TPaletteDitherImageProcessor, TPixel>(
129129
where TPaletteDitherImageProcessor : struct, IPaletteDitherImageProcessor<TPixel>
130130
where TPixel : unmanaged, IPixel<TPixel>
131131
{
132+
if (this == default)
133+
{
134+
ThrowDefaultInstance();
135+
}
136+
132137
float scale = processor.DitherScale;
133138
for (int y = bounds.Top; y < bounds.Bottom; y++)
134139
{

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ public void ApplyPaletteDither<TPaletteDitherImageProcessor, TPixel>(
141141
where TPaletteDitherImageProcessor : struct, IPaletteDitherImageProcessor<TPixel>
142142
where TPixel : unmanaged, IPixel<TPixel>
143143
{
144+
if (this == default)
145+
{
146+
ThrowDefaultInstance();
147+
}
148+
144149
int spread = CalculatePaletteSpread(processor.Palette.Length);
145150
float scale = processor.DitherScale;
146151

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ public static readonly TheoryData<IDither, string> OrderedDitherers
4343
{ KnownDitherings.Ordered3x3, nameof(KnownDitherings.Ordered3x3) }
4444
};
4545

46+
public static readonly TheoryData<IDither> DefaultInstanceDitherers
47+
= new TheoryData<IDither>
48+
{
49+
default(ErrorDither),
50+
default(OrderedDither)
51+
};
52+
4653
private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.05f);
4754

4855
private static IDither DefaultDitherer => KnownDitherings.Bayer4x4;
@@ -175,5 +182,18 @@ public void CommonDitherers_WorkWithDiscoBuffers<TPixel>(
175182
c => c.Dither(dither),
176183
name);
177184
}
185+
186+
[Theory]
187+
[MemberData(nameof(DefaultInstanceDitherers))]
188+
public void ShouldThrowForDefaultDitherInstance(IDither dither)
189+
{
190+
void Command()
191+
{
192+
using var image = new Image<Rgba32>(10, 10);
193+
image.Mutate(x => x.Dither(dither));
194+
}
195+
196+
Assert.Throws<ImageProcessingException>(Command);
197+
}
178198
}
179199
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ public static readonly TheoryData<IQuantizer> DitherScaleQuantizers
155155

156156
public static readonly TheoryData<IDither> DefaultInstanceDitherers
157157
= new TheoryData<IDither>
158-
{
159-
default(ErrorDither),
160-
default(OrderedDither)
161-
};
158+
{
159+
default(ErrorDither),
160+
default(OrderedDither)
161+
};
162162

163163
private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.05F);
164164

0 commit comments

Comments
 (0)