Browse Source

GifDecoderTests using reference images

af/merge-core
Anton Firszov 8 years ago
parent
commit
52ad911330
  1. 78
      tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs
  2. 36
      tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs
  3. 2
      tests/Images/External

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

@ -12,6 +12,10 @@ using SixLabors.ImageSharp.Advanced;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Tests
{
using System.Collections.Generic;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
public class GifDecoderTests
{
private const PixelTypes TestPixelTypes = PixelTypes.Rgba32 | PixelTypes.RgbaVector | PixelTypes.Argb32;
@ -26,56 +30,70 @@ namespace SixLabors.ImageSharp.Tests
TestImages.Gif.Giphy, TestImages.Gif.Kumin
};
public static readonly string[] BasicVerificationFiles =
{
TestImages.Gif.Cheers,
TestImages.Gif.Rings,
// previously DecodeBadApplicationExtensionLength:
TestImages.Gif.Issues.BadAppExtLength,
TestImages.Gif.Issues.BadAppExtLength_2,
// previously DecodeBadDescriptorDimensionsLength:
TestImages.Gif.Issues.BadDescriptorWidth
};
private static readonly Dictionary<string, int> BasicVerificationFrameCount =
new Dictionary<string, int>
{
[TestImages.Gif.Cheers] = 93,
[TestImages.Gif.Issues.BadDescriptorWidth] = 36,
};
public static readonly string[] BadAppExtFiles = { TestImages.Gif.Issues.BadAppExtLength, TestImages.Gif.Issues.BadAppExtLength_2 };
[Theory]
[WithFileCollection(nameof(MultiFrameTestFiles), PixelTypes.Rgba32)]
public void AllFramesDecoded<TPixel>(TestImageProvider<TPixel> provider)
public void Decode_VerifyAllFrames<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.DebugSaveMultiFrame(provider);
image.CompareToReferenceOutputMultiFrame(provider, ImageComparer.Exact);
}
}
[Theory]
[WithFile(TestImages.Gif.Trans, TestPixelTypes)]
public void IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
public void GifDecoder_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.DebugSave(provider);
image.CompareFirstFrameToReferenceOutput(provider, ImageComparer.Exact);
}
}
[Theory]
[WithFileCollection(nameof(TestFiles), TestPixelTypes)]
public void DecodeAndReSave<TPixel>(TestImageProvider<TPixel> imageProvider)
[WithFileCollection(nameof(BasicVerificationFiles), PixelTypes.Rgba32)]
public void Decode_VerifyRootFrameAndFrameCount<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = imageProvider.GetImage())
if (!BasicVerificationFrameCount.TryGetValue(provider.SourceFileOrDescription, out int expectedFrameCount))
{
imageProvider.Utility.SaveTestOutputFile(image, "bmp");
imageProvider.Utility.SaveTestOutputFile(image, "gif");
expectedFrameCount = 1;
}
}
[Theory]
[WithFileCollection(nameof(TestFiles), TestPixelTypes)]
public void DecodeResizeAndSave<TPixel>(TestImageProvider<TPixel> imageProvider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = imageProvider.GetImage())
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.Resize(new Size(image.Width / 2, image.Height / 2)));
imageProvider.Utility.SaveTestOutputFile(image, "bmp");
imageProvider.Utility.SaveTestOutputFile(image, "gif");
Assert.Equal(expectedFrameCount, image.Frames.Count);
image.DebugSave(provider);
image.CompareFirstFrameToReferenceOutput(provider, ImageComparer.Exact);
}
}
[Fact]
public void Decode_IgnoreMetadataIsFalse_CommentsAreRead()
{
@ -178,27 +196,5 @@ namespace SixLabors.ImageSharp.Tests
}
}
}
[Theory]
[WithFileCollection(nameof(BadAppExtFiles), PixelTypes.Rgba32)]
public void DecodeBadApplicationExtensionLength<TPixel>(TestImageProvider<TPixel> imageProvider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = imageProvider.GetImage())
{
imageProvider.Utility.SaveTestOutputFile(image, "bmp");
}
}
[Theory]
[WithFile(TestImages.Gif.Issues.BadDescriptorWidth, PixelTypes.Rgba32)]
public void DecodeBadDescriptorDimensionsLength<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
provider.Utility.SaveTestOutputFile(image, "bmp");
}
}
}
}

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

@ -141,6 +141,32 @@ namespace SixLabors.ImageSharp.Tests
return image;
}
public static Image<TPixel> CompareFirstFrameToReferenceOutput<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> firstFrameOnlyImage = new Image<TPixel>(image.Width, image.Height))
using (Image<TPixel> referenceImage = GetReferenceOutputImage<TPixel>(
provider,
testOutputDetails,
extension,
appendPixelTypeToFileName))
{
firstFrameOnlyImage.Frames.AddFrame(image.Frames.RootFrame);
firstFrameOnlyImage.Frames.RemoveFrame(0);
comparer.VerifySimilarity(referenceImage, firstFrameOnlyImage);
}
return image;
}
public static Image<TPixel> CompareToReferenceOutputMultiFrame<TPixel>(
this Image<TPixel> image,
ITestImageProvider provider,
@ -209,17 +235,19 @@ namespace SixLabors.ImageSharp.Tests
var tempImage = Image.Load<TPixel>(path, decoder);
temporalFrameImages.Add(tempImage);
}
Image<TPixel> firstTemp = temporalFrameImages[0];
var result = new Image<TPixel>(
Configuration.Default,
new ImageMetaData(),
temporalFrameImages.Select(fi => fi.Frames.RootFrame));
var result = new Image<TPixel>(firstTemp.Width, firstTemp.Height);
foreach (Image<TPixel> fi in temporalFrameImages)
{
result.Frames.AddFrame(fi.Frames.RootFrame);
fi.Dispose();
}
// remove the initial empty frame:
result.Frames.RemoveFrame(0);
return result;
}

2
tests/Images/External

@ -1 +1 @@
Subproject commit 6508e097adf1ef6c38a8df17465c6868ed133256
Subproject commit 65db1d045e74e7702ec33b44f88f97b4bf86f1ea
Loading…
Cancel
Save