Browse Source

DebugSaveMultiFrame() works

af/merge-core
Anton Firszov 8 years ago
parent
commit
9af8fb4e2a
  1. 1
      tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs
  2. 43
      tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs
  3. 65
      tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs
  4. 87
      tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs
  5. 24
      tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs

1
tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs

@ -10,7 +10,6 @@ using Xunit;
namespace SixLabors.ImageSharp.Tests
{
using static TestImages.Bmp;
public class BmpDecoderTests

43
tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs

@ -6,23 +6,52 @@ using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;
using Xunit;
using System.IO;
using SixLabors.ImageSharp.Advanced;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Tests
{
using System.IO;
using SixLabors.ImageSharp.Advanced;
public class GifDecoderTests
{
private const PixelTypes PixelTypes = Tests.PixelTypes.Rgba32 | Tests.PixelTypes.RgbaVector | Tests.PixelTypes.Argb32;
private const PixelTypes TestPixelTypes = PixelTypes.Rgba32 | PixelTypes.RgbaVector | PixelTypes.Argb32;
public static readonly string[] TestFiles =
{
TestImages.Gif.Giphy, TestImages.Gif.Rings, TestImages.Gif.Trans, TestImages.Gif.Kumin
};
public static readonly string[] TestFiles = { TestImages.Gif.Giphy, TestImages.Gif.Rings, TestImages.Gif.Trans };
public static readonly string[] MultiFrameTestFiles =
{
TestImages.Gif.Giphy, TestImages.Gif.Kumin
};
public static readonly string[] BadAppExtFiles = { TestImages.Gif.Issues.BadAppExtLength, TestImages.Gif.Issues.BadAppExtLength_2 };
[Theory]
[WithFileCollection(nameof(TestFiles), PixelTypes)]
[WithFileCollection(nameof(MultiFrameTestFiles), PixelTypes.Rgba32)]
public void AllFramesDecoded<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.DebugSaveMultiFrame(provider);
}
}
[Theory]
[WithFile(TestImages.Gif.Trans, TestPixelTypes)]
public void IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.DebugSave(provider);
}
}
[Theory]
[WithFileCollection(nameof(TestFiles), TestPixelTypes)]
public void DecodeAndReSave<TPixel>(TestImageProvider<TPixel> imageProvider)
where TPixel : struct, IPixel<TPixel>
{
@ -34,7 +63,7 @@ namespace SixLabors.ImageSharp.Tests
}
[Theory]
[WithFileCollection(nameof(TestFiles), PixelTypes)]
[WithFileCollection(nameof(TestFiles), TestPixelTypes)]
public void DecodeResizeAndSave<TPixel>(TestImageProvider<TPixel> imageProvider)
where TPixel : struct, IPixel<TPixel>
{

65
tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs

@ -157,16 +157,77 @@ namespace SixLabors.ImageSharp.Tests
return path;
}
public IEnumerable<string> GetTestOutputFileNamesMultiFrame(
int frameCount,
string extension = null,
object testOutputDetails = null,
bool appendPixelTypeToFileName = true)
{
string baseDir = this.GetTestOutputFileName("", testOutputDetails, appendPixelTypeToFileName);
if (!Directory.Exists(baseDir))
{
Directory.CreateDirectory(baseDir);
}
for (int i = 0; i < frameCount; i++)
{
string filePath = $"{baseDir}/{i:D2}.{extension}";
yield return filePath;
}
}
public string[] SaveTestOutputFileMultiFrame<TPixel>(
Image<TPixel> image,
string extension = "png",
IImageEncoder encoder = null,
object testOutputDetails = null,
bool appendPixelTypeToFileName = true)
where TPixel : struct, IPixel<TPixel>
{
encoder = encoder ?? TestEnvironment.GetReferenceEncoder($"foo.{extension}");
string[] files = this.GetTestOutputFileNamesMultiFrame(
image.Frames.Count,
extension,
testOutputDetails,
appendPixelTypeToFileName).ToArray();
for (int i = 0; i < image.Frames.Count; i++)
{
using (Image<TPixel> frameImage = image.Frames.CloneFrame(i))
{
string filePath = files[i];
using (FileStream stream = File.OpenWrite(filePath))
{
frameImage.Save(stream, encoder);
}
}
}
return files;
}
internal string GetReferenceOutputFileName(
string extension,
object settings,
object testOutputDetails,
bool appendPixelTypeToFileName)
{
return TestEnvironment.GetReferenceOutputFileName(
this.GetTestOutputFileName(extension, settings, appendPixelTypeToFileName)
this.GetTestOutputFileName(extension, testOutputDetails, appendPixelTypeToFileName)
);
}
public string[] GetReferenceOutputFileNamesMultiFrame(
int frameCount,
string extension,
object testOutputDetails,
bool appendPixelTypeToFileName = true)
{
return this.GetTestOutputFileNamesMultiFrame(frameCount, extension, testOutputDetails)
.Select(TestEnvironment.GetReferenceOutputFileName).ToArray();
}
internal void Init(string typeName, string methodName, string outputSubfolderName)
{
this.TestGroupName = typeName;

87
tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

@ -16,6 +16,7 @@ namespace SixLabors.ImageSharp.Tests
using System.Numerics;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.MetaData;
using Xunit;
@ -52,6 +53,28 @@ namespace SixLabors.ImageSharp.Tests
return image;
}
public static Image<TPixel> DebugSaveMultiFrame<TPixel>(
this Image<TPixel> image,
ITestImageProvider provider,
object testOutputDetails = null,
string extension = "png",
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.SaveTestOutputFileMultiFrame(
image,
extension,
testOutputDetails: testOutputDetails,
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)"/>.
@ -118,6 +141,29 @@ namespace SixLabors.ImageSharp.Tests
return image;
}
public static Image<TPixel> CompareToReferenceOutputMultiFrame<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>
{
using (Image<TPixel> referenceImage = GetReferenceOutputImageMultiFrame<TPixel>(
provider,
image.Frames.Count,
testOutputDetails,
extension,
appendPixelTypeToFileName))
{
comparer.VerifySimilarity(referenceImage, image);
}
return image;
}
public static Image<TPixel> GetReferenceOutputImage<TPixel>(this ITestImageProvider provider,
object testOutputDetails = null,
string extension = "png",
@ -136,6 +182,47 @@ namespace SixLabors.ImageSharp.Tests
return Image.Load<TPixel>(referenceOutputFile, decoder);
}
public static Image<TPixel> GetReferenceOutputImageMultiFrame<TPixel>(this ITestImageProvider provider,
int frameCount,
object testOutputDetails = null,
string extension = "png",
bool appendPixelTypeToFileName = true)
where TPixel : struct, IPixel<TPixel>
{
string[] frameFiles = provider.Utility.GetReferenceOutputFileNamesMultiFrame(
frameCount,
extension,
testOutputDetails,
appendPixelTypeToFileName);
var temporalFrameImages = new List<Image<TPixel>>();
IImageDecoder decoder = TestEnvironment.GetReferenceDecoder(frameFiles[0]);
foreach (string path in frameFiles)
{
if (!File.Exists(path))
{
throw new Exception("Reference output file missing: " + path);
}
var tempImage = Image.Load<TPixel>(path, decoder);
temporalFrameImages.Add(tempImage);
}
var result = new Image<TPixel>(
Configuration.Default,
new ImageMetaData(),
temporalFrameImages.Select(fi => fi.Frames.RootFrame));
foreach (Image<TPixel> fi in temporalFrameImages)
{
fi.Dispose();
}
return result;
}
public static IEnumerable<ImageSimilarityReport> GetReferenceOutputSimilarityReports<TPixel>(
this Image<TPixel> image,
ITestImageProvider provider,

24
tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs

@ -239,8 +239,28 @@ namespace SixLabors.ImageSharp.Tests
where TPixel : struct, IPixel<TPixel>
{
Assert.NotNull(provider.Utility.SourceFileOrDescription);
Image<TPixel> image = provider.GetImage();
provider.Utility.SaveTestOutputFile(image, "png");
using (Image<TPixel> image = provider.GetImage())
{
provider.Utility.SaveTestOutputFile(image, "png");
}
}
[Theory]
[WithFile(TestImages.Gif.Giphy, PixelTypes.Rgba32)]
public void SaveTestOutputFileMultiFrame<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
string[] files = provider.Utility.SaveTestOutputFileMultiFrame(image);
Assert.True(files.Length > 2);
foreach (string path in files)
{
this.Output.WriteLine(path);
Assert.True(File.Exists(path));
}
}
}
[Theory]

Loading…
Cancel
Save