From a066c48e29f46ef151ff28e7aec1f0b08f85b4b8 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 28 Apr 2017 13:33:32 +1000 Subject: [PATCH] Add missing tests We need to refactor our tests to mostly use the generated patterns. --- .../Processing/Effects/BackgroundColor.cs | 18 +++++++++- .../Processing/Effects/Brightness.cs | 6 ++-- .../Processors/Filters/AlphaTest.cs | 2 +- .../Processors/Filters/BackgroundColorTest.cs | 16 +++++++++ ...aryThreshold.cs => BinaryThresholdTest.cs} | 17 +++++++++ .../Processors/Filters/BlackWhiteTest.cs | 16 +++++++++ .../Processors/Filters/BoxBlurTest.cs | 17 +++++++++ .../Processors/Filters/BrightnessTest.cs | 17 +++++++++ .../Processors/Filters/ColorBlindnessTest.cs | 17 +++++++++ .../Processors/Filters/ContrastTest.cs | 17 +++++++++ .../Processors/Filters/GaussianBlurTest.cs | 4 +-- .../Processors/Filters/GaussianSharpenTest.cs | 36 +++++++++++++------ .../Processors/Filters/GrayscaleTest.cs | 28 +++++++++++---- .../Processors/Filters/HueTest.cs | 17 +++++++++ .../Processors/Filters/KodachromeTest.cs | 16 +++++++++ .../Processors/Filters/PolaroidTest.cs | 16 +++++++++ .../Processors/Filters/SaturationTest.cs | 17 +++++++++ .../Processors/Filters/SepiaTest.cs | 16 +++++++++ 18 files changed, 269 insertions(+), 24 deletions(-) rename tests/ImageSharp.Tests/Processors/Filters/{BinaryThreshold.cs => BinaryThresholdTest.cs} (62%) diff --git a/src/ImageSharp/Processing/Effects/BackgroundColor.cs b/src/ImageSharp/Processing/Effects/BackgroundColor.cs index cb189338e..a1914fee3 100644 --- a/src/ImageSharp/Processing/Effects/BackgroundColor.cs +++ b/src/ImageSharp/Processing/Effects/BackgroundColor.cs @@ -26,7 +26,23 @@ namespace ImageSharp public static Image BackgroundColor(this Image source, TPixel color) where TPixel : struct, IPixel { - source.ApplyProcessor(new BackgroundColorProcessor(color), source.Bounds); + return BackgroundColor(source, color, source.Bounds); + } + + /// + /// Replaces the background color of image with the given one. + /// + /// The pixel format. + /// The image this method extends. + /// The color to set as the background. + /// + /// The structure that specifies the portion of the image object to alter. + /// + /// The . + public static Image BackgroundColor(this Image source, TPixel color, Rectangle rectangle) + where TPixel : struct, IPixel + { + source.ApplyProcessor(new BackgroundColorProcessor(color), rectangle); return source; } } diff --git a/src/ImageSharp/Processing/Effects/Brightness.cs b/src/ImageSharp/Processing/Effects/Brightness.cs index 6b7477488..a28df82c0 100644 --- a/src/ImageSharp/Processing/Effects/Brightness.cs +++ b/src/ImageSharp/Processing/Effects/Brightness.cs @@ -20,12 +20,12 @@ namespace ImageSharp /// Alters the brightness component of the image. /// /// The pixel format. - /// The image this method extends. + /// The image this method extends. /// The new brightness of the image. Must be between -100 and 100. /// The . public static Image Brightness(this Image source, int amount) where TPixel : struct, IPixel - { + { return Brightness(source, amount, source.Bounds); } @@ -33,7 +33,7 @@ namespace ImageSharp /// Alters the brightness component of the image. /// /// The pixel format. - /// The image this method extends. + /// The image this method extends. /// The new brightness of the image. Must be between -100 and 100. /// /// The structure that specifies the portion of the image object to alter. diff --git a/tests/ImageSharp.Tests/Processors/Filters/AlphaTest.cs b/tests/ImageSharp.Tests/Processors/Filters/AlphaTest.cs index a8aeb3341..2b39086e3 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/AlphaTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/AlphaTest.cs @@ -43,7 +43,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - string filename = file.GetFileName(value); + string filename = file.GetFileName(value + "-InBox"); using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { diff --git a/tests/ImageSharp.Tests/Processors/Filters/BackgroundColorTest.cs b/tests/ImageSharp.Tests/Processors/Filters/BackgroundColorTest.cs index eccfc13af..4bc39f8ab 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/BackgroundColorTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/BackgroundColorTest.cs @@ -27,5 +27,21 @@ namespace ImageSharp.Tests } } } + + [Fact] + public void ImageShouldApplyBackgroundColorFilterInBox() + { + string path = this.CreateOutputDirectory("BackgroundColor"); + + foreach (TestFile file in Files) + { + string filename = file.GetFileName("-InBox"); + using (Image image = file.CreateImage()) + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.BackgroundColor(Rgba32.HotPink, 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/BinaryThreshold.cs b/tests/ImageSharp.Tests/Processors/Filters/BinaryThresholdTest.cs similarity index 62% rename from tests/ImageSharp.Tests/Processors/Filters/BinaryThreshold.cs rename to tests/ImageSharp.Tests/Processors/Filters/BinaryThresholdTest.cs index d7d4eac05..f36014542 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/BinaryThreshold.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/BinaryThresholdTest.cs @@ -34,5 +34,22 @@ namespace ImageSharp.Tests } } } + + [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/BlackWhiteTest.cs b/tests/ImageSharp.Tests/Processors/Filters/BlackWhiteTest.cs index 6b9a48f72..377ae4719 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/BlackWhiteTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/BlackWhiteTest.cs @@ -25,5 +25,21 @@ namespace ImageSharp.Tests } } } + + [Fact] + public void ImageShouldApplyBlackWhiteFilterInBox() + { + string path = this.CreateOutputDirectory("BlackWhite"); + + foreach (TestFile file in Files) + { + string filename = file.GetFileName("-InBox"); + using (Image image = file.CreateImage()) + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.BlackWhite(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/BoxBlurTest.cs b/tests/ImageSharp.Tests/Processors/Filters/BoxBlurTest.cs index 5d4f628ee..f4f5fb4bb 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/BoxBlurTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/BoxBlurTest.cs @@ -34,5 +34,22 @@ namespace ImageSharp.Tests } } } + + [Theory] + [MemberData(nameof(BoxBlurValues))] + public void ImageShouldApplyBoxBlurFilterInBox(int value) + { + string path = this.CreateOutputDirectory("BoxBlur"); + + foreach (TestFile file in Files) + { + string filename = file.GetFileName(value + "-InBox"); + using (Image image = file.CreateImage()) + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.BoxBlur(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/BrightnessTest.cs b/tests/ImageSharp.Tests/Processors/Filters/BrightnessTest.cs index e274ef041..f59d5be4c 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/BrightnessTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/BrightnessTest.cs @@ -34,5 +34,22 @@ namespace ImageSharp.Tests } } } + + [Theory] + [MemberData(nameof(BrightnessValues))] + public void ImageShouldApplyBrightnessFilterInBox(int value) + { + string path = this.CreateOutputDirectory("Brightness"); + + foreach (TestFile file in Files) + { + string filename = file.GetFileName(value + "-InBox"); + using (Image image = file.CreateImage()) + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.Brightness(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 d18f32caf..5564a77ef 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/ColorBlindnessTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/ColorBlindnessTest.cs @@ -41,5 +41,22 @@ namespace ImageSharp.Tests } } } + + [Theory] + [MemberData(nameof(ColorBlindnessFilters))] + public void ImageShouldApplyBrightnessFilterInBox(ColorBlindness colorBlindness) + { + string path = this.CreateOutputDirectory("ColorBlindness"); + + foreach (TestFile file in Files) + { + string filename = file.GetFileName(colorBlindness + "-InBox"); + using (Image image = file.CreateImage()) + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.ColorBlindness(colorBlindness, 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/ContrastTest.cs b/tests/ImageSharp.Tests/Processors/Filters/ContrastTest.cs index 09376f2c0..5bbe2338c 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/ContrastTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/ContrastTest.cs @@ -33,5 +33,22 @@ namespace ImageSharp.Tests } } } + + [Theory] + [MemberData(nameof(ContrastValues))] + public void ImageShouldApplyContrastFilterInBox(int value) + { + string path = this.CreateOutputDirectory("Contrast"); + + foreach (TestFile file in Files) + { + string filename = file.GetFileName(value + "-InBox"); + using (Image image = file.CreateImage()) + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.Contrast(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/GaussianBlurTest.cs b/tests/ImageSharp.Tests/Processors/Filters/GaussianBlurTest.cs index dd17aaeb0..4b2ac8b7c 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/GaussianBlurTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/GaussianBlurTest.cs @@ -26,7 +26,7 @@ namespace ImageSharp.Tests using (Image image = provider.GetImage()) { image.GaussianBlur(value) - .DebugSave(provider); + .DebugSave(provider, value.ToString()); } } @@ -40,7 +40,7 @@ namespace ImageSharp.Tests { Rectangle rect = new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2); image.GaussianBlur(value, rect) - .DebugSave(provider); + .DebugSave(provider, value.ToString()); // lets draw identical shapes over the blured areas and ensure that it didn't change the outer area image.Fill(NamedColors.HotPink, rect); diff --git a/tests/ImageSharp.Tests/Processors/Filters/GaussianSharpenTest.cs b/tests/ImageSharp.Tests/Processors/Filters/GaussianSharpenTest.cs index c1aa06941..1fa1ae15c 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/GaussianSharpenTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/GaussianSharpenTest.cs @@ -6,7 +6,7 @@ namespace ImageSharp.Tests { using System.IO; - + using ImageSharp.PixelFormats; using Xunit; public class GaussianSharpenTest : FileTestBase @@ -19,19 +19,33 @@ namespace ImageSharp.Tests }; [Theory] - [MemberData(nameof(GaussianSharpenValues))] - public void ImageShouldApplyGaussianSharpenFilter(int value) + [WithTestPatternImages(nameof(GaussianSharpenValues), 320, 240, PixelTypes.StandardImageClass)] + public void ImageShouldApplyGaussianSharpenFilter(TestImageProvider provider, int value) + where TPixel : struct, IPixel { - string path = this.CreateOutputDirectory("GaussianSharpen"); + using (Image image = provider.GetImage()) + { + image.GaussianSharpen(value) + .DebugSave(provider, value.ToString()); + } + } - foreach (TestFile file in Files) + [Theory] + [WithTestPatternImages(nameof(GaussianSharpenValues), 320, 240, PixelTypes.StandardImageClass)] + public void ImageShouldApplyGaussianSharpenFilterInBox(TestImageProvider provider, int value) + where TPixel : struct, IPixel + { + using (Image source = provider.GetImage()) + using (Image image = new Image(source)) { - string filename = file.GetFileName(value); - using (Image image = file.CreateImage()) - using (FileStream output = File.OpenWrite($"{path}/{filename}")) - { - image.GaussianSharpen(value).Save(output); - } + Rectangle rect = new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2); + image.GaussianSharpen(value, rect) + .DebugSave(provider, value.ToString()); + + // lets draw identical shapes over the Sharpened areas and ensure that it didn't change the outer area + image.Fill(NamedColors.HotPink, rect); + source.Fill(NamedColors.HotPink, rect); + ImageComparer.CheckSimilarity(image, source); } } } diff --git a/tests/ImageSharp.Tests/Processors/Filters/GrayscaleTest.cs b/tests/ImageSharp.Tests/Processors/Filters/GrayscaleTest.cs index 2b717a0b7..9a7d87854 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/GrayscaleTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/GrayscaleTest.cs @@ -5,12 +5,8 @@ namespace ImageSharp.Tests { - using System.IO; - using Xunit; using ImageSharp.Processing; - using ImageSharp.Tests; - using System.Numerics; using ImageSharp.PixelFormats; @@ -20,7 +16,7 @@ namespace ImageSharp.Tests /// Use test patterns over loaded images to save decode time. /// [Theory] - [WithTestPatternImages(50, 50, PixelTypes.StandardImageClass, GrayscaleMode.Bt709)] + [WithTestPatternImages(50, 50, PixelTypes.StandardImageClass, GrayscaleMode.Bt709)] [WithTestPatternImages(50, 50, PixelTypes.StandardImageClass, GrayscaleMode.Bt601)] public void ImageShouldApplyGrayscaleFilterAll(TestImageProvider provider, GrayscaleMode value) where TPixel : struct, IPixel @@ -36,7 +32,27 @@ namespace ImageSharp.Tests Assert.Equal(data[1], data[2]); } - image.DebugSave(provider); + image.DebugSave(provider, value.ToString()); + } + } + + [Theory] + [WithTestPatternImages(50, 50, PixelTypes.StandardImageClass, GrayscaleMode.Bt709)] + [WithTestPatternImages(50, 50, PixelTypes.StandardImageClass, GrayscaleMode.Bt601)] + public void ImageShouldApplyGrayscaleFilterInBox(TestImageProvider provider, GrayscaleMode value) + where TPixel : struct, IPixel + { + using (Image source = provider.GetImage()) + using (Image image = new Image(source)) + { + Rectangle rect = new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2); + image.Grayscale(rect, value) + .DebugSave(provider, value.ToString()); + + // Let's draw identical shapes over the greyed areas and ensure that it didn't change the outer area + image.Fill(NamedColors.HotPink, rect); + source.Fill(NamedColors.HotPink, rect); + ImageComparer.CheckSimilarity(image, source); } } } diff --git a/tests/ImageSharp.Tests/Processors/Filters/HueTest.cs b/tests/ImageSharp.Tests/Processors/Filters/HueTest.cs index 4241dc833..3081c638c 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/HueTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/HueTest.cs @@ -34,5 +34,22 @@ namespace ImageSharp.Tests } } } + + [Theory] + [MemberData(nameof(HueValues))] + public void ImageShouldApplyHueFilterInBox(int value) + { + string path = this.CreateOutputDirectory("Hue"); + + foreach (TestFile file in Files) + { + string filename = file.GetFileName(value + "-InBox"); + using (Image image = file.CreateImage()) + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.Hue(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/KodachromeTest.cs b/tests/ImageSharp.Tests/Processors/Filters/KodachromeTest.cs index 40734e02a..870f813a1 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/KodachromeTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/KodachromeTest.cs @@ -25,5 +25,21 @@ namespace ImageSharp.Tests } } } + + [Fact] + public void ImageShouldApplyKodachromeFilterInBox() + { + string path = this.CreateOutputDirectory("Kodachrome"); + + foreach (TestFile file in Files) + { + string filename = file.GetFileName("InBox"); + using (Image image = file.CreateImage()) + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.Kodachrome(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/PolaroidTest.cs b/tests/ImageSharp.Tests/Processors/Filters/PolaroidTest.cs index 040f8b4a2..e9938fb83 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/PolaroidTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/PolaroidTest.cs @@ -25,5 +25,21 @@ namespace ImageSharp.Tests } } } + + [Fact] + public void ImageShouldApplyPolaroidFilterInBox() + { + string path = this.CreateOutputDirectory("Polaroid"); + + foreach (TestFile file in Files) + { + string filename = file.GetFileName("InBox"); + using (Image image = file.CreateImage()) + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.Polaroid(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/SaturationTest.cs b/tests/ImageSharp.Tests/Processors/Filters/SaturationTest.cs index abd596d70..ee24f120c 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/SaturationTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/SaturationTest.cs @@ -34,5 +34,22 @@ namespace ImageSharp.Tests } } } + + [Theory] + [MemberData(nameof(SaturationValues))] + public void ImageShouldApplySaturationFilterInBox(int value) + { + string path = this.CreateOutputDirectory("Saturation"); + + foreach (TestFile file in Files) + { + string filename = file.GetFileName(value + "-InBox"); + using (Image image = file.CreateImage()) + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.Saturation(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/SepiaTest.cs b/tests/ImageSharp.Tests/Processors/Filters/SepiaTest.cs index fbae10fa5..0e1583cc6 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/SepiaTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/SepiaTest.cs @@ -25,5 +25,21 @@ namespace ImageSharp.Tests } } } + + [Fact] + public void ImageShouldApplySepiaFilterInBox() + { + string path = this.CreateOutputDirectory("Sepia"); + + foreach (TestFile file in Files) + { + string filename = file.GetFileName("InBox"); + using (Image image = file.CreateImage()) + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.Sepia(new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); + } + } + } } } \ No newline at end of file