// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs;
namespace SixLabors.ImageSharp.Tests
{
using System.Numerics;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
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 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,
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);
}
IImageDecoder decoder = TestEnvironment.GetReferenceDecoder(referenceOutputFile);
return Image.Load(referenceOutputFile, decoder);
}
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);
IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(path);
IImageFormat format = TestEnvironment.GetImageFormat(path);
IImageDecoder defaultDecoder = Configuration.Default.FindDecoder(format);
//if (referenceDecoder.GetType() == defaultDecoder.GetType())
//{
// throw new InvalidOperationException($"Can't use CompareToOriginal(): no actual reference decoder registered for {format.Name}");
//}
using (var original = Image.Load(testFile.Bytes, referenceDecoder))
{
comparer.VerifySimilarity(original, image);
}
return image;
}
internal static Image ToGrayscaleImage(this Buffer2D buffer, float scale)
{
var image = new Image(buffer.Width, buffer.Height);
Span pixels = image.GetPixelSpan();
for (int i = 0; i < buffer.Length; i++)
{
float value = buffer[i] * scale;
var v = new Vector4(value, value, value, 1f);
pixels[i].PackFromVector4(v);
}
return image;
}
}
}