-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathImageSharpCompare.cs
More file actions
813 lines (723 loc) · 35.1 KB
/
ImageSharpCompare.cs
File metadata and controls
813 lines (723 loc) · 35.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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System;
using System.IO;
namespace Codeuctivity.ImageSharpCompare
{
/// <summary>
/// ImageSharpCompare, compares images.
/// Use this class to compare images using a third image as mask of regions where your two compared images may differ.
/// An alpha channel is ignored.
/// </summary>
#pragma warning disable CA1724 // Type names should not match namespaces - this is accepted for now to prevent a breaking change
public static class ImageSharpCompare
#pragma warning restore CA1724 // Type names should not match namespaces - this is accepted for now to prevent a breaking change
{
private const string sizeDiffersExceptionMessage = "Size of images differ.";
private static bool ImagesHaveSameDimension(Image actual, Image expected)
{
ArgumentNullException.ThrowIfNull(actual);
ArgumentNullException.ThrowIfNull(expected);
return actual.Height == expected.Height && actual.Width == expected.Width;
}
private static (Image<Rgb24>, Image<Rgb24>) GrowToSameDimension(Image<Rgb24> actual, Image<Rgb24> expected)
{
var biggestWidth = actual.Width > expected.Width ? actual.Width : expected.Width;
var biggestHeight = actual.Height > expected.Height ? actual.Height : expected.Height;
var grownExpected = expected.Clone();
var grownActual = actual.Clone();
grownActual.Mutate(x => x.Resize(biggestWidth, biggestHeight));
grownExpected.Mutate(x => x.Resize(biggestWidth, biggestHeight));
return (grownActual, grownExpected);
}
private static (Image<Rgb24>, Image<Rgb24>, Image<Rgb24>) GrowToSameDimension(Image<Rgb24> actual, Image<Rgb24> expected, Image<Rgb24> mask)
{
var biggestWidth = actual.Width > expected.Width ? actual.Width : expected.Width;
biggestWidth = biggestWidth > mask.Width ? biggestWidth : mask.Width;
var biggestHeight = actual.Height > expected.Height ? actual.Height : expected.Height;
biggestHeight = biggestHeight > mask.Height ? biggestHeight : mask.Height;
var grownExpected = expected.Clone();
var grownActual = actual.Clone();
var grownMask = mask.Clone();
grownActual.Mutate(x => x.Resize(biggestWidth, biggestHeight));
grownExpected.Mutate(x => x.Resize(biggestWidth, biggestHeight));
grownMask.Mutate(x => x.Resize(biggestWidth, biggestHeight));
return (grownActual, grownExpected, grownMask);
}
/// <summary>
/// Is true if width and height of both images are equal
/// </summary>
/// <param name="pathImageActual"></param>
/// <param name="pathImageExpected"></param>
/// <returns></returns>
public static bool ImagesHaveEqualSize(string pathImageActual, string pathImageExpected)
{
using var actualImage = Image.Load(pathImageActual);
using var expectedImage = Image.Load(pathImageExpected);
return ImagesHaveEqualSize(actualImage, expectedImage);
}
/// <summary>
/// Is true if width and height of both images are equal
/// </summary>
/// <param name="actual"></param>
/// <param name="expected"></param>
/// <returns></returns>
public static bool ImagesHaveEqualSize(Stream actual, Stream expected)
{
using var actualImage = Image.Load(actual);
using var expectedImage = Image.Load(expected);
return ImagesHaveEqualSize(actualImage, expectedImage);
}
/// <summary>
/// Is true if width and height of both images are equal
/// </summary>
/// <param name="actualImage"></param>
/// <param name="expectedImage"></param>
/// <returns></returns>
public static bool ImagesHaveEqualSize(Image actualImage, Image expectedImage)
{
return ImagesHaveSameDimension(actualImage, expectedImage);
}
/// <summary>
/// Compares two images for equivalence
/// </summary>
/// <param name="pathImageActual"></param>
/// <param name="pathImageExpected"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>True if every pixel of actual is equal to expected</returns>
public static bool ImagesAreEqual(string pathImageActual, string pathImageExpected, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
using var actualImage = Image.Load(pathImageActual);
using var expectedImage = Image.Load(pathImageExpected);
return ImagesAreEqual(actualImage, expectedImage, resizeOption, pixelColorShiftTolerance);
}
/// <summary>
/// Compares two images for equivalence
/// </summary>
/// <param name="actual"></param>
/// <param name="expected"></param>
/// <param name="resizeOption"></param>
/// <returns>True if every pixel of actual is equal to expected</returns>
public static bool ImagesAreEqual(Stream actual, Stream expected, ResizeOption resizeOption = ResizeOption.DontResize)
{
using var actualImage = Image.Load(actual);
using var expectedImage = Image.Load(expected);
return ImagesAreEqual(actualImage, expectedImage, resizeOption);
}
/// <summary>
/// Compares two images for equivalence
/// </summary>
/// <param name="actual"></param>
/// <param name="expected"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>True if every pixel of actual is equal to expected</returns>
public static bool ImagesAreEqual(Image actual, Image expected, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
ArgumentNullException.ThrowIfNull(actual);
ArgumentNullException.ThrowIfNull(expected);
var ownsActual = false;
var ownsExpected = false;
Image<Rgb24>? actualPixelAccessibleImage = null;
Image<Rgb24>? expectedPixelAccusableImage = null;
try
{
actualPixelAccessibleImage = ImageSharpPixelTypeConverter.ToRgb24Image(actual, out ownsActual);
expectedPixelAccusableImage = ImageSharpPixelTypeConverter.ToRgb24Image(expected, out ownsExpected);
return ImagesAreEqual(actualPixelAccessibleImage, expectedPixelAccusableImage, resizeOption, pixelColorShiftTolerance);
}
finally
{
if (ownsActual)
{
actualPixelAccessibleImage?.Dispose();
}
if (ownsExpected)
{
expectedPixelAccusableImage?.Dispose();
}
}
}
/// <summary>
/// Compares two images for equivalence
/// </summary>
/// <param name="actual"></param>
/// <param name="expected"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>True if every pixel of actual is equal to expected</returns>
public static bool ImagesAreEqual(Image<Rgb24> actual, Image<Rgb24> expected, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
ArgumentNullException.ThrowIfNull(actual);
ArgumentNullException.ThrowIfNull(expected);
if (resizeOption == ResizeOption.DontResize && !ImagesHaveSameDimension(actual, expected))
{
return false;
}
if (resizeOption == ResizeOption.DontResize || ImagesHaveSameDimension(actual, expected))
{
for (var x = 0; x < actual.Width; x++)
{
for (var y = 0; y < actual.Height; y++)
{
if (pixelColorShiftTolerance == 0 && !actual[x, y].Equals(expected[x, y]))
{
return false;
}
else if (pixelColorShiftTolerance > 0)
{
var actualPixel = actual[x, y];
var expectedPixel = expected[x, y];
if (Math.Abs(actualPixel.R - expectedPixel.R) > pixelColorShiftTolerance ||
Math.Abs(actualPixel.G - expectedPixel.G) > pixelColorShiftTolerance ||
Math.Abs(actualPixel.B - expectedPixel.B) > pixelColorShiftTolerance)
{
return false;
}
}
}
}
return true;
}
var grown = GrowToSameDimension(actual, expected);
try
{
return ImagesAreEqual(grown.Item1, grown.Item2, ResizeOption.DontResize, pixelColorShiftTolerance);
}
finally
{
grown.Item1?.Dispose();
grown.Item2?.Dispose();
}
}
/// <summary>
/// Calculates ICompareResult expressing the amount of difference of both images
/// </summary>
/// <param name="pathActualImage"></param>
/// <param name="pathExpectedImage"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>Mean and absolute pixel error</returns>
public static ICompareResult CalcDiff(string pathActualImage, string pathExpectedImage, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
using var actual = Image.Load(pathActualImage);
using var expected = Image.Load(pathExpectedImage);
return CalcDiff(actual, expected, resizeOption, pixelColorShiftTolerance);
}
/// <summary>
/// Calculates ICompareResult expressing the amount of difference of both images using a mask image for tolerated difference between the two images
/// </summary>
/// <param name="pathActualImage"></param>
/// <param name="pathExpectedImage"></param>
/// <param name="pathMaskImage"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>Mean and absolute pixel error</returns>
public static ICompareResult CalcDiff(string pathActualImage, string pathExpectedImage, string pathMaskImage, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
using var actual = Image.Load(pathActualImage);
using var expected = Image.Load(pathExpectedImage);
using var mask = Image.Load(pathMaskImage);
return CalcDiff(actual, expected, mask, resizeOption, pixelColorShiftTolerance);
}
/// <summary>
/// Calculates ICompareResult expressing the amount of difference of both images
/// </summary>
/// <param name="actualImage"></param>
/// <param name="expectedImage"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>Mean and absolute pixel error</returns>
public static ICompareResult CalcDiff(Stream actualImage, Stream expectedImage, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
using var actual = Image.Load(actualImage);
using var expected = Image.Load(expectedImage);
return CalcDiff(actual, expected, resizeOption, pixelColorShiftTolerance);
}
/// <summary>
/// Calculates ICompareResult expressing the amount of difference of both images
/// </summary>
/// <param name="actualImage"></param>
/// <param name="expectedImage"></param>
/// <param name="maskImage"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns></returns>
public static ICompareResult CalcDiff(Stream actualImage, Stream expectedImage, Image maskImage, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
using var actual = Image.Load(actualImage);
using var expected = Image.Load(expectedImage);
return CalcDiff(actual, expected, maskImage, resizeOption, pixelColorShiftTolerance);
}
/// <summary>
/// Calculates ICompareResult expressing the amount of difference of both images
/// </summary>
/// <param name="actual"></param>
/// <param name="expected"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>Mean and absolute pixel error</returns>
public static ICompareResult CalcDiff(Image actual, Image expected, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
ArgumentNullException.ThrowIfNull(actual);
ArgumentNullException.ThrowIfNull(expected);
var ownsActual = false;
var ownsExpected = false;
Image<Rgb24>? actualRgb24 = null;
Image<Rgb24>? expectedRgb24 = null;
try
{
actualRgb24 = ImageSharpPixelTypeConverter.ToRgb24Image(actual, out ownsActual);
expectedRgb24 = ImageSharpPixelTypeConverter.ToRgb24Image(expected, out ownsExpected);
return CalcDiff(actualRgb24, expectedRgb24, resizeOption, pixelColorShiftTolerance);
}
finally
{
if (ownsActual)
{
actualRgb24?.Dispose();
}
if (ownsExpected)
{
expectedRgb24?.Dispose();
}
}
}
/// <summary>
/// Calculates ICompareResult expressing the amount of difference of both images
/// </summary>
/// <param name="actual"></param>
/// <param name="expected"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>Mean and absolute pixel error</returns>
public static ICompareResult CalcDiff(Image<Rgb24> actual, Image<Rgb24> expected, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
var imagesHaveSameDimension = ImagesHaveSameDimension(actual, expected);
if (resizeOption == ResizeOption.Resize && !imagesHaveSameDimension)
{
var grown = GrowToSameDimension(actual, expected);
try
{
return CalcDiff(grown.Item1, grown.Item2, ResizeOption.DontResize, pixelColorShiftTolerance);
}
finally
{
grown.Item1?.Dispose();
grown.Item2?.Dispose();
}
}
if (!imagesHaveSameDimension)
{
throw new ImageSharpCompareException(sizeDiffersExceptionMessage);
}
var quantity = actual.Width * actual.Height;
var absoluteError = 0;
var pixelErrorCount = 0;
for (var x = 0; x < actual.Width; x++)
{
for (var y = 0; y < actual.Height; y++)
{
var actualPixel = actual[x, y];
var expectedPixel = expected[x, y];
var r = Math.Abs(expectedPixel.R - actualPixel.R);
var g = Math.Abs(expectedPixel.G - actualPixel.G);
var b = Math.Abs(expectedPixel.B - actualPixel.B);
var sum = r + g + b;
absoluteError += sum > pixelColorShiftTolerance ? sum : 0;
pixelErrorCount += (sum > pixelColorShiftTolerance) ? 1 : 0;
}
}
var meanError = (double)absoluteError / quantity;
var pixelErrorPercentage = (double)pixelErrorCount / quantity * 100;
return new CompareResult(absoluteError, meanError, pixelErrorCount, pixelErrorPercentage);
}
/// <summary>
/// Calculates ICompareResult expressing the amount of difference of both images using a image mask for tolerated difference between the two images
/// </summary>
/// <param name="actual"></param>
/// <param name="expected"></param>
/// <param name="maskImage"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>Mean and absolute pixel error</returns>
public static ICompareResult CalcDiff(Image actual, Image expected, Image maskImage, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
ArgumentNullException.ThrowIfNull(actual);
ArgumentNullException.ThrowIfNull(expected);
ArgumentNullException.ThrowIfNull(maskImage);
var ownsActual = false;
var ownsExpected = false;
var ownsMask = false;
Image<Rgb24>? actualRgb24 = null;
Image<Rgb24>? expectedRgb24 = null;
Image<Rgb24>? maskImageRgb24 = null;
try
{
actualRgb24 = ImageSharpPixelTypeConverter.ToRgb24Image(actual, out ownsActual);
expectedRgb24 = ImageSharpPixelTypeConverter.ToRgb24Image(expected, out ownsExpected);
maskImageRgb24 = ImageSharpPixelTypeConverter.ToRgb24Image(maskImage, out ownsMask);
return CalcDiff(actualRgb24, expectedRgb24, maskImageRgb24, resizeOption, pixelColorShiftTolerance);
}
finally
{
if (ownsActual)
{
actualRgb24?.Dispose();
}
if (ownsExpected)
{
expectedRgb24?.Dispose();
}
if (ownsMask)
{
maskImageRgb24?.Dispose();
}
}
}
/// <summary>
/// Calculates ICompareResult expressing the amount of difference of both images using a image mask for tolerated difference between the two images
/// </summary>
/// <param name="actual"></param>
/// <param name="expected"></param>
/// <param name="maskImage"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>Mean and absolute pixel error</returns>
public static ICompareResult CalcDiff(Image<Rgb24> actual, Image<Rgb24> expected, Image<Rgb24> maskImage, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
ArgumentNullException.ThrowIfNull(maskImage);
var imagesHaveSameDimension = ImagesHaveSameDimension(actual, expected) && ImagesHaveSameDimension(actual, maskImage);
if (resizeOption == ResizeOption.Resize && !imagesHaveSameDimension)
{
var grown = GrowToSameDimension(actual, expected, maskImage);
try
{
return CalcDiff(grown.Item1, grown.Item2, grown.Item3, ResizeOption.DontResize, pixelColorShiftTolerance);
}
finally
{
grown.Item1?.Dispose();
grown.Item2?.Dispose();
grown.Item3?.Dispose();
}
}
if (!imagesHaveSameDimension)
{
throw new ImageSharpCompareException(sizeDiffersExceptionMessage);
}
var quantity = actual.Width * actual.Height;
var absoluteError = 0;
var pixelErrorCount = 0;
for (var x = 0; x < actual.Width; x++)
{
for (var y = 0; y < actual.Height; y++)
{
var maskImagePixel = maskImage[x, y];
var actualPixel = actual[x, y];
var expectedPixel = expected[x, y];
var r = Math.Abs(expectedPixel.R - actualPixel.R);
var g = Math.Abs(expectedPixel.G - actualPixel.G);
var b = Math.Abs(expectedPixel.B - actualPixel.B);
var error = 0;
if (r > maskImagePixel.R)
{
error += r;
}
if (g > maskImagePixel.G)
{
error += g;
}
if (b > maskImagePixel.B)
{
error += b;
}
absoluteError += error > pixelColorShiftTolerance ? error : 0;
pixelErrorCount += error > pixelColorShiftTolerance ? 1 : 0;
}
}
var meanError = (double)absoluteError / quantity;
var pixelErrorPercentage = (double)pixelErrorCount / quantity * 100;
return new CompareResult(absoluteError, meanError, pixelErrorCount, pixelErrorPercentage);
}
/// <summary>
/// Creates a diff mask image of two images
/// </summary>
/// <param name="pathActualImage"></param>
/// <param name="pathExpectedImage"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>Image representing diff, black means no diff between actual image and expected image, white means max diff</returns>
public static Image CalcDiffMaskImage(string pathActualImage, string pathExpectedImage, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
using var actual = Image.Load(pathActualImage);
using var expected = Image.Load(pathExpectedImage);
return CalcDiffMaskImage(actual, expected, resizeOption, pixelColorShiftTolerance);
}
/// <summary>
/// Creates a diff mask image of two images
/// </summary>
/// <param name="pathActualImage"></param>
/// <param name="pathExpectedImage"></param>
/// <param name="pathMaskImage"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>Image representing diff, black means no diff between actual image and expected image, white means max diff</returns>
public static Image CalcDiffMaskImage(string pathActualImage, string pathExpectedImage, string pathMaskImage, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
using var actual = Image.Load(pathActualImage);
using var expected = Image.Load(pathExpectedImage);
using var mask = Image.Load(pathMaskImage);
return CalcDiffMaskImage(actual, expected, mask, resizeOption, pixelColorShiftTolerance);
}
/// <summary>
/// Creates a diff mask image of two images
/// </summary>
/// <param name="actualImage"></param>
/// <param name="expectedImage"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>Image representing diff, black means no diff between actual image and expected image, white means max diff</returns>
public static Image CalcDiffMaskImage(Stream actualImage, Stream expectedImage, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
using var actual = Image.Load(actualImage);
using var expected = Image.Load(expectedImage);
return CalcDiffMaskImage(actual, expected, resizeOption, pixelColorShiftTolerance);
}
/// <summary>
/// Creates a diff mask image of two images
/// </summary>
/// <param name="actualImage"></param>
/// <param name="expectedImage"></param>
/// <param name="maskImage"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>Image representing diff, black means no diff between actual image and expected image, white means max diff</returns>
public static Image CalcDiffMaskImage(Stream actualImage, Stream expectedImage, Stream maskImage, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
using var actual = Image.Load(actualImage);
using var expected = Image.Load(expectedImage);
using var mask = Image.Load(maskImage);
return CalcDiffMaskImage(actual, expected, mask, resizeOption, pixelColorShiftTolerance);
}
/// <summary>
/// Creates a diff mask image of two images
/// </summary>
/// <param name="actual"></param>
/// <param name="expected"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>Image representing diff, black means no diff between actual image and expected image, white means max diff</returns>
public static Image CalcDiffMaskImage(Image actual, Image expected, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
ArgumentNullException.ThrowIfNull(actual);
ArgumentNullException.ThrowIfNull(expected);
var ownsActual = false;
var ownsExpected = false;
Image<Rgb24>? actualRgb24 = null;
Image<Rgb24>? expectedRgb24 = null;
try
{
actualRgb24 = ImageSharpPixelTypeConverter.ToRgb24Image(actual, out ownsActual);
expectedRgb24 = ImageSharpPixelTypeConverter.ToRgb24Image(expected, out ownsExpected);
return CalcDiffMaskImage(actualRgb24, expectedRgb24, resizeOption, pixelColorShiftTolerance);
}
finally
{
if (ownsActual)
{
actualRgb24?.Dispose();
}
if (ownsExpected)
{
expectedRgb24?.Dispose();
}
}
}
/// <summary>
/// Creates a diff mask image of two images
/// </summary>
/// <param name="actual"></param>
/// <param name="expected"></param>
/// <param name="mask"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>Image representing diff, black means no diff between actual image and expected image, white means max diff</returns>
public static Image CalcDiffMaskImage(Image actual, Image expected, Image mask, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
ArgumentNullException.ThrowIfNull(actual);
ArgumentNullException.ThrowIfNull(expected);
ArgumentNullException.ThrowIfNull(mask);
var ownsActual = false;
var ownsExpected = false;
var ownsMask = false;
Image<Rgb24>? actualRgb24 = null;
Image<Rgb24>? expectedRgb24 = null;
Image<Rgb24>? maskRgb24 = null;
try
{
actualRgb24 = ImageSharpPixelTypeConverter.ToRgb24Image(actual, out ownsActual);
expectedRgb24 = ImageSharpPixelTypeConverter.ToRgb24Image(expected, out ownsExpected);
maskRgb24 = ImageSharpPixelTypeConverter.ToRgb24Image(mask, out ownsMask);
return CalcDiffMaskImage(actualRgb24, expectedRgb24, maskRgb24, resizeOption, pixelColorShiftTolerance);
}
finally
{
if (ownsActual)
{
actualRgb24?.Dispose();
}
if (ownsExpected)
{
expectedRgb24?.Dispose();
}
if (ownsMask)
{
maskRgb24?.Dispose();
}
}
}
/// <summary>
/// Creates a diff mask image of two images
/// </summary>
/// <param name="actual"></param>
/// <param name="expected"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>Image representing diff, black means no diff between actual image and expected image, white means max diff</returns>
public static Image CalcDiffMaskImage(Image<Rgb24> actual, Image<Rgb24> expected, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
var imagesHAveSameDimension = ImagesHaveSameDimension(actual, expected);
if (resizeOption == ResizeOption.DontResize && !imagesHAveSameDimension)
{
throw new ImageSharpCompareException(sizeDiffersExceptionMessage);
}
if (imagesHAveSameDimension)
{
var maskImage = new Image<Rgb24>(actual.Width, actual.Height);
for (var x = 0; x < actual.Width; x++)
{
for (var y = 0; y < actual.Height; y++)
{
var actualPixel = actual[x, y];
var expectedPixel = expected[x, y];
if (pixelColorShiftTolerance == 0)
{
var pixel = new Rgb24
{
R = (byte)Math.Abs(actualPixel.R - expectedPixel.R),
G = (byte)Math.Abs(actualPixel.G - expectedPixel.G),
B = (byte)Math.Abs(actualPixel.B - expectedPixel.B)
};
maskImage[x, y] = pixel;
}
else
{
var r = Math.Abs(actualPixel.R - expectedPixel.R);
var g = Math.Abs(actualPixel.G - expectedPixel.G);
var b = Math.Abs(actualPixel.B - expectedPixel.B);
var error = r + g + b;
if (error <= pixelColorShiftTolerance)
{
r = 0;
g = 0;
b = 0;
}
var pixel = new Rgb24
{
R = (byte)r,
G = (byte)g,
B = (byte)b
};
maskImage[x, y] = pixel;
}
}
}
return maskImage;
}
var grown = GrowToSameDimension(actual, expected);
try
{
return CalcDiffMaskImage(grown.Item1, grown.Item2, ResizeOption.DontResize, pixelColorShiftTolerance);
}
finally
{
grown.Item1?.Dispose();
grown.Item2?.Dispose();
}
}
/// <summary>
/// Creates a diff mask image of two images using a image mask for tolerated difference between the two images.
/// </summary>
/// <param name="actual"></param>
/// <param name="expected"></param>
/// <param name="mask"></param>
/// <param name="resizeOption"></param>
/// <param name="pixelColorShiftTolerance"></param>
/// <returns>Image representing diff, black means no diff between actual image and expected image, white means max diff</returns>
public static Image CalcDiffMaskImage(Image<Rgb24> actual, Image<Rgb24> expected, Image<Rgb24> mask, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)
{
ArgumentNullException.ThrowIfNull(mask);
var imagesHaveSameDimensions = ImagesHaveSameDimension(actual, expected) && ImagesHaveSameDimension(actual, mask);
if (!imagesHaveSameDimensions && resizeOption == ResizeOption.DontResize)
{
throw new ImageSharpCompareException(sizeDiffersExceptionMessage);
}
if (imagesHaveSameDimensions)
{
var maskImageResult = new Image<Rgb24>(actual.Width, actual.Height);
for (var x = 0; x < actual.Width; x++)
{
for (var y = 0; y < actual.Height; y++)
{
var maskPixel = mask[x, y];
var actualPixel = actual[x, y];
var expectedPixel = expected[x, y];
maskImageResult[x, y] = new Rgb24
{
R = (byte)Math.Max(byte.MinValue, Math.Abs(expectedPixel.R - actualPixel.R) - maskPixel.R),
G = (byte)Math.Max(byte.MinValue, Math.Abs(expectedPixel.G - actualPixel.G) - maskPixel.G),
B = (byte)Math.Max(byte.MinValue, Math.Abs(expectedPixel.B - actualPixel.B) - maskPixel.B)
};
}
}
return maskImageResult;
}
var grown = GrowToSameDimension(actual, expected, mask);
try
{
return CalcDiffMaskImage(grown.Item1, grown.Item2, grown.Item3, ResizeOption.DontResize, pixelColorShiftTolerance);
}
finally
{
grown.Item1?.Dispose();
grown.Item2?.Dispose();
grown.Item3?.Dispose();
}
}
/// <summary>
/// Converts a Rgba32 Image to Rgb24 one
/// </summary>
/// <param name="imageRgba32"></param>
#pragma warning disable S1133 // Give the consumer of the public method some time to migrate
[Obsolete("use 'imageRgba32..CloneAs<Rgb24>()' instead")]
#pragma warning restore S1133
public static Image<Rgb24> ConvertRgba32ToRgb24(Image<Rgba32> imageRgba32)
{
ArgumentNullException.ThrowIfNull(imageRgba32);
var maskRgb24 = new Image<Rgb24>(imageRgba32.Width, imageRgba32.Height);
for (var x = 0; x < imageRgba32.Width; x++)
{
for (var y = 0; y < imageRgba32.Height; y++)
{
var pixel = new Rgb24();
pixel.FromRgba32(imageRgba32[x, y]);
maskRgb24[x, y] = pixel;
}
}
return maskRgb24;
}
}
}