Browse Source

Allow returning the image format with the info.

af/merge-core
James Jackson-South 7 years ago
parent
commit
3a9dfdf8b7
  1. 14
      src/ImageSharp/Image.Decode.cs
  2. 39
      src/ImageSharp/Image.FromStream.cs
  3. 33
      tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs

14
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. // Licensed under the Apache License, Version 2.0.
using System.IO; using System.IO;
@ -123,10 +123,14 @@ namespace SixLabors.ImageSharp
/// <returns> /// <returns>
/// The <see cref="IImageInfo"/> or null if suitable info detector not found. /// The <see cref="IImageInfo"/> or null if suitable info detector not found.
/// </returns> /// </returns>
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; if (!(DiscoverDecoder(stream, config, out IImageFormat format) is IImageInfoDetector detector))
return detector?.Identify(config, stream); {
return (null, null);
}
return (detector?.Identify(config, stream), format);
} }
} }
} }

39
src/ImageSharp/Image.FromStream.cs

@ -16,20 +16,20 @@ namespace SixLabors.ImageSharp
public abstract partial class Image public abstract partial class Image
{ {
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <param name="stream">The image stream to read the header from.</param> /// <param name="stream">The image stream to read the header from.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception> /// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <returns>The mime type or null if none found.</returns> /// <returns>The format type or null if none found.</returns>
public static IImageFormat DetectFormat(Stream stream) => DetectFormat(Configuration.Default, stream); public static IImageFormat DetectFormat(Stream stream) => DetectFormat(Configuration.Default, stream);
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <param name="config">The configuration.</param> /// <param name="config">The configuration.</param>
/// <param name="stream">The image stream to read the header from.</param> /// <param name="stream">The image stream to read the header from.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception> /// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <returns>The mime type or null if none found.</returns> /// <returns>The format type or null if none found.</returns>
public static IImageFormat DetectFormat(Configuration config, Stream stream) public static IImageFormat DetectFormat(Configuration config, Stream stream)
=> WithSeekableStream(config, stream, s => InternalDetectFormat(s, config)); => WithSeekableStream(config, stream, s => InternalDetectFormat(s, config));
@ -41,26 +41,43 @@ namespace SixLabors.ImageSharp
/// <returns> /// <returns>
/// The <see cref="IImageInfo"/> or null if suitable info detector not found. /// The <see cref="IImageInfo"/> or null if suitable info detector not found.
/// </returns> /// </returns>
public static IImageInfo Identify(Stream stream) => Identify(Configuration.Default, stream); public static IImageInfo Identify(Stream stream) => Identify(stream, out IImageFormat _);
/// <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>
/// <param name="format">The format type of the decoded image.</param>
/// <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, out IImageFormat format) => Identify(Configuration.Default, stream, out format);
/// <summary> /// <summary>
/// Reads the raw image information from the specified stream without fully decoding it. /// Reads the raw image information from the specified stream without fully decoding it.
/// </summary> /// </summary>
/// <param name="config">The configuration.</param> /// <param name="config">The configuration.</param>
/// <param name="stream">The image stream to read the information from.</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="NotSupportedException">Thrown if the stream is not readable.</exception> /// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <returns> /// <returns>
/// The <see cref="IImageInfo"/> or null if suitable info detector is not found. /// The <see cref="IImageInfo"/> or null if suitable info detector is not found.
/// </returns> /// </returns>
public static IImageInfo Identify(Configuration config, Stream stream) public static IImageInfo Identify(Configuration config, Stream stream, out IImageFormat format)
=> WithSeekableStream(config, stream, s => InternalIdentity(s, config ?? Configuration.Default)); {
(IImageInfo info, IImageFormat format) data = WithSeekableStream(config, stream, s => InternalIdentity(s, config ?? Configuration.Default));
format = data.format;
return data.info;
}
/// <summary> /// <summary>
/// Decode a new instance of the <see cref="Image"/> class from the given stream. /// Decode a new instance of the <see cref="Image"/> class from the given stream.
/// The pixel format is selected by the decoder. /// The pixel format is selected by the decoder.
/// </summary> /// </summary>
/// <param name="stream">The stream containing image information.</param> /// <param name="stream">The stream containing image information.</param>
/// <param name="format">the mime type of the decoded image.</param> /// <param name="format">The format type of the decoded image.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception> /// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception> /// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
/// <returns>A new <see cref="Image"/>.</returns>> /// <returns>A new <see cref="Image"/>.</returns>>
@ -126,7 +143,7 @@ namespace SixLabors.ImageSharp
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream. /// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
/// </summary> /// </summary>
/// <param name="stream">The stream containing image information.</param> /// <param name="stream">The stream containing image information.</param>
/// <param name="format">the mime type of the decoded image.</param> /// <param name="format">The format type of the decoded image.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception> /// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception> /// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
/// <typeparam name="TPixel">The pixel format.</typeparam> /// <typeparam name="TPixel">The pixel format.</typeparam>
@ -180,7 +197,7 @@ namespace SixLabors.ImageSharp
/// </summary> /// </summary>
/// <param name="config">The configuration options.</param> /// <param name="config">The configuration options.</param>
/// <param name="stream">The stream containing image information.</param> /// <param name="stream">The stream containing image information.</param>
/// <param name="format">the mime type of the decoded image.</param> /// <param name="format">The format type of the decoded image.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception> /// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception> /// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
/// <typeparam name="TPixel">The pixel format.</typeparam> /// <typeparam name="TPixel">The pixel format.</typeparam>
@ -215,7 +232,7 @@ namespace SixLabors.ImageSharp
/// </summary> /// </summary>
/// <param name="config">The configuration options.</param> /// <param name="config">The configuration options.</param>
/// <param name="stream">The stream containing image information.</param> /// <param name="stream">The stream containing image information.</param>
/// <param name="format">the mime type of the decoded image.</param> /// <param name="format">The format type of the decoded image.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception> /// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception> /// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
/// <returns>A new <see cref="Image{TPixel}"/>.</returns> /// <returns>A new <see cref="Image{TPixel}"/>.</returns>

33
tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs

@ -3,9 +3,6 @@
using System.IO; using System.IO;
using SixLabors.ImageSharp.Formats; 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.Formats.Png;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using Xunit; using Xunit;
@ -13,6 +10,7 @@ using Xunit;
namespace SixLabors.ImageSharp.Tests namespace SixLabors.ImageSharp.Tests
{ {
using System; using System;
using System.Linq;
using System.Reflection; using System.Reflection;
using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Quantization; using SixLabors.ImageSharp.Processing.Processors.Quantization;
@ -167,38 +165,35 @@ namespace SixLabors.ImageSharp.Tests
[InlineData(100, 100, "jpg")] [InlineData(100, 100, "jpg")]
[InlineData(100, 10, "jpg")] [InlineData(100, 10, "jpg")]
[InlineData(10, 100, "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 image = Image.LoadPixelData(new Rgba32[width * height], width, height))
{ {
using (var memoryStream = new MemoryStream()) using (var memoryStream = new MemoryStream())
{ {
image.Save(memoryStream, GetEncoder(format)); IImageFormat format = GetFormat(extension);
image.Save(memoryStream, format);
memoryStream.Position = 0; memoryStream.Position = 0;
IImageInfo imageInfo = Image.Identify(memoryStream); IImageInfo imageInfo = Image.Identify(memoryStream);
Assert.Equal(imageInfo.Width, width); Assert.Equal(imageInfo.Width, width);
Assert.Equal(imageInfo.Height, height); 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) return Configuration.Default.ImageFormats.FirstOrDefault(x => x.FileExtensions.Contains(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);
}
} }
} }
} }

Loading…
Cancel
Save