Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 49 additions & 20 deletions ImageSharpCompare/ImageSharpCompare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,46 +545,75 @@ public static Image CalcDiffMaskImage(Image actual, Image expected, ResizeOption
/// <returns>Image representing diff, black means no diff between actual image and expected image, white means max diff</returns>
public static Image CalcDiffMaskImage(Image<Rgb24> actual, Image<Rgb24> expected, ResizeOption resizeOption = ResizeOption.DontResize)
{
if (resizeOption == ResizeOption.DontResize && !ImagesHaveSameDimension(actual, expected))
var imagesHAveSameDimension = ImagesHaveSameDimension(actual, expected);

if (resizeOption == ResizeOption.DontResize && !imagesHAveSameDimension)
{
throw new ImageSharpCompareException(sizeDiffersExceptionMessage);
}

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

for (var x = 0; x < actual.Width; x++)
if (imagesHAveSameDimension)
{
for (var y = 0; y < actual.Height; y++)
{
var actualPixel = actual[x, y];
var expectedPixel = expected[x, y];
var maskImage = new Image<Rgb24>(actual.Width, actual.Height);

var pixel = new Rgb24
for (var x = 0; x < actual.Width; x++)
{
for (var y = 0; y < actual.Height; y++)
{
R = (byte)Math.Abs(actualPixel.R - expectedPixel.R),
G = (byte)Math.Abs(actualPixel.G - expectedPixel.G),
B = (byte)Math.Abs(actualPixel.B - expectedPixel.B)
};
var actualPixel = actual[x, y];
var expectedPixel = expected[x, y];

maskImage[x, y] = pixel;
var pixel = new Rgb24
{
R = (byte)Math.Abs(actualPixel.R - expectedPixel.R),
G = (byte)Math.Abs(actualPixel.G - expectedPixel.G),
B = (byte)Math.Abs(actualPixel.B - expectedPixel.B)
};

maskImage[x, y] = pixel;
}
}
return maskImage;
}
return maskImage;

var grown = GrowToSameDimension(actual, expected);
try
{
return CalcDiffMaskImage(grown.Item1, grown.Item2, ResizeOption.DontResize);
}
finally
{
grown.Item1?.Dispose();
grown.Item2?.Dispose();
}
}

private static (Image<Rgb24>, Image<Rgb24>) GrowToSameDimension(Image<Rgb24> actual, Image<Rgb24> expected)
{
var biggesWidh = actual.Width > expected.Width ? actual.Width : expected.Width;
var biggesHeight = actual.Height > expected.Height ? actual.Height : expected.Height;

var grownExpected = expected.Clone();
var grownActual = actual.Clone();
grownActual.Mutate(x => x.Resize(biggesWidh, biggesHeight));
grownExpected.Mutate(x => x.Resize(biggesWidh, biggesHeight));

return (grownActual, grownExpected);
}

private static (Image<Rgb24>, Image<Rgb24>, Image<Rgb24>?) GrowToSameDimension(Image<Rgb24> actual, Image<Rgb24> expected, Image<Rgb24>? mask = null)
private static (Image<Rgb24>, Image<Rgb24>, Image<Rgb24>) GrowToSameDimension(Image<Rgb24> actual, Image<Rgb24> expected, Image<Rgb24> mask)
{
var biggesWidh = actual.Width > expected.Width ? actual.Width : expected.Width;
biggesWidh = biggesWidh > (mask?.Width ?? 0) ? biggesWidh : (mask?.Width ?? 0);
biggesWidh = biggesWidh > mask.Width ? biggesWidh : mask.Width;
var biggesHeight = actual.Height > expected.Height ? actual.Height : expected.Height;
biggesHeight = biggesHeight > (mask?.Height ?? 0) ? biggesHeight : (mask?.Height ?? 0);
biggesHeight = biggesHeight > mask.Height ? biggesHeight : mask.Height;

var grownExpected = expected.Clone();
var grownActual = actual.Clone();
var grownMask = mask?.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));
grownMask.Mutate(x => x.Resize(biggesWidh, biggesHeight));

