Skip to content

Commit 604206a

Browse files
authored
Added ImagesHaveEqualSize(string pathImageActual, string pathImageExpected)
2 parents f1b8215 + 0e89f4d commit 604206a

File tree

7 files changed

+127
-25
lines changed

7 files changed

+127
-25
lines changed

.editorconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
# CA1303: Do not pass literals as localized parameters
44
dotnet_diagnostic.CA1303.severity = none
55

6+
# var preferences
7+
csharp_style_var_elsewhere = true:warning
8+
csharp_style_var_for_built_in_types = true:warning
9+
csharp_style_var_when_type_is_apparent = true:warning
10+
611
[*.{cs,vb}]
712
tab_width=4
813
indent_size=4

.github/workflows/cla.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
path-to-document: 'https://github.com/Codeuctivity/ImageSharp.Compare/cla.md' # e.g. a CLA or a DCO document
2323
# branch should not be protected
2424
branch: 'main'
25-
allowlist: dependabot
25+
allowlist: dependabot[bot]
2626

2727
#below are the optional inputs - If the optional inputs are not given, then default values will be taken
2828
#remote-organization-name: enter the remote organization name where the signatures should be stored (Default is storing the signatures in the same repository)
@@ -33,4 +33,4 @@ jobs:
3333
#custom-pr-sign-comment: 'The signature to be committed in order to sign the CLA'
3434
#custom-allsigned-prcomment: 'pull request comment when all contributors has signed, defaults to **CLA Assistant Lite bot** All Contributors have signed the CLA.'
3535
#lock-pullrequest-aftermerge: false - if you don't want this bot to automatically lock the pull request after merging (default - true)
36-
#use-dco-flag: true - If you are using DCO instead of CLA
36+
#use-dco-flag: true - If you are using DCO instead of CLA

.github/workflows/stale.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
pull-requests: write
1919

2020
steps:
21-
- uses: actions/stale@v3
21+
- uses: actions/stale@v5
2222
with:
2323
repo-token: ${{ secrets.GITHUB_TOKEN }}
2424
stale-issue-message: 'Stale issue message'

ImageSharpCompare/ImageSharpCompare.cs

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,44 @@ namespace Codeuctivity.ImageSharpCompare
1010
/// </summary>
1111
public static class ImageSharpCompare
1212
{
13-
private const string sizeDiffersExceptionMessage = "Dimension of images differ";
13+
private const string sizeDiffersExceptionMessage = "Size of images differ.";
14+
15+
/// <summary>
16+
/// Is true if width and height of both images are equal
17+
/// </summary>
18+
/// <param name="pathImageActual"></param>
19+
/// <param name="pathImageExpected"></param>
20+
/// <returns></returns>
21+
public static bool ImagesHaveEqualSize(string pathImageActual, string pathImageExpected)
22+
{
23+
using var actualImage = Image.Load(pathImageActual);
24+
using var expectedImage = Image.Load(pathImageExpected);
25+
return ImagesHaveEqualSize(actualImage, expectedImage);
26+
}
27+
28+
/// <summary>
29+
/// Is true if width and height of both images are equal
30+
/// </summary>
31+
/// <param name="actual"></param>
32+
/// <param name="expected"></param>
33+
/// <returns></returns>
34+
public static bool ImagesHaveEqualSize(Stream actual, Stream expected)
35+
{
36+
using var actualImage = Image.Load(actual);
37+
using var expectedImage = Image.Load(expected);
38+
return ImagesHaveEqualSize(actualImage, expectedImage);
39+
}
40+
41+
/// <summary>
42+
/// Is true if width and height of both images are equal
43+
/// </summary>
44+
/// <param name="actualImage"></param>
45+
/// <param name="expectedImage"></param>
46+
/// <returns></returns>
47+
public static bool ImagesHaveEqualSize(Image actualImage, Image expectedImage)
48+
{
49+
return ImagesHaveSameDimension(actualImage, expectedImage);
50+
}
1451

1552
/// <summary>
1653
/// Compares two images for equivalence
@@ -51,8 +88,8 @@ public static bool ImagesAreEqual(Image actual, Image expected)
5188
return false;
5289
}
5390

54-
Image<Rgb24>? actualPixelaccessableImage = ToRgb24Image(actual);
55-
Image<Rgb24>? expectedPixelaccessableImage = ToRgb24Image(expected);
91+
using var actualPixelaccessableImage = ToRgb24Image(actual);
92+
using var expectedPixelaccessableImage = ToRgb24Image(expected);
5693

