Browse Source

Add gif tests

af/merge-core
James Jackson-South 8 years ago
parent
commit
1941108d6c
  1. 88
      tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs
  2. 36
      tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs
  3. 5
      tests/ImageSharp.Tests/TestImages.cs
  4. 3
      tests/Images/Input/Gif/base_1x4.gif
  5. 3
      tests/Images/Input/Gif/base_4x1.gif

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

@ -13,7 +13,7 @@ using SixLabors.ImageSharp.Advanced;
namespace SixLabors.ImageSharp.Tests.Formats.Gif namespace SixLabors.ImageSharp.Tests.Formats.Gif
{ {
using System.Collections.Generic; using System.Collections.Generic;
using SixLabors.ImageSharp.MetaData;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
public class GifDecoderTests public class GifDecoderTests
@ -21,31 +21,43 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
private const PixelTypes TestPixelTypes = PixelTypes.Rgba32 | PixelTypes.RgbaVector | PixelTypes.Argb32; private const PixelTypes TestPixelTypes = PixelTypes.Rgba32 | PixelTypes.RgbaVector | PixelTypes.Argb32;
public static readonly string[] MultiFrameTestFiles = public static readonly string[] MultiFrameTestFiles =
{ {
TestImages.Gif.Giphy, TestImages.Gif.Kumin TestImages.Gif.Giphy, TestImages.Gif.Kumin
}; };
public static readonly string[] BasicVerificationFiles = public static readonly string[] BasicVerificationFiles =
{ {
TestImages.Gif.Cheers, TestImages.Gif.Cheers,
TestImages.Gif.Rings, TestImages.Gif.Rings,
// previously DecodeBadApplicationExtensionLength: // previously DecodeBadApplicationExtensionLength:
TestImages.Gif.Issues.BadAppExtLength, TestImages.Gif.Issues.BadAppExtLength,
TestImages.Gif.Issues.BadAppExtLength_2, TestImages.Gif.Issues.BadAppExtLength_2,
// previously DecodeBadDescriptorDimensionsLength: // previously DecodeBadDescriptorDimensionsLength:
TestImages.Gif.Issues.BadDescriptorWidth TestImages.Gif.Issues.BadDescriptorWidth
}; };
public static readonly TheoryData<string, int, int, PixelResolutionUnit> RatioFiles =
new TheoryData<string, int, int, PixelResolutionUnit>
{
{ TestImages.Gif.Rings, (int)ImageMetaData.DefaultHorizontalResolution, (int)ImageMetaData.DefaultVerticalResolution , PixelResolutionUnit.PixelsPerInch},
{ TestImages.Gif.Ratio1x4, 1, 4 , PixelResolutionUnit.AspectRatio},
{ TestImages.Gif.Ratio4x1, 4, 1, PixelResolutionUnit.AspectRatio }
};
private static readonly Dictionary<string, int> BasicVerificationFrameCount = private static readonly Dictionary<string, int> BasicVerificationFrameCount =
new Dictionary<string, int> new Dictionary<string, int>
{ {
[TestImages.Gif.Cheers] = 93, [TestImages.Gif.Cheers] = 93,
[TestImages.Gif.Issues.BadDescriptorWidth] = 36, [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)]
@ -59,6 +71,40 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
} }
} }
[Theory]
[MemberData(nameof(RatioFiles))]
public void Decode_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
{
var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false))
{
var decoder = new GifDecoder();
using (Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, stream))
{
ImageMetaData meta = image.MetaData;
Assert.Equal(xResolution, meta.HorizontalResolution);
Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(resolutionUnit, meta.ResolutionUnits);
}
}
}
[Theory]
[MemberData(nameof(RatioFiles))]
public void Identify_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
{
var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false))
{
var decoder = new GifDecoder();
IImageInfo image = decoder.Identify(Configuration.Default, stream);
ImageMetaData meta = image.MetaData;
Assert.Equal(xResolution, meta.HorizontalResolution);
Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(resolutionUnit, meta.ResolutionUnits);
}
}
[Theory] [Theory]
[WithFile(TestImages.Gif.Trans, TestPixelTypes)] [WithFile(TestImages.Gif.Trans, TestPixelTypes)]
public void GifDecoder_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider) public void GifDecoder_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
@ -88,7 +134,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
image.CompareFirstFrameToReferenceOutput(ImageComparer.Exact, provider); image.CompareFirstFrameToReferenceOutput(ImageComparer.Exact, provider);
} }
} }
[Fact] [Fact]
public void Decode_IgnoreMetadataIsFalse_CommentsAreRead() public void Decode_IgnoreMetadataIsFalse_CommentsAreRead()
{ {
@ -169,7 +215,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
[InlineData(TestImages.Gif.Trans, 8)] [InlineData(TestImages.Gif.Trans, 8)]
public void DetectPixelSize(string imagePath, int expectedPixelSize) public void DetectPixelSize(string imagePath, int expectedPixelSize)
{ {
TestFile testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false)) using (var stream = new MemoryStream(testFile.Bytes, false))
{ {
Assert.Equal(expectedPixelSize, Image.Identify(stream)?.PixelType?.BitsPerPixel); Assert.Equal(expectedPixelSize, Image.Identify(stream)?.PixelType?.BitsPerPixel);

36
tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs

@ -17,6 +17,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
private const PixelTypes TestPixelTypes = PixelTypes.Rgba32 | PixelTypes.RgbaVector | PixelTypes.Argb32; private const PixelTypes TestPixelTypes = PixelTypes.Rgba32 | PixelTypes.RgbaVector | PixelTypes.Argb32;
private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.001F); private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.001F);
public static readonly TheoryData<string, int, int, PixelResolutionUnit> RatioFiles =
new TheoryData<string, int, int, PixelResolutionUnit>
{
{ TestImages.Gif.Rings, (int)ImageMetaData.DefaultHorizontalResolution, (int)ImageMetaData.DefaultVerticalResolution , PixelResolutionUnit.PixelsPerInch},
{ TestImages.Gif.Ratio1x4, 1, 4 , PixelResolutionUnit.AspectRatio},
{ TestImages.Gif.Ratio4x1, 4, 1, PixelResolutionUnit.AspectRatio }
};
[Theory] [Theory]
[WithTestPatternImages(100, 100, TestPixelTypes)] [WithTestPatternImages(100, 100, TestPixelTypes)]
public void EncodeGeneratedPatterns<TPixel>(TestImageProvider<TPixel> provider) public void EncodeGeneratedPatterns<TPixel>(TestImageProvider<TPixel> provider)
@ -43,6 +51,34 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
} }
} }
[Theory]
[MemberData(nameof(RatioFiles))]
public void Encode_PreserveRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
{
var options = new GifEncoder()
{
IgnoreMetadata = false
};
var testFile = TestFile.Create(imagePath);
using (Image<Rgba32> input = testFile.CreateImage())
{
using (var memStream = new MemoryStream())
{
input.Save(memStream, options);
memStream.Position = 0;
using (var output = Image.Load<Rgba32>(memStream))
{
ImageMetaData meta = output.MetaData;
Assert.Equal(xResolution, meta.HorizontalResolution);
Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(resolutionUnit, meta.ResolutionUnits);
}
}
}
}
[Fact] [Fact]
public void Encode_IgnoreMetadataIsFalse_CommentsAreWritten() public void Encode_IgnoreMetadataIsFalse_CommentsAreWritten()
{ {

5
tests/ImageSharp.Tests/TestImages.cs

@ -176,6 +176,9 @@ namespace SixLabors.ImageSharp.Tests
public const string Cheers = "Gif/cheers.gif"; public const string Cheers = "Gif/cheers.gif";
public const string Trans = "Gif/trans.gif"; public const string Trans = "Gif/trans.gif";
public const string Kumin = "Gif/kumin.gif"; public const string Kumin = "Gif/kumin.gif";
public const string Ratio4x1 = "Gif/base_4x1.gif";
public const string Ratio1x4 = "Gif/base_1x4.gif";
public class Issues public class Issues
{ {
@ -184,7 +187,7 @@ namespace SixLabors.ImageSharp.Tests
public const string BadDescriptorWidth = "Gif/issues/issue403_baddescriptorwidth.gif"; public const string BadDescriptorWidth = "Gif/issues/issue403_baddescriptorwidth.gif";
} }
public static readonly string[] All = { Rings, Giphy, Cheers, Trans, Kumin }; public static readonly string[] All = { Rings, Giphy, Cheers, Trans, Kumin, Ratio4x1, Ratio1x4 };
} }
} }
} }

3
tests/Images/Input/Gif/base_1x4.gif

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:56e2409223f140145db2ba405f5562451dc0fa9d4274830fb02bd78d42552162
size 1620

3
tests/Images/Input/Gif/base_4x1.gif

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:24fd7e9dd3c6516ddab7336a30efc5901754b6d43a9d989c6fbb3e06d8944c80
size 1620
Loading…
Cancel
Save