RenderTests.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using Avalonia.Headless.XUnit;
  2. using ChunkyImageLib;
  3. using ChunkyImageLib.DataHolders;
  4. using Drawie.Backend.Core;
  5. using Drawie.Backend.Core.Bridge;
  6. using Drawie.Backend.Core.ColorsImpl;
  7. using Drawie.Numerics;
  8. using PixiEditor.Models.IO;
  9. using Xunit.Abstractions;
  10. using Color = Drawie.Backend.Core.ColorsImpl.Color;
  11. namespace PixiEditor.Tests;
  12. public class RenderTests : FullPixiEditorTest
  13. {
  14. private readonly ITestOutputHelper _testOutputHelper;
  15. public RenderTests(ITestOutputHelper testOutputHelper)
  16. {
  17. _testOutputHelper = testOutputHelper;
  18. }
  19. [AvaloniaTheory]
  20. [InlineData("Fibi")]
  21. [InlineData("Pond")]
  22. [InlineData("SmlPxlCircShadWithMask")]
  23. [InlineData("SmallPixelArtCircleShadow")]
  24. [InlineData("SmlPxlCircShadWithMaskClipped")]
  25. [InlineData("SmlPxlCircShadWithMaskClippedInFolder")]
  26. [InlineData("VectorRectangleClippedToCircle")]
  27. [InlineData("VectorRectangleClippedToCircleShadowFilter")]
  28. [InlineData("VectorRectangleClippedToCircleMasked")]
  29. [InlineData("BlendingLinearSrgb")]
  30. [InlineData("BlendingSrgb")]
  31. [InlineData("VectorWithSepiaFilter")]
  32. [InlineData("VectorWithSepiaFilterSrgb")]
  33. [InlineData("VectorWithSepiaFilterChained")]
  34. [InlineData("Offset")]
  35. [InlineData("Scale")]
  36. [InlineData("Skew")]
  37. [InlineData("Rotation")]
  38. [InlineData("MatrixChain")]
  39. [InlineData("GpuOffset", "Offset")]
  40. [InlineData("GpuScale")]
  41. [InlineData("GpuSkew")]
  42. [InlineData("GpuMatrixChain")]
  43. public void TestThatPixiFilesRenderTheSameResultAsSavedPng(string fileName, string? resultName = null)
  44. {
  45. if (!DrawingBackendApi.Current.IsHardwareAccelerated)
  46. {
  47. _testOutputHelper.WriteLine("Skipping the test because hardware acceleration is not enabled.");
  48. return;
  49. }
  50. string pixiFile = Path.Combine("TestFiles", "RenderTests", fileName + ".pixi");
  51. string pngFile = Path.Combine("TestFiles", "RenderTests", (resultName ?? fileName) + ".png");
  52. var document = Importer.ImportDocument(pixiFile);
  53. Assert.NotNull(pngFile);
  54. var result = document.TryRenderWholeImage(0);
  55. Assert.True(result is { IsT1: true, AsT1: not null }); // Check if rendering was successful
  56. using var image = result.AsT1;
  57. using var snapshot = image.DrawingSurface.Snapshot();
  58. using var encoded = snapshot.Encode();
  59. using var renderedToCompare = Surface.Load(encoded.AsSpan().ToArray());
  60. using var toCompareTo = Importer.ImportImage(pngFile, document.SizeBindable);
  61. Assert.NotNull(toCompareTo);
  62. Assert.True(PixelCompare(renderedToCompare, toCompareTo));
  63. }
  64. [AvaloniaTheory]
  65. [InlineData("SingleLayer")]
  66. [InlineData("SingleLayerWithMask")]
  67. [InlineData("LayerWithMaskClipped")]
  68. [InlineData("LayerWithMaskClippedHighDpiPresent")]
  69. [InlineData("LayerWithMaskClippedInFolder")]
  70. [InlineData("LayerWithMaskClippedInFolderWithMask")]
  71. public void TestThatHalfResolutionScalesRenderCorrectly(string pixiName)
  72. {
  73. string pixiFile = Path.Combine("TestFiles", "ResolutionTests", pixiName + ".pixi");
  74. var document = Importer.ImportDocument(pixiFile);
  75. using Surface output = Surface.ForDisplay(document.SizeBindable);
  76. document.SceneRenderer.RenderScene(output.DrawingSurface, ChunkResolution.Half);
  77. Color expectedColor = Colors.Yellow;
  78. Assert.True(AllPixelsAreColor(output, expectedColor));
  79. }
  80. private static bool PixelCompare(Surface image, Surface compareTo)
  81. {
  82. if (image.Size != compareTo.Size)
  83. {
  84. return false;
  85. }
  86. using Surface compareSurface1 = new Surface(image.Size);
  87. using Surface compareSurface2 = new Surface(image.Size);
  88. compareSurface1.DrawingSurface.Canvas.DrawSurface(image.DrawingSurface, 0, 0);
  89. compareSurface2.DrawingSurface.Canvas.DrawSurface(compareTo.DrawingSurface, 0, 0);
  90. var imageData1 = compareSurface1.PeekPixels();
  91. var imageData2 = compareSurface2.PeekPixels();
  92. if (imageData1.Width != imageData2.Width || imageData1.Height != imageData2.Height)
  93. {
  94. return false;
  95. }
  96. for (int y = 0; y < imageData1.Height; y++)
  97. {
  98. for (int x = 0; x < imageData1.Width; x++)
  99. {
  100. var pixel1 = imageData1.GetPixelColor(x, y);
  101. var pixel2 = imageData2.GetPixelColor(x, y);
  102. if (pixel1 != pixel2)
  103. {
  104. return false;
  105. }
  106. }
  107. }
  108. return true;
  109. }
  110. private static bool AllPixelsAreColor(Surface image, Color color)
  111. {
  112. var imageData = image.PeekPixels();
  113. for (int y = 0; y < imageData.Height; y++)
  114. {
  115. for (int x = 0; x < imageData.Width; x++)
  116. {
  117. var pixel = imageData.GetPixelColor(x, y);
  118. if (pixel != color)
  119. {
  120. return false;
  121. }
  122. }
  123. }
  124. return true;
  125. }
  126. }