Browse Source

Migrate Gif encoder

pull/2269/head
James Jackson-South 4 years ago
parent
commit
52bac6f61e
  1. 27
      src/ImageSharp/Formats/Gif/GifEncoder.cs
  2. 29
      src/ImageSharp/Formats/Gif/GifEncoderCore.cs
  3. 27
      src/ImageSharp/Formats/Gif/IGifEncoderOptions.cs

27
src/ImageSharp/Formats/Gif/GifEncoder.cs

@ -2,47 +2,30 @@
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Quantization;
namespace SixLabors.ImageSharp.Formats.Gif;
/// <summary>
/// Image encoder for writing image data to a stream in gif format.
/// </summary>
public sealed class GifEncoder : IImageEncoder, IGifEncoderOptions
public sealed class GifEncoder : QuantizingImageEncoder
{
/// <summary>
/// Gets or sets the quantizer for reducing the color count.
/// Defaults to the <see cref="OctreeQuantizer"/>
/// </summary>
public IQuantizer Quantizer { get; set; } = KnownQuantizers.Octree;
/// <summary>
/// Gets or sets the color table mode: Global or local.
/// </summary>
public GifColorTableMode? ColorTableMode { get; set; }
/// <summary>
/// Gets or sets the <see cref="IPixelSamplingStrategy"/> used for quantization
/// when building a global color table in case of <see cref="GifColorTableMode.Global"/>.
/// </summary>
public IPixelSamplingStrategy GlobalPixelSamplingStrategy { get; set; } = new DefaultPixelSamplingStrategy();
/// <inheritdoc/>
public void Encode<TPixel>(Image<TPixel> image, Stream stream)
where TPixel : unmanaged, IPixel<TPixel>
public override void Encode<TPixel>(Image<TPixel> image, Stream stream)
{
var encoder = new GifEncoderCore(image.GetConfiguration(), this);
GifEncoderCore encoder = new(image.GetConfiguration(), this);
encoder.Encode(image, stream);
}
/// <inheritdoc/>
public Task EncodeAsync<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel>
public override Task EncodeAsync<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
{
var encoder = new GifEncoderCore(image.GetConfiguration(), this);
GifEncoderCore encoder = new(image.GetConfiguration(), this);
return encoder.EncodeAsync(image, stream, cancellationToken);
}
}

29
src/ImageSharp/Formats/Gif/GifEncoderCore.cs

@ -33,6 +33,11 @@ internal sealed class GifEncoderCore : IImageEncoderInternals
/// </summary>
private readonly byte[] buffer = new byte[20];
/// <summary>
/// Whether to skip metadata during encode.
/// </summary>
private readonly bool skipMetadata;
/// <summary>
/// The quantizer used to generate the color palette.
/// </summary>
@ -57,14 +62,15 @@ internal sealed class GifEncoderCore : IImageEncoderInternals
/// Initializes a new instance of the <see cref="GifEncoderCore"/> class.
/// </summary>
/// <param name="configuration">The configuration which allows altering default behaviour or extending the library.</param>
/// <param name="options">The options for the encoder.</param>
public GifEncoderCore(Configuration configuration, IGifEncoderOptions options)
/// <param name="encoder">The gif encoder.</param>
public GifEncoderCore(Configuration configuration, GifEncoder encoder)
{
this.configuration = configuration;
this.memoryAllocator = configuration.MemoryAllocator;
this.quantizer = options.Quantizer;
this.colorTableMode = options.ColorTableMode;
this.pixelSamplingStrategy = options.GlobalPixelSamplingStrategy;
this.skipMetadata = encoder.SkipMetadata;
this.quantizer = encoder.Quantizer;
this.colorTableMode = encoder.ColorTableMode;
this.pixelSamplingStrategy = encoder.GlobalPixelSamplingStrategy;
}
/// <summary>
@ -116,12 +122,15 @@ internal sealed class GifEncoderCore : IImageEncoderInternals
this.WriteColorTable(quantized, stream);
}
// Write the comments.
this.WriteComments(gifMetadata, stream);
if (!this.skipMetadata)
{
// Write the comments.
this.WriteComments(gifMetadata, stream);
// Write application extensions.
XmpProfile xmpProfile = image.Metadata.XmpProfile ?? image.Frames.RootFrame.Metadata.XmpProfile;
this.WriteApplicationExtensions(stream, image.Frames.Count, gifMetadata.RepeatCount, xmpProfile);
// Write application extensions.
XmpProfile xmpProfile = image.Metadata.XmpProfile ?? image.Frames.RootFrame.Metadata.XmpProfile;
this.WriteApplicationExtensions(stream, image.Frames.Count, gifMetadata.RepeatCount, xmpProfile);
}
if (useGlobalTable)
{

27
src/ImageSharp/Formats/Gif/IGifEncoderOptions.cs

@ -1,27 +0,0 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Processing.Processors.Quantization;
namespace SixLabors.ImageSharp.Formats.Gif;
/// <summary>
/// The configuration options used for encoding gifs.
/// </summary>
internal interface IGifEncoderOptions
{
/// <summary>
/// Gets the quantizer used to generate the color palette.
/// </summary>
IQuantizer Quantizer { get; }
/// <summary>
/// Gets the color table mode: Global or local.
/// </summary>
GifColorTableMode? ColorTableMode { get; }
/// <summary>
/// Gets the <see cref="IPixelSamplingStrategy"/> used for quantization when building a global color table.
/// </summary>
IPixelSamplingStrategy GlobalPixelSamplingStrategy { get; }
}
Loading…
Cancel
Save