From c04fa80b9d8ec756d6cc474b0da7371694dea349 Mon Sep 17 00:00:00 2001 From: Julien Lebosquain Date: Thu, 8 Oct 2026 09:55:01 +0000 Subject: [PATCH] Decode PNGs with SkiaSharp in render tests (#22405) --- Directory.Packages.props | 1 - .../Avalonia.IntegrationTests.Appium.csproj | 2 +- .../WindowTests.cs | 23 +++--- .../Avalonia.RenderTests.WpfCompare.csproj | 1 - tests/Avalonia.RenderTests/TestBase.cs | 26 ++----- .../Avalonia.RenderTests/TestRenderHelper.cs | 71 +++++++++++-------- .../Avalonia.Skia.RenderTests.csproj | 1 - .../CrossTestBase.cs | 3 - 8 files changed, 60 insertions(+), 68 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 1809b71615..d3c7418c97 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -53,7 +53,6 @@ - diff --git a/tests/Avalonia.IntegrationTests.Appium/Avalonia.IntegrationTests.Appium.csproj b/tests/Avalonia.IntegrationTests.Appium/Avalonia.IntegrationTests.Appium.csproj index dfb325e414..5bd7eb013c 100644 --- a/tests/Avalonia.IntegrationTests.Appium/Avalonia.IntegrationTests.Appium.csproj +++ b/tests/Avalonia.IntegrationTests.Appium/Avalonia.IntegrationTests.Appium.csproj @@ -33,10 +33,10 @@ - + diff --git a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs index aa1c675e82..e1b7e4ed2e 100644 --- a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs +++ b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs @@ -6,9 +6,8 @@ using Avalonia.Controls; using OpenQA.Selenium; using OpenQA.Selenium.Appium; using OpenQA.Selenium.Interactions; -using SixLabors.ImageSharp.PixelFormats; +using SkiaSharp; using Xunit; -using Xunit.Sdk; namespace Avalonia.IntegrationTests.Appium { @@ -222,12 +221,12 @@ namespace Avalonia.IntegrationTests.Appium window.Click(); - var img = SixLabors.ImageSharp.Image.Load(screenshot.AsByteArray); - var topLeftColor = img[10, 10]; - var centerColor = img[img.Width / 2, img.Height / 2]; + using var img = SKBitmap.Decode(screenshot.AsByteArray); + var topLeftColor = img.GetPixel(10, 10); + var centerColor = img.GetPixel(img.Width / 2, img.Height / 2); - Assert.Equal(new Rgba32(0, 128, 0), topLeftColor); - Assert.Equal(new Rgba32(255, 0, 0), centerColor); + Assert.Equal(new SKColor(0, 128, 0), topLeftColor); + Assert.Equal(new SKColor(255, 0, 0), centerColor); } [Fact] @@ -243,12 +242,12 @@ namespace Avalonia.IntegrationTests.Appium window.Click(); - var img = SixLabors.ImageSharp.Image.Load(screenshot.AsByteArray); - var topLeftColor = img[10, 10]; - var centerColor = img[img.Width / 2, img.Height / 2]; + using var img = SKBitmap.Decode(screenshot.AsByteArray); + var topLeftColor = img.GetPixel(10, 10); + var centerColor = img.GetPixel(img.Width / 2, img.Height / 2); - Assert.Equal(new Rgba32(0, 128, 0), topLeftColor); - Assert.Equal(new Rgba32(255, 0, 0), centerColor); + Assert.Equal(new SKColor(0, 128, 0), topLeftColor); + Assert.Equal(new SKColor(255, 0, 0), centerColor); } [PlatformFact(TestPlatforms.Windows)] diff --git a/tests/Avalonia.RenderTests.WpfCompare/Avalonia.RenderTests.WpfCompare.csproj b/tests/Avalonia.RenderTests.WpfCompare/Avalonia.RenderTests.WpfCompare.csproj index 66e547e0ba..020a26df95 100644 --- a/tests/Avalonia.RenderTests.WpfCompare/Avalonia.RenderTests.WpfCompare.csproj +++ b/tests/Avalonia.RenderTests.WpfCompare/Avalonia.RenderTests.WpfCompare.csproj @@ -17,7 +17,6 @@ - diff --git a/tests/Avalonia.RenderTests/TestBase.cs b/tests/Avalonia.RenderTests/TestBase.cs index 21f95e09ff..6889893144 100644 --- a/tests/Avalonia.RenderTests/TestBase.cs +++ b/tests/Avalonia.RenderTests/TestBase.cs @@ -1,26 +1,10 @@ +using System; using System.IO; using System.Runtime.CompilerServices; -using Avalonia.Controls; -using Avalonia.Media.Imaging; -using Avalonia.Rendering; -using SixLabors.ImageSharp; -using Xunit; -using Avalonia.Platform; using System.Threading.Tasks; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Reactive.Disposables; -using System.Threading; +using Avalonia.Controls; using Avalonia.Media; -using Avalonia.Rendering.Composition; -using Avalonia.Skia; -using Avalonia.Threading; -using Avalonia.UnitTests; -using Avalonia.Utilities; -using SixLabors.ImageSharp.PixelFormats; -using Image = SixLabors.ImageSharp.Image; +using Xunit; namespace Avalonia.Skia.RenderTests { @@ -83,12 +67,12 @@ namespace Avalonia.Skia.RenderTests { var expectedPath = Path.Combine(OutputPath, testName + ".expected.png"); - using (var expected = Image.Load(expectedPath)) + using (var expected = TestRenderHelper.LoadImage(expectedPath)) { void Compare(string outputType, double allowedError) { var actualPath = Path.Combine(OutputPath, testName + "." + outputType + ".out.png"); - using var actual = Image.Load(actualPath); + using var actual = TestRenderHelper.LoadImage(actualPath); var error = TestRenderHelper.CompareImages(actual, expected); if (error > allowedError) { diff --git a/tests/Avalonia.RenderTests/TestRenderHelper.cs b/tests/Avalonia.RenderTests/TestRenderHelper.cs index 4ec70b2bb3..7e6ed0ad31 100644 --- a/tests/Avalonia.RenderTests/TestRenderHelper.cs +++ b/tests/Avalonia.RenderTests/TestRenderHelper.cs @@ -1,30 +1,19 @@ +using System; using System.IO; -using System.Runtime.CompilerServices; +using System.Threading.Tasks; using Avalonia.Controls; -using Avalonia.Media.Imaging; -using Avalonia.Rendering; -using SixLabors.ImageSharp; -using Xunit; +using Avalonia.Harfbuzz; using Avalonia.Input; +using Avalonia.Media.Imaging; +using Avalonia.OpenGL.Egl; using Avalonia.Platform; -using System.Threading.Tasks; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Reactive.Disposables; -using System.Threading; using Avalonia.Platform.Surfaces; -using Avalonia.Media; +using Avalonia.Rendering; using Avalonia.Rendering.Composition; using Avalonia.Threading; using Avalonia.UnitTests; -using Avalonia.Utilities; -using SixLabors.ImageSharp.PixelFormats; -using Image = SixLabors.ImageSharp.Image; -using Avalonia.Harfbuzz; -using Avalonia.OpenGL.Egl; -using Avalonia.Skia; +using SkiaSharp; +using Xunit; namespace Avalonia.Skia.RenderTests; @@ -195,8 +184,8 @@ static class TestRenderHelper public static void AssertCompareImages(string actualPath, string expectedPath) { - using (var expected = Image.Load(expectedPath)) - using (var actual = Image.Load(actualPath)) + using (var expected = LoadImage(expectedPath)) + using (var actual = LoadImage(actualPath)) { double immediateError = TestRenderHelper.CompareImages(actual, expected); @@ -207,12 +196,30 @@ static class TestRenderHelper } } + /// + /// Loads an image as non-premultiplied RGBA, so that can read raw channels. + /// + public static SKBitmap LoadImage(string path) + { + using var codec = SKCodec.Create(path) ?? throw new InvalidOperationException($"Couldn't decode {path}"); + var info = new SKImageInfo(codec.Info.Width, codec.Info.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul); + return SKBitmap.Decode(codec, info) ?? throw new InvalidOperationException($"Couldn't decode {path}"); + } + /// /// Calculates root mean square error for given two images. /// Based roughly on ImageMagick implementation to ensure consistency. /// - public static double CompareImages(Image actual, Image expected) + public static double CompareImages(SKBitmap actual, SKBitmap expected) { + if (actual.ColorType != SKColorType.Rgba8888 || + actual.AlphaType != SKAlphaType.Unpremul || + expected.ColorType != SKColorType.Rgba8888 || + expected.AlphaType != SKAlphaType.Unpremul) + { + throw new ArgumentException("Images must be in non-premultiplied RGBA format"); + } + if (actual.Width != expected.Width || actual.Height != expected.Height) { throw new ArgumentException("Images have different resolutions"); @@ -222,19 +229,27 @@ static class TestRenderHelper double squaresError = 0; const double scale = 1 / 255d; - + + var actualPixels = actual.GetPixelSpan(); + var expectedPixels = expected.GetPixelSpan(); + var actualRowBytes = actual.RowBytes; + var expectedRowBytes = expected.RowBytes; + for (var x = 0; x < actual.Width; x++) { double localError = 0; for (var y = 0; y < actual.Height; y++) { - var expectedAlpha = expected[x, y].A * scale; - var actualAlpha = actual[x, y].A * scale; + var expectedPixel = expectedPixels.Slice(y * expectedRowBytes + x * 4, 4); + var actualPixel = actualPixels.Slice(y * actualRowBytes + x * 4, 4); + + var expectedAlpha = expectedPixel[3] * scale; + var actualAlpha = actualPixel[3] * scale; - var r = scale * (expectedAlpha * expected[x, y].R - actualAlpha * actual[x, y].R); - var g = scale * (expectedAlpha * expected[x, y].G - actualAlpha * actual[x, y].G); - var b = scale * (expectedAlpha * expected[x, y].B - actualAlpha * actual[x, y].B); + var r = scale * (expectedAlpha * expectedPixel[0] - actualAlpha * actualPixel[0]); + var g = scale * (expectedAlpha * expectedPixel[1] - actualAlpha * actualPixel[1]); + var b = scale * (expectedAlpha * expectedPixel[2] - actualAlpha * actualPixel[2]); var a = expectedAlpha - actualAlpha; var error = r * r + g * g + b * b + a * a; diff --git a/tests/Avalonia.Skia.RenderTests/Avalonia.Skia.RenderTests.csproj b/tests/Avalonia.Skia.RenderTests/Avalonia.Skia.RenderTests.csproj index 500ac604a4..c92512ffe8 100644 --- a/tests/Avalonia.Skia.RenderTests/Avalonia.Skia.RenderTests.csproj +++ b/tests/Avalonia.Skia.RenderTests/Avalonia.Skia.RenderTests.csproj @@ -33,7 +33,6 @@ - diff --git a/tests/Avalonia.Skia.RenderTests/CrossTestBase.cs b/tests/Avalonia.Skia.RenderTests/CrossTestBase.cs index 4e33616b17..e255807b4e 100644 --- a/tests/Avalonia.Skia.RenderTests/CrossTestBase.cs +++ b/tests/Avalonia.Skia.RenderTests/CrossTestBase.cs @@ -3,11 +3,8 @@ using System; using System.IO; using System.Runtime.CompilerServices; -using Avalonia.Skia.RenderTests; using Avalonia.Skia.RenderTests.CrossUI; using CrossUI; -using SixLabors.ImageSharp; -using SixLabors.ImageSharp.PixelFormats; using Xunit; namespace Avalonia.Skia.RenderTests;