mirror of https://github.com/SixLabors/ImageSharp
10 changed files with 355 additions and 59 deletions
@ -0,0 +1,31 @@ |
|||||
|
// <copyright file="IPinnedImageBuffer{T}.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// An interface that represents a pinned 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>
|
||||
|
internal interface IPinnedImageBuffer<T> |
||||
|
where T : struct |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Gets the width.
|
||||
|
/// </summary>
|
||||
|
int Width { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the height.
|
||||
|
/// </summary>
|
||||
|
int Height { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets a <see cref="BufferSpan{T}"/> to the backing buffer.
|
||||
|
/// </summary>
|
||||
|
BufferSpan<T> Span { get; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,45 @@ |
|||||
|
// <copyright file="PinnedImageBufferExtensions{T}.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Defines extension methods for <see cref="IPinnedImageBuffer{T}"/>.
|
||||
|
/// </summary>
|
||||
|
internal static class PinnedImageBufferExtensions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Gets a <see cref="BufferSpan{T}"/> to the row 'y' beginning from the pixel at 'x'.
|
||||
|
/// </summary>
|
||||
|
/// <param name="buffer">The buffer</param>
|
||||
|
/// <param name="x">The x coordinate (position in the row)</param>
|
||||
|
/// <param name="y">The y (row) coordinate</param>
|
||||
|
/// <typeparam name="T">The element type</typeparam>
|
||||
|
/// <returns>The <see cref="BufferSpan{T}"/></returns>
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static BufferSpan<T> GetRowSpan<T>(this IPinnedImageBuffer<T> buffer, int x, int y) |
||||
|
where T : struct |
||||
|
{ |
||||
|
return buffer.Span.Slice((y * buffer.Width) + x, buffer.Width - x); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets a <see cref="BufferSpan{T}"/> to the row 'y' beginning from the pixel at 'x'.
|
||||
|
/// </summary>
|
||||
|
/// <param name="buffer">The buffer</param>
|
||||
|
/// <param name="y">The y (row) coordinate</param>
|
||||
|
/// <typeparam name="T">The element type</typeparam>
|
||||
|
/// <returns>The <see cref="BufferSpan{T}"/></returns>
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static BufferSpan<T> GetRowSpan<T>(this IPinnedImageBuffer<T> buffer, int y) |
||||
|
where T : struct |
||||
|
{ |
||||
|
return buffer.Span.Slice(y * buffer.Width, buffer.Width); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,62 @@ |
|||||
|
// <copyright file="PinnedImageBuffer{T}.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp |
||||
|
{ |
||||
|
using System; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Represents a pinned 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>
|
||||
|
internal class PinnedImageBuffer<T> : PinnedBuffer<T>, IPinnedImageBuffer<T> |
||||
|
where T : struct |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="PinnedImageBuffer{T}"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="width">The number of elements in a row</param>
|
||||
|
/// <param name="height">The number of rows</param>
|
||||
|
public PinnedImageBuffer(int width, int height) |
||||
|
: base(width * height) |
||||
|
{ |
||||
|
this.Width = width; |
||||
|
this.Height = height; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="PinnedImageBuffer{T}"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="array">The array to pin</param>
|
||||
|
/// <param name="width">The number of elements in a row</param>
|
||||
|
/// <param name="height">The number of rows</param>
|
||||
|
public PinnedImageBuffer(T[] array, int width, int height) |
||||
|
: base(array, width * height) |
||||
|
{ |
||||
|
this.Width = width; |
||||
|
this.Height = height; |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc />
|
||||
|
public int Width { get; } |
||||
|
|
||||
|
/// <inheritdoc />
|
||||
|
public int Height { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Creates a clean instance of <see cref="PinnedImageBuffer{T}"/> initializing it's elements with 'default(T)'.
|
||||
|
/// </summary>
|
||||
|
/// <param name="width">The number of elements in a row</param>
|
||||
|
/// <param name="height">The number of rows</param>
|
||||
|
/// <returns>The <see cref="PinnedBuffer{T}"/> instance</returns>
|
||||
|
public static PinnedImageBuffer<T> CreateClean(int width, int height) |
||||
|
{ |
||||
|
PinnedImageBuffer<T> buffer = new PinnedImageBuffer<T>(width, height); |
||||
|
buffer.Clear(); |
||||
|
return buffer; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,86 @@ |
|||||
|
// ReSharper disable InconsistentNaming
|
||||
|
namespace ImageSharp.Tests.Common |
||||
|
{ |
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
using Xunit; |
||||
|
|
||||
|
public unsafe class PinnedImageBufferTests |
||||
|
{ |
||||
|
[Theory] |
||||
|
[InlineData(7, 42)] |
||||
|
[InlineData(1025, 17)] |
||||
|
public void Construct(int width, int height) |
||||
|
{ |
||||
|
using (PinnedImageBuffer<int> buffer = new PinnedImageBuffer<int>(width, height)) |
||||
|
{ |
||||
|
Assert.Equal(width, buffer.Width); |
||||
|
Assert.Equal(height, buffer.Height); |
||||
|
Assert.Equal(width * height, buffer.Length); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(7, 42)] |
||||
|
[InlineData(1025, 17)] |
||||
|
public void Construct_FromExternalArray(int width, int height) |
||||
|
{ |
||||
|
int[] array = new int[width * height + 10]; |
||||
|
using (PinnedImageBuffer<int> buffer = new PinnedImageBuffer<int>(array, width, height)) |
||||
|
{ |
||||
|
Assert.Equal(width, buffer.Width); |
||||
|
Assert.Equal(height, buffer.Height); |
||||
|
Assert.Equal(width * height, buffer.Length); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
[Fact] |
||||
|
public void CreateClean() |
||||
|
{ |
||||
|
for (int i = 0; i < 100; i++) |
||||
|
{ |
||||
|
using (PinnedImageBuffer<int> buffer = PinnedImageBuffer<int>.CreateClean(42, 42)) |
||||
|
{ |
||||
|
for (int j = 0; j < buffer.Length; j++) |
||||
|
{ |
||||
|
Assert.Equal(0, buffer.Array[j]); |
||||
|
buffer.Array[j] = 666; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(7, 42, 0)] |
||||
|
[InlineData(7, 42, 10)] |
||||
|
[InlineData(17, 42, 41)] |
||||
|
public void GetRowSpanY(int width, int height, int y) |
||||
|
{ |
||||
|
using (PinnedImageBuffer<int> buffer = new PinnedImageBuffer<int>(width, height)) |
||||
|
{ |
||||
|
BufferSpan<int> span = buffer.GetRowSpan(y); |
||||
|
|
||||
|
Assert.Equal(width * y, span.Start); |
||||
|
Assert.Equal(width, span.Length); |
||||
|
Assert.Equal(buffer.Pointer + sizeof(int) * width * y, span.PointerAtOffset); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(7, 42, 0, 0)] |
||||
|
[InlineData(7, 42, 3, 10)] |
||||
|
[InlineData(17, 42, 0, 41)] |
||||
|
public void GetRowSpanXY(int width, int height, int x, int y) |
||||
|
{ |
||||
|
using (PinnedImageBuffer<int> buffer = new PinnedImageBuffer<int>(width, height)) |
||||
|
{ |
||||
|
BufferSpan<int> span = buffer.GetRowSpan(x, y); |
||||
|
|
||||
|
Assert.Equal(width * y + x, span.Start); |
||||
|
Assert.Equal(width - x, span.Length); |
||||
|
Assert.Equal(buffer.Pointer + sizeof(int) * (width * y + x), span.PointerAtOffset); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue