Add texture load/save overloads with string parameter
このコミットが含まれているのは:
1 親
fcc99d20aa
コミット
f3eb04e4b7
5ファイル変更
+223
-23
検索条件に一致するファイルはありません
@@ -3,6 +3,7 @@ using System.Diagnostics.Contracts;
|
||||
using ComputeSharp.__Internals;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using ComputeSharp.Resources;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
@@ -1070,6 +1071,24 @@ public static partial class GraphicsDeviceExtensions
|
||||
return new(device, width, height, depth, allocationMode);
|
||||
}
|
||||
|
||||
/// <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="filename">The filename of the image file to load and decode into the texture.</param>
|
||||
/// <returns>A <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the contents of the specified file.</returns>
|
||||
[Pure]
|
||||
public static ReadOnlyTexture2D<T, TPixel> LoadReadOnlyTexture2D<T, TPixel>(this GraphicsDevice device, string filename)
|
||||
where T : unmanaged, IUnorm<TPixel>
|
||||
where TPixel : unmanaged
|
||||
{
|
||||
Guard.IsNotNull(filename, nameof(filename));
|
||||
|
||||
return device.LoadReadOnlyTexture2D<T, TPixel>(filename.AsSpan());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a new readonly 2D texture with the contents of the specified file.
|
||||
/// </summary>
|
||||
@@ -1114,6 +1133,24 @@ public static partial class GraphicsDeviceExtensions
|
||||
return texture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a new writeable 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="filename">The filename of the image file to load and decode into the texture.</param>
|
||||
/// <returns>A <see cref="ReadWriteTexture2D{T, TPixel}"/> instance with the contents of the specified file.</returns>
|
||||
[Pure]
|
||||
public static ReadWriteTexture2D<T, TPixel> LoadReadWriteTexture2D<T, TPixel>(this GraphicsDevice device, string filename)
|
||||
where T : unmanaged, IUnorm<TPixel>
|
||||
where TPixel : unmanaged
|
||||
{
|
||||
Guard.IsNotNull(filename, nameof(filename));
|
||||
|
||||
return device.LoadReadWriteTexture2D<T, TPixel>(filename.AsSpan());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a new writeable 2D texture with the contents of the specified file.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using ComputeSharp.Resources;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
|
||||
namespace ComputeSharp;
|
||||
|
||||
@@ -55,6 +56,20 @@ public static class ReadBackTexture2DExtensions
|
||||
source.CopyTo(destination, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a texture to a specified file.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <param name="texture">The texture to save to an image.</param>
|
||||
/// <param name="filename">The filename of the image file to save.</param>
|
||||
public static void Save<T>(this ReadBackTexture2D<T> texture, string filename)
|
||||
where T : unmanaged
|
||||
{
|
||||
Guard.IsNotNull(filename, nameof(filename));
|
||||
|
||||
WICHelper.Instance.SaveTexture(texture.View, filename.AsSpan());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a texture to a specified file.
|
||||
/// </summary>
|
||||
|
||||
@@ -535,6 +535,20 @@ public static class Texture2DExtensions
|
||||
destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a texture to a specified file.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <param name="texture">The texture to save to an image.</param>
|
||||
/// <param name="filename">The filename of the image file to save.</param>
|
||||
public static void Save<T>(this Texture2D<T> texture, string filename)
|
||||
where T : unmanaged
|
||||
{
|
||||
Guard.IsNotNull(filename, nameof(filename));
|
||||
|
||||
texture.Save(filename.AsSpan());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a texture to a specified file.
|
||||
/// </summary>
|
||||
|
||||
@@ -306,19 +306,30 @@ public static class GraphicsDeviceExtensions
|
||||
/// <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="textureType">The type of texture to allocate.</param>
|
||||
/// <param name="inputType">The type of filepath to use.</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)
|
||||
public static Texture2D<T> LoadTexture2D<T, TPixel>(this GraphicsDevice device, Type textureType, Type inputType, string filename)
|
||||
where T : unmanaged, IUnorm<TPixel>
|
||||
where TPixel : unmanaged
|
||||
{
|
||||
return type switch
|
||||
return textureType 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))
|
||||
_ when textureType == typeof(ReadOnlyTexture2D<,>) => inputType switch
|
||||
{
|
||||
_ when inputType == typeof(string) => device.LoadReadOnlyTexture2D<T, TPixel>(filename),
|
||||
_ when inputType == typeof(ReadOnlySpan<char>) => device.LoadReadOnlyTexture2D<T, TPixel>(filename.AsSpan()),
|
||||
_ => throw new ArgumentException($"Invalid input type: {inputType}", nameof(inputType))
|
||||
},
|
||||
_ when textureType == typeof(ReadWriteTexture2D<,>) => inputType switch
|
||||
{
|
||||
_ when inputType == typeof(string) => device.LoadReadWriteTexture2D<T, TPixel>(filename),
|
||||
_ when inputType == typeof(ReadOnlySpan<char>) => device.LoadReadWriteTexture2D<T, TPixel>(filename.AsSpan()),
|
||||
_ => throw new ArgumentException($"Invalid input type: {inputType}", nameof(inputType))
|
||||
},
|
||||
_ => throw new ArgumentException($"Invalid texture type: {textureType}", nameof(textureType))
|
||||
};
|
||||
}
|
||||
|
||||
@@ -343,4 +354,50 @@ public static class GraphicsDeviceExtensions
|
||||
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a texture to a specified file.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <param name="texture">The texture to save to an image.</param>
|
||||
/// <param name="inputType">The type of filepath to use.</param>
|
||||
/// <param name="filename">The filename of the image file to save.</param>
|
||||
public static void Save<T>(this Texture2D<T> texture, Type inputType, string filename)
|
||||
where T : unmanaged
|
||||
{
|
||||
switch (inputType)
|
||||
{
|
||||
case var _ when inputType == typeof(string):
|
||||
texture.Save(filename);
|
||||
break;
|
||||
case var _ when inputType == typeof(ReadOnlySpan<char>):
|
||||
texture.Save(filename.AsSpan());
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException($"Invalid input type: {inputType}", nameof(inputType));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a texture to a specified file.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
|
||||
/// <param name="texture">The texture to save to an image.</param>
|
||||
/// <param name="inputType">The type of filepath to use.</param>
|
||||
/// <param name="filename">The filename of the image file to save.</param>
|
||||
public static void Save<T>(this ReadBackTexture2D<T> texture, Type inputType, string filename)
|
||||
where T : unmanaged
|
||||
{
|
||||
switch (inputType)
|
||||
{
|
||||
case var _ when inputType == typeof(string):
|
||||
texture.Save(filename);
|
||||
break;
|
||||
case var _ when inputType == typeof(ReadOnlySpan<char>):
|
||||
texture.Save(filename.AsSpan());
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException($"Invalid input type: {inputType}", nameof(inputType));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,11 +25,13 @@ public class ImagingTests
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
public void LoadAsRgba32FromFile(Device device, Type textureType)
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void LoadAsRgba32FromFile(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, path);
|
||||
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, inputType, path);
|
||||
|
||||
using Image<ImageSharpRgba32> loaded = texture.ToImage<Rgba32, ImageSharpRgba32>();
|
||||
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
|
||||
@@ -41,11 +43,13 @@ public class ImagingTests
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
public void LoadAsBgra32FromFile(Device device, Type textureType)
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void LoadAsBgra32FromFile(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
using Texture2D<Bgra32> texture = device.Get().LoadTexture2D<Bgra32, float4>(textureType, path);
|
||||
using Texture2D<Bgra32> texture = device.Get().LoadTexture2D<Bgra32, float4>(textureType, inputType, path);
|
||||
|
||||
using Image<ImageSharpBgra32> loaded = texture.ToImage<Bgra32, ImageSharpBgra32>();
|
||||
using Image<ImageSharpBgra32> original = Image.Load<ImageSharpBgra32>(path);
|
||||
@@ -57,11 +61,13 @@ public class ImagingTests
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
public void LoadAsBgra32FromFileWithSameFormat(Device device, Type textureType)
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void LoadAsBgra32FromFileWithSameFormat(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets", "CityAfter1024x1024Sampling.png");
|
||||
|
||||
using Texture2D<Bgra32> texture = device.Get().LoadTexture2D<Bgra32, float4>(textureType, path);
|
||||
using Texture2D<Bgra32> texture = device.Get().LoadTexture2D<Bgra32, float4>(textureType, inputType, path);
|
||||
|
||||
using Image<ImageSharpBgra32> loaded = texture.ToImage<Bgra32, ImageSharpBgra32>();
|
||||
using Image<ImageSharpBgra32> original = Image.Load<ImageSharpBgra32>(path);
|
||||
@@ -73,11 +79,13 @@ public class ImagingTests
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
public void LoadAsR8FromFile(Device device, Type textureType)
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void LoadAsR8FromFile(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
using Texture2D<R8> texture = device.Get().LoadTexture2D<R8, float>(textureType, path);
|
||||
using Texture2D<R8> texture = device.Get().LoadTexture2D<R8, float>(textureType, inputType, path);
|
||||
|
||||
using Image<ImageSharpL8> loaded = texture.ToImage<R8, ImageSharpL8>();
|
||||
using Image<ImageSharpL8> original = Image.Load<ImageSharpL8>(path);
|
||||
@@ -103,19 +111,31 @@ public class ImagingTests
|
||||
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[Device(Device.Warp)]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void LoadAsRgba32_WithNullPath(Device device, Type textureType)
|
||||
{
|
||||
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, typeof(string), null!);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
public void SaveRgba32AsJpeg(Device device, Type textureType)
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void SaveRgba32AsJpeg(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
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 Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, path);
|
||||
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, inputType, expectedPath);
|
||||
|
||||
texture.Save(actualPath);
|
||||
texture.Save(inputType, actualPath);
|
||||
|
||||
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f);
|
||||
}
|
||||
@@ -124,15 +144,56 @@ public class ImagingTests
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
public void SaveBgra32AsJpeg(Device device, Type textureType)
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void SaveRgba32AsJpeg_WithReadBackTexture(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
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 Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, inputType, expectedPath);
|
||||
using ReadBackTexture2D<Rgba32> readback = device.Get().AllocateReadBackTexture2D<Rgba32>(texture.Width, texture.Height);
|
||||
|
||||
texture.CopyTo(readback);
|
||||
|
||||
readback.Save(inputType, actualPath);
|
||||
|
||||
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[Device(Device.Warp)]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void SaveRgba32AsJpeg_WithReadBackTexture_WithNullPath(Device device, Type textureType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, typeof(string), path);
|
||||
using ReadBackTexture2D<Rgba32> readback = device.Get().AllocateReadBackTexture2D<Rgba32>(texture.Width, texture.Height);
|
||||
|
||||
texture.CopyTo(readback);
|
||||
|
||||
readback.Save(typeof(string), null!);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void SaveBgra32AsJpeg(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
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 Texture2D<Bgra32> texture = device.Get().LoadTexture2D<Bgra32, float4>(textureType, path);
|
||||
using Texture2D<Bgra32> texture = device.Get().LoadTexture2D<Bgra32, float4>(textureType, inputType, expectedPath);
|
||||
|
||||
texture.Save(actualPath);
|
||||
texture.Save(inputType, actualPath);
|
||||
|
||||
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f);
|
||||
}
|
||||
@@ -141,16 +202,18 @@ public class ImagingTests
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
public void SaveR8AsJpeg(Device device, Type textureType)
|
||||
[Data(typeof(string))]
|
||||
[Data(typeof(ReadOnlySpan<char>))]
|
||||
public void SaveR8AsJpeg(Device device, Type textureType, Type inputType)
|
||||
{
|
||||
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 Texture2D<R8> texture = device.Get().LoadTexture2D<R8, float>(textureType, path);
|
||||
using Texture2D<R8> texture = device.Get().LoadTexture2D<R8, float>(textureType, inputType, sourcePath);
|
||||
|
||||
texture.Save(actualPath);
|
||||
texture.Save(inputType, actualPath);
|
||||
|
||||
using Image<ImageSharpL8> original = Image.Load<ImageSharpL8>(sourcePath);
|
||||
|
||||
@@ -159,6 +222,20 @@ public class ImagingTests
|
||||
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00004037f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[Device(Device.Warp)]
|
||||
[Resource(typeof(ReadOnlyTexture2D<,>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<,>))]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void SaveRgba32AsJpeg_WithNullPath(Device device, Type textureType)
|
||||
{
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
|
||||
|
||||
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, typeof(string), path);
|
||||
|
||||
texture.Save(typeof(string), null!);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An tolerant image comparer type, from <see href="https://github.com/SixLabors/ImageSharp">ImageSharp</see>.
|
||||
/// </summary>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする