mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
345 lines
19 KiB
345 lines
19 KiB
// Copyright (c) Six Labors and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using SixLabors.ImageSharp.Formats;
|
|
using SixLabors.ImageSharp.IO;
|
|
using SixLabors.ImageSharp.PixelFormats;
|
|
|
|
namespace SixLabors.ImageSharp
|
|
{
|
|
/// <content>
|
|
/// Adds static methods allowing the creation of new image from a given stream.
|
|
/// </content>
|
|
public abstract partial class Image
|
|
{
|
|
/// <summary>
|
|
/// By reading the header on the provided stream this calculates the images format type.
|
|
/// </summary>
|
|
/// <param name="stream">The image stream to read the header from.</param>
|
|
/// <exception cref="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <returns>The format type or null if none found.</returns>
|
|
public static IImageFormat DetectFormat(Stream stream)
|
|
=> DetectFormat(Configuration.Default, stream);
|
|
|
|
/// <summary>
|
|
/// By reading the header on the provided stream this calculates the images format type.
|
|
/// </summary>
|
|
/// <param name="configuration">The configuration.</param>
|
|
/// <param name="stream">The image stream to read the header from.</param>
|
|
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
|
|
/// <exception cref="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <returns>The format type or null if none found.</returns>
|
|
public static IImageFormat DetectFormat(Configuration configuration, Stream stream)
|
|
=> WithSeekableStream(configuration, stream, false, s => InternalDetectFormat(s, configuration));
|
|
|
|
/// <summary>
|
|
/// Reads the raw image information from the specified stream without fully decoding it.
|
|
/// </summary>
|
|
/// <param name="stream">The image stream to read the header from.</param>
|
|
/// <exception cref="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
|
|
/// <returns>
|
|
/// The <see cref="IImageInfo"/> or null if suitable info detector not found.
|
|
/// </returns>
|
|
public static IImageInfo Identify(Stream stream)
|
|
=> Identify(stream, out IImageFormat _);
|
|
|
|
/// <summary>
|
|
/// Reads the raw image information from the specified stream without fully decoding it.
|
|
/// </summary>
|
|
/// <param name="stream">The image stream to read the header from.</param>
|
|
/// <param name="format">The format type of the decoded image.</param>
|
|
/// <exception cref="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
|
|
/// <returns>
|
|
/// The <see cref="IImageInfo"/> or null if suitable info detector not found.
|
|
/// </returns>
|
|
public static IImageInfo Identify(Stream stream, out IImageFormat format)
|
|
=> Identify(Configuration.Default, stream, out format);
|
|
|
|
/// <summary>
|
|
/// Reads the raw image information from the specified stream without fully decoding it.
|
|
/// </summary>
|
|
/// <param name="configuration">The configuration.</param>
|
|
/// <param name="stream">The image stream to read the information from.</param>
|
|
/// <param name="format">The format type of the decoded image.</param>
|
|
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
|
|
/// <exception cref="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
|
|
/// <returns>
|
|
/// The <see cref="IImageInfo"/> or null if suitable info detector is not found.
|
|
/// </returns>
|
|
public static IImageInfo Identify(Configuration configuration, Stream stream, out IImageFormat format)
|
|
{
|
|
(IImageInfo info, IImageFormat format) data = WithSeekableStream(configuration, stream, false, s => InternalIdentity(s, configuration ?? Configuration.Default));
|
|
|
|
format = data.format;
|
|
return data.info;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decode a new instance of the <see cref="Image"/> class from the given stream.
|
|
/// The pixel format is selected by the decoder.
|
|
/// </summary>
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
/// <param name="format">The format type of the decoded image.</param>
|
|
/// <exception cref="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
|
|
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
|
|
/// <returns>The <see cref="Image"/>.</returns>
|
|
public static Image Load(Stream stream, out IImageFormat format)
|
|
=> Load(Configuration.Default, stream, out format);
|
|
|
|
/// <summary>
|
|
/// Decode a new instance of the <see cref="Image"/> class from the given stream.
|
|
/// The pixel format is selected by the decoder.
|
|
/// </summary>
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
/// <exception cref="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
|
|
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
|
|
/// <returns>The <see cref="Image"/>.</returns>
|
|
public static Image Load(Stream stream) => Load(Configuration.Default, stream);
|
|
|
|
/// <summary>
|
|
/// Decode a new instance of the <see cref="Image"/> class from the given stream.
|
|
/// The pixel format is selected by the decoder.
|
|
/// </summary>
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
/// <param name="decoder">The decoder.</param>
|
|
/// <exception cref="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="ArgumentNullException">The decoder is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
|
|
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
|
|
/// <returns>The <see cref="Image"/>.</returns>
|
|
public static Image Load(Stream stream, IImageDecoder decoder)
|
|
=> Load(Configuration.Default, stream, decoder);
|
|
|
|
/// <summary>
|
|
/// Decode a new instance of the <see cref="Image"/> class from the given stream.
|
|
/// The pixel format is selected by the decoder.
|
|
/// </summary>
|
|
/// <param name="configuration">The configuration for the decoder.</param>
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
/// <param name="decoder">The decoder.</param>
|
|
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
|
|
/// <exception cref="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="ArgumentNullException">The decoder is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
|
|
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
|
|
/// <returns>A new <see cref="Image"/>.</returns>>
|
|
public static Image Load(Configuration configuration, Stream stream, IImageDecoder decoder)
|
|
=> WithSeekableStream(configuration, stream, true, s => decoder.Decode(configuration, s));
|
|
|
|
/// <summary>
|
|
/// Decode a new instance of the <see cref="Image"/> class from the given stream.
|
|
/// </summary>
|
|
/// <param name="configuration">The configuration for the decoder.</param>
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
|
|
/// <exception cref="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
|
|
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
|
|
/// <returns>A new <see cref="Image"/>.</returns>>
|
|
public static Image Load(Configuration configuration, Stream stream)
|
|
=> Load(configuration, stream, out _);
|
|
|
|
/// <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="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
|
|
/// <exception cref="InvalidImageContentException">Image contains invalid content.</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 : unmanaged, IPixel<TPixel>
|
|
=> Load<TPixel>(Configuration.Default, 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 format type of the decoded image.</param>
|
|
/// <exception cref="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
|
|
/// <exception cref="InvalidImageContentException">Image contains invalid content.</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 : unmanaged, IPixel<TPixel>
|
|
=> Load<TPixel>(Configuration.Default, 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="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
|
|
/// <exception cref="InvalidImageContentException">Image contains invalid content.</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 : unmanaged, IPixel<TPixel>
|
|
=> WithSeekableStream(Configuration.Default, stream, true, s => decoder.Decode<TPixel>(Configuration.Default, s));
|
|
|
|
/// <summary>
|
|
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
|
|
/// </summary>
|
|
/// <param name="configuration">The Configuration.</param>
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
/// <param name="decoder">The decoder.</param>
|
|
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
|
|
/// <exception cref="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
|
|
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
|
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
|
|
public static Image<TPixel> Load<TPixel>(Configuration configuration, Stream stream, IImageDecoder decoder)
|
|
where TPixel : unmanaged, IPixel<TPixel>
|
|
=> WithSeekableStream(configuration, stream, true, s => decoder.Decode<TPixel>(configuration, s));
|
|
|
|
/// <summary>
|
|
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
|
|
/// </summary>
|
|
/// <param name="configuration">The configuration options.</param>
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
|
|
/// <exception cref="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
|
|
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
|
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
|
|
public static Image<TPixel> Load<TPixel>(Configuration configuration, Stream stream)
|
|
where TPixel : unmanaged, IPixel<TPixel>
|
|
=> Load<TPixel>(configuration, stream, out IImageFormat _);
|
|
|
|
/// <summary>
|
|
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
|
|
/// </summary>
|
|
/// <param name="configuration">The configuration options.</param>
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
/// <param name="format">The format type of the decoded image.</param>
|
|
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
|
|
/// <exception cref="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
|
|
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
|
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
|
|
public static Image<TPixel> Load<TPixel>(Configuration configuration, Stream stream, out IImageFormat format)
|
|
where TPixel : unmanaged, IPixel<TPixel>
|
|
{
|
|
(Image<TPixel> img, IImageFormat format) data = WithSeekableStream(configuration, stream, true, s => Decode<TPixel>(s, configuration));
|
|
|
|
format = data.format;
|
|
|
|
if (data.img != null)
|
|
{
|
|
return data.img;
|
|
}
|
|
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine("Image cannot be loaded. Available decoders:");
|
|
|
|
foreach (KeyValuePair<IImageFormat, IImageDecoder> val in configuration.ImageFormatsManager.ImageDecoders)
|
|
{
|
|
sb.AppendFormat(" - {0} : {1}{2}", val.Key.Name, val.Value.GetType().Name, Environment.NewLine);
|
|
}
|
|
|
|
throw new UnknownImageFormatException(sb.ToString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decode a new instance of the <see cref="Image"/> class from the given stream.
|
|
/// The pixel format is selected by the decoder.
|
|
/// </summary>
|
|
/// <param name="configuration">The configuration options.</param>
|
|
/// <param name="stream">The stream containing image information.</param>
|
|
/// <param name="format">The format type of the decoded image.</param>
|
|
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
|
|
/// <exception cref="ArgumentNullException">The stream is null.</exception>
|
|
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
|
|
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
|
|
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
|
|
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
|
|
public static Image Load(Configuration configuration, Stream stream, out IImageFormat format)
|
|
{
|
|
(Image img, IImageFormat format) data = WithSeekableStream(configuration, stream, true, s => Decode(s, configuration));
|
|
|
|
format = data.format;
|
|
|
|
if (data.img != null)
|
|
{
|
|
return data.img;
|
|
}
|
|
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine("Image cannot be loaded. Available decoders:");
|
|
|
|
foreach (KeyValuePair<IImageFormat, IImageDecoder> val in configuration.ImageFormatsManager.ImageDecoders)
|
|
{
|
|
sb.AppendFormat(" - {0} : {1}{2}", val.Key.Name, val.Value.GetType().Name, Environment.NewLine);
|
|
}
|
|
|
|
throw new UnknownImageFormatException(sb.ToString());
|
|
}
|
|
|
|
private static T WithSeekableStream<T>(Configuration configuration, Stream stream, bool buffer, Func<Stream, T> action)
|
|
{
|
|
Guard.NotNull(configuration, nameof(configuration));
|
|
Guard.NotNull(stream, nameof(stream));
|
|
|
|
if (!stream.CanRead)
|
|
{
|
|
throw new NotSupportedException("Cannot read from the stream.");
|
|
}
|
|
|
|
if (stream.CanSeek)
|
|
{
|
|
if (configuration.ReadOrigin == ReadOrigin.Begin)
|
|
{
|
|
stream.Position = 0;
|
|
}
|
|
|
|
if (buffer)
|
|
{
|
|
using var bufferedStream = new BufferedReadStream(stream);
|
|
return action(bufferedStream);
|
|
}
|
|
|
|
return action(stream);
|
|
}
|
|
|
|
// We want to be able to load images from things like HttpContext.Request.Body
|
|
using (var memoryStream = new MemoryStream())
|
|
{
|
|
stream.CopyTo(memoryStream);
|
|
memoryStream.Position = 0;
|
|
|
|
return action(memoryStream);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|