BitmapTests.cs 4.3 KB

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