Added imaging unit tests
このコミットが含まれているのは:
1 親
501ab5f570
コミット
e2b190b64e
3ファイル変更
+186
-1
検索条件に一致するファイルはありません
@@ -409,7 +409,7 @@ namespace ComputeSharp.BokehBlur.Processors
|
||||
{
|
||||
Float4 v = source[i, ThreadIds.X];
|
||||
|
||||
v.XYZ = Hlsl.Clamp(v.XYZ, 0, float.MaxValue);
|
||||
v = Hlsl.Clamp(v, 0, float.MaxValue);
|
||||
v.XYZ = Hlsl.Pow(v.XYZ, 1 / 3f);
|
||||
|
||||
target[i, ThreadIds.X] = v;
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
<PackageReference Include="Microsoft.Toolkit.HighPerformance" Version="7.0.0-preview4" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
|
||||
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -22,6 +23,18 @@
|
||||
PrivateAssets="contentfiles;build"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\..\samples\ComputeSharp.ImageProcessing\Primitives\Complex64.cs" Link="Imaging\Primitives\Complex64.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.ImageProcessing\Processors\HlslBokehBlurProcessor.cs" Link="Imaging\Processors\HlslBokehBlurProcessor.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.ImageProcessing\Processors\HlslBokehBlurProcessor{TPixel}.cs" Link="Imaging\Processors\HlslBokehBlurProcessor{TPixel}.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.ImageProcessing\Processors\HlslGaussianBlurProcessor.cs" Link="Imaging\Processors\HlslGaussianBlurProcessor.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.ImageProcessing\Processors\HlslGaussianBlurProcessor{TPixel}.cs" Link="Imaging\Processors\HlslGaussianBlurProcessor{TPixel}.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\..\samples\ComputeSharp.ImageProcessing\city.jpg" Link="Imaging\city.jpg" CopyToOutputDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Texture2DTests.Pixels.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using ComputeSharp.BokehBlur.Processors;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32;
|
||||
|
||||
namespace ComputeSharp.Tests
|
||||
{
|
||||
[TestClass]
|
||||
[TestCategory("Imaging")]
|
||||
public class ImagingTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void BokehBlur()
|
||||
{
|
||||
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(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);
|
||||
|
||||
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.000009f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GaussianBlur()
|
||||
{
|
||||
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(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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An tolerant image comparer type, from <see href="https://github.com/SixLabors/ImageSharp">ImageSharp</see>.
|
||||
/// </summary>
|
||||
private static class TolerantImageComparer
|
||||
{
|
||||
/// <summary>
|
||||
/// Asserts that two images are equal.
|
||||
/// </summary>
|
||||
/// <param name="expectedPath">The path to the reference image.</param>
|
||||
/// <param name="actualPath">The path to the expected image.</param>
|
||||
/// <param name="threshold">The allowed difference threshold for the normalized delta.</param>
|
||||
public static void AssertEqual(string expectedPath, string actualPath, float threshold)
|
||||
{
|
||||
using var expected = Image.Load<ImageSharpRgba32>(expectedPath);
|
||||
using var actual = Image.Load<ImageSharpRgba32>(actualPath);
|
||||
|
||||
if (expected.Size() != actual.Size())
|
||||
{
|
||||
Assert.Fail($"The input images have different sizes: {expected.Size()} and {actual.Size()}");
|
||||
}
|
||||
|
||||
if (expected.Frames.Count != actual.Frames.Count ||
|
||||
expected.Frames.Count != 1 ||
|
||||
actual.Frames.Count != 1)
|
||||
{
|
||||
Assert.Fail("The two input images must have 1 frame each");
|
||||
}
|
||||
|
||||
int width = actual.Width;
|
||||
|
||||
float totalDifference = 0F;
|
||||
|
||||
var differences = new List<PixelDifference>(20);
|
||||
|
||||
for (int y = 0; y < actual.Height; y++)
|
||||
{
|
||||
Span<ImageSharpRgba32> aSpan = expected.GetPixelRowSpan(y);
|
||||
Span<ImageSharpRgba32> bSpan = actual.GetPixelRowSpan(y);
|
||||
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static int GetManhattanDistanceInRgbaSpace(ref ImageSharpRgba32 a, ref ImageSharpRgba32 b)
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static int Diff(ushort a, ushort b) => Math.Abs(a - b);
|
||||
|
||||
return Diff(a.R, b.R) + Diff(a.G, b.G) + Diff(a.B, b.B) + Diff(a.A, b.A);
|
||||
}
|
||||
|
||||
int d = GetManhattanDistanceInRgbaSpace(ref aSpan[x], ref bSpan[x]);
|
||||
|
||||
if (d > 0)
|
||||
{
|
||||
if (differences.Count < 20)
|
||||
{
|
||||
var diff = new PixelDifference(new Point(x, y), aSpan[x], bSpan[x]);
|
||||
differences.Add(diff);
|
||||
}
|
||||
|
||||
totalDifference += d;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float normalizedDifference = totalDifference / (actual.Width * (float)actual.Height);
|
||||
normalizedDifference /= 4F * 65535F;
|
||||
|
||||
if (normalizedDifference > threshold)
|
||||
{
|
||||
StringBuilder builder = new();
|
||||
builder.AppendLine($"The input images don't match. Normalized delta: {normalizedDifference}%");
|
||||
builder.AppendLine($"First {differences.Count} pixel deltas:");
|
||||
|
||||
foreach (var delta in differences)
|
||||
{
|
||||
builder.AppendLine(delta.ToString());
|
||||
}
|
||||
|
||||
Assert.Fail(builder.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A simple model repressenting info about a difference between two pixels.
|
||||
/// </summary>
|
||||
private readonly struct PixelDifference
|
||||
{
|
||||
public PixelDifference(Point position, ImageSharpRgba32 expected, ImageSharpRgba32 actual)
|
||||
{
|
||||
this.Position = position;
|
||||
this.RedDifference = actual.R - expected.R;
|
||||
this.GreenDifference = actual.G - expected.G;
|
||||
this.BlueDifference = actual.B - expected.B;
|
||||
this.AlphaDifference = actual.A - expected.A;
|
||||
}
|
||||
|
||||
public readonly Point Position;
|
||||
|
||||
public readonly int RedDifference;
|
||||
|
||||
public readonly int GreenDifference;
|
||||
|
||||
public readonly int BlueDifference;
|
||||
|
||||
public readonly int AlphaDifference;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[delta({RedDifference},{GreenDifference},{BlueDifference},{AlphaDifference}) @ ({Position.X},{Position.Y})]";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする