// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageSharp.Tests { using System; using System.Collections.Generic; using System.IO; using ImageSharp.PixelFormats; using ImageSharp.Tests.TestUtilities.ImageComparison; using ImageSharp.Tests.TestUtilities.ReferenceCodecs; public static class TestImageExtensions { /// /// Saves the image only when not running in the CI server. /// /// The pixel format /// The image /// The image provider /// Details to be concatenated to the test output file, describing the parameters of the test. /// The extension /// A boolean indicating whether we should save a smaller in size. /// A boolean indicating whether to append the pixel type to the output file name. public static Image DebugSave( this Image image, ITestImageProvider provider, object testOutputDetails = null, string extension = "png", bool grayscale = false, 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.SaveTestOutputFile( image, extension, testOutputDetails: testOutputDetails, grayscale: grayscale, 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 . /// /// The pixel format /// The image /// The image provider /// Details to be concatenated to the test output file, describing the parameters of the test. /// The extension /// A boolean indicating whether we should debug save + compare against a grayscale image, smaller in size. /// A boolean indicating whether to append the pixel type to the output file name. /// public static Image CompareToReferenceOutput( this Image image, ITestImageProvider provider, object testOutputDetails = null, string extension = "png", bool grayscale = false, bool appendPixelTypeToFileName = true) where TPixel : struct, IPixel { return CompareToReferenceOutput( image, provider, ImageComparer.Tolerant(), testOutputDetails, extension, grayscale, appendPixelTypeToFileName); } /// /// 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 . /// /// The pixel format /// The image /// The image provider /// The to use /// Details to be concatenated to the test output file, describing the parameters of the test. /// The extension /// A boolean indicating whether we should debug save + compare against a grayscale image, smaller in size. /// A boolean indicating whether to append the pixel type to the output file name. /// public static Image CompareToReferenceOutput( 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 = GetReferenceOutputImage( provider, testOutputDetails, extension, appendPixelTypeToFileName)) { comparer.VerifySimilarity(referenceImage, image); } return image; } public static Image GetReferenceOutputImage(this ITestImageProvider provider, object testOutputDetails = null, string extension = "png", bool appendPixelTypeToFileName = true) where TPixel : struct, IPixel { string referenceOutputFile = provider.Utility.GetReferenceOutputFileName(extension, testOutputDetails, appendPixelTypeToFileName); if (!File.Exists(referenceOutputFile)) { throw new Exception("Reference output file missing: " + referenceOutputFile); } return Image.Load(referenceOutputFile); } public static IEnumerable GetReferenceOutputSimilarityReports( this Image image, ITestImageProvider provider, ImageComparer comparer, object testOutputDetails = null, string extension = "png", bool appendPixelTypeToFileName = true) where TPixel : struct, IPixel { using (Image referenceImage = provider.GetReferenceOutputImage( testOutputDetails, extension, appendPixelTypeToFileName)) { return comparer.CompareImages(referenceImage, image); } } public static Image CompareToOriginal( this Image image, ITestImageProvider provider) where TPixel : struct, IPixel { return CompareToOriginal(image, provider, ImageComparer.Tolerant()); } public static Image CompareToOriginal( this Image image, ITestImageProvider provider, ImageComparer comparer) where TPixel : struct, IPixel { string path = TestImageProvider.GetFilePathOrNull(provider); if (path == null) { throw new InvalidOperationException("CompareToOriginal() works only with file providers!"); } var testFile = TestFile.Create(path); using (var original = Image.Load(testFile.Bytes, ReferenceDecoder.Instance)) { //original.DebugSave(provider, "__SYSTEMDRAWING__"); comparer.VerifySimilarity(original, image); } return image; } } }