From ebb387c6c0f6ba0cfd202a3518f1d23daf4f8154 Mon Sep 17 00:00:00 2001 From: routersys Date: Tue, 20 Oct 2026 09:11:50 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=AB=E3=82=B9=E3=82=BF=E3=83=A0=E3=82=A8?= =?UTF-8?q?=E3=83=95=E3=82=A7=E3=82=AF=E3=83=88=E3=81=AE=E8=A9=A6=E9=A8=93?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Inflate.Tests/InflateCustomEffectTests.cs | 218 ++++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 Inflate.Tests/InflateCustomEffectTests.cs diff --git a/Inflate.Tests/InflateCustomEffectTests.cs b/Inflate.Tests/InflateCustomEffectTests.cs new file mode 100644 index 0000000..deac125 --- /dev/null +++ b/Inflate.Tests/InflateCustomEffectTests.cs @@ -0,0 +1,218 @@ +using System.Numerics; +using System.Reflection; +using Vortice; +using Vortice.Direct2D1; +using Vortice.Direct2D1.Effects; +using Vortice.Mathematics; +using YukkuriMovieMaker.Commons; + +namespace Inflate.Tests; + +[Collection("Direct2D")] +public sealed class InflateCustomEffectTests +{ + const int AmountIndex = 0; + const int Width = 40; + const int Height = 24; + + static readonly Bgra Blue = Bgra.Opaque(255, 0, 0); + static readonly Bgra HalfTone = new(224, 192, 160, 128); + + static Rendering Render(IGraphicsDevicesAndContext devices, ID2D1Image source, ID2D1Image inflated, float amount) + { + using var effect = new InflateCustomEffect(devices); + effect.SetInput(0, source, true); + effect.SetInput(1, inflated, true); + effect.Amount = amount; + using var output = effect.Output; + return Rendering.Capture(devices, output); + } + + static AffineTransform2D Translate(IGraphicsDevicesAndContext devices, ID2D1Image image, float dx, float dy) + { + var transform = new AffineTransform2D(devices.DeviceContext) + { + InterPolationMode = AffineTransform2DInterpolationMode.NearestNeighbor, + BorderMode = BorderMode.Hard, + TransformMatrix = Matrix3x2.CreateTranslation(dx, dy), + }; + transform.SetInput(0, image, true); + return transform; + } + + static ID2D1Bitmap1 Premultiplied(IGraphicsDevicesAndContext devices, int width, int height, Bgra color) + { + var stride = width * SourceImage.BytesPerPixel; + var pixels = new byte[stride * height]; + for (var offset = 0; offset < pixels.Length; offset += SourceImage.BytesPerPixel) + { + pixels[offset] = color.Blue; + pixels[offset + 1] = color.Green; + pixels[offset + 2] = color.Red; + pixels[offset + 3] = color.Alpha; + } + + var bitmap = devices.DeviceContext.CreateBitmap( + new SizeI(width, height), nint.Zero, stride, + new BitmapProperties1(SourceImage.PixelFormat, SourceImage.Dpi, SourceImage.Dpi, BitmapOptions.None)); + bitmap.CopyFromMemory(pixels, stride); + return bitmap; + } + + static bool WithinRounding(Bgra expected, Bgra actual) + => Math.Abs(expected.Blue - actual.Blue) <= 1 && Math.Abs(expected.Green - actual.Green) <= 1 && Math.Abs(expected.Red - actual.Red) <= 1 && Math.Abs(expected.Alpha - actual.Alpha) <= 1; + + static Bgra Mix(Bgra source, Bgra inflated, float amount) + { + byte Channel(byte sourceValue, byte inflatedValue) => (byte)Math.Round(sourceValue + (inflatedValue - sourceValue) * (double)amount, MidpointRounding.AwayFromZero); + return new Bgra(Channel(source.Blue, inflated.Blue), Channel(source.Green, inflated.Green), Channel(source.Red, inflated.Red), Channel(source.Alpha, inflated.Alpha)); + } + + [Fact] + public void TheEffectIsEnabledOnceCreated() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + using var effect = new InflateCustomEffect(context); + + Assert.True(effect.IsEnabled); + } + + [Fact] + public void TheAmountStartsFromZero() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + using var effect = new InflateCustomEffect(context); + + Assert.Equal(0f, effect.GetFloatValue(AmountIndex)); + } + + [Theory] + [InlineData(-0.5f, 0f)] + [InlineData(0f, 0f)] + [InlineData(0.4f, 0.4f)] + [InlineData(1f, 1f)] + [InlineData(1.5f, 1f)] + [InlineData(float.MaxValue, 1f)] + public void TheAmountIsClampedToTheUnitRange(float value, float expected) + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + using var effect = new InflateCustomEffect(context); + + effect.Amount = value; + + Assert.Equal(expected, effect.GetFloatValue(AmountIndex)); + } + + [Fact] + public void TheOutputBoundsFollowTheSourceAlone() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + using var source = SourceImage.Solid(context, Width, Height, Blue); + using var inflated = SourceImage.Solid(context, 8, 8, HalfTone); + using var moved = Translate(context, inflated.Bitmap, 50f, -6f); + using var movedOutput = moved.Output; + using var effect = new InflateCustomEffect(context); + effect.SetInput(0, source.Bitmap, true); + effect.SetInput(1, movedOutput, true); + using var output = effect.Output; + + var bounds = context.DeviceContext.GetImageLocalBounds(output); + + Assert.Equal(0f, bounds.Left); + Assert.Equal(0f, bounds.Top); + Assert.Equal((float)Width, bounds.Right); + Assert.Equal((float)Height, bounds.Bottom); + } + + [Theory] + [InlineData(0f)] + [InlineData(-1f)] + public void WithoutAmountTheSourcePassesThroughUntouched(float amount) + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + using var source = SourceImage.Solid(context, Width, Height, Blue); + using var inflated = SourceImage.Solid(context, Width, Height, HalfTone); + + var rendering = Render(context, source.Bitmap, inflated.Bitmap, amount); + + Assert.All(rendering.Coordinates(), point => Assert.Equal(source[point.X, point.Y], rendering[point.X, point.Y])); + } + + [Theory] + [InlineData(1f)] + [InlineData(0.5f)] + [InlineData(0.25f)] + public void TheSourceFadesIntoTheInflatedByTheAmount(float amount) + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + using var source = SourceImage.Solid(context, Width, Height, Blue); + using var inflated = SourceImage.Solid(context, Width, Height, HalfTone); + var expected = Mix(Blue.Premultiplied(), HalfTone.Premultiplied(), amount); + + var rendering = Render(context, source.Bitmap, inflated.Bitmap, amount); + + Assert.All(rendering.Coordinates(), point => Assert.True(WithinRounding(expected, rendering[point.X, point.Y]), $"({point.X}, {point.Y}) {rendering[point.X, point.Y]}")); + } + + [Fact] + public void AnAmountChangedAfterADrawReachesTheNextDraw() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + using var source = SourceImage.Solid(context, Width, Height, Blue); + using var inflated = SourceImage.Solid(context, Width, Height, HalfTone); + using var effect = new InflateCustomEffect(context); + effect.SetInput(0, source.Bitmap, true); + effect.SetInput(1, inflated.Bitmap, true); + using var output = effect.Output; + effect.Amount = 0.25f; + var first = Rendering.Capture(context, output); + + effect.Amount = 0.75f; + var second = Rendering.Capture(context, output); + + var expectedFirst = Mix(Blue.Premultiplied(), HalfTone.Premultiplied(), 0.25f); + var expectedSecond = Mix(Blue.Premultiplied(), HalfTone.Premultiplied(), 0.75f); + Assert.All(first.Coordinates(), point => Assert.True(WithinRounding(expectedFirst, first[point.X, point.Y]))); + Assert.All(second.Coordinates(), point => Assert.True(WithinRounding(expectedSecond, second[point.X, point.Y]))); + } + + [Fact] + public void WithoutAnyInputTheOutputRectIsEmptyAndWithInputsItIsTheFirstOne() + { + var type = typeof(InflateCustomEffect).GetNestedType("EffectImpl", BindingFlags.NonPublic)!; + var implementation = Activator.CreateInstance(type)!; + var method = type.GetMethod("MapInputRectsToOutputRect")!; + var first = new RawRect(3, 4, 50, 60); + var second = new RawRect(1, 1, 99, 99); + + var none = new object?[] { Array.Empty(), Array.Empty(), default(RawRect), default(RawRect) }; + method.Invoke(implementation, none); + var some = new object?[] { new[] { first, second }, new RawRect[2], default(RawRect), default(RawRect) }; + method.Invoke(implementation, some); + + Assert.Equal(default(RawRect), (RawRect)none[2]!); + Assert.Equal(first, (RawRect)some[2]!); + Assert.Equal(default(RawRect), (RawRect)some[3]!); + } + + [Fact] + public void ColorsBrighterThanTheirAlphaAreHeldToTheAlpha() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + using var source = SourceImage.Solid(context, Width, Height, Bgra.Transparent); + using var inflated = Premultiplied(context, Width, Height, new Bgra(240, 200, 255, 128)); + + var rendering = Render(context, source.Bitmap, inflated, 1f); + + Assert.All(rendering.Coordinates(), point => Assert.True(WithinRounding(new Bgra(128, 128, 128, 128), rendering[point.X, point.Y]), $"({point.X}, {point.Y}) {rendering[point.X, point.Y]}")); + inflated.Dispose(); + } +}