diff --git a/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs b/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs index 1be0f68..2a623e3 100644 --- a/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs +++ b/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs @@ -515,4 +515,30 @@ public sealed class CpuTileConvolverTests Assert.Equal(CpuTileConvolver.ToUnorm(stored[pixel * 4 + 3] * gain), output[pixel * 4 + 3]); } } + + [Theory] + [InlineData(false, 1f)] + [InlineData(true, 1f)] + [InlineData(true, 1.8f)] + [InlineData(false, 2.5f)] + public void ADitheredTranslucentSceneKeepsEveryColorBelowItsOpacity(bool linear, float gain) + { + var light = new LightOptions(linear, true, 0f, 0f); + var scene = ConvolutionScene.Create(ConvolutionScene.Uniform(240, 160, 128), 240, 160, 8, 3, 64, light); + + var (output, _) = RunLight(scene, 3, gain); + + var exceeding = 0; + var translucent = 0; + for (var index = 0; index < output.Length; index += 4) + { + if (output[index + 3] is > 0 and < 255) + translucent++; + if (output[index] > output[index + 3] || output[index + 1] > output[index + 3] || output[index + 2] > output[index + 3]) + exceeding++; + } + + Assert.True(translucent > 1000, $"{translucent}"); + Assert.Equal(0, exceeding); + } } diff --git a/SpectralConvolution/SpectralConvolution.Tests/GpuTileConvolverTests.cs b/SpectralConvolution/SpectralConvolution.Tests/GpuTileConvolverTests.cs index 49aec8e..9562a9d 100644 --- a/SpectralConvolution/SpectralConvolution.Tests/GpuTileConvolverTests.cs +++ b/SpectralConvolution/SpectralConvolution.Tests/GpuTileConvolverTests.cs @@ -553,4 +553,29 @@ public sealed class GpuTileConvolverTests Assert.Equal(tile == lastTile, Marked(entries, tile)); Assert.Equal(second[0], entries[0]); } + + [Theory] + [InlineData(false, 1f)] + [InlineData(true, 1f)] + [InlineData(true, 1.8f)] + public void ADitheredTranslucentSceneKeepsEveryColorBelowItsOpacity(bool linear, float gain) + { + var light = new LightOptions(linear, true, 0f, 0f); + var scene = ConvolutionScene.Create(ConvolutionScene.Uniform(240, 160, 128), 240, 160, 8, 3, 64, light); + + var run = Run(HardwareOrDefault(), scene, gain); + + var exceeding = 0; + var translucent = 0; + for (var index = 0; index < run.Output.Length; index += 4) + { + if (run.Output[index + 3] is > 0 and < 255) + translucent++; + if (run.Output[index] > run.Output[index + 3] || run.Output[index + 1] > run.Output[index + 3] || run.Output[index + 2] > run.Output[index + 3]) + exceeding++; + } + + Assert.True(translucent > 1000, $"{translucent}"); + Assert.Equal(0, exceeding); + } } diff --git a/SpectralConvolution/SpectralConvolution.Tests/LightTransformTests.cs b/SpectralConvolution/SpectralConvolution.Tests/LightTransformTests.cs index bc41a4e..958da32 100644 --- a/SpectralConvolution/SpectralConvolution.Tests/LightTransformTests.cs +++ b/SpectralConvolution/SpectralConvolution.Tests/LightTransformTests.cs @@ -246,7 +246,7 @@ public sealed class LightTransformTests { for (var x = 0; x < 300; x++) { - var threshold = LightTransform.DitherThreshold(x, y, 0); + var threshold = LightTransform.DitherThreshold(x, y); Assert.InRange(threshold, 0f, 0.99999994f); sum += threshold; count++; @@ -257,13 +257,14 @@ public sealed class LightTransformTests } [Fact] - public void TheDitherThresholdIsDeterministicAndDiffersByChannel() + public void TheDitherThresholdIsDeterministicAndDiffersByPosition() { - Assert.Equal(LightTransform.DitherThreshold(12, 34, 1), LightTransform.DitherThreshold(12, 34, 1)); + Assert.Equal(LightTransform.DitherThreshold(12, 34), LightTransform.DitherThreshold(12, 34)); var differing = 0; for (var x = 0; x < 64; x++) { - if (LightTransform.DitherThreshold(x, 7, 0) != LightTransform.DitherThreshold(x, 7, 1)) + if (LightTransform.DitherThreshold(x, 7) != LightTransform.DitherThreshold(x + 1, 7) + && LightTransform.DitherThreshold(x, 7) != LightTransform.DitherThreshold(x, 8)) differing++; } @@ -349,7 +350,7 @@ public sealed class LightTransformTests { for (var x = 0; x < 2048; x++) { - if (LightTransform.DitherThreshold(x, y, 0) < 0.99999f && LightTransform.DitherThreshold(x, y, 3) < 0.99999f) + if (LightTransform.DitherThreshold(x, y) < 0.99999f) continue; nearOne++; @@ -447,4 +448,48 @@ public sealed class LightTransformTests ((byte)packed, (byte)(packed >> 8), (byte)(packed >> 16), (byte)(packed >> 24))); } } + + [Theory] + [InlineData(false, true, 1f)] + [InlineData(false, true, 3f)] + [InlineData(true, true, 1f)] + [InlineData(true, true, 2f)] + [InlineData(true, false, 1f)] + public void AColorNeverExceedsItsOpacityEvenWhenDithered(bool linear, bool dither, float gain) + { + var options = new LightOptions(linear, dither, 0f, 0f); + var random = new Random(31); + for (var index = 0; index < 100_000; index++) + { + var alpha = (float)random.NextDouble(); + var red = random.Next(3) == 0 ? alpha : alpha * (float)random.NextDouble(); + var green = random.Next(3) == 0 ? alpha : alpha * (float)random.NextDouble(); + var blue = random.Next(3) == 0 ? alpha : alpha * (float)random.NextDouble(); + var x = random.Next(0, 4000); + var y = random.Next(0, 4000); + + var expected = LightTransform.ToBytes(red, green, blue, alpha, gain, options, x, y); + + Assert.InRange(expected.Red, 0, expected.Alpha); + Assert.InRange(expected.Green, 0, expected.Alpha); + Assert.InRange(expected.Blue, 0, expected.Alpha); + } + } + + [Fact] + public void ATranslucentWhiteStaysPremultipliedWhenDithered() + { + var options = new LightOptions(false, true, 0f, 0f); + for (var y = 0; y < 100; y++) + { + for (var x = 0; x < 100; x++) + { + var (blue, green, red, alpha) = LightTransform.ToBytes(127.5f / 255f, 127.5f / 255f, 127.5f / 255f, 127.5f / 255f, 1f, options, x, y); + + Assert.InRange(red, 0, alpha); + Assert.InRange(green, 0, alpha); + Assert.InRange(blue, 0, alpha); + } + } + } }