/// <summary> /// Saves the raw image to the given bytes. /// </summary> /// <typeparam name="TPixel">The Pixel format.</typeparam> /// <param name="source">The source image</param> /// <param name="buffer">The buffer to save the raw pixel data to.</param> /// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception> internal static void SavePixelData <TPixel>(this ImageFrame <TPixel> source, Span <TPixel> buffer) where TPixel : struct, IPixel <TPixel> { Span <TPixel> sourceBuffer = source.GetPixelSpan(); Guard.MustBeGreaterThanOrEqualTo(buffer.Length, sourceBuffer.Length, nameof(buffer)); sourceBuffer.CopyTo(buffer); }
/// <summary> /// Create a new instance of the <see cref="Image{TPixel}"/> class from the raw <typeparamref name="TPixel"/> data. /// </summary> /// <param name="data">The Span containing the image Pixel data.</param> /// <param name="width">The width of the final image.</param> /// <param name="height">The height of the final image.</param> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <returns>A new <see cref="Image{TPixel}"/>.</returns> public static ImageFrame <TPixel> LoadPixelData <TPixel>(Span <TPixel> data, int width, int height) where TPixel : struct, IPixel <TPixel> { int count = width * height; Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data)); var image = new ImageFrame <TPixel>(width, height); SpanHelper.Copy(data, image.GetPixelSpan(), count); return(image); }
/// <summary> /// Create a new instance of the <see cref="Image{TPixel}"/> class from the raw <typeparamref name="TPixel"/> data. /// </summary> /// <param name="configuration">The configuration which allows altering default behaviour or extending the library.</param> /// <param name="data">The Span containing the image Pixel data.</param> /// <param name="width">The width of the final image.</param> /// <param name="height">The height of the final image.</param> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <returns>A new <see cref="Image{TPixel}"/>.</returns> internal static ImageFrame <TPixel> LoadPixelData <TPixel>(Configuration configuration, ReadOnlySpan <TPixel> data, int width, int height) where TPixel : struct, IPixel <TPixel> { int count = width * height; Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data)); var image = new ImageFrame <TPixel>(configuration, width, height); data.Slice(0, count).CopyTo(image.GetPixelSpan()); return(image); }
/// <summary> /// Create a new instance of the <see cref="Image{TPixel}"/> class from the raw <typeparamref name="TPixel"/> data. /// </summary> /// <param name="memoryManager">The memory manager to use for allocations</param> /// <param name="data">The Span containing the image Pixel data.</param> /// <param name="width">The width of the final image.</param> /// <param name="height">The height of the final image.</param> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <returns>A new <see cref="Image{TPixel}"/>.</returns> public static ImageFrame <TPixel> LoadPixelData <TPixel>(MemoryManager memoryManager, Span <TPixel> data, int width, int height) where TPixel : struct, IPixel <TPixel> { int count = width * height; Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data)); var image = new ImageFrame <TPixel>(memoryManager, width, height); data.Slice(0, count).CopyTo(image.GetPixelSpan()); return(image); }
/// <summary> /// Saves the raw image pixels to a byte array in row-major order. /// </summary> /// <typeparam name="TPixel">The Pixel format.</typeparam> /// <param name="source">The source image</param> /// <returns>A copy of the pixel data as bytes from this frame.</returns> /// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception> public static byte[] SavePixelData <TPixel>(this ImageFrame <TPixel> source) where TPixel : struct, IPixel <TPixel> => source.GetPixelSpan().AsBytes().ToArray();