From cad87f6cddedcf437214b89bc26f4d4f33559cfb Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Fri, 19 May 2017 13:56:13 +0100 Subject: [PATCH] updated to make better use of Span --- src/ImageSharp/Image/Image.LoadPixelData.cs | 40 +++++-------------- .../PixelFormats/PixelOperations{TPixel}.cs | 22 ---------- 2 files changed, 10 insertions(+), 52 deletions(-) diff --git a/src/ImageSharp/Image/Image.LoadPixelData.cs b/src/ImageSharp/Image/Image.LoadPixelData.cs index fd7979190..75aa318be 100644 --- a/src/ImageSharp/Image/Image.LoadPixelData.cs +++ b/src/ImageSharp/Image/Image.LoadPixelData.cs @@ -14,49 +14,36 @@ namespace ImageSharp using ImageSharp.PixelFormats; /// - /// Adds static methods allowing the creation of new image from a byte array. + /// Adds static methods allowing the creation of new image from raw pixel data. /// public static partial class Image { /// - /// Create a new instance of the class from the given pixel data. + /// Create a new instance of the class from the raw data. /// /// The byte array containing image data. /// The width of the final image. /// The height of the final image. /// The pixel format. /// A new . - public static Image LoadPixelData(TPixel[] data, int width, int height) + public static Image LoadPixelData(Span data, int width, int height) where TPixel : struct, IPixel => LoadPixelData(Configuration.Default, data, width, height); /// - /// Create a new instance of the class from the given pixel data. - /// - /// The config for the decoder. - /// The byte array containing image data. - /// The width of the final image. - /// The height of the final image. - /// The pixel format. - /// A new . - public static Image LoadPixelData(Configuration config, TPixel[] data, int width, int height) - where TPixel : struct, IPixel - => LoadPixelData(config, new Span(data), width, height); - - /// - /// Create a new instance of the class from the given byte array as raw pixel data. + /// Create a new instance of the class from the given byte array in format. /// /// The byte array containing image data. /// The width of the final image. /// The height of the final image. /// The pixel format. /// A new . - public static Image LoadPixelData(byte[] data, int width, int height) + public static Image LoadPixelData(Span data, int width, int height) where TPixel : struct, IPixel => LoadPixelData(Configuration.Default, data, width, height); /// - /// Create a new instance of the class from the given byte array as raw pixel data. + /// Create a new instance of the class from the given byte array in format. /// /// The config for the decoder. /// The byte array containing image data. @@ -64,19 +51,12 @@ namespace ImageSharp /// The height of the final image. /// The pixel format. /// A new . - public static Image LoadPixelData(Configuration config, byte[] data, int width, int height) + public static Image LoadPixelData(Configuration config, Span data, int width, int height) where TPixel : struct, IPixel - { - int size = width * height; - using (var sourceBuffer = new Buffer(size)) - { - PixelOperations.Instance.PackFromRawBytes(new Span(data), sourceBuffer.Span, size); - return LoadPixelData(config, sourceBuffer.Span, width, height); - } - } + => LoadPixelData(config, data.NonPortableCast(), width, height); /// - /// Create a new instance of the class from the given byte array as raw pixel data. + /// Create a new instance of the class from the raw data. /// /// The config for the decoder. /// The Span containing the image Pixel data. @@ -84,7 +64,7 @@ namespace ImageSharp /// The height of the final image. /// The pixel format. /// A new . - private static Image LoadPixelData(Configuration config, Span data, int width, int height) + public static Image LoadPixelData(Configuration config, Span data, int width, int height) where TPixel : struct, IPixel { int count = width * height; diff --git a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs index b29af94ee..993a11232 100644 --- a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs @@ -249,27 +249,5 @@ namespace ImageSharp.PixelFormats sp.ToZyxwBytes(destBytes, i * 4); } } - - /// - /// Bulk conversion of data from series bytes to a series of Pixels. - /// - /// The to the source bytes. - /// The to the destination colors. - /// The number of pixels to convert. - internal virtual void PackFromRawBytes(Span sourceBytes, Span destColors, int count) - { - Guard.MustBeSizedAtLeast(sourceBytes, count * Unsafe.SizeOf(), nameof(sourceBytes)); - Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors)); - - ref TPixel sourceRef = ref Unsafe.As(ref sourceBytes.DangerousGetPinnableReference()); - ref TPixel destRef = ref destColors.DangerousGetPinnableReference(); - - for (int i = 0; i < count; i++) - { - ref TPixel sp = ref Unsafe.Add(ref sourceRef, i); - ref TPixel dp = ref Unsafe.Add(ref destRef, i); - dp = sp; - } - } } } \ No newline at end of file