Browse Source

normalize IBuffer2D<T> API

pull/607/head
Anton Firszov 8 years ago
parent
commit
de717cf94a
  1. 6
      src/ImageSharp/Advanced/AdvancedImageExtensions.cs
  2. 2
      src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
  3. 2
      src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs
  4. 11
      src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs
  5. 4
      src/ImageSharp/ImageFrame{TPixel}.cs
  6. 13
      src/ImageSharp/Memory/Buffer2DExtensions.cs
  7. 5
      src/ImageSharp/Memory/Buffer2D{T}.cs
  8. 8
      src/ImageSharp/Memory/BufferArea{T}.cs
  9. 2
      src/ImageSharp/Memory/BufferExtensions.cs
  10. 6
      src/ImageSharp/Memory/IBuffer2D{T}.cs
  11. 10
      src/ImageSharp/Memory/IBuffer{T}.cs
  12. 9
      src/ImageSharp/PixelAccessor{TPixel}.cs
  13. 10
      tests/ImageSharp.Tests/Memory/ArrayPoolMemoryManagerTests.cs
  14. 4
      tests/ImageSharp.Tests/Memory/Buffer2DTests.cs
  15. 2
      tests/ImageSharp.Tests/Memory/BufferAreaTests.cs
  16. 2
      tests/ImageSharp.Tests/Memory/BufferTestSuite.cs
  17. 6
      tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs
  18. 4
      tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

6
src/ImageSharp/Advanced/AdvancedImageExtensions.cs

@ -117,7 +117,7 @@ namespace SixLabors.ImageSharp.Advanced
/// <returns>The span retuned from Pixel source</returns>
private static Span<TPixel> GetSpan<TPixel>(IPixelSource<TPixel> source)
where TPixel : struct, IPixel<TPixel>
=> source.PixelBuffer.Span;
=> source.PixelBuffer.GetSpan();
/// <summary>
/// Gets the span to the backing buffer at the given row.
@ -143,7 +143,7 @@ namespace SixLabors.ImageSharp.Advanced
/// </returns>
private static Span<TPixel> GetSpan<TPixel>(Buffer2D<TPixel> source, int row)
where TPixel : struct, IPixel<TPixel>
=> source.Span.Slice(row * source.Width, source.Width);
=> source.GetSpan().Slice(row * source.Width, source.Width);
/// <summary>
/// Gets the configuration.
@ -161,6 +161,6 @@ namespace SixLabors.ImageSharp.Advanced
/// <returns>A reference to the element.</returns>
private static ref TPixel DangerousGetPinnableReferenceToPixelBuffer<TPixel>(IPixelSource<TPixel> source)
where TPixel : struct, IPixel<TPixel>
=> ref MemoryMarshal.GetReference(source.PixelBuffer.Span);
=> ref MemoryMarshal.GetReference(source.PixelBuffer.GetSpan());
}
}

2
src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

@ -215,7 +215,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
using (Buffer2D<byte> buffer = this.memoryManager.AllocateClean2D<byte>(width, height))
{
this.UncompressRle8(width, buffer.Span);
this.UncompressRle8(width, buffer.GetSpan());
for (int y = 0; y < height; y++)
{

2
src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs

@ -136,7 +136,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
public ref Block8x8 GetBlockReference(int column, int row)
{
int offset = ((this.WidthInBlocks + 1) * row) + column;
return ref Unsafe.Add(ref MemoryMarshal.GetReference(this.SpectralBlocks.Span), offset);
return ref Unsafe.Add(ref MemoryMarshal.GetReference(this.SpectralBlocks.GetSpan()), offset);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]

11
src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs

@ -2,13 +2,14 @@
// Licensed under the Apache License, Version 2.0.
using System;
#if DEBUG
using System.Diagnostics;
#endif
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Formats.Jpeg.Components;
using SixLabors.ImageSharp.Memory;
namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
{
@ -166,7 +167,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
if (componentsLength == 1)
{
PdfJsFrameComponent component = components[this.compIndex];
ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast<Block8x8, short>(component.SpectralBlocks.Span));
ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast<Block8x8, short>(component.SpectralBlocks.GetSpan()));
ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId];
ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId];
@ -188,7 +189,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
for (int i = 0; i < componentsLength; i++)
{
PdfJsFrameComponent component = components[i];
ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast<Block8x8, short>(component.SpectralBlocks.Span));
ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast<Block8x8, short>(component.SpectralBlocks.GetSpan()));
ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId];
ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId];
int h = component.HorizontalSamplingFactor;
@ -224,7 +225,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
if (componentsLength == 1)
{
PdfJsFrameComponent component = components[this.compIndex];
ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast<Block8x8, short>(component.SpectralBlocks.Span));
ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast<Block8x8, short>(component.SpectralBlocks.GetSpan()));
ref PdfJsHuffmanTable huffmanTable = ref huffmanTables[isAC ? component.ACHuffmanTableId : component.DCHuffmanTableId];
for (int n = 0; n < this.mcuToRead; n++)
@ -267,7 +268,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
for (int i = 0; i < componentsLength; i++)
{
PdfJsFrameComponent component = components[i];
ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast<Block8x8, short>(component.SpectralBlocks.Span));
ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast<Block8x8, short>(component.SpectralBlocks.GetSpan()));
ref PdfJsHuffmanTable huffmanTable = ref huffmanTables[isAC ? component.ACHuffmanTableId : component.DCHuffmanTableId];
int h = component.HorizontalSamplingFactor;
int v = component.VerticalSamplingFactor;

