From e2b190b64e16898f15ca7bc2e9b53dfa5d8ef67d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 15 Feb 2021 13:58:45 +0100 Subject: [PATCH] Added imaging unit tests --- .../HlslBokehBlurProcessor{TPixel}.cs | 2 +- .../ComputeSharp.Tests.csproj | 13 ++ tests/ComputeSharp.Tests/ImagingTests.cs | 172 ++++++++++++++++++ 3 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 tests/ComputeSharp.Tests/ImagingTests.cs diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs index f53f3759..5b609cc0 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -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; diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index 11ddfefb..de8eb4a1 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -12,6 +12,7 @@ + @@ -22,6 +23,18 @@ PrivateAssets="contentfiles;build"/> + + + + + + + + + + + + TextTemplatingFileGenerator diff --git a/tests/ComputeSharp.Tests/ImagingTests.cs b/tests/ComputeSharp.Tests/ImagingTests.cs new file mode 100644 index 00000000..645e8378 --- /dev/null +++ b/tests/ComputeSharp.Tests/ImagingTests.cs @@ -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(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(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); + } + + /// + /// An tolerant image comparer type, from ImageSharp. + /// + private static class TolerantImageComparer + { + /// + /// Asserts that two images are equal. + /// + /// The path to the reference image. + /// The path to the expected image. + /// The allowed difference threshold for the normalized delta. + public static void AssertEqual(string expectedPath, string actualPath, float threshold) + { + using var expected = Image.Load(expectedPath); + using var actual = Image.Load(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(20); + + for (int y = 0; y < actual.Height; y++) + { + Span aSpan = expected.GetPixelRowSpan(y); + Span 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()); + } + } + + /// + /// A simple model repressenting info about a difference between two pixels. + /// + 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; + + /// + public override string ToString() + { + return $"[delta({RedDifference},{GreenDifference},{BlueDifference},{AlphaDifference}) @ ({Position.X},{Position.Y})]"; + } + } + } + } +}