|
|
|
@ -19,62 +19,48 @@ namespace SixLabors.ImageSharp |
|
|
|
/// By reading the header on the provided stream this calculates the images mime type.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="stream">The image stream to read the header from.</param>
|
|
|
|
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
|
|
|
|
/// <returns>The mime type or null if none found.</returns>
|
|
|
|
public static IImageFormat DetectFormat(Stream stream) |
|
|
|
{ |
|
|
|
return DetectFormat(null, stream); |
|
|
|
} |
|
|
|
public static IImageFormat DetectFormat(Stream stream) => DetectFormat(Configuration.Default, stream); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// By reading the header on the provided stream this calculates the images mime type.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="config">The configuration.</param>
|
|
|
|
/// <param name="stream">The image stream to read the header from.</param>
|
|
|
|
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
|
|
|
|
/// <returns>The mime type or null if none found.</returns>
|
|
|
|
public static IImageFormat DetectFormat(Configuration config, Stream stream) |
|
|
|
{ |
|
|
|
return WithSeekableStream(stream, s => InternalDetectFormat(s, config ?? Configuration.Default)); |
|
|
|
} |
|
|
|
=> WithSeekableStream(config, stream, s => InternalDetectFormat(s, config)); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// By reading the header on the provided stream this reads the raw image information.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="stream">The image stream to read the header from.</param>
|
|
|
|
/// <exception cref="NotSupportedException">
|
|
|
|
/// Thrown if the stream is not readable nor seekable.
|
|
|
|
/// </exception>
|
|
|
|
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
|
|
|
|
/// <returns>
|
|
|
|
/// The <see cref="IImageInfo"/> or null if suitable info detector not found.
|
|
|
|
/// </returns>
|
|
|
|
public static IImageInfo Identify(Stream stream) |
|
|
|
{ |
|
|
|
return Identify(null, stream); |
|
|
|
} |
|
|
|
public static IImageInfo Identify(Stream stream) => Identify(Configuration.Default, stream); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Reads the raw image information from the specified stream without fully decoding it.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="config">The configuration.</param>
|
|
|
|
/// <param name="stream">The image stream to read the information from.</param>
|
|
|
|
/// <exception cref="NotSupportedException">
|
|
|
|
/// Thrown if the stream is not readable nor seekable.
|
|
|
|
/// </exception>
|
|
|
|
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
|
|
|
|
/// <returns>
|
|
|
|
/// The <see cref="IImageInfo"/> or null if suitable info detector is not found.
|
|
|
|
/// </returns>
|
|
|
|
public static IImageInfo Identify(Configuration config, Stream stream) |
|
|
|
{ |
|
|
|
return WithSeekableStream(stream, s => InternalIdentity(s, config ?? Configuration.Default)); |
|
|
|
} |
|
|
|
=> WithSeekableStream(config, stream, s => InternalIdentity(s, config ?? Configuration.Default)); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Create a new instance of the <see cref="Image{Rgba32}"/> class from the given stream.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
|
|
/// <param name="format">the mime type of the decoded image.</param>
|
|
|
|
/// <exception cref="NotSupportedException">
|
|
|
|
/// Thrown if the stream is not readable nor seekable.
|
|
|
|
/// </exception>
|
|
|
|
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
|
|
|
|
/// <returns>A new <see cref="Image{Rgba32}"/>.</returns>>
|
|
|
|
public static Image<Rgba32> Load(Stream stream, out IImageFormat format) => Load<Rgba32>(stream, out format); |
|
|
|
|
|
|
|
@ -82,9 +68,7 @@ namespace SixLabors.ImageSharp |
|
|
|
/// Create a new instance of the <see cref="Image{Rgba32}"/> class from the given stream.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
|
|
/// <exception cref="NotSupportedException">
|
|
|
|
/// Thrown if the stream is not readable nor seekable.
|
|
|
|
/// </exception>
|
|
|
|
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
|
|
|
|
/// <returns>A new <see cref="Image{Rgba32}"/>.</returns>>
|
|
|
|
public static Image<Rgba32> Load(Stream stream) => Load<Rgba32>(stream); |
|
|
|
|
|
|
|
@ -93,9 +77,7 @@ namespace SixLabors.ImageSharp |
|
|
|
/// </summary>
|
|
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
|
|
/// <param name="decoder">The decoder.</param>
|
|
|
|
/// <exception cref="NotSupportedException">
|
|
|
|
/// Thrown if the stream is not readable nor seekable.
|
|
|
|
/// </exception>
|
|
|
|
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
|
|
|
|
/// <returns>A new <see cref="Image{Rgba32}"/>.</returns>>
|
|
|
|
public static Image<Rgba32> Load(Stream stream, IImageDecoder decoder) => Load<Rgba32>(stream, decoder); |
|
|
|
|
|
|
|
@ -104,9 +86,7 @@ namespace SixLabors.ImageSharp |
|
|
|
/// </summary>
|
|
|
|
/// <param name="config">The config for the decoder.</param>
|
|
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
|
|
/// <exception cref="NotSupportedException">
|
|
|
|
/// Thrown if the stream is not readable nor seekable.
|
|
|
|
/// </exception>
|
|
|
|
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
|
|
|
|
/// <returns>A new <see cref="Image{Rgba32}"/>.</returns>>
|
|
|
|
public static Image<Rgba32> Load(Configuration config, Stream stream) => Load<Rgba32>(config, stream); |
|
|
|
|
|
|
|
@ -116,58 +96,45 @@ namespace SixLabors.ImageSharp |
|
|
|
/// <param name="config">The config for the decoder.</param>
|
|
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
|
|
/// <param name="format">the mime type of the decoded image.</param>
|
|
|
|
/// <exception cref="NotSupportedException">
|
|
|
|
/// Thrown if the stream is not readable nor seekable.
|
|
|
|
/// </exception>
|
|
|
|
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
|
|
|
|
/// <returns>A new <see cref="Image{Rgba32}"/>.</returns>>
|
|
|
|
public static Image<Rgba32> Load(Configuration config, Stream stream, out IImageFormat format) => Load<Rgba32>(config, stream, out format); |
|
|
|
public static Image<Rgba32> Load(Configuration config, Stream stream, out IImageFormat format) |
|
|
|
=> Load<Rgba32>(config, stream, out format); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
|
|
/// <exception cref="NotSupportedException">
|
|
|
|
/// Thrown if the stream is not readable nor seekable.
|
|
|
|
/// </exception>
|
|
|
|
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
|
|
|
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
|
|
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
|
|
|
|
public static Image<TPixel> Load<TPixel>(Stream stream) |
|
|
|
where TPixel : struct, IPixel<TPixel> |
|
|
|
{ |
|
|
|
return Load<TPixel>(null, stream); |
|
|
|
} |
|
|
|
=> Load<TPixel>(null, stream); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
|
|
/// <param name="format">the mime type of the decoded image.</param>
|
|
|
|
/// <exception cref="NotSupportedException">
|
|
|
|
/// Thrown if the stream is not readable nor seekable.
|
|
|
|
/// </exception>
|
|
|
|
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
|
|
|
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
|
|
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
|
|
|
|
public static Image<TPixel> Load<TPixel>(Stream stream, out IImageFormat format) |
|
|
|
where TPixel : struct, IPixel<TPixel> |
|
|
|
{ |
|
|
|
return Load<TPixel>(null, stream, out format); |
|
|
|
} |
|
|
|
=> Load<TPixel>(null, stream, out format); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
|
|
/// <param name="decoder">The decoder.</param>
|
|
|
|
/// <exception cref="NotSupportedException">
|
|
|
|
/// Thrown if the stream is not readable nor seekable.
|
|
|
|
/// </exception>
|
|
|
|
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
|
|
|
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
|
|
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
|
|
|
|
public static Image<TPixel> Load<TPixel>(Stream stream, IImageDecoder decoder) |
|
|
|
where TPixel : struct, IPixel<TPixel> |
|
|
|
{ |
|
|
|
return WithSeekableStream(stream, s => decoder.Decode<TPixel>(Configuration.Default, s)); |
|
|
|
} |
|
|
|
=> WithSeekableStream(Configuration.Default, stream, s => decoder.Decode<TPixel>(Configuration.Default, s)); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
|
|
|
|
@ -175,32 +142,24 @@ namespace SixLabors.ImageSharp |
|
|
|
/// <param name="config">The Configuration.</param>
|
|
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
|
|
/// <param name="decoder">The decoder.</param>
|
|
|
|
/// <exception cref="NotSupportedException">
|
|
|
|
/// Thrown if the stream is not readable nor seekable.
|
|
|
|
/// </exception>
|
|
|
|
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
|
|
|
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
|
|
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
|
|
|
|
public static Image<TPixel> Load<TPixel>(Configuration config, Stream stream, IImageDecoder decoder) |
|
|
|
where TPixel : struct, IPixel<TPixel> |
|
|
|
{ |
|
|
|
return WithSeekableStream(stream, s => decoder.Decode<TPixel>(config, s)); |
|
|
|
} |
|
|
|
=> WithSeekableStream(config, stream, s => decoder.Decode<TPixel>(config, s)); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="config">The configuration options.</param>
|
|
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
|
|
/// <exception cref="NotSupportedException">
|
|
|
|
/// Thrown if the stream is not readable nor seekable.
|
|
|
|
/// </exception>
|
|
|
|
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
|
|
|
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
|
|
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
|
|
|
|
public static Image<TPixel> Load<TPixel>(Configuration config, Stream stream) |
|
|
|
where TPixel : struct, IPixel<TPixel> |
|
|
|
{ |
|
|
|
return Load<TPixel>(config, stream, out var _); |
|
|
|
} |
|
|
|
=> Load<TPixel>(config, stream, out IImageFormat _); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
|
|
|
|
@ -208,16 +167,14 @@ namespace SixLabors.ImageSharp |
|
|
|
/// <param name="config">The configuration options.</param>
|
|
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
|
|
/// <param name="format">the mime type of the decoded image.</param>
|
|
|
|
/// <exception cref="NotSupportedException">
|
|
|
|
/// Thrown if the stream is not readable nor seekable.
|
|
|
|
/// </exception>
|
|
|
|
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
|
|
|
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
|
|
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
|
|
|
|
public static Image<TPixel> Load<TPixel>(Configuration config, Stream stream, out IImageFormat format) |
|
|
|
where TPixel : struct, IPixel<TPixel> |
|
|
|
where TPixel : struct, IPixel<TPixel> |
|
|
|
{ |
|
|
|
config = config ?? Configuration.Default; |
|
|
|
(Image<TPixel> img, IImageFormat format) data = WithSeekableStream(stream, s => Decode<TPixel>(s, config)); |
|
|
|
(Image<TPixel> img, IImageFormat format) data = WithSeekableStream(config, stream, s => Decode<TPixel>(s, config)); |
|
|
|
|
|
|
|
format = data.format; |
|
|
|
|
|
|
|
@ -237,7 +194,7 @@ namespace SixLabors.ImageSharp |
|
|
|
throw new NotSupportedException(stringBuilder.ToString()); |
|
|
|
} |
|
|
|
|
|
|
|
private static T WithSeekableStream<T>(Stream stream, Func<Stream, T> action) |
|
|
|
private static T WithSeekableStream<T>(Configuration config, Stream stream, Func<Stream, T> action) |
|
|
|
{ |
|
|
|
if (!stream.CanRead) |
|
|
|
{ |
|
|
|
@ -246,6 +203,11 @@ namespace SixLabors.ImageSharp |
|
|
|
|
|
|
|
if (stream.CanSeek) |
|
|
|
{ |
|
|
|
if (config.ReadOrigin == ReadOrigin.Begin) |
|
|
|
{ |
|
|
|
stream.Position = 0; |
|
|
|
} |
|
|
|
|
|
|
|
return action(stream); |
|
|
|
} |
|
|
|
|
|
|
|
|