diff --git a/src/ImageSharp/Image.FromBytes.cs b/src/ImageSharp/Image.FromBytes.cs
index 0850e2213f..06b05fe3c4 100644
--- a/src/ImageSharp/Image.FromBytes.cs
+++ b/src/ImageSharp/Image.FromBytes.cs
@@ -26,14 +26,55 @@ 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(configuration, stream);
+ }
+ }
+
+ ///
+ /// 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 configuration, byte[] data, out IImageFormat format)
+ {
+ Guard.NotNull(configuration, nameof(configuration));
+ using (var stream = new MemoryStream(data, 0, data.Length, false, true))
{
- return DetectFormat(config, stream);
+ return Identify(configuration, stream, out format);
}
}
@@ -68,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);
}
}
@@ -117,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);
}
}
@@ -144,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);
@@ -203,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);
}
}
}
@@ -222,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
@@ -237,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);
}
}
}
@@ -245,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
@@ -260,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);
}
}
}
@@ -285,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);
}
}
@@ -348,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)
{
@@ -369,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);
}
}
}
@@ -377,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)
{
@@ -390,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 45ea378cfd..1a9fa15462 100644
--- a/src/ImageSharp/Image.FromFile.cs
+++ b/src/ImageSharp/Image.FromFile.cs
@@ -26,15 +26,55 @@ 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 = 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);
+ }
+ }
+
+ ///
+ /// 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 configuration, string filePath, out IImageFormat format)
+ {
+ Guard.NotNull(configuration, nameof(configuration));
+ using (Stream file = configuration.FileSystem.OpenRead(filePath))
+ {
+ return Identify(configuration, file, out format);
}
}
@@ -62,29 +102,30 @@ public static IImageFormat DetectFormat(Configuration config, string filePath)
///
/// 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);
}
}
@@ -133,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.
///
@@ -160,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);
}
}
@@ -173,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);
}
}
@@ -207,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.
///
@@ -215,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 d756ff7ac1..52d71409bb 100644
--- a/src/ImageSharp/Image.FromStream.cs
+++ b/src/ImageSharp/Image.FromStream.cs
@@ -26,15 +26,15 @@ 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));
///
- /// 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.
@@ -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;
}
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);