Browse Source

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

pull/607/head
Anton Firszov 8 years ago
parent
commit
8457372718
  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> where TPixel : struct, IPixel<TPixel>
=> GetConfiguration((IConfigurable)source); => 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> /// <summary>
/// Returns a reference to the 0th element of the Pixel buffer, /// Returns a reference to the 0th element of the Pixel buffer,
/// allowing direct manipulation of pixel data through unsafe operations. /// 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 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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Length<T>(this IBuffer<T> buffer) public static int Length<T>(this IBuffer<T> buffer)
where T : struct => buffer.GetSpan().Length; where T : struct => buffer.GetSpan().Length;

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

@ -15,9 +15,14 @@ namespace SixLabors.ImageSharp.Memory
where T : struct where T : struct
{ {
/// <summary> /// <summary>
/// Gets the span to the memory "promised" by this buffer /// Gets the span to the memory "promised" by this buffer.
/// </summary> /// </summary>
/// <returns>The <see cref="Span{T}"/></returns> /// <returns>The <see cref="Span{T}"/></returns>
Span<T> GetSpan(); 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 /> /// <inheritdoc />
public Span<TPixel> Span => this.PixelBuffer.Span; public Span<TPixel> Span => this.PixelBuffer.Span;
private static PixelOperations<TPixel> Operations => PixelOperations<TPixel>.Instance;
/// <summary> /// <summary>
/// Gets or sets the pixel at the specified position. /// Gets or sets the pixel at the specified position.
/// </summary> /// </summary>

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

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

Loading…
Cancel
Save