Browse Source

BufferSpan ref indexer + Fix Drawing usages

pull/141/head
Anton Firszov 9 years ago
parent
commit
e2d780107c
  1. 2
      src/ImageSharp.Drawing/Brushes/ImageBrush{TColor}.cs
  2. 2
      src/ImageSharp.Drawing/Brushes/PatternBrush{TColor}.cs
  3. 2
      src/ImageSharp.Drawing/Brushes/Processors/BrushApplicator.cs
  4. 2
      src/ImageSharp.Drawing/Brushes/RecolorBrush{TColor}.cs
  5. 2
      src/ImageSharp.Drawing/Brushes/SolidBrush{TColor}.cs
  6. 17
      src/ImageSharp/Common/Memory/BufferSpan{T}.cs
  7. 46
      tests/ImageSharp.Tests/Common/BufferSpanTests.cs

2
src/ImageSharp.Drawing/Brushes/ImageBrush{TColor}.cs

@ -119,7 +119,7 @@ namespace ImageSharp.Drawing.Brushes
using (PinnedBuffer<float> buffer = new PinnedBuffer<float>(scanlineBuffer))
{
BufferPointer<float> slice = buffer.Slice(offset);
BufferSpan<float> slice = buffer.Slice(offset);
for (int xPos = 0; xPos < scanlineWidth; xPos++)
{

2
src/ImageSharp.Drawing/Brushes/PatternBrush{TColor}.cs

@ -152,7 +152,7 @@ namespace ImageSharp.Drawing.Brushes
using (PinnedBuffer<float> buffer = new PinnedBuffer<float>(scanlineBuffer))
{
BufferPointer<float> slice = buffer.Slice(offset);
BufferSpan<float> slice = buffer.Slice(offset);
for (int xPos = 0; xPos < scanlineWidth; xPos++)
{

2
src/ImageSharp.Drawing/Brushes/Processors/BrushApplicator.cs

@ -57,7 +57,7 @@ namespace ImageSharp.Drawing.Processors
using (PinnedBuffer<float> buffer = new PinnedBuffer<float>(scanlineBuffer))
{
BufferPointer<float> slice = buffer.Slice(offset);
BufferSpan<float> slice = buffer.Slice(offset);
for (int xPos = 0; xPos < scanlineWidth; xPos++)
{

2
src/ImageSharp.Drawing/Brushes/RecolorBrush{TColor}.cs

@ -143,7 +143,7 @@ namespace ImageSharp.Drawing.Brushes
using (PinnedBuffer<float> buffer = new PinnedBuffer<float>(scanlineBuffer))
{
BufferPointer<float> slice = buffer.Slice(offset);
BufferSpan<float> slice = buffer.Slice(offset);
for (int xPos = 0; xPos < scanlineWidth; xPos++)
{

2
src/ImageSharp.Drawing/Brushes/SolidBrush{TColor}.cs

@ -91,7 +91,7 @@ namespace ImageSharp.Drawing.Brushes
using (PinnedBuffer<float> buffer = new PinnedBuffer<float>(scanlineBuffer))
{
BufferPointer<float> slice = buffer.Slice(offset);
BufferSpan<float> slice = buffer.Slice(offset);
for (int xPos = 0; xPos < scanlineWidth; xPos++)
{

17
src/ImageSharp/Common/Memory/BufferSpan{T}.cs

@ -106,6 +106,23 @@ namespace ImageSharp
/// </summary>
public IntPtr PointerAtOffset { get; private set; }
/// <summary>
/// Returns a reference to specified element of the span.
/// </summary>
/// <param name="index">The index</param>
/// <returns>The reference to the specified element</returns>
public ref T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
DebugGuard.MustBeLessThan(index, this.Length, nameof(index));
byte* ptr = (byte*)this.PointerAtOffset + BufferSpan.SizeOf<T>(index);
return ref Unsafe.AsRef<T>(ptr);
}
}
/// <summary>
/// Convertes <see cref="BufferSpan{T}"/> instance to a raw 'void*' pointer
/// </summary>

46
tests/ImageSharp.Tests/Common/BufferSpanTests.cs

@ -209,6 +209,51 @@ namespace ImageSharp.Tests.Common
}
public class Indexer
{
public static readonly TheoryData<int, int, int> IndexerData =
new TheoryData<int, int, int>()
{
{ 10, 0, 0 },
{ 10, 2, 0 },
{ 16, 0, 3 },
{ 16, 2, 3 },
{ 10, 0, 9 },
{ 10, 1, 8 }
};
[Theory]
[MemberData(nameof(IndexerData))]
public void Read(int length, int start, int index)
{
Foo[] a = Foo.CreateArray(length);
fixed (Foo* p = a)
{
BufferSpan<Foo> span = new BufferSpan<Foo>(a, p, start);
Foo element = span[index];
Assert.Equal(a[start + index], element);
}
}
[Theory]
[MemberData(nameof(IndexerData))]
public void Write(int length, int start, int index)
{
Foo[] a = Foo.CreateArray(length);
fixed (Foo* p = a)
{
BufferSpan<Foo> span = new BufferSpan<Foo>(a, p, start);
span[index] = new Foo(666, 666);
Assert.Equal(new Foo(666, 666), a[start + index]);
}
}
}
public class Copy
{
private static void AssertNotDefault<T>(T[] data, int idx)
@ -443,6 +488,7 @@ namespace ImageSharp.Tests.Common
}
}
internal static bool ElementsAreEqual(Foo[] array, byte[] rawArray, int index)
{
fixed (Foo* pArray = array)

Loading…
Cancel
Save