diff --git a/tests/ComputeSharp.Tests/Extensions/ImagingExtensions.cs b/tests/ComputeSharp.Tests/Extensions/ImagingExtensions.cs index ab357a2e..7babea3d 100644 --- a/tests/ComputeSharp.Tests/Extensions/ImagingExtensions.cs +++ b/tests/ComputeSharp.Tests/Extensions/ImagingExtensions.cs @@ -1,9 +1,11 @@ using System; using System.Diagnostics.Contracts; using System.Runtime.InteropServices; +using ComputeSharp.Resources; +using Microsoft.Toolkit.Diagnostics; using Microsoft.VisualStudio.TestTools.UnitTesting; using SixLabors.ImageSharp; -using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; +using SixLabors.ImageSharp.PixelFormats; namespace ComputeSharp.Tests.Extensions { @@ -15,35 +17,22 @@ namespace ComputeSharp.Tests.Extensions /// /// Creates a new instance with the specified texture data. /// - /// The source instance to read data from. + /// The input pixel format used in the texture. + /// The target pixel format for the returned image. + /// The source instance to read data from. /// An image with the data from the input texture. [Pure] - public static Image ToImage(this ReadOnlyTexture2D texture) + public static unsafe Image ToImage(this Texture2D texture) + where TFrom : unmanaged + where TTo : unmanaged, IPixel { - Image image = new(texture.Width, texture.Height); + Guard.IsEqualTo(sizeof(TTo), sizeof(TFrom), nameof(TTo)); - Assert.IsTrue(image.TryGetSinglePixelSpan(out Span span)); + Image image = new(texture.Width, texture.Height); - Span pixels = MemoryMarshal.Cast(span); + Assert.IsTrue(image.TryGetSinglePixelSpan(out Span span)); - texture.CopyTo(pixels); - - return image; - } - - /// - /// Creates a new instance with the specified texture data. - /// - /// The source instance to read data from. - /// An image with the data from the input texture. - [Pure] - public static Image ToImage(this ReadWriteTexture2D texture) - { - Image image = new(texture.Width, texture.Height); - - Assert.IsTrue(image.TryGetSinglePixelSpan(out Span span)); - - Span pixels = MemoryMarshal.Cast(span); + Span pixels = MemoryMarshal.Cast(span); texture.CopyTo(pixels); diff --git a/tests/ComputeSharp.Tests/ImagingTests.cs b/tests/ComputeSharp.Tests/ImagingTests.cs index 6e0e7656..7d68e8a0 100644 --- a/tests/ComputeSharp.Tests/ImagingTests.cs +++ b/tests/ComputeSharp.Tests/ImagingTests.cs @@ -25,7 +25,7 @@ namespace ComputeSharp.Tests using ReadOnlyTexture2D texture = device.Get().AllocateReadOnlyTexture2D(path); - using Image loaded = texture.ToImage(); + using Image loaded = texture.ToImage(); using Image original = Image.Load(path); TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f); @@ -41,7 +41,7 @@ namespace ComputeSharp.Tests using ReadOnlyTexture2D texture = device.Get().AllocateReadOnlyTexture2D(buffer); - using Image loaded = texture.ToImage(); + using Image loaded = texture.ToImage(); using Image original = Image.Load(path); TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f); diff --git a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs index 0c0fec91..37fa62fe 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs @@ -81,7 +81,7 @@ namespace ComputeSharp.Tests device.Get().For(sampled.Width, sampled.Height, new SamplingComputeShader(source, destination)); - using var processed = destination.ToImage(); + using var processed = destination.ToImage(); ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f); } @@ -114,7 +114,7 @@ namespace ComputeSharp.Tests device.Get().ForEach(destination, new SamplingPixelShader(source)); - using var processed = destination.ToImage(); + using var processed = destination.ToImage(); ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f); }