//
// 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.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.
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.
/// A custom if exact equlity is not the expected behaviour
///
public static Image CompareToReferenceOutput(
this Image image,
ITestImageProvider provider,
object testOutputDetails = null,
string extension = "png",
bool grayscale = false)
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))
{
ImageComparer comparer = ImageComparer.Exact;
comparer.CompareImages(referenceImage, image);
}
return image;
}
}
}