Skip to content

Commit

Permalink
Merge pull request #671 from SixLabors/js/remove-allocaton
Browse files Browse the repository at this point in the history
Use span directly for DetectFormat
  • Loading branch information
JimBobSquarePants authored Aug 8, 2018
2 parents afa2a1b + 37503b3 commit dcdb9db
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/ImageSharp/Image.FromBytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.IO;
using System.Linq;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;

Expand Down Expand Up @@ -193,13 +194,24 @@ public static IImageFormat DetectFormat(ReadOnlySpan<byte> data)
/// <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())
int maxHeaderSize = config.MaxHeaderSize;
if (maxHeaderSize <= 0)
{
using (var stream = new UnmanagedMemoryStream(ptr, data.Length))
return null;
}

IImageFormat format = default;
foreach (IImageFormatDetector detector in config.ImageFormatsManager.FormatDetectors)
{
IImageFormat f = detector.DetectFormat(data);

if (f != null)
{
return DetectFormat(config, stream);
format = f;
}
}

return format;
}

/// <summary>
Expand Down

0 comments on commit dcdb9db

Please sign in to comment.