forked from SixLabors/ImageSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResizeTests.cs
More file actions
687 lines (598 loc) · 28.8 KB
/
ResizeTests.cs
File metadata and controls
687 lines (598 loc) · 28.8 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
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Transforms;
using SixLabors.ImageSharp.Tests.Memory;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using Xunit;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
{
[Trait("Category", "Processors")]
public class ResizeTests
{
private const PixelTypes CommonNonDefaultPixelTypes =
PixelTypes.Rgba32 | PixelTypes.Bgra32 | PixelTypes.Bgr24 | PixelTypes.RgbaVector;
public static readonly string[] AllResamplerNames = TestUtils.GetAllResamplerNames();
public static readonly string[] CommonTestImages = { TestImages.Png.CalliphoraPartial };
public static readonly string[] SmokeTestResamplerNames =
{
nameof(KnownResamplers.NearestNeighbor),
nameof(KnownResamplers.Bicubic),
nameof(KnownResamplers.Box),
nameof(KnownResamplers.Lanczos5),
};
private static readonly ImageComparer ValidatorComparer =
ImageComparer.TolerantPercentage(TestEnvironment.IsMacOS && TestEnvironment.RunsOnCI ? 0.26F : 0.07F);
[Fact]
public void Resize_PixelAgnostic()
{
string filePath = TestFile.GetInputFileFullPath(TestImages.Jpeg.Baseline.Calliphora);
using (var image = Image.Load(filePath))
{
image.Mutate(x => x.Resize(image.Size() / 2));
string path = System.IO.Path.Combine(
TestEnvironment.CreateOutputDirectory(nameof(ResizeTests)),
nameof(this.Resize_PixelAgnostic) + ".png");
image.Save(path);
}
}
[Theory(Skip = "Debug only, enable manually")]
[WithTestPatternImages(4000, 4000, PixelTypes.Rgba32, 300, 1024)]
[WithTestPatternImages(3032, 3032, PixelTypes.Rgba32, 400, 1024)]
[WithTestPatternImages(3032, 3032, PixelTypes.Rgba32, 400, 128)]
public void LargeImage<TPixel>(TestImageProvider<TPixel> provider, int destSize, int workingBufferSizeHintInKilobytes)
where TPixel : unmanaged, IPixel<TPixel>
{
if (!TestEnvironment.Is64BitProcess)
{
return;
}
provider.Configuration.WorkingBufferSizeHintInBytes = workingBufferSizeHintInKilobytes * 1024;
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.Resize(destSize, destSize));
image.DebugSave(provider, appendPixelTypeToFileName: false);
}
}
[Theory]
[WithBasicTestPatternImages(15, 12, PixelTypes.Rgba32, 2, 3, 1, 2)]
[WithBasicTestPatternImages(2, 256, PixelTypes.Rgba32, 1, 1, 1, 8)]
[WithBasicTestPatternImages(2, 32, PixelTypes.Rgba32, 1, 1, 1, 2)]
public void Resize_BasicSmall<TPixel>(TestImageProvider<TPixel> provider, int wN, int wD, int hN, int hD)
where TPixel : unmanaged, IPixel<TPixel>
{
// Basic test case, very helpful for debugging
// [WithBasicTestPatternImages(15, 12, PixelTypes.Rgba32, 2, 3, 1, 2)] means:
// resizing: (15, 12) -> (10, 6)
// kernel dimensions: (3, 4)
using (Image<TPixel> image = provider.GetImage())
{
var destSize = new Size(image.Width * wN / wD, image.Height * hN / hD);
image.Mutate(x => x.Resize(destSize, KnownResamplers.Bicubic, false));
FormattableString outputInfo = $"({wN}÷{wD},{hN}÷{hD})";
image.DebugSave(provider, outputInfo, appendPixelTypeToFileName: false);
image.CompareToReferenceOutput(provider, outputInfo, appendPixelTypeToFileName: false);
}
}
private static readonly int SizeOfVector4 = Unsafe.SizeOf<Vector4>();
[Theory]
[WithTestPatternImages(100, 100, PixelTypes.Rgba32, 50)]
[WithTestPatternImages(100, 100, PixelTypes.Rgba32, 60)]
[WithTestPatternImages(100, 400, PixelTypes.Rgba32, 110)]
[WithTestPatternImages(79, 97, PixelTypes.Rgba32, 73)]
[WithTestPatternImages(79, 97, PixelTypes.Rgba32, 5)]
[WithTestPatternImages(47, 193, PixelTypes.Rgba32, 73)]
[WithTestPatternImages(23, 211, PixelTypes.Rgba32, 31)]
public void WorkingBufferSizeHintInBytes_IsAppliedCorrectly<TPixel>(
TestImageProvider<TPixel> provider,
int workingBufferLimitInRows)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image0 = provider.GetImage())
{
Size destSize = image0.Size() / 4;
var configuration = Configuration.CreateDefaultInstance();
int workingBufferSizeHintInBytes = workingBufferLimitInRows * destSize.Width * SizeOfVector4;
var allocator = new TestMemoryAllocator();
allocator.EnableNonThreadSafeLogging();
configuration.MemoryAllocator = allocator;
configuration.WorkingBufferSizeHintInBytes = workingBufferSizeHintInBytes;
var verticalKernelMap = ResizeKernelMap.Calculate<BicubicResampler>(
default,
destSize.Height,
image0.Height,
Configuration.Default.MemoryAllocator);
int minimumWorkerAllocationInBytes = verticalKernelMap.MaxDiameter * 2 * destSize.Width * SizeOfVector4;
verticalKernelMap.Dispose();
using (Image<TPixel> image = image0.Clone(configuration))
{
image.Mutate(x => x.Resize(destSize, KnownResamplers.Bicubic, false));
image.DebugSave(
provider,
testOutputDetails: workingBufferLimitInRows,
appendPixelTypeToFileName: false);
image.CompareToReferenceOutput(
ImageComparer.TolerantPercentage(0.004f),
provider,
testOutputDetails: workingBufferLimitInRows,
appendPixelTypeToFileName: false);
Assert.NotEmpty(allocator.AllocationLog);
int maxAllocationSize = allocator.AllocationLog.Where(
e => e.ElementType == typeof(Vector4)).Max(e => e.LengthInBytes);
Assert.True(maxAllocationSize <= Math.Max(workingBufferSizeHintInBytes, minimumWorkerAllocationInBytes));
}
}
}
[Theory]
[WithTestPatternImages(100, 100, PixelTypes.Rgba32, 100, 100)]
[WithTestPatternImages(200, 200, PixelTypes.Rgba32, 31, 73)]
[WithTestPatternImages(200, 200, PixelTypes.Rgba32, 73, 31)]
[WithTestPatternImages(200, 193, PixelTypes.Rgba32, 13, 17)]
[WithTestPatternImages(200, 193, PixelTypes.Rgba32, 79, 23)]
[WithTestPatternImages(200, 503, PixelTypes.Rgba32, 61, 33)]
public void WorksWithDiscoBuffers<TPixel>(
TestImageProvider<TPixel> provider,
int workingBufferLimitInRows,
int bufferCapacityInRows)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> expected = provider.GetImage();
int width = expected.Width;
Size destSize = expected.Size() / 4;
expected.Mutate(c => c.Resize(destSize, KnownResamplers.Bicubic, false));
// Replace configuration:
provider.Configuration = Configuration.CreateDefaultInstance();
// Note: when AllocatorCapacityInBytes < WorkingBufferSizeHintInBytes,
// ResizeProcessor is expected to use the minimum of the two values, when establishing the working buffer.
provider.LimitAllocatorBufferCapacity().InBytes(width * bufferCapacityInRows * SizeOfVector4);
provider.Configuration.WorkingBufferSizeHintInBytes = width * workingBufferLimitInRows * SizeOfVector4;
using Image<TPixel> actual = provider.GetImage();
actual.Mutate(c => c.Resize(destSize, KnownResamplers.Bicubic, false));
actual.DebugSave(provider, $"{workingBufferLimitInRows}-{bufferCapacityInRows}");
ImageComparer.Exact.VerifySimilarity(expected, actual);
}
[Theory]
[WithTestPatternImages(100, 100, PixelTypes.Rgba32)]
public void Resize_Compand<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.Resize(image.Size() / 2, true));
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider);
}
}
[Theory]
[WithFile(TestImages.Png.Kaboom, PixelTypes.Rgba32, false)]
[WithFile(TestImages.Png.Kaboom, PixelTypes.Rgba32, true)]
public void Resize_DoesNotBleedAlphaPixels<TPixel>(TestImageProvider<TPixel> provider, bool compand)
where TPixel : unmanaged, IPixel<TPixel>
{
string details = compand ? "Compand" : string.Empty;
provider.RunValidatingProcessorTest(
x => x.Resize(x.GetCurrentSize() / 2, compand),
details,
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: false);
}
[Theory]
[WithFile(TestImages.Png.Kaboom, PixelTypes.Rgba32, false)]
[WithFile(TestImages.Png.Kaboom, PixelTypes.Rgba32, true)]
public void Resize_PremultiplyAlpha<TPixel>(TestImageProvider<TPixel> provider, bool premultiplyAlpha)
where TPixel : unmanaged, IPixel<TPixel>
{
string details = premultiplyAlpha ? "On" : "Off";
provider.RunValidatingProcessorTest(
x =>
{
var resizeOptions = new ResizeOptions()
{
Size = x.GetCurrentSize() / 2,
Mode = ResizeMode.Crop,
Sampler = KnownResamplers.Bicubic,
Compand = false,
PremultiplyAlpha = premultiplyAlpha
};
x.Resize(resizeOptions);
},
details,
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: false);
}
[Theory]
[WithFile(TestImages.Gif.Giphy, PixelTypes.Rgba32)]
public void Resize_IsAppliedToAllFrames<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2, KnownResamplers.Bicubic));
// Comparer fights decoder with gif-s. Could not use CompareToReferenceOutput here :(
image.DebugSave(provider, extension: "gif");
}
}
[Theory]
[WithTestPatternImages(50, 50, CommonNonDefaultPixelTypes)]
public void Resize_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
provider.RunValidatingProcessorTest(x => x.Resize(x.GetCurrentSize() / 2), comparer: ValidatorComparer);
}
[Theory]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32)]
public void Resize_ThrowsForWrappedMemoryImage<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image0 = provider.GetImage())
{
Assert.True(image0.DangerousTryGetSinglePixelMemory(out Memory<TPixel> imageMem));
var mmg = TestMemoryManager<TPixel>.CreateAsCopyOf(imageMem.Span);
using (var image1 = Image.WrapMemory(mmg.Memory, image0.Width, image0.Height))
{
Assert.ThrowsAny<Exception>(
() => { image1.Mutate(x => x.Resize(image0.Width / 2, image0.Height / 2, true)); });
}
}
}
[Theory]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32 | PixelTypes.Rgb24, 1)]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32 | PixelTypes.Rgb24, 4)]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32 | PixelTypes.Rgb24, 8)]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32 | PixelTypes.Rgb24, -1)]
public void Resize_WorksWithAllParallelismLevels<TPixel>(
TestImageProvider<TPixel> provider,
int maxDegreeOfParallelism)
where TPixel : unmanaged, IPixel<TPixel>
{
provider.Configuration.MaxDegreeOfParallelism =
maxDegreeOfParallelism > 0 ? maxDegreeOfParallelism : Environment.ProcessorCount;
FormattableString details = $"MDP{maxDegreeOfParallelism}";
provider.RunValidatingProcessorTest(
x => x.Resize(x.GetCurrentSize() / 2),
details,
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: false);
}
[Theory]
[WithFileCollection(nameof(CommonTestImages), nameof(AllResamplerNames), PixelTypes.Rgba32 | PixelTypes.Rgb24, 0.5f, null, null)]
[WithFileCollection(
nameof(CommonTestImages),
nameof(SmokeTestResamplerNames),
PixelTypes.Rgba32 | PixelTypes.Rgb24,
0.3f,
null,
null)]
[WithFileCollection(
nameof(CommonTestImages),
nameof(SmokeTestResamplerNames),
PixelTypes.Rgba32 | PixelTypes.Rgb24,
1.8f,
null,
null)]
[WithTestPatternImages(nameof(SmokeTestResamplerNames), 100, 100, PixelTypes.Rgba32, 0.5f, null, null)]
[WithTestPatternImages(nameof(SmokeTestResamplerNames), 100, 100, PixelTypes.Rgba32, 1f, null, null)]
[WithTestPatternImages(nameof(SmokeTestResamplerNames), 50, 50, PixelTypes.Rgba32, 8f, null, null)]
[WithTestPatternImages(nameof(SmokeTestResamplerNames), 201, 199, PixelTypes.Rgba32, null, 100, 99)]
[WithTestPatternImages(nameof(SmokeTestResamplerNames), 301, 1180, PixelTypes.Rgba32, null, 300, 480)]
[WithTestPatternImages(nameof(SmokeTestResamplerNames), 49, 80, PixelTypes.Rgba32, null, 301, 100)]
public void Resize_WorksWithAllResamplers<TPixel>(
TestImageProvider<TPixel> provider,
string samplerName,
float? ratio,
int? specificDestWidth,
int? specificDestHeight)
where TPixel : unmanaged, IPixel<TPixel>
{
IResampler sampler = TestUtils.GetResampler(samplerName);
// NearestNeighbourResampler is producing slightly different results With classic .NET framework on 32bit
// most likely because of differences in numeric behavior.
// The difference is well visible when comparing output for
// Resize_WorksWithAllResamplers_TestPattern301x1180_NearestNeighbor-300x480.png
// TODO: Should we investigate this?
bool allowHigherInaccuracy = !TestEnvironment.Is64BitProcess
&& TestEnvironment.NetCoreVersion == null
&& sampler is NearestNeighborResampler;
var comparer = ImageComparer.TolerantPercentage(allowHigherInaccuracy ? 0.3f : 0.017f);
// Let's make the working buffer size non-default:
provider.Configuration.WorkingBufferSizeHintInBytes = 16 * 1024 * SizeOfVector4;
provider.RunValidatingProcessorTest(
ctx =>
{
SizeF newSize;
string destSizeInfo;
if (ratio.HasValue)
{
newSize = ctx.GetCurrentSize() * ratio.Value;
destSizeInfo = ratio.Value.ToString(System.Globalization.CultureInfo.InvariantCulture);
}
else
{
if (!specificDestWidth.HasValue || !specificDestHeight.HasValue)
{
throw new InvalidOperationException(
"invalid dimensional input for Resize_WorksWithAllResamplers!");
}
newSize = new SizeF(specificDestWidth.Value, specificDestHeight.Value);
destSizeInfo = $"{newSize.Width}x{newSize.Height}";
}
FormattableString testOutputDetails = $"{samplerName}-{destSizeInfo}";
ctx.Resize((Size)newSize, sampler, false);
return testOutputDetails;
},
comparer,
appendPixelTypeToFileName: false);
}
[Theory]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32 | PixelTypes.Rgb24)]
public void ResizeFromSourceRectangle<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var sourceRectangle = new Rectangle(
image.Width / 8,
image.Height / 8,
image.Width / 4,
image.Height / 4);
var destRectangle = new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2);
image.Mutate(
x => x.Resize(
image.Width,
image.Height,
KnownResamplers.Bicubic,
sourceRectangle,
destRectangle,
false));
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false);
}
}
[Theory]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32 | PixelTypes.Rgb24)]
public void ResizeHeightAndKeepAspect<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.Resize(0, image.Height / 3, false));
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false);
}
}
[Theory]
[WithTestPatternImages(10, 100, PixelTypes.Rgba32 | PixelTypes.Rgb24)]
public void ResizeHeightCannotKeepAspectKeepsOnePixel<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.Resize(0, 5));
Assert.Equal(1, image.Width);
Assert.Equal(5, image.Height);
}
}
[Theory]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32 | PixelTypes.Rgb24)]
public void ResizeWidthAndKeepAspect<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.Resize(image.Width / 3, 0, false));
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false);
}
}
[Theory]
[WithTestPatternImages(100, 10, PixelTypes.Rgba32 | PixelTypes.Rgb24)]
public void ResizeWidthCannotKeepAspectKeepsOnePixel<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.Resize(5, 0));
Assert.Equal(5, image.Width);
Assert.Equal(1, image.Height);
}
}
[Theory]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32 | PixelTypes.Rgb24)]
public void ResizeWithBoxPadMode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var options = new ResizeOptions
{
Size = new Size(image.Width + 200, image.Height + 200),
Mode = ResizeMode.BoxPad,
PadColor = Color.HotPink
};
image.Mutate(x => x.Resize(options));
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false);
}
}
[Theory]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32 | PixelTypes.Rgb24)]
public void ResizeWithCropHeightMode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var options = new ResizeOptions { Size = new Size(image.Width, image.Height / 2) };
image.Mutate(x => x.Resize(options));
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false);
}
}
[Theory]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32 | PixelTypes.Rgb24)]
public void ResizeWithCropWidthMode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var options = new ResizeOptions { Size = new Size(image.Width / 2, image.Height) };
image.Mutate(x => x.Resize(options));
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false);
}
}
[Theory]
[WithFile(TestImages.Jpeg.Issues.IncorrectResize1006, PixelTypes.Rgba32 | PixelTypes.Rgb24)]
public void CanResizeLargeImageWithCropMode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var options = new ResizeOptions
{
Size = new Size(480, 600),
Mode = ResizeMode.Crop
};
image.Mutate(x => x.Resize(options));
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false);
}
}
[Theory]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32 | PixelTypes.Rgb24)]
public void ResizeWithMaxMode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var options = new ResizeOptions { Size = new Size(300, 300), Mode = ResizeMode.Max };
image.Mutate(x => x.Resize(options));
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false);
}
}
[Theory]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32 | PixelTypes.Rgb24)]
public void ResizeWithMinMode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var options = new ResizeOptions
{
Size = new Size((int)Math.Round(image.Width * .75F), (int)Math.Round(image.Height * .95F)),
Mode = ResizeMode.Min
};
image.Mutate(x => x.Resize(options));
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false);
}
}
[Theory]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32 | PixelTypes.Rgb24)]
public void ResizeWithPadMode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var options = new ResizeOptions
{
Size = new Size(image.Width + 200, image.Height),
Mode = ResizeMode.Pad,
PadColor = Color.Lavender
};
image.Mutate(x => x.Resize(options));
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false);
}
}
[Theory]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32 | PixelTypes.Rgb24)]
public void ResizeWithStretchMode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var options = new ResizeOptions
{
Size = new Size(image.Width / 2, image.Height),
Mode = ResizeMode.Stretch
};
image.Mutate(x => x.Resize(options));
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false);
}
}
[Theory]
[WithFile(TestImages.Jpeg.Issues.ExifDecodeOutOfRange694, PixelTypes.Rgb24)]
[WithFile(TestImages.Jpeg.Issues.ExifGetString750Transform, PixelTypes.Rgb24)]
[WithFile(TestImages.Jpeg.Issues.ExifResize1049, PixelTypes.Rgb24)]
public void CanResizeExifIssueImages<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
// Test images are large so skip on 32bit for now.
if (!TestEnvironment.Is64BitProcess)
{
return;
}
using (Image<TPixel> image = provider.GetImage())
{
// Don't bother saving, we're testing the EXIF metadata updates.
image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2));
}
}
[Fact]
public void Issue1195()
{
using (var image = new Image<Rgba32>(2, 300))
{
var size = new Size(50, 50);
image.Mutate(x => x
.Resize(
new ResizeOptions
{
Size = size,
Mode = ResizeMode.Max
}));
}
}
[Theory]
[InlineData(1, 1)]
[InlineData(4, 6)]
[InlineData(2, 10)]
[InlineData(8, 1)]
[InlineData(3, 7)]
public void Issue1342(int width, int height)
{
using (var image = new Image<Rgba32>(1, 1))
{
var size = new Size(width, height);
image.Mutate(x => x
.Resize(
new ResizeOptions
{
Size = size,
Sampler = KnownResamplers.NearestNeighbor
}));
Assert.Equal(width, image.Width);
Assert.Equal(height, image.Height);
}
}
[Theory]
[WithBasicTestPatternImages(20, 20, PixelTypes.Rgba32)]
public void Issue1625_LimitedAllocator<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
provider.LimitAllocatorBufferCapacity().InBytes(1000);
provider.RunValidatingProcessorTest(
x => x.Resize(30, 30),
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: false);
}
}
}