Browse Source

GetPixelMemory() tests

pull/607/head
Anton Firszov 8 years ago
parent
commit
141d39c319
  1. 12
      src/ImageSharp/Advanced/AdvancedImageExtensions.cs
  2. 3
      src/ImageSharp/Memory/ArrayPoolMemoryManager.Buffer{T}.cs
  3. 2
      src/ImageSharp/Memory/BasicArrayBuffer.cs
  4. 12
      src/ImageSharp/Memory/IBuffer{T}.cs
  5. 26
      tests/ImageSharp.Tests/Advanced/AdvancedImageExtensionsTests.cs

12
src/ImageSharp/Advanced/AdvancedImageExtensions.cs

@ -35,6 +35,18 @@ namespace SixLabors.ImageSharp.Advanced
return source.PixelBuffer.Buffer.Memory;
}
/// <summary>
/// Gets the <see cref="Memory{T}"/> storing the whole pixel buffer in row major order.
/// </summary>
/// <typeparam name="TPixel">The Pixel format.</typeparam>
/// <param name="source">The source <see cref="Image{TPixel}"/></param>
/// <returns>The <see cref="Memory{T}"/></returns>
public static Memory<TPixel> GetPixelMemory<TPixel>(this Image<TPixel> source)
where TPixel : struct, IPixel<TPixel>
{
return source.Frames.RootFrame.GetPixelMemory();
}
/// <summary>
/// Returns a reference to the 0th element of the Pixel buffer,
/// allowing direct manipulation of pixel data through unsafe operations.

3
src/ImageSharp/Memory/ArrayPoolMemoryManager.Buffer{T}.cs

@ -13,7 +13,8 @@ namespace SixLabors.ImageSharp.Memory
public partial class ArrayPoolMemoryManager
{
/// <summary>
/// The buffer implementation of <see cref="ArrayPoolMemoryManager"/>
/// The buffer implementation of <see cref="ArrayPoolMemoryManager"/>.
/// In this implementation <see cref="IBuffer{T}.Memory"/> is owned.
/// </summary>
private class Buffer<T> : ManagedBufferBase<T>, IBuffer<T>
where T : struct

2
src/ImageSharp/Memory/BasicArrayBuffer.cs

@ -5,7 +5,7 @@ using System.Runtime.CompilerServices;
namespace SixLabors.ImageSharp.Memory
{
/// <summary>
/// Exposes an array through the <see cref="IBuffer{T}"/> interface.
/// Wraps an array as an <see cref="IBuffer{T}"/> instance. In this implementation <see cref="IBuffer{T}.Memory"/> is owned.
/// </summary>
internal class BasicArrayBuffer<T> : ManagedBufferBase<T>, IBuffer<T>
where T : struct

12
src/ImageSharp/Memory/IBuffer{T}.cs

@ -5,22 +5,24 @@ using System;
namespace SixLabors.ImageSharp.Memory
{
/// <inheritdoc />
/// <summary>
/// Represents a contigous memory buffer of value-type items "promising" a <see cref="Span{T}"/>
/// A proper im
/// Represents a contigous memory buffer of value-type items.
/// Depending on it's implementation, an <see cref="IBuffer{T}"/> can (1) OWN or (2) CONSUME the <see cref="Memory{T}"/> instance it wraps.
/// For a deeper understanding of the owner/consumer model, read the following docs: <br/>
/// https://gist.github.com/GrabYourPitchforks/4c3e1935fd4d9fa2831dbfcab35dffc6
/// </summary>
/// <typeparam name="T">The value type</typeparam>
internal interface IBuffer<T> : IDisposable
where T : struct
{
/// <summary>
/// Gets the <see cref="Memory{T}"/> ownerd by this buffer.
/// Gets the <see cref="Memory{T}"/> ownerd/consumed by this buffer.
/// </summary>
Memory<T> Memory { get; }
/// <summary>
/// Gets the span to the memory "promised" by this buffer.
/// Gets the span to the memory "promised" by this buffer when it's OWNED (1).
/// Gets `this.Memory.Span` when the buffer CONSUMED (2).
/// </summary>
/// <returns>The <see cref="Span{T}"/></returns>
Span<T> GetSpan();

26
tests/ImageSharp.Tests/Advanced/AdvancedImageExtensionsTests.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
@ -8,8 +9,33 @@ using Xunit;
namespace SixLabors.ImageSharp.Tests.Advanced
{
public class AdvancedImageExtensionsTests
{
public class GetPixelMemory
{
[Theory]
[WithSolidFilledImages(1, 1, "Red", PixelTypes.Rgba32)]
[WithTestPatternImages(131, 127, PixelTypes.Rgba32 | PixelTypes.Bgr24)]
public void WhenMemoryIsOwned<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
Memory<TPixel> memory = image.GetPixelMemory();
Assert.Equal(image.Width * image.Height, memory.Length);
var targetBuffer = new TPixel[image.Width * image.Height];
memory.Span.CopyTo(targetBuffer);
image.ComparePixelBufferTo(targetBuffer);
}
}
}
[Theory]
[WithTestPatternImages(131, 127, PixelTypes.Rgba32 | PixelTypes.Bgr24)]
public unsafe void DangerousGetPinnableReference_CopyToBuffer<TPixel>(TestImageProvider<TPixel> provider)

Loading…
Cancel
Save