// // 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 System.Linq; using System.Reflection; using ImageSharp.PixelFormats; 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. public static Image DebugSave( this Image image, ITestImageProvider provider, object testOutputDetails = null, string extension = "png", bool grayscale = false) 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); 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. /// /// The threshold for the percentage difference where the images are asumed to be the same. /// The default/undefined value is /// /// /// The threshold of the individual segments before it acumulates towards the overall difference. /// The default undefined value is /// /// /// This is a sampling factor we sample a grid of average pixels width by high /// The default undefined value is /// /// public static Image CompareToReferenceOutput( this Image image, ITestImageProvider provider, object testOutputDetails = null, string extension = "png", bool grayscale = false, float imageTheshold = PercentageImageComparer.DefaultImageThreshold, byte segmentThreshold = PercentageImageComparer.DefaultSegmentThreshold, int scalingFactor = PercentageImageComparer.DefaultScaleIntoSize) where TPixel : struct, IPixel { string referenceOutputFile = provider.Utility.GetReferenceOutputFileName(extension, testOutputDetails); if (!TestEnvironment.RunsOnCI) { provider.Utility.SaveTestOutputFile( image, extension, testOutputDetails: testOutputDetails, grayscale: grayscale); } if (!File.Exists(referenceOutputFile)) { throw new Exception("Reference output file missing: " + referenceOutputFile); } using (Image referenceImage = Image.Load(referenceOutputFile, ReferenceDecoder.Instance)) { PercentageImageComparer.VerifySimilarity(referenceImage, image, imageTheshold, segmentThreshold, scalingFactor); } return image; } } }