From e238cc60f6c920d87b77039d8c2276691e2b99f8 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 13 Oct 2022 13:03:10 +1000 Subject: [PATCH] Define base classes and options --- src/ImageSharp/Formats/IEncoderOptions.cs | 33 +++++++++++++++++ .../Formats/ImageDecoderUtilities.cs | 4 +- src/ImageSharp/Formats/ImageEncoder.cs | 37 +++++++++++++++++++ .../Formats/ImageEncoderUtilities.cs | 6 +-- 4 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 src/ImageSharp/Formats/IEncoderOptions.cs create mode 100644 src/ImageSharp/Formats/ImageEncoder.cs diff --git a/src/ImageSharp/Formats/IEncoderOptions.cs b/src/ImageSharp/Formats/IEncoderOptions.cs new file mode 100644 index 0000000000..c05ba23bb5 --- /dev/null +++ b/src/ImageSharp/Formats/IEncoderOptions.cs @@ -0,0 +1,33 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using SixLabors.ImageSharp.Processing.Processors.Quantization; + +namespace SixLabors.ImageSharp.Formats; + +/// +/// Defines the contract for basic encoder options. +/// +public interface IEncoderOptions +{ + /// + /// Gets or sets a value indicating whether to ignore decoded metadata when encoding. + /// + bool SkipMetadata { get; set; } +} + +/// +/// Defines the contract for encoder options that allow color palette generation via quantization. +/// +public interface IQuantizingEncoderOptions : IEncoderOptions +{ + /// + /// Gets or sets the quantizer used to generate the color palette. + /// + IQuantizer Quantizer { get; set; } + + /// + /// Gets or sets the used for quantization when building a global color palette. + /// + IPixelSamplingStrategy GlobalPixelSamplingStrategy { get; set; } +} diff --git a/src/ImageSharp/Formats/ImageDecoderUtilities.cs b/src/ImageSharp/Formats/ImageDecoderUtilities.cs index 42f15cf976..c05e0d83cb 100644 --- a/src/ImageSharp/Formats/ImageDecoderUtilities.cs +++ b/src/ImageSharp/Formats/ImageDecoderUtilities.cs @@ -58,7 +58,7 @@ internal static class ImageDecoderUtilities Stream stream, CancellationToken cancellationToken) { - using var bufferedReadStream = new BufferedReadStream(configuration, stream); + using BufferedReadStream bufferedReadStream = new(configuration, stream); try { @@ -86,7 +86,7 @@ internal static class ImageDecoderUtilities CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { - using var bufferedReadStream = new BufferedReadStream(configuration, stream); + using BufferedReadStream bufferedReadStream = new(configuration, stream); try { diff --git a/src/ImageSharp/Formats/ImageEncoder.cs b/src/ImageSharp/Formats/ImageEncoder.cs new file mode 100644 index 0000000000..ef4365e380 --- /dev/null +++ b/src/ImageSharp/Formats/ImageEncoder.cs @@ -0,0 +1,37 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; +using SixLabors.ImageSharp.Processing.Processors.Quantization; + +namespace SixLabors.ImageSharp.Formats; + +/// +/// The base class for all image encoders. +/// +public abstract class ImageEncoder : IImageEncoder, IEncoderOptions +{ + /// + public bool SkipMetadata { get; set; } + + /// + public abstract void Encode(Image image, Stream stream) + where TPixel : unmanaged, IPixel; + + /// + public abstract Task EncodeAsync(Image image, Stream stream, CancellationToken cancellationToken) + where TPixel : unmanaged, IPixel; +} + +/// +/// The base class for all image encoders that allow color palette generation via quantization. +/// +public abstract class QuantizingImageEncoder : ImageEncoder, IQuantizingEncoderOptions +{ + /// + public IQuantizer Quantizer { get; set; } = KnownQuantizers.Octree; + + /// + public IPixelSamplingStrategy GlobalPixelSamplingStrategy { get; set; } = new DefaultPixelSamplingStrategy(); +} diff --git a/src/ImageSharp/Formats/ImageEncoderUtilities.cs b/src/ImageSharp/Formats/ImageEncoderUtilities.cs index 94f74ea253..665431c952 100644 --- a/src/ImageSharp/Formats/ImageEncoderUtilities.cs +++ b/src/ImageSharp/Formats/ImageEncoderUtilities.cs @@ -1,4 +1,4 @@ -// Copyright (c) Six Labors. +// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. using SixLabors.ImageSharp.Advanced; @@ -22,11 +22,11 @@ internal static class ImageEncoderUtilities } else { - using var ms = new MemoryStream(); + using MemoryStream ms = new(); await DoEncodeAsync(ms); ms.Position = 0; await ms.CopyToAsync(stream, configuration.StreamProcessingBufferSize, cancellationToken) - .ConfigureAwait(false); + .ConfigureAwait(false); } Task DoEncodeAsync(Stream innerStream)