Skip to content

Commit b2226c2

Browse files
committed
Fix ignored parameter pixelColorShiftTolerance of CalcDiffMaskImage
Refactor pixel error calculation and update test cases to include color shift tolerance
1 parent eefecfb commit b2226c2

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

ImageSharpCompare/ImageSharpCompare.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ public static ICompareResult CalcDiff(Image<Rgb24> actual, Image<Rgb24> expected
491491
error += b;
492492
}
493493

494-
absoluteError += (error > pixelColorShiftTolerance ? error : 0);
494+
absoluteError += error > pixelColorShiftTolerance ? error : 0;
495495
pixelErrorCount += error > pixelColorShiftTolerance ? 1 : 0;
496496
}
497497
}
@@ -677,14 +677,42 @@ public static Image CalcDiffMaskImage(Image<Rgb24> actual, Image<Rgb24> expected
677677
var actualPixel = actual[x, y];
678678
var expectedPixel = expected[x, y];
679679

680-
var pixel = new Rgb24
680+
if (pixelColorShiftTolerance == 0)
681+
{
682+
var pixel = new Rgb24
681683
{
682684
R = (byte)Math.Abs(actualPixel.R - expectedPixel.R),
683685
G = (byte)Math.Abs(actualPixel.G - expectedPixel.G),
684686
B = (byte)Math.Abs(actualPixel.B - expectedPixel.B)
685687
};
686688

687689
maskImage[x, y] = pixel;
690+
}
691+
else
692+
{
693+
var r = Math.Abs(actualPixel.R - expectedPixel.R);
694+
var g = Math.Abs(actualPixel.G - expectedPixel.G);
695+
var b = Math.Abs(actualPixel.B - expectedPixel.B);
696+
697+
var error = r + g + b;
698+
if (error <= pixelColorShiftTolerance)
699+
{
700+
r = 0;
701+
g = 0;
702+
b = 0;
703+
}
704+
else
705+
{ }
706+
707+
var pixel = new Rgb24
708+
{
709+
R = (byte)r,
710+
G = (byte)g,
711+
B = (byte)b
712+
};
713+
714+
maskImage[x, y] = pixel;
715+
}
688716
}
689717
}
690718
return maskImage;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.5.2.0
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageSharpCompare", "ImageSharpCompare.csproj", "{6D218566-E4FA-E901-D111-08AEB4065B5C}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
Release|Any CPU = Release|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{6D218566-E4FA-E901-D111-08AEB4065B5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{6D218566-E4FA-E901-D111-08AEB4065B5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
{6D218566-E4FA-E901-D111-08AEB4065B5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
{6D218566-E4FA-E901-D111-08AEB4065B5C}.Release|Any CPU.Build.0 = Release|Any CPU
17+
EndGlobalSection
18+
GlobalSection(SolutionProperties) = preSolution
19+
HideSolutionNode = FALSE
20+
EndGlobalSection
21+
GlobalSection(ExtensibilityGlobals) = postSolution
22+
SolutionGuid = {239FA1C2-CD5C-4D95-BE3C-97B7B1BF4F3C}
23+
EndGlobalSection
24+
EndGlobal

ImageSharpCompareTestNunit/ImageSharpCompareTest.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,26 +240,32 @@ public void ShouldVerifyThatImageStreamsAreSemiEqual(string pathPic1, string pat
240240
Assert.That(diff.PixelErrorPercentage, Is.EqualTo(expectedPixelErrorPercentage), "PixelErrorPercentage");
241241
}
242242

243-
[TestCase(png0Rgba32, png1Rgba32, 0, 0, 0, 0, null)]
244-
[TestCase(png0Rgba32, png1Rgba32, 0, 0, 0, 0, ResizeOption.DontResize)]
245-
[TestCase(png0Rgba32, png1Rgba32, 0, 0, 0, 0, ResizeOption.Resize)]
246-
[TestCase(pngWhite2x2px, pngBlack4x4px, 0, 0, 0, 0, ResizeOption.Resize)]
247-
[TestCase(pngBlack4x4px, pngWhite2x2px, 0, 0, 0, 0, ResizeOption.Resize)]
248-
[TestCase(renderedForm1, renderedForm2, 0, 0, 0, 0, ResizeOption.Resize)]
249-
[TestCase(renderedForm2, renderedForm1, 0, 0, 0, 0, ResizeOption.Resize)]
250-
public void CalcDiffMaskImage(string pathPic1, string pathPic2, double expectedMeanError, int expectedAbsoluteError, int expectedPixelErrorCount, double expectedPixelErrorPercentage, ResizeOption resizeOption)
243+
[TestCase(png0Rgba32, png1Rgba32, 0, 0, 0, 0, ResizeOption.DontResize, 0, false)]
244+
[TestCase(png0Rgba32, png1Rgba32, 0, 0, 0, 0, ResizeOption.Resize, 0, false)]
245+
[TestCase(pngWhite2x2px, pngBlack4x4px, 0, 0, 0, 0, ResizeOption.Resize, 0, false)]
246+
[TestCase(pngBlack4x4px, pngWhite2x2px, 0, 0, 0, 0, ResizeOption.Resize, 0, false)]
247+
[TestCase(renderedForm1, renderedForm2, 0, 0, 0, 0, ResizeOption.Resize, 0, false)]
248+
[TestCase(renderedForm2, renderedForm1, 0, 0, 0, 0, ResizeOption.Resize, 0, false)]
249+
[TestCase(colorShift1, colorShift1, 0, 0, 0, 0, ResizeOption.DontResize, 15, true)]
250+
[TestCase(colorShift1, colorShift1, 0, 0, 0, 0, ResizeOption.Resize, 15, true)]
251+
[TestCase(colorShift1, colorShift2, 0, 0, 0, 0, ResizeOption.Resize, 15, true)]
252+
[TestCase(colorShift1, colorShift2, 0, 0, 0, 0, ResizeOption.DontResize, 15, true)]
253+
[TestCase(colorShift1, colorShift2, 0, 0, 0, 0, ResizeOption.Resize, 14, false)]
254+
[TestCase(colorShift1, colorShift2, 0, 0, 0, 0, ResizeOption.DontResize, 14, false)]
255+
public void CalcDiffMaskImage(string pathPic1, string pathPic2, double expectedMeanError, int expectedAbsoluteError, int expectedPixelErrorCount, double expectedPixelErrorPercentage, ResizeOption resizeOption, int expectedColorShift, bool expectMaskToBeBlack)
251256
{
252257
var absolutePathPic1 = Path.Combine(AppContext.BaseDirectory, pathPic1);
253258
var absolutePathPic2 = Path.Combine(AppContext.BaseDirectory, pathPic2);
254259
var differenceMask = Path.GetTempFileName() + "differenceMask.png";
255260

256261
using (var fileStreamDifferenceMask = File.Create(differenceMask))
257-
using (var maskImage = ImageSharpCompare.CalcDiffMaskImage(absolutePathPic1, absolutePathPic2, resizeOption))
262+
using (var maskImage = ImageSharpCompare.CalcDiffMaskImage(absolutePathPic1, absolutePathPic2, resizeOption, expectedColorShift))
258263
{
259264
ImageExtensions.SaveAsPng(maskImage, fileStreamDifferenceMask);
265+
Assert.That(IsImageEntirelyBlack(maskImage), Is.EqualTo(expectMaskToBeBlack));
260266
}
261267

262-
var maskedDiff = ImageSharpCompare.CalcDiff(absolutePathPic1, absolutePathPic2, differenceMask, resizeOption);
268+
var maskedDiff = ImageSharpCompare.CalcDiff(absolutePathPic1, absolutePathPic2, differenceMask, resizeOption, expectedColorShift);
263269
File.Delete(differenceMask);
264270

265271
Assert.That(maskedDiff.AbsoluteError, Is.EqualTo(expectedAbsoluteError), "AbsoluteError");

0 commit comments

Comments
 (0)