mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
151 lines
6.8 KiB
151 lines
6.8 KiB
// <copyright file="TestImageExtensions.cs" company="James Jackson-South">
|
|
// Copyright (c) James Jackson-South and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
// </copyright>
|
|
|
|
namespace ImageSharp.Tests
|
|
{
|
|
using System;
|
|
using System.IO;
|
|
|
|
using ImageSharp.PixelFormats;
|
|
using ImageSharp.Tests.TestUtilities.ImageComparison;
|
|
using ImageSharp.Tests.TestUtilities.ReferenceCodecs;
|
|
|
|
public static class TestImageExtensions
|
|
{
|
|
/// <summary>
|
|
/// Saves the image only when not running in the CI server.
|
|
/// </summary>
|
|
/// <typeparam name="TPixel">The pixel format</typeparam>
|
|
/// <param name="image">The image</param>
|
|
/// <param name="provider">The image provider</param>
|
|
/// <param name="testOutputDetails">Details to be concatenated to the test output file, describing the parameters of the test.</param>
|
|
/// <param name="extension">The extension</param>
|
|
/// <param name="grayscale">A boolean indicating whether we should save a smaller in size.</param>
|
|
/// <param name="appendPixelTypeToFileName">A boolean indicating whether to append the pixel type to the output file name.</param>
|
|
public static Image<TPixel> DebugSave<TPixel>(
|
|
this Image<TPixel> image,
|
|
ITestImageProvider provider,
|
|
object testOutputDetails = null,
|
|
string extension = "png",
|
|
bool grayscale = false,
|
|
bool appendPixelTypeToFileName = true)
|
|
where TPixel : struct, IPixel<TPixel>
|
|
{
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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 <see cref="DebugSave{TPixel}(Image{TPixel}, ITestImageProvider, object, string, bool)"/>.
|
|
/// </summary>
|
|
/// <typeparam name="TPixel">The pixel format</typeparam>
|
|
/// <param name="image">The image</param>
|
|
/// <param name="provider">The image provider</param>
|
|
/// <param name="testOutputDetails">Details to be concatenated to the test output file, describing the parameters of the test.</param>
|
|
/// <param name="extension">The extension</param>
|
|
/// <param name="grayscale">A boolean indicating whether we should debug save + compare against a grayscale image, smaller in size.</param>
|
|
/// <param name="appendPixelTypeToFileName">A boolean indicating whether to append the pixel type to the output file name.</param>
|
|
/// <returns></returns>
|
|
public static Image<TPixel> CompareToReferenceOutput<TPixel>(
|
|
this Image<TPixel> image,
|
|
ITestImageProvider provider,
|
|
object testOutputDetails = null,
|
|
string extension = "png",
|
|
bool grayscale = false,
|
|
bool appendPixelTypeToFileName = true)
|
|
where TPixel : struct, IPixel<TPixel>
|
|
{
|
|
return CompareToReferenceOutput(
|
|
image,
|
|
provider,
|
|
ImageComparer.Tolerant(),
|
|
testOutputDetails,
|
|
extension,
|
|
grayscale,
|
|
appendPixelTypeToFileName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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 <see cref="DebugSave{TPixel}(Image{TPixel}, ITestImageProvider, object, string, bool)"/>.
|
|
/// </summary>
|
|
/// <typeparam name="TPixel">The pixel format</typeparam>
|
|
/// <param name="image">The image</param>
|
|
/// <param name="provider">The image provider</param>
|
|
/// <param name="comparer">The <see cref="ImageComparer"/> to use</param>
|
|
/// <param name="testOutputDetails">Details to be concatenated to the test output file, describing the parameters of the test.</param>
|
|
/// <param name="extension">The extension</param>
|
|
/// <param name="grayscale">A boolean indicating whether we should debug save + compare against a grayscale image, smaller in size.</param>
|
|
/// <param name="appendPixelTypeToFileName">A boolean indicating whether to append the pixel type to the output file name.</param>
|
|
/// <returns></returns>
|
|
public static Image<TPixel> CompareToReferenceOutput<TPixel>(
|
|
this Image<TPixel> image,
|
|
ITestImageProvider provider,
|
|
ImageComparer comparer,
|
|
object testOutputDetails = null,
|
|
string extension = "png",
|
|
bool grayscale = false,
|
|
bool appendPixelTypeToFileName = true)
|
|
where TPixel : struct, IPixel<TPixel>
|
|
{
|
|
string referenceOutputFile = provider.Utility.GetReferenceOutputFileName(extension, testOutputDetails, appendPixelTypeToFileName);
|
|
|
|
if (!File.Exists(referenceOutputFile))
|
|
{
|
|
throw new Exception("Reference output file missing: " + referenceOutputFile);
|
|
}
|
|
|
|
using (var referenceImage = Image.Load<Rgba32>(referenceOutputFile/*, ReferenceDecoder.Instance*/))
|
|
{
|
|
comparer.VerifySimilarity(referenceImage, image);
|
|
}
|
|
|
|
return image;
|
|
}
|
|
|
|
public static Image<TPixel> CompareToOriginal<TPixel>(
|
|
this Image<TPixel> image,
|
|
ITestImageProvider provider)
|
|
where TPixel : struct, IPixel<TPixel>
|
|
{
|
|
return CompareToOriginal(image, provider, ImageComparer.Tolerant());
|
|
}
|
|
|
|
public static Image<TPixel> CompareToOriginal<TPixel>(
|
|
this Image<TPixel> image,
|
|
ITestImageProvider provider,
|
|
ImageComparer comparer)
|
|
where TPixel : struct, IPixel<TPixel>
|
|
{
|
|
string path = TestImageProvider<TPixel>.GetFilePathOrNull(provider);
|
|
if (path == null)
|
|
{
|
|
throw new InvalidOperationException("CompareToOriginal() works only with file providers!");
|
|
}
|
|
|
|
var testFile = TestFile.Create(path);
|
|
|
|
using (var original = Image.Load<TPixel>(testFile.Bytes, ReferenceDecoder.Instance))
|
|
{
|
|
//original.DebugSave(provider, "__SYSTEMDRAWING__");
|
|
comparer.VerifySimilarity(original, image);
|
|
}
|
|
|
|
return image;
|
|
}
|
|
}
|
|
}
|