BitmapTests.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Platform.Surfaces;
  6. using Avalonia.Controls.Shapes;
  7. using Avalonia.Layout;
  8. using Avalonia.Media;
  9. using Avalonia.Media.Imaging;
  10. using Avalonia.Platform;
  11. using Xunit;
  12. #if AVALONIA_SKIA
  13. namespace Avalonia.Skia.RenderTests
  14. #else
  15. namespace Avalonia.Direct2D1.RenderTests.Media
  16. #endif
  17. {
  18. public class BitmapTests : TestBase
  19. {
  20. public BitmapTests()
  21. : base(@"Media\Bitmap")
  22. {
  23. Directory.CreateDirectory(OutputPath);
  24. }
  25. class Framebuffer : ILockedFramebuffer, IFramebufferPlatformSurface
  26. {
  27. public Framebuffer(PixelFormat fmt, PixelSize size)
  28. {
  29. Format = fmt;
  30. var bpp = fmt == PixelFormat.Rgb565 ? 2 : 4;
  31. Size = size;
  32. RowBytes = bpp * size.Width;
  33. Address = Marshal.AllocHGlobal(size.Height * RowBytes);
  34. }
  35. public IntPtr Address { get; }
  36. public Vector Dpi { get; } = new Vector(96, 96);
  37. public PixelFormat Format { get; }
  38. public PixelSize Size { get; }
  39. public int RowBytes { get; }
  40. public void Dispose()
  41. {
  42. //no-op
  43. }
  44. public ILockedFramebuffer Lock()
  45. {
  46. return this;
  47. }
  48. public void Deallocate() => Marshal.FreeHGlobal(Address);
  49. }
  50. [Theory]
  51. [InlineData(PixelFormat.Rgba8888), InlineData(PixelFormat.Bgra8888),
  52. #if AVALONIA_SKIA
  53. InlineData(PixelFormat.Rgb565)
  54. #endif
  55. ]
  56. public void FramebufferRenderResultsShouldBeUsableAsBitmap(PixelFormat fmt)
  57. {
  58. var testName = nameof(FramebufferRenderResultsShouldBeUsableAsBitmap) + "_" + fmt;
  59. var fb = new Framebuffer(fmt, new PixelSize(80, 80));
  60. var r = Avalonia.AvaloniaLocator.Current.GetService<IPlatformRenderInterface>();
  61. using (var target = r.CreateRenderTarget(new object[] { fb }))
  62. using (var ctx = target.CreateDrawingContext(null))
  63. {
  64. ctx.Clear(Colors.Transparent);
  65. ctx.PushOpacity(0.8);
  66. ctx.DrawRectangle(Brushes.Chartreuse, null, new Rect(0, 0, 20, 100));
  67. ctx.DrawRectangle(Brushes.Crimson, null, new Rect(20, 0, 20, 100));
  68. ctx.DrawRectangle(Brushes.Gold,null, new Rect(40, 0, 20, 100));
  69. ctx.PopOpacity();
  70. }
  71. var bmp = new Bitmap(fmt, AlphaFormat.Premul, fb.Address, fb.Size, new Vector(96, 96), fb.RowBytes);
  72. fb.Deallocate();
  73. using (var rtb = new RenderTargetBitmap(new PixelSize(100, 100), new Vector(96, 96)))
  74. {
  75. using (var ctx = rtb.CreateDrawingContext(null))
  76. {
  77. ctx.DrawRectangle(Brushes.Blue, null, new Rect(0, 0, 100, 100));
  78. ctx.DrawRectangle(Brushes.Pink, null, new Rect(0, 20, 100, 10));
  79. var rc = new Rect(0, 0, 60, 60);
  80. ctx.DrawBitmap(bmp.PlatformImpl, 1, rc, rc);
  81. }
  82. rtb.Save(System.IO.Path.Combine(OutputPath, testName + ".out.png"));
  83. }
  84. CompareImagesNoRenderer(testName);
  85. }
  86. [Theory]
  87. [InlineData(PixelFormat.Bgra8888), InlineData(PixelFormat.Rgba8888)]
  88. public void WriteableBitmapShouldBeUsable(PixelFormat fmt)
  89. {
  90. #pragma warning disable CS0618 // Type or member is obsolete
  91. var writeableBitmap = new WriteableBitmap(new PixelSize(256, 256), new Vector(96, 96), fmt);
  92. #pragma warning restore CS0618 // Type or member is obsolete
  93. var data = new int[256 * 256];
  94. for (int y = 0; y < 256; y++)
  95. for (int x = 0; x < 256; x++)
  96. data[y * 256 + x] =(int)((uint)(x + (y << 8)) | 0xFF000000u);
  97. using (var l = writeableBitmap.Lock())
  98. {
  99. for(var r = 0; r<256; r++)
  100. {
  101. Marshal.Copy(data, r * 256, new IntPtr(l.Address.ToInt64() + r * l.RowBytes), 256);
  102. }
  103. }
  104. var name = nameof(WriteableBitmapShouldBeUsable) + "_" + fmt;
  105. writeableBitmap.Save(System.IO.Path.Combine(OutputPath, name + ".out.png"));
  106. CompareImagesNoRenderer(name);
  107. }
  108. }
  109. }