Skip to content

Commit f4c1f62

Browse files
authored
Fixed CalcDiffMaskImage using images with different dimension
2 parents 933f9f4 + c50d85b commit f4c1f62

File tree

4 files changed

+71
-21
lines changed

4 files changed

+71
-21
lines changed

ImageSharpCompare/ImageSharpCompare.cs

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -545,46 +545,75 @@ public static Image CalcDiffMaskImage(Image actual, Image expected, ResizeOption
545545
/// <returns>Image representing diff, black means no diff between actual image and expected image, white means max diff</returns>
546546
public static Image CalcDiffMaskImage(Image<Rgb24> actual, Image<Rgb24> expected, ResizeOption resizeOption = ResizeOption.DontResize)
547547
{
548-
if (resizeOption == ResizeOption.DontResize && !ImagesHaveSameDimension(actual, expected))
548+
var imagesHAveSameDimension = ImagesHaveSameDimension(actual, expected);
549+
550+
if (resizeOption == ResizeOption.DontResize && !imagesHAveSameDimension)
549551
{
550552
throw new ImageSharpCompareException(sizeDiffersExceptionMessage);
551553
}
552554

553-
var maskImage = new Image<Rgb24>(actual.Width, actual.Height);
554-
555-
for (var x = 0; x < actual.Width; x++)
555+
if (imagesHAveSameDimension)
556556
{
557-
for (var y = 0; y < actual.Height; y++)
558-
{
559-
var actualPixel = actual[x, y];
560-
var expectedPixel = expected[x, y];
557+
var maskImage = new Image<Rgb24>(actual.Width, actual.Height);
561558

562-
var pixel = new Rgb24
559+
for (var x = 0; x < actual.Width; x++)
560+
{
561+
for (var y = 0; y < actual.Height; y++)
563562
{
564-
R = (byte)Math.Abs(actualPixel.R - expectedPixel.R),
565-
G = (byte)Math.Abs(actualPixel.G - expectedPixel.G),
566-
B = (byte)Math.Abs(actualPixel.B - expectedPixel.B)
567-
};
563+
var actualPixel = actual[x, y];
564+
var expectedPixel = expected[x, y];
568565

569-
maskImage[x, y] = pixel;
566+
var pixel = new Rgb24
567+
{
568+
R = (byte)Math.Abs(actualPixel.R - expectedPixel.R),
569+
G = (byte)Math.Abs(actualPixel.G - expectedPixel.G),
570+
B = (byte)Math.Abs(actualPixel.B - expectedPixel.B)
571+
};
572+
573+
maskImage[x, y] = pixel;
574+
}
570575
}
576+
return maskImage;
571577
}
572-
return maskImage;
578+
579+
var grown = GrowToSameDimension(actual, expected);
580+
try
581+
{
582+
return CalcDiffMaskImage(grown.Item1, grown.Item2, ResizeOption.DontResize);
583+
}
584+
finally
585+
{
586+
grown.Item1?.Dispose();
587+
grown.Item2?.Dispose();
588+
}
589+
}
590+
591+
private static (Image<Rgb24>, Image<Rgb24>) GrowToSameDimension(Image<Rgb24> actual, Image<Rgb24> expected)
592+
{
593+
var biggesWidh = actual.Width > expected.Width ? actual.Width : expected.Width;
594+
var biggesHeight = actual.Height > expected.Height ? actual.Height : expected.Height;
595+
596+
var grownExpected = expected.Clone();
597+
var grownActual = actual.Clone();
598+
grownActual.Mutate(x => x.Resize(biggesWidh, biggesHeight));
599+
grownExpected.Mutate(x => x.Resize(biggesWidh, biggesHeight));
600+
601+
return (grownActual, grownExpected);
573602
}
574603

575-
private static (Image<Rgb24>, Image<Rgb24>, Image<Rgb24>?) GrowToSameDimension(Image<Rgb24> actual, Image<Rgb24> expected, Image<Rgb24>? mask = null)
604+
private static (Image<Rgb24>, Image<Rgb24>, Image<Rgb24>) GrowToSameDimension(Image<Rgb24> actual, Image<Rgb24> expected, Image<Rgb24> mask)
576605
{
577606
var biggesWidh = actual.Width > expected.Width ? actual.Width : expected.Width;
578-
biggesWidh = biggesWidh > (mask?.Width ?? 0) ? biggesWidh : (mask?.Width ?? 0);
607+
biggesWidh = biggesWidh > mask.Width ? biggesWidh : mask.Width;
579608
var biggesHeight = actual.Height > expected.Height ? actual.Height : expected.Height;
580-
biggesHeight = biggesHeight > (mask?.Height ?? 0) ? biggesHeight : (mask?.Height ?? 0);
609+
biggesHeight = biggesHeight > mask.Height ? biggesHeight : mask.Height;
581610

582611
var grownExpected = expected.Clone();
583612
var grownActual = actual.Clone();
584-
var grownMask = mask?.Clone();
613+
var grownMask = mask.Clone();
585614
grownActual.Mutate(x => x.Resize(biggesWidh, biggesHeight));
586615
grownExpected.Mutate(x => x.Resize(biggesWidh, biggesHeight));
587-
grownMask?.Mutate(x => x.Resize(biggesWidh, biggesHeight));
616+
grownMask.Mutate(x => x.Resize(biggesWidh, biggesHeight));
588617

589618
return (grownActual, grownExpected, grownMask);
590619
}

ImageSharpCompareTestNunit/ImageSharpCompareTest.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class IntegrationTest
1717
private const string pngBlack4x4px = "../../../TestData/BlackDoubleSize.png";
1818
private const string pngWhite2x2px = "../../../TestData/White.png";
1919
private const string pngTransparent2x2px = "../../../TestData/pngTransparent2x2px.png";
20+
private const string renderdForm1 = "../../../TestData/HC007-Test-02-3-OxPt.html1.png";
21+
private const string renderdForm2 = "../../../TestData/HC007-Test-02-3-OxPt.html2.png";
2022

2123
[Test]
2224
[TestCase(jpg0Rgb24, jpg0Rgb24, true)]
@@ -131,6 +133,8 @@ public void ShouldVerifyThatImageSharpImagesAreEqual(string pathActual, string p
131133
[TestCase(pngBlack2x2px, pngWhite2x2px, 3060, 765, 4, 100.0d, ResizeOption.DontResize)]
132134
[TestCase(pngBlack2x2px, pngBlack4x4px, 0, 0, 0, 0, ResizeOption.Resize)]
133135
[TestCase(pngBlack4x4px, pngWhite2x2px, 12240, 765, 16, 100.0d, ResizeOption.Resize)]
136+
[TestCase(renderdForm1, renderdForm2, 50103469, 61.825603405725566d, 220164, 27.167324777887465d, ResizeOption.Resize)]
137+
[TestCase(renderdForm2, renderdForm1, 50103469, 61.825603405725566d, 220164, 27.167324777887465d, ResizeOption.Resize)]
134138
public void ShouldVerifyThatImagesAreSemiEqual(string pathPic1, string pathPic2, int expectedAbsoluteError, double expectedMeanError, int expectedPixelErrorCount, double expectedPixelErrorPercentage, ResizeOption resizeOption)
135139
{
136140
var absolutePathPic1 = Path.Combine(AppContext.BaseDirectory, pathPic1);
@@ -193,7 +197,10 @@ public void ShouldVerifyThatImageStreamsAreSemiEqual(string pathPic1, string pat
193197
[TestCase(png0Rgba32, png1Rgba32, 0, 0, 0, 0, ResizeOption.DontResize)]
194198
[TestCase(png0Rgba32, png1Rgba32, 0, 0, 0, 0, ResizeOption.Resize)]
195199
[TestCase(pngWhite2x2px, pngBlack4x4px, 0, 0, 0, 0, ResizeOption.Resize)]
196-
public void Diffmask(string pathPic1, string pathPic2, int expectedMeanError, int expectedAbsoluteError, int expectedPixelErrorCount, double expectedPixelErrorPercentage, ResizeOption resizeOption)
200+
[TestCase(pngBlack4x4px, pngWhite2x2px, 0, 0, 0, 0, ResizeOption.Resize)]
201+
[TestCase(renderdForm1, renderdForm2, 0, 0, 0, 0, ResizeOption.Resize)]
202+
[TestCase(renderdForm2, renderdForm1, 0, 0, 0, 0, ResizeOption.Resize)]
203+
public void CalcDiffMaskImage(string pathPic1, string pathPic2, double expectedMeanError, int expectedAbsoluteError, int expectedPixelErrorCount, double expectedPixelErrorPercentage, ResizeOption resizeOption)
197204
{
198205
var absolutePathPic1 = Path.Combine(AppContext.BaseDirectory, pathPic1);
199206
var absolutePathPic2 = Path.Combine(AppContext.BaseDirectory, pathPic2);
@@ -378,5 +385,19 @@ public void ShouldVerifyThatImageWithDifferentSizeThrows(string pathPic1, string
378385

379386
Assert.That(exception?.Message, Is.EqualTo("Size of images differ."));
380387
}
388+
389+
[TestCase(png0Rgba32, png0Rgba32, pngBlack2x2px)]
390+
[TestCase(png0Rgba32, pngBlack2x2px, png0Rgba32)]
391+
[TestCase(pngBlack2x2px, png0Rgba32, png0Rgba32)]
392+
public void ShouldVerifyThatImageWithDifferentSizeThrows(string pathPic1, string pathPic2, string pathPic3)
393+
{
394+
var absolutePathPic1 = Path.Combine(AppContext.BaseDirectory, pathPic1);
395+
var absolutePathPic2 = Path.Combine(AppContext.BaseDirectory, pathPic2);
396+
var absolutePathPic3 = Path.Combine(AppContext.BaseDirectory, pathPic3);
397+
398+
var exception = Assert.Throws<ImageSharpCompareException>(() => ImageSharpCompare.CalcDiff(absolutePathPic1, absolutePathPic2, absolutePathPic3));
399+
400+
Assert.That(exception?.Message, Is.EqualTo("Size of images differ."));
401+
}
381402
}
382403
}
31.7 KB
Loading
30.4 KB
Loading

0 commit comments

Comments
 (0)