diff --git a/Inflate.Tests/Bgra.cs b/Inflate.Tests/Bgra.cs new file mode 100644 index 0000000..e13f32c --- /dev/null +++ b/Inflate.Tests/Bgra.cs @@ -0,0 +1,12 @@ +namespace Inflate.Tests; + +internal readonly record struct Bgra(byte Blue, byte Green, byte Red, byte Alpha) +{ + public static Bgra Transparent => default; + + public static Bgra Opaque(byte blue, byte green, byte red) => new(blue, green, red, byte.MaxValue); + + public Bgra Premultiplied() => new(Premultiply(Blue), Premultiply(Green), Premultiply(Red), Alpha); + + byte Premultiply(byte value) => (byte)Math.Round(value * (double)Alpha / byte.MaxValue, MidpointRounding.AwayFromZero); +} diff --git a/Inflate.Tests/EffectDescriptions.cs b/Inflate.Tests/EffectDescriptions.cs new file mode 100644 index 0000000..3f85fa6 --- /dev/null +++ b/Inflate.Tests/EffectDescriptions.cs @@ -0,0 +1,28 @@ +using System.Drawing; +using System.Numerics; +using Vortice.Direct2D1; +using YukkuriMovieMaker.Player.Video; + +namespace Inflate.Tests; + +internal static class EffectDescriptions +{ + public const int Fps = 30; + + static readonly Size ScreenSize = new(1920, 1080); + + public static EffectDescription At(int frame, int length) + { + var timeline = new TimelineSourceDescription( + ScreenSize, + new FrameTime(frame, Fps), + new FrameTime(length, Fps), + Fps, + TimelineSourceUsage.Playing, + Guid.Empty, + []); + var item = new TimelineItemSourceDescription(timeline, frame, length, 0); + var draw = new DrawDescription(Vector3.Zero, Vector2.Zero, Vector2.One, Vector3.Zero, Matrix4x4.Identity, InterpolationMode.Linear, 1d, false, []); + return new EffectDescription(item, draw, 0, 1, 0, 1); + } +} diff --git a/Inflate.Tests/PackUriScheme.cs b/Inflate.Tests/PackUriScheme.cs new file mode 100644 index 0000000..e4f3ac5 --- /dev/null +++ b/Inflate.Tests/PackUriScheme.cs @@ -0,0 +1,10 @@ +using System.IO.Packaging; +using System.Runtime.CompilerServices; + +namespace Inflate.Tests; + +internal static class PackUriScheme +{ + [ModuleInitializer] + internal static void Register() => _ = PackUriHelper.UriSchemePack; +} diff --git a/Inflate.Tests/Rendering.cs b/Inflate.Tests/Rendering.cs new file mode 100644 index 0000000..21bdd93 --- /dev/null +++ b/Inflate.Tests/Rendering.cs @@ -0,0 +1,95 @@ +using System.Numerics; +using System.Runtime.InteropServices; +using Vortice.Direct2D1; +using Vortice.Mathematics; +using YukkuriMovieMaker.Commons; + +namespace Inflate.Tests; + +internal sealed class Rendering +{ + readonly byte[] pixels; + + public int Left { get; } + + public int Top { get; } + + public int Width { get; } + + public int Height { get; } + + public int Right => Left + Width; + + public int Bottom => Top + Height; + + Rendering(int left, int top, int width, int height, byte[] pixels) + { + Left = left; + Top = top; + Width = width; + Height = height; + this.pixels = pixels; + } + + public static Rendering Capture(IGraphicsDevicesAndContext devices, ID2D1Image image) + { + var context = devices.DeviceContext; + var bounds = context.GetImageLocalBounds(image); + var left = (int)Math.Floor(bounds.Left); + var top = (int)Math.Floor(bounds.Top); + var width = (int)Math.Ceiling(bounds.Right) - left; + var height = (int)Math.Ceiling(bounds.Bottom) - top; + var stride = width * SourceImage.BytesPerPixel; + var size = new SizeI(width, height); + + using var target = context.CreateBitmap(size, nint.Zero, stride, + new BitmapProperties1(SourceImage.PixelFormat, SourceImage.Dpi, SourceImage.Dpi, BitmapOptions.Target)); + using var readback = context.CreateBitmap(size, nint.Zero, stride, + new BitmapProperties1(SourceImage.PixelFormat, SourceImage.Dpi, SourceImage.Dpi, BitmapOptions.CpuRead | BitmapOptions.CannotDraw)); + + context.Target = target; + context.BeginDraw(); + context.Clear(new Color4(0f, 0f, 0f, 0f)); + context.DrawImage(image, new Vector2(-left, -top), null, InterpolationMode.NearestNeighbor, CompositeMode.SourceCopy); + context.EndDraw(); + context.Target = null; + + readback.CopyFromBitmap(target); + var pixels = new byte[stride * height]; + var mapped = readback.Map(MapOptions.Read); + try + { + for (var y = 0; y < height; y++) + Marshal.Copy(mapped.Bits + y * (int)mapped.Pitch, pixels, y * stride, stride); + } + finally + { + readback.Unmap(); + } + + return new Rendering(left, top, width, height, pixels); + } + + public Bgra this[int x, int y] + { + get + { + var offset = ((y - Top) * Width + (x - Left)) * SourceImage.BytesPerPixel; + return new Bgra(pixels[offset], pixels[offset + 1], pixels[offset + 2], pixels[offset + 3]); + } + } + + public bool Contains(int x, int y) => x >= Left && y >= Top && x < Right && y < Bottom; + + public bool SamePixelsAs(Rendering other) + => Left == other.Left && Top == other.Top && Width == other.Width && Height == other.Height && pixels.AsSpan().SequenceEqual(other.pixels); + + public IEnumerable<(int X, int Y)> Coordinates() + { + for (var y = Top; y < Bottom; y++) + { + for (var x = Left; x < Right; x++) + yield return (x, y); + } + } +} diff --git a/Inflate.Tests/SourceImage.cs b/Inflate.Tests/SourceImage.cs new file mode 100644 index 0000000..b6ff84c --- /dev/null +++ b/Inflate.Tests/SourceImage.cs @@ -0,0 +1,59 @@ +using Vortice.DCommon; +using Vortice.Direct2D1; +using Vortice.DXGI; +using Vortice.Mathematics; +using YukkuriMovieMaker.Commons; + +namespace Inflate.Tests; + +internal sealed class SourceImage : IDisposable +{ + public const float Dpi = 96f; + public const int BytesPerPixel = 4; + + public static readonly PixelFormat PixelFormat = new(Format.B8G8R8A8_UNorm, Vortice.DCommon.AlphaMode.Premultiplied); + + readonly Func pixelOf; + + public int Width { get; } + + public int Height { get; } + + public ID2D1Bitmap1 Bitmap { get; } + + public SourceImage(IGraphicsDevicesAndContext devices, int width, int height, Func pixelOf) + { + this.pixelOf = pixelOf; + Width = width; + Height = height; + + var stride = width * BytesPerPixel; + var pixels = new byte[stride * height]; + for (var y = 0; y < height; y++) + { + for (var x = 0; x < width; x++) + { + var (blue, green, red, alpha) = pixelOf(x, y).Premultiplied(); + var offset = y * stride + x * BytesPerPixel; + pixels[offset] = blue; + pixels[offset + 1] = green; + pixels[offset + 2] = red; + pixels[offset + 3] = alpha; + } + } + + Bitmap = devices.DeviceContext.CreateBitmap( + new SizeI(width, height), nint.Zero, stride, + new BitmapProperties1(PixelFormat, Dpi, Dpi, BitmapOptions.None)); + Bitmap.CopyFromMemory(pixels, stride); + } + + public static SourceImage Solid(IGraphicsDevicesAndContext devices, int width, int height, Bgra color) + => new(devices, width, height, (_, _) => color); + + public Bgra this[int x, int y] => pixelOf(x, y).Premultiplied(); + + public bool Contains(int x, int y) => x >= 0 && y >= 0 && x < Width && y < Height; + + public void Dispose() => Bitmap.Dispose(); +}