Skip to content

Commit

Permalink
Use file paths as well as image objects
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow committed Aug 30, 2022
1 parent 46c5911 commit 7474171
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
22 changes: 22 additions & 0 deletions source/SkiaSharp.Extended/Comparer/SKPixelComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ namespace SkiaSharp.Extended
{
public static class SKPixelComparer
{
public static SKPixelComparisonResult Compare(string firstFilename, string secondFilename)
{
using var first = SKImage.FromEncodedData(firstFilename);
using var second = SKImage.FromEncodedData(secondFilename);
return Compare(first, second);
}

public static SKPixelComparisonResult Compare(SKBitmap first, SKBitmap second)
{
using var firstPixmap = first.PeekPixels();
Expand Down Expand Up @@ -55,6 +62,14 @@ public static SKPixelComparisonResult Compare(SKImage first, SKImage second)
return new SKPixelComparisonResult(totalPixels, errorPixels, absoluteError);
}

public static SKPixelComparisonResult Compare(string firstFilename, string secondFilename, string maskFilename)
{
using var first = SKImage.FromEncodedData(firstFilename);
using var second = SKImage.FromEncodedData(secondFilename);
using var mask = SKImage.FromEncodedData(maskFilename);
return Compare(first, second, mask);
}

public static SKPixelComparisonResult Compare(SKBitmap first, SKBitmap second, SKBitmap mask)
{
using var firstPixmap = first.PeekPixels();
Expand Down Expand Up @@ -121,6 +136,13 @@ public static SKPixelComparisonResult Compare(SKImage first, SKImage second, SKI
return new SKPixelComparisonResult(totalPixels, errorPixels, absoluteError);
}

public static SKImage GenerateDifferenceMask(string firstFilename, string secondFilename)
{
using var first = SKImage.FromEncodedData(firstFilename);
using var second = SKImage.FromEncodedData(secondFilename);
return GenerateDifferenceMask(first, second);
}

public static SKImage GenerateDifferenceMask(SKBitmap first, SKBitmap second)
{
using var firstPixmap = first.PeekPixels();
Expand Down
26 changes: 25 additions & 1 deletion tests/SkiaSharp.Extended.Tests/Comparer/SKPixelComparerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void DifferentPixmapsReportDifferenceCorrectly(uint firstColor, uint seco
[InlineData(pngFirst, pngFirst)]
[InlineData(jpgSecond, jpgSecond)]
[InlineData(pngSecond, pngSecond)]
public void SameFilesReportNoDifference(string first, string second)
public void SameImagesReportNoDifference(string first, string second)
{
using var firstImage = SKImage.FromEncodedData(GetImagePath(first));
using var secondImage = SKImage.FromEncodedData(GetImagePath(second));
Expand All @@ -101,6 +101,18 @@ public void SameFilesReportNoDifference(string first, string second)
[InlineData(jpgFirst, jpgSecond, 2259184, 15870, 0.05288166768853464)]
[InlineData(pngFirst, pngSecond, 2249290, 12570, 0.041885479700370536)]
public void SimilarFilesAreSimilar(string first, string second, int expAbsError, int expPixError, double expPixPercent)
{
var result = SKPixelComparer.Compare(GetImagePath(first), GetImagePath(second));

Assert.Equal(expAbsError, result.AbsoluteError);
Assert.Equal(expPixError, result.ErrorPixelCount);
Assert.Equal(expPixPercent, result.ErrorPixelPercentage);
}

[Theory]
[InlineData(jpgFirst, jpgSecond, 2259184, 15870, 0.05288166768853464)]
[InlineData(pngFirst, pngSecond, 2249290, 12570, 0.041885479700370536)]
public void SimilarImagesAreSimilar(string first, string second, int expAbsError, int expPixError, double expPixPercent)
{
using var firstImage = SKImage.FromEncodedData(GetImagePath(first));
using var secondImage = SKImage.FromEncodedData(GetImagePath(second));
Expand All @@ -116,6 +128,18 @@ public void SimilarFilesAreSimilar(string first, string second, int expAbsError,
[InlineData(jpgFirst, pngFirst, 884487, 231040, 0.7698664462986164)]
[InlineData(jpgSecond, pngSecond, 873399, 221697, 0.7387339055793991)]
public void SimilarFilesAreCompressedDifferent(string first, string second, int expAbsError, int expPixError, double expPixPercent)
{
var result = SKPixelComparer.Compare(GetImagePath(first), GetImagePath(second));

Assert.Equal(expAbsError, result.AbsoluteError);
Assert.Equal(expPixError, result.ErrorPixelCount);
Assert.Equal(expPixPercent, result.ErrorPixelPercentage);
}

[Theory]
[InlineData(jpgFirst, pngFirst, 884487, 231040, 0.7698664462986164)]
[InlineData(jpgSecond, pngSecond, 873399, 221697, 0.7387339055793991)]
public void SimilarImagesAreCompressedDifferent(string first, string second, int expAbsError, int expPixError, double expPixPercent)
{
using var firstImage = SKImage.FromEncodedData(GetImagePath(first));
using var secondImage = SKImage.FromEncodedData(GetImagePath(second));
Expand Down

0 comments on commit 7474171

Please sign in to comment.