4
src/ImageSharp/ImageFrame{TPixel}.cs

@ -104,7 +104,7 @@ namespace SixLabors.ImageSharp
this.configuration = configuration;
this.MemoryManager = configuration.MemoryManager;
this.PixelBuffer = this.MemoryManager.Allocate2D<TPixel>(source.PixelBuffer.Width, source.PixelBuffer.Height);
source.PixelBuffer.Span.CopyTo(this.PixelBuffer.Span);
source.PixelBuffer.GetSpan().CopyTo(this.PixelBuffer.GetSpan());
this.MetaData = source.MetaData.Clone();
}
@ -213,7 +213,7 @@ namespace SixLabors.ImageSharp
throw new ArgumentException("ImageFrame<TPixel>.CopyTo(): target must be of the same size!", nameof(target));
}
this.GetPixelSpan().CopyTo(target.Span);
this.GetPixelSpan().CopyTo(target.GetSpan());
}
/// <summary>

13
src/ImageSharp/Memory/Buffer2DExtensions.cs

@ -12,6 +12,15 @@ namespace SixLabors.ImageSharp.Memory
/// </summary>
internal static class Buffer2DExtensions
{
/// <summary>
/// Gets a <see cref="Span{T}"/> to the backing buffer of <paramref name="buffer"/>.
/// </summary>
internal static Span<T> GetSpan<T>(this IBuffer2D<T> buffer)
where T : struct
{
return buffer.Buffer.GetSpan();
}
/// <summary>
/// Gets a <see cref="Span{T}"/> to the row 'y' beginning from the pixel at 'x'.
/// </summary>
@ -24,7 +33,7 @@ namespace SixLabors.ImageSharp.Memory
public static Span<T> GetRowSpan<T>(this IBuffer2D<T> buffer, int x, int y)
where T : struct
{
return buffer.Span.Slice((y * buffer.Width) + x, buffer.Width - x);
return buffer.GetSpan().Slice((y * buffer.Width) + x, buffer.Width - x);
}
/// <summary>
@ -38,7 +47,7 @@ namespace SixLabors.ImageSharp.Memory
public static Span<T> GetRowSpan<T>(this IBuffer2D<T> buffer, int y)
where T : struct
{
return buffer.Span.Slice(y * buffer.Width, buffer.Width);
return buffer.GetSpan().Slice(y * buffer.Width, buffer.Width);
}
/// <summary>

5
src/ImageSharp/Memory/Buffer2D{T}.cs

@ -34,11 +34,6 @@ namespace SixLabors.ImageSharp.Memory
/// <inheritdoc />
public int Height { get; private set; }
/// <summary>
/// Gets the span to the whole area.
/// </summary>
public Span<T> Span => this.Buffer.GetSpan();
/// <summary>
/// Gets the backing <see cref="IBuffer{T}"/>
/// </summary>

8
src/ImageSharp/Memory/BufferArea{T}.cs

@ -71,7 +71,7 @@ namespace SixLabors.ImageSharp.Memory
/// <param name="x">The position inside a row</param>
/// <param name="y">The row index</param>
/// <returns>The reference to the value</returns>
public ref T this[int x, int y] => ref this.DestinationBuffer.Span[this.GetIndexOf(x, y)];
public ref T this[int x, int y] => ref this.DestinationBuffer.GetSpan()[this.GetIndexOf(x, y)];
/// <summary>
/// Gets a reference to the [0,0] element.
@ -79,7 +79,7 @@ namespace SixLabors.ImageSharp.Memory
/// <returns>The reference to the [0,0] element</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T GetReferenceToOrigin() =>
ref this.DestinationBuffer.Span[(this.Rectangle.Y * this.DestinationBuffer.Width) + this.Rectangle.X];
ref this.DestinationBuffer.GetSpan()[(this.Rectangle.Y * this.DestinationBuffer.Width) + this.Rectangle.X];
/// <summary>
/// Gets a span to row 'y' inside this area.
@ -93,7 +93,7 @@ namespace SixLabors.ImageSharp.Memory
int xx = this.Rectangle.X;
int width = this.Rectangle.Width;
return this.DestinationBuffer.Span.Slice(yy + xx, width);
return this.DestinationBuffer.GetSpan().Slice(yy + xx, width);
}
/// <summary>
@ -147,7 +147,7 @@ namespace SixLabors.ImageSharp.Memory
// Optimization for when the size of the area is the same as the buffer size.
if (this.IsFullBufferArea)
{
this.DestinationBuffer.Span.Clear();
this.DestinationBuffer.GetSpan().Clear();
return;
}

