SurfaceTests.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using PixiEditor.Models.DataHolders;
  2. using SkiaSharp;
  3. using Xunit;
  4. namespace PixiEditorTests.ModelsTests.DataHoldersTests
  5. {
  6. #pragma warning disable CA1001 // Types that own disposable fields should be disposable
  7. public class SurfaceTests
  8. #pragma warning restore CA1001 // Types that own disposable fields should be disposable
  9. {
  10. SKColor redColor = new SKColor(254, 2, 3);
  11. SKColor greenColor = new SKColor(6, 224, 3);
  12. SKPaint redPaint;
  13. SKPaint greenPaint;
  14. public SurfaceTests()
  15. {
  16. redPaint = new SKPaint()
  17. {
  18. Color = redColor,
  19. };
  20. greenPaint = new SKPaint()
  21. {
  22. Color = greenColor,
  23. };
  24. }
  25. [Fact]
  26. public void TestSurfaceSRGBPixelManipulation()
  27. {
  28. using Surface surface = new Surface(128, 200);
  29. surface.SkiaSurface.Canvas.Clear(SKColors.Red);
  30. surface.SkiaSurface.Canvas.DrawRect(new SKRect(10, 10, 70, 70), redPaint);
  31. surface.SetSRGBPixel(73, 21, greenColor);
  32. Assert.Equal(redColor, surface.GetSRGBPixel(14, 14));
  33. Assert.Equal(greenColor, surface.GetSRGBPixel(73, 21));
  34. }
  35. [Fact]
  36. public void TestSurfacePbgraBytes()
  37. {
  38. byte[] bytes = new byte[]
  39. {
  40. 123, 121, 141, 255, 014, 010, 007, 255,
  41. 042, 022, 055, 128, 024, 020, 021, 128,
  42. 040, 010, 055, 064, 042, 022, 005, 064,
  43. 005, 009, 001, 032, 001, 011, 016, 032,
  44. };
  45. using Surface surface = new Surface(2, 4, bytes);
  46. Assert.Equal(new SKColor(141, 121, 123, 255), surface.GetSRGBPixel(0, 0));
  47. Assert.Equal(new SKColor(110, 44, 84, 128), surface.GetSRGBPixel(0, 1));
  48. var newBytes = surface.ToByteArray();
  49. Assert.Equal(bytes, newBytes);
  50. }
  51. [Fact]
  52. public void TestCloneSurface()
  53. {
  54. using Surface original = new Surface(30, 40);
  55. original.SkiaSurface.Canvas.Clear(redColor);
  56. original.SkiaSurface.Canvas.DrawRect(5, 5, 10, 10, greenPaint);
  57. using Surface clone = new Surface(original);
  58. Assert.NotSame(original.SkiaSurface, clone.SkiaSurface);
  59. Assert.NotSame(original.SkiaSurface.Canvas, clone.SkiaSurface.Canvas);
  60. Assert.Equal(redColor, clone.GetSRGBPixel(3, 3));
  61. Assert.Equal(greenColor, clone.GetSRGBPixel(6, 6));
  62. }
  63. [Fact]
  64. public void TestSurfaceNearestNeighborResize()
  65. {
  66. using Surface original = new Surface(30, 40);
  67. original.SkiaSurface.Canvas.Clear(redColor);
  68. original.SkiaSurface.Canvas.DrawRect(5, 5, 20, 20, greenPaint);
  69. using Surface resized = original.ResizeNearestNeighbor(10, 10);
  70. Assert.Equal(10, resized.Width);
  71. Assert.Equal(10, resized.Height);
  72. Assert.Equal(redColor, resized.GetSRGBPixel(0, 0));
  73. Assert.Equal(redColor, resized.GetSRGBPixel(9, 9));
  74. Assert.Equal(greenColor, resized.GetSRGBPixel(5, 5));
  75. }
  76. [Fact]
  77. public void TestSurfaceToWriteableBitmap()
  78. {
  79. using Surface original = new Surface(30, 40);
  80. original.SkiaSurface.Canvas.Clear(redColor);
  81. original.SkiaSurface.Canvas.DrawRect(5, 5, 20, 20, greenPaint);
  82. var bitmap = original.ToWriteableBitmap();
  83. byte[] pixels = new byte[30 * 40 * 4];
  84. bitmap.CopyPixels(pixels, 30 * 4, 0);
  85. Assert.Equal(redColor, new SKColor(pixels[2], pixels[1], pixels[0], pixels[3]));
  86. int offset = (30 * 5 + 5) * 4;
  87. Assert.Equal(greenColor, new SKColor(pixels[2 + offset], pixels[1 + offset], pixels[0 + offset], pixels[3 + offset]));
  88. }
  89. [Fact]
  90. public void TestSurfaceFromWriteableBitmap()
  91. {
  92. using Surface original = new Surface(30, 30);
  93. original.SkiaSurface.Canvas.Clear(SKColors.Transparent);
  94. original.SkiaSurface.Canvas.DrawRect(5, 5, 20, 20, redPaint);
  95. original.SkiaSurface.Canvas.DrawRect(10, 10, 20, 20, greenPaint);
  96. using Surface fromWriteable = new Surface(original.ToWriteableBitmap());
  97. Assert.Equal(original.GetSRGBPixel(0, 0), fromWriteable.GetSRGBPixel(0, 0));
  98. Assert.Equal(original.GetSRGBPixel(6, 6), fromWriteable.GetSRGBPixel(6, 6));
  99. Assert.Equal(original.GetSRGBPixel(15, 15), fromWriteable.GetSRGBPixel(15, 15));
  100. }
  101. }
  102. }