diff --git a/src/ImageSharp/Formats/Gif/GifEncoder.cs b/src/ImageSharp/Formats/Gif/GifEncoder.cs
index b6441db102..7490490a12 100644
--- a/src/ImageSharp/Formats/Gif/GifEncoder.cs
+++ b/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;
///
/// Image encoder for writing image data to a stream in gif format.
///
-public sealed class GifEncoder : IImageEncoder, IGifEncoderOptions
+public sealed class GifEncoder : QuantizingImageEncoder
{
- ///
- /// Gets or sets the quantizer for reducing the color count.
- /// Defaults to the
- ///
- public IQuantizer Quantizer { get; set; } = KnownQuantizers.Octree;
-
///
/// Gets or sets the color table mode: Global or local.
///
public GifColorTableMode? ColorTableMode { get; set; }
- ///
- /// Gets or sets the used for quantization
- /// when building a global color table in case of .
- ///
- public IPixelSamplingStrategy GlobalPixelSamplingStrategy { get; set; } = new DefaultPixelSamplingStrategy();
-
///
- public void Encode(Image image, Stream stream)
- where TPixel : unmanaged, IPixel
+ public override void Encode(Image image, Stream stream)
{
- var encoder = new GifEncoderCore(image.GetConfiguration(), this);
+ GifEncoderCore encoder = new(image.GetConfiguration(), this);
encoder.Encode(image, stream);
}
///
- public Task EncodeAsync(Image image, Stream stream, CancellationToken cancellationToken)
- where TPixel : unmanaged, IPixel
+ public override Task EncodeAsync(Image image, Stream stream, CancellationToken cancellationToken)
{
- var encoder = new GifEncoderCore(image.GetConfiguration(), this);
+ GifEncoderCore encoder = new(image.GetConfiguration(), this);
return encoder.EncodeAsync(image, stream, cancellationToken);
}
}
diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs
index cfd4ba36a6..f1dfd26b42 100644
--- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs
+++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs
@@ -33,6 +33,11 @@ internal sealed class GifEncoderCore : IImageEncoderInternals
///
private readonly byte[] buffer = new byte[20];
+ ///
+ /// Whether to skip metadata during encode.
+ ///
+ private readonly bool skipMetadata;
+
///
/// The quantizer used to generate the color palette.
///
@@ -57,14 +62,15 @@ internal sealed class GifEncoderCore : IImageEncoderInternals
/// Initializes a new instance of the class.
///
/// The configuration which allows altering default behaviour or extending the library.
- /// The options for the encoder.
- public GifEncoderCore(Configuration configuration, IGifEncoderOptions options)
+ /// The gif encoder.
+ 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;
}
///
@@ -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)
{
diff --git a/src/ImageSharp/Formats/Gif/IGifEncoderOptions.cs b/src/ImageSharp/Formats/Gif/IGifEncoderOptions.cs
deleted file mode 100644
index 1ed68a9177..0000000000
--- a/src/ImageSharp/Formats/Gif/IGifEncoderOptions.cs
+++ /dev/null
@@ -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;
-
-///
-/// The configuration options used for encoding gifs.
-///
-internal interface IGifEncoderOptions
-{
- ///
- /// Gets the quantizer used to generate the color palette.
- ///
- IQuantizer Quantizer { get; }
-
- ///
- /// Gets the color table mode: Global or local.
- ///
- GifColorTableMode? ColorTableMode { get; }
-
- ///
- /// Gets the used for quantization when building a global color table.
- ///
- IPixelSamplingStrategy GlobalPixelSamplingStrategy { get; }
-}