diff --git a/src/ComputeSharp/Core/Imaging/Formats/ImageFormat.cs b/src/ComputeSharp/Core/Imaging/Formats/ImageFormat.cs new file mode 100644 index 00000000..218957e5 --- /dev/null +++ b/src/ComputeSharp/Core/Imaging/Formats/ImageFormat.cs @@ -0,0 +1,37 @@ +namespace ComputeSharp; + +/// +/// An representing a supported image format. +/// +public enum ImageFormat +{ + /// + /// The BMP format. + /// + Bmp, + + /// + /// The PNG format. + /// + Png, + + /// + /// The JPEG format. + /// + Jpeg, + + /// + /// The WMP format (also used for .jxr, .hdp, .wdp and .wmp files). + /// + Wmp, + + /// + /// The TIFF format. + /// + Tiff, + + /// + /// The DDS format. + /// + Dds +} diff --git a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs index 7a4b51cd..4cfc2758 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs @@ -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); } + + /// + /// Saves a texture to a specified stream. + /// + /// The type of items to store in the texture. + /// The texture to save to an image. + /// The stream to save the image to. + /// The target image format to use. + public static void Save(this ReadBackTexture2D texture, Stream stream, ImageFormat format) + where T : unmanaged + { + Guard.IsNotNull(stream, nameof(stream)); + + WICHelper.Instance.SaveTexture(texture.View, stream, format); + } } diff --git a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs index 887659dd..d365be74 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs @@ -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); } + + /// + /// Saves a texture to a specified stream. + /// + /// The type of items to store in the texture. + /// The texture to save to an image. + /// The stream to save the image to. + /// The target image format to use. + public static void Save(this Texture2D texture, Stream stream, ImageFormat format) + where T : unmanaged + { + Guard.IsNotNull(stream, nameof(stream)); + + using ReadBackTexture2D readback = texture.GraphicsDevice.AllocateReadBackTexture2D(texture.Width, texture.Height); + + texture.CopyTo(readback); + + WICHelper.Instance.SaveTexture(readback.View, stream, format); + } } diff --git a/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs b/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs index 88d6ea2f..cbc95d3e 100644 --- a/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs @@ -132,4 +132,25 @@ internal static class WICFormatHelper _ => ThrowHelper.ThrowArgumentException("Invalid filename"), }; } + + /// + /// Gets the appropriate WIC container format value for the input format. + /// + /// The target format to get the container format for. + /// The WIC format container value matching . + /// Thrown when the input format isn't valid. + [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("Invalid format"), + }; + } } diff --git a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs index c66b9a10..d5e78467 100644 --- a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs @@ -247,10 +247,57 @@ internal sealed unsafe class WICHelper (IStream*)wicStream.Get(), WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache).Assert(); + SaveTexture(texture, wicBitmapEncoder.Get(), containerGuid); + } + + /// + /// Saves a texture to a specified stream. + /// + /// The type of items to store in the texture. + /// The texture data to save to a stream. + /// The target stream to write to. + /// The target image format to use. + public void SaveTexture(TextureView2D texture, Stream stream, ImageFormat format) + where T : unmanaged + { + using ComPtr wicBitmapEncoder = default; + Guid containerGuid = WICFormatHelper.GetForFormat(format); + + // Create the image encoder + this.wicImagingFactory2.Get()->CreateEncoder( + &containerGuid, + null, + wicBitmapEncoder.GetAddressOf()).Assert(); + + using ComPtr 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); + } + + /// + /// Saves a texture to a specified object. + /// + /// The type of items to store in the texture. + /// The texture data to save to an image. + /// The object in use. + /// The identifying the target format. + private void SaveTexture(TextureView2D texture, IWICBitmapEncoder* wicBitmapEncoder, Guid format) + where T : unmanaged + { using ComPtr 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(containerGuid, out Guid intermediateGuid)) + if (WICFormatHelper.TryGetIntermediateFormatForType(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(); } } diff --git a/tests/ComputeSharp.Tests/ImagingTests.cs b/tests/ComputeSharp.Tests/ImagingTests.cs index f89ef461..cff15df5 100644 --- a/tests/ComputeSharp.Tests/ImagingTests.cs +++ b/tests/ComputeSharp.Tests/ImagingTests.cs @@ -27,7 +27,7 @@ public class ImagingTests [Resource(typeof(ReadWriteTexture2D<,>))] [Data(typeof(string))] [Data(typeof(ReadOnlySpan))] - 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))] - public void LoadAsBgra32FromFile(Device device, Type textureType, Type inputType) - { - string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); - - using Texture2D texture = device.Get().LoadTexture2D(textureType, inputType, path); - - using Image loaded = texture.ToImage(); - using Image original = Image.Load(path); - - TolerantImageComparer.AssertEqual(original, loaded, 0.00000132f); - } - - [CombinatorialTestMethod] - [AllDevices] - [Resource(typeof(ReadOnlyTexture2D<,>))] - [Resource(typeof(ReadWriteTexture2D<,>))] - [Data(typeof(string))] - [Data(typeof(ReadOnlySpan))] - public void LoadAsBgra32FromFileWithSameFormat(Device device, Type textureType, Type inputType) - { - string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets", "CityAfter1024x1024Sampling.png"); - - using Texture2D texture = device.Get().LoadTexture2D(textureType, inputType, path); - - using Image loaded = texture.ToImage(); - using Image original = Image.Load(path); - - TolerantImageComparer.AssertEqual(original, loaded, 0.00000132f); - } - - [CombinatorialTestMethod] - [AllDevices] - [Resource(typeof(ReadOnlyTexture2D<,>))] - [Resource(typeof(ReadWriteTexture2D<,>))] - [Data(typeof(string))] - [Data(typeof(ReadOnlySpan))] - public void LoadAsR8FromFile(Device device, Type textureType, Type inputType) - { - string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); - - using Texture2D texture = device.Get().LoadTexture2D(textureType, inputType, path); - - using Image loaded = texture.ToImage(); - using Image original = Image.Load(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))] + public void LoadAsBgra32_FromFile(Device device, Type textureType, Type inputType) + { + string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); + + using Texture2D texture = device.Get().LoadTexture2D(textureType, inputType, path); + + using Image loaded = texture.ToImage(); + using Image original = Image.Load(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 texture = device.Get().LoadTexture2D(textureType, stream); + + using Image loaded = texture.ToImage(); + using Image original = Image.Load(path); + + TolerantImageComparer.AssertEqual(original, loaded, 0.00000132f); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + [Data(typeof(string))] + [Data(typeof(ReadOnlySpan))] + 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 texture = device.Get().LoadTexture2D(textureType, inputType, path); + + using Image loaded = texture.ToImage(); + using Image original = Image.Load(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 texture = device.Get().LoadTexture2D(textureType, stream); + + using Image loaded = texture.ToImage(); + using Image original = Image.Load(path); + + TolerantImageComparer.AssertEqual(original, loaded, 0.00000132f); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + [Data(typeof(string))] + [Data(typeof(ReadOnlySpan))] + public void LoadAsR8_FromFile(Device device, Type textureType, Type inputType) + { + string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); + + using Texture2D texture = device.Get().LoadTexture2D(textureType, inputType, path); + + using Image loaded = texture.ToImage(); + using Image original = Image.Load(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 texture = device.Get().LoadTexture2D(textureType, stream); + + using Image loaded = texture.ToImage(); + using Image original = Image.Load(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))] - 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 texture = device.Get().LoadTexture2D(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))] - 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 texture = device.Get().LoadTexture2D(textureType, typeof(string), expectedPath); + using ReadBackTexture2D readback = device.Get().AllocateReadBackTexture2D(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 texture = device.Get().LoadTexture2D(textureType, typeof(string), path); + using ReadBackTexture2D readback = device.Get().AllocateReadBackTexture2D(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))] - 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))] - 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");