SurfaceTests.cs 4.3 KB

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