diff --git a/src/ImageSharp/Configuration.cs b/src/ImageSharp/Configuration.cs index f169822858..12ceca0b28 100644 --- a/src/ImageSharp/Configuration.cs +++ b/src/ImageSharp/Configuration.cs @@ -140,7 +140,7 @@ namespace ImageSharp /// /// The extension to discover /// The if found otherwise null - public IImageFormat FindFormatByFileExtensions(string extension) + public IImageFormat FindFormatByFileExtension(string extension) { return this.imageFormats.FirstOrDefault(x => x.FileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase)); } diff --git a/src/ImageSharp/Image/ImageExtensions.cs b/src/ImageSharp/Image/ImageExtensions.cs index 6095b49e3a..0eb3e2cab8 100644 --- a/src/ImageSharp/Image/ImageExtensions.cs +++ b/src/ImageSharp/Image/ImageExtensions.cs @@ -52,7 +52,7 @@ namespace ImageSharp Guard.NotNullOrEmpty(filePath, nameof(filePath)); string ext = Path.GetExtension(filePath).Trim('.'); - IImageFormat format = source.Configuration.FindFormatByFileExtensions(ext); + IImageFormat format = source.Configuration.FindFormatByFileExtension(ext); if (format == null) { var stringBuilder = new StringBuilder(); diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index 598834672b..13a7705ddf 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -139,8 +139,8 @@ namespace ImageSharp.Tests image.Save(ms, encoder); } } - - Image mirror = provider.Factory.CreateImage(data); + + Image mirror = Image.Load(data); mirror.DebugSave(provider, $"_{subsample}_Q{quality}"); } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegUtilsTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegUtilsTests.cs index f681e1d8f9..1075c46921 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegUtilsTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegUtilsTests.cs @@ -16,19 +16,19 @@ namespace ImageSharp.Tests public class JpegUtilsTests : TestBase { - public static Image CreateTestImage(GenericFactory factory) + public static Image CreateTestImage() where TPixel : struct, IPixel { - Image image = factory.CreateImage(10, 10); + var image = new Image(10, 10); using (PixelAccessor pixels = image.Lock()) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { - Vector4 v = new Vector4(i / 10f, j / 10f, 0, 1); + var v = new Vector4(i / 10f, j / 10f, 0, 1); - TPixel color = default(TPixel); + var color = default(TPixel); color.PackFromVector4(v); pixels[i, j] = color; @@ -45,7 +45,7 @@ namespace ImageSharp.Tests where TPixel : struct, IPixel { using (Image src = provider.GetImage()) - using (Image dest = provider.Factory.CreateImage(8, 8)) + using (Image dest = new Image(8,8)) using (PixelArea area = new PixelArea(8, 8, ComponentOrder.Xyz)) using (PixelAccessor s = src.Lock()) using (PixelAccessor d = dest.Lock()) @@ -68,7 +68,7 @@ namespace ImageSharp.Tests { using (Image src = provider.GetImage()) using (PixelArea area = new PixelArea(8, 8, ComponentOrder.Xyz)) - using (Image dest = provider.Factory.CreateImage(8, 8)) + using (Image dest = new Image(8, 8)) using (PixelAccessor s = src.Lock()) using (PixelAccessor d = dest.Lock()) { diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index 5f4055a936..1d40163d10 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -12,6 +12,8 @@ using Xunit; namespace ImageSharp.Tests { + using ImageSharp.Tests.TestUtilities.ImageComparison; + public class PngDecoderTests { private const PixelTypes PixelTypes = Tests.PixelTypes.Rgba32 | Tests.PixelTypes.RgbaVector | Tests.PixelTypes.Argb32; @@ -19,17 +21,19 @@ namespace ImageSharp.Tests public static readonly string[] TestFiles = { TestImages.Png.Splash, TestImages.Png.Indexed, TestImages.Png.Interlaced, TestImages.Png.FilterVar, - TestImages.Png.Bad.ChunkLength1, TestImages.Png.Bad.ChunkLength2, TestImages.Png.Rgb48Bpp, TestImages.Png.Rgb48BppInterlaced + TestImages.Png.Bad.ChunkLength1, TestImages.Png.Bad.ChunkLength2, TestImages.Png.Rgb48Bpp, + TestImages.Png.Rgb48BppInterlaced, TestImages.Png.SnakeGame }; [Theory] [WithFileCollection(nameof(TestFiles), PixelTypes)] - public void Decode(TestImageProvider imageProvider) + public void Decode(TestImageProvider provider) where TPixel : struct, IPixel { - using (Image image = imageProvider.GetImage()) + using (Image image = provider.GetImage(new PngDecoder())) { - image.DebugSave(imageProvider); + image.DebugSave(provider); + image.CompareToOriginal(provider, ImageComparer.Exact); } } diff --git a/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs b/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs index 98f316f439..37333576b5 100644 --- a/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs +++ b/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs @@ -17,20 +17,20 @@ namespace ImageSharp.Tests /// public class PixelAccessorTests { - public static Image CreateTestImage(GenericFactory factory) + public static Image CreateTestImage() where TPixel : struct, IPixel { - Image image = factory.CreateImage(10, 10); + var image = new Image(10, 10); using (PixelAccessor pixels = image.Lock()) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { - Vector4 v = new Vector4(i, j, 0, 1); + var v = new Vector4(i, j, 0, 1); v /= 10; - TPixel color = default(TPixel); + var color = default(TPixel); color.PackFromVector4(v); pixels[i, j] = color; diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index 491b245bc4..03805ae32a 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -28,4 +28,7 @@ PreserveNewest + + + \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs b/tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs index c753528f38..cdb48bdd2f 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs @@ -40,7 +40,8 @@ namespace ImageSharp.Tests.Processing.Processors.Convolution using (Image image = provider.GetImage()) { image.Mutate(x => x.DetectEdges(detector)); - image.DebugSave(provider, detector.ToString(), grayscale: true); + image.DebugSave(provider, detector.ToString()); + image.CompareToReferenceOutput(provider, detector.ToString()); } } @@ -52,7 +53,8 @@ namespace ImageSharp.Tests.Processing.Processors.Convolution using (Image image = provider.GetImage()) { image.Mutate(x => x.DetectEdges()); - image.DebugSave(provider, grayscale: true); + image.DebugSave(provider); + image.CompareToReferenceOutput(provider); } } @@ -73,16 +75,16 @@ namespace ImageSharp.Tests.Processing.Processors.Convolution public void DetectEdges_InBox(TestImageProvider provider) where TPixel : struct, IPixel { - using (Image source = provider.GetImage()) - using (Image image = source.Clone()) + using (Image image = provider.GetImage()) { var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2); image.Mutate(x => x.DetectEdges(bounds)); - image.DebugSave(provider, grayscale: true); + image.DebugSave(provider); + image.CompareToReferenceOutput(provider); // TODO: We don't need this any longer after switching to ReferenceImages - ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds); + //ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds); } } } diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 527b78ae6b..66ca93bd23 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -31,6 +31,7 @@ namespace ImageSharp.Tests public const string Bike = "Png/Bike.png"; public const string BikeGrayscale = "Png/BikeGrayscale.png"; public const string Rgb48BppInterlaced = "Png/rgb-48bpp-interlaced.png"; + public const string SnakeGame = "Png/SnakeGame.png"; // Filtered test images from http://www.schaik.com/pngsuite/pngsuite_fil_png.html public const string Filter0 = "Png/filter0.png"; diff --git a/tests/ImageSharp.Tests/TestUtilities/Attributes/WithMemberFactoryAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithMemberFactoryAttribute.cs index 6c6198c38d..53faff640a 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Attributes/WithMemberFactoryAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithMemberFactoryAttribute.cs @@ -39,9 +39,8 @@ namespace ImageSharp.Tests Type colorType = args.Single(); Type imgType = typeof(Image<>).MakeGenericType(colorType); - Type genericFactoryType = (typeof(GenericFactory<>)).MakeGenericType(colorType); - Type funcType = typeof(Func<,>).MakeGenericType(genericFactoryType, imgType); + Type funcType = typeof(Func<>).MakeGenericType(imgType); MethodInfo genericMethod = m.MakeGenericMethod(args); diff --git a/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs b/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs deleted file mode 100644 index efe548ee88..0000000000 --- a/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs +++ /dev/null @@ -1,36 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp.Tests -{ - using System; - - using ImageSharp.PixelFormats; - - /// - /// TODO: Non-generic 'Image' class has been removed. We no longer need the factory pattern here! - /// - /// Utility class to create specialized subclasses of generic classes (eg. ) - /// Used as parameter for -based factory methods - /// - public class GenericFactory - where TPixel : struct, IPixel - { - public virtual Image CreateImage(int width, int height) - { - return new Image(width, height); - } - - public virtual Image CreateImage(byte[] bytes) - { - return Image.Load(bytes); - } - - public virtual Image CreateImage(Image other) - { - return other.Clone(); - } - } -} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs b/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs deleted file mode 100644 index efa08c736f..0000000000 --- a/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs +++ /dev/null @@ -1,24 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp.Tests -{ - using ImageSharp.PixelFormats; - - /// - /// TODO: Non-generic 'Image' class has been removed. We no longer need the factory pattern here! - /// - public class ImageFactory : GenericFactory - { - public override Image CreateImage(byte[] bytes) => Image.Load(bytes); - - public override Image CreateImage(int width, int height) => new Image(width, height); - - public override Image CreateImage(Image other) - { - return other.Clone(); - } - } -} diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs index b13905ef57..80102e6010 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs @@ -10,7 +10,7 @@ namespace ImageSharp.Tests.TestUtilities.ImageComparison public abstract class ImageComparer { - public static ImageComparer Exact { get; } = ExactImageComparer.Instance; + public static ImageComparer Exact { get; } = Tolerant(0, 0); public static ImageComparer Tolerant( float imageThreshold = TolerantImageComparer.DefaultImageThreshold, diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs index c4f74e21be..2ae48d7456 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs @@ -66,7 +66,7 @@ for (int x = 0; x < width; x++) { - int d = GetDifferenceInPixelByteSum(ref aBuffer[x], ref bBuffer[x]); + int d = GetHammingDistanceInRgbaSpace(ref aBuffer[x], ref bBuffer[x]); if (d > this.PixelThresholdInPixelByteSum) { @@ -92,7 +92,7 @@ } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static int GetDifferenceInPixelByteSum(ref Rgba32 a, ref Rgba32 b) + private static int GetHammingDistanceInRgbaSpace(ref Rgba32 a, ref Rgba32 b) { return Diff(a.R, b.R) + Diff(a.G, b.G) + Diff(a.B, b.B) + Diff(a.A, b.A); } diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs index 4252a60b5e..af5e838755 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs @@ -33,7 +33,7 @@ namespace ImageSharp.Tests protected int Width { get; private set; } - public override Image GetImage() => this.Factory.CreateImage(this.Width, this.Height); + public override Image GetImage() => new Image(this.Width, this.Height); public override void Deserialize(IXunitSerializationInfo info) diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs index 7ae103cd29..db30bb65d4 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs @@ -45,17 +45,8 @@ namespace ImageSharp.Tests public override Image GetImage() { - Key key = new Key(this.PixelType, this.FilePath); - - Image cachedImage = cache.GetOrAdd( - key, - fn => - { - TestFile testFile = TestFile.Create(this.FilePath); - return Image.Load(testFile.Bytes); - }); - - return cachedImage.Clone(); + IImageDecoder decoder = TestEnvironment.GetReferenceDecoder(this.FilePath); + return this.GetImage(decoder); } public override Image GetImage(IImageDecoder decoder) diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/LambdaProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/LambdaProvider.cs index 30e7a63b50..ee150adc7c 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/LambdaProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/LambdaProvider.cs @@ -18,14 +18,14 @@ namespace ImageSharp.Tests { private class LambdaProvider : TestImageProvider { - private readonly Func, Image> creator; + private readonly Func> factoryFunc; - public LambdaProvider(Func, Image> creator) + public LambdaProvider(Func> factoryFunc) { - this.creator = creator; + this.factoryFunc = factoryFunc; } - public override Image GetImage() => this.creator(this.Factory); + public override Image GetImage() => this.factoryFunc(); } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs index 99d1125d72..cc8c453c80 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs @@ -33,7 +33,6 @@ namespace ImageSharp.Tests /// public ImagingTestCaseUtility Utility { get; private set; } - public GenericFactory Factory { get; private set; } = new GenericFactory(); public string TypeName { get; private set; } public string MethodName { get; private set; } @@ -60,10 +59,10 @@ namespace ImageSharp.Tests } public static TestImageProvider Lambda( - Func, Image> func, + Func> factoryFunc, MethodInfo testMethod = null, PixelTypes pixelTypeOverride = PixelTypes.Undefined) - => new LambdaProvider(func).Init(testMethod, pixelTypeOverride); + => new LambdaProvider(factoryFunc).Init(testMethod, pixelTypeOverride); public static TestImageProvider Solid( int width, @@ -122,12 +121,7 @@ namespace ImageSharp.Tests } this.TypeName = typeName; this.MethodName = methodName; - - if (pixelTypeOverride == PixelTypes.Rgba32) - { - this.Factory = new ImageFactory() as GenericFactory; - } - + this.Utility = new ImagingTestCaseUtility { SourceFileOrDescription = this.SourceFileOrDescription, diff --git a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs index edb8bafac5..b157042b6e 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs @@ -138,13 +138,11 @@ namespace ImageSharp.Tests string extension = null, IImageEncoder encoder = null, object testOutputDetails = null, - bool grayscale = false, bool appendPixelTypeToFileName = true) where TPixel : struct, IPixel { string path = this.GetTestOutputFileName(extension, testOutputDetails, appendPixelTypeToFileName); - string extension1 = Path.GetExtension(path); - encoder = encoder ?? GetImageFormatByExtension(extension1, grayscale); + encoder = encoder ?? TestEnvironment.GetReferenceEncoder(path); using (FileStream stream = File.OpenWrite(path)) { @@ -173,26 +171,26 @@ namespace ImageSharp.Tests this.Init(method.DeclaringType.Name, method.Name); } - private static IImageEncoder GetImageFormatByExtension(string extension, bool grayscale) - { - extension = extension?.TrimStart('.'); - var format = Configuration.Default.FindFormatByFileExtensions(extension); - IImageEncoder encoder = Configuration.Default.FindEncoder(format); - PngEncoder pngEncoder = encoder as PngEncoder; - if (pngEncoder != null) - { - pngEncoder = new PngEncoder(); - encoder = pngEncoder; - pngEncoder.CompressionLevel = 9; - - if (grayscale) - { - pngEncoder.PngColorType = PngColorType.Grayscale; - } - } + //private static IImageEncoder GetEncoderByExtension(string extension, bool grayscale) + //{ + // extension = extension?.TrimStart('.'); + // var format = Configuration.Default.FindFormatByFileExtension(extension); + // IImageEncoder encoder = Configuration.Default.FindEncoder(format); + // PngEncoder pngEncoder = encoder as PngEncoder; + // if (pngEncoder != null) + // { + // pngEncoder = new PngEncoder(); + // encoder = pngEncoder; + // pngEncoder.CompressionLevel = 9; + + // if (grayscale) + // { + // pngEncoder.PngColorType = PngColorType.Grayscale; + // } + // } - return encoder; - } + // return encoder; + //} private string GetTestOutputDir() { diff --git a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/ReferenceDecoder.cs b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs similarity index 90% rename from tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/ReferenceDecoder.cs rename to tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs index 3ed3248b26..493866170a 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/ReferenceDecoder.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs @@ -8,9 +8,9 @@ namespace ImageSharp.Tests.TestUtilities.ReferenceCodecs using ImageSharp.Formats; using ImageSharp.PixelFormats; - public class ReferenceDecoder : IImageDecoder + public class SystemDrawingReferenceDecoder : IImageDecoder { - public static ReferenceDecoder Instance { get; } = new ReferenceDecoder(); + public static SystemDrawingReferenceDecoder Instance { get; } = new SystemDrawingReferenceDecoder(); public Image Decode(Configuration configuration, Stream stream) where TPixel : struct, IPixel diff --git a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/ReferenceEncoder.cs b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceEncoder.cs similarity index 72% rename from tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/ReferenceEncoder.cs rename to tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceEncoder.cs index 9018d66f7f..c1c10c469a 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/ReferenceEncoder.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceEncoder.cs @@ -9,16 +9,16 @@ namespace ImageSharp.Tests.TestUtilities.ReferenceCodecs using ImageSharp.Formats; using ImageSharp.PixelFormats; - public class ReferenceEncoder : IImageEncoder + public class SystemDrawingReferenceEncoder : IImageEncoder { private readonly System.Drawing.Imaging.ImageFormat imageFormat; - public ReferenceEncoder(ImageFormat imageFormat) + public SystemDrawingReferenceEncoder(ImageFormat imageFormat) { this.imageFormat = imageFormat; } - public static ReferenceEncoder Png { get; } = new ReferenceEncoder(System.Drawing.Imaging.ImageFormat.Png); + public static SystemDrawingReferenceEncoder Png { get; } = new SystemDrawingReferenceEncoder(System.Drawing.Imaging.ImageFormat.Png); public void Encode(Image image, Stream stream) where TPixel : struct, IPixel diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs index 728cf45696..9b2f7c479e 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs @@ -1,12 +1,13 @@ namespace ImageSharp.Tests { using System; - using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; - using System.Security; + using ImageSharp.Formats; + using ImageSharp.Tests.TestUtilities.ReferenceCodecs; + public static class TestEnvironment { private const string ImageSharpSolutionFileName = "ImageSharp.sln"; @@ -26,6 +27,8 @@ namespace ImageSharp.Tests return bool.TryParse(Environment.GetEnvironmentVariable("CI"), out isCi) && isCi; }); + private static Lazy configuration = new Lazy(CreateDefaultConfiguration); + // ReSharper disable once InconsistentNaming /// /// Gets a value indicating whether test execution runs on CI. @@ -34,6 +37,24 @@ namespace ImageSharp.Tests internal static string SolutionDirectoryFullPath => solutionDirectoryFullPath.Value; + internal static Configuration Configuration => configuration.Value; + + private static Configuration CreateDefaultConfiguration() + { + var configuration = new Configuration( + new PngConfigurationModule(), + new JpegConfigurationModule(), + new GifConfigurationModule(), + new BmpConfigurationModule() + ); + + configuration.SetDecoder(ImageFormats.Png, SystemDrawingReferenceDecoder.Instance); + configuration.SetEncoder(ImageFormats.Png, SystemDrawingReferenceEncoder.Png); + configuration.AddImageFormatDetector(new PngImageFormatDetector()); + + return configuration; + } + private static string GetSolutionDirectoryFullPathImpl() { string assemblyLocation = typeof(TestFile).GetTypeInfo().Assembly.Location; @@ -62,23 +83,48 @@ namespace ImageSharp.Tests return directory.FullName; } - + /// /// Gets the correct full path to the Input Images directory. /// - internal static string InputImagesDirectoryFullPath => Path.Combine(SolutionDirectoryFullPath, InputImagesRelativePath); + internal static string InputImagesDirectoryFullPath => + Path.Combine(SolutionDirectoryFullPath, InputImagesRelativePath); /// /// Gets the correct full path to the Actual Output directory. (To be written to by the test cases.) /// - internal static string ActualOutputDirectoryFullPath => Path.Combine(SolutionDirectoryFullPath, ActualOutputDirectoryRelativePath); + internal static string ActualOutputDirectoryFullPath => Path.Combine( + SolutionDirectoryFullPath, + ActualOutputDirectoryRelativePath); /// /// Gets the correct full path to the Expected Output directory. (To compare the test results to.) /// - internal static string ReferenceOutputDirectoryFullPath => Path.Combine(SolutionDirectoryFullPath, ReferenceOutputDirectoryRelativePath); + internal static string ReferenceOutputDirectoryFullPath => Path.Combine( + SolutionDirectoryFullPath, + ReferenceOutputDirectoryRelativePath); internal static string GetReferenceOutputFileName(string actualOutputFileName) => actualOutputFileName.Replace("ActualOutput", @"External\ReferenceOutput"); + + internal static IImageDecoder GetReferenceDecoder(string filePath) + { + IImageFormat format = GetImageFormat(filePath); + return Configuration.FindDecoder(format); + } + + internal static IImageEncoder GetReferenceEncoder(string filePath) + { + IImageFormat format = GetImageFormat(filePath); + return Configuration.FindEncoder(format); + } + + internal static IImageFormat GetImageFormat(string filePath) + { + string extension = Path.GetExtension(filePath).ToLower(); + if (extension[0] == '.') extension = extension.Substring(1); + IImageFormat format = Configuration.FindFormatByFileExtension(extension); + return format; + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index 8d9b60cc05..1f931976c8 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -8,7 +8,9 @@ namespace ImageSharp.Tests using System; using System.Collections.Generic; using System.IO; + using System.Linq; + using ImageSharp.Formats; using ImageSharp.PixelFormats; using ImageSharp.Tests.TestUtilities.ImageComparison; using ImageSharp.Tests.TestUtilities.ReferenceCodecs; @@ -30,7 +32,6 @@ namespace ImageSharp.Tests ITestImageProvider provider, object testOutputDetails = null, string extension = "png", - bool grayscale = false, bool appendPixelTypeToFileName = true) where TPixel : struct, IPixel { @@ -44,7 +45,6 @@ namespace ImageSharp.Tests image, extension, testOutputDetails: testOutputDetails, - grayscale: grayscale, appendPixelTypeToFileName: appendPixelTypeToFileName); return image; } @@ -171,9 +171,17 @@ namespace ImageSharp.Tests var testFile = TestFile.Create(path); - using (var original = Image.Load(testFile.Bytes, ReferenceDecoder.Instance)) + IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(path); + IImageFormat format = TestEnvironment.GetImageFormat(path); + IImageDecoder defaultDecoder = Configuration.Default.FindDecoder(format); + + if (referenceDecoder.GetType() == defaultDecoder.GetType()) + { + throw new InvalidOperationException($"Can't use CompareToOriginal(): no actual reference decoder registered for {format.Name}"); + } + + using (var original = Image.Load(testFile.Bytes, referenceDecoder)) { - //original.DebugSave(provider, "__SYSTEMDRAWING__"); comparer.VerifySimilarity(original, image); } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestUtils.cs similarity index 97% rename from tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs rename to tests/ImageSharp.Tests/TestUtilities/TestUtils.cs index 835561fe0f..21ef03fa6f 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestUtils.cs @@ -11,12 +11,13 @@ namespace ImageSharp.Tests using System.Linq; using System.Reflection; + using ImageSharp.Formats; using ImageSharp.PixelFormats; /// - /// Extension methods for TestUtilities + /// Various utility and extension methods. /// - public static class TestUtilityExtensions + public static class TestUtils { private static readonly Dictionary ClrTypes2PixelTypes = new Dictionary(); @@ -28,7 +29,7 @@ namespace ImageSharp.Tests .Except(new[] { PixelTypes.Undefined, PixelTypes.All }) .ToArray(); - static TestUtilityExtensions() + static TestUtils() { // Add Rgba32 Our default. Type defaultPixelFormatType = typeof(Rgba32); diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/ReferenceCodecTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/ReferenceCodecTests.cs index 51b5f49b61..6e3afbcae2 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/ReferenceCodecTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/ReferenceCodecTests.cs @@ -52,7 +52,7 @@ namespace ImageSharp.Tests where TPixel : struct, IPixel { string path = TestFile.GetInputFileFullPath(TestImages.Png.Splash); - using (Image image = Image.Load(path, ReferenceDecoder.Instance)) + using (Image image = Image.Load(path, SystemDrawingReferenceDecoder.Instance)) { image.DebugSave(dummyProvider); } @@ -65,7 +65,7 @@ namespace ImageSharp.Tests { using (Image image = provider.GetImage()) { - provider.Utility.SaveTestOutputFile(image, "png", ReferenceEncoder.Png); + provider.Utility.SaveTestOutputFile(image, "png", SystemDrawingReferenceEncoder.Png); } } } diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs index ac537b3c39..10e08b5de0 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs @@ -6,8 +6,12 @@ // ReSharper disable InconsistentNaming namespace ImageSharp.Tests { + using System; using System.IO; + using ImageSharp.Formats; + using ImageSharp.Tests.TestUtilities.ReferenceCodecs; + using Xunit; using Xunit.Abstractions; @@ -59,5 +63,25 @@ namespace ImageSharp.Tests this.Output.WriteLine(expected); Assert.Contains(TestEnvironment.ReferenceOutputDirectoryFullPath, expected); } + + [Theory] + [InlineData("lol/foo.png", typeof(SystemDrawingReferenceEncoder))] + [InlineData("lol/Baz.JPG", typeof(JpegEncoder))] + [InlineData("lol/Baz.gif", typeof(GifEncoder))] + public void GetReferenceEncoder_ReturnsCorrectEncoders(string fileName, Type expectedEncoderType) + { + IImageEncoder encoder = TestEnvironment.GetReferenceEncoder(fileName); + Assert.IsType(expectedEncoderType, encoder); + } + + [Theory] + [InlineData("lol/foo.png", typeof(SystemDrawingReferenceDecoder))] + [InlineData("lol/Baz.JPG", typeof(JpegDecoder))] + [InlineData("lol/Baz.gif", typeof(GifDecoder))] + public void GetReferenceDecoder_ReturnsCorrectEncoders(string fileName, Type expectedDecoderType) + { + IImageDecoder decoder = TestEnvironment.GetReferenceDecoder(fileName); + Assert.IsType(expectedDecoderType, decoder); + } } } diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs index e9c4fc3d3d..f10ebeb301 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs @@ -165,10 +165,10 @@ namespace ImageSharp.Tests /// /// /// - public static Image CreateTestImage(GenericFactory factory) + public static Image CreateTestImage() where TPixel : struct, IPixel { - return factory.CreateImage(3, 3); + return new Image(3, 3); } [Theory] diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs index edee09c909..8651d246bc 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs @@ -27,10 +27,10 @@ namespace ImageSharp.Tests private ITestOutputHelper Output { get; } - public static Image CreateTestImage(GenericFactory factory) + public static Image CreateTestImage() where TPixel : struct, IPixel { - Image image = factory.CreateImage(10, 10); + var image = new Image(10, 10); using (PixelAccessor pixels = image.Lock()) { @@ -38,10 +38,10 @@ namespace ImageSharp.Tests { for (int j = 0; j < 10; j++) { - Vector4 v = new Vector4(i, j, 0, 1); + var v = new Vector4(i, j, 0, 1); v /= 10; - TPixel color = default(TPixel); + var color = default(TPixel); color.PackFromVector4(v); pixels[i, j] = color; @@ -147,7 +147,7 @@ namespace ImageSharp.Tests { KeyValuePair[] expanded = PixelTypes.All.ExpandAllTypes().ToArray(); - Assert.True(expanded.Length >= TestUtilityExtensions.GetAllPixelTypes().Length - 2); + Assert.True(expanded.Length >= TestUtils.GetAllPixelTypes().Length - 2); AssertContainsPixelType(PixelTypes.Rgba32, expanded); AssertContainsPixelType(PixelTypes.Rgba32, expanded); } diff --git a/tests/Images/External b/tests/Images/External index a91dd59e1c..5029858a87 160000 --- a/tests/Images/External +++ b/tests/Images/External @@ -1 +1 @@ -Subproject commit a91dd59e1cc2abbfa5ff2a8fb5690143343a3434 +Subproject commit 5029858a874a7e5127d516f0875b1e9df82d01b6 diff --git a/tests/Images/Input/Png/SnakeGame.png b/tests/Images/Input/Png/SnakeGame.png new file mode 100644 index 0000000000..96d72b38aa Binary files /dev/null and b/tests/Images/Input/Png/SnakeGame.png differ