mirror of https://github.com/SixLabors/ImageSharp
3 changed files with 164 additions and 0 deletions
@ -0,0 +1,100 @@ |
|||
// <copyright file="Image.LoadPixelData.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp |
|||
{ |
|||
using System; |
|||
using System.IO; |
|||
using System.Runtime.CompilerServices; |
|||
using System.Threading.Tasks; |
|||
using Formats; |
|||
using ImageSharp.Memory; |
|||
using ImageSharp.PixelFormats; |
|||
|
|||
/// <content>
|
|||
/// Adds static methods allowing the creation of new image from a byte array.
|
|||
/// </content>
|
|||
public static partial class Image |
|||
{ |
|||
/// <summary>
|
|||
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given pixel data.
|
|||
/// </summary>
|
|||
/// <param name="data">The byte array containing image 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 Image<TPixel> LoadPixelData<TPixel>(TPixel[] data, int width, int height) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
=> LoadPixelData(Configuration.Default, data, width, height); |
|||
|
|||
/// <summary>
|
|||
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given pixel data.
|
|||
/// </summary>
|
|||
/// <param name="config">The config for the decoder.</param>
|
|||
/// <param name="data">The byte array containing image 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 Image<TPixel> LoadPixelData<TPixel>(Configuration config, TPixel[] data, int width, int height) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
=> LoadPixelData(config, new Span<TPixel>(data), width, height); |
|||
|
|||
/// <summary>
|
|||
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given byte array as raw pixel data.
|
|||
/// </summary>
|
|||
/// <param name="data">The byte array containing image 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 Image<TPixel> LoadPixelData<TPixel>(byte[] data, int width, int height) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
=> LoadPixelData<TPixel>(Configuration.Default, data, width, height); |
|||
|
|||
/// <summary>
|
|||
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given byte array as raw pixel data.
|
|||
/// </summary>
|
|||
/// <param name="config">The config for the decoder.</param>
|
|||
/// <param name="data">The byte array containing image 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 Image<TPixel> LoadPixelData<TPixel>(Configuration config, byte[] data, int width, int height) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
int size = width * height; |
|||
using (var sourceBuffer = new Buffer<TPixel>(size)) |
|||
{ |
|||
PixelOperations<TPixel>.Instance.PackFromRawBytes(new Span<byte>(data), sourceBuffer.Span, size); |
|||
return LoadPixelData(config, sourceBuffer.Span, width, height); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given byte array as raw pixel data.
|
|||
/// </summary>
|
|||
/// <param name="config">The config for the decoder.</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>
|
|||
private static Image<TPixel> LoadPixelData<TPixel>(Configuration config, Span<TPixel> data, int width, int height) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
int count = width * height; |
|||
Guard.MustBeGreaterThanOrEqualTo(data.Length, width * height, nameof(data)); |
|||
var image = new Image<TPixel>(config, width, height); |
|||
var dest = new Span<TPixel>(image.Pixels, 0, count); |
|||
|
|||
SpanHelper.Copy(data, dest, count); |
|||
|
|||
return image; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue