Skip to content

Commit 85368cc

Browse files
authored
Merge branch 'master' into bp/tiff24bit
2 parents 8c7ee58 + 4638965 commit 85368cc

File tree

9 files changed

+80
-16
lines changed

9 files changed

+80
-16
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
2727
[MethodImpl(InliningOptions.ShortMethod)]
2828
public ErrorDither(in DenseMatrix<float> matrix, int offset)
2929
{
30+
Guard.MustBeGreaterThan(offset, 0, nameof(offset));
31+
3032
this.matrix = matrix;
3133
this.offset = offset;
3234
}
@@ -95,6 +97,11 @@ public void ApplyQuantizationDither<TFrameQuantizer, TPixel>(
9597
where TFrameQuantizer : struct, IQuantizer<TPixel>
9698
where TPixel : unmanaged, IPixel<TPixel>
9799
{
100+
if (this == default)
101+
{
102+
ThrowDefaultInstance();
103+
}
104+
98105
int offsetY = bounds.Top;
99106
int offsetX = bounds.Left;
100107
float scale = quantizer.Options.DitherScale;
@@ -122,6 +129,11 @@ public void ApplyPaletteDither<TPaletteDitherImageProcessor, TPixel>(
122129
where TPaletteDitherImageProcessor : struct, IPaletteDitherImageProcessor<TPixel>
123130
where TPixel : unmanaged, IPixel<TPixel>
124131
{
132+
if (this == default)
133+
{
134+
ThrowDefaultInstance();
135+
}
136+
125137
float scale = processor.DitherScale;
126138
for (int y = bounds.Top; y < bounds.Bottom; y++)
127139
{
@@ -210,5 +222,9 @@ public bool Equals(IDither other)
210222
/// <inheritdoc/>
211223
public override int GetHashCode()
212224
=> HashCode.Combine(this.offset, this.matrix);
225+
226+
[MethodImpl(InliningOptions.ColdPath)]
227+
private static void ThrowDefaultInstance()
228+
=> throw new ImageProcessingException("Cannot use the default value type instance to dither.");
213229
}
214230
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
2424
[MethodImpl(InliningOptions.ShortMethod)]
2525
public OrderedDither(uint length)
2626
{
27+
Guard.MustBeGreaterThan(length, 0, nameof(length));
28+
2729
DenseMatrix<uint> ditherMatrix = OrderedDitherFactory.CreateDitherMatrix(length);
2830

2931
// Create a new matrix to run against, that pre-thresholds the values.
@@ -109,6 +111,11 @@ public void ApplyQuantizationDither<TFrameQuantizer, TPixel>(
109111
where TFrameQuantizer : struct, IQuantizer<TPixel>
110112
where TPixel : unmanaged, IPixel<TPixel>
111113
{
114+
if (this == default)
115+
{
116+
ThrowDefaultInstance();
117+
}
118+
112119
int spread = CalculatePaletteSpread(destination.Palette.Length);
113120
float scale = quantizer.Options.DitherScale;
114121

@@ -134,6 +141,11 @@ public void ApplyPaletteDither<TPaletteDitherImageProcessor, TPixel>(
134141
where TPaletteDitherImageProcessor : struct, IPaletteDitherImageProcessor<TPixel>
135142
where TPixel : unmanaged, IPixel<TPixel>
136143
{
144+
if (this == default)
145+
{
146+
ThrowDefaultInstance();
147+
}
148+
137149
int spread = CalculatePaletteSpread(processor.Palette.Length);
138150
float scale = processor.DitherScale;
139151

@@ -201,5 +213,9 @@ public bool Equals(IDither other)
201213
[MethodImpl(InliningOptions.ShortMethod)]
202214
public override int GetHashCode()
203215
=> HashCode.Combine(this.thresholdMatrix, this.modulusX, this.modulusY);
216+
217+
[MethodImpl(InliningOptions.ColdPath)]
218+
private static void ThrowDefaultInstance()
219+
=> throw new ImageProcessingException("Cannot use the default value type instance to dither.");
204220
}
205221
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
1111
/// </summary>
1212
public class OctreeQuantizer : IQuantizer
1313
{
14-
private static readonly QuantizerOptions DefaultOptions = new QuantizerOptions();
15-
1614
/// <summary>
1715
/// Initializes a new instance of the <see cref="OctreeQuantizer"/> class
1816
/// using the default <see cref="QuantizerOptions"/>.
1917
/// </summary>
2018
public OctreeQuantizer()
21-
: this(DefaultOptions)
19+
: this(new QuantizerOptions())
2220
{
2321
}
2422

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
1111
/// </summary>
1212
public class PaletteQuantizer : IQuantizer
1313
{
14-
private static readonly QuantizerOptions DefaultOptions = new QuantizerOptions();
1514
private readonly ReadOnlyMemory<Color> colorPalette;
1615

1716
/// <summary>
1817
/// Initializes a new instance of the <see cref="PaletteQuantizer"/> class.
1918
/// </summary>
2019
/// <param name="palette">The color palette.</param>
2120
public PaletteQuantizer(ReadOnlyMemory<Color> palette)
22-
: this(palette, DefaultOptions)
21+
: this(palette, new QuantizerOptions())
2322
{
2423
}
2524

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4-
using SixLabors.ImageSharp.Processing.Processors.Dithering;
5-
64
namespace SixLabors.ImageSharp.Processing.Processors.Quantization
75
{
86
/// <summary>
97
/// A palette quantizer consisting of web safe colors as defined in the CSS Color Module Level 4.
108
/// </summary>
119
public class WebSafePaletteQuantizer : PaletteQuantizer
1210
{
13-
private static readonly QuantizerOptions DefaultOptions = new QuantizerOptions();
14-
1511
/// <summary>
1612
/// Initializes a new instance of the <see cref="WebSafePaletteQuantizer" /> class.
1713
/// </summary>
1814
public WebSafePaletteQuantizer()
19-
: this(DefaultOptions)
15+
: this(new QuantizerOptions())
2016
{
2117
}
2218

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
99
/// </summary>
1010
public class WernerPaletteQuantizer : PaletteQuantizer
1111
{
12-
private static readonly QuantizerOptions DefaultOptions = new QuantizerOptions();
13-
1412
/// <summary>
1513
/// Initializes a new instance of the <see cref="WernerPaletteQuantizer" /> class.
1614
/// </summary>
1715
public WernerPaletteQuantizer()
18-
: this(DefaultOptions)
16+
: this(new QuantizerOptions())
1917
{
2018
}
2119

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
1010
/// </summary>
1111
public class WuQuantizer : IQuantizer
1212
{
13-
private static readonly QuantizerOptions DefaultOptions = new QuantizerOptions();
14-
1513
/// <summary>
1614
/// Initializes a new instance of the <see cref="WuQuantizer"/> class
1715
/// using the default <see cref="QuantizerOptions"/>.
1816
/// </summary>
1917
public WuQuantizer()
20-
: this(DefaultOptions)
18+
: this(new QuantizerOptions())
2119
{
2220
}
2321

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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using SixLabors.ImageSharp.PixelFormats;
77
using SixLabors.ImageSharp.Processing;
8+
using SixLabors.ImageSharp.Processing.Processors.Dithering;
89
using SixLabors.ImageSharp.Processing.Processors.Quantization;
910
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
1011
using Xunit;
@@ -152,6 +153,13 @@ public static readonly TheoryData<IQuantizer> DitherScaleQuantizers
152153
new WuQuantizer(OrderedDitherOptions),
153154
};
154155

156+
public static readonly TheoryData<IDither> DefaultInstanceDitherers
157+
= new TheoryData<IDither>
158+
{
159+
default(ErrorDither),
160+
default(OrderedDither)
161+
};
162+
155163
private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.05F);
156164

157165
[Theory]
@@ -217,5 +225,20 @@ public void ApplyQuantizationWithDitheringScale<TPixel>(TestImageProvider<TPixel
217225
testOutputDetails: testOutputDetails,
218226
appendPixelTypeToFileName: false);
219227
}
228+
229+
[Theory]
230+
[MemberData(nameof(DefaultInstanceDitherers))]
231+
public void ShouldThrowForDefaultDitherInstance(IDither dither)
232+
{
233+
void Command()
234+
{
235+
using var image = new Image<Rgba32>(10, 10);
236+
var quantizer = new WebSafePaletteQuantizer();
237+
quantizer.Options.Dither = dither;
238+
image.Mutate(x => x.Quantize(quantizer));
239+
}
240+
241+
Assert.Throws<ImageProcessingException>(Command);
242+
}
220243
}
221244
}

0 commit comments

Comments
 (0)