diff --git a/src/ImageSharp/Image.Decode.cs b/src/ImageSharp/Image.Decode.cs
index 8d0df599ea..4521864c36 100644
--- a/src/ImageSharp/Image.Decode.cs
+++ b/src/ImageSharp/Image.Decode.cs
@@ -1,4 +1,4 @@
-// Copyright (c) Six Labors and contributors.
+// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System.IO;
@@ -123,10 +123,14 @@ namespace SixLabors.ImageSharp
///
/// The or null if suitable info detector not found.
///
- private static IImageInfo InternalIdentity(Stream stream, Configuration config)
+ private static (IImageInfo info, IImageFormat format) InternalIdentity(Stream stream, Configuration config)
{
- var detector = DiscoverDecoder(stream, config, out IImageFormat _) as IImageInfoDetector;
- return detector?.Identify(config, stream);
+ if (!(DiscoverDecoder(stream, config, out IImageFormat format) is IImageInfoDetector detector))
+ {
+ return (null, null);
+ }
+
+ return (detector?.Identify(config, stream), format);
}
}
-}
\ No newline at end of file
+}
diff --git a/src/ImageSharp/Image.FromStream.cs b/src/ImageSharp/Image.FromStream.cs
index c4336c9aca..60db45f215 100644
--- a/src/ImageSharp/Image.FromStream.cs
+++ b/src/ImageSharp/Image.FromStream.cs
@@ -16,20 +16,20 @@ namespace SixLabors.ImageSharp
public abstract partial class Image
{
///
- /// By reading the header on the provided stream this calculates the images mime type.
+ /// By reading the header on the provided stream this calculates the images format type.
///
/// The image stream to read the header from.
/// Thrown if the stream is not readable.
- /// The mime type or null if none found.
+ /// The format type or null if none found.
public static IImageFormat DetectFormat(Stream stream) => DetectFormat(Configuration.Default, stream);
///
- /// By reading the header on the provided stream this calculates the images mime type.
+ /// By reading the header on the provided stream this calculates the images format type.
///
/// 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.
+ /// The format type or null if none found.
public static IImageFormat DetectFormat(Configuration config, Stream stream)
=> WithSeekableStream(config, stream, s => InternalDetectFormat(s, config));
@@ -41,26 +41,43 @@ namespace SixLabors.ImageSharp
///
/// The or null if suitable info detector not found.
///
- public static IImageInfo Identify(Stream stream) => Identify(Configuration.Default, 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.
+ ///
+ /// The image stream 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(Stream stream, out IImageFormat format) => Identify(Configuration.Default, stream, out format);
///
/// Reads the raw image information from the specified stream without fully decoding it.
///
/// 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)
- => WithSeekableStream(config, stream, s => InternalIdentity(s, config ?? Configuration.Default));
+ public static IImageInfo Identify(Configuration config, Stream stream, out IImageFormat format)
+ {
+ (IImageInfo info, IImageFormat format) data = WithSeekableStream(config, stream, s => InternalIdentity(s, config ?? Configuration.Default));
+
+ format = data.format;
+ return data.info;
+ }
///
/// Decode a new instance of the class from the given stream.
/// The pixel format is selected by the decoder.
///
/// The stream containing image information.
- /// the mime type of the decoded image.
+ /// The format type of the decoded image.
/// Thrown if the stream is not readable.
/// Image cannot be loaded.
/// A new .>
@@ -126,7 +143,7 @@ namespace SixLabors.ImageSharp
/// Create a new instance of the class from the given stream.
///
/// The stream containing image information.
- /// the mime type of the decoded image.
+ /// The format type of the decoded image.
/// Thrown if the stream is not readable.
/// Image cannot be loaded.
/// The pixel format.
@@ -180,7 +197,7 @@ namespace SixLabors.ImageSharp
///
/// The configuration options.
/// The stream containing image information.
- /// the mime type of the decoded image.
+ /// The format type of the decoded image.
/// Thrown if the stream is not readable.
/// Image cannot be loaded.
/// The pixel format.
@@ -215,7 +232,7 @@ namespace SixLabors.ImageSharp
///
/// The configuration options.
/// The stream containing image information.
- /// the mime type of the decoded image.
+ /// The format type of the decoded image.
/// Thrown if the stream is not readable.
/// Image cannot be loaded.
/// A new .
diff --git a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs
index 62e9acf747..835dcb3203 100644
--- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs
+++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs
@@ -3,9 +3,6 @@
using System.IO;
using SixLabors.ImageSharp.Formats;
-using SixLabors.ImageSharp.Formats.Bmp;
-using SixLabors.ImageSharp.Formats.Gif;
-using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using Xunit;
@@ -13,6 +10,7 @@ using Xunit;
namespace SixLabors.ImageSharp.Tests
{
using System;
+ using System.Linq;
using System.Reflection;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Quantization;
@@ -167,38 +165,35 @@ namespace SixLabors.ImageSharp.Tests
[InlineData(100, 100, "jpg")]
[InlineData(100, 10, "jpg")]
[InlineData(10, 100, "jpg")]
- public void CanIdentifyImageLoadedFromBytes(int width, int height, string format)
+ [InlineData(100, 100, "tga")]
+ [InlineData(100, 10, "tga")]
+ [InlineData(10, 100, "tga")]
+ public void CanIdentifyImageLoadedFromBytes(int width, int height, string extension)
{
using (var image = Image.LoadPixelData(new Rgba32[width * height], width, height))
{
using (var memoryStream = new MemoryStream())
{
- image.Save(memoryStream, GetEncoder(format));
+ IImageFormat format = GetFormat(extension);
+ image.Save(memoryStream, format);
memoryStream.Position = 0;
IImageInfo imageInfo = Image.Identify(memoryStream);
Assert.Equal(imageInfo.Width, width);
Assert.Equal(imageInfo.Height, height);
+ memoryStream.Position = 0;
+
+ imageInfo = Image.Identify(memoryStream, out IImageFormat detectedFormat);
+
+ Assert.Equal(format, detectedFormat);
}
}
}
- private static IImageEncoder GetEncoder(string format)
+ private static IImageFormat GetFormat(string format)
{
- switch (format)
- {
- case "png":
- return new PngEncoder();
- case "gif":
- return new GifEncoder();
- case "bmp":
- return new BmpEncoder();
- case "jpg":
- return new JpegEncoder();
- default:
- throw new ArgumentOutOfRangeException(nameof(format), format, null);
- }
+ return Configuration.Default.ImageFormats.FirstOrDefault(x => x.FileExtensions.Contains(format));
}
}
}