RenderTests.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. public void TestThatPixiFilesRenderTheSameResultAsSavedPng(string fileName)
  35. {
  36. if (!DrawingBackendApi.Current.IsHardwareAccelerated)
  37. {
  38. _testOutputHelper.WriteLine("Skipping the test because hardware acceleration is not enabled.");
  39. return;
  40. }
  41. string pixiFile = Path.Combine("TestFiles", "RenderTests", fileName + ".pixi");
  42. string pngFile = Path.Combine("TestFiles", "RenderTests", fileName + ".png");
  43. var document = Importer.ImportDocument(pixiFile);
  44. Assert.NotNull(pngFile);
  45. var result = document.TryRenderWholeImage(0);
  46. Assert.True(result is { IsT1: true, AsT1: not null }); // Check if rendering was successful
  47. using var image = result.AsT1;
  48. using var toCompareTo = Importer.ImportImage(pngFile, document.SizeBindable);
  49. Assert.NotNull(toCompareTo);
  50. Assert.True(PixelCompare(image, toCompareTo));
  51. }
  52. [AvaloniaTheory]
  53. [InlineData("SingleLayer")]
  54. [InlineData("SingleLayerWithMask")]
  55. [InlineData("LayerWithMaskClipped")]
  56. [InlineData("LayerWithMaskClippedHighDpiPresent")]
  57. [InlineData("LayerWithMaskClippedInFolder")]
  58. [InlineData("LayerWithMaskClippedInFolderWithMask")]
  59. public void TestThatHalfResolutionScalesRenderCorrectly(string pixiName)
  60. {
  61. string pixiFile = Path.Combine("TestFiles", "ResolutionTests", pixiName + ".pixi");
  62. var document = Importer.ImportDocument(pixiFile);
  63. using Surface output = Surface.ForDisplay(document.SizeBindable);
  64. document.SceneRenderer.RenderScene(output.DrawingSurface, ChunkResolution.Half);
  65. Color expectedColor = Colors.Yellow;
  66. Assert.True(AllPixelsAreColor(output, expectedColor));
  67. }
  68. private static bool PixelCompare(Surface image, Surface compareTo)
  69. {
  70. if (image.Size != compareTo.Size)
  71. {
  72. return false;
  73. }
  74. using Surface compareSurface1 = new Surface(image.Size);
  75. using Surface compareSurface2 = new Surface(image.Size);
  76. compareSurface1.DrawingSurface.Canvas.DrawSurface(image.DrawingSurface, 0, 0);
  77. compareSurface2.DrawingSurface.Canvas.DrawSurface(compareTo.DrawingSurface, 0, 0);
  78. var imageData1 = compareSurface1.PeekPixels();
  79. var imageData2 = compareSurface2.PeekPixels();
  80. if (imageData1.Width != imageData2.Width || imageData1.Height != imageData2.Height)
  81. {
  82. return false;
  83. }
  84. for (int y = 0; y < imageData1.Height; y++)
  85. {
  86. for (int x = 0; x < imageData1.Width; x++)
  87. {
  88. var pixel1 = imageData1.GetPixelColor(x, y);
  89. var pixel2 = imageData2.GetPixelColor(x, y);
  90. if (pixel1 != pixel2)
  91. {
  92. return false;
  93. }
  94. }
  95. }
  96. return true;
  97. }
  98. private static bool AllPixelsAreColor(Surface image, Color color)
  99. {
  100. var imageData = image.PeekPixels();
  101. for (int y = 0; y < imageData.Height; y++)
  102. {
  103. for (int x = 0; x < imageData.Width; x++)
  104. {
  105. var pixel = imageData.GetPixelColor(x, y);
  106. if (pixel != color)
  107. {
  108. return false;
  109. }
  110. }
  111. }
  112. return true;
  113. }
  114. }