Skip to content

Commit

Permalink
Merge pull request SixLabors#682 from SixLabors/js/image-blending-tests
Browse files Browse the repository at this point in the history
Add image blending tests to match the SVG spec examples
  • Loading branch information
JimBobSquarePants authored Aug 24, 2018
2 parents 20dd97c + 1adf129 commit a734cdf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
53 changes: 47 additions & 6 deletions tests/ImageSharp.Tests/Drawing/DrawImageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
using System.Numerics;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Transforms;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using SixLabors.Primitives;
using Xunit;

namespace SixLabors.ImageSharp.Tests
{
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Transforms;

[GroupOutput("Drawing")]
public class DrawImageTest : FileTestBase
{
private const PixelTypes PixelTypes = Tests.PixelTypes.Rgba32;
Expand Down Expand Up @@ -41,11 +41,32 @@ public void ImageShouldApplyDrawImage<TPixel>(TestImageProvider<TPixel> provider
using (var blend = Image.Load<TPixel>(TestFile.Create(TestImages.Bmp.Car).Bytes))
{
blend.Mutate(x => x.Resize(image.Width / 2, image.Height / 2));
image.Mutate(x => x.DrawImage(blend, new Point(image.Width / 4, image.Height / 4), mode, .75f) );
image.Mutate(x => x.DrawImage(blend, new Point(image.Width / 4, image.Height / 4), mode, .75f));
image.DebugSave(provider, new { mode });
}
}

[Theory]
[WithFile(TestImages.Png.Rainbow, PixelTypes, PixelColorBlendingMode.Normal)]
[WithFile(TestImages.Png.Rainbow, PixelTypes, PixelColorBlendingMode.Multiply)]
[WithFile(TestImages.Png.Rainbow, PixelTypes, PixelColorBlendingMode.Add)]
[WithFile(TestImages.Png.Rainbow, PixelTypes, PixelColorBlendingMode.Subtract)]
[WithFile(TestImages.Png.Rainbow, PixelTypes, PixelColorBlendingMode.Screen)]
[WithFile(TestImages.Png.Rainbow, PixelTypes, PixelColorBlendingMode.Darken)]
[WithFile(TestImages.Png.Rainbow, PixelTypes, PixelColorBlendingMode.Lighten)]
[WithFile(TestImages.Png.Rainbow, PixelTypes, PixelColorBlendingMode.Overlay)]
[WithFile(TestImages.Png.Rainbow, PixelTypes, PixelColorBlendingMode.HardLight)]
public void ImageBlendingMatchesSvgSpecExamples<TPixel>(TestImageProvider<TPixel> provider, PixelColorBlendingMode mode)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> background = provider.GetImage())
using (var source = Image.Load<TPixel>(TestFile.Create(TestImages.Png.Ducky).Bytes))
{
background.Mutate(x => x.DrawImage(source, mode, 1F));
VerifyImage(provider, mode, background);
}
}

[Theory]
[WithFileCollection(nameof(TestFiles), PixelTypes, PixelColorBlendingMode.Normal)]
public void ImageShouldDrawTransformedImage<TPixel>(TestImageProvider<TPixel> provider, PixelColorBlendingMode mode)
Expand Down Expand Up @@ -84,7 +105,7 @@ public void ImageShouldHandleNegativeLocation(TestImageProvider<Rgba32> provider
{
overlay.Mutate(x => x.Fill(Rgba32.Black));

int xy = -25;
const int xy = -25;
Rgba32 backgroundPixel = background[0, 0];
Rgba32 overlayPixel = overlay[Math.Abs(xy) + 1, Math.Abs(xy) + 1];

Expand All @@ -106,7 +127,7 @@ public void ImageShouldHandlePositiveLocation(TestImageProvider<Rgba32> provider
{
overlay.Mutate(x => x.Fill(Rgba32.Black));

int xy = 25;
const int xy = 25;
Rgba32 backgroundPixel = background[xy - 1, xy - 1];
Rgba32 overlayPixel = overlay[0, 0];

Expand All @@ -118,5 +139,25 @@ public void ImageShouldHandlePositiveLocation(TestImageProvider<Rgba32> provider
background.DebugSave(provider, testOutputDetails: "Positive");
}
}

private static void VerifyImage<TPixel>(
TestImageProvider<TPixel> provider,
PixelColorBlendingMode mode,
Image<TPixel> img)
where TPixel : struct, IPixel<TPixel>
{
img.DebugSave(
provider,
new { mode },
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: false);

var comparer = ImageComparer.TolerantPercentage(0.01F, 3);
img.CompareFirstFrameToReferenceOutput(comparer,
provider,
new { mode },
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: false);
}
}
}
3 changes: 3 additions & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public static class Png
public const string Ratio1x4 = "Png/ratio-1x4.png";
public const string Ratio4x1 = "Png/ratio-4x1.png";

public const string Ducky = "Png/ducky.png";
public const string Rainbow = "Png/rainbow.png";

public static class Bad
{
// Odd chunk lengths
Expand Down

0 comments on commit a734cdf

Please sign in to comment.