From a04e9c8efcce79e6430046db1c4a428788c4d639 Mon Sep 17 00:00:00 2001
From: "Stefan.Seeland" <168659+stesee@users.noreply.github.com>
Date: Sun, 9 Apr 2023 07:18:34 +0200
Subject: [PATCH] Bugfix CalcDiff - resizing optional mask on using
ResizeOption.Resize
---
ImageSharpCompare/ImageSharpCompare.cs | 15 +++--
.../ImageSharpCompareTest.cs | 60 ++++++++++++++++--
.../TestData/pngTransparent2x2px.png | Bin 0 -> 489 bytes
3 files changed, 64 insertions(+), 11 deletions(-)
create mode 100644 ImageSharpCompareTestNunit/TestData/pngTransparent2x2px.png
diff --git a/ImageSharpCompare/ImageSharpCompare.cs b/ImageSharpCompare/ImageSharpCompare.cs
index d4e8fb6..d496690 100644
--- a/ImageSharpCompare/ImageSharpCompare.cs
+++ b/ImageSharpCompare/ImageSharpCompare.cs
@@ -406,15 +406,14 @@ public static ICompareResult CalcDiff(Image actual, Image expected, Image maskIm
/// Mean and absolute pixel error
public static ICompareResult CalcDiff(Image actual, Image expected, Image maskImage, ResizeOption resizeOption = ResizeOption.DontResize)
{
- var immagesHaveSameDimension = ImagesHaveSameDimension(actual, expected);
+ var immagesHaveSameDimension = ImagesHaveSameDimension(actual, expected) && ImagesHaveSameDimension(actual, maskImage);
if (resizeOption == ResizeOption.Resize && !immagesHaveSameDimension)
{
- var grown = GrowToSameDimension(actual, expected);
- var grownMask = GrowToSameDimension(grown.Item1, maskImage);
+ var grown = GrowToSameDimension(actual, expected, maskImage);
try
{
- return CalcDiff(grown.Item1, grown.Item2, grownMask.Item2, ResizeOption.DontResize);
+ return CalcDiff(grown.Item1, grown.Item2, grown.Item3, ResizeOption.DontResize);
}
finally
{
@@ -573,17 +572,21 @@ public static Image CalcDiffMaskImage(Image actual, Image expected
return maskImage;
}
- private static (Image, Image) GrowToSameDimension(Image actual, Image expected)
+ private static (Image, Image, Image?) GrowToSameDimension(Image actual, Image expected, Image? mask = null)
{
var biggesWidh = actual.Width > expected.Width ? actual.Width : expected.Width;
+ biggesWidh = biggesWidh > (mask?.Width ?? 0) ? biggesWidh : (mask?.Width ?? 0);
var biggesHeight = actual.Height > expected.Height ? actual.Height : expected.Height;
+ biggesHeight = biggesHeight > (mask?.Height ?? 0) ? biggesHeight : (mask?.Height ?? 0);
var grownExpected = expected.Clone();
var grownActual = actual.Clone();
+ var grownMask = mask?.Clone();
grownActual.Mutate(x => x.Resize(biggesWidh, biggesHeight));
grownExpected.Mutate(x => x.Resize(biggesWidh, biggesHeight));
+ grownMask?.Mutate(x => x.Resize(biggesWidh, biggesHeight));
- return (grownActual, grownExpected);
+ return (grownActual, grownExpected, grownMask);
}
}
}
\ No newline at end of file
diff --git a/ImageSharpCompareTestNunit/ImageSharpCompareTest.cs b/ImageSharpCompareTestNunit/ImageSharpCompareTest.cs
index 1cc47e0..7f55576 100644
--- a/ImageSharpCompareTestNunit/ImageSharpCompareTest.cs
+++ b/ImageSharpCompareTestNunit/ImageSharpCompareTest.cs
@@ -16,6 +16,7 @@ public class IntegrationTest
private const string pngBlack2x2px = "../../../TestData/Black.png";
private const string pngBlack4x4px = "../../../TestData/BlackDoubleSize.png";
private const string pngWhite2x2px = "../../../TestData/White.png";
+ private const string pngTransparent2x2px = "../../../TestData/pngTransparent2x2px.png";
[Test]
[TestCase(jpg0Rgb24, jpg0Rgb24, true)]
@@ -213,9 +214,11 @@ public void Diffmask(string pathPic1, string pathPic2, int expectedMeanError, in
Assert.That(maskedDiff.PixelErrorPercentage, Is.EqualTo(expectedPixelErrorPercentage), "PixelErrorPercentage");
}
- [TestCase(png0Rgba32, png1Rgba32, 0, 0, 0, 0)]
- [TestCase(jpg0Rgb24, jpg1Rgb24, 0, 0, 0, 0)]
- public void ShoulCalcDiffmaskImageSharp(string pathPic1, string pathPic2, int expectedMeanError, int expectedAbsoluteError, int expectedPixelErrorCount, double expectedPixelErrorPercentage)
+ [TestCase(png0Rgba32, png1Rgba32, 0, 0, 0, 0, ResizeOption.DontResize)]
+ [TestCase(jpg0Rgb24, jpg1Rgb24, 0, 0, 0, 0, ResizeOption.DontResize)]
+ [TestCase(jpg0Rgb24, jpg1Rgb24, 0, 0, 0, 0, ResizeOption.Resize)]
+ [TestCase(pngBlack2x2px, pngBlack4x4px, 0, 0, 0, 0, ResizeOption.Resize)]
+ public void ShoulCalcDiffmaskImageSharpAndUseOutcome(string pathPic1, string pathPic2, int expectedMeanError, int expectedAbsoluteError, int expectedPixelErrorCount, double expectedPixelErrorPercentage, ResizeOption resizeOption)
{
var absolutePathPic1 = Path.Combine(AppContext.BaseDirectory, pathPic1);
var absolutePathPic2 = Path.Combine(AppContext.BaseDirectory, pathPic2);
@@ -225,13 +228,13 @@ public void ShoulCalcDiffmaskImageSharp(string pathPic1, string pathPic2, int ex
using var absolutePic2 = Image.Load(absolutePathPic2);
using (var fileStreamDifferenceMask = File.Create(differenceMaskPicPath))
- using (var maskImage = ImageSharpCompare.CalcDiffMaskImage(absolutePic1, absolutePic2))
+ using (var maskImage = ImageSharpCompare.CalcDiffMaskImage(absolutePic1, absolutePic2, resizeOption))
{
ImageExtensions.SaveAsPng(maskImage, fileStreamDifferenceMask);
}
using var differenceMaskPic = Image.Load(differenceMaskPicPath);
- var maskedDiff = ImageSharpCompare.CalcDiff(absolutePic1, absolutePic2, differenceMaskPic);
+ var maskedDiff = ImageSharpCompare.CalcDiff(absolutePic1, absolutePic2, differenceMaskPic, resizeOption);
File.Delete(differenceMaskPicPath);
Assert.That(maskedDiff.AbsoluteError, Is.EqualTo(expectedAbsoluteError), "AbsoluteError");
@@ -244,6 +247,53 @@ public void ShoulCalcDiffmaskImageSharp(string pathPic1, string pathPic2, int ex
AssertDisposeBehavior(differenceMaskPic);
}
+ [TestCase(pngWhite2x2px, pngBlack2x2px, pngTransparent2x2px, 765, 12240, 16, 100d, ResizeOption.Resize)]
+ [TestCase(pngWhite2x2px, pngBlack2x2px, pngBlack4x4px, 765, 12240, 16, 100d, ResizeOption.Resize)]
+ [TestCase(pngBlack2x2px, pngBlack2x2px, pngBlack4x4px, 0, 0, 0, 0, ResizeOption.Resize)]
+ [TestCase(pngBlack2x2px, pngBlack4x4px, pngBlack2x2px, 0, 0, 0, 0, ResizeOption.Resize)]
+ [TestCase(pngBlack4x4px, pngBlack2x2px, pngBlack2x2px, 0, 0, 0, 0, ResizeOption.Resize)]
+ public void ShoulUseDiffMask(string pathPic1, string pathPic2, string pathPic3, double expectedMeanError, int expectedAbsoluteError, int expectedPixelErrorCount, double expectedPixelErrorPercentage, ResizeOption resizeOption)
+ {
+ var absolutePathPic1 = Path.Combine(AppContext.BaseDirectory, pathPic1);
+ var absolutePathPic2 = Path.Combine(AppContext.BaseDirectory, pathPic2);
+ var differenceMaskPic = Path.Combine(AppContext.BaseDirectory, pathPic3);
+ using var pic1 = Image.Load(absolutePathPic1);
+ using var pic2 = Image.Load(absolutePathPic2);
+ using var maskPic = Image.Load(differenceMaskPic);
+
+ var maskedDiff = ImageSharpCompare.CalcDiff(pic1, pic2, maskPic, resizeOption);
+
+ Assert.That(maskedDiff.MeanError, Is.EqualTo(expectedMeanError), "MeanError");
+ Assert.That(maskedDiff.AbsoluteError, Is.EqualTo(expectedAbsoluteError), "AbsoluteError");
+ Assert.That(maskedDiff.PixelErrorCount, Is.EqualTo(expectedPixelErrorCount), "PixelErrorCount");
+ Assert.That(maskedDiff.PixelErrorPercentage, Is.EqualTo(expectedPixelErrorPercentage), "PixelErrorPercentage");
+
+ AssertDisposeBehavior(pic1);
+ AssertDisposeBehavior(pic2);
+ AssertDisposeBehavior(maskPic);
+ }
+
+ [TestCase(pngBlack2x2px, pngBlack2x2px, pngBlack4x4px)]
+ [TestCase(pngBlack2x2px, pngBlack4x4px, pngBlack2x2px)]
+ [TestCase(pngBlack4x4px, pngBlack2x2px, pngBlack2x2px)]
+ public void ShoulThrowUsingInvalidImageDimenstionsDiffMask(string pathPic1, string pathPic2, string pathPic3)
+ {
+ var absolutePathPic1 = Path.Combine(AppContext.BaseDirectory, pathPic1);
+ var absolutePathPic2 = Path.Combine(AppContext.BaseDirectory, pathPic2);
+ var differenceMaskPic = Path.Combine(AppContext.BaseDirectory, pathPic3);
+ using var pic1 = Image.Load(absolutePathPic1);
+ using var pic2 = Image.Load(absolutePathPic2);
+ using var maskPic = Image.Load(differenceMaskPic);
+
+ var exception = Assert.Throws(() => ImageSharpCompare.CalcDiff(pic1, pic2, maskPic, ResizeOption.DontResize));
+
+ Assert.That(exception?.Message, Is.EqualTo("Size of images differ."));
+
+ AssertDisposeBehavior(pic1);
+ AssertDisposeBehavior(pic2);
+ AssertDisposeBehavior(maskPic);
+ }
+
private void AssertDisposeBehavior(Image image)
{
const string imageSharpPrivateFieldNameIsDisposed = "isDisposed";
diff --git a/ImageSharpCompareTestNunit/TestData/pngTransparent2x2px.png b/ImageSharpCompareTestNunit/TestData/pngTransparent2x2px.png
new file mode 100644
index 0000000000000000000000000000000000000000..c83cef58fab25beb136057b41278368db05ee320
GIT binary patch
literal 489
zcmV4Tx04UFukxeK>Q5?m82AMRnFiEm_3#?3pWXVuc)HEoAh4$s5m}usZ#4rL}l^6(U4qnO{mg@hs>Ol7hV(|Sk31Y
z?+Ay@Wj!f;D%|DB1%>aGZWQ@NaoXpA$_zWx@e$#$u$C@kqm0dE#}e)rZckda@Q+>U~q)-%UpFHoa1M0TmYt!>S4?XYe2&y09!Y=u<2Qc_9`esRM
z--pI^IJ+!q#x|U9K+8i;cXVH>mKjK;;PphlDFIE_P(NSn?Ps6kJD8TPCaz$24?5G5
zy-?xZ!SdezJmdFA^c$yEa=aIpsfhpp010qNS#tmYE+PN`E+PS{;@y$}000e1L_t&-
fS7V?UFfafB07L)*zFcwY00000NkvXXu0mjfwC>op
literal 0
HcmV?d00001