From c5f7f27e19b5ebfe73d54aba65b90b962fe2801b Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 1 Mar 2018 00:29:55 +1100 Subject: [PATCH] Use configuration to set ReadOrigin --- src/ImageSharp/Configuration.cs | 5 + src/ImageSharp/Image/Image.FromStream.cs | 101 +++---------------- tests/ImageSharp.Tests/ConfigurationTests.cs | 11 +- 3 files changed, 27 insertions(+), 90 deletions(-) diff --git a/src/ImageSharp/Configuration.cs b/src/ImageSharp/Configuration.cs index 9a627eeb7..7b02f21c4 100644 --- a/src/ImageSharp/Configuration.cs +++ b/src/ImageSharp/Configuration.cs @@ -83,6 +83,11 @@ namespace SixLabors.ImageSharp /// public IEnumerable ImageFormats => this.imageFormats; + /// + /// Gets or sets the position in a stream to use for reading. + /// + public ReadOrigin ReadOrigin { get; set; } = ReadOrigin.Begin; + /// /// Gets or sets the that is currently in use. /// diff --git a/src/ImageSharp/Image/Image.FromStream.cs b/src/ImageSharp/Image/Image.FromStream.cs index 30d801423..a0c71133c 100644 --- a/src/ImageSharp/Image/Image.FromStream.cs +++ b/src/ImageSharp/Image/Image.FromStream.cs @@ -20,15 +20,7 @@ namespace SixLabors.ImageSharp /// /// The image stream to read the header from. /// The mime type or null if none found. - public static IImageFormat DetectFormat(Stream stream) => DetectFormat(null, stream); - - /// - /// By reading the header on the provided stream this calculates the images mime type. - /// - /// The image stream to read the header from. - /// The position in the stream to use for reading. - /// The mime type or null if none found. - public static IImageFormat DetectFormat(Stream stream, ReadOrigin origin) => DetectFormat(null, stream, origin); + public static IImageFormat DetectFormat(Stream stream) => DetectFormat(Configuration.Default, stream); /// /// By reading the header on the provided stream this calculates the images mime type. @@ -36,17 +28,8 @@ namespace SixLabors.ImageSharp /// The configuration. /// The image stream to read the header from. /// The mime type or null if none found. - public static IImageFormat DetectFormat(Configuration config, Stream stream) => DetectFormat(config, stream, ReadOrigin.Begin); - - /// - /// By reading the header on the provided stream this calculates the images mime type. - /// - /// The configuration. - /// The image stream to read the header from. - /// The position in the stream to use for reading. - /// The mime type or null if none found. - public static IImageFormat DetectFormat(Configuration config, Stream stream, ReadOrigin origin) - => WithSeekableStream(stream, origin, s => InternalDetectFormat(s, config ?? Configuration.Default)); + public static IImageFormat DetectFormat(Configuration config, Stream stream) + => WithSeekableStream(config, stream, s => InternalDetectFormat(s, config)); /// /// By reading the header on the provided stream this reads the raw image information. @@ -58,33 +41,19 @@ namespace SixLabors.ImageSharp /// /// The or null if suitable info detector not found. /// - public static IImageInfo Identify(Stream stream) => Identify(null, stream); - - /// - /// Reads the raw image information from the specified stream without fully decoding it. - /// - /// The configuration. - /// The image stream to read the information from. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// - /// The or null if suitable info detector is not found. - /// - public static IImageInfo Identify(Configuration config, Stream stream) => Identify(config, stream, ReadOrigin.Begin); + public static IImageInfo Identify(Stream stream) => Identify(Configuration.Default, stream); /// /// Reads the raw image information from the specified stream without fully decoding it. /// /// The configuration. /// The image stream to read the information from. - /// The position in the stream to use for reading. /// Thrown if the stream is not readable. /// /// The or null if suitable info detector is not found. /// - public static IImageInfo Identify(Configuration config, Stream stream, ReadOrigin origin) - => WithSeekableStream(stream, origin, s => InternalIdentity(s, config ?? Configuration.Default)); + public static IImageInfo Identify(Configuration config, Stream stream) + => WithSeekableStream(config, stream, s => InternalIdentity(s, config ?? Configuration.Default)); /// /// Create a new instance of the class from the given stream. @@ -181,22 +150,7 @@ namespace SixLabors.ImageSharp /// A new .> public static Image Load(Stream stream, IImageDecoder decoder) where TPixel : struct, IPixel - => Load(stream, ReadOrigin.Begin, decoder); - - /// - /// Create a new instance of the class from the given stream. - /// - /// The stream containing image information. - /// The position in the stream to use for reading. - /// The decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The pixel format. - /// A new .> - public static Image Load(Stream stream, ReadOrigin origin, IImageDecoder decoder) - where TPixel : struct, IPixel - => WithSeekableStream(stream, origin, s => decoder.Decode(Configuration.Default, s)); + => WithSeekableStream(Configuration.Default, stream, s => decoder.Decode(Configuration.Default, s)); /// /// Create a new instance of the class from the given stream. @@ -211,23 +165,7 @@ namespace SixLabors.ImageSharp /// A new .> public static Image Load(Configuration config, Stream stream, IImageDecoder decoder) where TPixel : struct, IPixel - => Load(config, stream, ReadOrigin.Begin, decoder); - - /// - /// Create a new instance of the class from the given stream. - /// - /// The Configuration. - /// The stream containing image information. - /// The position in the stream to use for reading. - /// The decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The pixel format. - /// A new .> - public static Image Load(Configuration config, Stream stream, ReadOrigin origin, IImageDecoder decoder) - where TPixel : struct, IPixel - => WithSeekableStream(stream, origin, s => decoder.Decode(config, s)); + => WithSeekableStream(config, stream, s => decoder.Decode(config, s)); /// /// Create a new instance of the class from the given stream. @@ -243,21 +181,6 @@ namespace SixLabors.ImageSharp where TPixel : struct, IPixel => Load(config, stream, out IImageFormat _); - /// - /// Create a new instance of the class from the given stream. - /// - /// The configuration options. - /// The stream containing image information. - /// The position in the stream to use for reading. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The pixel format. - /// A new .> - public static Image Load(Configuration config, Stream stream, ReadOrigin origin) - where TPixel : struct, IPixel - => Load(config, stream, origin, out IImageFormat _); - /// /// Create a new instance of the class from the given stream. /// @@ -289,7 +212,7 @@ namespace SixLabors.ImageSharp where TPixel : struct, IPixel { config = config ?? Configuration.Default; - (Image img, IImageFormat format) data = WithSeekableStream(stream, origin, s => Decode(s, config)); + (Image img, IImageFormat format) data = WithSeekableStream(config, stream, s => Decode(s, config)); format = data.format; @@ -309,7 +232,7 @@ namespace SixLabors.ImageSharp throw new NotSupportedException(stringBuilder.ToString()); } - private static T WithSeekableStream(Stream stream, ReadOrigin origin, Func action) + private static T WithSeekableStream(Configuration config, Stream stream, Func action) { if (!stream.CanRead) { @@ -318,7 +241,7 @@ namespace SixLabors.ImageSharp if (stream.CanSeek) { - if (origin == ReadOrigin.Begin) + if (config.ReadOrigin == ReadOrigin.Begin) { stream.Position = 0; } @@ -326,7 +249,7 @@ namespace SixLabors.ImageSharp return action(stream); } - if (origin == ReadOrigin.Current) + if (config.ReadOrigin == ReadOrigin.Current) { throw new NotSupportedException("Cannot seek within the stream."); } diff --git a/tests/ImageSharp.Tests/ConfigurationTests.cs b/tests/ImageSharp.Tests/ConfigurationTests.cs index 18d4abdd1..fc97b2209 100644 --- a/tests/ImageSharp.Tests/ConfigurationTests.cs +++ b/tests/ImageSharp.Tests/ConfigurationTests.cs @@ -59,6 +59,15 @@ namespace SixLabors.ImageSharp.Tests Assert.True(Configuration.Default.ParallelOptions != null); } + /// + /// Test that the default configuration read origin options is set to begin. + /// + [Fact] + public void TestDefultConfigurationReadOriginIsBegin() + { + Assert.True(Configuration.Default.ReadOrigin == ReadOrigin.Begin); + } + /// /// Test that the default configuration parallel options max degrees of parallelism matches the /// environment processor count. @@ -83,7 +92,7 @@ namespace SixLabors.ImageSharp.Tests { Assert.Throws(() => { - this.DefaultConfiguration.SetEncoder(null, new Mock().Object); + this.DefaultConfiguration.SetEncoder(null, new Mock().Object); }); Assert.Throws(() => {