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/Processing/Processors/Convolution/DetectEdgesTest.cs b/tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs index c753528f38..ee6650e55c 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs @@ -40,7 +40,7 @@ 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()); } } @@ -52,7 +52,7 @@ namespace ImageSharp.Tests.Processing.Processors.Convolution using (Image image = provider.GetImage()) { image.Mutate(x => x.DetectEdges()); - image.DebugSave(provider, grayscale: true); + image.DebugSave(provider); } } @@ -79,7 +79,7 @@ namespace ImageSharp.Tests.Processing.Processors.Convolution 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); // TODO: We don't need this any longer after switching to ReferenceImages ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds); 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/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..7cfc5c6e40 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); + } + + private 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 35a7d17d40..e4563df4fd 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -29,7 +29,6 @@ namespace ImageSharp.Tests ITestImageProvider provider, object testOutputDetails = null, string extension = "png", - bool grayscale = false, bool appendPixelTypeToFileName = true) where TPixel : struct, IPixel { @@ -43,7 +42,6 @@ namespace ImageSharp.Tests image, extension, testOutputDetails: testOutputDetails, - grayscale: grayscale, appendPixelTypeToFileName: appendPixelTypeToFileName); return image; } @@ -139,7 +137,7 @@ namespace ImageSharp.Tests var testFile = TestFile.Create(path); - using (var original = Image.Load(testFile.Bytes, ReferenceDecoder.Instance)) + using (var original = Image.Load(testFile.Bytes, SystemDrawingReferenceDecoder.Instance)) { //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/TestUtilityExtensionsTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs index 9a468da678..8651d246bc 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs @@ -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 6996009ff5..4929cbe0d7 160000 --- a/tests/Images/External +++ b/tests/Images/External @@ -1 +1 @@ -Subproject commit 6996009ff537d1c9cbc2b93d692cd89bf8f2a5c7 +Subproject commit 4929cbe0d743fe3c67a9b4d0c71fb3eed8b5537d