@ -1,30 +1,19 @@
using System ;
using System.IO ;
using System.IO ;
using System.Runtime.CompilerService s ;
using System.Threading.Task s ;
using Avalonia.Controls ;
using Avalonia.Controls ;
using Avalonia.Media.Imaging ;
using Avalonia.Harfbuzz ;
using Avalonia.Rendering ;
using SixLabors.ImageSharp ;
using Xunit ;
using Avalonia.Input ;
using Avalonia.Input ;
using Avalonia.Media.Imaging ;
using Avalonia.OpenGL.Egl ;
using Avalonia.Platform ;
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.Platform.Surfaces ;
using Avalonia.Media ;
using Avalonia.Rendering ;
using Avalonia.Rendering.Composition ;
using Avalonia.Rendering.Composition ;
using Avalonia.Threading ;
using Avalonia.Threading ;
using Avalonia.UnitTests ;
using Avalonia.UnitTests ;
using Avalonia.Utilities ;
using SkiaSharp ;
using SixLabors.ImageSharp.PixelFormats ;
using Xunit ;
using Image = SixLabors . ImageSharp . Image ;
using Avalonia.Harfbuzz ;
using Avalonia.OpenGL.Egl ;
using Avalonia.Skia ;
namespace Avalonia.Skia.RenderTests ;
namespace Avalonia.Skia.RenderTests ;
@ -195,8 +184,8 @@ static class TestRenderHelper
public static void AssertCompareImages ( string actualPath , string expectedPath )
public static void AssertCompareImages ( string actualPath , string expectedPath )
{
{
using ( var expected = Image . Load < Rgba32 > ( expectedPath ) )
using ( var expected = LoadImage ( expectedPath ) )
using ( var actual = Image . Load < Rgba32 > ( actualPath ) )
using ( var actual = LoadImage ( actualPath ) )
{
{
double immediateError = TestRenderHelper . CompareImages ( actual , expected ) ;
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>
/// <summary>
/// Calculates root mean square error for given two images.
/// Calculates root mean square error for given two images.
/// Based roughly on ImageMagick implementation to ensure consistency.
/// Based roughly on ImageMagick implementation to ensure consistency.
/// </summary>
/// </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 )
if ( actual . Width ! = expected . Width | | actual . Height ! = expected . Height )
{
{
throw new ArgumentException ( "Images have different resolutions" ) ;
throw new ArgumentException ( "Images have different resolutions" ) ;
@ -222,19 +229,27 @@ static class TestRenderHelper
double squaresError = 0 ;
double squaresError = 0 ;
const double scale = 1 / 2 5 5d ;
const double scale = 1 / 2 5 5d ;
var actualPixels = actual . GetPixelSpan ( ) ;
var expectedPixels = expected . GetPixelSpan ( ) ;
var actualRowBytes = actual . RowBytes ;
var expectedRowBytes = expected . RowBytes ;
for ( var x = 0 ; x < actual . Width ; x + + )
for ( var x = 0 ; x < actual . Width ; x + + )
{
{
double localError = 0 ;
double localError = 0 ;
for ( var y = 0 ; y < actual . Height ; y + + )
for ( var y = 0 ; y < actual . Height ; y + + )
{
{
var expectedAlpha = expected [ x , y ] . A * scale ;
var expectedPixel = expectedPixels . Slice ( y * expectedRowBytes + x * 4 , 4 ) ;
var actualAlpha = actual [ x , y ] . A * scale ;
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 r = scale * ( expectedAlpha * expectedPixel [ 0 ] - actualAlpha * actualPixel [ 0 ] ) ;
var g = scale * ( expectedAlpha * expected [ x , y ] . G - actualAlpha * actual [ x , y ] . G ) ;
var g = scale * ( expectedAlpha * expectedPixel [ 1 ] - actualAlpha * actualPixel [ 1 ] ) ;
var b = scale * ( expectedAlpha * expected [ x , y ] . B - actualAlpha * actual [ x , y ] . B ) ;
var b = scale * ( expectedAlpha * expectedPixel [ 2 ] - actualAlpha * actualPixel [ 2 ] ) ;
var a = expectedAlpha - actualAlpha ;
var a = expectedAlpha - actualAlpha ;
var error = r * r + g * g + b * b + a * a ;
var error = r * r + g * g + b * b + a * a ;