Repository navigation
Expand file tree
/
Copy pathWebpEncoderTests.cs
More file actions
722 lines (617 loc) · 28.1 KB
/
Copy pathWebpEncoderTests.cs
File metadata and controls
722 lines (617 loc) · 28.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Formats.Webp;
using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Quantization;
using SixLabors.ImageSharp.Tests.TestUtilities;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs;
using static SixLabors.ImageSharp.Tests.TestImages.Webp;
namespace SixLabors.ImageSharp.Tests.Formats.Webp;
[Trait("Format", "Webp")]
public class WebpEncoderTests
{
private static string TestImageLossyFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, Lossy.NoFilter06);
/// <summary>
/// Selected EXIF parts are respected whether the lazy profile is installed before or after synchronization.
/// </summary>
/// <param name="reentrant">Whether the stream installs the profile after metadata synchronization.</param>
[Theory]
[InlineData(false)]
[InlineData(true)]
public void Encode_LazyExifProfile_AppliesSelectedParts(bool reentrant)
{
ExifProfile source = new();
source.SetValue(ExifTag.Make, "POC");
source.SetValue(ExifTag.GPSLatitudeRef, "N");
ExifProfile filteredLazy = new(source.ToByteArray())
{
Parts = ExifParts.IfdTags | ExifParts.ExifTags
};
using Image<Rgba32> image = new(1, 1);
using MemoryStream output = reentrant
? new SwapOnCanSeekStream(() => image.Metadata.ExifProfile = filteredLazy)
: new MemoryStream();
if (!reentrant)
{
image.Metadata.ExifProfile = filteredLazy;
}
image.SaveAsWebp(output);
output.Position = 0;
using Image decoded = Image.Load(output);
Assert.NotNull(decoded.Metadata.ExifProfile);
Assert.True(decoded.Metadata.ExifProfile.TryGetValue(ExifTag.Make, out IExifValue<string> make));
Assert.Equal("POC", make.Value);
Assert.False(decoded.Metadata.ExifProfile.TryGetValue(ExifTag.GPSLatitudeRef, out _));
}
/// <summary>
/// Replaces metadata at the stream capability check, after encoder synchronization has completed.
/// </summary>
private sealed class SwapOnCanSeekStream : MemoryStream
{
private Action callback;
/// <summary>
/// Initializes a stream that invokes the callback on its first capability check.
/// </summary>
/// <param name="callback">The metadata replacement callback.</param>
public SwapOnCanSeekStream(Action callback) => this.callback = callback;
/// <inheritdoc/>
public override bool CanSeek
{
get
{
Action action = this.callback;
this.callback = null;
action?.Invoke();
return base.CanSeek;
}
}
}
[Theory]
[WithFile(Lossless.Animated, PixelTypes.Rgba32)]
public void Encode_AnimatedLossless<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage();
WebpEncoder encoder = new()
{
FileFormat = WebpFileFormatType.Lossless,
Quality = 100
};
// Always save as we need to compare the encoded output.
provider.Utility.SaveTestOutputFile(image, "webp", encoder);
// Compare encoded result
image.VerifyEncoder(provider, "webp", string.Empty, encoder);
}
[Theory]
[WithFile(Lossy.Animated, PixelTypes.Rgba32)]
[WithFile(Lossy.AnimatedLandscape, PixelTypes.Rgba32)]
public void Encode_AnimatedLossy<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage();
WebpEncoder encoder = new()
{
FileFormat = WebpFileFormatType.Lossy,
Quality = 100
};
// Always save as we need to compare the encoded output.
provider.Utility.SaveTestOutputFile(image, "webp", encoder);
// Compare encoded result
// The reference decoder seems to produce differences up to 0.1% but the input/output have been
// checked to be correct.
string path = provider.Utility.GetTestOutputFileName("webp", null, true);
using Image<Rgba32> encoded = Image.Load<Rgba32>(path);
encoded.CompareToReferenceOutput(ImageComparer.Tolerant(0.01f), provider, null, "webp");
}
[Theory]
[WithFile(TestImages.Gif.Leo, PixelTypes.Rgba32)]
public void Encode_AnimatedFormatTransform_FromGif<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
if (TestEnvironment.RunsOnCI && !TestEnvironment.IsWindows)
{
return;
}
using Image<TPixel> image = provider.GetImage(GifDecoder.Instance);
using MemoryStream memStream = new();
image.Save(memStream, new WebpEncoder());
memStream.Position = 0;
using Image<TPixel> output = Image.Load<TPixel>(memStream);
ImageComparer.Exact.VerifySimilarity(output, image);
GifMetadata gif = image.Metadata.GetGifMetadata();
WebpMetadata webp = output.Metadata.GetWebpMetadata();
Assert.Equal(gif.RepeatCount, webp.RepeatCount);
for (int i = 0; i < image.Frames.Count; i++)
{
GifFrameMetadata gifF = image.Frames[i].Metadata.GetGifMetadata();
WebpFrameMetadata webpF = output.Frames[i].Metadata.GetWebpMetadata();
Assert.Equal(gifF.FrameDelay, (int)(webpF.FrameDelay / 10));
switch (gifF.DisposalMode)
{
case FrameDisposalMode.RestoreToBackground:
Assert.Equal(FrameDisposalMode.RestoreToBackground, webpF.DisposalMode);
break;
case FrameDisposalMode.RestoreToPrevious:
case FrameDisposalMode.Unspecified:
case FrameDisposalMode.DoNotDispose:
default:
Assert.Equal(FrameDisposalMode.DoNotDispose, webpF.DisposalMode);
break;
}
}
}
[Theory]
// [WithFile(AlphaBlend, PixelTypes.Rgba32)]
// [WithFile(AlphaBlend2, PixelTypes.Rgba32)]
[WithFile(AlphaBlend3, PixelTypes.Rgba32)]
// [WithFile(AlphaBlend4, PixelTypes.Rgba32)]
public void Encode_AlphaBlended<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage();
WebpEncoder encoder = new()
{
FileFormat = WebpFileFormatType.Lossless
};
QuantizerOptions options = new()
{
TransparencyThreshold = 128 / 255F
};
// First save as gif to gif using different quantizers with default options.
// Alpha thresholding is 64/255F.
GifEncoder gifEncoder = new()
{
Quantizer = new HexadecatreeQuantizer(options)
};
provider.Utility.SaveTestOutputFile(image, "gif", gifEncoder, "hexadecatree");
gifEncoder = new GifEncoder
{
Quantizer = new WuQuantizer(options)
};
provider.Utility.SaveTestOutputFile(image, "gif", gifEncoder, "wu");
// Now clone and quantize the image using the same quantizers without alpha thresholding and save as webp.
options = new QuantizerOptions
{
TransparencyThreshold = 0
};
using Image<TPixel> cloned1 = image.Clone();
cloned1.Mutate(c => c.Quantize(new HexadecatreeQuantizer(options)));
provider.Utility.SaveTestOutputFile(cloned1, "webp", encoder, "hexadecatree");
using Image<TPixel> cloned2 = image.Clone();
cloned2.Mutate(c => c.Quantize(new WuQuantizer(options)));
provider.Utility.SaveTestOutputFile(cloned2, "webp", encoder, "wu");
// Now blend the images with a blue background and save as webp.
using Image<Rgba32> background1 = new(image.Width, image.Height, Color.White.ToPixel<Rgba32>());
background1.Mutate(c => c.DrawImage(cloned1, 1));
provider.Utility.SaveTestOutputFile(background1, "webp", encoder, "hexadecatree-blended");
using Image<Rgba32> background2 = new(image.Width, image.Height, Color.White.ToPixel<Rgba32>());
background2.Mutate(c => c.DrawImage(cloned2, 1));
provider.Utility.SaveTestOutputFile(background2, "webp", encoder, "wu-blended");
}
[Theory]
[WithFile(TestImages.Png.APng, PixelTypes.Rgba32)]
public void Encode_AnimatedFormatTransform_FromPng<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
if (TestEnvironment.RunsOnCI && !TestEnvironment.IsWindows)
{
return;
}
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
using MemoryStream memStream = new();
image.Save(memStream, new WebpEncoder());
memStream.Position = 0;
provider.Utility.SaveTestOutputFile(image, "gif", new GifEncoder());
provider.Utility.SaveTestOutputFile(image, "png", new PngEncoder());
provider.Utility.SaveTestOutputFile(image, "webp", new WebpEncoder());
using Image<TPixel> output = Image.Load<TPixel>(memStream);
ImageComparer.Exact.VerifySimilarity(output, image);
PngMetadata png = image.Metadata.GetPngMetadata();
WebpMetadata webp = output.Metadata.GetWebpMetadata();
Assert.Equal(png.RepeatCount, webp.RepeatCount);
for (int i = 0; i < image.Frames.Count; i++)
{
PngFrameMetadata pngF = image.Frames[i].Metadata.GetPngMetadata();
WebpFrameMetadata webpF = output.Frames[i].Metadata.GetWebpMetadata();
Assert.Equal((uint)(pngF.FrameDelay.ToDouble() * 1000), webpF.FrameDelay);
switch (pngF.BlendMode)
{
case FrameBlendMode.Source:
Assert.Equal(FrameBlendMode.Source, webpF.BlendMode);
break;
case FrameBlendMode.Over:
default:
Assert.Equal(FrameBlendMode.Over, webpF.BlendMode);
break;
}
switch (pngF.DisposalMode)
{
case FrameDisposalMode.RestoreToBackground:
Assert.Equal(FrameDisposalMode.RestoreToBackground, webpF.DisposalMode);
break;
case FrameDisposalMode.DoNotDispose:
default:
Assert.Equal(FrameDisposalMode.DoNotDispose, webpF.DisposalMode);
break;
}
}
}
[Theory]
[WithFile(TestImages.Jpeg.Baseline.Jpeg410, PixelTypes.Rgba32, WebpFileFormatType.Lossy)] // Input is lossy jpeg.
[WithFile(Flag, PixelTypes.Rgba32, WebpFileFormatType.Lossless)] // Input is lossless png.
[WithFile(Lossless.NoTransform1, PixelTypes.Rgba32, WebpFileFormatType.Lossless)]
[WithFile(Lossy.BikeWithExif, PixelTypes.Rgba32, WebpFileFormatType.Lossy)]
public void Encode_PreserveEncodingType<TPixel>(TestImageProvider<TPixel> provider, WebpFileFormatType expectedFormat)
where TPixel : unmanaged, IPixel<TPixel>
{
WebpEncoder options = new();
using Image<TPixel> input = provider.GetImage();
using MemoryStream memoryStream = new();
input.Save(memoryStream, options);
memoryStream.Position = 0;
using Image<Rgba32> output = Image.Load<Rgba32>(memoryStream);
ImageMetadata meta = output.Metadata;
WebpMetadata webpMetaData = meta.GetWebpMetadata();
Assert.Equal(expectedFormat, webpMetaData.FileFormat);
}
[Theory]
[WithFile(Flag, PixelTypes.Rgba32)]
[WithFile(TestImages.Png.PalettedTwoColor, PixelTypes.Rgba32)]
[WithFile(TestImages.Png.Paletted256Colors, PixelTypes.Rgba32)]
public void Encode_Lossless_WithPalette_Works<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
WebpEncoder encoder = new()
{
FileFormat = WebpFileFormatType.Lossless,
Quality = 100,
Method = WebpEncodingMethod.BestQuality
};
using Image<TPixel> image = provider.GetImage();
image.VerifyEncoder(provider, "webp", string.Empty, encoder);
}
[Theory]
[WithFile(TestImages.Bmp.Car, PixelTypes.Rgba32, 100)]
[WithFile(TestImages.Bmp.Car, PixelTypes.Rgba32, 80)]
[WithFile(TestImages.Bmp.Car, PixelTypes.Rgba32, 20)]
public void Encode_Lossless_WithDifferentQuality_Works<TPixel>(TestImageProvider<TPixel> provider, int quality)
where TPixel : unmanaged, IPixel<TPixel>
{
WebpEncoder encoder = new()
{
FileFormat = WebpFileFormatType.Lossless,
Quality = quality
};
using Image<TPixel> image = provider.GetImage();
string testOutputDetails = $"lossless_q{quality}";
image.VerifyEncoder(provider, "webp", testOutputDetails, encoder);
}
[Theory]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 0, 75)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 1, 75)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 2, 75)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 3, 75)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 4, 75)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 5, 75)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 6, 75)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 0, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 1, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 2, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 3, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 4, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 5, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 6, 100)]
public void Encode_Lossless_WithDifferentMethodAndQuality_Works<TPixel>(TestImageProvider<TPixel> provider, WebpEncodingMethod method, int quality)
where TPixel : unmanaged, IPixel<TPixel>
{
WebpEncoder encoder = new()
{
FileFormat = WebpFileFormatType.Lossless,
Method = method,
Quality = quality
};
using Image<TPixel> image = provider.GetImage();
string testOutputDetails = $"lossless_m{method}_q{quality}";
image.VerifyEncoder(provider, "webp", testOutputDetails, encoder);
}
[Theory]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 15114)]
public void Encode_Lossless_WithBestQuality_HasExpectedSize<TPixel>(TestImageProvider<TPixel> provider, int expectedBytes)
where TPixel : unmanaged, IPixel<TPixel>
{
WebpEncoder encoder = new()
{
FileFormat = WebpFileFormatType.Lossless,
Method = WebpEncodingMethod.BestQuality
};
using Image<TPixel> image = provider.GetImage();
using MemoryStream memoryStream = new();
image.Save(memoryStream, encoder);
Assert.Equal(memoryStream.Length, expectedBytes);
}
[Theory]
[WithFile(RgbTestPattern100x100, PixelTypes.Rgba32, 85)]
[WithFile(RgbTestPattern100x100, PixelTypes.Rgba32, 60)]
[WithFile(RgbTestPattern80x80, PixelTypes.Rgba32, 40)]
[WithFile(RgbTestPattern80x80, PixelTypes.Rgba32, 20)]
[WithFile(RgbTestPattern80x80, PixelTypes.Rgba32, 10)]
[WithFile(RgbTestPattern63x63, PixelTypes.Rgba32, 40)]
public void Encode_Lossless_WithNearLosslessFlag_Works<TPixel>(TestImageProvider<TPixel> provider, int nearLosslessQuality)
where TPixel : unmanaged, IPixel<TPixel>
{
WebpEncoder encoder = new()
{
FileFormat = WebpFileFormatType.Lossless,
NearLossless = true,
NearLosslessQuality = nearLosslessQuality
};
using Image<TPixel> image = provider.GetImage();
string testOutputDetails = $"nearlossless_q{nearLosslessQuality}";
image.VerifyEncoder(provider, "webp", testOutputDetails, encoder, customComparer: GetComparer(nearLosslessQuality));
}
[Theory]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 0)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 1)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 2)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 3)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 4)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 5)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 6)]
[WithFile(Lossy.Alpha1, PixelTypes.Rgba32, 4)]
public void Encode_Lossless_WithPreserveTransparentColor_Works<TPixel>(TestImageProvider<TPixel> provider, WebpEncodingMethod method)
where TPixel : unmanaged, IPixel<TPixel>
{
WebpEncoder encoder = new()
{
FileFormat = WebpFileFormatType.Lossless,
Method = method,
TransparentColorMode = TransparentColorMode.Preserve
};
using Image<TPixel> image = provider.GetImage();
string testOutputDetails = $"lossless_m{method}";
image.VerifyEncoder(provider, "webp", testOutputDetails, encoder);
}
[Fact]
public void Encode_Lossless_OneByOnePixel_Works()
{
// Just make sure, encoding 1 pixel by 1 pixel does not throw an exception.
using Image<Rgba32> image = new(1, 1);
WebpEncoder encoder = new() { FileFormat = WebpFileFormatType.Lossless };
using (MemoryStream memStream = new())
{
image.SaveAsWebp(memStream, encoder);
}
}
[Theory]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 75)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 20)]
public void Encode_Lossy_WithDifferentQuality_Works<TPixel>(TestImageProvider<TPixel> provider, int quality)
where TPixel : unmanaged, IPixel<TPixel>
{
WebpEncoder encoder = new()
{
FileFormat = WebpFileFormatType.Lossy,
Quality = quality
};
using Image<TPixel> image = provider.GetImage();
string testOutputDetails = $"lossy_q{quality}";
image.VerifyEncoder(provider, "webp", testOutputDetails, encoder, customComparer: GetComparer(quality));
}
[Theory]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 80)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 50)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 30)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 10)]
public void Encode_Lossy_WithDifferentFilterStrength_Works<TPixel>(TestImageProvider<TPixel> provider, int filterStrength)
where TPixel : unmanaged, IPixel<TPixel>
{
WebpEncoder encoder = new()
{
FileFormat = WebpFileFormatType.Lossy,
FilterStrength = filterStrength
};
using Image<TPixel> image = provider.GetImage();
string testOutputDetails = $"lossy_f{filterStrength}";
image.VerifyEncoder(provider, "webp", testOutputDetails, encoder, customComparer: GetComparer(75));
}
[Theory]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 80)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 50)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 30)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 10)]
public void Encode_Lossy_WithDifferentSpatialNoiseShapingStrength_Works<TPixel>(TestImageProvider<TPixel> provider, int snsStrength)
where TPixel : unmanaged, IPixel<TPixel>
{
WebpEncoder encoder = new()
{
FileFormat = WebpFileFormatType.Lossy,
SpatialNoiseShaping = snsStrength
};
using Image<TPixel> image = provider.GetImage();
string testOutputDetails = $"lossy_sns{snsStrength}";
image.VerifyEncoder(provider, "webp", testOutputDetails, encoder, customComparer: GetComparer(75));
}
[Theory]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 0, 75)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 1, 75)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 2, 75)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 3, 75)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 4, 75)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 5, 75)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 6, 75)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 0, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 1, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 2, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 3, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 4, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 5, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 6, 100)]
public void Encode_Lossy_WithDifferentMethodsAndQuality_Works<TPixel>(TestImageProvider<TPixel> provider, WebpEncodingMethod method, int quality)
where TPixel : unmanaged, IPixel<TPixel>
{
WebpEncoder encoder = new()
{
FileFormat = WebpFileFormatType.Lossy,
Method = method,
Quality = quality
};
using Image<TPixel> image = provider.GetImage();
string testOutputDetails = $"lossy_m{method}_q{quality}";
image.VerifyEncoder(provider, "webp", testOutputDetails, encoder, customComparer: GetComparer(quality));
}
[Theory]
[WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32)]
public void Encode_Lossy_WithAlpha_Works<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
// All quantizer paths apply the same luma AC sharpening. This stored fixture therefore uses
// one size bound for the scalar, SSE4.1 and AVX2 paths.
const int expectedFileSize = 64060;
WebpEncoder encoder = new()
{
FileFormat = WebpFileFormatType.Lossy,
UseAlphaCompression = false
};
using Image<TPixel> image = provider.GetImage();
string encodedFile = image.VerifyEncoder(
provider,
"webp",
"with_alpha",
encoder,
ImageComparer.Tolerant(0.04f),
referenceDecoder: MagickReferenceDecoder.WebP);
int encodedBytes = File.ReadAllBytes(encodedFile).Length;
Assert.True(encodedBytes <= expectedFileSize, $"encoded bytes are {encodedBytes} and should be smaller then expected file size of {expectedFileSize}");
}
[Theory]
[WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32)]
public void Encode_Lossy_WithAlphaUsingCompression_Works<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
// All quantizer paths apply the same luma AC sharpening. This stored fixture therefore uses
// one size bound for the scalar, SSE4.1 and AVX2 paths.
const int expectedFileSize = 16240;
WebpEncoder encoder = new()
{
FileFormat = WebpFileFormatType.Lossy,
UseAlphaCompression = true
};
using Image<TPixel> image = provider.GetImage();
string encodedFile = image.VerifyEncoder(
provider,
"webp",
"with_alpha_compressed",
encoder,
ImageComparer.Tolerant(0.04f),
referenceDecoder: MagickReferenceDecoder.WebP);
int encodedBytes = File.ReadAllBytes(encodedFile).Length;
Assert.True(encodedBytes <= expectedFileSize, $"encoded bytes are {encodedBytes} and should be smaller then expected file size of {expectedFileSize}");
}
[Theory]
[WithFile(TestPatternOpaque, PixelTypes.Rgba32)]
[WithFile(TestPatternOpaqueSmall, PixelTypes.Rgba32)]
public void Encode_Lossless_WorksWithTestPattern<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage();
WebpEncoder encoder = new() { FileFormat = WebpFileFormatType.Lossless };
image.VerifyEncoder(provider, "webp", string.Empty, encoder);
}
[Theory]
[WithFile(TestPatternOpaque, PixelTypes.Rgba32)]
[WithFile(TestPatternOpaqueSmall, PixelTypes.Rgba32)]
public void Encode_Lossy_WorksWithTestPattern<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage();
WebpEncoder encoder = new() { FileFormat = WebpFileFormatType.Lossy };
image.VerifyEncoder(provider, "webp", string.Empty, encoder, ImageComparer.Tolerant(0.04f));
}
// https://github.com/SixLabors/ImageSharp/issues/2763
[Theory]
[WithFile(Lossy.Issue2763, PixelTypes.Rgba32)]
public void WebpDecoder_CanDecode_Issue2763<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
WebpEncoder encoder = new()
{
Quality = 84,
FileFormat = WebpFileFormatType.Lossless
};
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
image.DebugSave(provider);
image.VerifyEncoder(provider, "webp", string.Empty, encoder);
}
// https://github.com/SixLabors/ImageSharp/issues/2801
[Theory]
[WithFile(Lossy.Issue2801, PixelTypes.Rgba32)]
public void WebpDecoder_CanDecode_Issue2801<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
WebpEncoder encoder = new()
{
Quality = 100
};
using Image<TPixel> image = provider.GetImage();
image.VerifyEncoder(provider, "webp", string.Empty, encoder, ImageComparer.TolerantPercentage(0.0994F));
}
public static void RunEncodeLossy_WithPeakImage()
{
TestImageProvider<Rgba32> provider = TestImageProvider<Rgba32>.File(TestImageLossyFullPath);
using Image<Rgba32> image = provider.GetImage();
WebpEncoder encoder = new() { FileFormat = WebpFileFormatType.Lossy };
image.VerifyEncoder(provider, "webp", string.Empty, encoder, ImageComparer.Tolerant(0.04f));
}
[Fact]
public void RunEncodeLossy_WithPeakImage_WithHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunEncodeLossy_WithPeakImage, HwIntrinsics.AllowAll);
[Fact]
public void RunEncodeLossy_WithPeakImage_WithoutHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunEncodeLossy_WithPeakImage, HwIntrinsics.DisableHWIntrinsic);
[Theory]
[WithFile(TestPatternOpaque, PixelTypes.Rgba32)]
public void CanSave_NonSeekableStream<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage();
WebpEncoder encoder = new();
using MemoryStream seekable = new();
image.Save(seekable, encoder);
using MemoryStream memoryStream = new();
using NonSeekableStream nonSeekable = new(memoryStream);
image.Save(nonSeekable, encoder);
Assert.True(seekable.ToArray().SequenceEqual(memoryStream.ToArray()));
}
[Theory]
[WithFile(TestPatternOpaque, PixelTypes.Rgba32)]
public async Task CanSave_NonSeekableStream_Async<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage();
WebpEncoder encoder = new();
await using MemoryStream seekable = new();
image.Save(seekable, encoder);
await using MemoryStream memoryStream = new();
await using NonSeekableStream nonSeekable = new(memoryStream);
await image.SaveAsync(nonSeekable, encoder);
Assert.True(seekable.ToArray().SequenceEqual(memoryStream.ToArray()));
}
private static ImageComparer GetComparer(int quality)
{
float tolerance = 0.01f; // ~1.0%
if (quality < 30)
{
tolerance = 0.02f; // ~2.0%
}
return ImageComparer.Tolerant(tolerance);
}
}