Skip to content

Commit 4ef363d

Browse files
Only compare a subset of frames.
1 parent 949e6ad commit 4ef363d

File tree

259 files changed

+159
-777
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

259 files changed

+159
-777
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ public void Decode_VerifyAllFrames<TPixel>(TestImageProvider<TPixel> provider)
4040
public void Decode_Issue2450<TPixel>(TestImageProvider<TPixel> provider)
4141
where TPixel : unmanaged, IPixel<TPixel>
4242
{
43+
// Images have many frames, only compare a selection of them.
44+
static bool Predicate(int i, int _) => i % 8 == 0;
45+
4346
using Image<TPixel> image = provider.GetImage();
44-
image.DebugSaveMultiFrame(provider);
45-
image.CompareToReferenceOutputMultiFrame(provider, ImageComparer.Exact);
47+
image.DebugSaveMultiFrame(provider, predicate: Predicate);
48+
image.CompareToReferenceOutputMultiFrame(provider, ImageComparer.Exact, predicate: Predicate);
4649
}
4750

4851
[Theory]

tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,38 @@ public static ImageSimilarityReport<TPixelA, TPixelB> CompareImagesOrFrames<TPix
4646
public static IEnumerable<ImageSimilarityReport<TPixelA, TPixelB>> CompareImages<TPixelA, TPixelB>(
4747
this ImageComparer comparer,
4848
Image<TPixelA> expected,
49-
Image<TPixelB> actual)
49+
Image<TPixelB> actual,
50+
Func<int, int, bool> predicate = null)
5051
where TPixelA : unmanaged, IPixel<TPixelA>
5152
where TPixelB : unmanaged, IPixel<TPixelB>
5253
{
53-
var result = new List<ImageSimilarityReport<TPixelA, TPixelB>>();
54+
List<ImageSimilarityReport<TPixelA, TPixelB>> result = new();
5455

55-
if (expected.Frames.Count != actual.Frames.Count)
56+
int expectedFrameCount = actual.Frames.Count;
57+
if (predicate != null)
58+
{
59+
expectedFrameCount = 0;
60+
for (int i = 0; i < actual.Frames.Count; i++)
61+
{
62+
if (predicate(i, actual.Frames.Count))
63+
{
64+
expectedFrameCount++;
65+
}
66+
}
67+
}
68+
69+
if (expected.Frames.Count != expectedFrameCount)
5670
{
57-
throw new Exception("Frame count does not match!");
71+
throw new ImagesSimilarityException("Frame count does not match!");
5872
}
5973

6074
for (int i = 0; i < expected.Frames.Count; i++)
6175
{
76+
if (predicate != null && !predicate(i, expected.Frames.Count))
77+
{
78+
continue;
79+
}
80+
6281
ImageSimilarityReport<TPixelA, TPixelB> report = comparer.CompareImagesOrFrames(i, expected.Frames[i], actual.Frames[i]);
6382
if (!report.IsEmpty)
6483
{
@@ -72,7 +91,8 @@ public static IEnumerable<ImageSimilarityReport<TPixelA, TPixelB>> CompareImages
7291
public static void VerifySimilarity<TPixelA, TPixelB>(
7392
this ImageComparer comparer,
7493
Image<TPixelA> expected,
75-
Image<TPixelB> actual)
94+
Image<TPixelB> actual,
95+
Func<int, int, bool> predicate = null)
7696
where TPixelA : unmanaged, IPixel<TPixelA>
7797
where TPixelB : unmanaged, IPixel<TPixelB>
7898
{
@@ -81,12 +101,25 @@ public static void VerifySimilarity<TPixelA, TPixelB>(
81101
throw new ImageDimensionsMismatchException(expected.Size, actual.Size);
82102
}
83103

84-
if (expected.Frames.Count != actual.Frames.Count)
104+
int expectedFrameCount = actual.Frames.Count;
105+
if (predicate != null)
106+
{
107+
expectedFrameCount = 0;
108+
for (int i = 0; i < actual.Frames.Count; i++)
109+
{
110+
if (predicate(i, actual.Frames.Count))
111+
{
112+
expectedFrameCount++;
113+
}
114+
}
115+
}
116+
117+
if (expected.Frames.Count != expectedFrameCount)
85118
{
86119
throw new ImagesSimilarityException("Image frame count does not match!");
87120
}
88121

89-
IEnumerable<ImageSimilarityReport> reports = comparer.CompareImages(expected, actual);
122+
IEnumerable<ImageSimilarityReport> reports = comparer.CompareImages(expected, actual, predicate);
90123
if (reports.Any())
91124
{
92125
throw new ImageDifferenceIsOverThresholdException(reports);

tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ public IEnumerable<string> GetTestOutputFileNamesMultiFrame(
184184
string extension = null,
185185
object testOutputDetails = null,
186186
bool appendPixelTypeToFileName = true,
187-
bool appendSourceFileOrDescription = true)
187+
bool appendSourceFileOrDescription = true,
188+
Func<int, int, bool> predicate = null)
188189
{
189190
string baseDir = this.GetTestOutputFileName(string.Empty, testOutputDetails, appendPixelTypeToFileName, appendSourceFileOrDescription);
190191

@@ -195,8 +196,12 @@ public IEnumerable<string> GetTestOutputFileNamesMultiFrame(
195196

196197
for (int i = 0; i < frameCount; i++)
197198
{
198-
string filePath = $"{baseDir}/{i:D2}.{extension}";
199-
yield return filePath;
199+
if (predicate != null && !predicate(i, frameCount))
200+
{
201+
continue;
202+
}
203+
204+
yield return $"{baseDir}/{i:D2}.{extension}";
200205
}
201206
}
202207

@@ -205,27 +210,35 @@ public string[] SaveTestOutputFileMultiFrame<TPixel>(
205210
string extension = "png",
206211
IImageEncoder encoder = null,
207212
object testOutputDetails = null,
208-
bool appendPixelTypeToFileName = true)
213+
bool appendPixelTypeToFileName = true,
214+
Func<int, int, bool> predicate = null)
209215
where TPixel : unmanaged, IPixel<TPixel>
210216
{
211-
encoder = encoder ?? TestEnvironment.GetReferenceEncoder($"foo.{extension}");
217+
encoder ??= TestEnvironment.GetReferenceEncoder($"foo.{extension}");
212218

213219
string[] files = this.GetTestOutputFileNamesMultiFrame(
214220
image.Frames.Count,
215221
extension,
216222
testOutputDetails,
217-
appendPixelTypeToFileName).ToArray();
223+
appendPixelTypeToFileName,
224+
predicate: predicate).ToArray();
218225

219226
for (int i = 0; i < image.Frames.Count; i++)
220227
{
221-
using (Image<TPixel> frameImage = image.Frames.CloneFrame(i))
228+
if (predicate != null && !predicate(i, image.Frames.Count))
222229
{
223-
string filePath = files[i];
224-
using (FileStream stream = File.OpenWrite(filePath))
225-
{
226-
frameImage.Save(stream, encoder);
227-
}
230+
continue;
228231
}
232+
233+
if (i >= files.Length)
234+
{
235+
break;
236+
}
237+
238+
using Image<TPixel> frameImage = image.Frames.CloneFrame(i);
239+
string filePath = files[i];
240+
using FileStream stream = File.OpenWrite(filePath);
241+
frameImage.Save(stream, encoder);
229242
}
230243

231244
return files;
@@ -236,20 +249,17 @@ internal string GetReferenceOutputFileName(
236249
object testOutputDetails,
237250
bool appendPixelTypeToFileName,
238251
bool appendSourceFileOrDescription)
239-
{
240-
return TestEnvironment.GetReferenceOutputFileName(
252+
=> TestEnvironment.GetReferenceOutputFileName(
241253
this.GetTestOutputFileName(extension, testOutputDetails, appendPixelTypeToFileName, appendSourceFileOrDescription));
242-
}
243254

244255
public string[] GetReferenceOutputFileNamesMultiFrame(
245256
int frameCount,
246257
string extension,
247258
object testOutputDetails,
248-
bool appendPixelTypeToFileName = true)
249-
{
250-
return this.GetTestOutputFileNamesMultiFrame(frameCount, extension, testOutputDetails)
251-
.Select(TestEnvironment.GetReferenceOutputFileName).ToArray();
252-
}
259+
bool appendPixelTypeToFileName = true,
260+
Func<int, int, bool> predicate = null)
261+
=> this.GetTestOutputFileNamesMultiFrame(frameCount, extension, testOutputDetails, appendPixelTypeToFileName, predicate: predicate)
262+
.Select(TestEnvironment.GetReferenceOutputFileName).ToArray();
253263

254264
internal void Init(string typeName, string methodName, string outputSubfolderName)
255265
{

tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ public static Image DebugSave(
6767
provider.Utility.SaveTestOutputFile(
6868
image,
6969
extension,
70+
encoder: encoder,
7071
testOutputDetails: testOutputDetails,
7172
appendPixelTypeToFileName: appendPixelTypeToFileName,
72-
appendSourceFileOrDescription: appendSourceFileOrDescription,
73-
encoder: encoder);
73+
appendSourceFileOrDescription: appendSourceFileOrDescription);
7474
return image;
7575
}
7676

@@ -107,7 +107,8 @@ public static Image<TPixel> DebugSaveMultiFrame<TPixel>(
107107
ITestImageProvider provider,
108108
object testOutputDetails = null,
109109
string extension = "png",
110-
bool appendPixelTypeToFileName = true)
110+
bool appendPixelTypeToFileName = true,
111+
Func<int, int, bool> predicate = null)
111112
where TPixel : unmanaged, IPixel<TPixel>
112113
{
113114
if (TestEnvironment.RunsWithCodeCoverage)
@@ -119,7 +120,8 @@ public static Image<TPixel> DebugSaveMultiFrame<TPixel>(
119120
image,
120121
extension,
121122
testOutputDetails: testOutputDetails,
122-
appendPixelTypeToFileName: appendPixelTypeToFileName);
123+
appendPixelTypeToFileName: appendPixelTypeToFileName,
124+
predicate: predicate);
123125

124126
return image;
125127
}
@@ -237,7 +239,6 @@ public static Image<TPixel> CompareFirstFrameToReferenceOutput<TPixel>(
237239
ITestImageProvider provider,
238240
FormattableString testOutputDetails,
239241
string extension = "png",
240-
bool grayscale = false,
241242
bool appendPixelTypeToFileName = true,
242243
bool appendSourceFileOrDescription = true)
243244
where TPixel : unmanaged, IPixel<TPixel>
@@ -246,7 +247,6 @@ public static Image<TPixel> CompareFirstFrameToReferenceOutput<TPixel>(
246247
provider,
247248
(object)testOutputDetails,
248249
extension,
249-
grayscale,
250250
appendPixelTypeToFileName,
251251
appendSourceFileOrDescription);
252252

@@ -256,12 +256,11 @@ public static Image<TPixel> CompareFirstFrameToReferenceOutput<TPixel>(
256256
ITestImageProvider provider,
257257
object testOutputDetails = null,
258258
string extension = "png",
259-
bool grayscale = false,
260259
bool appendPixelTypeToFileName = true,
261260
bool appendSourceFileOrDescription = true)
262261
where TPixel : unmanaged, IPixel<TPixel>
263262
{
264-
using (var firstFrameOnlyImage = new Image<TPixel>(image.Width, image.Height))
263+
using (Image<TPixel> firstFrameOnlyImage = new(image.Width, image.Height))
265264
using (Image<TPixel> referenceImage = GetReferenceOutputImage<TPixel>(
266265
provider,
267266
testOutputDetails,
@@ -284,18 +283,19 @@ public static Image<TPixel> CompareToReferenceOutputMultiFrame<TPixel>(
284283
ImageComparer comparer,
285284
object testOutputDetails = null,
286285
string extension = "png",
287-
bool grayscale = false,
288-
bool appendPixelTypeToFileName = true)
286+
bool appendPixelTypeToFileName = true,
287+
Func<int, int, bool> predicate = null)
289288
where TPixel : unmanaged, IPixel<TPixel>
290289
{
291290
using (Image<TPixel> referenceImage = GetReferenceOutputImageMultiFrame<TPixel>(
292291
provider,
293292
image.Frames.Count,
294293
testOutputDetails,
295294
extension,
296-
appendPixelTypeToFileName))
295+
appendPixelTypeToFileName,
296+
predicate: predicate))
297297
{
298-
comparer.VerifySimilarity(referenceImage, image);
298+
comparer.VerifySimilarity(referenceImage, image, predicate);
299299
}
300300

301301
return image;
@@ -332,16 +332,18 @@ public static Image<TPixel> GetReferenceOutputImageMultiFrame<TPixel>(
332332
int frameCount,
333333
object testOutputDetails = null,
334334
string extension = "png",
335-
bool appendPixelTypeToFileName = true)
335+
bool appendPixelTypeToFileName = true,
336+
Func<int, int, bool> predicate = null)
336337
where TPixel : unmanaged, IPixel<TPixel>
337338
{
338339
string[] frameFiles = provider.Utility.GetReferenceOutputFileNamesMultiFrame(
339340
frameCount,
340341
extension,
341342
testOutputDetails,
342-
appendPixelTypeToFileName);
343+
appendPixelTypeToFileName,
344+
predicate);
343345

344-
var temporaryFrameImages = new List<Image<TPixel>>();
346+
List<Image<TPixel>> temporaryFrameImages = new();
345347

346348
IImageDecoder decoder = TestEnvironment.GetReferenceDecoder(frameFiles[0]);
347349

@@ -359,7 +361,7 @@ public static Image<TPixel> GetReferenceOutputImageMultiFrame<TPixel>(
359361

360362
Image<TPixel> firstTemp = temporaryFrameImages[0];
361363

362-
var result = new Image<TPixel>(firstTemp.Width, firstTemp.Height);
364+
Image<TPixel> result = new(firstTemp.Width, firstTemp.Height);
363365

364366
foreach (Image<TPixel> fi in temporaryFrameImages)
365367
{
Lines changed: 2 additions & 2 deletions
Loading

tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Issue2450_Rgba32_issue_2450.gif/01.png

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Issue2450_Rgba32_issue_2450.gif/02.png

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Issue2450_Rgba32_issue_2450.gif/03.png

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Issue2450_Rgba32_issue_2450.gif/04.png

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Issue2450_Rgba32_issue_2450.gif/05.png

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)