Browse Source

Convert PbmEncoder. Make options immutable

pull/2269/head
James Jackson-South 4 years ago
parent
commit
8c90f78b47
  1. 8
      src/ImageSharp/Formats/Bmp/BmpEncoder.cs
  2. 2
      src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
  3. 4
      src/ImageSharp/Formats/Gif/GifEncoder.cs
  4. 2
      src/ImageSharp/Formats/Gif/GifEncoderCore.cs
  5. 12
      src/ImageSharp/Formats/IEncoderOptions.cs
  6. 6
      src/ImageSharp/Formats/ImageEncoder.cs
  7. 28
      src/ImageSharp/Formats/Jpeg/JpegEncoder.cs
  8. 25
      src/ImageSharp/Formats/Pbm/PbmEncoder.cs
  9. 16
      src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs

8
src/ImageSharp/Formats/Bmp/BmpEncoder.cs

@ -11,17 +11,17 @@ namespace SixLabors.ImageSharp.Formats.Bmp;
public sealed class BmpEncoder : QuantizingImageEncoder
{
/// <summary>
/// Gets or sets the number of bits per pixel.
/// Gets the number of bits per pixel.
/// </summary>
public BmpBitsPerPixel? BitsPerPixel { get; set; }
public BmpBitsPerPixel? BitsPerPixel { get; init; }
/// <summary>
/// Gets or sets a value indicating whether the encoder should support transparency.
/// Gets a value indicating whether the encoder should support transparency.
/// Note: Transparency support only works together with 32 bits per pixel. This option will
/// change the default behavior of the encoder of writing a bitmap version 3 info header with no compression.
/// Instead a bitmap version 4 info header will be written with the BITFIELDS compression.
/// </summary>
public bool SupportTransparency { get; set; }
public bool SupportTransparency { get; init; }
/// <inheritdoc/>
public override void Encode<TPixel>(Image<TPixel> image, Stream stream)

2
src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

@ -99,7 +99,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals
/// <summary>
/// Initializes a new instance of the <see cref="BmpEncoderCore"/> class.
/// </summary>
/// <param name="encoder">The Bmp encoder.</param>
/// <param name="encoder">The encoder with options.</param>
/// <param name="memoryAllocator">The memory manager.</param>
public BmpEncoderCore(BmpEncoder encoder, MemoryAllocator memoryAllocator)
{

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

@ -11,9 +11,9 @@ namespace SixLabors.ImageSharp.Formats.Gif;
public sealed class GifEncoder : QuantizingImageEncoder
{
/// <summary>
/// Gets or sets the color table mode: Global or local.
/// Gets the color table mode: Global or local.
/// </summary>
public GifColorTableMode? ColorTableMode { get; set; }
public GifColorTableMode? ColorTableMode { get; init; }
/// <inheritdoc/>
public override void Encode<TPixel>(Image<TPixel> image, Stream stream)

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

@ -62,7 +62,7 @@ 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="encoder">The gif encoder.</param>
/// <param name="encoder">The encoder with options.</param>
public GifEncoderCore(Configuration configuration, GifEncoder encoder)
{
this.configuration = configuration;

12
src/ImageSharp/Formats/IEncoderOptions.cs

@ -11,9 +11,9 @@ namespace SixLabors.ImageSharp.Formats;
public interface IEncoderOptions
{
/// <summary>
/// Gets or sets a value indicating whether to ignore decoded metadata when encoding.
/// Gets a value indicating whether to ignore decoded metadata when encoding.
/// </summary>
bool SkipMetadata { get; set; }
bool SkipMetadata { get; init; }
}
/// <summary>
@ -22,12 +22,12 @@ public interface IEncoderOptions
public interface IQuantizingEncoderOptions : IEncoderOptions
{
/// <summary>
/// Gets or sets the quantizer used to generate the color palette.
/// Gets the quantizer used to generate the color palette.
/// </summary>
IQuantizer Quantizer { get; set; }
IQuantizer Quantizer { get; init; }
/// <summary>
/// Gets or sets the <see cref="IPixelSamplingStrategy"/> used for quantization when building a global color palette.
/// Gets the <see cref="IPixelSamplingStrategy"/> used for quantization when building a global color palette.
/// </summary>
IPixelSamplingStrategy GlobalPixelSamplingStrategy { get; set; }
IPixelSamplingStrategy GlobalPixelSamplingStrategy { get; init; }
}

6
src/ImageSharp/Formats/ImageEncoder.cs

@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Formats;
public abstract class ImageEncoder : IImageEncoder, IEncoderOptions
{
/// <inheritdoc/>
public bool SkipMetadata { get; set; }
public bool SkipMetadata { get; init; }
/// <inheritdoc/>
public abstract void Encode<TPixel>(Image<TPixel> image, Stream stream)
@ -30,8 +30,8 @@ public abstract class ImageEncoder : IImageEncoder, IEncoderOptions
public abstract class QuantizingImageEncoder : ImageEncoder, IQuantizingEncoderOptions
{
/// <inheritdoc/>
public IQuantizer Quantizer { get; set; } = KnownQuantizers.Octree;
public IQuantizer Quantizer { get; init; } = KnownQuantizers.Octree;
/// <inheritdoc/>
public IPixelSamplingStrategy GlobalPixelSamplingStrategy { get; set; } = new DefaultPixelSamplingStrategy();
public IPixelSamplingStrategy GlobalPixelSamplingStrategy { get; init; } = new DefaultPixelSamplingStrategy();
}

28
src/ImageSharp/Formats/Jpeg/JpegEncoder.cs

@ -14,7 +14,7 @@ public sealed class JpegEncoder : ImageEncoder
private int? quality;
/// <summary>
/// Gets or sets the quality, that will be used to encode the image. Quality
/// Gets the quality, that will be used to encode the image. Quality
/// index must be between 0 and 100 (compression from max to min).
/// Defaults to <value>75</value>.
/// </summary>
@ -22,7 +22,7 @@ public sealed class JpegEncoder : ImageEncoder
public int? Quality
{
get => this.quality;
set
init
{
if (value is < 1 or > 100)
{
@ -34,39 +34,27 @@ public sealed class JpegEncoder : ImageEncoder
}
/// <summary>
/// Gets or sets the component encoding mode.
/// Gets the component encoding mode.
/// </summary>
/// <remarks>
/// Interleaved encoding mode encodes all color components in a single scan.
/// Non-interleaved encoding mode encodes each color component in a separate scan.
/// </remarks>
public bool? Interleaved { get; set; }
public bool? Interleaved { get; init; }
/// <summary>
/// Gets or sets jpeg color for encoding.
/// Gets the jpeg color for encoding.
/// </summary>
public JpegEncodingColor? ColorType { get; set; }
public JpegEncodingColor? ColorType { get; init; }
/// <summary>
/// Encodes the image to the specified stream from the <see cref="Image{TPixel}"/>.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="image">The <see cref="Image{TPixel}"/> to encode from.</param>
/// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param>
/// <inheritdoc/>
public override void Encode<TPixel>(Image<TPixel> image, Stream stream)
{
JpegEncoderCore encoder = new(this);
encoder.Encode(image, stream);
}
/// <summary>
/// Encodes the image to the specified stream from the <see cref="Image{TPixel}"/>.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="image">The <see cref="Image{TPixel}"/> to encode from.</param>
/// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
/// <inheritdoc/>
public override Task EncodeAsync<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
{
JpegEncoderCore encoder = new(this);

25
src/ImageSharp/Formats/Pbm/PbmEncoder.cs

@ -2,7 +2,6 @@
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Pbm;
@ -30,36 +29,34 @@ namespace SixLabors.ImageSharp.Formats.Pbm;
/// </para>
/// The specification of these images is found at <seealso href="http://netpbm.sourceforge.net/doc/pnm.html"/>.
/// </summary>
public sealed class PbmEncoder : IImageEncoder, IPbmEncoderOptions
public sealed class PbmEncoder : ImageEncoder
{
/// <summary>
/// Gets or sets the Encoding of the pixels.
/// Gets the encoding of the pixels.
/// </summary>
public PbmEncoding? Encoding { get; set; }
public PbmEncoding? Encoding { get; init; }
/// <summary>
/// Gets or sets the Color type of the resulting image.
/// Gets the Color type of the resulting image.
/// </summary>
public PbmColorType? ColorType { get; set; }
public PbmColorType? ColorType { get; init; }
/// <summary>
/// Gets or sets the data type of the pixels components.
/// Gets the Data Type of the pixel components.
/// </summary>
public PbmComponentType? ComponentType { get; set; }
public PbmComponentType? ComponentType { get; init; }
/// <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 PbmEncoderCore(image.GetConfiguration(), this);
PbmEncoderCore 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 PbmEncoderCore(image.GetConfiguration(), this);
PbmEncoderCore encoder = new(image.GetConfiguration(), this);
return encoder.EncodeAsync(image, stream, cancellationToken);
}
}

16
src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs

@ -22,9 +22,9 @@ internal sealed class PbmEncoderCore : IImageEncoderInternals
private Configuration configuration;
/// <summary>
/// The encoder options.
/// The encoder with options.
/// </summary>
private readonly IPbmEncoderOptions options;
private readonly PbmEncoder encoder;
/// <summary>
/// The encoding for the pixels.
@ -45,11 +45,11 @@ internal sealed class PbmEncoderCore : IImageEncoderInternals
/// Initializes a new instance of the <see cref="PbmEncoderCore"/> class.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="options">The encoder options.</param>
public PbmEncoderCore(Configuration configuration, IPbmEncoderOptions options)
/// <param name="encoder">The encoder with options.</param>
public PbmEncoderCore(Configuration configuration, PbmEncoder encoder)
{
this.configuration = configuration;
this.options = options;
this.encoder = encoder;
}
/// <summary>
@ -80,11 +80,11 @@ internal sealed class PbmEncoderCore : IImageEncoderInternals
{
this.configuration = image.GetConfiguration();
PbmMetadata metadata = image.Metadata.GetPbmMetadata();
this.encoding = this.options.Encoding ?? metadata.Encoding;
this.colorType = this.options.ColorType ?? metadata.ColorType;
this.encoding = this.encoder.Encoding ?? metadata.Encoding;
this.colorType = this.encoder.ColorType ?? metadata.ColorType;
if (this.colorType != PbmColorType.BlackAndWhite)
{
this.componentType = this.options.ComponentType ?? metadata.ComponentType;
this.componentType = this.encoder.ComponentType ?? metadata.ComponentType;
}
else
{

Loading…
Cancel
Save