Improve tests for loading/saving images

このコミットが含まれているのは:
Sergio Pedri committed 2021-11-16 20:19:01 +01:00
1 親 a0ee841edb
コミット fcc99d20aa
2ファイル変更
+82 -21

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

+49 -5
ファイルの表示
@@ -174,9 +174,9 @@ public static class GraphicsDeviceExtensions
/// <summary>
/// Allocates a new <see cref="TransferTexture3D{T}"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of items in the buffer.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the buffer for.</param>
/// <param name="type">The type of buffer to allocate.</param>
/// <typeparam name="T">The type of items in the texture.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the texture for.</param>
/// <param name="type">The type of texture to allocate.</param>
/// <param name="width">The width of the texture to create.</param>
/// <param name="height">The height of the texture to create.</param>
/// <param name="depth">The depth of the texture to create.</param>
@@ -220,7 +220,7 @@ public static class GraphicsDeviceExtensions
/// </summary>
/// <typeparam name="T">The type of items in the texture.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the texture for.</param>
/// <param name="type">The type of buffer to allocate.</param>
/// <param name="type">The type of texture to allocate.</param>
/// <param name="data">The data to load on the texture.</param>
/// <param name="width">The width of the texture to create.</param>
/// <param name="height">The height of the texture to create.</param>
@@ -242,7 +242,7 @@ public static class GraphicsDeviceExtensions
/// </summary>
/// <typeparam name="T">The type of items in the texture.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the texture for.</param>
/// <param name="type">The type of buffer to allocate.</param>
/// <param name="type">The type of texture to allocate.</param>
/// <param name="data">The data to load on the texture.</param>
/// <param name="width">The width of the texture to create.</param>
/// <param name="height">The height of the texture to create.</param>
@@ -299,4 +299,48 @@ public static class GraphicsDeviceExtensions
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
/// <summary>
/// Loads a new readonly 2D texture with the contents of the specified file.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="type">The type of texture to allocate.</param>
/// <param name="filename">The filename of the image file to load and decode into the texture.</param>
/// <returns>A <see cref="Texture2D{T, TPixel}"/> instance with the contents of the specified file.</returns>
[Pure]
public static Texture2D<T> LoadTexture2D<T, TPixel>(this GraphicsDevice device, Type type, ReadOnlySpan<char> filename)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
return type switch
{
_ when type == typeof(ReadOnlyTexture2D<,>) => device.LoadReadOnlyTexture2D<T, TPixel>(filename),
_ when type == typeof(ReadWriteTexture2D<,>) => device.LoadReadWriteTexture2D<T, TPixel>(filename),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
/// <summary>
/// Loads a new readonly 2D texture with the contents of the specified file.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="type">The type of texture to allocate.</param>
/// <param name="buffer">The buffer with the image data to load and decode into the texture.</param>
/// <returns>A <see cref="Texture2D{T, TPixel}"/> instance with the contents of the specified file.</returns>
[Pure]
public static Texture2D<T> LoadTexture2D<T, TPixel>(this GraphicsDevice device, Type type, byte[] buffer)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
return type switch
{
_ when type == typeof(ReadOnlyTexture2D<,>) => device.LoadReadOnlyTexture2D<T, TPixel>(buffer),
_ when type == typeof(ReadWriteTexture2D<,>) => device.LoadReadWriteTexture2D<T, TPixel>(buffer),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
}
+33 -16
ファイルの表示
@@ -4,6 +4,7 @@ using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using ComputeSharp.Resources;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -22,11 +23,13 @@ public class ImagingTests
{
[CombinatorialTestMethod]
[AllDevices]
public void LoadAsRgba32FromFile(Device device)
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
public void LoadAsRgba32FromFile(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
using ReadOnlyTexture2D<Rgba32, float4> texture = device.Get().LoadReadOnlyTexture2D<Rgba32, float4>(path);
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, path);
using Image<ImageSharpRgba32> loaded = texture.ToImage<Rgba32, ImageSharpRgba32>();
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
@@ -36,11 +39,13 @@ public class ImagingTests
[CombinatorialTestMethod]
[AllDevices]
public void LoadAsBgra32FromFile(Device device)
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
public void LoadAsBgra32FromFile(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
using ReadOnlyTexture2D<Bgra32, float4> texture = device.Get().LoadReadOnlyTexture2D<Bgra32, float4>(path);
using Texture2D<Bgra32> texture = device.Get().LoadTexture2D<Bgra32, float4>(textureType, path);
using Image<ImageSharpBgra32> loaded = texture.ToImage<Bgra32, ImageSharpBgra32>();
using Image<ImageSharpBgra32> original = Image.Load<ImageSharpBgra32>(path);
@@ -50,11 +55,13 @@ public class ImagingTests
[CombinatorialTestMethod]
[AllDevices]
public void LoadAsBgra32FromFileWithSameFormat(Device device)
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
public void LoadAsBgra32FromFileWithSameFormat(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets", "CityAfter1024x1024Sampling.png");
using ReadOnlyTexture2D<Bgra32, float4> texture = device.Get().LoadReadOnlyTexture2D<Bgra32, float4>(path);
using Texture2D<Bgra32> texture = device.Get().LoadTexture2D<Bgra32, float4>(textureType, path);
using Image<ImageSharpBgra32> loaded = texture.ToImage<Bgra32, ImageSharpBgra32>();
using Image<ImageSharpBgra32> original = Image.Load<ImageSharpBgra32>(path);
@@ -64,11 +71,13 @@ public class ImagingTests
[CombinatorialTestMethod]
[AllDevices]
public void LoadAsR8FromFile(Device device)
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
public void LoadAsR8FromFile(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
using ReadOnlyTexture2D<R8, float> texture = device.Get().LoadReadOnlyTexture2D<R8, float>(path);
using Texture2D<R8> texture = device.Get().LoadTexture2D<R8, float>(textureType, path);
using Image<ImageSharpL8> loaded = texture.ToImage<R8, ImageSharpL8>();
using Image<ImageSharpL8> original = Image.Load<ImageSharpL8>(path);
@@ -78,13 +87,15 @@ public class ImagingTests
[CombinatorialTestMethod]
[AllDevices]
public void LoadAsRgba32FromBuffer(Device device)
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
public void LoadAsRgba32FromBuffer(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
byte[] buffer = File.ReadAllBytes(path);
using ReadOnlyTexture2D<Rgba32, float4> texture = device.Get().LoadReadOnlyTexture2D<Rgba32, float4>(buffer);
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, buffer);
using Image<ImageSharpRgba32> loaded = texture.ToImage<Rgba32, ImageSharpRgba32>();
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
@@ -94,13 +105,15 @@ public class ImagingTests
[CombinatorialTestMethod]
[AllDevices]
public void SaveRgba32AsJpeg(Device device)
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
public void SaveRgba32AsJpeg(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
string expectedPath = Path.Combine(path, "city.jpg");
string actualPath = Path.Combine(path, "city_rgba32_saved.jpg");
using ReadOnlyTexture2D<Rgba32, float4> texture = device.Get().LoadReadOnlyTexture2D<Rgba32, float4>(expectedPath);
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, path);
texture.Save(actualPath);
@@ -109,13 +122,15 @@ public class ImagingTests
[CombinatorialTestMethod]
[AllDevices]
public void SaveBgra32AsJpeg(Device device)
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
public void SaveBgra32AsJpeg(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
string expectedPath = Path.Combine(path, "city.jpg");
string actualPath = Path.Combine(path, "city_bgra32_saved.jpg");
using ReadOnlyTexture2D<Bgra32, float4> texture = device.Get().LoadReadOnlyTexture2D<Bgra32, float4>(expectedPath);
using Texture2D<Bgra32> texture = device.Get().LoadTexture2D<Bgra32, float4>(textureType, path);
texture.Save(actualPath);
@@ -124,14 +139,16 @@ public class ImagingTests
[CombinatorialTestMethod]
[AllDevices]
public void SaveR8AsJpeg(Device device)
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
public void SaveR8AsJpeg(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
string sourcePath = Path.Combine(path, "city.jpg");
string expectedPath = Path.Combine(path, "city_r8_reference.jpg");
string actualPath = Path.Combine(path, "city_r8_saved.jpg");
using ReadOnlyTexture2D<R8, float> texture = device.Get().LoadReadOnlyTexture2D<R8, float>(sourcePath);
using Texture2D<R8> texture = device.Get().LoadTexture2D<R8, float>(textureType, path);
texture.Save(actualPath);