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 // ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Tests namespace SixLabors.ImageSharp.Tests
{ {
using System.Collections.Generic;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
public class GifDecoderTests public class GifDecoderTests
{ {
private const PixelTypes TestPixelTypes = PixelTypes.Rgba32 | PixelTypes.RgbaVector | PixelTypes.Argb32; private const PixelTypes TestPixelTypes = PixelTypes.Rgba32 | PixelTypes.RgbaVector | PixelTypes.Argb32;
@ -26,56 +30,70 @@ namespace SixLabors.ImageSharp.Tests
TestImages.Gif.Giphy, TestImages.Gif.Kumin 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 }; public static readonly string[] BadAppExtFiles = { TestImages.Gif.Issues.BadAppExtLength, TestImages.Gif.Issues.BadAppExtLength_2 };
[Theory] [Theory]
[WithFileCollection(nameof(MultiFrameTestFiles), PixelTypes.Rgba32)] [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> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
{ {
image.DebugSaveMultiFrame(provider); image.DebugSaveMultiFrame(provider);
image.CompareToReferenceOutputMultiFrame(provider, ImageComparer.Exact);
} }
} }
[Theory] [Theory]
[WithFile(TestImages.Gif.Trans, TestPixelTypes)] [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> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
{ {
image.DebugSave(provider); image.DebugSave(provider);
image.CompareFirstFrameToReferenceOutput(provider, ImageComparer.Exact);
} }
} }
[Theory] [Theory]
[WithFileCollection(nameof(TestFiles), TestPixelTypes)] [WithFileCollection(nameof(BasicVerificationFiles), PixelTypes.Rgba32)]
public void DecodeAndReSave<TPixel>(TestImageProvider<TPixel> imageProvider) public void Decode_VerifyRootFrameAndFrameCount<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = imageProvider.GetImage()) if (!BasicVerificationFrameCount.TryGetValue(provider.SourceFileOrDescription, out int expectedFrameCount))
{ {
imageProvider.Utility.SaveTestOutputFile(image, "bmp"); expectedFrameCount = 1;
imageProvider.Utility.SaveTestOutputFile(image, "gif");
} }
}
[Theory] using (Image<TPixel> image = provider.GetImage())
[WithFileCollection(nameof(TestFiles), TestPixelTypes)]
public void DecodeResizeAndSave<TPixel>(TestImageProvider<TPixel> imageProvider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = imageProvider.GetImage())
{ {
image.Mutate(x => x.Resize(new Size(image.Width / 2, image.Height / 2))); Assert.Equal(expectedFrameCount, image.Frames.Count);
image.DebugSave(provider);
imageProvider.Utility.SaveTestOutputFile(image, "bmp"); image.CompareFirstFrameToReferenceOutput(provider, ImageComparer.Exact);
imageProvider.Utility.SaveTestOutputFile(image, "gif");
} }
} }
[Fact] [Fact]
public void Decode_IgnoreMetadataIsFalse_CommentsAreRead() 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; 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>( public static Image<TPixel> CompareToReferenceOutputMultiFrame<TPixel>(
this Image<TPixel> image, this Image<TPixel> image,
ITestImageProvider provider, ITestImageProvider provider,
@ -209,17 +235,19 @@ namespace SixLabors.ImageSharp.Tests
var tempImage = Image.Load<TPixel>(path, decoder); var tempImage = Image.Load<TPixel>(path, decoder);
temporalFrameImages.Add(tempImage); temporalFrameImages.Add(tempImage);
} }
Image<TPixel> firstTemp = temporalFrameImages[0];
var result = new Image<TPixel>( var result = new Image<TPixel>(firstTemp.Width, firstTemp.Height);
Configuration.Default,
new ImageMetaData(),
temporalFrameImages.Select(fi => fi.Frames.RootFrame));
foreach (Image<TPixel> fi in temporalFrameImages) foreach (Image<TPixel> fi in temporalFrameImages)
{ {
result.Frames.AddFrame(fi.Frames.RootFrame);
fi.Dispose(); fi.Dispose();
} }
// remove the initial empty frame:
result.Frames.RemoveFrame(0);
return result; return result;
} }

2
tests/Images/External

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