Browse Source

updated to make better use of Span<T>

af/merge-core
Scott Williams 9 years ago
parent
commit
cad87f6cdd
  1. 40
      src/ImageSharp/Image/Image.LoadPixelData.cs
  2. 22
      src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs

40
src/ImageSharp/Image/Image.LoadPixelData.cs

@ -14,49 +14,36 @@ namespace ImageSharp
using ImageSharp.PixelFormats;
/// <content>
/// 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.
/// </content>
public static partial class Image
{
/// <summary>
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given pixel data.
/// 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>(TPixel[] data, int width, int height)
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 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.
/// 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>(byte[] data, int width, int height)
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 as raw pixel data.
/// 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>
@ -64,19 +51,12 @@ namespace ImageSharp
/// <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)
public static Image<TPixel> LoadPixelData<TPixel>(Configuration config, Span<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);
}
}
=> LoadPixelData(config, data.NonPortableCast<byte, TPixel>(), width, height);
/// <summary>
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given byte array as raw pixel data.
/// 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>
@ -84,7 +64,7 @@ namespace ImageSharp
/// <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)
public static Image<TPixel> LoadPixelData<TPixel>(Configuration config, Span<TPixel> data, int width, int height)
where TPixel : struct, IPixel<TPixel>
{
int count = width * height;

22
src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs

@ -249,27 +249,5 @@ namespace ImageSharp.PixelFormats
sp.ToZyxwBytes(destBytes, i * 4);
}
}
/// <summary>
/// Bulk conversion of data from series bytes to a series of Pixels.
/// </summary>
/// <param name="sourceBytes">The <see cref="Span{T}"/> to the source bytes.</param>
/// <param name="destColors">The <see cref="Span{T}"/> to the destination colors.</param>
/// <param name="count">The number of pixels to convert.</param>
internal virtual void PackFromRawBytes(Span<byte> sourceBytes, Span<TPixel> destColors, int count)
{
Guard.MustBeSizedAtLeast(sourceBytes, count * Unsafe.SizeOf<TPixel>(), nameof(sourceBytes));
Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors));
ref TPixel sourceRef = ref Unsafe.As<byte, TPixel>(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;
}
}
}
}
Loading…
Cancel
Save