Browse Source

Also allow specification of the text encoding inside the gif decoder.

pull/22/merge
Dirk Lemstra 9 years ago
committed by Dirk Lemstra
parent
commit
d35dc80374
  1. 4
      src/ImageSharp.Formats.Gif/GifDecoder.cs
  2. 8
      src/ImageSharp.Formats.Gif/GifDecoderCore.cs
  3. 62
      src/ImageSharp.Formats.Gif/GifDecoderOptions.cs
  4. 20
      src/ImageSharp.Formats.Gif/IGifDecoderOptions.cs
  5. 20
      tests/ImageSharp.Tests/Formats/Gif/GifDecoderCoreTests.cs

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

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

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

@ -25,7 +25,7 @@ namespace ImageSharp.Formats
/// <summary>
/// The decoder options.
/// </summary>
private readonly IDecoderOptions options;
private readonly IGifDecoderOptions options;
/// <summary>
/// The image to decode the information to.
@ -71,9 +71,9 @@ namespace ImageSharp.Formats
/// Initializes a new instance of the <see cref="GifDecoderCore{TColor}"/> class.
/// </summary>
/// <param name="options">The decoder options.</param>
public GifDecoderCore(IDecoderOptions options)
public GifDecoderCore(IGifDecoderOptions options)
{
this.options = options ?? new DecoderOptions();
this.options = options ?? new GifDecoderOptions();
}
/// <summary>
@ -260,7 +260,7 @@ namespace ImageSharp.Formats
try
{
this.currentStream.Read(commentsBuffer, 0, length);
string comments = Encoding.GetEncoding("ASCII").GetString(commentsBuffer, 0, length);
string comments = this.options.TextEncoding.GetString(commentsBuffer, 0, length);
this.decodedImage.MetaData.Properties.Add(new ImageProperty("Comments", comments));
}
finally

62
src/ImageSharp.Formats.Gif/GifDecoderOptions.cs

@ -0,0 +1,62 @@
// <copyright file="GifDecoderOptions.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Formats
{
using System.Text;
/// <summary>
/// Encapsulates the gif decoder options.
/// </summary>
public sealed class GifDecoderOptions : DecoderOptions, IGifDecoderOptions
{
private static readonly Encoding DefaultEncoding = Encoding.GetEncoding("ASCII");
/// <summary>
/// Initializes a new instance of the <see cref="GifDecoderOptions"/> class.
/// </summary>
public GifDecoderOptions()
{
this.InitializeWithDefaults();
}
/// <summary>
/// Initializes a new instance of the <see cref="GifDecoderOptions"/> class.
/// </summary>
/// <param name="options">The options for the decoder.</param>
private GifDecoderOptions(IDecoderOptions options)
: base(options)
{
this.InitializeWithDefaults();
}
/// <summary>
/// Gets the encoding that should be used when reading comments.
/// </summary>
public Encoding TextEncoding { get; set; }
/// <summary>
/// Converts the options to a <see cref="GifDecoderOptions"/> instance with a cast
/// or by creating a new instance with the specfied options.
/// </summary>
/// <param name="options">The options for the decoder.</param>
/// <returns>The options for the png decoder.</returns>
internal static IGifDecoderOptions Create(IDecoderOptions options)
{
IGifDecoderOptions gifOptions = options as IGifDecoderOptions;
if (gifOptions != null)
{
return gifOptions;
}
return new GifDecoderOptions(options);
}
private void InitializeWithDefaults()
{
this.TextEncoding = DefaultEncoding;
}
}
}

20
src/ImageSharp.Formats.Gif/IGifDecoderOptions.cs

@ -0,0 +1,20 @@
// <copyright file="IGifDecoderOptions.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Formats
{
using System.Text;
/// <summary>
/// Encapsulates the gif decoder options.
/// </summary>
public interface IGifDecoderOptions : IDecoderOptions
{
/// <summary>
/// Gets the encoding that should be used when reading comments.
/// </summary>
Encoding TextEncoding { get; }
}
}

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

@ -5,8 +5,11 @@
namespace ImageSharp.Tests
{
using System.Text;
using Xunit;
using ImageSharp.Formats;
public class GifDecoderCoreTests
{
[Fact]
@ -42,5 +45,22 @@ namespace ImageSharp.Tests
Assert.Equal(0, image.MetaData.Properties.Count);
}
}
[Fact]
public void Decode_TextEncodingSetToUnicode_TextIsReadWithCorrectEncoding()
{
var options = new GifDecoderOptions()
{
TextEncoding = Encoding.Unicode
};
TestFile testFile = TestFile.Create(TestImages.Gif.Rings);
using (Image image = new Image(testFile.FilePath, options))
{
Assert.Equal(1, image.MetaData.Properties.Count);
Assert.Equal("浉条卥慨灲", image.MetaData.Properties[0].Value);
}
}
}
}

Loading…
Cancel
Save