Browse Source

Merge pull request #220 from JimBobSquarePants/LoadFromPixelData

Load from pixel data methods
pull/230/head
James Jackson-South 9 years ago
committed by GitHub
parent
commit
bf1258dabe
  1. 80
      src/ImageSharp/Image/Image.LoadPixelData.cs
  2. 42
      tests/ImageSharp.Tests/Image/ImageLoadTests.cs

80
src/ImageSharp/Image/Image.LoadPixelData.cs

@ -0,0 +1,80 @@
// <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 raw pixel data.
/// </content>
public static partial class Image
{
/// <summary>
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the raw <typeparamref name="TPixel"/> 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>(Span<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 byte array in <typeparamref name="TPixel"/> format.
/// </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>(Span<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 in <typeparamref name="TPixel"/> format.
/// </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, Span<byte> data, int width, int height)
where TPixel : struct, IPixel<TPixel>
=> LoadPixelData(config, data.NonPortableCast<byte, TPixel>(), width, height);
/// <summary>
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the raw <typeparamref name="TPixel"/> 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>
public 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;
}
}
}

42
tests/ImageSharp.Tests/Image/ImageLoadTests.cs

@ -526,6 +526,48 @@ namespace ImageSharp.Tests
this.localDecoder.Verify(x => x.Decode<Rgba32>(Configuration.Default, this.DataStream, this.decoderOptions));
}
[Fact]
public void LoadFromPixelData_Pixels()
{
var img = Image.LoadPixelData<Rgba32>(new Rgba32[] {
Rgba32.Black, Rgba32.White,
Rgba32.White, Rgba32.Black,
}, 2, 2);
Assert.NotNull(img);
using (var px = img.Lock())
{
Assert.Equal(Rgba32.Black, px[0, 0]);
Assert.Equal(Rgba32.White, px[0, 1]);
Assert.Equal(Rgba32.White, px[1, 0]);
Assert.Equal(Rgba32.Black, px[1, 1]);
}
}
[Fact]
public void LoadFromPixelData_Bytes()
{
var img = Image.LoadPixelData<Rgba32>(new byte[] {
0,0,0,255, // 0,0
255,255,255,255, // 0,1
255,255,255,255, // 1,0
0,0,0,255, // 1,1
}, 2, 2);
Assert.NotNull(img);
using (var px = img.Lock())
{
Assert.Equal(Rgba32.Black, px[0, 0]);
Assert.Equal(Rgba32.White, px[0, 1]);
Assert.Equal(Rgba32.White, px[1, 0]);
Assert.Equal(Rgba32.Black, px[1, 1]);
}
}
public void Dispose()
{
// clean up the global object;

Loading…
Cancel
Save