From 31b77898552b0018ec40c8d01152ef182c9bfd7e Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 17 Jun 2018 20:53:21 +0200 Subject: [PATCH] reference UnmanagedMemoryStream and implement ReadOnlySpan overloads of Image.Load() --- src/ImageSharp/Image.FromBytes.cs | 39 ++++++++++++++++++++++++++----- src/ImageSharp/ImageSharp.csproj | 3 +++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/ImageSharp/Image.FromBytes.cs b/src/ImageSharp/Image.FromBytes.cs index 7e55fe787..8365dbd50 100644 --- a/src/ImageSharp/Image.FromBytes.cs +++ b/src/ImageSharp/Image.FromBytes.cs @@ -173,6 +173,8 @@ namespace SixLabors.ImageSharp } } +#if !NETSTANDARD1_1 + /// /// Load a new instance of from the given encoded byte span. /// @@ -205,10 +207,16 @@ namespace SixLabors.ImageSharp /// The byte span containing encoded image data. /// The pixel format. /// A new . - public static Image Load(Configuration config, ReadOnlySpan data) + public static unsafe Image Load(Configuration config, ReadOnlySpan data) where TPixel : struct, IPixel { - throw new NotImplementedException(); + fixed (byte* ptr = &data.GetPinnableReference()) + { + using (var stream = new UnmanagedMemoryStream(ptr, data.Length)) + { + return Load(config, stream); + } + } } /// @@ -219,10 +227,19 @@ namespace SixLabors.ImageSharp /// The decoder. /// The pixel format. /// A new . - public static Image Load(Configuration config, ReadOnlySpan data, IImageDecoder decoder) + public static unsafe Image Load( + Configuration config, + ReadOnlySpan data, + IImageDecoder decoder) where TPixel : struct, IPixel { - throw new NotImplementedException(); + fixed (byte* ptr = &data.GetPinnableReference()) + { + using (var stream = new UnmanagedMemoryStream(ptr, data.Length)) + { + return Load(config, stream, decoder); + } + } } /// @@ -233,10 +250,20 @@ namespace SixLabors.ImageSharp /// The of the decoded image. /// The pixel format. /// A new . - public static Image Load(Configuration config, ReadOnlySpan data, out IImageFormat format) + public static unsafe Image Load( + Configuration config, + ReadOnlySpan data, + out IImageFormat format) where TPixel : struct, IPixel { - throw new NotImplementedException(); + fixed (byte* ptr = &data.GetPinnableReference()) + { + using (var stream = new UnmanagedMemoryStream(ptr, data.Length)) + { + return Load(config, stream, out format); + } + } } +#endif } } \ No newline at end of file diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index b1934faa6..9860486e3 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -44,6 +44,9 @@ + + +