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 public sealed class BmpEncoder : QuantizingImageEncoder
{ {
/// <summary> /// <summary>
/// Gets or sets the number of bits per pixel. /// Gets the number of bits per pixel.
/// </summary> /// </summary>
public BmpBitsPerPixel? BitsPerPixel { get; set; } public BmpBitsPerPixel? BitsPerPixel { get; init; }
/// <summary> /// <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 /// 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. /// 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. /// Instead a bitmap version 4 info header will be written with the BITFIELDS compression.
/// </summary> /// </summary>
public bool SupportTransparency { get; set; } public bool SupportTransparency { get; init; }
/// <inheritdoc/> /// <inheritdoc/>
public override void Encode<TPixel>(Image<TPixel> image, Stream stream) 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> /// <summary>
/// Initializes a new instance of the <see cref="BmpEncoderCore"/> class. /// Initializes a new instance of the <see cref="BmpEncoderCore"/> class.
/// </summary> /// </summary>
/// <param name="encoder">The Bmp encoder.</param> /// <param name="encoder">The encoder with options.</param>
/// <param name="memoryAllocator">The memory manager.</param> /// <param name="memoryAllocator">The memory manager.</param>
public BmpEncoderCore(BmpEncoder encoder, MemoryAllocator memoryAllocator) 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 public sealed class GifEncoder : QuantizingImageEncoder
{ {
/// <summary> /// <summary>
/// Gets or sets the color table mode: Global or local. /// Gets the color table mode: Global or local.
/// </summary> /// </summary>
public GifColorTableMode? ColorTableMode { get; set; } public GifColorTableMode? ColorTableMode { get; init; }
/// <inheritdoc/> /// <inheritdoc/>
public override void Encode<TPixel>(Image<TPixel> image, Stream stream) 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. /// Initializes a new instance of the <see cref="GifEncoderCore"/> class.
/// </summary> /// </summary>
/// <param name="configuration">The configuration which allows altering default behaviour or extending the library.</param> /// <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) public GifEncoderCore(Configuration configuration, GifEncoder encoder)
{ {
this.configuration = configuration; this.configuration = configuration;

12
src/ImageSharp/Formats/IEncoderOptions.cs

@ -11,9 +11,9 @@ namespace SixLabors.ImageSharp.Formats;
public interface IEncoderOptions public interface IEncoderOptions
{ {
/// <summary> /// <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> /// </summary>
bool SkipMetadata { get; set; } bool SkipMetadata { get; init; }
} }
/// <summary> /// <summary>
@ -22,12 +22,12 @@ public interface IEncoderOptions
public interface IQuantizingEncoderOptions : IEncoderOptions public interface IQuantizingEncoderOptions : IEncoderOptions
{ {
/// <summary> /// <summary>
/// Gets or sets the quantizer used to generate the color palette. /// Gets the quantizer used to generate the color palette.
/// </summary> /// </summary>
IQuantizer Quantizer { get; set; } IQuantizer Quantizer { get; init; }
/// <summary> /// <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> /// </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 public abstract class ImageEncoder : IImageEncoder, IEncoderOptions
{ {
/// <inheritdoc/> /// <inheritdoc/>
public bool SkipMetadata { get; set; } public bool SkipMetadata { get; init; }
/// <inheritdoc/> /// <inheritdoc/>
public abstract void Encode<TPixel>(Image<TPixel> image, Stream stream) 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 public abstract class QuantizingImageEncoder : ImageEncoder, IQuantizingEncoderOptions
{ {
/// <inheritdoc/> /// <inheritdoc/>
public IQuantizer Quantizer { get; set; } = KnownQuantizers.Octree; public IQuantizer Quantizer { get; init; } = KnownQuantizers.Octree;
/// <inheritdoc/> /// <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; private int? quality;
/// <summary> /// <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). /// index must be between 0 and 100 (compression from max to min).
/// Defaults to <value>75</value>. /// Defaults to <value>75</value>.
/// </summary> /// </summary>
@ -22,7 +22,7 @@ public sealed class JpegEncoder : ImageEncoder
public int? Quality public int? Quality
{ {
get => this.quality; get => this.quality;
set init
{ {
if (value is < 1 or > 100) if (value is < 1 or > 100)
{ {
@ -34,39 +34,27 @@ public sealed class JpegEncoder : ImageEncoder
} }
/// <summary> /// <summary>
/// Gets or sets the component encoding mode. /// Gets the component encoding mode.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// Interleaved encoding mode encodes all color components in a single scan. /// Interleaved encoding mode encodes all color components in a single scan.
/// Non-interleaved encoding mode encodes each color component in a separate scan. /// Non-interleaved encoding mode encodes each color component in a separate scan.
/// </remarks> /// </remarks>
public bool? Interleaved { get; set; } public bool? Interleaved { get; init; }
/// <summary> /// <summary>
/// Gets or sets jpeg color for encoding. /// Gets the jpeg color for encoding.
/// </summary> /// </summary>
public JpegEncodingColor? ColorType { get; set; } public JpegEncodingColor? ColorType { get; init; }
/// <summary> /// <inheritdoc/>
/// 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>
public override void Encode<TPixel>(Image<TPixel> image, Stream stream) public override void Encode<TPixel>(Image<TPixel> image, Stream stream)
{ {
JpegEncoderCore encoder = new(this); JpegEncoderCore encoder = new(this);
encoder.Encode(image, stream); encoder.Encode(image, stream);
} }
/// <summary> /// <inheritdoc/>
/// 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>
public override Task EncodeAsync<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken) public override Task EncodeAsync<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
{ {
JpegEncoderCore encoder = new(this); JpegEncoderCore encoder = new(this);

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

@ -2,7 +2,6 @@
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Pbm; namespace SixLabors.ImageSharp.Formats.Pbm;
@ -30,36 +29,34 @@ namespace SixLabors.ImageSharp.Formats.Pbm;
/// </para> /// </para>
/// The specification of these images is found at <seealso href="http://netpbm.sourceforge.net/doc/pnm.html"/>. /// The specification of these images is found at <seealso href="http://netpbm.sourceforge.net/doc/pnm.html"/>.
/// </summary> /// </summary>
public sealed class PbmEncoder : IImageEncoder, IPbmEncoderOptions public sealed class PbmEncoder : ImageEncoder
{ {
/// <summary> /// <summary>
/// Gets or sets the Encoding of the pixels. /// Gets the encoding of the pixels.
/// </summary> /// </summary>
public PbmEncoding? Encoding { get; set; } public PbmEncoding? Encoding { get; init; }
/// <summary> /// <summary>
/// Gets or sets the Color type of the resulting image. /// Gets the Color type of the resulting image.
/// </summary> /// </summary>
public PbmColorType? ColorType { get; set; } public PbmColorType? ColorType { get; init; }
/// <summary> /// <summary>
/// Gets or sets the data type of the pixels components. /// Gets the Data Type of the pixel components.
/// </summary> /// </summary>
public PbmComponentType? ComponentType { get; set; } public PbmComponentType? ComponentType { get; init; }
/// <inheritdoc/> /// <inheritdoc/>
public void Encode<TPixel>(Image<TPixel> image, Stream stream) public override void Encode<TPixel>(Image<TPixel> image, Stream stream)
where TPixel : unmanaged, IPixel<TPixel>
{ {
var encoder = new PbmEncoderCore(image.GetConfiguration(), this); PbmEncoderCore encoder = new(image.GetConfiguration(), this);
encoder.Encode(image, stream); encoder.Encode(image, stream);
} }
/// <inheritdoc/> /// <inheritdoc/>
public Task EncodeAsync<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken) public override Task EncodeAsync<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel>
{ {
var encoder = new PbmEncoderCore(image.GetConfiguration(), this); PbmEncoderCore encoder = new(image.GetConfiguration(), this);
return encoder.EncodeAsync(image, stream, cancellationToken); 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; private Configuration configuration;
/// <summary> /// <summary>
/// The encoder options. /// The encoder with options.
/// </summary> /// </summary>
private readonly IPbmEncoderOptions options; private readonly PbmEncoder encoder;
/// <summary> /// <summary>
/// The encoding for the pixels. /// The encoding for the pixels.
@ -45,11 +45,11 @@ internal sealed class PbmEncoderCore : IImageEncoderInternals
/// Initializes a new instance of the <see cref="PbmEncoderCore"/> class. /// Initializes a new instance of the <see cref="PbmEncoderCore"/> class.
/// </summary> /// </summary>
/// <param name="configuration">The configuration.</param> /// <param name="configuration">The configuration.</param>
/// <param name="options">The encoder options.</param> /// <param name="encoder">The encoder with options.</param>
public PbmEncoderCore(Configuration configuration, IPbmEncoderOptions options) public PbmEncoderCore(Configuration configuration, PbmEncoder encoder)
{ {
this.configuration = configuration; this.configuration = configuration;
this.options = options; this.encoder = encoder;
} }
/// <summary> /// <summary>
@ -80,11 +80,11 @@ internal sealed class PbmEncoderCore : IImageEncoderInternals
{ {
this.configuration = image.GetConfiguration(); this.configuration = image.GetConfiguration();
PbmMetadata metadata = image.Metadata.GetPbmMetadata(); PbmMetadata metadata = image.Metadata.GetPbmMetadata();
this.encoding = this.options.Encoding ?? metadata.Encoding; this.encoding = this.encoder.Encoding ?? metadata.Encoding;
this.colorType = this.options.ColorType ?? metadata.ColorType; this.colorType = this.encoder.ColorType ?? metadata.ColorType;
if (this.colorType != PbmColorType.BlackAndWhite) if (this.colorType != PbmColorType.BlackAndWhite)
{ {
this.componentType = this.options.ComponentType ?? metadata.ComponentType; this.componentType = this.encoder.ComponentType ?? metadata.ComponentType;
} }
else else
{ {

Loading…
Cancel
Save