Browse Source

Implemented the IDecoderOptions inside the gif decoder.

af/merge-core
Dirk Lemstra 9 years ago
committed by Dirk Lemstra
parent
commit
32481be496
  1. 2
      src/ImageSharp.Formats.Gif/GifDecoder.cs
  2. 38
      src/ImageSharp.Formats.Gif/GifDecoderCore.cs
  3. 16
      src/ImageSharp/Formats/DecoderOptions.cs
  4. 2
      src/ImageSharp/Formats/IDecoderOptions.cs
  5. 46
      tests/ImageSharp.Tests/Formats/Gif/GifDecoderCoreTests.cs
  6. 4
      tests/ImageSharp.Tests/TestImages/Formats/Gif/rings.gif

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

@ -17,7 +17,7 @@ namespace ImageSharp.Formats
public void Decode<TColor>(Image<TColor> image, Stream stream, IDecoderOptions options)
where TColor : struct, IPixel<TColor>
{
new GifDecoderCore<TColor>().Decode(image, stream);
new GifDecoderCore<TColor>(options).Decode(image, stream);
}
}
}

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

@ -8,6 +8,7 @@ namespace ImageSharp.Formats
using System;
using System.Buffers;
using System.IO;
using System.Text;
/// <summary>
/// Performs the gif decoding operation.
@ -21,6 +22,11 @@ namespace ImageSharp.Formats
/// </summary>
private readonly byte[] buffer = new byte[16];
/// <summary>
/// The decoder options.
/// </summary>
private readonly IDecoderOptions options;
/// <summary>
/// The image to decode the information to.
/// </summary>
@ -61,6 +67,15 @@ namespace ImageSharp.Formats
/// </summary>
private GifGraphicsControlExtension graphicsControlExtension;
/// <summary>
/// Initializes a new instance of the <see cref="GifDecoderCore{TColor}"/> class.
/// </summary>
/// <param name="options">The decoder options.</param>
public GifDecoderCore(IDecoderOptions options)
{
this.options = options ?? DecoderOptions.Default;
}
/// <summary>
/// Decodes the stream to the image.
/// </summary>
@ -225,25 +240,32 @@ namespace ImageSharp.Formats
/// </summary>
private void ReadComments()
{
int flag;
int length;
while ((flag = this.currentStream.ReadByte()) != 0)
while ((length = this.currentStream.ReadByte()) != 0)
{
if (flag > GifConstants.MaxCommentLength)
if (length > GifConstants.MaxCommentLength)
{
throw new ImageFormatException($"Gif comment length '{length}' exceeds max '{GifConstants.MaxCommentLength}'");
}
if (this.options.IgnoreMetadata)
{
throw new ImageFormatException($"Gif comment length '{flag}' exceeds max '{GifConstants.MaxCommentLength}'");
this.currentStream.Seek(length, SeekOrigin.Current);
continue;
}
byte[] flagBuffer = ArrayPool<byte>.Shared.Rent(flag);
byte[] commentsBuffer = ArrayPool<byte>.Shared.Rent(length);
try
{
this.currentStream.Read(flagBuffer, 0, flag);
this.decodedImage.MetaData.Properties.Add(new ImageProperty("Comments", BitConverter.ToString(flagBuffer, 0, flag)));
this.currentStream.Read(commentsBuffer, 0, length);
string comments = Encoding.GetEncoding("ASCII").GetString(commentsBuffer, 0, length);
this.decodedImage.MetaData.Properties.Add(new ImageProperty("Comments", comments));
}
finally
{
ArrayPool<byte>.Shared.Return(flagBuffer);
ArrayPool<byte>.Shared.Return(commentsBuffer);
}
}
}

16
src/ImageSharp/Formats/DecoderOptions.cs

@ -3,13 +3,27 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Formats
namespace ImageSharp
{
/// <summary>
/// Encapsulates the shared decoder options.
/// </summary>
public class DecoderOptions : IDecoderOptions
{
/// <summary>
/// Gets the default decoder options
/// </summary>
public static DecoderOptions Default
{
get
{
return new DecoderOptions()
{
IgnoreMetadata = false
};
}
}
/// <summary>
/// Gets or sets a value indicating whether the metadata should be ignored when the image is being decoded.
/// </summary>

2
src/ImageSharp/Formats/IDecoderOptions.cs

@ -3,7 +3,7 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Formats
namespace ImageSharp
{
/// <summary>
/// Encapsulates the shared decoder options.

46
tests/ImageSharp.Tests/Formats/Gif/GifDecoderCoreTests.cs

@ -0,0 +1,46 @@
// <copyright file="GifDecoderCoreTests.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using Xunit;
public class GifDecoderCoreTests
{
[Fact]
public void Decode_IgnoreMetadataIsFalse_CommentsAreRead()
{
var options = new DecoderOptions()
{
IgnoreMetadata = false
};
TestFile testFile = TestFile.Create(TestImages.Gif.Rings);
using (Image image = new Image(testFile.FilePath, options))
{
Assert.Equal(1, image.MetaData.Properties.Count);
Assert.Equal("Comments", image.MetaData.Properties[0].Name);
Assert.Equal("ImageSharp", image.MetaData.Properties[0].Value);
}
}
[Fact]
public void Decode_IgnoreMetadataIsTrue_CommentsAreIgnored()
{
var options = new DecoderOptions()
{
IgnoreMetadata = true
};
TestFile testFile = TestFile.Create(TestImages.Gif.Rings);
using (Image image = new Image(testFile.FilePath, options))
{
Assert.Equal(0, image.MetaData.Properties.Count);
}
}
}
}

4
tests/ImageSharp.Tests/TestImages/Formats/Gif/rings.gif

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:716448da88152225767c024aac498f5b7562b6e8391907cefc9d03dba40050fd
size 53435
oid sha256:a7b9b5f5056a8d90134d72b462bf37675ab37aa2551ffeae0072037a0a27ad74
size 53457

Loading…
Cancel
Save