diff --git a/src/ImageSharp.Formats.Gif/GifDecoder.cs b/src/ImageSharp.Formats.Gif/GifDecoder.cs index 42dbf4fc7..ce97689e9 100644 --- a/src/ImageSharp.Formats.Gif/GifDecoder.cs +++ b/src/ImageSharp.Formats.Gif/GifDecoder.cs @@ -17,7 +17,9 @@ namespace ImageSharp.Formats public void Decode(Image image, Stream stream, IDecoderOptions options) where TColor : struct, IPixel { - new GifDecoderCore(options).Decode(image, stream); + IGifDecoderOptions gifOptions = GifDecoderOptions.Create(options); + + new GifDecoderCore(gifOptions).Decode(image, stream); } } } diff --git a/src/ImageSharp.Formats.Gif/GifDecoderCore.cs b/src/ImageSharp.Formats.Gif/GifDecoderCore.cs index 2a0d53bde..3ab0e6ca8 100644 --- a/src/ImageSharp.Formats.Gif/GifDecoderCore.cs +++ b/src/ImageSharp.Formats.Gif/GifDecoderCore.cs @@ -25,7 +25,7 @@ namespace ImageSharp.Formats /// /// The decoder options. /// - private readonly IDecoderOptions options; + private readonly IGifDecoderOptions options; /// /// The image to decode the information to. @@ -71,9 +71,9 @@ namespace ImageSharp.Formats /// Initializes a new instance of the class. /// /// The decoder options. - public GifDecoderCore(IDecoderOptions options) + public GifDecoderCore(IGifDecoderOptions options) { - this.options = options ?? new DecoderOptions(); + this.options = options ?? new GifDecoderOptions(); } /// @@ -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 diff --git a/src/ImageSharp.Formats.Gif/GifDecoderOptions.cs b/src/ImageSharp.Formats.Gif/GifDecoderOptions.cs new file mode 100644 index 000000000..8bff3e38d --- /dev/null +++ b/src/ImageSharp.Formats.Gif/GifDecoderOptions.cs @@ -0,0 +1,62 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Formats +{ + using System.Text; + + /// + /// Encapsulates the gif decoder options. + /// + public sealed class GifDecoderOptions : DecoderOptions, IGifDecoderOptions + { + private static readonly Encoding DefaultEncoding = Encoding.GetEncoding("ASCII"); + + /// + /// Initializes a new instance of the class. + /// + public GifDecoderOptions() + { + this.InitializeWithDefaults(); + } + + /// + /// Initializes a new instance of the class. + /// + /// The options for the decoder. + private GifDecoderOptions(IDecoderOptions options) + : base(options) + { + this.InitializeWithDefaults(); + } + + /// + /// Gets the encoding that should be used when reading comments. + /// + public Encoding TextEncoding { get; set; } + + /// + /// Converts the options to a instance with a cast + /// or by creating a new instance with the specfied options. + /// + /// The options for the decoder. + /// The options for the png decoder. + 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; + } + } +} diff --git a/src/ImageSharp.Formats.Gif/IGifDecoderOptions.cs b/src/ImageSharp.Formats.Gif/IGifDecoderOptions.cs new file mode 100644 index 000000000..dd94a616f --- /dev/null +++ b/src/ImageSharp.Formats.Gif/IGifDecoderOptions.cs @@ -0,0 +1,20 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Formats +{ + using System.Text; + + /// + /// Encapsulates the gif decoder options. + /// + public interface IGifDecoderOptions : IDecoderOptions + { + /// + /// Gets the encoding that should be used when reading comments. + /// + Encoding TextEncoding { get; } + } +} diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderCoreTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderCoreTests.cs index 758c72400..3e3010cfc 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderCoreTests.cs +++ b/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); + } + } } }