From 9af8fb4e2abb74950884907357d72cc69b3aaf2b Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 18 Feb 2018 21:21:01 +0100 Subject: [PATCH] DebugSaveMultiFrame() works --- .../Formats/Bmp/BmpDecoderTests.cs | 1 - .../Formats/Gif/GifDecoderTests.cs | 43 +++++++-- .../TestUtilities/ImagingTestCaseUtility.cs | 65 +++++++++++++- .../TestUtilities/TestImageExtensions.cs | 87 +++++++++++++++++++ .../Tests/TestImageProviderTests.cs | 24 ++++- 5 files changed, 208 insertions(+), 12 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs index 2209f223b..d958278f6 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs @@ -10,7 +10,6 @@ using Xunit; namespace SixLabors.ImageSharp.Tests { - using static TestImages.Bmp; public class BmpDecoderTests diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs index 9a095548a..f92d5da81 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs @@ -6,23 +6,52 @@ using SixLabors.ImageSharp.Formats.Gif; using SixLabors.ImageSharp.PixelFormats; using SixLabors.Primitives; using Xunit; +using System.IO; +using SixLabors.ImageSharp.Advanced; // ReSharper disable InconsistentNaming namespace SixLabors.ImageSharp.Tests { - using System.IO; - using SixLabors.ImageSharp.Advanced; - public class GifDecoderTests { - private const PixelTypes PixelTypes = Tests.PixelTypes.Rgba32 | Tests.PixelTypes.RgbaVector | Tests.PixelTypes.Argb32; + private const PixelTypes TestPixelTypes = PixelTypes.Rgba32 | PixelTypes.RgbaVector | PixelTypes.Argb32; + + public static readonly string[] TestFiles = + { + TestImages.Gif.Giphy, TestImages.Gif.Rings, TestImages.Gif.Trans, TestImages.Gif.Kumin + }; - public static readonly string[] TestFiles = { TestImages.Gif.Giphy, TestImages.Gif.Rings, TestImages.Gif.Trans }; + public static readonly string[] MultiFrameTestFiles = + { + TestImages.Gif.Giphy, TestImages.Gif.Kumin + }; public static readonly string[] BadAppExtFiles = { TestImages.Gif.Issues.BadAppExtLength, TestImages.Gif.Issues.BadAppExtLength_2 }; [Theory] - [WithFileCollection(nameof(TestFiles), PixelTypes)] + [WithFileCollection(nameof(MultiFrameTestFiles), PixelTypes.Rgba32)] + public void AllFramesDecoded(TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage()) + { + image.DebugSaveMultiFrame(provider); + } + } + + [Theory] + [WithFile(TestImages.Gif.Trans, TestPixelTypes)] + public void IsNotBoundToSinglePixelType(TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage()) + { + image.DebugSave(provider); + } + } + + [Theory] + [WithFileCollection(nameof(TestFiles), TestPixelTypes)] public void DecodeAndReSave(TestImageProvider imageProvider) where TPixel : struct, IPixel { @@ -34,7 +63,7 @@ namespace SixLabors.ImageSharp.Tests } [Theory] - [WithFileCollection(nameof(TestFiles), PixelTypes)] + [WithFileCollection(nameof(TestFiles), TestPixelTypes)] public void DecodeResizeAndSave(TestImageProvider imageProvider) where TPixel : struct, IPixel { diff --git a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs index e7dfe5488..cde8ec9e4 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs @@ -157,16 +157,77 @@ namespace SixLabors.ImageSharp.Tests return path; } + public IEnumerable GetTestOutputFileNamesMultiFrame( + int frameCount, + string extension = null, + object testOutputDetails = null, + bool appendPixelTypeToFileName = true) + { + string baseDir = this.GetTestOutputFileName("", testOutputDetails, appendPixelTypeToFileName); + + if (!Directory.Exists(baseDir)) + { + Directory.CreateDirectory(baseDir); + } + + for (int i = 0; i < frameCount; i++) + { + string filePath = $"{baseDir}/{i:D2}.{extension}"; + yield return filePath; + } + } + + public string[] SaveTestOutputFileMultiFrame( + Image image, + string extension = "png", + IImageEncoder encoder = null, + object testOutputDetails = null, + bool appendPixelTypeToFileName = true) + where TPixel : struct, IPixel + { + encoder = encoder ?? TestEnvironment.GetReferenceEncoder($"foo.{extension}"); + + string[] files = this.GetTestOutputFileNamesMultiFrame( + image.Frames.Count, + extension, + testOutputDetails, + appendPixelTypeToFileName).ToArray(); + + for (int i = 0; i < image.Frames.Count; i++) + { + using (Image frameImage = image.Frames.CloneFrame(i)) + { + string filePath = files[i]; + using (FileStream stream = File.OpenWrite(filePath)) + { + frameImage.Save(stream, encoder); + } + } + } + + return files; + } + internal string GetReferenceOutputFileName( string extension, - object settings, + object testOutputDetails, bool appendPixelTypeToFileName) { return TestEnvironment.GetReferenceOutputFileName( - this.GetTestOutputFileName(extension, settings, appendPixelTypeToFileName) + this.GetTestOutputFileName(extension, testOutputDetails, appendPixelTypeToFileName) ); } + public string[] GetReferenceOutputFileNamesMultiFrame( + int frameCount, + string extension, + object testOutputDetails, + bool appendPixelTypeToFileName = true) + { + return this.GetTestOutputFileNamesMultiFrame(frameCount, extension, testOutputDetails) + .Select(TestEnvironment.GetReferenceOutputFileName).ToArray(); + } + internal void Init(string typeName, string methodName, string outputSubfolderName) { this.TestGroupName = typeName; diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index 5d43816a5..c95ebc424 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -16,6 +16,7 @@ namespace SixLabors.ImageSharp.Tests using System.Numerics; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Memory; + using SixLabors.ImageSharp.MetaData; using Xunit; @@ -52,6 +53,28 @@ namespace SixLabors.ImageSharp.Tests return image; } + public static Image DebugSaveMultiFrame( + this Image image, + ITestImageProvider provider, + object testOutputDetails = null, + string extension = "png", + bool appendPixelTypeToFileName = true) + where TPixel : struct, IPixel + { + if (TestEnvironment.RunsOnCI) + { + return image; + } + + // We are running locally then we want to save it out + provider.Utility.SaveTestOutputFileMultiFrame( + image, + extension, + testOutputDetails: testOutputDetails, + appendPixelTypeToFileName: appendPixelTypeToFileName); + return image; + } + /// /// Compares the image against the expected Reference output, throws an exception if the images are not similar enough. /// The output file should be named identically to the output produced by . @@ -118,6 +141,29 @@ namespace SixLabors.ImageSharp.Tests return image; } + public static Image CompareToReferenceOutputMultiFrame( + this Image image, + ITestImageProvider provider, + ImageComparer comparer, + object testOutputDetails = null, + string extension = "png", + bool grayscale = false, + bool appendPixelTypeToFileName = true) + where TPixel : struct, IPixel + { + using (Image referenceImage = GetReferenceOutputImageMultiFrame( + provider, + image.Frames.Count, + testOutputDetails, + extension, + appendPixelTypeToFileName)) + { + comparer.VerifySimilarity(referenceImage, image); + } + + return image; + } + public static Image GetReferenceOutputImage(this ITestImageProvider provider, object testOutputDetails = null, string extension = "png", @@ -136,6 +182,47 @@ namespace SixLabors.ImageSharp.Tests return Image.Load(referenceOutputFile, decoder); } + public static Image GetReferenceOutputImageMultiFrame(this ITestImageProvider provider, + int frameCount, + object testOutputDetails = null, + string extension = "png", + bool appendPixelTypeToFileName = true) + where TPixel : struct, IPixel + { + string[] frameFiles = provider.Utility.GetReferenceOutputFileNamesMultiFrame( + frameCount, + extension, + testOutputDetails, + appendPixelTypeToFileName); + + var temporalFrameImages = new List>(); + + IImageDecoder decoder = TestEnvironment.GetReferenceDecoder(frameFiles[0]); + + foreach (string path in frameFiles) + { + if (!File.Exists(path)) + { + throw new Exception("Reference output file missing: " + path); + } + + var tempImage = Image.Load(path, decoder); + temporalFrameImages.Add(tempImage); + } + + var result = new Image( + Configuration.Default, + new ImageMetaData(), + temporalFrameImages.Select(fi => fi.Frames.RootFrame)); + + foreach (Image fi in temporalFrameImages) + { + fi.Dispose(); + } + + return result; + } + public static IEnumerable GetReferenceOutputSimilarityReports( this Image image, ITestImageProvider provider, diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs index f0adeb753..2f306e949 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs @@ -239,8 +239,28 @@ namespace SixLabors.ImageSharp.Tests where TPixel : struct, IPixel { Assert.NotNull(provider.Utility.SourceFileOrDescription); - Image image = provider.GetImage(); - provider.Utility.SaveTestOutputFile(image, "png"); + using (Image image = provider.GetImage()) + { + provider.Utility.SaveTestOutputFile(image, "png"); + } + } + + [Theory] + [WithFile(TestImages.Gif.Giphy, PixelTypes.Rgba32)] + public void SaveTestOutputFileMultiFrame(TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage()) + { + string[] files = provider.Utility.SaveTestOutputFileMultiFrame(image); + + Assert.True(files.Length > 2); + foreach (string path in files) + { + this.Output.WriteLine(path); + Assert.True(File.Exists(path)); + } + } } [Theory]