From 1d45fe8d3c7a66098b0769a7c22b9baa17d6dd25 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 28 Feb 2018 23:18:11 +1100 Subject: [PATCH 1/5] Introducing ReadOrigin. Fix #477 --- src/ImageSharp/Image/Image.FromStream.cs | 152 +++++++++++++++++------ src/ImageSharp/Image/ReadOrigin.cs | 21 ++++ 2 files changed, 138 insertions(+), 35 deletions(-) create mode 100644 src/ImageSharp/Image/ReadOrigin.cs diff --git a/src/ImageSharp/Image/Image.FromStream.cs b/src/ImageSharp/Image/Image.FromStream.cs index 62668dd02..30d801423 100644 --- a/src/ImageSharp/Image/Image.FromStream.cs +++ b/src/ImageSharp/Image/Image.FromStream.cs @@ -20,10 +20,15 @@ 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) - { - return DetectFormat(null, stream); - } + 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); /// /// By reading the header on the provided stream this calculates the images mime type. @@ -31,10 +36,17 @@ 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) - { - return WithSeekableStream(stream, s => InternalDetectFormat(s, config ?? Configuration.Default)); - } + 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)); /// /// By reading the header on the provided stream this reads the raw image information. @@ -46,10 +58,7 @@ namespace SixLabors.ImageSharp /// /// The or null if suitable info detector not found. /// - public static IImageInfo Identify(Stream stream) - { - return Identify(null, stream); - } + public static IImageInfo Identify(Stream stream) => Identify(null, stream); /// /// Reads the raw image information from the specified stream without fully decoding it. @@ -62,10 +71,20 @@ namespace SixLabors.ImageSharp /// /// The or null if suitable info detector is not found. /// - public static IImageInfo Identify(Configuration config, Stream stream) - { - return WithSeekableStream(stream, s => InternalIdentity(s, config ?? Configuration.Default)); - } + public static IImageInfo Identify(Configuration config, Stream stream) => Identify(config, stream, ReadOrigin.Begin); + + /// + /// 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)); /// /// Create a new instance of the class from the given stream. @@ -120,7 +139,8 @@ namespace SixLabors.ImageSharp /// Thrown if the stream is not readable nor seekable. /// /// A new .> - public static Image Load(Configuration config, Stream stream, out IImageFormat format) => Load(config, stream, out format); + public static Image Load(Configuration config, Stream stream, out IImageFormat format) + => Load(config, stream, out format); /// /// Create a new instance of the class from the given stream. @@ -133,9 +153,7 @@ namespace SixLabors.ImageSharp /// A new .> public static Image Load(Stream stream) where TPixel : struct, IPixel - { - return Load(null, stream); - } + => Load(null, stream); /// /// Create a new instance of the class from the given stream. @@ -149,9 +167,7 @@ namespace SixLabors.ImageSharp /// A new .> public static Image Load(Stream stream, out IImageFormat format) where TPixel : struct, IPixel - { - return Load(null, stream, out format); - } + => Load(null, stream, out format); /// /// Create a new instance of the class from the given stream. @@ -165,9 +181,22 @@ namespace SixLabors.ImageSharp /// A new .> public static Image Load(Stream stream, IImageDecoder decoder) where TPixel : struct, IPixel - { - return WithSeekableStream(stream, s => decoder.Decode(Configuration.Default, s)); - } + => 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)); /// /// Create a new instance of the class from the given stream. @@ -182,9 +211,23 @@ namespace SixLabors.ImageSharp /// A new .> public static Image Load(Configuration config, Stream stream, IImageDecoder decoder) where TPixel : struct, IPixel - { - return WithSeekableStream(stream, s => decoder.Decode(config, s)); - } + => 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)); /// /// Create a new instance of the class from the given stream. @@ -198,9 +241,22 @@ namespace SixLabors.ImageSharp /// A new .> public static Image Load(Configuration config, Stream stream) where TPixel : struct, IPixel - { - return Load(config, stream, out var _); - } + => 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. @@ -214,10 +270,26 @@ namespace SixLabors.ImageSharp /// The pixel format. /// A new .> public static Image Load(Configuration config, Stream stream, out IImageFormat format) - where TPixel : struct, IPixel + where TPixel : struct, IPixel + => Load(config, stream, ReadOrigin.Begin, out format); + + /// + /// 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. + /// the mime type of the decoded image. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// The pixel format. + /// A new .> + public static Image Load(Configuration config, Stream stream, ReadOrigin origin, out IImageFormat format) + where TPixel : struct, IPixel { config = config ?? Configuration.Default; - (Image img, IImageFormat format) data = WithSeekableStream(stream, s => Decode(s, config)); + (Image img, IImageFormat format) data = WithSeekableStream(stream, origin, s => Decode(s, config)); format = data.format; @@ -237,7 +309,7 @@ namespace SixLabors.ImageSharp throw new NotSupportedException(stringBuilder.ToString()); } - private static T WithSeekableStream(Stream stream, Func action) + private static T WithSeekableStream(Stream stream, ReadOrigin origin, Func action) { if (!stream.CanRead) { @@ -246,9 +318,19 @@ namespace SixLabors.ImageSharp if (stream.CanSeek) { + if (origin == ReadOrigin.Begin) + { + stream.Position = 0; + } + return action(stream); } + if (origin == ReadOrigin.Current) + { + throw new NotSupportedException("Cannot seek within the stream."); + } + // We want to be able to load images from things like HttpContext.Request.Body using (var memoryStream = new MemoryStream()) { diff --git a/src/ImageSharp/Image/ReadOrigin.cs b/src/ImageSharp/Image/ReadOrigin.cs new file mode 100644 index 000000000..f17bc82f1 --- /dev/null +++ b/src/ImageSharp/Image/ReadOrigin.cs @@ -0,0 +1,21 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +namespace SixLabors.ImageSharp +{ + /// + /// Specifies the position in a stream to use for reading. + /// + public enum ReadOrigin + { + /// + /// Specifies the beginning of a stream. + /// + Begin, + + /// + /// Specifies the current position within a stream. + /// + Current + } +} From c5f7f27e19b5ebfe73d54aba65b90b962fe2801b Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 1 Mar 2018 00:29:55 +1100 Subject: [PATCH 2/5] 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(() => { From 24b42172969e8d8c392c927aa8809c7307c4687b Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 1 Mar 2018 00:49:43 +1100 Subject: [PATCH 3/5] So many nulls! --- src/ImageSharp/Image/Image.FromBytes.cs | 14 +++++--------- src/ImageSharp/Image/Image.FromFile.cs | 11 ++++------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/ImageSharp/Image/Image.FromBytes.cs b/src/ImageSharp/Image/Image.FromBytes.cs index 9da9c5e43..44c53d776 100644 --- a/src/ImageSharp/Image/Image.FromBytes.cs +++ b/src/ImageSharp/Image/Image.FromBytes.cs @@ -19,7 +19,7 @@ namespace SixLabors.ImageSharp /// The format or null if none found. public static IImageFormat DetectFormat(byte[] data) { - return DetectFormat(null, data); + return DetectFormat(Configuration.Default, data); } /// @@ -41,7 +41,7 @@ namespace SixLabors.ImageSharp /// /// The byte array containing image data. /// A new . - public static Image Load(byte[] data) => Load(null, data); + public static Image Load(byte[] data) => Load(Configuration.Default, data); /// /// Create a new instance of the class from the given byte array. @@ -49,7 +49,7 @@ namespace SixLabors.ImageSharp /// The byte array containing image data. /// The mime type of the decoded image. /// A new . - public static Image Load(byte[] data, out IImageFormat format) => Load(null, data, out format); + public static Image Load(byte[] data, out IImageFormat format) => Load(Configuration.Default, data, out format); /// /// Create a new instance of the class from the given byte array. @@ -93,9 +93,7 @@ namespace SixLabors.ImageSharp /// A new . public static Image Load(byte[] data) where TPixel : struct, IPixel - { - return Load(null, data); - } + => Load(Configuration.Default, data); /// /// Create a new instance of the class from the given byte array. @@ -106,9 +104,7 @@ namespace SixLabors.ImageSharp /// A new . public static Image Load(byte[] data, out IImageFormat format) where TPixel : struct, IPixel - { - return Load(null, data, out format); - } + => Load(Configuration.Default, data, out format); /// /// Create a new instance of the class from the given byte array. diff --git a/src/ImageSharp/Image/Image.FromFile.cs b/src/ImageSharp/Image/Image.FromFile.cs index 0474a6d47..ad8f3426f 100644 --- a/src/ImageSharp/Image/Image.FromFile.cs +++ b/src/ImageSharp/Image/Image.FromFile.cs @@ -21,7 +21,7 @@ namespace SixLabors.ImageSharp /// The mime type or null if none found. public static IImageFormat DetectFormat(string filePath) { - return DetectFormat(null, filePath); + return DetectFormat(Configuration.Default, filePath); } /// @@ -118,7 +118,7 @@ namespace SixLabors.ImageSharp public static Image Load(string path) where TPixel : struct, IPixel { - return Load(null, path); + return Load(Configuration.Default, path); } /// @@ -134,7 +134,7 @@ namespace SixLabors.ImageSharp public static Image Load(string path, out IImageFormat format) where TPixel : struct, IPixel { - return Load(null, path, out format); + return Load(Configuration.Default, path, out format); } /// @@ -150,7 +150,6 @@ namespace SixLabors.ImageSharp public static Image Load(Configuration config, string path) where TPixel : struct, IPixel { - config = config ?? Configuration.Default; using (Stream stream = config.FileSystem.OpenRead(path)) { return Load(config, stream); @@ -171,7 +170,6 @@ namespace SixLabors.ImageSharp public static Image Load(Configuration config, string path, out IImageFormat format) where TPixel : struct, IPixel { - config = config ?? Configuration.Default; using (Stream stream = config.FileSystem.OpenRead(path)) { return Load(config, stream, out format); @@ -191,7 +189,7 @@ namespace SixLabors.ImageSharp public static Image Load(string path, IImageDecoder decoder) where TPixel : struct, IPixel { - return Load(null, path, decoder); + return Load(Configuration.Default, path, decoder); } /// @@ -208,7 +206,6 @@ namespace SixLabors.ImageSharp public static Image Load(Configuration config, string path, IImageDecoder decoder) where TPixel : struct, IPixel { - config = config ?? Configuration.Default; using (Stream stream = config.FileSystem.OpenRead(path)) { return Load(config, stream, decoder); From c483cb5a53025cc801fe3e949537987ed01cf7c6 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 1 Mar 2018 15:03:28 +1100 Subject: [PATCH 4/5] Use Current as Default, Don't additionally throw --- src/ImageSharp/Configuration.cs | 4 +- src/ImageSharp/Image/Image.FromStream.cs | 59 +++++--------------- tests/ImageSharp.Tests/ConfigurationTests.cs | 4 +- 3 files changed, 19 insertions(+), 48 deletions(-) diff --git a/src/ImageSharp/Configuration.cs b/src/ImageSharp/Configuration.cs index 7b02f21c4..34bbb61e0 100644 --- a/src/ImageSharp/Configuration.cs +++ b/src/ImageSharp/Configuration.cs @@ -84,9 +84,9 @@ namespace SixLabors.ImageSharp public IEnumerable ImageFormats => this.imageFormats; /// - /// Gets or sets the position in a stream to use for reading. + /// Gets or sets the position in a stream to use for reading when using a seekable stream as an image data source. /// - public ReadOrigin ReadOrigin { get; set; } = ReadOrigin.Begin; + public ReadOrigin ReadOrigin { get; set; } = ReadOrigin.Current; /// /// 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 a0c71133c..c3f4a714f 100644 --- a/src/ImageSharp/Image/Image.FromStream.cs +++ b/src/ImageSharp/Image/Image.FromStream.cs @@ -19,6 +19,7 @@ namespace SixLabors.ImageSharp /// By reading the header on the provided stream this calculates the images mime type. /// /// The image stream to read the header from. + /// Thrown if the stream is not readable. /// The mime type or null if none found. public static IImageFormat DetectFormat(Stream stream) => DetectFormat(Configuration.Default, stream); @@ -27,6 +28,7 @@ namespace SixLabors.ImageSharp /// /// The configuration. /// The image stream to read the header from. + /// Thrown if the stream is not readable. /// The mime type or null if none found. public static IImageFormat DetectFormat(Configuration config, Stream stream) => WithSeekableStream(config, stream, s => InternalDetectFormat(s, config)); @@ -35,9 +37,7 @@ namespace SixLabors.ImageSharp /// By reading the header on the provided stream this reads the raw image information. /// /// The image stream to read the header from. - /// - /// Thrown if the stream is not readable nor seekable. - /// + /// Thrown if the stream is not readable. /// /// The or null if suitable info detector not found. /// @@ -60,9 +60,7 @@ namespace SixLabors.ImageSharp /// /// The stream containing image information. /// the mime type of the decoded image. - /// - /// Thrown if the stream is not readable nor seekable. - /// + /// Thrown if the stream is not readable. /// A new .> public static Image Load(Stream stream, out IImageFormat format) => Load(stream, out format); @@ -70,9 +68,7 @@ namespace SixLabors.ImageSharp /// Create a new instance of the class from the given stream. /// /// The stream containing image information. - /// - /// Thrown if the stream is not readable nor seekable. - /// + /// Thrown if the stream is not readable. /// A new .> public static Image Load(Stream stream) => Load(stream); @@ -81,9 +77,7 @@ namespace SixLabors.ImageSharp /// /// The stream containing image information. /// The decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// + /// Thrown if the stream is not readable. /// A new .> public static Image Load(Stream stream, IImageDecoder decoder) => Load(stream, decoder); @@ -92,9 +86,7 @@ namespace SixLabors.ImageSharp /// /// The config for the decoder. /// The stream containing image information. - /// - /// Thrown if the stream is not readable nor seekable. - /// + /// Thrown if the stream is not readable. /// A new .> public static Image Load(Configuration config, Stream stream) => Load(config, stream); @@ -104,9 +96,7 @@ namespace SixLabors.ImageSharp /// The config for the decoder. /// The stream containing image information. /// the mime type of the decoded image. - /// - /// Thrown if the stream is not readable nor seekable. - /// + /// Thrown if the stream is not readable. /// A new .> public static Image Load(Configuration config, Stream stream, out IImageFormat format) => Load(config, stream, out format); @@ -115,9 +105,7 @@ namespace SixLabors.ImageSharp /// Create a new instance of the class from the given stream. /// /// The stream containing image information. - /// - /// Thrown if the stream is not readable nor seekable. - /// + /// Thrown if the stream is not readable. /// The pixel format. /// A new .> public static Image Load(Stream stream) @@ -129,9 +117,7 @@ namespace SixLabors.ImageSharp /// /// The stream containing image information. /// the mime type of the decoded image. - /// - /// Thrown if the stream is not readable nor seekable. - /// + /// Thrown if the stream is not readable. /// The pixel format. /// A new .> public static Image Load(Stream stream, out IImageFormat format) @@ -143,9 +129,7 @@ namespace SixLabors.ImageSharp /// /// The stream containing image information. /// The decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// + /// Thrown if the stream is not readable. /// The pixel format. /// A new .> public static Image Load(Stream stream, IImageDecoder decoder) @@ -158,9 +142,7 @@ namespace SixLabors.ImageSharp /// The Configuration. /// The stream containing image information. /// The decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// + /// Thrown if the stream is not readable. /// The pixel format. /// A new .> public static Image Load(Configuration config, Stream stream, IImageDecoder decoder) @@ -172,9 +154,7 @@ namespace SixLabors.ImageSharp /// /// The configuration options. /// The stream containing image information. - /// - /// Thrown if the stream is not readable nor seekable. - /// + /// Thrown if the stream is not readable. /// The pixel format. /// A new .> public static Image Load(Configuration config, Stream stream) @@ -187,9 +167,7 @@ namespace SixLabors.ImageSharp /// The configuration options. /// The stream containing image information. /// the mime type of the decoded image. - /// - /// Thrown if the stream is not readable nor seekable. - /// + /// Thrown if the stream is not readable. /// The pixel format. /// A new .> public static Image Load(Configuration config, Stream stream, out IImageFormat format) @@ -203,9 +181,7 @@ namespace SixLabors.ImageSharp /// The stream containing image information. /// The position in the stream to use for reading. /// the mime type of the decoded image. - /// - /// Thrown if the stream is not readable nor seekable. - /// + /// Thrown if the stream is not readable. /// The pixel format. /// A new .> public static Image Load(Configuration config, Stream stream, ReadOrigin origin, out IImageFormat format) @@ -249,11 +225,6 @@ namespace SixLabors.ImageSharp return action(stream); } - if (config.ReadOrigin == ReadOrigin.Current) - { - throw new NotSupportedException("Cannot seek within the stream."); - } - // We want to be able to load images from things like HttpContext.Request.Body using (var memoryStream = new MemoryStream()) { diff --git a/tests/ImageSharp.Tests/ConfigurationTests.cs b/tests/ImageSharp.Tests/ConfigurationTests.cs index fc97b2209..06f02fcf1 100644 --- a/tests/ImageSharp.Tests/ConfigurationTests.cs +++ b/tests/ImageSharp.Tests/ConfigurationTests.cs @@ -63,9 +63,9 @@ namespace SixLabors.ImageSharp.Tests /// Test that the default configuration read origin options is set to begin. /// [Fact] - public void TestDefultConfigurationReadOriginIsBegin() + public void TestDefultConfigurationReadOriginIsCurrent() { - Assert.True(Configuration.Default.ReadOrigin == ReadOrigin.Begin); + Assert.True(Configuration.Default.ReadOrigin == ReadOrigin.Current); } /// From 450a85387c305291bff136ff8d20854939d4bc1f Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 1 Mar 2018 20:33:03 +1100 Subject: [PATCH 5/5] Remove unneeded overload. --- src/ImageSharp/Image/Image.FromStream.cs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/ImageSharp/Image/Image.FromStream.cs b/src/ImageSharp/Image/Image.FromStream.cs index c3f4a714f..4e294260a 100644 --- a/src/ImageSharp/Image/Image.FromStream.cs +++ b/src/ImageSharp/Image/Image.FromStream.cs @@ -172,20 +172,6 @@ namespace SixLabors.ImageSharp /// A new .> public static Image Load(Configuration config, Stream stream, out IImageFormat format) where TPixel : struct, IPixel - => Load(config, stream, ReadOrigin.Begin, out format); - - /// - /// 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. - /// the mime type of the decoded image. - /// Thrown if the stream is not readable. - /// The pixel format. - /// A new .> - public static Image Load(Configuration config, Stream stream, ReadOrigin origin, out IImageFormat format) - where TPixel : struct, IPixel { config = config ?? Configuration.Default; (Image img, IImageFormat format) data = WithSeekableStream(config, stream, s => Decode(s, config));