Browse Source

Decode PNGs with SkiaSharp in render tests (#22405)

pull/22409/head
Julien Lebosquain 1 month ago
committed by GitHub
parent
commit
c04fa80b9d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 1
      Directory.Packages.props
  2. 2
      tests/Avalonia.IntegrationTests.Appium/Avalonia.IntegrationTests.Appium.csproj
  3. 23
      tests/Avalonia.IntegrationTests.Appium/WindowTests.cs
  4. 1
      tests/Avalonia.RenderTests.WpfCompare/Avalonia.RenderTests.WpfCompare.csproj
  5. 26
      tests/Avalonia.RenderTests/TestBase.cs
  6. 71
      tests/Avalonia.RenderTests/TestRenderHelper.cs
  7. 1
      tests/Avalonia.Skia.RenderTests/Avalonia.Skia.RenderTests.csproj
  8. 3
      tests/Avalonia.Skia.RenderTests/CrossTestBase.cs

1
Directory.Packages.props

@ -53,7 +53,6 @@
<PackageVersion Include="Silk.NET.Vulkan" Version="2.22.0" />
<PackageVersion Include="Silk.NET.Vulkan.Extensions.EXT" Version="2.22.0" />
<PackageVersion Include="Silk.NET.Vulkan.Extensions.KHR" Version="2.22.0" />
<PackageVersion Include="SixLabors.ImageSharp" Version="2.1.12" />
<PackageVersion Include="SkiaSharp" Version="3.119.4" />
<PackageVersion Include="SkiaSharp.NativeAssets.Linux" Version="3.119.4" />
<PackageVersion Include="SkiaSharp.NativeAssets.WebAssembly" Version="3.119.4" />

2
tests/Avalonia.IntegrationTests.Appium/Avalonia.IntegrationTests.Appium.csproj

@ -33,10 +33,10 @@
<!-- This test project is run in CI via VSTest Azure Task, to have video recorder support -->
<!-- Video recorder isn't yet supported by Microsoft.Testing.Platform, we keep this project compatible with both VSTest and Microsoft.Testing.Platform. -->
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="SixLabors.ImageSharp" />
<PackageReference Include="System.Reactive" />
<PackageReference Include="xunit.runner.visualstudio" />
</ItemGroup>
<Import Project="..\..\build\XUnit.props" />
<Import Project="..\..\build\SkiaSharp.props" />
</Project>

23
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<Rgba32>(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<Rgba32>(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)]

1
tests/Avalonia.RenderTests.WpfCompare/Avalonia.RenderTests.WpfCompare.csproj

@ -17,7 +17,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Moq" />
<PackageReference Include="SixLabors.ImageSharp" />
<PackageReference Include="System.Reactive" />
<PackageReference Include="Xunit.StaFact" />
</ItemGroup>

26
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<Rgba32>(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<Rgba32>(actualPath);
using var actual = TestRenderHelper.LoadImage(actualPath);
var error = TestRenderHelper.CompareImages(actual, expected);
if (error > allowedError)
{

71
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<Rgba32>(expectedPath))
using (var actual = Image.Load<Rgba32>(actualPath))
using (var expected = LoadImage(expectedPath))
using (var actual = LoadImage(actualPath))
{
double immediateError = TestRenderHelper.CompareImages(actual, expected);
@ -207,12 +196,30 @@ static class TestRenderHelper
}
}
/// <summary>
/// Loads an image as non-premultiplied RGBA, so that <see cref="CompareImages"/> can read raw channels.
/// </summary>
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}");
}
/// <summary>
/// Calculates root mean square error for given two images.
/// Based roughly on ImageMagick implementation to ensure consistency.
/// </summary>
public static double CompareImages(Image<Rgba32> actual, Image<Rgba32> 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;

1
tests/Avalonia.Skia.RenderTests/Avalonia.Skia.RenderTests.csproj

@ -33,7 +33,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Moq" />
<PackageReference Include="SixLabors.ImageSharp" />
<PackageReference Include="System.Reactive" />
<PackageReference Include="unofficial.mesa.softwarerenderer" />
</ItemGroup>

3
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;

Loading…
Cancel
Save