Skip to content

Commit fc7219d

Browse files
Add quantizer property tests
1 parent d196b22 commit fc7219d

File tree

4 files changed

+95
-78
lines changed

4 files changed

+95
-78
lines changed

tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,20 @@ public class BmpEncoderTests
4646
{ Bit32Rgb, BmpBitsPerPixel.Pixel32 }
4747
};
4848

49+
[Fact]
50+
public void BmpEncoderDefaultInstanceHasQuantizer() => Assert.NotNull(BmpEncoder.Quantizer);
51+
4952
[Theory]
5053
[MemberData(nameof(RatioFiles))]
5154
public void Encode_PreserveRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
5255
{
53-
var testFile = TestFile.Create(imagePath);
56+
TestFile testFile = TestFile.Create(imagePath);
5457
using Image<Rgba32> input = testFile.CreateRgba32Image();
55-
using var memStream = new MemoryStream();
58+
using MemoryStream memStream = new();
5659
input.Save(memStream, BmpEncoder);
5760

5861
memStream.Position = 0;
59-
using var output = Image.Load<Rgba32>(memStream);
62+
using Image<Rgba32> output = Image.Load<Rgba32>(memStream);
6063
ImageMetadata meta = output.Metadata;
6164
Assert.Equal(xResolution, meta.HorizontalResolution);
6265
Assert.Equal(yResolution, meta.VerticalResolution);
@@ -67,13 +70,13 @@ public void Encode_PreserveRatio(string imagePath, int xResolution, int yResolut
6770
[MemberData(nameof(BmpBitsPerPixelFiles))]
6871
public void Encode_PreserveBitsPerPixel(string imagePath, BmpBitsPerPixel bmpBitsPerPixel)
6972
{
70-
var testFile = TestFile.Create(imagePath);
73+
TestFile testFile = TestFile.Create(imagePath);
7174
using Image<Rgba32> input = testFile.CreateRgba32Image();
72-
using var memStream = new MemoryStream();
75+
using MemoryStream memStream = new();
7376
input.Save(memStream, BmpEncoder);
7477

7578
memStream.Position = 0;
76-
using var output = Image.Load<Rgba32>(memStream);
79+
using Image<Rgba32> output = Image.Load<Rgba32>(memStream);
7780
BmpMetadata meta = output.Metadata.GetBmpMetadata();
7881

7982
Assert.Equal(bmpBitsPerPixel, meta.BitsPerPixel);
@@ -196,16 +199,16 @@ public void Encode_2Bit_WithV3Header_Works<TPixel>(
196199
where TPixel : unmanaged, IPixel<TPixel>
197200
{
198201
// arrange
199-
var encoder = new BmpEncoder() { BitsPerPixel = bitsPerPixel };
200-
using var memoryStream = new MemoryStream();
202+
BmpEncoder encoder = new() { BitsPerPixel = bitsPerPixel };
203+
using MemoryStream memoryStream = new();
201204
using Image<TPixel> input = provider.GetImage(BmpDecoder.Instance);
202205

203206
// act
204207
encoder.Encode(input, memoryStream);
205208
memoryStream.Position = 0;
206209

207210
// assert
208-
using var actual = Image.Load<TPixel>(memoryStream);
211+
using Image<TPixel> actual = Image.Load<TPixel>(memoryStream);
209212
ImageSimilarityReport similarityReport = ImageComparer.Exact.CompareImagesOrFrames(input, actual);
210213
Assert.True(similarityReport.IsEmpty, "encoded image does not match reference image");
211214
}
@@ -218,16 +221,16 @@ public void Encode_2Bit_WithV4Header_Works<TPixel>(
218221
where TPixel : unmanaged, IPixel<TPixel>
219222
{
220223
// arrange
221-
var encoder = new BmpEncoder() { BitsPerPixel = bitsPerPixel };
222-
using var memoryStream = new MemoryStream();
224+
BmpEncoder encoder = new() { BitsPerPixel = bitsPerPixel };
225+
using MemoryStream memoryStream = new();
223226
using Image<TPixel> input = provider.GetImage(BmpDecoder.Instance);
224227

225228
// act
226229
encoder.Encode(input, memoryStream);
227230
memoryStream.Position = 0;
228231

229232
// assert
230-
using var actual = Image.Load<TPixel>(memoryStream);
233+
using Image<TPixel> actual = Image.Load<TPixel>(memoryStream);
231234
ImageSimilarityReport similarityReport = ImageComparer.Exact.CompareImagesOrFrames(input, actual);
232235
Assert.True(similarityReport.IsEmpty, "encoded image does not match reference image");
233236
}
@@ -266,7 +269,7 @@ public void Encode_8BitColor_WithWuQuantizer<TPixel>(TestImageProvider<TPixel> p
266269
}
267270

268271
using Image<TPixel> image = provider.GetImage();
269-
var encoder = new BmpEncoder
272+
BmpEncoder encoder = new()
270273
{
271274
BitsPerPixel = BmpBitsPerPixel.Pixel8,
272275
Quantizer = new WuQuantizer()
@@ -298,7 +301,7 @@ public void Encode_8BitColor_WithOctreeQuantizer<TPixel>(TestImageProvider<TPixe
298301
}
299302

300303
using Image<TPixel> image = provider.GetImage();
301-
var encoder = new BmpEncoder
304+
BmpEncoder encoder = new()
302305
{
303306
BitsPerPixel = BmpBitsPerPixel.Pixel8,
304307
Quantizer = new OctreeQuantizer()
@@ -333,11 +336,11 @@ public void Encode_PreservesColorProfile<TPixel>(TestImageProvider<TPixel> provi
333336
ImageSharp.Metadata.Profiles.Icc.IccProfile expectedProfile = input.Metadata.IccProfile;
334337
byte[] expectedProfileBytes = expectedProfile.ToByteArray();
335338

336-
using var memStream = new MemoryStream();
339+
using MemoryStream memStream = new();
337340
input.Save(memStream, new BmpEncoder());
338341

339342
memStream.Position = 0;
340-
using var output = Image.Load<Rgba32>(memStream);
343+
using Image<Rgba32> output = Image.Load<Rgba32>(memStream);
341344
ImageSharp.Metadata.Profiles.Icc.IccProfile actualProfile = output.Metadata.IccProfile;
342345
byte[] actualProfileBytes = actualProfile.ToByteArray();
343346

@@ -353,7 +356,7 @@ public void Encode_WorksWithSizeGreaterThen65k(int width, int height)
353356
Exception exception = Record.Exception(() =>
354357
{
355358
using Image image = new Image<Rgba32>(width, height);
356-
using var memStream = new MemoryStream();
359+
using MemoryStream memStream = new();
357360
image.Save(memStream, BmpEncoder);
358361
});
359362

@@ -387,7 +390,7 @@ private static void TestBmpEncoderCore<TPixel>(
387390
image.Mutate(c => c.MakeOpaque());
388391
}
389392

390-
var encoder = new BmpEncoder
393+
BmpEncoder encoder = new()
391394
{
392395
BitsPerPixel = bitsPerPixel,
393396
SupportTransparency = supportTransparency,

tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public GifEncoderTests()
3333
}
3434
}
3535

36+
[Fact]
37+
public void GifEncoderDefaultInstanceHasNullQuantizer() => Assert.Null(new GifEncoder().Quantizer);
38+
3639
[Theory]
3740
[WithTestPatternImages(100, 100, TestPixelTypes, false)]
3841
[WithTestPatternImages(100, 100, TestPixelTypes, false)]

tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ public static readonly TheoryData<PngCompressionLevel> CompressionLevels
9999
{ TestImages.Png.Ratio4x1, 4, 1, PixelResolutionUnit.AspectRatio }
100100
};
101101

102+
[Fact]
103+
public void PngEncoderDefaultInstanceHasNullQuantizer() => Assert.Null(PngEncoder.Quantizer);
104+
102105
[Theory]
103106
[WithFile(TestImages.Png.Palette8Bpp, nameof(PngColorTypes), PixelTypes.Rgba32)]
104107
[WithTestPatternImages(nameof(PngColorTypes), 48, 24, PixelTypes.Rgba32)]
@@ -129,8 +132,8 @@ public void IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provid
129132
PngFilterMethod.Adaptive,
130133
PngBitDepth.Bit8,
131134
interlaceMode,
132-
appendPixelType: true,
133-
appendPngColorType: true);
135+
appendPngColorType: true,
136+
appendPixelType: true);
134137
}
135138
}
136139

@@ -321,7 +324,7 @@ public void WritesFileMarker<TPixel>(TestImageProvider<TPixel> provider)
321324
where TPixel : unmanaged, IPixel<TPixel>
322325
{
323326
using Image<TPixel> image = provider.GetImage();
324-
using var ms = new MemoryStream();
327+
using MemoryStream ms = new();
325328
image.Save(ms, PngEncoder);
326329

327330
byte[] data = ms.ToArray().Take(8).ToArray();
@@ -344,13 +347,13 @@ public void WritesFileMarker<TPixel>(TestImageProvider<TPixel> provider)
344347
[MemberData(nameof(RatioFiles))]
345348
public void Encode_PreserveRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
346349
{
347-
var testFile = TestFile.Create(imagePath);
350+
TestFile testFile = TestFile.Create(imagePath);
348351
using Image<Rgba32> input = testFile.CreateRgba32Image();
349-
using var memStream = new MemoryStream();
352+
using MemoryStream memStream = new();
350353
input.Save(memStream, PngEncoder);
351354

352355
memStream.Position = 0;
353-
using var output = Image.Load<Rgba32>(memStream);
356+
using Image<Rgba32> output = Image.Load<Rgba32>(memStream);
354357
ImageMetadata meta = output.Metadata;
355358
Assert.Equal(xResolution, meta.HorizontalResolution);
356359
Assert.Equal(yResolution, meta.VerticalResolution);
@@ -361,13 +364,13 @@ public void Encode_PreserveRatio(string imagePath, int xResolution, int yResolut
361364
[MemberData(nameof(PngBitDepthFiles))]
362365
public void Encode_PreserveBits(string imagePath, PngBitDepth pngBitDepth)
363366
{
364-
var testFile = TestFile.Create(imagePath);
367+
TestFile testFile = TestFile.Create(imagePath);
365368
using Image<Rgba32> input = testFile.CreateRgba32Image();
366-
using var memStream = new MemoryStream();
369+
using MemoryStream memStream = new();
367370
input.Save(memStream, PngEncoder);
368371

369372
memStream.Position = 0;
370-
using var output = Image.Load<Rgba32>(memStream);
373+
using Image<Rgba32> output = Image.Load<Rgba32>(memStream);
371374
PngMetadata meta = output.Metadata.GetPngMetadata();
372375

373376
Assert.Equal(pngBitDepth, meta.BitDepth);
@@ -380,8 +383,8 @@ public void Encode_PreserveBits(string imagePath, PngBitDepth pngBitDepth)
380383
public void Encode_WithPngTransparentColorBehaviorClear_Works(PngColorType colorType)
381384
{
382385
// arrange
383-
var image = new Image<Rgba32>(50, 50);
384-
var encoder = new PngEncoder()
386+
Image<Rgba32> image = new(50, 50);
387+
PngEncoder encoder = new()
385388
{
386389
TransparentColorMode = PngTransparentColorMode.Clear,
387390
ColorType = colorType
@@ -391,7 +394,7 @@ public void Encode_WithPngTransparentColorBehaviorClear_Works(PngColorType color
391394
{
392395
for (int y = 0; y < image.Height; y++)
393396
{
394-
System.Span<Rgba32> rowSpan = accessor.GetRowSpan(y);
397+
Span<Rgba32> rowSpan = accessor.GetRowSpan(y);
395398

396399
// Half of the test image should be transparent.
397400
if (y > 25)
@@ -407,12 +410,12 @@ public void Encode_WithPngTransparentColorBehaviorClear_Works(PngColorType color
407410
});
408411

409412
// act
410-
using var memStream = new MemoryStream();
413+
using MemoryStream memStream = new();
411414
image.Save(memStream, encoder);
412415

413416
// assert
414417
memStream.Position = 0;
415-
using var actual = Image.Load<Rgba32>(memStream);
418+
using Image<Rgba32> actual = Image.Load<Rgba32>(memStream);
416419
Rgba32 expectedColor = Color.Blue;
417420
if (colorType is PngColorType.Grayscale or PngColorType.GrayscaleWithAlpha)
418421
{
@@ -424,7 +427,7 @@ public void Encode_WithPngTransparentColorBehaviorClear_Works(PngColorType color
424427
{
425428
for (int y = 0; y < accessor.Height; y++)
426429
{
427-
System.Span<Rgba32> rowSpan = accessor.GetRowSpan(y);
430+
Span<Rgba32> rowSpan = accessor.GetRowSpan(y);
428431

429432
if (y > 25)
430433
{
@@ -443,15 +446,15 @@ public void Encode_WithPngTransparentColorBehaviorClear_Works(PngColorType color
443446
[MemberData(nameof(PngTrnsFiles))]
444447
public void Encode_PreserveTrns(string imagePath, PngBitDepth pngBitDepth, PngColorType pngColorType)
445448
{
446-
var testFile = TestFile.Create(imagePath);
449+
TestFile testFile = TestFile.Create(imagePath);
447450
using Image<Rgba32> input = testFile.CreateRgba32Image();
448451
PngMetadata inMeta = input.Metadata.GetPngMetadata();
449452
Assert.True(inMeta.HasTransparency);
450453

451-
using var memStream = new MemoryStream();
454+
using MemoryStream memStream = new();
452455
input.Save(memStream, PngEncoder);
453456
memStream.Position = 0;
454-
using var output = Image.Load<Rgba32>(memStream);
457+
using Image<Rgba32> output = Image.Load<Rgba32>(memStream);
455458
PngMetadata outMeta = output.Metadata.GetPngMetadata();
456459
Assert.True(outMeta.HasTransparency);
457460

@@ -501,8 +504,8 @@ public void Encode_WorksWithDiscontiguousBuffers<TPixel>(TestImageProvider<TPixe
501504
PngFilterMethod.Adaptive,
502505
PngBitDepth.Bit8,
503506
interlaceMode,
504-
appendPixelType: true,
505-
appendPngColorType: true);
507+
appendPngColorType: true,
508+
appendPixelType: true);
506509
}
507510
}
508511

@@ -523,8 +526,8 @@ static void RunTest(string serialized)
523526
PngFilterMethod.Adaptive,
524527
PngBitDepth.Bit8,
525528
interlaceMode,
526-
appendPixelType: true,
527-
appendPngColorType: true);
529+
appendPngColorType: true,
530+
appendPixelType: true);
528531
}
529532
}
530533

@@ -538,8 +541,8 @@ static void RunTest(string serialized)
538541
public void EncodeFixesInvalidOptions()
539542
{
540543
// https://github.com/SixLabors/ImageSharp/issues/935
541-
using var ms = new MemoryStream();
542-
var testFile = TestFile.Create(TestImages.Png.Issue935);
544+
using MemoryStream ms = new();
545+
TestFile testFile = TestFile.Create(TestImages.Png.Issue935);
543546
using Image<Rgba32> image = testFile.CreateRgba32Image(PngDecoder.Instance);
544547

545548
image.Save(ms, new PngEncoder { ColorType = PngColorType.RgbWithAlpha });
@@ -563,7 +566,7 @@ private static void TestPngEncoderCore<TPixel>(
563566
where TPixel : unmanaged, IPixel<TPixel>
564567
{
565568
using Image<TPixel> image = provider.GetImage();
566-
var encoder = new PngEncoder
569+
PngEncoder encoder = new()
567570
{
568571
ColorType = pngColorType,
569572
FilterMethod = pngFilterMethod,
@@ -581,7 +584,7 @@ private static void TestPngEncoderCore<TPixel>(
581584
string pngBitDepthInfo = appendPngBitDepth ? bitDepth.ToString() : string.Empty;
582585
string pngInterlaceModeInfo = interlaceMode != PngInterlaceMode.None ? $"_{interlaceMode}" : string.Empty;
583586

584-
string debugInfo = $"{pngColorTypeInfo}{pngFilterMethodInfo}{compressionLevelInfo}{paletteSizeInfo}{pngBitDepthInfo}{pngInterlaceModeInfo}";
587+
string debugInfo = pngColorTypeInfo + pngFilterMethodInfo + compressionLevelInfo + paletteSizeInfo + pngBitDepthInfo + pngInterlaceModeInfo;
585588

586589
string actualOutputFile = provider.Utility.SaveTestOutputFile(image, "png", encoder, debugInfo, appendPixelType);
587590

0 commit comments

Comments
 (0)