return (grownActual, grownExpected, grownMask);
}
Expand Down
23 changes: 22 additions & 1 deletion ImageSharpCompareTestNunit/ImageSharpCompareTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class IntegrationTest
private const string pngBlack4x4px = "../../../TestData/BlackDoubleSize.png";
private const string pngWhite2x2px = "../../../TestData/White.png";
private const string pngTransparent2x2px = "../../../TestData/pngTransparent2x2px.png";
private const string renderdForm1 = "../../../TestData/HC007-Test-02-3-OxPt.html1.png";
private const string renderdForm2 = "../../../TestData/HC007-Test-02-3-OxPt.html2.png";

[Test]
[TestCase(jpg0Rgb24, jpg0Rgb24, true)]
Expand Down Expand Up @@ -131,6 +133,8 @@ public void ShouldVerifyThatImageSharpImagesAreEqual(string pathActual, string p
[TestCase(pngBlack2x2px, pngWhite2x2px, 3060, 765, 4, 100.0d, ResizeOption.DontResize)]
[TestCase(pngBlack2x2px, pngBlack4x4px, 0, 0, 0, 0, ResizeOption.Resize)]
[TestCase(pngBlack4x4px, pngWhite2x2px, 12240, 765, 16, 100.0d, ResizeOption.Resize)]
[TestCase(renderdForm1, renderdForm2, 50103469, 61.825603405725566d, 220164, 27.167324777887465d, ResizeOption.Resize)]
[TestCase(renderdForm2, renderdForm1, 50103469, 61.825603405725566d, 220164, 27.167324777887465d, ResizeOption.Resize)]
public void ShouldVerifyThatImagesAreSemiEqual(string pathPic1, string pathPic2, int expectedAbsoluteError, double expectedMeanError, int expectedPixelErrorCount, double expectedPixelErrorPercentage, ResizeOption resizeOption)
{
var absolutePathPic1 = Path.Combine(AppContext.BaseDirectory, pathPic1);
Expand Down Expand Up @@ -193,7 +197,10 @@ public void ShouldVerifyThatImageStreamsAreSemiEqual(string pathPic1, string pat
[TestCase(png0Rgba32, png1Rgba32, 0, 0, 0, 0, ResizeOption.DontResize)]
[TestCase(png0Rgba32, png1Rgba32, 0, 0, 0, 0, ResizeOption.Resize)]
[TestCase(pngWhite2x2px, pngBlack4x4px, 0, 0, 0, 0, ResizeOption.Resize)]
public void Diffmask(string pathPic1, string pathPic2, int expectedMeanError, int expectedAbsoluteError, int expectedPixelErrorCount, double expectedPixelErrorPercentage, ResizeOption resizeOption)
[TestCase(pngBlack4x4px, pngWhite2x2px, 0, 0, 0, 0, ResizeOption.Resize)]
[TestCase(renderdForm1, renderdForm2, 0, 0, 0, 0, ResizeOption.Resize)]
[TestCase(renderdForm2, renderdForm1, 0, 0, 0, 0, ResizeOption.Resize)]
public void CalcDiffMaskImage(string pathPic1, string pathPic2, double expectedMeanError, int expectedAbsoluteError, int expectedPixelErrorCount, double expectedPixelErrorPercentage, ResizeOption resizeOption)
{
var absolutePathPic1 = Path.Combine(AppContext.BaseDirectory, pathPic1);
var absolutePathPic2 = Path.Combine(AppContext.BaseDirectory, pathPic2);
Expand Down Expand Up @@ -378,5 +385,19 @@ public void ShouldVerifyThatImageWithDifferentSizeThrows(string pathPic1, string

Assert.That(exception?.Message, Is.EqualTo("Size of images differ."));
}

[TestCase(png0Rgba32, png0Rgba32, pngBlack2x2px)]
[TestCase(png0Rgba32, pngBlack2x2px, png0Rgba32)]
[TestCase(pngBlack2x2px, png0Rgba32, png0Rgba32)]
public void ShouldVerifyThatImageWithDifferentSizeThrows(string pathPic1, string pathPic2, string pathPic3)
{
var absolutePathPic1 = Path.Combine(AppContext.BaseDirectory, pathPic1);
var absolutePathPic2 = Path.Combine(AppContext.BaseDirectory, pathPic2);
var absolutePathPic3 = Path.Combine(AppContext.BaseDirectory, pathPic3);

var exception = Assert.Throws<ImageSharpCompareException>(() => ImageSharpCompare.CalcDiff(absolutePathPic1, absolutePathPic2, absolutePathPic3));

Assert.That(exception?.Message, Is.EqualTo("Size of images differ."));
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.