Add Texture2D<T>.ToArray overload

このコミットが含まれているのは:
Sergio Pedri committed 2021-11-12 13:27:35 +01:00
1 親 aef6630709
コミット d657b9cc95
2ファイル変更
+77

検索条件に一致するファイルはありません

+27
ファイルの表示
@@ -29,6 +29,33 @@ public static class Texture2DExtensions
return data;
}
/// <summary>
/// Reads the contents of the current <see cref="Texture2D{T}"/> instance and returns an array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="Texture2D{T}"/> instance to read data from.</param>
/// <param name="x">The horizontal offset in the source texture.</param>
/// <param name="y">The vertical offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
/// <param name="height">The height of the memory area to copy.</param>
/// <returns>A <typeparamref name="T"/> array with the contents of the current texture.</returns>
[Pure]
public static unsafe T[,] ToArray<T>(this Texture2D<T> source, int x, int y, int width, int height)
where T : unmanaged
{
Guard.IsGreaterThanOrEqualTo(width, 0, nameof(width));
Guard.IsGreaterThanOrEqualTo(height, 0, nameof(height));
T[,] data = new T[height, width];
fixed (T* p = data)
{
source.CopyTo(new Span<T>(p, width * height), x, y, width, height);
}
return data;
}
/// <summary>
/// Reads the contents of the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
+50
ファイルの表示
@@ -112,6 +112,56 @@ public partial class Texture2DTests
texture.Dispose();
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<>))]
[Resource(typeof(ReadWriteTexture2D<>))]
[Data(0, 0, 64, 64)]
[Data(0, 14, 64, 50)]
[Data(14, 0, 50, 64)]
[Data(10, 10, 54, 54)]
[Data(20, 20, 32, 27)]
[Data(60, 0, 4, 64)]
[Data(0, 60, 64, 4)]
[Data(63, 2, 1, 60)]
[Data(2, 63, 60, 1)]
public void GetData_RangeToArray_Ok(Device device, Type textureType, int x, int y, int width, int height)
{
float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray();
using Texture2D<float> texture = device.Get().AllocateTexture2D(textureType, array, 64, 64);
float[,] result = texture.ToArray(x, y, width, height);
Assert.AreEqual(height, result.GetLength(0));
Assert.AreEqual(width, result.GetLength(1));
Span2D<float> expected = new Span2D<float>(array, 64, 64).Slice(y, x, height, width);
Span2D<float> data = new(result);
CollectionAssert.AreEqual(expected.ToArray(), data.ToArray());
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<>))]
[Resource(typeof(ReadWriteTexture2D<>))]
[Data(0, -1, 50, 50)]
[Data(-1, 0, 50, 50)]
[Data(12, 0, -1, 50)]
[Data(12, 0, 20, -1)]
[Data(12, 20, 20, 50)]
[Data(12, 20, 60, 20)]
[Data(80, 20, 40, 20)]
[Data(0, 80, 40, 20)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void GetData_RangeToArray_Fail(Device device, Type textureType, int x, int y, int width, int height)
{
using Texture2D<float> texture = device.Get().AllocateTexture2D<float>(textureType, 64, 64);
_ = texture.ToArray(x, y, width, height);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<>))]