2
src/ImageSharp/Memory/BufferExtensions.cs

@ -52,7 +52,7 @@ namespace SixLabors.ImageSharp.Memory
buffer.GetSpan().Clear();
}
public static ref T DangerousGetPinnableReference<T>(this IBuffer<T> buffer)
public static ref T GetReference<T>(this IBuffer<T> buffer)
where T : struct =>
ref MemoryMarshal.GetReference(buffer.GetSpan());

6
src/ImageSharp/Memory/IBuffer2D{T}.cs

@ -6,7 +6,7 @@ using System;
namespace SixLabors.ImageSharp.Memory
{
/// <summary>
/// An interface that represents a pinned buffer of value type objects
/// An interface that represents a contigous buffer of value type objects
/// interpreted as a 2D region of <see cref="Width"/> x <see cref="Height"/> elements.
/// </summary>
/// <typeparam name="T">The value type.</typeparam>
@ -24,8 +24,8 @@ namespace SixLabors.ImageSharp.Memory
int Height { get; }
/// <summary>
/// Gets a <see cref="Span{T}"/> to the backing buffer.
/// Gets the contigous buffer being wrapped.
/// </summary>
Span<T> Span { get; }
IBuffer<T> Buffer { get; }
}
}

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

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

9
src/ImageSharp/PixelAccessor{TPixel}.cs

@ -51,8 +51,7 @@ namespace SixLabors.ImageSharp
/// <inheritdoc />
public int Height { get; private set; }
/// <inheritdoc />
public Span<TPixel> Span => this.PixelBuffer.Span;
public IBuffer<TPixel> Buffer => this.PixelBuffer.Buffer;
/// <summary>
/// Gets or sets the pixel at the specified position.
@ -66,14 +65,14 @@ namespace SixLabors.ImageSharp
get
{
this.CheckCoordinates(x, y);
return this.Span[(y * this.Width) + x];
return this.GetSpan()[(y * this.Width) + x];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
this.CheckCoordinates(x, y);
Span<TPixel> span = this.Span;
Span<TPixel> span = this.GetSpan();
span[(y * this.Width) + x] = value;
}
}
@ -110,7 +109,7 @@ namespace SixLabors.ImageSharp
/// <param name="target">The target pixel buffer accessor.</param>
internal void CopyTo(PixelAccessor<TPixel> target)
{
this.PixelBuffer.Span.CopyTo(target.PixelBuffer.Span);
this.PixelBuffer.GetSpan().CopyTo(target.PixelBuffer.GetSpan());
}
/// <summary>

10
tests/ImageSharp.Tests/Memory/ArrayPoolMemoryManagerTests.cs

@ -31,11 +31,11 @@ namespace SixLabors.ImageSharp.Tests.Memory
where T : struct
{
IBuffer<T> buffer = this.MemoryManager.Allocate<T>(length);
ref T ptrToPrevPosition0 = ref buffer.DangerousGetPinnableReference();
ref T ptrToPrevPosition0 = ref buffer.GetReference();
buffer.Dispose();
buffer = this.MemoryManager.Allocate<T>(length);
bool sameBuffers = Unsafe.AreSame(ref ptrToPrevPosition0, ref buffer.DangerousGetPinnableReference());
bool sameBuffers = Unsafe.AreSame(ref ptrToPrevPosition0, ref buffer.GetReference());
buffer.Dispose();
return sameBuffers;
@ -159,7 +159,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
buffer = this.MemoryManager.Allocate<int>(32);
Assert.False(Unsafe.AreSame(ref ptrToPrev0, ref buffer.DangerousGetPinnableReference()));
Assert.False(Unsafe.AreSame(ref ptrToPrev0, ref buffer.GetReference()));
}
[Fact]
@ -182,12 +182,12 @@ namespace SixLabors.ImageSharp.Tests.Memory
int arrayLengthThreshold = PoolSelectorThresholdInBytes / sizeof(int);
IBuffer<int> small = this.MemoryManager.Allocate<int>(arrayLengthThreshold - 1);
ref int ptr2Small = ref small.DangerousGetPinnableReference();
ref int ptr2Small = ref small.GetReference();
small.Dispose();
IBuffer<int> large = this.MemoryManager.Allocate<int>(arrayLengthThreshold + 1);
Assert.False(Unsafe.AreSame(ref ptr2Small, ref large.DangerousGetPinnableReference()));
Assert.False(Unsafe.AreSame(ref ptr2Small, ref large.GetReference()));
}
[Fact]

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

@ -21,7 +21,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
where T : struct
{
ref T actual = ref MemoryMarshal.GetReference(span);
ref T expected = ref Unsafe.Add(ref buffer.DangerousGetPinnableReference(), bufferOffset);
ref T expected = ref Unsafe.Add(ref buffer.GetReference(), bufferOffset);
Assert.True(Unsafe.AreSame(ref expected, ref actual), "span does not point to the expected position");
}
@ -71,7 +71,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
{
using (Buffer2D<int> buffer = this.MemoryManager.Allocate2D<int>(42, 42, true))
{
Span<int> span = buffer.Span;
Span<int> span = buffer.GetSpan();
for (int j = 0; j < span.Length; j++)
{
Assert.Equal(0, span[j]);

2
tests/ImageSharp.Tests/Memory/BufferAreaTests.cs

@ -117,7 +117,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
using (Buffer2D<int> buffer = CreateTestBuffer(22, 13))
{
buffer.GetArea().Clear();
Span<int> fullSpan = buffer.Span;
Span<int> fullSpan = buffer.GetSpan();
Assert.True(fullSpan.SequenceEqual(new int[fullSpan.Length]));
}
}

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

@ -276,7 +276,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
using (IManagedByteBuffer buffer = this.MemoryManager.AllocateManagedByteBuffer(desiredLength))
{
ref byte array0 = ref buffer.Array[0];
ref byte span0 = ref buffer.DangerousGetPinnableReference();
ref byte span0 = ref buffer.GetReference();
Assert.True(Unsafe.AreSame(ref span0, ref array0));
Assert.True(buffer.Array.Length >= buffer.GetSpan().Length);

6
tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs

@ -35,7 +35,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
using (IBuffer<Bgra32> workBuffer = Configuration.Default.MemoryManager.Allocate<Bgra32>(w))
{
fixed (Bgra32* destPtr = &workBuffer.DangerousGetPinnableReference())
fixed (Bgra32* destPtr = &workBuffer.GetReference())
{
for (int y = 0; y < h; y++)
{
@ -78,7 +78,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
using (IBuffer<Bgr24> workBuffer = Configuration.Default.MemoryManager.Allocate<Bgr24>(w))
{
fixed (Bgr24* destPtr = &workBuffer.DangerousGetPinnableReference())
fixed (Bgr24* destPtr = &workBuffer.GetReference())
{
for (int y = 0; y < h; y++)
{
@ -113,7 +113,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
using (IBuffer<Bgra32> workBuffer = image.GetConfiguration().MemoryManager.Allocate<Bgra32>(w))
{
fixed (Bgra32* sourcePtr = &workBuffer.DangerousGetPinnableReference())
fixed (Bgra32* sourcePtr = &workBuffer.GetReference())
{
for (int y = 0; y < h; y++)

4
tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

@ -33,7 +33,7 @@ namespace SixLabors.ImageSharp.Tests
{
using (Buffer2D<Vector4> temp = memoryManager.Allocate2D<Vector4>(img.Width, img.Height))
{
Span<Vector4> tempSpan = temp.Span;
Span<Vector4> tempSpan = temp.GetSpan();
foreach (ImageFrame<TPixel> frame in img.Frames)
{
Span<TPixel> pixelSpan = frame.GetPixelSpan();
@ -665,7 +665,7 @@ namespace SixLabors.ImageSharp.Tests
Span<Rgba32> pixels = image.Frames.RootFrame.GetPixelSpan();
Span<float> bufferSpan = buffer.Span;
Span<float> bufferSpan = buffer.GetSpan();
for (int i = 0; i < bufferSpan.Length; i++)
{

Loading…
Cancel
Save