From eb5e8a83aff0ee0c9a426ca2a6c8841f7ca41eb5 Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Sun, 19 Feb 2017 14:51:52 +0100 Subject: [PATCH] Added options for the encoder. --- src/ImageSharp.Formats.Bmp/BmpEncoder.cs | 2 +- src/ImageSharp.Formats.Gif/GifEncoder.cs | 2 +- src/ImageSharp.Formats.Jpeg/JpegEncoder.cs | 2 +- src/ImageSharp.Formats.Png/PngEncoder.cs | 2 +- src/ImageSharp/Formats/EncoderOptions.cs | 46 ++++++++++++++++++++++ src/ImageSharp/Formats/IEncoderOptions.cs | 18 +++++++++ src/ImageSharp/Formats/IImageEncoder.cs | 3 +- src/ImageSharp/Image/Image{TColor}.cs | 16 ++++---- 8 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 src/ImageSharp/Formats/EncoderOptions.cs create mode 100644 src/ImageSharp/Formats/IEncoderOptions.cs diff --git a/src/ImageSharp.Formats.Bmp/BmpEncoder.cs b/src/ImageSharp.Formats.Bmp/BmpEncoder.cs index 6edaf178b..ea1342272 100644 --- a/src/ImageSharp.Formats.Bmp/BmpEncoder.cs +++ b/src/ImageSharp.Formats.Bmp/BmpEncoder.cs @@ -20,7 +20,7 @@ namespace ImageSharp.Formats public BmpBitsPerPixel BitsPerPixel { get; set; } = BmpBitsPerPixel.Pixel24; /// - public void Encode(Image image, Stream stream) + public void Encode(Image image, Stream stream, IEncoderOptions options) where TColor : struct, IPixel { BmpEncoderCore encoder = new BmpEncoderCore(); diff --git a/src/ImageSharp.Formats.Gif/GifEncoder.cs b/src/ImageSharp.Formats.Gif/GifEncoder.cs index de7e03322..402d2d09a 100644 --- a/src/ImageSharp.Formats.Gif/GifEncoder.cs +++ b/src/ImageSharp.Formats.Gif/GifEncoder.cs @@ -32,7 +32,7 @@ namespace ImageSharp.Formats public IQuantizer Quantizer { get; set; } /// - public void Encode(Image image, Stream stream) + public void Encode(Image image, Stream stream, IEncoderOptions options) where TColor : struct, IPixel { GifEncoderCore encoder = new GifEncoderCore diff --git a/src/ImageSharp.Formats.Jpeg/JpegEncoder.cs b/src/ImageSharp.Formats.Jpeg/JpegEncoder.cs index 07d9b24cd..46972cd93 100644 --- a/src/ImageSharp.Formats.Jpeg/JpegEncoder.cs +++ b/src/ImageSharp.Formats.Jpeg/JpegEncoder.cs @@ -61,7 +61,7 @@ namespace ImageSharp.Formats } /// - public void Encode(Image image, Stream stream) + public void Encode(Image image, Stream stream, IEncoderOptions options) where TColor : struct, IPixel { // Ensure that quality can be set but has a fallback. diff --git a/src/ImageSharp.Formats.Png/PngEncoder.cs b/src/ImageSharp.Formats.Png/PngEncoder.cs index ba790f4ae..8d9c4bc08 100644 --- a/src/ImageSharp.Formats.Png/PngEncoder.cs +++ b/src/ImageSharp.Formats.Png/PngEncoder.cs @@ -56,7 +56,7 @@ namespace ImageSharp.Formats public bool WriteGamma { get; set; } /// - public void Encode(Image image, Stream stream) + public void Encode(Image image, Stream stream, IEncoderOptions options) where TColor : struct, IPixel { PngEncoderCore encoder = new PngEncoderCore diff --git a/src/ImageSharp/Formats/EncoderOptions.cs b/src/ImageSharp/Formats/EncoderOptions.cs new file mode 100644 index 000000000..1f92927f1 --- /dev/null +++ b/src/ImageSharp/Formats/EncoderOptions.cs @@ -0,0 +1,46 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp +{ + /// + /// Encapsulates the shared encoder options. + /// + public class EncoderOptions : IEncoderOptions + { + /// + /// Initializes a new instance of the class. + /// + public EncoderOptions() + { + this.InitializeWithDefaults(); + } + + /// + /// Initializes a new instance of the class. + /// + /// The encoder options + protected EncoderOptions(IEncoderOptions options) + { + if (options == null) + { + this.InitializeWithDefaults(); + return; + } + + this.IgnoreMetadata = options.IgnoreMetadata; + } + + /// + /// Gets or sets a value indicating whether the metadata should be ignored when the image is being encoded. + /// + public bool IgnoreMetadata { get; set; } + + private void InitializeWithDefaults() + { + this.IgnoreMetadata = false; + } + } +} diff --git a/src/ImageSharp/Formats/IEncoderOptions.cs b/src/ImageSharp/Formats/IEncoderOptions.cs new file mode 100644 index 000000000..0fd3d1c43 --- /dev/null +++ b/src/ImageSharp/Formats/IEncoderOptions.cs @@ -0,0 +1,18 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp +{ + /// + /// Encapsulates the shared encoder options. + /// + public interface IEncoderOptions + { + /// + /// Gets a value indicating whether the metadata should be ignored when the image is being encoded. + /// + bool IgnoreMetadata { get; } + } +} diff --git a/src/ImageSharp/Formats/IImageEncoder.cs b/src/ImageSharp/Formats/IImageEncoder.cs index 0ba56477a..918f0d273 100644 --- a/src/ImageSharp/Formats/IImageEncoder.cs +++ b/src/ImageSharp/Formats/IImageEncoder.cs @@ -19,7 +19,8 @@ namespace ImageSharp.Formats /// The pixel format. /// The to encode from. /// The to encode the image data to. - void Encode(Image image, Stream stream) + /// The options for the encoder. + void Encode(Image image, Stream stream, IEncoderOptions options) where TColor : struct, IPixel; } } diff --git a/src/ImageSharp/Image/Image{TColor}.cs b/src/ImageSharp/Image/Image{TColor}.cs index 9e4ecba2f..d8890f5fa 100644 --- a/src/ImageSharp/Image/Image{TColor}.cs +++ b/src/ImageSharp/Image/Image{TColor}.cs @@ -206,12 +206,13 @@ namespace ImageSharp /// Saves the image to the given stream using the currently loaded image format. /// /// The stream to save the image to. + /// The options for the encoder. /// Thrown if the stream is null. /// The - public Image Save(Stream stream) + public Image Save(Stream stream, IEncoderOptions options = null) { Guard.NotNull(stream, nameof(stream)); - this.CurrentImageFormat.Encoder.Encode(this, stream); + this.CurrentImageFormat.Encoder.Encode(this, stream, options); return this; } @@ -220,13 +221,13 @@ namespace ImageSharp /// /// The stream to save the image to. /// The format to save the image as. - /// Thrown if the stream or format is null. + /// The options for the encoder. /// The - public Image Save(Stream stream, IImageFormat format) + public Image Save(Stream stream, IImageFormat format, IEncoderOptions options = null) { Guard.NotNull(stream, nameof(stream)); Guard.NotNull(format, nameof(format)); - format.Encoder.Encode(this, stream); + format.Encoder.Encode(this, stream, options); return this; } @@ -235,15 +236,16 @@ namespace ImageSharp /// /// The stream to save the image to. /// The encoder to save the image with. + /// The options for the encoder. /// Thrown if the stream or encoder is null. /// /// The . /// - public Image Save(Stream stream, IImageEncoder encoder) + public Image Save(Stream stream, IImageEncoder encoder, IEncoderOptions options = null) { Guard.NotNull(stream, nameof(stream)); Guard.NotNull(encoder, nameof(encoder)); - encoder.Encode(this, stream); + encoder.Encode(this, stream, options); // Reset to the start of the stream. if (stream.CanSeek)