From 994f5fc4a170300319f99f4a5e395bafc6523ac2 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 29 Apr 2018 23:44:39 +0200 Subject: [PATCH 1/2] "BlendedShapes" -> SolidFillBlendedShapesTests --- .../ImageSharp.Tests/Drawing/BlendedShapes.cs | 92 ----------- .../Drawing/SolidFillBlendedShapesTests.cs | 149 ++++++++++++++++++ .../Formats/Png/PngEncoderTests.cs | 2 +- .../TestUtilities/ImagingTestCaseUtility.cs | 52 ++++-- .../TestUtilities/TestImageExtensions.cs | 16 +- 5 files changed, 199 insertions(+), 112 deletions(-) delete mode 100644 tests/ImageSharp.Tests/Drawing/BlendedShapes.cs create mode 100644 tests/ImageSharp.Tests/Drawing/SolidFillBlendedShapesTests.cs diff --git a/tests/ImageSharp.Tests/Drawing/BlendedShapes.cs b/tests/ImageSharp.Tests/Drawing/BlendedShapes.cs deleted file mode 100644 index c39b5bc34..000000000 --- a/tests/ImageSharp.Tests/Drawing/BlendedShapes.cs +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System; -using System.Collections.Generic; -using System.Linq; -using SixLabors.ImageSharp.PixelFormats; -using SixLabors.ImageSharp.Processing.Drawing; -using SixLabors.Primitives; -using Xunit; - -namespace SixLabors.ImageSharp.Tests.Drawing -{ - using SixLabors.ImageSharp.Processing; - - public class BlendedShapes - { - public static IEnumerable modes = ((PixelBlenderMode[])Enum.GetValues(typeof(PixelBlenderMode))) - .Select(x => new object[] { x }); - - [Theory] - [WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)] - public void DrawBlendedValues(TestImageProvider provider, PixelBlenderMode mode) - where TPixel : struct, IPixel - { - using (var img = provider.GetImage()) - { - var scaleX = (img.Width / 100); - var scaleY = (img.Height / 100); - img.Mutate(x => x - .Fill(NamedColors.DarkBlue, new Rectangle(0 * scaleX, 40 * scaleY, 100 * scaleX, 20 * scaleY)) - .Fill(new GraphicsOptions(true) { BlenderMode = mode }, NamedColors.HotPink, new Rectangle(20 * scaleX, 0 * scaleY, 30 * scaleX, 100 * scaleY) - )); - img.DebugSave(provider, new { mode }); - } - } - - [Theory] - [WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)] - public void DrawBlendedValues_transparent(TestImageProvider provider, PixelBlenderMode mode) - where TPixel : struct, IPixel - { - using (var img = provider.GetImage()) - { - var scaleX = (img.Width / 100); - var scaleY = (img.Height / 100); - img.Mutate(x => x.Fill(NamedColors.DarkBlue, new Rectangle(0 * scaleX, 40 * scaleY, 100 * scaleX, 20 * scaleY))); - img.Mutate(x => x.Fill(new GraphicsOptions(true) { BlenderMode = mode }, NamedColors.HotPink, new Rectangle(20 * scaleX, 0 * scaleY, 30 * scaleX, 100 * scaleY))); - img.Mutate(x => x.Fill(new GraphicsOptions(true) { BlenderMode = mode }, NamedColors.Transparent, new Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY))); - img.DebugSave(provider, new { mode }); - } - } - - [Theory] - [WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)] - public void DrawBlendedValues_transparent50Percent(TestImageProvider provider, PixelBlenderMode mode) - where TPixel : struct, IPixel - { - using (var img = provider.GetImage()) - { - var scaleX = (img.Width / 100); - var scaleY = (img.Height / 100); - img.Mutate(x => x.Fill(NamedColors.DarkBlue, new Rectangle(0 * scaleX, 40, 100 * scaleX, 20 * scaleY))); - img.Mutate(x => x.Fill(new GraphicsOptions(true) { BlenderMode = mode }, NamedColors.HotPink, new Rectangle(20 * scaleX, 0, 30 * scaleX, 100 * scaleY))); - var c = NamedColors.Red.ToVector4(); - c.W *= 0.5f; - TPixel pixel = default(TPixel); - pixel.PackFromVector4(c); - - img.Mutate(x => x.Fill(new GraphicsOptions(true) { BlenderMode = mode }, pixel, new Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY))); - img.DebugSave(provider, new { mode }); - } - } - - - - [Theory] - [WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)] - public void DrawBlendedValues_doldidEllips(TestImageProvider provider, PixelBlenderMode mode) - where TPixel : struct, IPixel - { - using (var img = provider.GetImage()) - { - var scaleX = (img.Width / 100); - var scaleY = (img.Height / 100); - img.Mutate(x => x.Fill(NamedColors.DarkBlue, new Rectangle(0 * scaleX, 40 * scaleY, 100 * scaleX, 20 * scaleY))); - img.Mutate(x => x.Fill(new GraphicsOptions(true) { BlenderMode = mode }, NamedColors.Black, new Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY))); - img.DebugSave(provider, new { mode }); - } - } - } -} diff --git a/tests/ImageSharp.Tests/Drawing/SolidFillBlendedShapesTests.cs b/tests/ImageSharp.Tests/Drawing/SolidFillBlendedShapesTests.cs new file mode 100644 index 000000000..e23a09d52 --- /dev/null +++ b/tests/ImageSharp.Tests/Drawing/SolidFillBlendedShapesTests.cs @@ -0,0 +1,149 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +// ReSharper disable InconsistentNaming +namespace SixLabors.ImageSharp.Tests.Drawing +{ + using System; + using System.Collections.Generic; + using System.Linq; + + using SixLabors.ImageSharp.PixelFormats; + using SixLabors.ImageSharp.Processing; + using SixLabors.ImageSharp.Processing.Drawing; + using SixLabors.Primitives; + + using Xunit; + + [GroupOutput("Drawing")] + public class SolidFillBlendedShapesTests + { + public static IEnumerable modes = + ((PixelBlenderMode[])Enum.GetValues(typeof(PixelBlenderMode))).Select(x => new object[] { x }); + + [Theory] + [WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)] + public void _1DarkBlueRect_2BlendHotPinkRect( + TestImageProvider provider, + PixelBlenderMode mode) + where TPixel : struct, IPixel + { + using (Image img = provider.GetImage()) + { + int scaleX = img.Width / 100; + int scaleY = img.Height / 100; + img.Mutate( + x => x.Fill( + NamedColors.DarkBlue, + new Rectangle(0 * scaleX, 40 * scaleY, 100 * scaleX, 20 * scaleY) + ) + .Fill(new GraphicsOptions(true) { BlenderMode = mode }, + NamedColors.HotPink, + new Rectangle(20 * scaleX, 0 * scaleY, 30 * scaleX, 100 * scaleY)) + ); + + VerifyImage(provider, mode, img); + } + } + + [Theory] + [WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)] + public void _1DarkBlueRect_2BlendHotPinkRect_3BlendTransparentEllipse( + TestImageProvider provider, + PixelBlenderMode mode) + where TPixel : struct, IPixel + { + using (Image img = provider.GetImage()) + { + int scaleX = img.Width / 100; + int scaleY = img.Height / 100; + img.Mutate( + x => x.Fill( + NamedColors.DarkBlue, + new Rectangle(0 * scaleX, 40 * scaleY, 100 * scaleX, 20 * scaleY))); + img.Mutate( + x => x.Fill( + new GraphicsOptions(true) { BlenderMode = mode }, + NamedColors.HotPink, + new Rectangle(20 * scaleX, 0 * scaleY, 30 * scaleX, 100 * scaleY))); + img.Mutate( + x => x.Fill( + new GraphicsOptions(true) { BlenderMode = mode }, + NamedColors.Transparent, + new Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY)) + ); + + VerifyImage(provider, mode, img); + } + } + + [Theory] + [WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)] + public void _1DarkBlueRect_2BlendHotPinkRect_3BlendSemiTransparentRedEllipse( + TestImageProvider provider, + PixelBlenderMode mode) + where TPixel : struct, IPixel + { + using (Image img = provider.GetImage()) + { + int scaleX = (img.Width / 100); + int scaleY = (img.Height / 100); + img.Mutate( + x => x.Fill( + NamedColors.DarkBlue, + new Rectangle(0 * scaleX, 40, 100 * scaleX, 20 * scaleY))); + img.Mutate( + x => x.Fill( + new GraphicsOptions(true) { BlenderMode = mode }, + NamedColors.HotPink, + new Rectangle(20 * scaleX, 0, 30 * scaleX, 100 * scaleY))); + var c = NamedColors.Red.ToVector4(); + c.W *= 0.5f; + var pixel = default(TPixel); + pixel.PackFromVector4(c); + + img.Mutate( + x => x.Fill( + new GraphicsOptions(true) { BlenderMode = mode }, + pixel, + new Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY)) + ); + + VerifyImage(provider, mode, img); ; + } + } + + [Theory] + [WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)] + public void _1DarkBlueRect_2BlendBlackEllipse(TestImageProvider provider, PixelBlenderMode mode) + where TPixel : struct, IPixel + { + using (Image img = provider.GetImage()) + { + int scaleX = (img.Width / 100); + int scaleY = (img.Height / 100); + img.Mutate( + x => x.Fill( + NamedColors.DarkBlue, + new Rectangle(0 * scaleX, 40 * scaleY, 100 * scaleX, 20 * scaleY))); + img.Mutate( + x => x.Fill( + new GraphicsOptions(true) { BlenderMode = mode }, + NamedColors.Black, + new Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY))); + + VerifyImage(provider, mode, img); + } + } + + private static void VerifyImage(TestImageProvider provider, PixelBlenderMode mode, Image img) + where TPixel : struct, IPixel + { + img.DebugSave( + provider, + new { mode }, + appendPixelTypeToFileName: false, + appendSourceFileOrDescription: false); + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs index e9ae98d10..11124ad03 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs @@ -153,7 +153,7 @@ namespace SixLabors.ImageSharp.Tests } IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(actualOutputFile); - string referenceOutputFile = ((ITestImageProvider)provider).Utility.GetReferenceOutputFileName("png", debugInfo, appendPixelType); + string referenceOutputFile = ((ITestImageProvider)provider).Utility.GetReferenceOutputFileName("png", debugInfo, appendPixelType, true); using (var actualImage = Image.Load(actualOutputFile, referenceDecoder)) using (var referenceImage = Image.Load(referenceOutputFile, referenceDecoder)) diff --git a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs index cde8ec9e4..340fc600a 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs @@ -41,16 +41,20 @@ namespace SixLabors.ImageSharp.Tests /// public string TestName { get; set; } = string.Empty; - private string GetTestOutputFileNameImpl(string extension, string details, bool appendPixelTypeToFileName) + private string GetTestOutputFileNameImpl( + string extension, + string details, + bool appendPixelTypeToFileName, + bool appendSourceFileOrDescription) { - string fn = string.Empty; - if (string.IsNullOrWhiteSpace(extension)) { extension = null; } - fn = Path.GetFileNameWithoutExtension(this.SourceFileOrDescription); + string fn = appendSourceFileOrDescription + ? Path.GetFileNameWithoutExtension(this.SourceFileOrDescription) + : ""; if (string.IsNullOrWhiteSpace(extension)) { @@ -92,20 +96,24 @@ namespace SixLabors.ImageSharp.Tests } private static string Inv(FormattableString formattable) => System.FormattableString.Invariant(formattable); - + /// /// Gets the recommended file name for the output of the test /// /// The required extension /// The settings modifying the output path /// A boolean indicating whether to append the pixel type to output file name. + /// A boolean indicating whether to append to the test output file name. /// The file test name - public string GetTestOutputFileName(string extension = null, object testOutputDetails = null, bool appendPixelTypeToFileName = true) + public string GetTestOutputFileName( + string extension = null, + object testOutputDetails = null, + bool appendPixelTypeToFileName = true, + bool appendSourceFileOrDescription = true) { string detailsString = null; - string s = testOutputDetails as string; - if (s != null) + if (testOutputDetails is string s) { detailsString = s; } @@ -128,7 +136,12 @@ namespace SixLabors.ImageSharp.Tests ); } } - return this.GetTestOutputFileNameImpl(extension, detailsString, appendPixelTypeToFileName); + + return this.GetTestOutputFileNameImpl( + extension, + detailsString, + appendPixelTypeToFileName, + appendSourceFileOrDescription); } @@ -139,15 +152,22 @@ namespace SixLabors.ImageSharp.Tests /// The image instance /// The requested extension /// Optional encoder + /// /// A boolean indicating whether to append to the test output file name. public string SaveTestOutputFile( Image image, string extension = null, IImageEncoder encoder = null, object testOutputDetails = null, - bool appendPixelTypeToFileName = true) + bool appendPixelTypeToFileName = true, + bool appendSourceFileOrDescription = true) where TPixel : struct, IPixel { - string path = this.GetTestOutputFileName(extension, testOutputDetails, appendPixelTypeToFileName); + string path = this.GetTestOutputFileName( + extension, + testOutputDetails, + appendPixelTypeToFileName, + appendSourceFileOrDescription); + encoder = encoder ?? TestEnvironment.GetReferenceEncoder(path); using (FileStream stream = File.OpenWrite(path)) @@ -161,9 +181,10 @@ namespace SixLabors.ImageSharp.Tests int frameCount, string extension = null, object testOutputDetails = null, - bool appendPixelTypeToFileName = true) + bool appendPixelTypeToFileName = true, + bool appendSourceFileOrDescription = true) { - string baseDir = this.GetTestOutputFileName("", testOutputDetails, appendPixelTypeToFileName); + string baseDir = this.GetTestOutputFileName("", testOutputDetails, appendPixelTypeToFileName, appendSourceFileOrDescription); if (!Directory.Exists(baseDir)) { @@ -211,10 +232,11 @@ namespace SixLabors.ImageSharp.Tests internal string GetReferenceOutputFileName( string extension, object testOutputDetails, - bool appendPixelTypeToFileName) + bool appendPixelTypeToFileName, + bool appendSourceFileOrDescription) { return TestEnvironment.GetReferenceOutputFileName( - this.GetTestOutputFileName(extension, testOutputDetails, appendPixelTypeToFileName) + this.GetTestOutputFileName(extension, testOutputDetails, appendPixelTypeToFileName, appendSourceFileOrDescription) ); } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index ee0382dbe..da335555d 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -61,12 +61,14 @@ namespace SixLabors.ImageSharp.Tests /// Details to be concatenated to the test output file, describing the parameters of the test. /// The extension /// A boolean indicating whether to append the pixel type to the output file name. + /// A boolean indicating whether to append to the test output file name. public static Image DebugSave( this Image image, ITestImageProvider provider, object testOutputDetails = null, string extension = "png", - bool appendPixelTypeToFileName = true) + bool appendPixelTypeToFileName = true, + bool appendSourceFileOrDescription = true) where TPixel : struct, IPixel { if (TestEnvironment.RunsOnCI) @@ -79,7 +81,8 @@ namespace SixLabors.ImageSharp.Tests image, extension, testOutputDetails: testOutputDetails, - appendPixelTypeToFileName: appendPixelTypeToFileName); + appendPixelTypeToFileName: appendPixelTypeToFileName, + appendSourceFileOrDescription: appendSourceFileOrDescription); return image; } @@ -255,10 +258,15 @@ namespace SixLabors.ImageSharp.Tests public static Image GetReferenceOutputImage(this ITestImageProvider provider, object testOutputDetails = null, string extension = "png", - bool appendPixelTypeToFileName = true) + bool appendPixelTypeToFileName = true, + bool appendSourceFileOrDescription = true) where TPixel : struct, IPixel { - string referenceOutputFile = provider.Utility.GetReferenceOutputFileName(extension, testOutputDetails, appendPixelTypeToFileName); + string referenceOutputFile = provider.Utility.GetReferenceOutputFileName( + extension, + testOutputDetails, + appendPixelTypeToFileName, + appendSourceFileOrDescription); if (!File.Exists(referenceOutputFile)) { From 34ed38db8895f31c54d7e900b1f305599d558019 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 29 Apr 2018 23:52:39 +0200 Subject: [PATCH 2/2] assertions for SolidFillBlendedShapesTests --- .../Drawing/SolidFillBlendedShapesTests.cs | 27 +++++++++++-------- .../TestUtilities/TestImageExtensions.cs | 6 +++-- tests/Images/External | 2 +- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/tests/ImageSharp.Tests/Drawing/SolidFillBlendedShapesTests.cs b/tests/ImageSharp.Tests/Drawing/SolidFillBlendedShapesTests.cs index e23a09d52..7d73d1b65 100644 --- a/tests/ImageSharp.Tests/Drawing/SolidFillBlendedShapesTests.cs +++ b/tests/ImageSharp.Tests/Drawing/SolidFillBlendedShapesTests.cs @@ -1,20 +1,18 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System; +using System.Collections.Generic; +using System.Linq; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; +using SixLabors.ImageSharp.Processing.Drawing; +using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; +using SixLabors.Primitives; +using Xunit; // ReSharper disable InconsistentNaming namespace SixLabors.ImageSharp.Tests.Drawing { - using System; - using System.Collections.Generic; - using System.Linq; - - using SixLabors.ImageSharp.PixelFormats; - using SixLabors.ImageSharp.Processing; - using SixLabors.ImageSharp.Processing.Drawing; - using SixLabors.Primitives; - - using Xunit; - [GroupOutput("Drawing")] public class SolidFillBlendedShapesTests { @@ -144,6 +142,13 @@ namespace SixLabors.ImageSharp.Tests.Drawing new { mode }, appendPixelTypeToFileName: false, appendSourceFileOrDescription: false); + + var comparer = ImageComparer.TolerantPercentage(0.01f, 3); + img.CompareFirstFrameToReferenceOutput(comparer, + provider, + new { mode }, + appendPixelTypeToFileName: false, + appendSourceFileOrDescription: false); } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index da335555d..c2c0eb487 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -213,7 +213,8 @@ namespace SixLabors.ImageSharp.Tests object testOutputDetails = null, string extension = "png", bool grayscale = false, - bool appendPixelTypeToFileName = true) + bool appendPixelTypeToFileName = true, + bool appendSourceFileOrDescription = true) where TPixel : struct, IPixel { using (var firstFrameOnlyImage = new Image(image.Width, image.Height)) @@ -221,7 +222,8 @@ namespace SixLabors.ImageSharp.Tests provider, testOutputDetails, extension, - appendPixelTypeToFileName)) + appendPixelTypeToFileName, + appendSourceFileOrDescription)) { firstFrameOnlyImage.Frames.AddFrame(image.Frames.RootFrame); firstFrameOnlyImage.Frames.RemoveFrame(0); diff --git a/tests/Images/External b/tests/Images/External index 558729ec8..5a9a88380 160000 --- a/tests/Images/External +++ b/tests/Images/External @@ -1 +1 @@ -Subproject commit 558729ec87bcf52f22362175842f88a81ccfc483 +Subproject commit 5a9a88380166a87521d10048f53cda7f5f761d66