Browse Source

introduce .GetPixelMemory(), get rid of BufferExtensions.GetMemory()

af/merge-core
Anton Firszov 8 years ago
parent
commit
b32c7849f3
  1. 12
      src/ImageSharp/Advanced/AdvancedImageExtensions.cs
  2. 16
      src/ImageSharp/Memory/BufferExtensions.cs
  3. 7
      src/ImageSharp/Memory/IBuffer{T}.cs
  4. 2
      src/ImageSharp/PixelAccessor{TPixel}.cs
  5. 4
      tests/ImageSharp.Tests/Memory/BufferTestSuite.cs

12
src/ImageSharp/Advanced/AdvancedImageExtensions.cs

@ -23,6 +23,18 @@ namespace SixLabors.ImageSharp.Advanced
where TPixel : struct, IPixel<TPixel>
=> GetConfiguration((IConfigurable)source);
/// <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="ImageFrame{TPixel}"/></param>
/// <returns>The <see cref="Memory{T}"/></returns>
public static Memory<TPixel> GetPixelMemory<TPixel>(this ImageFrame<TPixel> source)
where TPixel : struct, IPixel<TPixel>
{
return source.PixelBuffer.Buffer.Memory;
}
/// <summary>
/// Returns a reference to the 0th element of the Pixel buffer,
/// allowing direct manipulation of pixel data through unsafe operations.

16
src/ImageSharp/Memory/BufferExtensions.cs

@ -10,22 +10,6 @@ namespace SixLabors.ImageSharp.Memory
{
internal static class BufferExtensions
{
public static Memory<T> GetMemory<T>(this IBuffer<T> buffer)
where T : struct
{
System.Buffers.MemoryManager<T> bufferManager = buffer as System.Buffers.MemoryManager<T>;
if (bufferManager == null)
{
// TODO: We need a better way to integrate IBuffer<T> with MemoryManager<T>. The prior should probably entirely replace the latter!
throw new ArgumentException(
"BufferExtensions.GetMemory<T>(buffer): buffer should be convertable to System.Buffers.MemoryManager<T>!",
nameof(buffer));
}
return bufferManager.Memory;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Length<T>(this IBuffer<T> buffer)
where T : struct => buffer.GetSpan().Length;

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

@ -15,9 +15,14 @@ namespace SixLabors.ImageSharp.Memory
where T : struct
{
/// <summary>
/// Gets the span to the memory "promised" by this buffer
/// Gets the span to the memory "promised" by this buffer.
/// </summary>
/// <returns>The <see cref="Span{T}"/></returns>
Span<T> GetSpan();
/// <summary>
/// Gets the <see cref="Memory{T}"/> ownerd by this buffer.
/// </summary>
Memory<T> Memory { get; }
}
}

2
src/ImageSharp/PixelAccessor{TPixel}.cs

@ -54,8 +54,6 @@ namespace SixLabors.ImageSharp
/// <inheritdoc />
public Span<TPixel> Span => this.PixelBuffer.Span;
private static PixelOperations<TPixel> Operations => PixelOperations<TPixel>.Instance;
/// <summary>
/// Gets or sets the pixel at the specified position.
/// </summary>

4
tests/ImageSharp.Tests/Memory/BufferTestSuite.cs

@ -290,7 +290,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
{
Span<CustomStruct> span0 = buffer.GetSpan();
span0[10].A = 30;
Memory<CustomStruct> memory = buffer.GetMemory();
Memory<CustomStruct> memory = buffer.Memory;
Assert.Equal(42, memory.Length);
Span<CustomStruct> span1 = memory.Span;
@ -308,7 +308,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
Span<int> span0 = buffer.GetSpan();
span0[10] = 30;
Memory<int> memory = buffer.GetMemory();
Memory<int> memory = buffer.Memory;
using (MemoryHandle h = memory.Pin())
{

Loading…
Cancel
Save