// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Processors.Quantization;
namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
{
///
/// A Png encoder that uses the ImageSharp core encoder but the default configuration.
/// This allows encoding under environments with restricted memory.
///
public sealed class ImageSharpPngEncoderWithDefaultConfiguration : IImageEncoder, IPngEncoderOptions
{
///
public PngBitDepth? BitDepth { get; set; }
///
public PngColorType? ColorType { get; set; }
///
public PngFilterMethod? FilterMethod { get; set; }
///
public PngCompressionLevel CompressionLevel { get; set; } = PngCompressionLevel.DefaultCompression;
///
public int TextCompressionThreshold { get; set; } = 1024;
///
public float? Gamma { get; set; }
///
public IQuantizer Quantizer { get; set; }
///
public byte Threshold { get; set; } = byte.MaxValue;
///
public PngInterlaceMode? InterlaceMethod { get; set; }
///
public PngChunkFilter? ChunkFilter { get; set; }
///
public bool IgnoreMetadata { get; set; }
///
public PngTransparentColorMode TransparentColorMode { get; set; }
///
/// Encodes the image to the specified stream from the .
///
/// The pixel format.
/// The to encode from.
/// The to encode the image data to.
public void Encode(Image image, Stream stream)
where TPixel : unmanaged, IPixel
{
Configuration configuration = Configuration.Default;
MemoryAllocator allocator = configuration.MemoryAllocator;
using var encoder = new PngEncoderCore(allocator, configuration, new PngEncoderOptions(this));
encoder.Encode(image, stream);
}
///
/// Encodes the image to the specified stream from the .
///
/// The pixel format.
/// The to encode from.
/// The to encode the image data to.
/// The token to monitor for cancellation requests.
/// A representing the asynchronous operation.
public async Task EncodeAsync(Image image, Stream stream, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel
{
Configuration configuration = Configuration.Default;
MemoryAllocator allocator = configuration.MemoryAllocator;
// The introduction of a local variable that refers to an object the implements
// IDisposable means you must use async/await, where the compiler generates the
// state machine and a continuation.
using var encoder = new PngEncoderCore(allocator, configuration, new PngEncoderOptions(this));
await encoder.EncodeAsync(image, stream, cancellationToken).ConfigureAwait(false);
}
}
}