11using SixLabors . ImageSharp . PixelFormats ;
2+ using SixLabors . ImageSharp . Processing ;
23using System ;
34using System . IO ;
45using System . Linq ;
@@ -14,43 +15,74 @@ protected Compare(ITestOutputHelper output) : base(output)
1415 {
1516 }
1617
17- protected static void AssertImageAreEqual ( string expectedImagePath , string resultImagePath , bool isCleanAll = false )
18+ protected static void AssertLargeImageAreEqual ( string expectedImagePath , string resultImagePath , bool isCleanAll = false )
1819 {
19- string assertName = "AssertImage.AreEqual" ;
20+ using SixLabors . ImageSharp . Image < Rgba32 > expectedImageSharp = SixLabors . ImageSharp . Image . Load < Rgba32 > ( expectedImagePath ) ;
21+ using SixLabors . ImageSharp . Image < Rgba32 > actualImageSharp = SixLabors . ImageSharp . Image . Load < Rgba32 > ( resultImagePath ) ;
2022
21- var expected = AnyBitmap . FromFile ( expectedImagePath ) ;
22- var actual = AnyBitmap . FromFile ( resultImagePath ) ;
23+ CleanCompareResultFile ( expectedImagePath , resultImagePath , isCleanAll ) ;
2324
24- if ( isCleanAll )
25- {
26- CleanResultFile ( expectedImagePath ) ;
27- }
25+ expectedImageSharp . Mutate ( x => x . Resize ( 100 , 100 ) ) ;
26+ actualImageSharp . Mutate ( x => x . Resize ( 100 , 100 ) ) ;
2827
29- CleanResultFile ( resultImagePath ) ;
28+ AssertImageAreEqual ( expectedImageSharp , actualImageSharp ) ;
29+ }
3030
31- //Test to see if we have the same size of image
32- if ( expected . Width != actual . Width || expected . Height != actual . Height )
33- {
34- throw new AssertActualExpectedException ( $ "Expected:<Height { expected . Height } , Width { expected . Width } >.", $ "Actual:<Height { actual . Height } ,Width { actual . Width } >.", $ "{ assertName } failed.") ;
35- }
31+ protected static void AssertImageAreEqual ( string expectedImagePath , string resultImagePath , bool isCleanAll = false )
32+ {
33+ using var expected = AnyBitmap . FromFile ( expectedImagePath ) ;
34+ using var actual = AnyBitmap . FromFile ( resultImagePath ) ;
3635
37- //Convert each image to a byte array
38- byte [ ] btImageExpected = expected . ExportBytes ( ) ;
39- byte [ ] btImageActual = expected . ExportBytes ( ) ;
36+ CleanCompareResultFile ( expectedImagePath , resultImagePath , isCleanAll ) ;
4037
41- //Compute a hash for each image
42- var shaM = SHA256 . Create ( ) ;
43- byte [ ] hash1 = shaM . ComputeHash ( btImageExpected ) ;
44- byte [ ] hash2 = shaM . ComputeHash ( btImageActual ) ;
38+ AssertImageAreEqual ( expected , actual ) ;
39+ }
4540
46- //Compare the hash values
47- for ( int i = 0 ; i < hash1 . Length && i < hash2 . Length ; i ++ )
41+ protected static void AssertImageAreEqual ( AnyBitmap expected , AnyBitmap actual )
42+ {
43+ try
4844 {
49- if ( hash1 [ i ] != hash2 [ i ] )
45+ string assertName = "AssertImage.AreEqual" ;
46+
47+ //Test to see if we have the same size of image
48+ if ( expected . Width != actual . Width || expected . Height != actual . Height )
5049 {
51- throw new AssertActualExpectedException ( $ "Expected:<hash value { hash1 [ i ] } >.", $ "Actual:<hash value { hash2 [ i ] } >.", $ "{ assertName } failed.") ;
50+ throw new AssertActualExpectedException ( $ "Expected:<Height { expected . Height } , Width { expected . Width } >.", $ "Actual:<Height { actual . Height } ,Width { actual . Width } >.", $ "{ assertName } failed.") ;
5251 }
52+
53+ //Convert each image to a byte array
54+ byte [ ] btImageExpected = expected . ExportBytes ( ) ;
55+ byte [ ] btImageActual = expected . ExportBytes ( ) ;
56+
57+ //Compute a hash for each image
58+ var shaM = SHA256 . Create ( ) ;
59+ byte [ ] hash1 = shaM . ComputeHash ( btImageExpected ) ;
60+ byte [ ] hash2 = shaM . ComputeHash ( btImageActual ) ;
61+
62+ //Compare the hash values
63+ for ( int i = 0 ; i < hash1 . Length && i < hash2 . Length ; i ++ )
64+ {
65+ if ( hash1 [ i ] != hash2 [ i ] )
66+ {
67+ throw new AssertActualExpectedException ( $ "Expected:<hash value { hash1 [ i ] } >.", $ "Actual:<hash value { hash2 [ i ] } >.", $ "{ assertName } failed.") ;
68+ }
69+ }
70+ }
71+ finally
72+ {
73+ expected ? . Dispose ( ) ;
74+ actual ? . Dispose ( ) ;
75+ }
76+ }
77+
78+ private static void CleanCompareResultFile ( string expectedImagePath , string resultImagePath , bool isCleanAll )
79+ {
80+ if ( isCleanAll )
81+ {
82+ CleanResultFile ( expectedImagePath ) ;
5383 }
84+
85+ CleanResultFile ( resultImagePath ) ;
5486 }
5587
5688 protected static void CleanResultFile ( string filename )
@@ -60,6 +92,7 @@ protected static void CleanResultFile(string filename)
6092 File . Delete ( filename ) ;
6193 }
6294 }
95+
6396 protected static void AssertStreamAreEqual ( MemoryStream expected , MemoryStream actual )
6497 {
6598 string assertName = "AssertStream.AreEqual" ;
0 commit comments