mirror of https://github.com/SixLabors/ImageSharp
committed by
Dirk Lemstra
9 changed files with 264 additions and 48 deletions
@ -0,0 +1,71 @@ |
|||||
|
// <copyright file="GifEncoderOptions.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; |
||||
|
|
||||
|
using Quantizers; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Encapsulates the options for the <see cref="GifEncoder"/>.
|
||||
|
/// </summary>
|
||||
|
public sealed class GifEncoderOptions : EncoderOptions, IGifEncoderOptions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="GifEncoderOptions"/> class.
|
||||
|
/// </summary>
|
||||
|
public GifEncoderOptions() |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="GifEncoderOptions"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="options">The options for the encoder.</param>
|
||||
|
private GifEncoderOptions(IEncoderOptions options) |
||||
|
: base(options) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets the encoding that should be used when writing comments.
|
||||
|
/// </summary>
|
||||
|
public Encoding TextEncoding { get; set; } = GifConstants.DefaultEncoding; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets the quality of output for images.
|
||||
|
/// </summary>
|
||||
|
/// <remarks>For gifs the value ranges from 1 to 256.</remarks>
|
||||
|
public int Quality { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets the transparency threshold.
|
||||
|
/// </summary>
|
||||
|
public byte Threshold { get; set; } = 128; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets the quantizer for reducing the color count.
|
||||
|
/// </summary>
|
||||
|
public IQuantizer Quantizer { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Converts the options to a <see cref="IGifEncoderOptions"/> instance with a
|
||||
|
/// cast or by creating a new instance with the specfied options.
|
||||
|
/// </summary>
|
||||
|
/// <param name="options">The options for the encoder.</param>
|
||||
|
/// <returns>The options for the <see cref="GifEncoder"/>.</returns>
|
||||
|
internal static IGifEncoderOptions Create(IEncoderOptions options) |
||||
|
{ |
||||
|
IGifEncoderOptions gifOptions = options as IGifEncoderOptions; |
||||
|
if (gifOptions != null) |
||||
|
{ |
||||
|
return gifOptions; |
||||
|
} |
||||
|
|
||||
|
return new GifEncoderOptions(options); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,38 @@ |
|||||
|
// <copyright file="IGifEncoderOptions.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; |
||||
|
|
||||
|
using Quantizers; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Encapsulates the options for the <see cref="GifEncoder"/>.
|
||||
|
/// </summary>
|
||||
|
public interface IGifEncoderOptions : IEncoderOptions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Gets the encoding that should be used when writing comments.
|
||||
|
/// </summary>
|
||||
|
Encoding TextEncoding { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the quality of output for images.
|
||||
|
/// </summary>
|
||||
|
/// <remarks>For gifs the value ranges from 1 to 256.</remarks>
|
||||
|
int Quality { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the transparency threshold.
|
||||
|
/// </summary>
|
||||
|
byte Threshold { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the quantizer for reducing the color count.
|
||||
|
/// </summary>
|
||||
|
IQuantizer Quantizer { get; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,67 @@ |
|||||
|
// <copyright file="GifEncoderCoreTests.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.IO; |
||||
|
using Xunit; |
||||
|
|
||||
|
using ImageSharp.Formats; |
||||
|
|
||||
|
public class GifEncoderCoreTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Encode_IgnoreMetadataIsFalse_CommentsAreWritten() |
||||
|
{ |
||||
|
var options = new EncoderOptions() |
||||
|
{ |
||||
|
IgnoreMetadata = false |
||||
|
}; |
||||
|
|
||||
|
TestFile testFile = TestFile.Create(TestImages.Gif.Rings); |
||||
|
|
||||
|
using (Image input = testFile.CreateImage()) |
||||
|
{ |
||||
|
using (MemoryStream memStream = new MemoryStream()) |
||||
|
{ |
||||
|
input.Save(memStream, new GifFormat(), options); |
||||
|
|
||||
|
memStream.Position = 0; |
||||
|
using (Image output = new Image(memStream)) |
||||
|
{ |
||||
|
Assert.Equal(1, output.MetaData.Properties.Count); |
||||
|
Assert.Equal("Comments", output.MetaData.Properties[0].Name); |
||||
|
Assert.Equal("ImageSharp", output.MetaData.Properties[0].Value); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Encode_IgnoreMetadataIsTrue_CommentsAreNotWritten() |
||||
|
{ |
||||
|
var options = new GifEncoderOptions() |
||||
|
{ |
||||
|
IgnoreMetadata = true |
||||
|
}; |
||||
|
|
||||
|
TestFile testFile = TestFile.Create(TestImages.Gif.Rings); |
||||
|
|
||||
|
using (Image input = testFile.CreateImage()) |
||||
|
{ |
||||
|
using (MemoryStream memStream = new MemoryStream()) |
||||
|
{ |
||||
|
input.SaveAsGif(memStream, options); |
||||
|
|
||||
|
memStream.Position = 0; |
||||
|
using (Image output = new Image(memStream)) |
||||
|
{ |
||||
|
Assert.Equal(0, output.MetaData.Properties.Count); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue