/// <summary> /// By reading the header on the provided stream this calculates the images format. /// </summary> /// <param name="stream">The image stream to read the header from.</param> /// <param name="config">The configuration.</param> /// <param name="format">The IImageFormat.</param> /// <returns>The image format or null if none found.</returns> private static IImageDecoder DiscoverDecoder(SpanUnmanagedArray <byte> stream, Configuration config, out IImageFormat format) { format = InternalDetectFormat(stream, config); if (format != null) { return(config.FindDecoder(format)); } return(null); }
/// <summary> /// By reading the header on the provided stream this calculates the images format. /// </summary> /// <param name="stream">The image stream to read the header from.</param> /// <param name="config">The configuration.</param> /// <returns>The mime type or null if none found.</returns> private static IImageFormat InternalDetectFormat(SpanUnmanagedArray <byte> stream, Configuration config) { // This is probably a candidate for making into a public API in the future! int maxHeaderSize = config.MaxHeaderSize; if (maxHeaderSize <= 0) { return(null); } var header = stream.Slice(0, maxHeaderSize); return(config.FormatDetectors.Select(x => x.DetectFormat(header)).LastOrDefault(x => x != null)); }
/// <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> /// <param name="format">the mime type of the decoded image.</param> /// <exception cref="NotSupportedException"> /// Thrown if the stream is not readable nor seekable. /// </exception> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <returns>A new <see cref="Image{TPixel}"/>.</returns>> public static Image <TPixel> Load <TPixel>(Configuration config, SpanUnmanagedArray <byte> stream, out IImageFormat format) where TPixel : struct, IPixel <TPixel> { config = config ?? Configuration.Default; (Image <TPixel> img, IImageFormat format)data = Decode <TPixel>(stream, config); format = data.format; if (data.img != null) { return(data.img); } var stringBuilder = new StringBuilder(); stringBuilder.AppendLine("Image cannot be loaded. Available decoders:"); foreach (KeyValuePair <IImageFormat, IImageDecoder> val in config.ImageDecoders) { stringBuilder.AppendLine($" - {val.Key.Name} : {val.Value.GetType().Name}"); } throw new NotSupportedException(stringBuilder.ToString()); }
/// <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> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <returns>A new <see cref="Image{TPixel}"/>.</returns>> public static Image <TPixel> Load <TPixel>(Configuration config, SpanUnmanagedArray <byte> stream) where TPixel : struct, IPixel <TPixel> { return(Load <TPixel>(config, stream, out var _)); }
/// <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> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <returns>A new <see cref="Image{TPixel}"/>.</returns>> public static Image <TPixel> Load <TPixel>(SpanUnmanagedArray <byte> stream) where TPixel : struct, IPixel <TPixel> { return(Load <TPixel>(null, stream)); }
/// <summary> /// Create a new instance of the <see cref="Image{Rgba32}"/> class from the given stream. /// </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> /// <returns>A new <see cref="Image{Rgba32}"/>.</returns>> public static Image <Rgba32> Load(Configuration config, SpanUnmanagedArray <byte> stream) => Load <Rgba32>(config, 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> /// <returns>The mime type or null if none found.</returns> public static IImageFormat DetectFormat(Configuration config, SpanUnmanagedArray <byte> stream) { return(InternalDetectFormat(stream, config ?? Configuration.Default)); }
/// <summary> /// 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> /// <returns>The mime type or null if none found.</returns> public static IImageFormat DetectFormat(SpanUnmanagedArray <byte> stream) { return(DetectFormat(null, stream)); }