|
@@ -1,5 +1,6 @@
|
|
using System;
|
|
using System;
|
|
using System.IO;
|
|
using System.IO;
|
|
|
|
+using System.Runtime.CompilerServices;
|
|
using System.Runtime.InteropServices;
|
|
using System.Runtime.InteropServices;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Controls.Platform.Surfaces;
|
|
using Avalonia.Controls.Platform.Surfaces;
|
|
@@ -9,6 +10,8 @@ using Avalonia.Media;
|
|
using Avalonia.Media.Imaging;
|
|
using Avalonia.Media.Imaging;
|
|
using Avalonia.Platform;
|
|
using Avalonia.Platform;
|
|
using Xunit;
|
|
using Xunit;
|
|
|
|
+using Path = System.IO.Path;
|
|
|
|
+#pragma warning disable CS0649
|
|
|
|
|
|
#if AVALONIA_SKIA
|
|
#if AVALONIA_SKIA
|
|
namespace Avalonia.Skia.RenderTests
|
|
namespace Avalonia.Skia.RenderTests
|
|
@@ -60,13 +63,14 @@ namespace Avalonia.Direct2D1.RenderTests.Media
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
[Theory]
|
|
- [InlineData(PixelFormat.Rgba8888), InlineData(PixelFormat.Bgra8888),
|
|
|
|
|
|
+ [InlineData(PixelFormatEnum.Rgba8888), InlineData(PixelFormatEnum.Bgra8888),
|
|
#if AVALONIA_SKIA
|
|
#if AVALONIA_SKIA
|
|
- InlineData(PixelFormat.Rgb565)
|
|
|
|
|
|
+ InlineData(PixelFormatEnum.Rgb565)
|
|
#endif
|
|
#endif
|
|
]
|
|
]
|
|
- public void FramebufferRenderResultsShouldBeUsableAsBitmap(PixelFormat fmt)
|
|
|
|
|
|
+ internal void FramebufferRenderResultsShouldBeUsableAsBitmap(PixelFormatEnum fmte)
|
|
{
|
|
{
|
|
|
|
+ var fmt = new PixelFormat(fmte);
|
|
var testName = nameof(FramebufferRenderResultsShouldBeUsableAsBitmap) + "_" + fmt;
|
|
var testName = nameof(FramebufferRenderResultsShouldBeUsableAsBitmap) + "_" + fmt;
|
|
var fb = new Framebuffer(fmt, new PixelSize(80, 80));
|
|
var fb = new Framebuffer(fmt, new PixelSize(80, 80));
|
|
var r = Avalonia.AvaloniaLocator.Current.GetRequiredService<IPlatformRenderInterface>();
|
|
var r = Avalonia.AvaloniaLocator.Current.GetRequiredService<IPlatformRenderInterface>();
|
|
@@ -100,9 +104,10 @@ namespace Avalonia.Direct2D1.RenderTests.Media
|
|
}
|
|
}
|
|
|
|
|
|
[Theory]
|
|
[Theory]
|
|
- [InlineData(PixelFormat.Bgra8888), InlineData(PixelFormat.Rgba8888)]
|
|
|
|
- public void WriteableBitmapShouldBeUsable(PixelFormat fmt)
|
|
|
|
|
|
+ [InlineData(PixelFormatEnum.Bgra8888), InlineData(PixelFormatEnum.Rgba8888)]
|
|
|
|
+ internal void WriteableBitmapShouldBeUsable(PixelFormatEnum fmte)
|
|
{
|
|
{
|
|
|
|
+ var fmt = new PixelFormat(fmte);
|
|
var writeableBitmap = new WriteableBitmap(new PixelSize(256, 256), new Vector(96, 96), fmt);
|
|
var writeableBitmap = new WriteableBitmap(new PixelSize(256, 256), new Vector(96, 96), fmt);
|
|
|
|
|
|
var data = new int[256 * 256];
|
|
var data = new int[256 * 256];
|
|
@@ -126,5 +131,110 @@ namespace Avalonia.Direct2D1.RenderTests.Media
|
|
CompareImagesNoRenderer(name);
|
|
CompareImagesNoRenderer(name);
|
|
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ struct RawHeader
|
|
|
|
+ {
|
|
|
|
+ public int Width, Height, Stride;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ [Theory,
|
|
|
|
+ InlineData(PixelFormatEnum.BlackWhite),
|
|
|
|
+ InlineData(PixelFormatEnum.Gray2),
|
|
|
|
+ InlineData(PixelFormatEnum.Gray4),
|
|
|
|
+ InlineData(PixelFormatEnum.Gray8),
|
|
|
|
+ InlineData(PixelFormatEnum.Gray16),
|
|
|
|
+ InlineData(PixelFormatEnum.Gray32Float),
|
|
|
|
+ InlineData(PixelFormatEnum.Rgba64),
|
|
|
|
+ InlineData(PixelFormatEnum.Rgba64, AlphaFormat.Premul),
|
|
|
|
+ ]
|
|
|
|
+ internal unsafe void BitmapsShouldSupportTranscoders_Lenna(PixelFormatEnum format, AlphaFormat alphaFormat = AlphaFormat.Unpremul)
|
|
|
|
+ {
|
|
|
|
+ var relativeFilesDir = "../../../PixelFormats/Lenna";
|
|
|
|
+ var filesDir = Path.Combine(OutputPath, relativeFilesDir);
|
|
|
|
+
|
|
|
|
+ var formatName = format.ToString();
|
|
|
|
+ if (alphaFormat == AlphaFormat.Premul)
|
|
|
|
+ formatName = "P" + formatName.ToLowerInvariant();
|
|
|
|
+
|
|
|
|
+ var bitsData = File.ReadAllBytes(Path.Combine(filesDir, formatName + ".bits")).AsSpan();
|
|
|
|
+ var header = MemoryMarshal.Cast<byte, RawHeader>(bitsData.Slice(0, Unsafe.SizeOf<RawHeader>()))[0];
|
|
|
|
+ var data = bitsData.Slice(Unsafe.SizeOf<RawHeader>());
|
|
|
|
+
|
|
|
|
+ var size = new PixelSize(header.Width, header.Height);
|
|
|
|
+ var stride = header.Stride;
|
|
|
|
+
|
|
|
|
+ string expectedName = Path.Combine(relativeFilesDir, formatName);
|
|
|
|
+ if (!File.Exists(Path.Combine(OutputPath, expectedName + ".expected.png")))
|
|
|
|
+ expectedName = Path.Combine(relativeFilesDir, "Default");
|
|
|
|
+
|
|
|
|
+ foreach (var writable in new[] { false, true })
|
|
|
|
+ {
|
|
|
|
+ var testName = nameof(BitmapsShouldSupportTranscoders_Lenna) + "_" + formatName +
|
|
|
|
+ (writable ? "_Writeable" : "_Normal");
|
|
|
|
+
|
|
|
|
+ var path = System.IO.Path.Combine(OutputPath, testName + ".out.png");
|
|
|
|
+ fixed (byte* pData = data)
|
|
|
|
+ {
|
|
|
|
+ Bitmap? b = null;
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ if (writable)
|
|
|
|
+ {
|
|
|
|
+ var bmp = new WriteableBitmap(size, new Vector(96, 96), new PixelFormat(format),
|
|
|
|
+ alphaFormat);
|
|
|
|
+
|
|
|
|
+ using (var l = bmp.Lock())
|
|
|
|
+ {
|
|
|
|
+ var minStride = (l.Size.Width * l.Format.BitsPerPixel + 7) / 8;
|
|
|
|
+ for (var y = 0; y < size.Height; y++)
|
|
|
|
+ {
|
|
|
|
+ Unsafe.CopyBlock((l.Address + y * l.RowBytes).ToPointer(), pData + y * stride,
|
|
|
|
+ (uint)minStride);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ b = bmp;
|
|
|
|
+ var copyTo = new byte[data.Length];
|
|
|
|
+ fixed (byte* pCopyTo = copyTo)
|
|
|
|
+ b.CopyPixels(default, new IntPtr(pCopyTo), copyTo.Length, stride);
|
|
|
|
+ Assert.Equal(data.ToArray(), copyTo);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ b = new Bitmap(new PixelFormat(format), alphaFormat, new IntPtr(pData),
|
|
|
|
+ size, new Vector(96, 96), stride);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ b.Save(path);
|
|
|
|
+ CompareImagesNoRenderer(testName, expectedName);
|
|
|
|
+ }
|
|
|
|
+ finally
|
|
|
|
+ {
|
|
|
|
+ b?.Dispose();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ [Fact]
|
|
|
|
+ public unsafe void CopyPixelsShouldWorkForNonTranscodedBitmaps()
|
|
|
|
+ {
|
|
|
|
+ var stride = 32 * 4;
|
|
|
|
+ var data = new byte[32 * stride];
|
|
|
|
+ new Random().NextBytes(data);
|
|
|
|
+ for (var c = 0; c < data.Length; c++)
|
|
|
|
+ if (data[c] == 0)
|
|
|
|
+ data[c] = 1;
|
|
|
|
+
|
|
|
|
+ Bitmap bmp;
|
|
|
|
+ fixed (byte* pData = data)
|
|
|
|
+ bmp = new Bitmap(PixelFormat.Bgra8888, AlphaFormat.Unpremul, new IntPtr(pData), new PixelSize(32, 32),
|
|
|
|
+ new Vector(96, 96), 32 * 4);
|
|
|
|
+
|
|
|
|
+ var copyTo = new byte[data.Length];
|
|
|
|
+ fixed (byte* pCopyTo = copyTo)
|
|
|
|
+ bmp.CopyPixels(default, new IntPtr(pCopyTo), data.Length, stride);
|
|
|
|
+ Assert.Equal(data, copyTo);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|