From 0dcbf0af2efb7e7991335491a816fd92062564ec Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 29 May 2017 18:20:44 +1000 Subject: [PATCH] BinaryThresholdTests as template --- src/ImageSharp/Image/ImageBase{TPixel}.cs | 2 +- .../Image/ImageProcessingExtensions.cs | 2 +- src/ImageSharp/Image/Image{TPixel}.cs | 2 +- .../Processing/ColorMatrix/BlackWhite.cs | 2 +- .../Processing/ColorMatrix/ColorBlindness.cs | 2 +- .../Processing/ColorMatrix/Grayscale.cs | 2 +- src/ImageSharp/Processing/ColorMatrix/Hue.cs | 2 +- .../Processing/ColorMatrix/Kodachrome.cs | 2 +- .../Processing/ColorMatrix/Lomograph.cs | 2 +- .../Processing/ColorMatrix/Polaroid.cs | 2 +- .../Processing/ColorMatrix/Saturation.cs | 2 +- .../Processing/ColorMatrix/Sepia.cs | 2 +- .../Processing/Convolution/DetectEdges.cs | 2 +- .../Processing/Convolution/GaussianBlur.cs | 2 +- .../Processing/Convolution/GaussianSharpen.cs | 2 +- .../Processing/Transforms/AutoOrient.cs | 2 +- src/ImageSharp/Processing/Transforms/Flip.cs | 2 +- src/ImageSharp/Processing/Transforms/Pad.cs | 2 +- .../Processing/Transforms/Resize.cs | 2 +- .../Processing/Transforms/Rotate.cs | 2 +- .../Processing/Transforms/RotateFlip.cs | 2 +- .../Samplers/DetectEdges.cs | 2 +- .../Drawing/Paths/Extensions.cs | 2 +- .../Drawing/Paths/ProcessorWatchingImage.cs | 2 +- .../Drawing/Paths/ShapePathTests.cs | 2 +- .../Drawing/Paths/ShapeRegionTests.cs | 2 +- .../Drawing/Text/OutputText.cs | 2 +- tests/ImageSharp.Tests/FileTestBase.cs | 36 +++++++ .../Formats/Png/PngDecoderTests.cs | 2 +- .../Binarization/BinaryThresholdTest.cs | 47 ++++++++++ .../Binarization}/DitherTest.cs | 0 .../Processors/Filters/AutoOrientTests.cs | 2 +- .../Processors/Filters/BinaryThresholdTest.cs | 57 ------------ .../Processors/Filters/ColorBlindnessTest.cs | 2 +- .../Processors/Filters/DetectEdgesTest.cs | 2 +- .../Processors/Filters/FlipTests.cs | 2 +- .../Processors/Filters/ResizeTests.cs | 2 +- .../Processors/Filters/RotateFlipTest.cs | 2 +- .../Processors/Filters/RotateTest.cs | 2 +- tests/ImageSharp.Tests/TestImages.cs | 19 +++- .../Attributes/ImageDataAttributeBase.cs | 93 ++++++++++++------- .../Attributes/WithBlankImageAttribute.cs | 1 + .../Attributes/WithFileCollectionAttribute.cs | 35 +++++-- .../ImageProviders/FileProvider.cs | 5 +- .../TestUtilities/TestImageExtensions.cs | 27 ++++-- .../TestUtilities/TestUtilityExtensions.cs | 5 + 46 files changed, 243 insertions(+), 152 deletions(-) create mode 100644 tests/ImageSharp.Tests/Processing/Binarization/BinaryThresholdTest.cs rename tests/ImageSharp.Tests/{Processors/Filters => Processing/Binarization}/DitherTest.cs (100%) delete mode 100644 tests/ImageSharp.Tests/Processors/Filters/BinaryThresholdTest.cs diff --git a/src/ImageSharp/Image/ImageBase{TPixel}.cs b/src/ImageSharp/Image/ImageBase{TPixel}.cs index 508c73eb2b..647d60075d 100644 --- a/src/ImageSharp/Image/ImageBase{TPixel}.cs +++ b/src/ImageSharp/Image/ImageBase{TPixel}.cs @@ -11,7 +11,7 @@ namespace ImageSharp using ImageSharp.Memory; using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; /// /// The base class of all images. Encapsulates the basic properties and methods required to manipulate diff --git a/src/ImageSharp/Image/ImageProcessingExtensions.cs b/src/ImageSharp/Image/ImageProcessingExtensions.cs index c9577ac15f..8eed103d10 100644 --- a/src/ImageSharp/Image/ImageProcessingExtensions.cs +++ b/src/ImageSharp/Image/ImageProcessingExtensions.cs @@ -7,7 +7,7 @@ namespace ImageSharp { using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; /// /// Extension methods for the type. diff --git a/src/ImageSharp/Image/Image{TPixel}.cs b/src/ImageSharp/Image/Image{TPixel}.cs index 092706e416..059ccb9a07 100644 --- a/src/ImageSharp/Image/Image{TPixel}.cs +++ b/src/ImageSharp/Image/Image{TPixel}.cs @@ -15,7 +15,7 @@ namespace ImageSharp using Formats; using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; /// /// Encapsulates an image, which consists of the pixel data for a graphics image and its attributes. diff --git a/src/ImageSharp/Processing/ColorMatrix/BlackWhite.cs b/src/ImageSharp/Processing/ColorMatrix/BlackWhite.cs index 76977455a4..de543f2cd9 100644 --- a/src/ImageSharp/Processing/ColorMatrix/BlackWhite.cs +++ b/src/ImageSharp/Processing/ColorMatrix/BlackWhite.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/ColorMatrix/ColorBlindness.cs b/src/ImageSharp/Processing/ColorMatrix/ColorBlindness.cs index d012d6fe2b..c48a862930 100644 --- a/src/ImageSharp/Processing/ColorMatrix/ColorBlindness.cs +++ b/src/ImageSharp/Processing/ColorMatrix/ColorBlindness.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/ColorMatrix/Grayscale.cs b/src/ImageSharp/Processing/ColorMatrix/Grayscale.cs index 8700b63e88..89bc2a9478 100644 --- a/src/ImageSharp/Processing/ColorMatrix/Grayscale.cs +++ b/src/ImageSharp/Processing/ColorMatrix/Grayscale.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/ColorMatrix/Hue.cs b/src/ImageSharp/Processing/ColorMatrix/Hue.cs index 8dbc555307..bcaf68d9a9 100644 --- a/src/ImageSharp/Processing/ColorMatrix/Hue.cs +++ b/src/ImageSharp/Processing/ColorMatrix/Hue.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/ColorMatrix/Kodachrome.cs b/src/ImageSharp/Processing/ColorMatrix/Kodachrome.cs index 13a71a71ef..a302bb2e90 100644 --- a/src/ImageSharp/Processing/ColorMatrix/Kodachrome.cs +++ b/src/ImageSharp/Processing/ColorMatrix/Kodachrome.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/ColorMatrix/Lomograph.cs b/src/ImageSharp/Processing/ColorMatrix/Lomograph.cs index 937dca9ba8..cbfeb7d055 100644 --- a/src/ImageSharp/Processing/ColorMatrix/Lomograph.cs +++ b/src/ImageSharp/Processing/ColorMatrix/Lomograph.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/ColorMatrix/Polaroid.cs b/src/ImageSharp/Processing/ColorMatrix/Polaroid.cs index f1a573c05d..ac9f7391f1 100644 --- a/src/ImageSharp/Processing/ColorMatrix/Polaroid.cs +++ b/src/ImageSharp/Processing/ColorMatrix/Polaroid.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/ColorMatrix/Saturation.cs b/src/ImageSharp/Processing/ColorMatrix/Saturation.cs index c41f304b4e..ffe336c62d 100644 --- a/src/ImageSharp/Processing/ColorMatrix/Saturation.cs +++ b/src/ImageSharp/Processing/ColorMatrix/Saturation.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/ColorMatrix/Sepia.cs b/src/ImageSharp/Processing/ColorMatrix/Sepia.cs index 39eca4e8ef..8488dcfe5a 100644 --- a/src/ImageSharp/Processing/ColorMatrix/Sepia.cs +++ b/src/ImageSharp/Processing/ColorMatrix/Sepia.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/Convolution/DetectEdges.cs b/src/ImageSharp/Processing/Convolution/DetectEdges.cs index 3aa1d0b513..368688f624 100644 --- a/src/ImageSharp/Processing/Convolution/DetectEdges.cs +++ b/src/ImageSharp/Processing/Convolution/DetectEdges.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/Convolution/GaussianBlur.cs b/src/ImageSharp/Processing/Convolution/GaussianBlur.cs index 72abec6df9..0b8c5383c8 100644 --- a/src/ImageSharp/Processing/Convolution/GaussianBlur.cs +++ b/src/ImageSharp/Processing/Convolution/GaussianBlur.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/Convolution/GaussianSharpen.cs b/src/ImageSharp/Processing/Convolution/GaussianSharpen.cs index 2ed99ea260..1a5d3c2df5 100644 --- a/src/ImageSharp/Processing/Convolution/GaussianSharpen.cs +++ b/src/ImageSharp/Processing/Convolution/GaussianSharpen.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/Transforms/AutoOrient.cs b/src/ImageSharp/Processing/Transforms/AutoOrient.cs index f9d3a60aa1..b892db41a4 100644 --- a/src/ImageSharp/Processing/Transforms/AutoOrient.cs +++ b/src/ImageSharp/Processing/Transforms/AutoOrient.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/Transforms/Flip.cs b/src/ImageSharp/Processing/Transforms/Flip.cs index 342b4dd46e..1c8baebf13 100644 --- a/src/ImageSharp/Processing/Transforms/Flip.cs +++ b/src/ImageSharp/Processing/Transforms/Flip.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/Transforms/Pad.cs b/src/ImageSharp/Processing/Transforms/Pad.cs index 42851e205d..4bded07724 100644 --- a/src/ImageSharp/Processing/Transforms/Pad.cs +++ b/src/ImageSharp/Processing/Transforms/Pad.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/Transforms/Resize.cs b/src/ImageSharp/Processing/Transforms/Resize.cs index 543b982976..0ed682648d 100644 --- a/src/ImageSharp/Processing/Transforms/Resize.cs +++ b/src/ImageSharp/Processing/Transforms/Resize.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/Transforms/Rotate.cs b/src/ImageSharp/Processing/Transforms/Rotate.cs index de9dbdc3bc..af7c06a27e 100644 --- a/src/ImageSharp/Processing/Transforms/Rotate.cs +++ b/src/ImageSharp/Processing/Transforms/Rotate.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Processing.Processors; /// diff --git a/src/ImageSharp/Processing/Transforms/RotateFlip.cs b/src/ImageSharp/Processing/Transforms/RotateFlip.cs index 460d004cbc..805deb8d16 100644 --- a/src/ImageSharp/Processing/Transforms/RotateFlip.cs +++ b/src/ImageSharp/Processing/Transforms/RotateFlip.cs @@ -9,7 +9,7 @@ namespace ImageSharp using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; /// /// Extension methods for the type. diff --git a/tests/ImageSharp.Benchmarks/Samplers/DetectEdges.cs b/tests/ImageSharp.Benchmarks/Samplers/DetectEdges.cs index d4920ff081..e77716ec85 100644 --- a/tests/ImageSharp.Benchmarks/Samplers/DetectEdges.cs +++ b/tests/ImageSharp.Benchmarks/Samplers/DetectEdges.cs @@ -9,7 +9,7 @@ namespace ImageSharp.Benchmarks using BenchmarkDotNet.Attributes; - using Processing; + using ImageSharp.Processing; using CoreImage = ImageSharp.Image; diff --git a/tests/ImageSharp.Tests/Drawing/Paths/Extensions.cs b/tests/ImageSharp.Tests/Drawing/Paths/Extensions.cs index a18dd90bc2..24f2a6bd8b 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/Extensions.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/Extensions.cs @@ -5,7 +5,7 @@ namespace ImageSharp.Tests.Drawing.Paths using System.IO; using ImageSharp; using ImageSharp.Drawing.Brushes; - using Processing; + using ImageSharp.Processing; using System.Collections.Generic; using Xunit; using ImageSharp.Drawing; diff --git a/tests/ImageSharp.Tests/Drawing/Paths/ProcessorWatchingImage.cs b/tests/ImageSharp.Tests/Drawing/Paths/ProcessorWatchingImage.cs index c1d34a112a..0392160788 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/ProcessorWatchingImage.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/ProcessorWatchingImage.cs @@ -4,7 +4,7 @@ namespace ImageSharp.Tests.Drawing.Paths using System; using System.IO; using ImageSharp; - using Processing; + using ImageSharp.Processing; using System.Collections.Generic; using ImageSharp.PixelFormats; diff --git a/tests/ImageSharp.Tests/Drawing/Paths/ShapePathTests.cs b/tests/ImageSharp.Tests/Drawing/Paths/ShapePathTests.cs index 494e2a6726..d26745a278 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/ShapePathTests.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/ShapePathTests.cs @@ -5,7 +5,7 @@ namespace ImageSharp.Tests.Drawing.Paths using System.IO; using ImageSharp; using ImageSharp.Drawing.Brushes; - using Processing; + using ImageSharp.Processing; using System.Collections.Generic; using Xunit; using ImageSharp.Drawing; diff --git a/tests/ImageSharp.Tests/Drawing/Paths/ShapeRegionTests.cs b/tests/ImageSharp.Tests/Drawing/Paths/ShapeRegionTests.cs index 78c3492332..1f2e92c16d 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/ShapeRegionTests.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/ShapeRegionTests.cs @@ -5,7 +5,7 @@ namespace ImageSharp.Tests.Drawing.Paths using System.IO; using ImageSharp; using ImageSharp.Drawing.Brushes; - using Processing; + using ImageSharp.Processing; using System.Collections.Generic; using Xunit; using ImageSharp.Drawing; diff --git a/tests/ImageSharp.Tests/Drawing/Text/OutputText.cs b/tests/ImageSharp.Tests/Drawing/Text/OutputText.cs index bb9cd264e7..21646b03f3 100644 --- a/tests/ImageSharp.Tests/Drawing/Text/OutputText.cs +++ b/tests/ImageSharp.Tests/Drawing/Text/OutputText.cs @@ -5,7 +5,7 @@ namespace ImageSharp.Tests.Drawing.Text using System.IO; using ImageSharp; using ImageSharp.Drawing.Brushes; - using Processing; + using ImageSharp.Processing; using System.Collections.Generic; using Xunit; using ImageSharp.Drawing; diff --git a/tests/ImageSharp.Tests/FileTestBase.cs b/tests/ImageSharp.Tests/FileTestBase.cs index 12c7d51541..4316efdd83 100644 --- a/tests/ImageSharp.Tests/FileTestBase.cs +++ b/tests/ImageSharp.Tests/FileTestBase.cs @@ -12,6 +12,42 @@ namespace ImageSharp.Tests /// public abstract class FileTestBase : TestBase { + /// + /// A collection of all the bmp test images + /// + public static IEnumerable AllBmpFiles => TestImages.Bmp.All; + + /// + /// A collection of all the jpeg test images + /// + public static IEnumerable AllJpegFiles => TestImages.Jpeg.All; + + /// + /// A collection of all the png test images + /// + public static IEnumerable AllPngFiles => TestImages.Png.All; + + /// + /// A collection of all the gif test images + /// + public static IEnumerable AllGifFiles => TestImages.Gif.All; + + /// + /// The standard pixel formats enumerations + /// + public const PixelTypes StandardPixelTypes = PixelTypes.StandardImageClass | PixelTypes.Rgba32 | PixelTypes.Argb32; + + public static class Extensions + { + public const string Bmp = "bmp"; + + public const string Jpeg = "jpg"; + + public const string Png = "png"; + + public const string Gif = "gif"; + } + /// /// The collection of image files to test against. /// diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index cf5c1cfa9b..3d3f5b0069 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -18,7 +18,7 @@ namespace ImageSharp.Tests public static readonly string[] TestFiles = { TestImages.Png.Splash, TestImages.Png.Indexed, TestImages.Png.Interlaced, TestImages.Png.FilterVar, - TestImages.Png.ChunkLength1, TestImages.Png.ChunkLength2 + TestImages.Png.Bad.ChunkLength1, TestImages.Png.Bad.ChunkLength2 }; [Theory] diff --git a/tests/ImageSharp.Tests/Processing/Binarization/BinaryThresholdTest.cs b/tests/ImageSharp.Tests/Processing/Binarization/BinaryThresholdTest.cs new file mode 100644 index 0000000000..f3dcbe1cd6 --- /dev/null +++ b/tests/ImageSharp.Tests/Processing/Binarization/BinaryThresholdTest.cs @@ -0,0 +1,47 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests.Processing.Binarization +{ + using ImageSharp.PixelFormats; + + using Xunit; + + public class BinaryThresholdTest : FileTestBase + { + [Theory] + [WithFileCollection(nameof(AllBmpFiles), StandardPixelTypes, .75F)] + public void ImageShouldApplyBinaryThresholdFilter(TestImageProvider provider, float value) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage()) + { + image.BinaryThreshold(value) + .DebugSave(provider, null, Extensions.Bmp); + } + } + + [Theory] + [WithFileCollection(nameof(AllBmpFiles), StandardPixelTypes, .75F)] + public void ImageShouldApplyBinaryThresholdInBox(TestImageProvider provider, float value) + where TPixel : struct, IPixel + { + + using (Image source = provider.GetImage()) + using (var image = new Image(source)) + { + var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2); + + image.BinaryThreshold(value, bounds) + .DebugSave(provider, null, 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); + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Processors/Filters/DitherTest.cs b/tests/ImageSharp.Tests/Processing/Binarization/DitherTest.cs similarity index 100% rename from tests/ImageSharp.Tests/Processors/Filters/DitherTest.cs rename to tests/ImageSharp.Tests/Processing/Binarization/DitherTest.cs diff --git a/tests/ImageSharp.Tests/Processors/Filters/AutoOrientTests.cs b/tests/ImageSharp.Tests/Processors/Filters/AutoOrientTests.cs index 470e7150b3..d31a7468ff 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/AutoOrientTests.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/AutoOrientTests.cs @@ -9,7 +9,7 @@ namespace ImageSharp.Tests using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Xunit; public class AutoOrientTests : FileTestBase diff --git a/tests/ImageSharp.Tests/Processors/Filters/BinaryThresholdTest.cs b/tests/ImageSharp.Tests/Processors/Filters/BinaryThresholdTest.cs deleted file mode 100644 index 4e5e8a82ee..0000000000 --- a/tests/ImageSharp.Tests/Processors/Filters/BinaryThresholdTest.cs +++ /dev/null @@ -1,57 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp.Tests -{ - using System.IO; - - using ImageSharp.PixelFormats; - - using Xunit; - - public class BinaryThresholdTest : FileTestBase - { - public static readonly TheoryData BinaryThresholdValues - = new TheoryData - { - .25f , - .75f , - }; - - [Theory] - [MemberData(nameof(BinaryThresholdValues))] - public void ImageShouldApplyBinaryThresholdFilter(float value) - { - string path = this.CreateOutputDirectory("BinaryThreshold"); - - foreach (TestFile file in Files) - { - string filename = file.GetFileName(value); - using (Image image = file.CreateImage()) - using (FileStream output = File.OpenWrite($"{path}/{filename}")) - { - image.BinaryThreshold(value).Save(output); - } - } - } - - [Theory] - [MemberData(nameof(BinaryThresholdValues))] - public void ImageShouldApplyBinaryThresholdInBox(float value) - { - string path = this.CreateOutputDirectory("BinaryThreshold"); - - foreach (TestFile file in Files) - { - string filename = file.GetFileName(value + "-InBox"); - using (Image image = file.CreateImage()) - using (FileStream output = File.OpenWrite($"{path}/{filename}")) - { - image.BinaryThreshold(value, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); - } - } - } - } -} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Processors/Filters/ColorBlindnessTest.cs b/tests/ImageSharp.Tests/Processors/Filters/ColorBlindnessTest.cs index c287322530..a047268710 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/ColorBlindnessTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/ColorBlindnessTest.cs @@ -5,7 +5,7 @@ namespace ImageSharp.Tests { - using Processing; + using ImageSharp.Processing; using System.IO; using ImageSharp.PixelFormats; diff --git a/tests/ImageSharp.Tests/Processors/Filters/DetectEdgesTest.cs b/tests/ImageSharp.Tests/Processors/Filters/DetectEdgesTest.cs index 00440e8a5a..f3976ae0b7 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/DetectEdgesTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/DetectEdgesTest.cs @@ -5,7 +5,7 @@ namespace ImageSharp.Tests { - using Processing; + using ImageSharp.Processing; using System.IO; using ImageSharp.PixelFormats; diff --git a/tests/ImageSharp.Tests/Processors/Filters/FlipTests.cs b/tests/ImageSharp.Tests/Processors/Filters/FlipTests.cs index 87f5e1025a..c10eac4300 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/FlipTests.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/FlipTests.cs @@ -9,7 +9,7 @@ namespace ImageSharp.Tests using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Xunit; public class FlipTests : FileTestBase diff --git a/tests/ImageSharp.Tests/Processors/Filters/ResizeTests.cs b/tests/ImageSharp.Tests/Processors/Filters/ResizeTests.cs index 31f4020b6f..8268c6b64e 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/ResizeTests.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/ResizeTests.cs @@ -9,7 +9,7 @@ namespace ImageSharp.Tests using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Xunit; public class ResizeTests : FileTestBase diff --git a/tests/ImageSharp.Tests/Processors/Filters/RotateFlipTest.cs b/tests/ImageSharp.Tests/Processors/Filters/RotateFlipTest.cs index c4c4fa8d7a..5f75e98e3f 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/RotateFlipTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/RotateFlipTest.cs @@ -9,7 +9,7 @@ namespace ImageSharp.Tests using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Xunit; public class RotateFlipTest : FileTestBase diff --git a/tests/ImageSharp.Tests/Processors/Filters/RotateTest.cs b/tests/ImageSharp.Tests/Processors/Filters/RotateTest.cs index b30c795ad6..8ced88ce6b 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/RotateTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/RotateTest.cs @@ -9,7 +9,7 @@ namespace ImageSharp.Tests using ImageSharp.PixelFormats; - using Processing; + using ImageSharp.Processing; using Xunit; public class RotateTest : FileTestBase diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 44c8c34ee3..471d61abb0 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -36,9 +36,20 @@ namespace ImageSharp.Tests // Filter changing per scanline public const string FilterVar = "Png/filterVar.png"; - // Odd chunk lengths - public const string ChunkLength1 = "Png/chunklength1.png"; - public const string ChunkLength2 = "Png/chunklength2.png"; + public static class Bad + { + // Odd chunk lengths + public const string ChunkLength1 = "Png/chunklength1.png"; + public const string ChunkLength2 = "Png/chunklength2.png"; + } + + public static readonly string[] All = + { + P1, Pd, Blur, Splash, Cross, + Powerpoint, SplashInterlaced, Interlaced, + Filter0, Filter1, Filter2, Filter3, Filter4, + FilterVar + }; } public static class Jpeg @@ -105,6 +116,8 @@ namespace ImageSharp.Tests public const string Giphy = "Gif/giphy.gif"; public const string Cheers = "Gif/cheers.gif"; public const string Trans = "Gif/trans.gif"; + + public static readonly string[] All = { Rings, Giphy, Cheers, Trans }; } } } diff --git a/tests/ImageSharp.Tests/TestUtilities/Attributes/ImageDataAttributeBase.cs b/tests/ImageSharp.Tests/TestUtilities/Attributes/ImageDataAttributeBase.cs index 379ce3d054..05e8c61360 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Attributes/ImageDataAttributeBase.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Attributes/ImageDataAttributeBase.cs @@ -21,25 +21,40 @@ namespace ImageSharp.Tests protected readonly PixelTypes PixelTypes; + /// + /// Initializes a new instance of the class. + /// + /// + /// + /// protected ImageDataAttributeBase(string memberName, PixelTypes pixelTypes, object[] additionalParameters) { this.PixelTypes = pixelTypes; this.AdditionalParameters = additionalParameters; this.MemberName = memberName; - } - public string MemberName { get; private set; } + /// + /// Gets the member name + /// + public string MemberName { get; } + /// + /// Gets the member type + /// public Type MemberType { get; set; } + /// Returns the data to be used to test the theory. + /// The method that is being tested + /// One or more sets of theory data. Each invocation of the test method + /// is represented by a single object array. public override IEnumerable GetData(MethodInfo testMethod) { - IEnumerable addedRows = Enumerable.Empty(); + IEnumerable addedRows = Enumerable.Empty().ToArray(); if (!string.IsNullOrWhiteSpace(this.MemberName)) { Type type = this.MemberType ?? testMethod.DeclaringType; - Func accessor = GetPropertyAccessor(type) ?? GetFieldAccessor(type);// ?? GetMethodAccessor(type); + Func accessor = this.GetPropertyAccessor(type) ?? this.GetFieldAccessor(type); if (accessor != null) { @@ -49,7 +64,7 @@ namespace ImageSharp.Tests addedRows = memberItems.Select(x => x as object[]); if (addedRows.Any(x => x == null)) { - throw new ArgumentException($"Property {MemberName} on {MemberType ?? testMethod.DeclaringType} yielded an item that is not an object[]"); + throw new ArgumentException($"Property {this.MemberName} on {this.MemberType ?? testMethod.DeclaringType} yielded an item that is not an object[]"); } } } @@ -60,18 +75,20 @@ namespace ImageSharp.Tests addedRows = new[] { new object[0] }; } - bool firstIsprovider = FirstIsProvider(testMethod); - IEnumerable dataItems = Enumerable.Empty(); + bool firstIsprovider = this.FirstIsProvider(testMethod); if (firstIsprovider) { - return InnerGetData(testMethod, addedRows); - } - else - { - return addedRows.Select(x => x.Concat(this.AdditionalParameters).ToArray()); + return this.InnerGetData(testMethod, addedRows); } + + return addedRows.Select(x => x.Concat(this.AdditionalParameters).ToArray()); } + /// + /// Returns a value indicating whether the first parameter of the method is a test provider. + /// + /// + /// private bool FirstIsProvider(MethodInfo testMethod) { TypeInfo dataType = testMethod.GetParameters().First().ParameterType.GetTypeInfo(); @@ -106,25 +123,45 @@ namespace ImageSharp.Tests } } + /// + /// Generates the collection of method arguments from the given test as a generic enumerable. + /// + /// The test method + /// The test image provider factory type + /// The protected virtual IEnumerable GetAllFactoryMethodArgs(MethodInfo testMethod, Type factoryType) { object[] args = this.GetFactoryMethodArgs(testMethod, factoryType); return Enumerable.Repeat(args, 1); } + /// + /// Generates the collection of method arguments from the given test. + /// + /// The test method + /// The test image provider factory type + /// The protected virtual object[] GetFactoryMethodArgs(MethodInfo testMethod, Type factoryType) { throw new InvalidOperationException("Semi-abstract method"); } + /// + /// Generates the method name from the given test method. + /// + /// The test method + /// The protected abstract string GetFactoryMethodName(MethodInfo testMethod); + /// + /// Gets the field accessor for the given type. + /// Func GetFieldAccessor(Type type) { FieldInfo fieldInfo = null; for (Type reflectionType = type; reflectionType != null; reflectionType = reflectionType.GetTypeInfo().BaseType) { - fieldInfo = reflectionType.GetRuntimeField(MemberName); + fieldInfo = reflectionType.GetRuntimeField(this.MemberName); if (fieldInfo != null) break; } @@ -135,39 +172,27 @@ namespace ImageSharp.Tests return () => fieldInfo.GetValue(null); } - //Func GetMethodAccessor(Type type) - //{ - // MethodInfo methodInfo = null; - // var parameterTypes = Parameters == null ? new Type[0] : Parameters.Select(p => p?.GetType()).ToArray(); - // for (var reflectionType = type; reflectionType != null; reflectionType = reflectionType.GetTypeInfo().BaseType) - // { - // methodInfo = reflectionType.GetRuntimeMethods() - // .FirstOrDefault(m => m.Name == MemberName && ParameterTypesCompatible(m.GetParameters(), parameterTypes)); - // if (methodInfo != null) - // break; - // } - - // if (methodInfo == null || !methodInfo.IsStatic) - // return null; - - // return () => methodInfo.Invoke(null, Parameters); - //} - + /// + /// Gets the property accessor for the given type. + /// Func GetPropertyAccessor(Type type) { PropertyInfo propInfo = null; for (Type reflectionType = type; reflectionType != null; reflectionType = reflectionType.GetTypeInfo().BaseType) { - propInfo = reflectionType.GetRuntimeProperty(MemberName); + propInfo = reflectionType.GetRuntimeProperty(this.MemberName); if (propInfo != null) + { break; + } } - if (propInfo == null || propInfo.GetMethod == null || !propInfo.GetMethod.IsStatic) + if (propInfo?.GetMethod == null || !propInfo.GetMethod.IsStatic) + { return null; + } return () => propInfo.GetValue(null, null); } - } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/Attributes/WithBlankImageAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithBlankImageAttribute.cs index 32278d7558..72be5abc29 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Attributes/WithBlankImageAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithBlankImageAttribute.cs @@ -31,6 +31,7 @@ namespace ImageSharp.Tests /// /// Triggers passing an that produces a blank image of size width * height /// + /// The member data /// The required width /// The required height /// The requested parameter diff --git a/tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileCollectionAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileCollectionAttribute.cs index df8f8d0909..6f445dfcc9 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileCollectionAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileCollectionAttribute.cs @@ -34,37 +34,43 @@ namespace ImageSharp.Tests this.enumeratorMemberName = enumeratorMemberName; } - /// + /// /// 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 member name /// The requested pixel types /// Additional theory parameter values public WithFileCollectionAttribute( string enumeratorMemberName, - string DataMemberName, + string memberName, PixelTypes pixelTypes, params object[] additionalParameters) - : base(DataMemberName, pixelTypes, additionalParameters) + : base(memberName, pixelTypes, additionalParameters) { this.enumeratorMemberName = enumeratorMemberName; } + /// + /// Generates the collection of method arguments from the given test. + /// + /// The test method + /// The test image provider factory type + /// The protected override IEnumerable GetAllFactoryMethodArgs(MethodInfo testMethod, Type factoryType) { Func accessor = this.GetPropertyAccessor(testMethod.DeclaringType); - accessor = accessor ?? this.GetFieldAccessor(testMethod.DeclaringType); - IEnumerable files = (IEnumerable)accessor(); + var files = (IEnumerable)accessor(); return files.Select(f => new object[] { f }); } protected override string GetFactoryMethodName(MethodInfo testMethod) => "File"; /// - /// Based on MemberData implementation + /// Gets the field accessor for the given type. /// private Func GetFieldAccessor(Type type) { @@ -74,16 +80,22 @@ namespace ImageSharp.Tests reflectionType = reflectionType.GetTypeInfo().BaseType) { fieldInfo = reflectionType.GetRuntimeField(this.enumeratorMemberName); - if (fieldInfo != null) break; + if (fieldInfo != null) + { + break; + } } - if (fieldInfo == null || !fieldInfo.IsStatic) return null; + if (fieldInfo == null || !fieldInfo.IsStatic) + { + return null; + } return () => fieldInfo.GetValue(null); } /// - /// Based on MemberData implementation + /// Gets the property accessor for the given type. /// private Func GetPropertyAccessor(Type type) { @@ -96,7 +108,10 @@ namespace ImageSharp.Tests if (propInfo != null) break; } - if (propInfo == null || propInfo.GetMethod == null || !propInfo.GetMethod.IsStatic) return null; + if (propInfo?.GetMethod == null || !propInfo.GetMethod.IsStatic) + { + return null; + } return () => propInfo.GetValue(null, null); } diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs index 4217d52b06..9e8e0dce3f 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs @@ -17,7 +17,7 @@ namespace ImageSharp.Tests { private class FileProvider : TestImageProvider, IXunitSerializable { - // Need PixelTypes in the dictionary key, because result images of TestImageProvider.FileProvider + // Need PixelTypes in the dictionary key, because result images of TestImageProvider.FileProvider // are shared between PixelTypes.Color & PixelTypes.StandardImageClass private class Key : Tuple { @@ -27,8 +27,7 @@ namespace ImageSharp.Tests } } - private static ConcurrentDictionary> cache = - new ConcurrentDictionary>(); + private static readonly ConcurrentDictionary> cache = new ConcurrentDictionary>(); private string filePath; diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index dbd316423a..064c52064c 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -2,33 +2,40 @@ namespace ImageSharp.Tests { using System; - using System.Collections.Generic; using System.Linq; using System.Reflection; - using System.Text; using ImageSharp.PixelFormats; public static class TestImageExtensions { - public static void DebugSave(this Image img, ITestImageProvider provider, object settings = null, string extension = "png") - where TPixel : struct, IPixel + /// + /// Saves the image only when not running in the CI server. + /// + /// The pixel format + /// The image + /// The image provider + /// The settings + /// The extension + public static void DebugSave(this Image image, ITestImageProvider provider, object settings = null, string extension = "png") + where TPixel : struct, IPixel { string tag = null; - if (settings is string) + string s = settings as string; + if (s != null) { - tag = (string)settings; + tag = s; } else if (settings != null) { - var properties = settings.GetType().GetRuntimeProperties(); + System.Collections.Generic.IEnumerable properties = settings.GetType().GetRuntimeProperties(); 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) + if (!bool.TryParse(Environment.GetEnvironmentVariable("CI"), out bool isCi) || !isCi) { - // we are running locally then we want to save it out - provider.Utility.SaveTestOutputFile(img, extension, tag: tag); + // We are running locally then we want to save it out + provider.Utility.SaveTestOutputFile(image, extension, tag: tag); } } } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs index dfaf1c0528..115fb895d2 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs @@ -106,6 +106,11 @@ namespace ImageSharp.Tests public static Type ToType(this PixelTypes pixelType) => PixelTypes2ClrTypes[pixelType]; + /// + /// Returns the enumerations for the given type. + /// + /// + /// public static PixelTypes GetPixelType(this Type colorStructClrType) => ClrTypes2PixelTypes[colorStructClrType]; public static IEnumerable> ExpandAllTypes(this PixelTypes pixelTypes)