From cef5bdd32fcb0279b83159c6326697b5181ba964 Mon Sep 17 00:00:00 2001 From: routersys Date: Wed, 23 Sep 2026 14:50:23 +0900 Subject: [PATCH] =?UTF-8?q?=E5=87=A6=E7=90=86=E5=99=A8=E3=81=AE=E8=A9=A6?= =?UTF-8?q?=E9=A8=93=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 --- .../InkDispersionEffectProcessorTests.cs | 229 ++++++++++++++++++ 1 file changed, 229 insertions(+) create mode 100644 InkDispersion.Tests/InkDispersionEffectProcessorTests.cs diff --git a/InkDispersion.Tests/InkDispersionEffectProcessorTests.cs b/InkDispersion.Tests/InkDispersionEffectProcessorTests.cs new file mode 100644 index 0000000..fb27ccf --- /dev/null +++ b/InkDispersion.Tests/InkDispersionEffectProcessorTests.cs @@ -0,0 +1,229 @@ +using System.Globalization; +using System.Windows.Media; +using ComputeWeave; +using YukkuriMovieMaker.Commons; +using YukkuriMovieMaker.Json; +using YukkuriMovieMaker.Player.Video; + +namespace InkDispersion.Tests; + +[Collection("Direct2D")] +public sealed class InkDispersionEffectProcessorTests +{ + const int Size = 64; + const int Length = 30; + + static readonly Bgra Gray = Bgra.Opaque(192, 192, 192); + + static Bgra CenteredSquare(int x, int y) => x is >= 16 and < 48 && y is >= 16 and < 48 ? Gray : Bgra.Transparent; + + static void RequireInterop(IGraphicsDevicesAndContext devices) + { + using var scheduler = ComputeExternalQueueScheduler.Create(); + using var provider = InkDispersionInteropProvider.TryCreate(devices, scheduler, out var device); + if (provider is null || device is null) + Assert.Skip("Direct3D 11 and Direct3D 12 sharing is unavailable."); + } + + static Animation Linear(double from, double to) + => Json.LoadFromText(string.Create(CultureInfo.InvariantCulture, $$"""{"AnimationType":"直線移動","Values":[{"Value":{{from}}},{"Value":{{to}}}]}"""))!; + + static Rendering RenderFrame(IGraphicsDevicesAndContext devices, IVideoEffectProcessor processor, int frame) + { + processor.Update(EffectDescriptions.At(frame, Length)); + return Rendering.Capture(devices, processor.Output); + } + + static void AssertSameAsSource(Rendering rendering, SourceImage source) + { + Assert.Equal((0, 0, source.Width, source.Height), (rendering.Left, rendering.Top, rendering.Width, rendering.Height)); + Assert.All(rendering.Coordinates(), point => Assert.Equal(source[point.X, point.Y], rendering[point.X, point.Y])); + } + + static bool HasInkOutside(Rendering rendering, SourceImage source) + => rendering.Coordinates().Any(point => (!source.Contains(point.X, point.Y) || source[point.X, point.Y].Alpha == 0) && rendering[point.X, point.Y].Alpha > 0); + + [Fact] + public void TheProcessorHandsTheDrawDescriptionBackUnchanged() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + using var source = new SourceImage(context, Size, Size, CenteredSquare); + using var processor = new InkDispersionEffect().CreateVideoEffect(context); + processor.SetInput(source.Bitmap); + var description = EffectDescriptions.At(0, Length); + + var draw = processor.Update(description); + + Assert.Same(description.DrawDescription, draw); + } + + [Fact] + public void TheInkSpreadsOutOfTheSource() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + RequireInterop(context); + using var source = new SourceImage(context, Size, Size, CenteredSquare); + using var processor = new InkDispersionEffect().CreateVideoEffect(context); + processor.SetInput(source.Bitmap); + + var rendering = RenderFrame(context, processor, 0); + + Assert.True(HasInkOutside(rendering, source)); + Assert.All(rendering.Coordinates(), point => + { + var pixel = rendering[point.X, point.Y]; + Assert.InRange(pixel.Blue, 0, pixel.Alpha); + Assert.InRange(pixel.Green, 0, pixel.Alpha); + Assert.InRange(pixel.Red, 0, pixel.Alpha); + }); + } + + public static readonly TheoryData> PassThroughSettings = new() + { + { nameof(InkDispersionEffect.Amount), effect => effect.Amount.Values[0].Value = 0d }, + { nameof(InkDispersionEffect.Spread), effect => effect.Spread.Values[0].Value = 0d }, + }; + + [Theory] + [MemberData(nameof(PassThroughSettings))] + public void AZeroAmountOrSpreadPassesTheImageThrough(string setting, Action configure) + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + using var source = new SourceImage(context, Size, Size, CenteredSquare); + var effect = new InkDispersionEffect(); + configure(effect); + using var processor = effect.CreateVideoEffect(context); + processor.SetInput(source.Bitmap); + + var rendering = RenderFrame(context, processor, 0); + + Assert.Equal((0, 0, Size, Size), (rendering.Left, rendering.Top, rendering.Width, rendering.Height)); + Assert.All(rendering.Coordinates(), point => Assert.True(source[point.X, point.Y] == rendering[point.X, point.Y], $"{setting} ({point.X}, {point.Y})")); + } + + [Fact] + public void ReturningToAFrameReproducesItExactly() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + RequireInterop(context); + using var source = new SourceImage(context, Size, Size, CenteredSquare); + using var processor = new InkDispersionEffect().CreateVideoEffect(context); + processor.SetInput(source.Bitmap); + + var first = RenderFrame(context, processor, 4); + RenderFrame(context, processor, 9); + var again = RenderFrame(context, processor, 4); + + Assert.True(first.SamePixelsAs(again)); + } + + [Fact] + public void ANewProcessorDrawsTheSameInk() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + RequireInterop(context); + using var source = new SourceImage(context, Size, Size, CenteredSquare); + var effect = new InkDispersionEffect { Seed = 9 }; + using var firstProcessor = effect.CreateVideoEffect(context); + firstProcessor.SetInput(source.Bitmap); + var first = RenderFrame(context, firstProcessor, 0); + using var secondProcessor = effect.CreateVideoEffect(context); + secondProcessor.SetInput(source.Bitmap); + + var second = RenderFrame(context, secondProcessor, 0); + + Assert.True(first.SamePixelsAs(second)); + } + + [Fact] + public void AnimatedSpreadIsReadAtEachFrame() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + RequireInterop(context); + using var source = new SourceImage(context, Size, Size, CenteredSquare); + var effect = new InkDispersionEffect(); + effect.Spread.CopyFrom(Linear(0d, 100d)); + using var processor = effect.CreateVideoEffect(context); + processor.SetInput(source.Bitmap); + + var start = RenderFrame(context, processor, 0); + var end = RenderFrame(context, processor, Length - 1); + + AssertSameAsSource(start, source); + Assert.True(HasInkOutside(end, source)); + } + + public static readonly TheoryData> LaterChanges = new() + { + { nameof(InkDispersionEffect.Amount), effect => effect.Amount.Values[0].Value = 50d }, + { nameof(InkDispersionEffect.Spread), effect => effect.Spread.Values[0].Value = 30d }, + { nameof(InkDispersionEffect.Quality), effect => effect.Quality = InkDispersionQuality.Balanced }, + { nameof(InkDispersionEffect.Water), effect => effect.Water.Values[0].Value = 100d }, + { nameof(InkDispersionEffect.Reach), effect => effect.Reach.Values[0].Value = 10d }, + { nameof(InkDispersionEffect.Fiber), effect => effect.Fiber.Values[0].Value = 100d }, + { nameof(InkDispersionEffect.Sizing), effect => effect.Sizing.Values[0].Value = 100d }, + { nameof(InkDispersionEffect.Viscosity), effect => effect.Viscosity.Values[0].Value = 100d }, + { nameof(InkDispersionEffect.Drying), effect => effect.Drying.Values[0].Value = 100d }, + { nameof(InkDispersionEffect.Seed), effect => effect.Seed = 1 }, + { nameof(InkDispersionEffect.Density), effect => effect.Density.Values[0].Value = 20d }, + { nameof(InkDispersionEffect.Grain), effect => effect.Grain.Values[0].Value = 100d }, + { nameof(InkDispersionEffect.InkColor), effect => effect.InkColor = Colors.Crimson }, + }; + + [Theory] + [MemberData(nameof(LaterChanges))] + public void EverySettingChangedAfterTheFirstFrameReachesTheEffect(string setting, Action change) + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + RequireInterop(context); + using var source = new SourceImage(context, Size, Size, CenteredSquare); + var effect = new InkDispersionEffect(); + using var processor = effect.CreateVideoEffect(context); + processor.SetInput(source.Bitmap); + + var before = RenderFrame(context, processor, 0); + change(effect); + var after = RenderFrame(context, processor, 0); + + Assert.False(before.SamePixelsAs(after), setting); + } + + [Theory] + [InlineData(32, 128)] + [InlineData(128, 32)] + public void AResizedSourceKeepsProducingInk(int firstSize, int secondSize) + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + RequireInterop(context); + using var first = new SourceImage(context, firstSize, firstSize, (x, y) => Gray); + using var second = new SourceImage(context, secondSize, secondSize, (x, y) => Gray); + using var processor = new InkDispersionEffect().CreateVideoEffect(context); + processor.SetInput(first.Bitmap); + var before = RenderFrame(context, processor, 0); + + processor.SetInput(second.Bitmap); + var after = RenderFrame(context, processor, 0); + + Assert.True(HasInkOutside(before, first)); + Assert.True(HasInkOutside(after, second)); + Assert.Equal(secondSize > firstSize, after.Width > before.Width); + } + + [Fact] + public void AFailureWhileUpdatingIsNotSwallowed() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + using var processor = new InkDispersionEffect().CreateVideoEffect(context); + + Assert.ThrowsAny(() => processor.Update(null!)); + } +}