From cf06ff4d5980a3e5bcbe00f7909dd0bc2a66fcc7 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 29 May 2017 22:17:46 +1000 Subject: [PATCH] Finish dither tests --- .../Binarization/BinaryThresholdTest.cs | 15 +++- .../Processing/Binarization/DitherTest.cs | 86 +++++++++---------- .../Attributes/WithFileCollectionAttribute.cs | 17 ++-- .../TestUtilities/TestImageExtensions.cs | 12 ++- 4 files changed, 70 insertions(+), 60 deletions(-) diff --git a/tests/ImageSharp.Tests/Processing/Binarization/BinaryThresholdTest.cs b/tests/ImageSharp.Tests/Processing/Binarization/BinaryThresholdTest.cs index f3dcbe1cd..7286375ba 100644 --- a/tests/ImageSharp.Tests/Processing/Binarization/BinaryThresholdTest.cs +++ b/tests/ImageSharp.Tests/Processing/Binarization/BinaryThresholdTest.cs @@ -11,20 +11,27 @@ namespace ImageSharp.Tests.Processing.Binarization public class BinaryThresholdTest : FileTestBase { + public static readonly TheoryData BinaryThresholdValues + = new TheoryData + { + .25F, + .75F + }; + [Theory] - [WithFileCollection(nameof(AllBmpFiles), StandardPixelTypes, .75F)] + [WithFileCollection(nameof(AllBmpFiles), nameof(BinaryThresholdValues), StandardPixelTypes)] public void ImageShouldApplyBinaryThresholdFilter(TestImageProvider provider, float value) where TPixel : struct, IPixel { using (Image image = provider.GetImage()) { image.BinaryThreshold(value) - .DebugSave(provider, null, Extensions.Bmp); + .DebugSave(provider, value, Extensions.Bmp); } } [Theory] - [WithFileCollection(nameof(AllBmpFiles), StandardPixelTypes, .75F)] + [WithFileCollection(nameof(AllBmpFiles), nameof(BinaryThresholdValues), StandardPixelTypes)] public void ImageShouldApplyBinaryThresholdInBox(TestImageProvider provider, float value) where TPixel : struct, IPixel { @@ -35,7 +42,7 @@ namespace ImageSharp.Tests.Processing.Binarization var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2); image.BinaryThreshold(value, bounds) - .DebugSave(provider, null, Extensions.Bmp); + .DebugSave(provider, value, Extensions.Bmp); // Draw identical shapes over the bounded and compare to ensure changes are constrained. image.Fill(NamedColors.HotPink, bounds); diff --git a/tests/ImageSharp.Tests/Processing/Binarization/DitherTest.cs b/tests/ImageSharp.Tests/Processing/Binarization/DitherTest.cs index 066e2d134..c88227b2c 100644 --- a/tests/ImageSharp.Tests/Processing/Binarization/DitherTest.cs +++ b/tests/ImageSharp.Tests/Processing/Binarization/DitherTest.cs @@ -5,8 +5,6 @@ namespace ImageSharp.Tests { - using System.IO; - using ImageSharp.Dithering; using ImageSharp.Dithering.Ordered; using ImageSharp.PixelFormats; @@ -34,70 +32,66 @@ namespace ImageSharp.Tests }; [Theory] - [MemberData(nameof(Ditherers))] - public void ImageShouldApplyDitherFilter(string name, IOrderedDither ditherer) + [WithFileCollection(nameof(AllBmpFiles), nameof(Ditherers), StandardPixelTypes)] + public void ImageShouldApplyDitherFilter(TestImageProvider provider, string name, IOrderedDither ditherer) + where TPixel : struct, IPixel { - string path = this.CreateOutputDirectory("Dither", "Dither"); - - foreach (TestFile file in Files) + using (Image image = provider.GetImage()) { - string filename = file.GetFileName(name); - using (Image image = file.CreateImage()) - using (FileStream output = File.OpenWrite($"{path}/{filename}")) - { - image.Dither(ditherer).Save(output); - } + image.Dither(ditherer) + .DebugSave(provider, name, Extensions.Bmp); } } [Theory] - [MemberData(nameof(Ditherers))] - public void ImageShouldApplyDitherFilterInBox(string name, IOrderedDither ditherer) + [WithFileCollection(nameof(AllBmpFiles), nameof(Ditherers), StandardPixelTypes)] + public void ImageShouldApplyDitherFilterInBox(TestImageProvider provider, string name, IOrderedDither ditherer) + where TPixel : struct, IPixel { - string path = this.CreateOutputDirectory("Dither", "Dither"); - - foreach (TestFile file in Files) + using (Image source = provider.GetImage()) + using (var image = new Image(source)) { - string filename = file.GetFileName($"{name}-InBox"); - using (Image image = file.CreateImage()) - using (FileStream output = File.OpenWrite($"{path}/{filename}")) - { - image.Dither(ditherer, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); - } + var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2); + + image.Dither(ditherer, bounds) + .DebugSave(provider, name, Extensions.Bmp); + + // Draw identical shapes over the bounded and compare to ensure changes are constrained. + image.Fill(NamedColors.HotPink, bounds); + source.Fill(NamedColors.HotPink, bounds); + ImageComparer.CheckSimilarity(image, source); } } [Theory] - [MemberData(nameof(ErrorDiffusers))] - public void ImageShouldApplyDiffusionFilter(string name, IErrorDiffuser diffuser) + [WithFileCollection(nameof(AllBmpFiles), nameof(ErrorDiffusers), StandardPixelTypes)] + public void ImageShouldApplyDiffusionFilter(TestImageProvider provider, string name, IErrorDiffuser diffuser) + where TPixel : struct, IPixel { - string path = this.CreateOutputDirectory("Dither", "Diffusion"); - - foreach (TestFile file in Files) + using (Image image = provider.GetImage()) { - string filename = file.GetFileName(name); - using (Image image = file.CreateImage()) - using (FileStream output = File.OpenWrite($"{path}/{filename}")) - { - image.Dither(diffuser, .5F).Save(output); - } + image.Dither(diffuser, .5F) + .DebugSave(provider, name, Extensions.Bmp); } } [Theory] - [MemberData(nameof(ErrorDiffusers))] - public void ImageShouldApplyDiffusionFilterInBox(string name, IErrorDiffuser diffuser) + [WithFileCollection(nameof(AllBmpFiles), nameof(ErrorDiffusers), StandardPixelTypes)] + public void ImageShouldApplyDiffusionFilterInBox(TestImageProvider provider, string name, IErrorDiffuser diffuser) + where TPixel : struct, IPixel { - string path = this.CreateOutputDirectory("Dither", "Diffusion"); - - foreach (TestFile file in Files) + using (Image source = provider.GetImage()) + using (var image = new Image(source)) { - string filename = file.GetFileName($"{name}-InBox"); - using (Image image = file.CreateImage()) - using (FileStream output = File.OpenWrite($"{path}/{filename}")) - { - image.Dither(diffuser, .5F, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); - } + var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2); + + image.Dither(diffuser,.5F, bounds) + .DebugSave(provider, name, Extensions.Bmp); + + // Draw identical shapes over the bounded and compare to ensure changes are constrained. + image.Fill(NamedColors.HotPink, bounds); + source.Fill(NamedColors.HotPink, bounds); + ImageComparer.CheckSimilarity(image, source); } } } diff --git a/tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileCollectionAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileCollectionAttribute.cs index 6f445dfcc..1b37c45a9 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileCollectionAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileCollectionAttribute.cs @@ -16,22 +16,22 @@ namespace ImageSharp.Tests /// public class WithFileCollectionAttribute : ImageDataAttributeBase { - private readonly string enumeratorMemberName; + private readonly string fileEnumeratorMemberName; /// /// Triggers passing instances which read an image for each file being enumerated by the (static) test class field/property defined by enumeratorMemberName /// instances will be passed for each the pixel format defined by the pixelTypes parameter /// - /// The name of the static test class field/property enumerating the files + /// The name of the static test class field/property enumerating the files /// The requested pixel types /// Additional theory parameter values public WithFileCollectionAttribute( - string enumeratorMemberName, + string fileEnumeratorMemberName, PixelTypes pixelTypes, params object[] additionalParameters) : base(null, pixelTypes, additionalParameters) { - this.enumeratorMemberName = enumeratorMemberName; + this.fileEnumeratorMemberName = fileEnumeratorMemberName; } /// @@ -39,7 +39,7 @@ namespace ImageSharp.Tests /// instances will be passed for each the pixel format defined by the pixelTypes parameter /// /// The name of the static test class field/property enumerating the files - /// The member name + /// The member name for enumerating method parameters /// The requested pixel types /// Additional theory parameter values public WithFileCollectionAttribute( @@ -49,7 +49,7 @@ namespace ImageSharp.Tests params object[] additionalParameters) : base(memberName, pixelTypes, additionalParameters) { - this.enumeratorMemberName = enumeratorMemberName; + this.fileEnumeratorMemberName = enumeratorMemberName; } /// @@ -67,6 +67,7 @@ namespace ImageSharp.Tests return files.Select(f => new object[] { f }); } + /// protected override string GetFactoryMethodName(MethodInfo testMethod) => "File"; /// @@ -79,7 +80,7 @@ namespace ImageSharp.Tests reflectionType != null; reflectionType = reflectionType.GetTypeInfo().BaseType) { - fieldInfo = reflectionType.GetRuntimeField(this.enumeratorMemberName); + fieldInfo = reflectionType.GetRuntimeField(this.fileEnumeratorMemberName); if (fieldInfo != null) { break; @@ -104,7 +105,7 @@ namespace ImageSharp.Tests reflectionType != null; reflectionType = reflectionType.GetTypeInfo().BaseType) { - propInfo = reflectionType.GetRuntimeProperty(this.enumeratorMemberName); + propInfo = reflectionType.GetRuntimeProperty(this.fileEnumeratorMemberName); if (propInfo != null) break; } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index 064c52064..1c5038014 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -2,6 +2,7 @@ namespace ImageSharp.Tests { using System; + using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -28,9 +29,16 @@ namespace ImageSharp.Tests } else if (settings != null) { - System.Collections.Generic.IEnumerable properties = settings.GetType().GetRuntimeProperties(); + if (settings.GetType().GetTypeInfo().IsPrimitive) + { + tag = settings.ToString(); + } + else + { + IEnumerable properties = settings.GetType().GetRuntimeProperties(); - tag = string.Join("_", properties.ToDictionary(x => x.Name, x => x.GetValue(settings)).Select(x => $"{x.Key}-{x.Value}")); + tag = string.Join("_", properties.ToDictionary(x => x.Name, x => x.GetValue(settings)).Select(x => $"{x.Key}-{x.Value}")); + } } if (!bool.TryParse(Environment.GetEnvironmentVariable("CI"), out bool isCi) || !isCi) {