Browse Source

ReadOnlySpan<byte> overloads for Image.DetectFormat()

af/merge-core
Anton Firszov 8 years ago
parent
commit
6e80236178
  1. 27
      src/ImageSharp/Image.FromBytes.cs
  2. 23
      tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs

27
src/ImageSharp/Image.FromBytes.cs

@ -175,6 +175,33 @@ namespace SixLabors.ImageSharp
#if !NETSTANDARD1_1
/// <summary>
/// By reading the header on the provided byte array this calculates the images format.
/// </summary>
/// <param name="data">The byte array containing encoded image data to read the header from.</param>
/// <returns>The format or null if none found.</returns>
public static IImageFormat DetectFormat(ReadOnlySpan<byte> data)
{
return DetectFormat(Configuration.Default, data);
}
/// <summary>
/// By reading the header on the provided byte array this calculates the images format.
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="data">The byte array containing encoded image data to read the header from.</param>
/// <returns>The mime type or null if none found.</returns>
public static unsafe IImageFormat DetectFormat(Configuration config, ReadOnlySpan<byte> data)
{
fixed (byte* ptr = &data.GetPinnableReference())
{
using (var stream = new UnmanagedMemoryStream(ptr, data.Length))
{
return DetectFormat(config, stream);
}
}
}
/// <summary>
/// Load a new instance of <see cref="Image{Rgba32}"/> from the given encoded byte span.
/// </summary>

23
tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs

@ -33,18 +33,27 @@ namespace SixLabors.ImageSharp.Tests
private static readonly IImageFormat ExpectedGlobalFormat =
Configuration.Default.ImageFormatsManager.FindFormatByFileExtension("bmp");
[Fact]
public void FromBytes_GlobalConfiguration()
[Theory]
[InlineData(false)]
[InlineData(true)]
public void FromBytes_GlobalConfiguration(bool useSpan)
{
IImageFormat type = Image.DetectFormat(this.ActualImageBytes);
IImageFormat type = useSpan
? Image.DetectFormat(this.ActualImageSpan)
: Image.DetectFormat(this.ActualImageBytes);
Assert.Equal(ExpectedGlobalFormat, type);
}
[Fact]
public void FromBytes_CustomConfiguration()
[Theory]
[InlineData(false)]
[InlineData(true)]
public void FromBytes_CustomConfiguration(bool useSpan)
{
IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.ByteArray);
IImageFormat type = useSpan
? Image.DetectFormat(this.LocalConfiguration, this.ByteArray.AsSpan())
: Image.DetectFormat(this.LocalConfiguration, this.ByteArray);
Assert.Equal(this.LocalImageFormat, type);
}

Loading…
Cancel
Save