Added unit test for WIC image loader

このコミットが含まれているのは:
Sergio Pedri committed 2021-05-12 11:33:05 +02:00
1 親 d1a2c667dd
コミット ec647da78b
3ファイル変更
+89 -40

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

+19
ファイルの表示
@@ -12,6 +12,25 @@ namespace ComputeSharp.Tests.Extensions
/// </summary>
public static class ImagingExtensions
{
/// <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>
/// <returns>An image with the data from the input texture.</returns>
[Pure]
public static Image<ImageSharpRgba32> ToImage(this ReadOnlyTexture2D<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);
texture.CopyTo(pixels);
return image;
}
/// <summary>
/// Creates a new <see cref="Image{TPixel}"/> instance with the specified texture data.
/// </summary>
+63
ファイルの表示
@@ -0,0 +1,63 @@
using System.IO;
using System.Reflection;
using ComputeSharp.BokehBlur.Processors;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32;
namespace ComputeSharp.Tests
{
[TestClass]
[TestCategory("ImageProcessing")]
public class ImageProcessingTests
{
[CombinatorialTestMethod]
[AllDevices]
public void BokehBlur(Device device)
{
// Early test to ensure the device is available. This saves time when running the
// unit test if the target device is not available, as we skip the preprocessing.
_ = device.Get();
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
using var original = Image.Load<ImageSharpRgba32>(Path.Combine(path, "city.jpg"));
using var cpu = original.Clone(c => c.BokehBlur(80, 2, 3));
using var gpu = original.Clone(c => c.ApplyProcessor(new HlslBokehBlurProcessor(device.Get(), 80, 2)));
string
expectedPath = Path.Combine(path, "city_bokeh_cpu.jpg"),
actualPath = Path.Combine(path, "city_bokeh_gpu.jpg");
cpu.Save(expectedPath);
gpu.Save(actualPath);
ImagingTests.TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.000009f);
}
[CombinatorialTestMethod]
[AllDevices]
public void GaussianBlur(Device device)
{
_ = device.Get();
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
using var original = Image.Load<ImageSharpRgba32>(Path.Combine(path, "city.jpg"));
using var cpu = original.Clone(c => c.GaussianBlur(30f));
using var gpu = original.Clone(c => c.ApplyProcessor(new HlslGaussianBlurProcessor(device.Get(), 90)));
string
expectedPath = Path.Combine(path, "city_gaussian_cpu.jpg"),
actualPath = Path.Combine(path, "city_gaussian_gpu.jpg");
cpu.Save(expectedPath);
gpu.Save(actualPath);
ImagingTests.TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.000003f);
}
}
}
+7 -40
ファイルの表示
@@ -4,13 +4,12 @@ using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using ComputeSharp.BokehBlur.Processors;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32;
using ImageSharpBgra32 = SixLabors.ImageSharp.PixelFormats.Bgra32;
namespace ComputeSharp.Tests
{
@@ -20,48 +19,16 @@ namespace ComputeSharp.Tests
{
[CombinatorialTestMethod]
[AllDevices]
public void BokehBlur(Device device)
public void LoadImageAsRgba32(Device device)
{
// Early test to ensure the device is available. This saves time when running the
// unit test if the target device is not available, as we skip the preprocessing.
_ = device.Get();
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
using ReadOnlyTexture2D<Rgba32, Float4> texture = Gpu.Default.AllocateReadOnlyTexture2D<Rgba32, Float4>(path);
using var original = Image.Load<ImageSharpRgba32>(Path.Combine(path, "city.jpg"));
using var cpu = original.Clone(c => c.BokehBlur(80, 2, 3));
using var gpu = original.Clone(c => c.ApplyProcessor(new HlslBokehBlurProcessor(device.Get(), 80, 2)));
using Image<ImageSharpRgba32> loaded = texture.ToImage();
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
string
expectedPath = Path.Combine(path, "city_bokeh_cpu.jpg"),
actualPath = Path.Combine(path, "city_bokeh_gpu.jpg");
cpu.Save(expectedPath);
gpu.Save(actualPath);
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.000009f);
}
[CombinatorialTestMethod]
[AllDevices]
public void GaussianBlur(Device device)
{
_ = device.Get();
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
using var original = Image.Load<ImageSharpRgba32>(Path.Combine(path, "city.jpg"));
using var cpu = original.Clone(c => c.GaussianBlur(30f));
using var gpu = original.Clone(c => c.ApplyProcessor(new HlslGaussianBlurProcessor(device.Get(), 90)));
string
expectedPath = Path.Combine(path, "city_gaussian_cpu.jpg"),
actualPath = Path.Combine(path, "city_gaussian_gpu.jpg");
cpu.Save(expectedPath);
gpu.Save(actualPath);
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.000003f);
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
}
/// <summary>