Minor code refactoring to unit tests

このコミットが含まれているのは:
Sergio Pedri committed 2021-05-13 13:19:33 +02:00
1 親 a5f77cbf79
コミット c9ff2c5baf
3ファイル変更
+17 -28

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

+13 -24
ファイルの表示
@@ -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
/// <summary>
/// Creates a new <see cref="Image{TPixel}"/> instance with the specified texture data.
/// </summary>
/// <param name="texture">The source <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance to read data from.</param>
/// <typeparam name="TFrom">The input pixel format used in the texture.</typeparam>
/// <typeparam name="To">The target pixel format for the returned image.</typeparam>
/// <param name="texture">The source <see cref="Texture2D{T}"/> instance to read data from.</param>
/// <returns>An image with the data from the input texture.</returns>
[Pure]
public static Image<ImageSharpRgba32> ToImage(this ReadOnlyTexture2D<Rgba32, Float4> texture)
public static unsafe Image<TTo> ToImage<TFrom, TTo>(this Texture2D<TFrom> texture)
where TFrom : unmanaged
where TTo : unmanaged, IPixel<TTo>
{
Image<ImageSharpRgba32> image = new(texture.Width, texture.Height);
Guard.IsEqualTo(sizeof(TTo), sizeof(TFrom), nameof(TTo));
Assert.IsTrue(image.TryGetSinglePixelSpan(out Span<ImageSharpRgba32> span));
Image<TTo> image = new(texture.Width, texture.Height);
Span<Rgba32> pixels = MemoryMarshal.Cast<ImageSharpRgba32, Rgba32>(span);
Assert.IsTrue(image.TryGetSinglePixelSpan(out Span<TTo> span));
texture.CopyTo(pixels);
return image;
}
/// <summary>
/// Creates a new <see cref="Image{TPixel}"/> instance with the specified texture data.
/// </summary>
/// <param name="texture">The source <see cref="ReadWriteTexture2D{T, TPixel}"/> instance to read data from.</param>
/// <returns>An image with the data from the input texture.</returns>
[Pure]
public static Image<ImageSharpRgba32> ToImage(this ReadWriteTexture2D<Rgba32, Float4> texture)
{
Image<ImageSharpRgba32> image = new(texture.Width, texture.Height);
Assert.IsTrue(image.TryGetSinglePixelSpan(out Span<ImageSharpRgba32> span));
Span<Rgba32> pixels = MemoryMarshal.Cast<ImageSharpRgba32, Rgba32>(span);
Span<TFrom> pixels = MemoryMarshal.Cast<TTo, TFrom>(span);
texture.CopyTo(pixels);
+2 -2
ファイルの表示
@@ -25,7 +25,7 @@ namespace ComputeSharp.Tests
using ReadOnlyTexture2D<Rgba32, Float4> texture = device.Get().AllocateReadOnlyTexture2D<Rgba32, Float4>(path);
using Image<ImageSharpRgba32> loaded = texture.ToImage();
using Image<ImageSharpRgba32> loaded = texture.ToImage<Rgba32, ImageSharpRgba32>();
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
@@ -41,7 +41,7 @@ namespace ComputeSharp.Tests
using ReadOnlyTexture2D<Rgba32, Float4> texture = device.Get().AllocateReadOnlyTexture2D<Rgba32, Float4>(buffer);
using Image<ImageSharpRgba32> loaded = texture.ToImage();
using Image<ImageSharpRgba32> loaded = texture.ToImage<Rgba32, ImageSharpRgba32>();
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
+2 -2
ファイルの表示
@@ -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<Rgba32, ImageSharpRgba32>();
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<Rgba32, ImageSharpRgba32>();
ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f);
}