Browse Source

Add tests for discontiguous buffers for gif

af/octree-no-pixelmap
Brian Popow 6 years ago
parent
commit
7217552ebf
  1. 3
      src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
  2. 16
      src/ImageSharp/Formats/Gif/GifDecoder.cs
  3. 7
      src/ImageSharp/Formats/Gif/GifDecoderCore.cs
  4. 3
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  5. 3
      src/ImageSharp/Formats/Tga/TgaDecoderCore.cs
  6. 40
      tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs
  7. 10
      tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs

3
src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

@ -114,6 +114,9 @@ namespace SixLabors.ImageSharp.Formats.Bmp
this.options = options; this.options = options;
} }
/// <summary>
/// Gets the dimensions of the image.
/// </summary>
public Size Dimensions => new Size(this.infoHeader.Width, this.infoHeader.Height); public Size Dimensions => new Size(this.infoHeader.Width, this.infoHeader.Height);
/// <summary> /// <summary>

16
src/ImageSharp/Formats/Gif/GifDecoder.cs

@ -1,7 +1,9 @@
// Copyright (c) Six Labors and contributors. // Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System;
using System.IO; using System.IO;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -27,7 +29,19 @@ namespace SixLabors.ImageSharp.Formats.Gif
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
var decoder = new GifDecoderCore(configuration, this); var decoder = new GifDecoderCore(configuration, this);
return decoder.Decode<TPixel>(stream);
try
{
return decoder.Decode<TPixel>(stream);
}
catch (InvalidMemoryOperationException ex)
{
Size dims = decoder.Dimensions;
// TODO: use InvalidImageContentException here, if we decide to define it
// https://github.com/SixLabors/ImageSharp/issues/1110
throw new ImageFormatException($"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {dims.Width}x{dims.Height}.", ex);
}
} }
/// <inheritdoc/> /// <inheritdoc/>

7
src/ImageSharp/Formats/Gif/GifDecoderCore.cs

@ -86,10 +86,15 @@ namespace SixLabors.ImageSharp.Formats.Gif
public bool IgnoreMetadata { get; internal set; } public bool IgnoreMetadata { get; internal set; }
/// <summary> /// <summary>
/// Gets the decoding mode for multi-frame images /// Gets the decoding mode for multi-frame images.
/// </summary> /// </summary>
public FrameDecodingMode DecodingMode { get; } public FrameDecodingMode DecodingMode { get; }
/// <summary>
/// Gets the dimensions of the image.
/// </summary>
public Size Dimensions => new Size(this.imageDescriptor.Width, this.imageDescriptor.Height);
private MemoryAllocator MemoryAllocator => this.configuration.MemoryAllocator; private MemoryAllocator MemoryAllocator => this.configuration.MemoryAllocator;
/// <summary> /// <summary>

3
src/ImageSharp/Formats/Png/PngDecoderCore.cs

@ -132,6 +132,9 @@ namespace SixLabors.ImageSharp.Formats.Png
this.ignoreMetadata = options.IgnoreMetadata; this.ignoreMetadata = options.IgnoreMetadata;
} }
/// <summary>
/// Gets the dimensions of the image.
/// </summary>
public Size Dimensions => new Size(this.header.Width, this.header.Height); public Size Dimensions => new Size(this.header.Width, this.header.Height);
/// <summary> /// <summary>

3
src/ImageSharp/Formats/Tga/TgaDecoderCore.cs

@ -61,6 +61,9 @@ namespace SixLabors.ImageSharp.Formats.Tga
this.options = options; this.options = options;
} }
/// <summary>
/// Gets the dimensions of the image.
/// </summary>
public Size Dimensions => new Size(this.fileHeader.Width, this.fileHeader.Height); public Size Dimensions => new Size(this.fileHeader.Width, this.fileHeader.Height);
/// <summary> /// <summary>

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

@ -4,10 +4,14 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using Microsoft.DotNet.RemoteExecutor;
using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats.Gif; using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Formats.Tga;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests.TestUtilities;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using Xunit; using Xunit;
@ -163,5 +167,41 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
} }
} }
} }
[Theory]
[WithFile(TestImages.Gif.Giphy, PixelTypes.Rgba32)]
[WithFile(TestImages.Gif.Kumin, PixelTypes.Rgba32)]
public void GifDecoder_DegenerateMemoryRequest_ShouldTranslateTo_ImageFormatException<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
provider.LimitAllocatorBufferCapacity().InPixels(10);
ImageFormatException ex = Assert.Throws<ImageFormatException>(provider.GetImage);
Assert.IsType<InvalidMemoryOperationException>(ex.InnerException);
}
[Theory]
[WithFile(TestImages.Gif.Giphy, PixelTypes.Rgba32)]
[WithFile(TestImages.Gif.Kumin, PixelTypes.Rgba32)]
public void GifDecoder_CanDecode_WithLimitedAllocatorBufferCapacity<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
static void RunTest(string providerDump, string nonContiguousBuffersStr)
{
TestImageProvider<TPixel> provider = BasicSerializer.Deserialize<TestImageProvider<TPixel>>(providerDump);
provider.LimitAllocatorBufferCapacity().InPixels(100);
using Image<TPixel> image = provider.GetImage(new GifDecoder());
image.DebugSave(provider);
image.CompareToOriginal(provider);
}
string providerDump = BasicSerializer.Serialize(provider);
RemoteExecutor.Invoke(
RunTest,
providerDump,
"Disco")
.Dispose();
}
} }
} }

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

@ -26,10 +26,16 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
}; };
[Theory] [Theory]
[WithTestPatternImages(100, 100, TestPixelTypes)] [WithTestPatternImages(100, 100, TestPixelTypes, false)]
public void EncodeGeneratedPatterns<TPixel>(TestImageProvider<TPixel> provider) [WithTestPatternImages(100, 100, TestPixelTypes, false)]
public void EncodeGeneratedPatterns<TPixel>(TestImageProvider<TPixel> provider, bool limitAllocationBuffer)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
if (limitAllocationBuffer)
{
provider.LimitAllocatorBufferCapacity().InPixels(100);
}
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
{ {
var encoder = new GifEncoder var encoder = new GifEncoder

Loading…
Cancel
Save