mirror of https://github.com/SixLabors/ImageSharp
committed by
Dirk Lemstra
5 changed files with 172 additions and 3 deletions
@ -0,0 +1,20 @@ |
|||
// <copyright file="IPngDecoderOptions.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 png decoder options.
|
|||
/// </summary>
|
|||
public interface IPngDecoderOptions : IDecoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the encoding that should be used when reading text chunks.
|
|||
/// </summary>
|
|||
Encoding TextEncoding { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,62 @@ |
|||
// <copyright file="PngDecoderOptions.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 png decoder options.
|
|||
/// </summary>
|
|||
public sealed class PngDecoderOptions : DecoderOptions, IPngDecoderOptions |
|||
{ |
|||
private static readonly Encoding DefaultEncoding = Encoding.GetEncoding("ASCII"); |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="PngDecoderOptions"/> class.
|
|||
/// </summary>
|
|||
public PngDecoderOptions() |
|||
{ |
|||
this.InitializeWithDefaults(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="PngDecoderOptions"/> class.
|
|||
/// </summary>
|
|||
/// <param name="options">The options for the decoder.</param>
|
|||
private PngDecoderOptions(IDecoderOptions options) |
|||
: base(options) |
|||
{ |
|||
this.InitializeWithDefaults(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the encoding that should be used when reading text chunks.
|
|||
/// </summary>
|
|||
public Encoding TextEncoding { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Converts the options to a <see cref="IPngDecoderOptions"/> 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 IPngDecoderOptions Create(IDecoderOptions options) |
|||
{ |
|||
IPngDecoderOptions pngOptions = options as IPngDecoderOptions; |
|||
if (pngOptions != null) |
|||
{ |
|||
return pngOptions; |
|||
} |
|||
|
|||
return new PngDecoderOptions(options); |
|||
} |
|||
|
|||
private void InitializeWithDefaults() |
|||
{ |
|||
this.TextEncoding = DefaultEncoding; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,66 @@ |
|||
// <copyright file="JpegDecoderCoreTests.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 System.Text; |
|||
using Xunit; |
|||
|
|||
using ImageSharp.Formats; |
|||
|
|||
public class PngDecoderCoreTests |
|||
{ |
|||
[Fact] |
|||
public void Decode_IgnoreMetadataIsFalse_TextChunckIsRead() |
|||
{ |
|||
var options = new PngDecoderOptions() |
|||
{ |
|||
IgnoreMetadata = false |
|||
}; |
|||
|
|||
TestFile testFile = TestFile.Create(TestImages.Png.Blur); |
|||
|
|||
using (Image image = new Image(testFile.FilePath, options)) |
|||
{ |
|||
Assert.Equal(1, image.MetaData.Properties.Count); |
|||
Assert.Equal("Software", image.MetaData.Properties[0].Name); |
|||
Assert.Equal("paint.net 4.0.6", image.MetaData.Properties[0].Value); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public void Decode_IgnoreMetadataIsTrue_TextChunksAreIgnored() |
|||
{ |
|||
var options = new PngDecoderOptions() |
|||
{ |
|||
IgnoreMetadata = true |
|||
}; |
|||
|
|||
TestFile testFile = TestFile.Create(TestImages.Png.Blur); |
|||
|
|||
using (Image image = new Image(testFile.FilePath, options)) |
|||
{ |
|||
Assert.Equal(0, image.MetaData.Properties.Count); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public void Decode_TextEncodingSetToUnicode_TextIsReadWithCorrectEncoding() |
|||
{ |
|||
var options = new PngDecoderOptions() |
|||
{ |
|||
TextEncoding = Encoding.Unicode |
|||
}; |
|||
|
|||
TestFile testFile = TestFile.Create(TestImages.Png.Blur); |
|||
|
|||
using (Image image = new Image(testFile.FilePath, options)) |
|||
{ |
|||
Assert.Equal(1, image.MetaData.Properties.Count); |
|||
Assert.Equal("潓瑦慷敲", image.MetaData.Properties[0].Name); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue