From 29b709da12e3d23f9efb132dc83ca74216fb8c06 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 14 Mar 2020 23:28:12 +1100 Subject: [PATCH 1/2] Add overloads. Fix #1144 --- src/ImageSharp/Image.FromBytes.cs | 40 ++++++++ src/ImageSharp/Image.FromFile.cs | 42 +++++++- src/ImageSharp/Image.FromStream.cs | 4 +- .../Image/ImageTests.Identify.cs | 99 +++++++++++++++++++ .../Image/ImageTests.ImageLoadTestBase.cs | 11 ++- 5 files changed, 189 insertions(+), 7 deletions(-) create mode 100644 tests/ImageSharp.Tests/Image/ImageTests.Identify.cs diff --git a/src/ImageSharp/Image.FromBytes.cs b/src/ImageSharp/Image.FromBytes.cs index 0850e2213f..f1196ac977 100644 --- a/src/ImageSharp/Image.FromBytes.cs +++ b/src/ImageSharp/Image.FromBytes.cs @@ -37,6 +37,46 @@ public static IImageFormat DetectFormat(Configuration config, byte[] data) } } + /// + /// Reads the raw image information from the specified stream without fully decoding it. + /// + /// The byte array containing encoded image data to read the header from. + /// Thrown if the stream is not readable. + /// + /// The or null if suitable info detector not found. + /// + public static IImageInfo Identify(byte[] data) => Identify(data, out IImageFormat _); + + /// + /// Reads the raw image information from the specified stream without fully decoding it. + /// + /// The byte array containing encoded image data to read the header from. + /// The format type of the decoded image. + /// Thrown if the stream is not readable. + /// + /// The or null if suitable info detector not found. + /// + public static IImageInfo Identify(byte[] data, out IImageFormat format) => Identify(Configuration.Default, data, out format); + + /// + /// Reads the raw image information from the specified stream without fully decoding it. + /// + /// The configuration. + /// The byte array containing encoded image data to read the header from. + /// The format type of the decoded image. + /// Thrown if the stream is not readable. + /// + /// The or null if suitable info detector is not found. + /// + public static IImageInfo Identify(Configuration config, byte[] data, out IImageFormat format) + { + config ??= Configuration.Default; + using (var stream = new MemoryStream(data)) + { + return Identify(config, stream, out format); + } + } + /// /// Load a new instance of from the given encoded byte array. /// diff --git a/src/ImageSharp/Image.FromFile.cs b/src/ImageSharp/Image.FromFile.cs index 45ea378cfd..bb26e4142c 100644 --- a/src/ImageSharp/Image.FromFile.cs +++ b/src/ImageSharp/Image.FromFile.cs @@ -31,13 +31,53 @@ public static IImageFormat DetectFormat(string filePath) /// The mime type or null if none found. public static IImageFormat DetectFormat(Configuration config, string filePath) { - config = config ?? Configuration.Default; + config ??= Configuration.Default; using (Stream file = config.FileSystem.OpenRead(filePath)) { return DetectFormat(config, file); } } + /// + /// Reads the raw image information from the specified stream without fully decoding it. + /// + /// The image file to open and to read the header from. + /// Thrown if the stream is not readable. + /// + /// The or null if suitable info detector not found. + /// + public static IImageInfo Identify(string filePath) => Identify(filePath, out IImageFormat _); + + /// + /// Reads the raw image information from the specified stream without fully decoding it. + /// + /// The image file to open and to read the header from. + /// The format type of the decoded image. + /// Thrown if the stream is not readable. + /// + /// The or null if suitable info detector not found. + /// + public static IImageInfo Identify(string filePath, out IImageFormat format) => Identify(Configuration.Default, filePath, out format); + + /// + /// Reads the raw image information from the specified stream without fully decoding it. + /// + /// The configuration. + /// The image file to open and to read the header from. + /// The format type of the decoded image. + /// Thrown if the stream is not readable. + /// + /// The or null if suitable info detector is not found. + /// + public static IImageInfo Identify(Configuration config, string filePath, out IImageFormat format) + { + config ??= Configuration.Default; + using (Stream file = config.FileSystem.OpenRead(filePath)) + { + return Identify(config, file, out format); + } + } + /// /// Create a new instance of the class from the given file. /// diff --git a/src/ImageSharp/Image.FromStream.cs b/src/ImageSharp/Image.FromStream.cs index d756ff7ac1..95a71903a8 100644 --- a/src/ImageSharp/Image.FromStream.cs +++ b/src/ImageSharp/Image.FromStream.cs @@ -34,7 +34,7 @@ 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. + /// Reads the raw image information from the specified stream without fully decoding it. /// /// The image stream to read the header from. /// Thrown if the stream is not readable. @@ -44,7 +44,7 @@ public static IImageFormat DetectFormat(Configuration config, Stream stream) public static IImageInfo Identify(Stream stream) => Identify(stream, out IImageFormat _); /// - /// By reading the header on the provided stream this reads the raw image information. + /// Reads the raw image information from the specified stream without fully decoding it. /// /// The image stream to read the header from. /// The format type of the decoded image. diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Identify.cs b/tests/ImageSharp.Tests/Image/ImageTests.Identify.cs new file mode 100644 index 0000000000..2be9504079 --- /dev/null +++ b/tests/ImageSharp.Tests/Image/ImageTests.Identify.cs @@ -0,0 +1,99 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.IO; +using SixLabors.ImageSharp.Formats; + +using Xunit; + +// ReSharper disable InconsistentNaming +namespace SixLabors.ImageSharp.Tests +{ + public partial class ImageTests + { + /// + /// Tests the class. + /// + public class Identify : ImageLoadTestBase + { + private static readonly string ActualImagePath = TestFile.GetInputFileFullPath(TestImages.Bmp.F); + + private byte[] ActualImageBytes => TestFile.Create(TestImages.Bmp.F).Bytes; + + private byte[] ByteArray => this.DataStream.ToArray(); + + private IImageInfo LocalImageInfo => this.localImageInfoMock.Object; + + private IImageFormat LocalImageFormat => this.localImageFormatMock.Object; + + private static readonly IImageFormat ExpectedGlobalFormat = + Configuration.Default.ImageFormatsManager.FindFormatByFileExtension("bmp"); + + [Fact] + public void FromBytes_GlobalConfiguration() + { + IImageInfo info = Image.Identify(this.ActualImageBytes, out IImageFormat type); + + Assert.NotNull(info); + Assert.Equal(ExpectedGlobalFormat, type); + } + + [Fact] + public void FromBytes_CustomConfiguration() + { + IImageInfo info = Image.Identify(this.LocalConfiguration, this.ByteArray, out IImageFormat type); + + Assert.Equal(this.LocalImageInfo, info); + Assert.Equal(this.LocalImageFormat, type); + } + + [Fact] + public void FromFileSystemPath_GlobalConfiguration() + { + IImageInfo info = Image.Identify(ActualImagePath, out IImageFormat type); + + Assert.NotNull(info); + Assert.Equal(ExpectedGlobalFormat, type); + } + + [Fact] + public void FromFileSystemPath_CustomConfiguration() + { + IImageInfo info = Image.Identify(this.LocalConfiguration, this.MockFilePath, out IImageFormat type); + + Assert.Equal(this.LocalImageInfo, info); + Assert.Equal(this.LocalImageFormat, type); + } + + [Fact] + public void FromStream_GlobalConfiguration() + { + using (var stream = new MemoryStream(this.ActualImageBytes)) + { + IImageInfo info = Image.Identify(stream, out IImageFormat type); + + Assert.NotNull(info); + Assert.Equal(ExpectedGlobalFormat, type); + } + } + + [Fact] + public void FromStream_CustomConfiguration() + { + IImageInfo info = Image.Identify(this.LocalConfiguration, this.DataStream, out IImageFormat type); + + Assert.Equal(this.LocalImageInfo, info); + Assert.Equal(this.LocalImageFormat, type); + } + + [Fact] + public void WhenNoMatchingFormatFound_ReturnsNull() + { + IImageInfo info = Image.Identify(new Configuration(), this.DataStream, out IImageFormat type); + + Assert.Null(info); + Assert.Null(type); + } + } + } +} diff --git a/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs b/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs index dff83df26d..d010f60236 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs @@ -26,6 +26,8 @@ public abstract class ImageLoadTestBase : IDisposable protected Mock localImageFormatMock; + protected Mock localImageInfoMock; + protected readonly string MockFilePath = Guid.NewGuid().ToString(); internal readonly Mock LocalFileSystemMock = new Mock(); @@ -53,9 +55,12 @@ protected ImageLoadTestBase() this.localStreamReturnImageRgba32 = new Image(1, 1); this.localStreamReturnImageAgnostic = new Image(1, 1); + this.localImageInfoMock = new Mock(); this.localImageFormatMock = new Mock(); - this.localDecoder = new Mock(); + var detector = new Mock(); + detector.Setup(x => x.Identify(It.IsAny(), It.IsAny())).Returns(this.localImageInfoMock.Object); + this.localDecoder = detector.As(); this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); this.localDecoder.Setup(x => x.Decode(It.IsAny(), It.IsAny())) @@ -80,9 +85,7 @@ protected ImageLoadTestBase() }) .Returns(this.localStreamReturnImageAgnostic); - this.LocalConfiguration = new Configuration - { - }; + this.LocalConfiguration = new Configuration(); this.LocalConfiguration.ImageFormatsManager.AddImageFormatDetector(this.localMimeTypeDetector); this.LocalConfiguration.ImageFormatsManager.SetDecoder(this.localImageFormatMock.Object, this.localDecoder.Object); From 0873fe52446b7dd9f4d466d0727c340729afcd97 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 15 Mar 2020 00:29:54 +1100 Subject: [PATCH 2/2] Add Guard checks, don't pass null, --- src/ImageSharp/Image.FromBytes.cs | 95 +++++++++++++++--------------- src/ImageSharp/Image.FromFile.cs | 69 ++++++++++++---------- src/ImageSharp/Image.FromStream.cs | 64 ++++++++++---------- 3 files changed, 117 insertions(+), 111 deletions(-) diff --git a/src/ImageSharp/Image.FromBytes.cs b/src/ImageSharp/Image.FromBytes.cs index f1196ac977..06b05fe3c4 100644 --- a/src/ImageSharp/Image.FromBytes.cs +++ b/src/ImageSharp/Image.FromBytes.cs @@ -26,14 +26,15 @@ public static IImageFormat DetectFormat(byte[] data) /// /// By reading the header on the provided byte array this calculates the images format. /// - /// The configuration. + /// The configuration. /// The byte array containing encoded image data to read the header from. /// The mime type or null if none found. - public static IImageFormat DetectFormat(Configuration config, byte[] data) + public static IImageFormat DetectFormat(Configuration configuration, byte[] data) { - using (var stream = new MemoryStream(data)) + Guard.NotNull(configuration, nameof(configuration)); + using (var stream = new MemoryStream(data, 0, data.Length, false, true)) { - return DetectFormat(config, stream); + return DetectFormat(configuration, stream); } } @@ -61,19 +62,19 @@ public static IImageFormat DetectFormat(Configuration config, byte[] data) /// /// Reads the raw image information from the specified stream without fully decoding it. /// - /// The configuration. + /// The configuration. /// The byte array containing encoded image data to read the header from. /// The format type of the decoded image. /// Thrown if the stream is not readable. /// /// The or null if suitable info detector is not found. /// - public static IImageInfo Identify(Configuration config, byte[] data, out IImageFormat format) + public static IImageInfo Identify(Configuration configuration, byte[] data, out IImageFormat format) { - config ??= Configuration.Default; - using (var stream = new MemoryStream(data)) + Guard.NotNull(configuration, nameof(configuration)); + using (var stream = new MemoryStream(data, 0, data.Length, false, true)) { - return Identify(config, stream, out format); + return Identify(configuration, stream, out format); } } @@ -108,33 +109,33 @@ public static Image Load(byte[] data, out IImageFormat format) /// /// Load a new instance of from the given encoded byte array. /// - /// The configuration options. + /// The configuration options. /// The byte array containing encoded image data. /// The pixel format. /// A new . - public static Image Load(Configuration config, byte[] data) + public static Image Load(Configuration configuration, byte[] data) where TPixel : unmanaged, IPixel { using (var stream = new MemoryStream(data, 0, data.Length, false, true)) { - return Load(config, stream); + return Load(configuration, stream); } } /// /// Load a new instance of from the given encoded byte array. /// - /// The configuration options. + /// The configuration options. /// The byte array containing encoded image data. /// The of the decoded image. /// The pixel format. /// A new . - public static Image Load(Configuration config, byte[] data, out IImageFormat format) + public static Image Load(Configuration configuration, byte[] data, out IImageFormat format) where TPixel : unmanaged, IPixel { using (var stream = new MemoryStream(data, 0, data.Length, false, true)) { - return Load(config, stream, out format); + return Load(configuration, stream, out format); } } @@ -157,17 +158,17 @@ public static Image Load(byte[] data, IImageDecoder decoder) /// /// Load a new instance of from the given encoded byte array. /// - /// The Configuration. + /// The Configuration. /// The byte array containing encoded image data. /// The decoder. /// The pixel format. /// A new . - public static Image Load(Configuration config, byte[] data, IImageDecoder decoder) + public static Image Load(Configuration configuration, byte[] data, IImageDecoder decoder) where TPixel : unmanaged, IPixel { using (var stream = new MemoryStream(data, 0, data.Length, false, true)) { - return Load(config, stream, decoder); + return Load(configuration, stream, decoder); } } @@ -184,18 +185,18 @@ public static IImageFormat DetectFormat(ReadOnlySpan data) /// /// By reading the header on the provided byte array this calculates the images format. /// - /// The configuration. + /// The configuration. /// The byte array containing encoded image data to read the header from. /// The mime type or null if none found. - public static IImageFormat DetectFormat(Configuration config, ReadOnlySpan data) + public static IImageFormat DetectFormat(Configuration configuration, ReadOnlySpan data) { - int maxHeaderSize = config.MaxHeaderSize; + int maxHeaderSize = configuration.MaxHeaderSize; if (maxHeaderSize <= 0) { return null; } - foreach (IImageFormatDetector detector in config.ImageFormatsManager.FormatDetectors) + foreach (IImageFormatDetector detector in configuration.ImageFormatsManager.FormatDetectors) { IImageFormat f = detector.DetectFormat(data); @@ -243,18 +244,18 @@ public static Image Load(ReadOnlySpan data, IImageDecoder /// /// Load a new instance of from the given encoded byte span. /// - /// The configuration options. + /// The configuration options. /// The byte span containing encoded image data. /// The pixel format. /// A new . - public static unsafe Image Load(Configuration config, ReadOnlySpan data) + public static unsafe Image Load(Configuration configuration, ReadOnlySpan data) where TPixel : unmanaged, IPixel { fixed (byte* ptr = &data.GetPinnableReference()) { using (var stream = new UnmanagedMemoryStream(ptr, data.Length)) { - return Load(config, stream); + return Load(configuration, stream); } } } @@ -262,13 +263,13 @@ public static unsafe Image Load(Configuration config, ReadOnlySp /// /// Load a new instance of from the given encoded byte span. /// - /// The Configuration. + /// The Configuration. /// The byte span containing image data. /// The decoder. /// The pixel format. /// A new . public static unsafe Image Load( - Configuration config, + Configuration configuration, ReadOnlySpan data, IImageDecoder decoder) where TPixel : unmanaged, IPixel @@ -277,7 +278,7 @@ public static unsafe Image Load( { using (var stream = new UnmanagedMemoryStream(ptr, data.Length)) { - return Load(config, stream, decoder); + return Load(configuration, stream, decoder); } } } @@ -285,13 +286,13 @@ public static unsafe Image Load( /// /// Load a new instance of from the given encoded byte span. /// - /// The configuration options. + /// The configuration options. /// The byte span containing image data. /// The of the decoded image. /// The pixel format. /// A new . public static unsafe Image Load( - Configuration config, + Configuration configuration, ReadOnlySpan data, out IImageFormat format) where TPixel : unmanaged, IPixel @@ -300,7 +301,7 @@ public static unsafe Image Load( { using (var stream = new UnmanagedMemoryStream(ptr, data.Length)) { - return Load(config, stream, out format); + return Load(configuration, stream, out format); } } } @@ -325,38 +326,38 @@ public static Image Load(byte[] data, out IImageFormat format) => /// /// Load a new instance of from the given encoded byte array. /// - /// The config for the decoder. + /// The configuration for the decoder. /// The byte array containing encoded image data. /// The . - public static Image Load(Configuration config, byte[] data) => Load(config, data, out _); + public static Image Load(Configuration configuration, byte[] data) => Load(configuration, data, out _); /// /// Load a new instance of from the given encoded byte array. /// - /// The config for the decoder. + /// The configuration for the decoder. /// The byte array containing image data. /// The decoder. /// The . - public static Image Load(Configuration config, byte[] data, IImageDecoder decoder) + public static Image Load(Configuration configuration, byte[] data, IImageDecoder decoder) { using (var stream = new MemoryStream(data, 0, data.Length, false, true)) { - return Load(config, stream, decoder); + return Load(configuration, stream, decoder); } } /// /// Load a new instance of from the given encoded byte array. /// - /// The config for the decoder. + /// The configuration for the decoder. /// The byte array containing image data. /// The mime type of the decoded image. /// The . - public static Image Load(Configuration config, byte[] data, out IImageFormat format) + public static Image Load(Configuration configuration, byte[] data, out IImageFormat format) { using (var stream = new MemoryStream(data, 0, data.Length, false, true)) { - return Load(config, stream, out format); + return Load(configuration, stream, out format); } } @@ -388,20 +389,20 @@ public static Image Load(ReadOnlySpan data, out IImageFormat format) => /// /// Decodes a new instance of from the given encoded byte span. /// - /// The configuration options. + /// The configuration options. /// The byte span containing image data. /// The . - public static Image Load(Configuration config, ReadOnlySpan data) => Load(config, data, out _); + public static Image Load(Configuration configuration, ReadOnlySpan data) => Load(configuration, data, out _); /// /// Load a new instance of from the given encoded byte span. /// - /// The Configuration. + /// The Configuration. /// The byte span containing image data. /// The decoder. /// The . public static unsafe Image Load( - Configuration config, + Configuration configuration, ReadOnlySpan data, IImageDecoder decoder) { @@ -409,7 +410,7 @@ public static unsafe Image Load( { using (var stream = new UnmanagedMemoryStream(ptr, data.Length)) { - return Load(config, stream, decoder); + return Load(configuration, stream, decoder); } } } @@ -417,12 +418,12 @@ public static unsafe Image Load( /// /// Load a new instance of from the given encoded byte span. /// - /// The configuration options. + /// The configuration options. /// The byte span containing image data. /// The of the decoded image.> /// The . public static unsafe Image Load( - Configuration config, + Configuration configuration, ReadOnlySpan data, out IImageFormat format) { @@ -430,7 +431,7 @@ public static unsafe Image Load( { using (var stream = new UnmanagedMemoryStream(ptr, data.Length)) { - return Load(config, stream, out format); + return Load(configuration, stream, out format); } } } diff --git a/src/ImageSharp/Image.FromFile.cs b/src/ImageSharp/Image.FromFile.cs index bb26e4142c..1a9fa15462 100644 --- a/src/ImageSharp/Image.FromFile.cs +++ b/src/ImageSharp/Image.FromFile.cs @@ -26,15 +26,15 @@ public static IImageFormat DetectFormat(string filePath) /// /// By reading the header on the provided file this calculates the images mime type. /// - /// The configuration. + /// The configuration. /// The image file to open and to read the header from. /// The mime type or null if none found. - public static IImageFormat DetectFormat(Configuration config, string filePath) + public static IImageFormat DetectFormat(Configuration configuration, string filePath) { - config ??= Configuration.Default; - using (Stream file = config.FileSystem.OpenRead(filePath)) + Guard.NotNull(configuration, nameof(configuration)); + using (Stream file = configuration.FileSystem.OpenRead(filePath)) { - return DetectFormat(config, file); + return DetectFormat(configuration, file); } } @@ -62,19 +62,19 @@ public static IImageFormat DetectFormat(Configuration config, string filePath) /// /// Reads the raw image information from the specified stream without fully decoding it. /// - /// The configuration. + /// The configuration. /// The image file to open and to read the header from. /// The format type of the decoded image. /// Thrown if the stream is not readable. /// /// The or null if suitable info detector is not found. /// - public static IImageInfo Identify(Configuration config, string filePath, out IImageFormat format) + public static IImageInfo Identify(Configuration configuration, string filePath, out IImageFormat format) { - config ??= Configuration.Default; - using (Stream file = config.FileSystem.OpenRead(filePath)) + Guard.NotNull(configuration, nameof(configuration)); + using (Stream file = configuration.FileSystem.OpenRead(filePath)) { - return Identify(config, file, out format); + return Identify(configuration, file, out format); } } @@ -102,29 +102,30 @@ public static IImageInfo Identify(Configuration config, string filePath, out IIm /// /// Create a new instance of the class from the given file. /// - /// The config for the decoder. + /// The configuration for the decoder. /// The file path to the image. /// /// Thrown if the stream is not readable nor seekable. /// /// The . - public static Image Load(Configuration config, string path) => Load(config, path, out _); + public static Image Load(Configuration configuration, string path) => Load(configuration, path, out _); /// /// Create a new instance of the class from the given file. /// - /// The Configuration. + /// The Configuration. /// The file path to the image. /// The decoder. /// /// Thrown if the stream is not readable nor seekable. /// /// The . - public static Image Load(Configuration config, string path, IImageDecoder decoder) + public static Image Load(Configuration configuration, string path, IImageDecoder decoder) { - using (Stream stream = config.FileSystem.OpenRead(path)) + Guard.NotNull(configuration, nameof(configuration)); + using (Stream stream = configuration.FileSystem.OpenRead(path)) { - return Load(config, stream, decoder); + return Load(configuration, stream, decoder); } } @@ -173,26 +174,27 @@ public static Image Load(string path, out IImageFormat format) /// /// Create a new instance of the class from the given file. /// - /// The configuration options. + /// The configuration options. /// The file path to the image. /// /// Thrown if the stream is not readable nor seekable. /// /// The pixel format. /// A new . - public static Image Load(Configuration config, string path) + public static Image Load(Configuration configuration, string path) where TPixel : unmanaged, IPixel { - using (Stream stream = config.FileSystem.OpenRead(path)) + Guard.NotNull(configuration, nameof(configuration)); + using (Stream stream = configuration.FileSystem.OpenRead(path)) { - return Load(config, stream); + return Load(configuration, stream); } } /// /// Create a new instance of the class from the given file. /// - /// The configuration options. + /// The configuration options. /// The file path to the image. /// The mime type of the decoded image. /// @@ -200,12 +202,13 @@ public static Image Load(Configuration config, string path) /// /// The pixel format. /// A new . - public static Image Load(Configuration config, string path, out IImageFormat format) + public static Image Load(Configuration configuration, string path, out IImageFormat format) where TPixel : unmanaged, IPixel { - using (Stream stream = config.FileSystem.OpenRead(path)) + Guard.NotNull(configuration, nameof(configuration)); + using (Stream stream = configuration.FileSystem.OpenRead(path)) { - return Load(config, stream, out format); + return Load(configuration, stream, out format); } } @@ -213,18 +216,19 @@ public static Image Load(Configuration config, string path, out /// Create a new instance of the class from the given file. /// The pixel type is selected by the decoder. /// - /// The configuration options. + /// The configuration options. /// The file path to the image. /// The mime type of the decoded image. /// /// Thrown if the stream is not readable nor seekable. /// /// A new . - public static Image Load(Configuration config, string path, out IImageFormat format) + public static Image Load(Configuration configuration, string path, out IImageFormat format) { - using (Stream stream = config.FileSystem.OpenRead(path)) + Guard.NotNull(configuration, nameof(configuration)); + using (Stream stream = configuration.FileSystem.OpenRead(path)) { - return Load(config, stream, out format); + return Load(configuration, stream, out format); } } @@ -247,7 +251,7 @@ public static Image Load(string path, IImageDecoder decoder) /// /// Create a new instance of the class from the given file. /// - /// The Configuration. + /// The Configuration. /// The file path to the image. /// The decoder. /// @@ -255,12 +259,13 @@ public static Image Load(string path, IImageDecoder decoder) /// /// The pixel format. /// A new . - public static Image Load(Configuration config, string path, IImageDecoder decoder) + public static Image Load(Configuration configuration, string path, IImageDecoder decoder) where TPixel : unmanaged, IPixel { - using (Stream stream = config.FileSystem.OpenRead(path)) + Guard.NotNull(configuration, nameof(configuration)); + using (Stream stream = configuration.FileSystem.OpenRead(path)) { - return Load(config, stream, decoder); + return Load(configuration, stream, decoder); } } } diff --git a/src/ImageSharp/Image.FromStream.cs b/src/ImageSharp/Image.FromStream.cs index 95a71903a8..52d71409bb 100644 --- a/src/ImageSharp/Image.FromStream.cs +++ b/src/ImageSharp/Image.FromStream.cs @@ -26,12 +26,12 @@ public abstract partial class Image /// /// By reading the header on the provided stream this calculates the images format type. /// - /// The configuration. + /// The configuration. /// The image stream to read the header from. /// Thrown if the stream is not readable. /// The format type or null if none found. - public static IImageFormat DetectFormat(Configuration config, Stream stream) - => WithSeekableStream(config, stream, s => InternalDetectFormat(s, config)); + public static IImageFormat DetectFormat(Configuration configuration, Stream stream) + => WithSeekableStream(configuration, stream, s => InternalDetectFormat(s, configuration)); /// /// Reads the raw image information from the specified stream without fully decoding it. @@ -57,16 +57,16 @@ public static IImageFormat DetectFormat(Configuration config, Stream stream) /// /// Reads the raw image information from the specified stream without fully decoding it. /// - /// The configuration. + /// The configuration. /// The image stream to read the information from. /// The format type of the decoded image. /// 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, out IImageFormat format) + public static IImageInfo Identify(Configuration configuration, Stream stream, out IImageFormat format) { - (IImageInfo info, IImageFormat format) data = WithSeekableStream(config, stream, s => InternalIdentity(s, config ?? Configuration.Default)); + (IImageInfo info, IImageFormat format) data = WithSeekableStream(configuration, stream, s => InternalIdentity(s, configuration ?? Configuration.Default)); format = data.format; return data.info; @@ -108,24 +108,24 @@ public static IImageInfo Identify(Configuration config, Stream stream, out IImag /// Decode a new instance of the class from the given stream. /// The pixel format is selected by the decoder. /// - /// The config for the decoder. + /// The configuration for the decoder. /// The stream containing image information. /// The decoder. /// Thrown if the stream is not readable. /// Image cannot be loaded. /// A new .> - public static Image Load(Configuration config, Stream stream, IImageDecoder decoder) => - WithSeekableStream(config, stream, s => decoder.Decode(config, s)); + public static Image Load(Configuration configuration, Stream stream, IImageDecoder decoder) => + WithSeekableStream(configuration, stream, s => decoder.Decode(configuration, s)); /// /// Decode a new instance of the class from the given stream. /// - /// The config for the decoder. + /// The configuration for the decoder. /// The stream containing image information. /// Thrown if the stream is not readable. /// Image cannot be loaded. /// A new .> - public static Image Load(Configuration config, Stream stream) => Load(config, stream, out _); + public static Image Load(Configuration configuration, Stream stream) => Load(configuration, stream, out _); /// /// Create a new instance of the class from the given stream. @@ -137,7 +137,7 @@ public static Image Load(Configuration config, Stream stream, IImageDecoder deco /// A new .> public static Image Load(Stream stream) where TPixel : unmanaged, IPixel - => Load(null, stream); + => Load(Configuration.Default, stream); /// /// Create a new instance of the class from the given stream. @@ -150,7 +150,7 @@ public static Image Load(Stream stream) /// A new .> public static Image Load(Stream stream, out IImageFormat format) where TPixel : unmanaged, IPixel - => Load(null, stream, out format); + => Load(Configuration.Default, stream, out format); /// /// Create a new instance of the class from the given stream. @@ -168,45 +168,45 @@ public static Image Load(Stream stream, IImageDecoder decoder) /// /// Create a new instance of the class from the given stream. /// - /// The Configuration. + /// The Configuration. /// The stream containing image information. /// The decoder. /// Thrown if the stream is not readable. /// Image cannot be loaded. /// The pixel format. /// A new .> - public static Image Load(Configuration config, Stream stream, IImageDecoder decoder) + public static Image Load(Configuration configuration, Stream stream, IImageDecoder decoder) where TPixel : unmanaged, IPixel - => WithSeekableStream(config, stream, s => decoder.Decode(config, s)); + => WithSeekableStream(configuration, stream, s => decoder.Decode(configuration, s)); /// /// Create a new instance of the class from the given stream. /// - /// The configuration options. + /// The configuration options. /// The stream containing image information. /// Thrown if the stream is not readable. /// Image cannot be loaded. /// The pixel format. /// A new .> - public static Image Load(Configuration config, Stream stream) + public static Image Load(Configuration configuration, Stream stream) where TPixel : unmanaged, IPixel - => Load(config, stream, out IImageFormat _); + => Load(configuration, stream, out IImageFormat _); /// /// Create a new instance of the class from the given stream. /// - /// The configuration options. + /// The configuration options. /// The stream containing image information. /// The format type of the decoded image. /// Thrown if the stream is not readable. /// Image cannot be loaded. /// The pixel format. - /// A new .> - public static Image Load(Configuration config, Stream stream, out IImageFormat format) + /// A new . + public static Image Load(Configuration configuration, Stream stream, out IImageFormat format) where TPixel : unmanaged, IPixel { - config = config ?? Configuration.Default; - (Image img, IImageFormat format) data = WithSeekableStream(config, stream, s => Decode(s, config)); + Guard.NotNull(configuration, nameof(configuration)); + (Image img, IImageFormat format) data = WithSeekableStream(configuration, stream, s => Decode(s, configuration)); format = data.format; @@ -218,7 +218,7 @@ public static Image Load(Configuration config, Stream stream, ou var sb = new StringBuilder(); sb.AppendLine("Image cannot be loaded. Available decoders:"); - foreach (KeyValuePair val in config.ImageFormatsManager.ImageDecoders) + foreach (KeyValuePair val in configuration.ImageFormatsManager.ImageDecoders) { sb.AppendLine($" - {val.Key.Name} : {val.Value.GetType().Name}"); } @@ -230,16 +230,16 @@ public static Image Load(Configuration config, Stream stream, ou /// Decode a new instance of the class from the given stream. /// The pixel format is selected by the decoder. /// - /// The configuration options. + /// The configuration options. /// The stream containing image information. /// The format type of the decoded image. /// Thrown if the stream is not readable. /// Image cannot be loaded. /// A new . - public static Image Load(Configuration config, Stream stream, out IImageFormat format) + public static Image Load(Configuration configuration, Stream stream, out IImageFormat format) { - config = config ?? Configuration.Default; - (Image img, IImageFormat format) data = WithSeekableStream(config, stream, s => Decode(s, config)); + Guard.NotNull(configuration, nameof(configuration)); + (Image img, IImageFormat format) data = WithSeekableStream(configuration, stream, s => Decode(s, configuration)); format = data.format; @@ -251,7 +251,7 @@ public static Image Load(Configuration config, Stream stream, out IImageFormat f var sb = new StringBuilder(); sb.AppendLine("Image cannot be loaded. Available decoders:"); - foreach (KeyValuePair val in config.ImageFormatsManager.ImageDecoders) + foreach (KeyValuePair val in configuration.ImageFormatsManager.ImageDecoders) { sb.AppendLine($" - {val.Key.Name} : {val.Value.GetType().Name}"); } @@ -259,7 +259,7 @@ public static Image Load(Configuration config, Stream stream, out IImageFormat f throw new UnknownImageFormatException(sb.ToString()); } - private static T WithSeekableStream(Configuration config, Stream stream, Func action) + private static T WithSeekableStream(Configuration configuration, Stream stream, Func action) { if (!stream.CanRead) { @@ -268,7 +268,7 @@ private static T WithSeekableStream(Configuration config, Stream stream, Func if (stream.CanSeek) { - if (config.ReadOrigin == ReadOrigin.Begin) + if (configuration.ReadOrigin == ReadOrigin.Begin) { stream.Position = 0; }