RenderTests.cs 5.0 KB

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