Add [Upload]Texture2D<T>.Save overload for Stream

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-08 19:57:28 +01:00
1 親 989ac8aeef
コミット 5d5dc541bc
6ファイル変更
+322 -67

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

+37
ファイルの表示
@@ -0,0 +1,37 @@
namespace ComputeSharp;
/// <summary>
/// An <see langword="enum"/> representing a supported image format.
/// </summary>
public enum ImageFormat
{
/// <summary>
/// The BMP format.
/// </summary>
Bmp,
/// <summary>
/// The PNG format.
/// </summary>
Png,
/// <summary>
/// The JPEG format.
/// </summary>
Jpeg,
/// <summary>
/// The WMP format (also used for .jxr, .hdp, .wdp and .wmp files).
/// </summary>
Wmp,
/// <summary>
/// The TIFF format.
/// </summary>
Tiff,
/// <summary>
/// The DDS format.
/// </summary>
Dds
}
+16
ファイルの表示
@@ -1,4 +1,5 @@
using System;
using System.IO;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Resources;
using Microsoft.Toolkit.Diagnostics;
@@ -81,4 +82,19 @@ public static class ReadBackTexture2DExtensions
{
WICHelper.Instance.SaveTexture(texture.View, filename);
}
/// <summary>
/// Saves a texture to a specified stream.
/// </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="stream">The stream to save the image to.</param>
/// <param name="format">The target image format to use.</param>
public static void Save<T>(this ReadBackTexture2D<T> texture, Stream stream, ImageFormat format)
where T : unmanaged
{
Guard.IsNotNull(stream, nameof(stream));
WICHelper.Instance.SaveTexture(texture.View, stream, format);
}
}
+20
ファイルの表示
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics.Contracts;
using System.IO;
using System.Runtime.InteropServices;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Resources;
@@ -564,4 +565,23 @@ public static class Texture2DExtensions
WICHelper.Instance.SaveTexture(readback.View, filename);
}
/// <summary>
/// Saves a texture to a specified stream.
/// </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="stream">The stream to save the image to.</param>
/// <param name="format">The target image format to use.</param>
public static void Save<T>(this Texture2D<T> texture, Stream stream, ImageFormat format)
where T : unmanaged
{
Guard.IsNotNull(stream, nameof(stream));
using ReadBackTexture2D<T> readback = texture.GraphicsDevice.AllocateReadBackTexture2D<T>(texture.Width, texture.Height);
texture.CopyTo(readback);
WICHelper.Instance.SaveTexture(readback.View, stream, format);
}
}
+21
ファイルの表示
@@ -132,4 +132,25 @@ internal static class WICFormatHelper
_ => ThrowHelper.ThrowArgumentException<Guid>("Invalid filename"),
};
}
/// <summary>
/// Gets the appropriate WIC container format <see cref="Guid"/> value for the input format.
/// </summary>
/// <param name="format">The target format to get the container format for.</param>
/// <returns>The WIC format container <see cref="Guid"/> value matching <paramref name="format"/>.</returns>
/// <exception cref="ArgumentException">Thrown when the input format isn't valid.</exception>
[Pure]
public static Guid GetForFormat(ImageFormat format)
{
return format switch
{
ImageFormat.Bmp => GUID.GUID_ContainerFormatBmp,
ImageFormat.Png => GUID.GUID_ContainerFormatPng,
ImageFormat.Jpeg => GUID.GUID_ContainerFormatJpeg,
ImageFormat.Wmp => GUID.GUID_ContainerFormatWmp,
ImageFormat.Tiff => GUID.GUID_ContainerFormatTiff,
ImageFormat.Dds => GUID.GUID_ContainerFormatDds,
_ => ThrowHelper.ThrowArgumentException<Guid>("Invalid format"),
};
}
}
+51 -4
ファイルの表示
@@ -247,10 +247,57 @@ internal sealed unsafe class WICHelper
(IStream*)wicStream.Get(),
WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache).Assert();
SaveTexture(texture, wicBitmapEncoder.Get(), containerGuid);
}
/// <summary>
/// Saves a texture to a specified stream.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="texture">The texture data to save to a stream.</param>
/// <param name="stream">The target stream to write to.</param>
/// <param name="format">The target image format to use.</param>
public void SaveTexture<T>(TextureView2D<T> texture, Stream stream, ImageFormat format)
where T : unmanaged
{
using ComPtr<IWICBitmapEncoder> wicBitmapEncoder = default;
Guid containerGuid = WICFormatHelper.GetForFormat(format);
// Create the image encoder
this.wicImagingFactory2.Get()->CreateEncoder(
&containerGuid,
null,
wicBitmapEncoder.GetAddressOf()).Assert();
using ComPtr<IWICStream> wicStream = default;
// Create and initialize a stream
this.wicImagingFactory2.Get()->CreateStream(wicStream.GetAddressOf()).Assert();
wicStream.Get()->InitializeFromStream(stream);
// Initialize the encoder
wicBitmapEncoder.Get()->Initialize(
(IStream*)wicStream.Get(),
WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache).Assert();
SaveTexture(texture, wicBitmapEncoder.Get(), containerGuid);
}
/// <summary>
/// Saves a texture to a specified <see cref="IWICBitmapEncoder"/> object.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="texture">The texture data to save to an image.</param>
/// <param name="wicBitmapEncoder">The <see cref="IWICBitmapEncoder"/> object in use.</param>
/// <param name="format">The <see cref="Guid"/> identifying the target format.</param>
private void SaveTexture<T>(TextureView2D<T> texture, IWICBitmapEncoder* wicBitmapEncoder, Guid format)
where T : unmanaged
{
using ComPtr<IWICBitmapFrameEncode> wicBitmapFrameEncode = default;
// Create the image frame and initialize it
wicBitmapEncoder.Get()->CreateNewFrame(wicBitmapFrameEncode.GetAddressOf(), null).Assert();
wicBitmapEncoder->CreateNewFrame(wicBitmapFrameEncode.GetAddressOf(), null).Assert();
wicBitmapFrameEncode.Get()->Initialize(null).Assert();
wicBitmapFrameEncode.Get()->SetSize((uint)texture.Width, (uint)texture.Height).Assert();
@@ -258,7 +305,7 @@ internal sealed unsafe class WICHelper
// Depending on the target format and the current pixel type, we need to check whether
// an intermediate encoding step is necessary. This is because not all pixel formats
// are directly supported by the native image encoders present in Windows.
if (WICFormatHelper.TryGetIntermediateFormatForType<T>(containerGuid, out Guid intermediateGuid))
if (WICFormatHelper.TryGetIntermediateFormatForType<T>(format, out Guid intermediateGuid))
{
T* data = texture.DangerousGetAddressAndByteStride(out int strideInBytes);
@@ -306,9 +353,9 @@ internal sealed unsafe class WICHelper
cbStride: (uint)strideInBytes,
cbBufferSize: (uint)(strideInBytes * texture.Height),
pbPixels: (byte*)data).Assert();
}
}
wicBitmapFrameEncode.Get()->Commit();
wicBitmapEncoder.Get()->Commit();
wicBitmapEncoder->Commit();
}
}
+177 -63
ファイルの表示
@@ -27,7 +27,7 @@ public class ImagingTests
[Resource(typeof(ReadWriteTexture2D<,>))]
[Data(typeof(string))]
[Data(typeof(ReadOnlySpan<char>))]
public void LoadAsRgba32FromFile(Device device, Type textureType, Type inputType)
public void LoadAsRgba32_FromFile(Device device, Type textureType, Type inputType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
@@ -43,61 +43,7 @@ public class ImagingTests
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
[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, inputType, path);
using Image<ImageSharpBgra32> loaded = texture.ToImage<Bgra32, ImageSharpBgra32>();
using Image<ImageSharpBgra32> original = Image.Load<ImageSharpBgra32>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.00000132f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
[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, inputType, path);
using Image<ImageSharpBgra32> loaded = texture.ToImage<Bgra32, ImageSharpBgra32>();
using Image<ImageSharpBgra32> original = Image.Load<ImageSharpBgra32>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.00000132f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
[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, inputType, path);
using Image<ImageSharpL8> loaded = texture.ToImage<R8, ImageSharpL8>();
using Image<ImageSharpL8> original = Image.Load<ImageSharpL8>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.000039f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
public void LoadAsRgba32FromBuffer(Device device, Type textureType)
public void LoadAsRgba32_FromBuffer(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
@@ -115,7 +61,7 @@ public class ImagingTests
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
public void LoadAsRgba32FromStream(Device device, Type textureType)
public void LoadAsRgba32_FromStream(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
@@ -129,6 +75,114 @@ public class ImagingTests
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
[Data(typeof(string))]
[Data(typeof(ReadOnlySpan<char>))]
public void LoadAsBgra32_FromFile(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, inputType, path);
using Image<ImageSharpBgra32> loaded = texture.ToImage<Bgra32, ImageSharpBgra32>();
using Image<ImageSharpBgra32> original = Image.Load<ImageSharpBgra32>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.00000132f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
public void LoadAsBgra32_FromStream(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
using Stream stream = File.OpenRead(path);
using Texture2D<Bgra32> texture = device.Get().LoadTexture2D<Bgra32, float4>(textureType, stream);
using Image<ImageSharpBgra32> loaded = texture.ToImage<Bgra32, ImageSharpBgra32>();
using Image<ImageSharpBgra32> original = Image.Load<ImageSharpBgra32>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.00000132f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
[Data(typeof(string))]
[Data(typeof(ReadOnlySpan<char>))]
public void LoadAsBgra32_FromFile_WithSameFormat(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, inputType, path);
using Image<ImageSharpBgra32> loaded = texture.ToImage<Bgra32, ImageSharpBgra32>();
using Image<ImageSharpBgra32> original = Image.Load<ImageSharpBgra32>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.00000132f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
public void LoadAsBgra32_FromStream_WithSameFormat(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets", "CityAfter1024x1024Sampling.png");
using Stream stream = File.OpenRead(path);
using Texture2D<Bgra32> texture = device.Get().LoadTexture2D<Bgra32, float4>(textureType, stream);
using Image<ImageSharpBgra32> loaded = texture.ToImage<Bgra32, ImageSharpBgra32>();
using Image<ImageSharpBgra32> original = Image.Load<ImageSharpBgra32>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.00000132f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
[Data(typeof(string))]
[Data(typeof(ReadOnlySpan<char>))]
public void LoadAsR8_FromFile(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, inputType, path);
using Image<ImageSharpL8> loaded = texture.ToImage<R8, ImageSharpL8>();
using Image<ImageSharpL8> original = Image.Load<ImageSharpL8>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.000039f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
public void LoadAsR8_FromFile(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
using Stream stream = File.OpenRead(path);
using Texture2D<R8> texture = device.Get().LoadTexture2D<R8, float>(textureType, stream);
using Image<ImageSharpL8> loaded = texture.ToImage<R8, ImageSharpL8>();
using Image<ImageSharpL8> original = Image.Load<ImageSharpL8>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.000039f);
}
[CombinatorialTestMethod]
[Device(Device.Warp)]
[Resource(typeof(ReadOnlyTexture2D<,>))]
@@ -155,7 +209,7 @@ public class ImagingTests
[Resource(typeof(ReadWriteTexture2D<,>))]
[Data(typeof(string))]
[Data(typeof(ReadOnlySpan<char>))]
public void SaveRgba32AsJpeg(Device device, Type textureType, Type inputType)
public void SaveRgba32AsJpeg_ToFile(Device device, Type textureType, Type inputType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
string expectedPath = Path.Combine(path, "city.jpg");
@@ -168,13 +222,33 @@ public class ImagingTests
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
public void SaveRgba32AsJpeg_ToStream(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 Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, typeof(string), expectedPath);
using (Stream stream = File.OpenWrite(actualPath))
{
texture.Save(stream, ImageFormat.Jpeg);
}
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
[Data(typeof(string))]
[Data(typeof(ReadOnlySpan<char>))]
public void SaveRgba32AsJpeg_WithReadBackTexture(Device device, Type textureType, Type inputType)
public void SaveRgba32AsJpeg_ToFile_WithReadBackTexture(Device device, Type textureType, Type inputType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
string expectedPath = Path.Combine(path, "city.jpg");
@@ -190,12 +264,35 @@ public class ImagingTests
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
public void SaveRgba32AsJpeg_ToStream_WithReadBackTexture(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 Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, typeof(string), expectedPath);
using ReadBackTexture2D<Rgba32> readback = device.Get().AllocateReadBackTexture2D<Rgba32>(texture.Width, texture.Height);
texture.CopyTo(readback);
using (Stream stream = File.OpenWrite(actualPath))
{
readback.Save(stream, ImageFormat.Jpeg);
}
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)
public void SaveRgba32AsJpeg_ToFile_WithReadBackTexture_WithNullPath(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
@@ -207,13 +304,30 @@ public class ImagingTests
readback.Save(typeof(string), null!);
}
[CombinatorialTestMethod]
[Device(Device.Warp)]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
[ExpectedException(typeof(ArgumentNullException))]
public void SaveRgba32AsJpeg_ToStream_WithReadBackTexture_WithNullStream(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(null!, ImageFormat.Jpeg);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
[Data(typeof(string))]
[Data(typeof(ReadOnlySpan<char>))]
public void SaveBgra32AsJpeg(Device device, Type textureType, Type inputType)
public void SaveBgra32AsJpeg_ToFile(Device device, Type textureType, Type inputType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
string expectedPath = Path.Combine(path, "city.jpg");
@@ -232,7 +346,7 @@ public class ImagingTests
[Resource(typeof(ReadWriteTexture2D<,>))]
[Data(typeof(string))]
[Data(typeof(ReadOnlySpan<char>))]
public void SaveR8AsJpeg(Device device, Type textureType, Type inputType)
public void SaveR8AsJpeg_ToFile(Device device, Type textureType, Type inputType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
string sourcePath = Path.Combine(path, "city.jpg");
@@ -255,7 +369,7 @@ public class ImagingTests
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
[ExpectedException(typeof(ArgumentNullException))]
public void SaveRgba32AsJpeg_WithNullPath(Device device, Type textureType)
public void SaveRgba32AsJpeg_ToFile_WithNullPath(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");