From 395505ac66338d9a648fb2095d3ac21b5032e3ab Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Sat, 18 Feb 2017 13:16:49 +0100 Subject: [PATCH] Added options for the decoder. --- src/ImageSharp.Formats.Bmp/BmpDecoder.cs | 2 +- src/ImageSharp.Formats.Gif/GifDecoder.cs | 2 +- src/ImageSharp.Formats.Jpeg/JpegDecoder.cs | 2 +- src/ImageSharp.Formats.Png/PngDecoder.cs | 9 ++---- src/ImageSharp/Formats/DecoderOptions.cs | 18 ++++++++++++ src/ImageSharp/Formats/IDecoderOptions.cs | 18 ++++++++++++ src/ImageSharp/Formats/IImageDecoder.cs | 3 +- src/ImageSharp/Image.cs | 23 +++++++++++---- src/ImageSharp/Image/Image{TColor}.cs | 33 ++++++++++++++-------- 9 files changed, 82 insertions(+), 28 deletions(-) create mode 100644 src/ImageSharp/Formats/DecoderOptions.cs create mode 100644 src/ImageSharp/Formats/IDecoderOptions.cs diff --git a/src/ImageSharp.Formats.Bmp/BmpDecoder.cs b/src/ImageSharp.Formats.Bmp/BmpDecoder.cs index 4b7da38afc..9f490a3a9b 100644 --- a/src/ImageSharp.Formats.Bmp/BmpDecoder.cs +++ b/src/ImageSharp.Formats.Bmp/BmpDecoder.cs @@ -26,7 +26,7 @@ namespace ImageSharp.Formats public class BmpDecoder : IImageDecoder { /// - public void Decode(Image image, Stream stream) + public void Decode(Image image, Stream stream, IDecoderOptions options) where TColor : struct, IPixel { Guard.NotNull(image, "image"); diff --git a/src/ImageSharp.Formats.Gif/GifDecoder.cs b/src/ImageSharp.Formats.Gif/GifDecoder.cs index 76530dc504..aea96bb65f 100644 --- a/src/ImageSharp.Formats.Gif/GifDecoder.cs +++ b/src/ImageSharp.Formats.Gif/GifDecoder.cs @@ -14,7 +14,7 @@ namespace ImageSharp.Formats public class GifDecoder : IImageDecoder { /// - public void Decode(Image image, Stream stream) + public void Decode(Image image, Stream stream, IDecoderOptions options) where TColor : struct, IPixel { new GifDecoderCore().Decode(image, stream); diff --git a/src/ImageSharp.Formats.Jpeg/JpegDecoder.cs b/src/ImageSharp.Formats.Jpeg/JpegDecoder.cs index 435ae51cfd..a399bb6b38 100644 --- a/src/ImageSharp.Formats.Jpeg/JpegDecoder.cs +++ b/src/ImageSharp.Formats.Jpeg/JpegDecoder.cs @@ -14,7 +14,7 @@ namespace ImageSharp.Formats public class JpegDecoder : IImageDecoder { /// - public void Decode(Image image, Stream stream) + public void Decode(Image image, Stream stream, IDecoderOptions options) where TColor : struct, IPixel { Guard.NotNull(image, "image"); diff --git a/src/ImageSharp.Formats.Png/PngDecoder.cs b/src/ImageSharp.Formats.Png/PngDecoder.cs index 088bea5919..9aab6673a9 100644 --- a/src/ImageSharp.Formats.Png/PngDecoder.cs +++ b/src/ImageSharp.Formats.Png/PngDecoder.cs @@ -30,13 +30,8 @@ namespace ImageSharp.Formats /// public class PngDecoder : IImageDecoder { - /// - /// Decodes the image from the specified stream to the . - /// - /// The pixel format. - /// The to decode to. - /// The containing image data. - public void Decode(Image image, Stream stream) + /// + public void Decode(Image image, Stream stream, IDecoderOptions options) where TColor : struct, IPixel { new PngDecoderCore().Decode(image, stream); diff --git a/src/ImageSharp/Formats/DecoderOptions.cs b/src/ImageSharp/Formats/DecoderOptions.cs new file mode 100644 index 0000000000..f25e33d71f --- /dev/null +++ b/src/ImageSharp/Formats/DecoderOptions.cs @@ -0,0 +1,18 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Formats +{ + /// + /// Encapsulates the shared decoder options. + /// + public class DecoderOptions : IDecoderOptions + { + /// + /// Gets or sets a value indicating whether the metadata should be ignored when the image is being decoded. + /// + public bool IgnoreMetadata { get; set; } + } +} diff --git a/src/ImageSharp/Formats/IDecoderOptions.cs b/src/ImageSharp/Formats/IDecoderOptions.cs new file mode 100644 index 0000000000..460de3eb28 --- /dev/null +++ b/src/ImageSharp/Formats/IDecoderOptions.cs @@ -0,0 +1,18 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Formats +{ + /// + /// Encapsulates the shared decoder options. + /// + public interface IDecoderOptions + { + /// + /// Gets a value indicating whether the metadata should be ignored when the image is being decoded. + /// + bool IgnoreMetadata { get; } + } +} diff --git a/src/ImageSharp/Formats/IImageDecoder.cs b/src/ImageSharp/Formats/IImageDecoder.cs index 28bda7837e..df98870ddd 100644 --- a/src/ImageSharp/Formats/IImageDecoder.cs +++ b/src/ImageSharp/Formats/IImageDecoder.cs @@ -19,7 +19,8 @@ namespace ImageSharp.Formats /// The pixel format. /// The to decode to. /// The containing image data. - void Decode(Image image, Stream stream) + /// The options for the decoder. + void Decode(Image image, Stream stream, IDecoderOptions options) where TColor : struct, IPixel; } } diff --git a/src/ImageSharp/Image.cs b/src/ImageSharp/Image.cs index bdb6b49a92..b1c1252ab4 100644 --- a/src/ImageSharp/Image.cs +++ b/src/ImageSharp/Image.cs @@ -8,6 +8,8 @@ namespace ImageSharp using System.Diagnostics; using System.IO; + using Formats; + /// /// Represents an image. Each pixel is a made up four 8-bit components red, green, blue, and alpha /// packed into a single unsigned integer value. @@ -35,12 +37,15 @@ namespace ImageSharp /// /// The stream containing image information. /// + /// + /// The options for the decoder. + /// /// /// The configuration providing initialization code which allows extending the library. /// /// Thrown if the is null. - public Image(Stream stream, Configuration configuration = null) - : base(stream, configuration) + public Image(Stream stream, IDecoderOptions options = null, Configuration configuration = null) + : base(stream, options, configuration) { } @@ -51,12 +56,15 @@ namespace ImageSharp /// /// A file path to read image information. /// + /// + /// The options for the decoder. + /// /// /// The configuration providing initialization code which allows extending the library. /// /// Thrown if the is null. - public Image(string filePath, Configuration configuration = null) - : base(filePath, configuration) + public Image(string filePath, IDecoderOptions options = null, Configuration configuration = null) + : base(filePath, options, configuration) { } #endif @@ -67,12 +75,15 @@ namespace ImageSharp /// /// The byte array containing image information. /// + /// + /// The options for the decoder. + /// /// /// The configuration providing initialization code which allows extending the library. /// /// Thrown if the is null. - public Image(byte[] bytes, Configuration configuration = null) - : base(bytes, configuration) + public Image(byte[] bytes, IDecoderOptions options = null, Configuration configuration = null) + : base(bytes, options, configuration) { } diff --git a/src/ImageSharp/Image/Image{TColor}.cs b/src/ImageSharp/Image/Image{TColor}.cs index bbd839d168..9e4ecba2f5 100644 --- a/src/ImageSharp/Image/Image{TColor}.cs +++ b/src/ImageSharp/Image/Image{TColor}.cs @@ -52,15 +52,18 @@ namespace ImageSharp /// /// The stream containing image information. /// + /// + /// The options for the decoder. + /// /// /// The configuration providing initialization code which allows extending the library. /// /// Thrown if the is null. - public Image(Stream stream, Configuration configuration = null) + public Image(Stream stream, IDecoderOptions options = null, Configuration configuration = null) : base(configuration) { Guard.NotNull(stream, nameof(stream)); - this.Load(stream); + this.Load(stream, options); } #if !NETSTANDARD1_1 @@ -70,17 +73,20 @@ namespace ImageSharp /// /// The file containing image information. /// + /// + /// The options for the decoder. + /// /// /// The configuration providing initialization code which allows extending the library. /// /// Thrown if the is null. - public Image(string filePath, Configuration configuration = null) + public Image(string filePath, IDecoderOptions options = null, Configuration configuration = null) : base(configuration) { Guard.NotNull(filePath, nameof(filePath)); using (var fs = File.OpenRead(filePath)) { - this.Load(fs); + this.Load(fs, options); } } #endif @@ -91,18 +97,21 @@ namespace ImageSharp /// /// The byte array containing image information. /// + /// + /// The options for the decoder. + /// /// /// The configuration providing initialization code which allows extending the library. /// /// Thrown if the is null. - public Image(byte[] bytes, Configuration configuration = null) + public Image(byte[] bytes, IDecoderOptions options = null, Configuration configuration = null) : base(configuration) { Guard.NotNull(bytes, nameof(bytes)); using (MemoryStream stream = new MemoryStream(bytes, false)) { - this.Load(stream); + this.Load(stream, options); } } @@ -397,10 +406,11 @@ namespace ImageSharp /// Loads the image from the given stream. /// /// The stream containing image information. + /// The options for the decoder. /// /// Thrown if the stream is not readable nor seekable. /// - private void Load(Stream stream) + private void Load(Stream stream, IDecoderOptions options) { if (!this.Configuration.ImageFormats.Any()) { @@ -414,7 +424,7 @@ namespace ImageSharp if (stream.CanSeek) { - if (this.Decode(stream)) + if (this.Decode(stream, options)) { return; } @@ -427,7 +437,7 @@ namespace ImageSharp stream.CopyTo(ms); ms.Position = 0; - if (this.Decode(ms)) + if (this.Decode(ms, options)) { return; } @@ -449,10 +459,11 @@ namespace ImageSharp /// Decodes the image stream to the current image. /// /// The stream. + /// The options for the decoder. /// /// The . /// - private bool Decode(Stream stream) + private bool Decode(Stream stream, IDecoderOptions options) { int maxHeaderSize = this.Configuration.MaxHeaderSize; if (maxHeaderSize <= 0) @@ -479,7 +490,7 @@ namespace ImageSharp return false; } - format.Decoder.Decode(this, stream); + format.Decoder.Decode(this, stream, options); this.CurrentImageFormat = format; return true; }