Skip to content

Commit

Permalink
Bugfix CalcDiff - resizing optional mask on using ResizeOption.Resize
Browse files Browse the repository at this point in the history
  • Loading branch information
stesee authored Apr 9, 2023
2 parents c2ef877 + c7f816f commit 933f9f4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
15 changes: 9 additions & 6 deletions ImageSharpCompare/ImageSharpCompare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,15 +406,14 @@ public static ICompareResult CalcDiff(Image actual, Image expected, Image maskIm
/// <returns>Mean and absolute pixel error</returns>
public static ICompareResult CalcDiff(Image<Rgb24> actual, Image<Rgb24> expected, Image<Rgb24> 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
{
Expand Down Expand Up @@ -573,17 +572,21 @@ public static Image CalcDiffMaskImage(Image<Rgb24> actual, Image<Rgb24> expected
return maskImage;
}

private static (Image<Rgb24>, Image<Rgb24>) GrowToSameDimension(Image<Rgb24> actual, Image<Rgb24> expected)
private static (Image<Rgb24>, Image<Rgb24>, Image<Rgb24>?) GrowToSameDimension(Image<Rgb24> actual, Image<Rgb24> expected, Image<Rgb24>? 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);
}
}
}
60 changes: 55 additions & 5 deletions ImageSharpCompareTestNunit/ImageSharpCompareTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand All @@ -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<ImageSharpCompareException>(() => 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";
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 933f9f4

Please sign in to comment.