5794
for (var x = 0; x < actual.Width; x++)
5895
{
@@ -107,8 +144,8 @@ public static ICompareResult CalcDiff(Image actual, Image expected)
107144
throw new ImageSharpCompareException(sizeDiffersExceptionMessage);
108145
}
109146

110-
var actualRgb24 = ToRgb24Image(actual);
111-
var expectedRgb24 = ToRgb24Image(expected);
147+
using var actualRgb24 = ToRgb24Image(actual);
148+
using var expectedRgb24 = ToRgb24Image(expected);
112149

113150
var quantity = actual.Width * actual.Height;
114151
var absoluteError = 0;
@@ -178,7 +215,7 @@ private static bool ImagesHaveSameDimension(Image actual, Image expected)
178215

179216
private static Image<Rgb24> ToRgb24Image(Image actual)
180217
{
181-
if ((actual is Image<Rgb24> actualPixelaccessableImage))
218+
if (actual is Image<Rgb24> actualPixelaccessableImage)
182219
{
183220
return actualPixelaccessableImage;
184221
}
@@ -218,7 +255,9 @@ private static Image<Rgb24> Rgba32ToRgb24(Image<Rgba32> imageRgba32)
218255
public static ICompareResult CalcDiff(Image actual, Image expected, Image maskImage)
219256
{
220257
if (!ImagesHaveSameDimension(actual, expected))
258+
{
221259
throw new ImageSharpCompareException(sizeDiffersExceptionMessage);
260+
}
222261

223262
if (maskImage == null)
224263
{
@@ -229,9 +268,9 @@ public static ICompareResult CalcDiff(Image actual, Image expected, Image maskIm
229268
var absoluteError = 0;
230269
var pixelErrorCount = 0;
231270

232-
var actualRgb24 = ToRgb24Image(actual);
233-
var expectedRgb24 = ToRgb24Image(expected);
234-
var maskImageRgb24 = ToRgb24Image(maskImage);
271+
using var actualRgb24 = ToRgb24Image(actual);
272+
using var expectedRgb24 = ToRgb24Image(expected);
273+
using var maskImageRgb24 = ToRgb24Image(maskImage);
235274

236275
for (var x = 0; x < actual.Width; x++)
237276
{
@@ -307,20 +346,21 @@ public static Image CalcDiffMaskImage(Image actual, Image expected)
307346
throw new ImageSharpCompareException(sizeDiffersExceptionMessage);
308347
}
309348

310-
var actualRgb24 = ToRgb24Image(actual);
311-
var expectedRgb24 = ToRgb24Image(expected);
349+
using var actualRgb24 = ToRgb24Image(actual);
350+
using var expectedRgb24 = ToRgb24Image(expected);
312351

313352
var maskImage = new Image<Rgb24>(actual.Width, actual.Height);
314353

315354
for (var x = 0; x < actual.Width; x++)
316355
{
317356
for (var y = 0; y < actual.Height; y++)
318357
{
319-
var pixel = new Rgb24();
320-
321-
pixel.R = (byte)Math.Abs(actualRgb24[x, y].R - expectedRgb24[x, y].R);
322-
pixel.G = (byte)Math.Abs(actualRgb24[x, y].G - expectedRgb24[x, y].G);
323-
pixel.B = (byte)Math.Abs(actualRgb24[x, y].B - expectedRgb24[x, y].B);
358+
var pixel = new Rgb24
359+
{
360+
R = (byte)Math.Abs(actualRgb24[x, y].R - expectedRgb24[x, y].R),
361+
G = (byte)Math.Abs(actualRgb24[x, y].G - expectedRgb24[x, y].G),
362+
B = (byte)Math.Abs(actualRgb24[x, y].B - expectedRgb24[x, y].B)
363+
};
324364

325365
maskImage[x, y] = pixel;
326366
}

ImageSharpCompare/ImageSharpCompare.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<ItemGroup>
4949
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.1" />
5050
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
51-
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.36.1.44192">
51+
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.38.0.46746">
5252
<PrivateAssets>all</PrivateAssets>
5353
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
5454
</PackageReference>

ImageSharpCompareTestNunit/ImageSharpCompareTest.cs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,54 @@ public class IntegrationTest
1414
private const string pngBlack = "../../../TestData/Black.png";
1515
private const string pngWhite = "../../../TestData/White.png";
1616

17+
[Test]
18+
[TestCase(jpg0, jpg0, true)]
19+
[TestCase(png0, png0, true)]
20+
[TestCase(png0, jpg0, true)]
21+
[TestCase(png0, jpg1, true)]
22+
[TestCase(png0, pngBlack, false)]
23+
public void ShouldVerifyThatImagesFromFilepathSizeAreEqual(string pathActual, string pathExpected, bool expectedOutcome)
24+
{
25+
var absolutePathActual = Path.Combine(AppContext.BaseDirectory, pathActual);
26+
var absolutePathExpected = Path.Combine(AppContext.BaseDirectory, pathExpected);
27+
28+
Assert.That(ImageSharpCompare.ImagesHaveEqualSize(absolutePathActual, absolutePathExpected), Is.EqualTo(expectedOutcome));
29+
}
30+
31+
[Test]
32+
[TestCase(jpg0, jpg0, true)]
33+
[TestCase(png0, png0, true)]
34+
[TestCase(png0, jpg0, true)]
35+
[TestCase(png0, jpg1, true)]
36+
[TestCase(png0, pngBlack, false)]
37+
public void ShouldVerifyThatImagesSizeAreEqual(string pathActual, string pathExpected, bool expectedOutcome)
38+
{
39+
var absolutePathActual = Path.Combine(AppContext.BaseDirectory, pathActual);
40+
var absolutePathExpected = Path.Combine(AppContext.BaseDirectory, pathExpected);
41+
42+
using var actual = SixLabors.ImageSharp.Image.Load(absolutePathActual);
43+
using var expected = SixLabors.ImageSharp.Image.Load(absolutePathExpected);
44+
45+
Assert.That(ImageSharpCompare.ImagesHaveEqualSize(absolutePathActual, absolutePathExpected), Is.EqualTo(expectedOutcome));
46+
}
47+
48+
[Test]
49+
[TestCase(jpg0, jpg0, true)]
50+
[TestCase(png0, png0, true)]
51+
[TestCase(png0, jpg0, true)]
52+
[TestCase(png0, jpg1, true)]
53+
[TestCase(png0, pngBlack, false)]
54+
public void ShouldVerifyThatImageStreamsSizeAreEqual(string pathActual, string pathExpected, bool expectedOutcome)
55+
{
56+
var absolutePathActual = Path.Combine(AppContext.BaseDirectory, pathActual);
57+
var absolutePathExpected = Path.Combine(AppContext.BaseDirectory, pathExpected);
58+
59+
using var actual = new FileStream(absolutePathActual, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
60+
using var expected = new FileStream(absolutePathExpected, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
61+
62+
Assert.That(ImageSharpCompare.ImagesHaveEqualSize(absolutePathActual, absolutePathExpected), Is.EqualTo(expectedOutcome));
63+
}
64+
1765
[Test]
1866
[TestCase(jpg0, jpg0)]
1967
[TestCase(png0, png0)]
@@ -102,8 +150,6 @@ public void Diffmask(string pathPic1, string pathPic2, int expectedMeanError, in
102150
Assert.That(maskedDiff.MeanError, Is.EqualTo(expectedMeanError), "MeanError");
103151
Assert.That(maskedDiff.PixelErrorCount, Is.EqualTo(expectedPixelErrorCount), "PixelErrorCount");
104152
Assert.That(maskedDiff.PixelErrorPercentage, Is.EqualTo(expectedPixelErrorPercentage), "PixelErrorPercentage");
105-
106-
107153
}
108154

109155
[TestCase(png0, png1, 0, 0, 0, 0)]
@@ -157,5 +203,16 @@ public void ShouldVerifyThatImageStreamAreNotEqual(string pathActual, string pat
157203

158204
Assert.That(ImageSharpCompare.ImagesAreEqual(actual, expected), Is.False);
159205
}
206+
207+
[TestCase(png0, pngBlack)]
208+
public void ShouldVerifyThatImageWithDifferentSizeThrows(string pathPic1, string pathPic2)
209+
{
210+
var absolutePathPic1 = Path.Combine(AppContext.BaseDirectory, pathPic1);
211+
var absolutePathPic2 = Path.Combine(AppContext.BaseDirectory, pathPic2);
212+
213+
var exception = Assert.Throws<ImageSharpCompareException>(() => ImageSharpCompare.CalcDiff(absolutePathPic1, absolutePathPic2));
214+
215+
Assert.That(exception?.Message, Is.EqualTo("Size of images differ."));
216+
}
160217
}
161218
}

ImageSharpCompareTestNunit/ImageSharpCompareTestNunit.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="nunit" Version="3.13.2" />
14-
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.36.1.44192">
13+
<PackageReference Include="nunit" Version="3.13.3" />
14+
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.38.0.46746">
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
<PrivateAssets>all</PrivateAssets>
1717
</PackageReference>
1818
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1">
1919
<PrivateAssets>all</PrivateAssets>
2020
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2121
</PackageReference>
22-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
22+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
2323
</ItemGroup>
2424

2525
<ItemGroup>

0 commit comments

Comments
 (0)