diff --git a/SpectralConvolution/SpectralConvolution.Tests/ConvolutionScene.cs b/SpectralConvolution/SpectralConvolution.Tests/ConvolutionScene.cs new file mode 100644 index 0000000..4071e93 --- /dev/null +++ b/SpectralConvolution/SpectralConvolution.Tests/ConvolutionScene.cs @@ -0,0 +1,113 @@ +namespace SpectralConvolution.Tests; + +internal sealed class ConvolutionScene +{ + ConvolutionScene(byte[] source, int sourceX, int sourceY, int sourceWidth, int sourceHeight, KernelSpectrum spectrum, TilePlan plan) + { + Source = source; + SourceX = sourceX; + SourceY = sourceY; + SourceWidth = sourceWidth; + SourceHeight = sourceHeight; + Spectrum = spectrum; + Plan = plan; + } + + public byte[] Source { get; } + + public int SourceX { get; } + + public int SourceY { get; } + + public int SourceWidth { get; } + + public int SourceHeight { get; } + + public KernelSpectrum Spectrum { get; } + + public TilePlan Plan { get; } + + public int RegionLength => Plan.RegionWidth * Plan.RegionHeight * 4; + + public static double[] AsymmetricKernel(int radius) + { + var size = radius * 2 + 1; + var values = new double[size * size]; + for (var y = 0; y < size; y++) + { + for (var x = 0; x < size; x++) + { + var dx = x - radius * 0.6; + var dy = y - radius * 1.3; + values[y * size + x] = Math.Exp(-(dx * dx * 0.4 + dy * dy * 0.15) / (radius * radius + 1d)) + (x == 0 && y == size - 1 ? 0.5 : 0d); + } + } + + return values; + } + + public static byte[] RandomSource(int width, int height, int seed, double litFraction) + { + var random = new Random(seed); + var pixels = new byte[width * height * 4]; + for (var index = 0; index < width * height; index++) + { + if (random.NextDouble() >= litFraction) + continue; + var alpha = (byte)random.Next(1, 256); + pixels[index * 4] = (byte)random.Next(alpha + 1); + pixels[index * 4 + 1] = (byte)random.Next(alpha + 1); + pixels[index * 4 + 2] = (byte)random.Next(alpha + 1); + pixels[index * 4 + 3] = alpha; + } + + return pixels; + } + + public static byte[] Uniform(int width, int height, byte value) + { + var pixels = new byte[width * height * 4]; + Array.Fill(pixels, value); + return pixels; + } + + public static ConvolutionScene Create(byte[] source, int sourceWidth, int sourceHeight, int margin, int radius, int size) + { + var spectrum = new KernelSpectrum(); + spectrum.Update(AsymmetricKernel(radius), radius, size); + var plan = TilePlan.Create(size, radius, 0, 0, sourceWidth + margin * 2, sourceHeight + margin * 2); + return new ConvolutionScene(source, margin, margin, sourceWidth, sourceHeight, spectrum, plan); + } + + public void Reference(int x, int y, Span result) + => DirectCorrelation.Evaluate(Source, SourceWidth, SourceHeight, Spectrum.Kernel, Spectrum.Radius, x - SourceX, y - SourceY, result); + + public (double RedGreen, double BlueAlpha) Norms(int x, int y) + => TileInput.Norms(Source, SourceX, SourceY, SourceWidth, SourceHeight, Plan, Plan.TileAt(x, y)); + + public double WorstRatio(ReadOnlySpan convolved, double operationError) + { + var reference = new double[4]; + var worst = 0d; + var referenceError = DirectCorrelation.ErrorBound(Spectrum.Kernel.Length); + for (var y = 0; y < Plan.RegionHeight; y++) + { + for (var x = 0; x < Plan.RegionWidth; x++) + { + var canvasX = Plan.RegionX + x; + var canvasY = Plan.RegionY + y; + Reference(canvasX, canvasY, reference); + var (redGreen, blueAlpha) = Norms(canvasX, canvasY); + var index = (y * Plan.RegionWidth + x) * 4; + for (var channel = 0; channel < 4; channel++) + { + var norm = channel < 2 ? redGreen : blueAlpha; + var bound = ConvolutionBound.Absolute(Plan.Size, operationError, norm) + referenceError; + worst = Math.Max(worst, Math.Abs(convolved[index + channel] - reference[channel]) / bound); + } + } + } + + return worst; + } +} diff --git a/SpectralConvolution/SpectralConvolution.Tests/TileInputTests.cs b/SpectralConvolution/SpectralConvolution.Tests/TileInputTests.cs new file mode 100644 index 0000000..5286f82 --- /dev/null +++ b/SpectralConvolution/SpectralConvolution.Tests/TileInputTests.cs @@ -0,0 +1,40 @@ +namespace SpectralConvolution.Tests; + +public sealed class TileInputTests +{ + [Fact] + public void TheNormsCoverTheWholeTileIncludingItsApron() + { + var source = new byte[10 * 10 * 4]; + source[0] = 3; + source[1] = 4; + source[2] = 12; + source[3] = 5; + var plan = TilePlan.Create(64, 2, 2, 2, 100, 100); + + var (redGreen, blueAlpha) = TileInput.Norms(source, 0, 0, 10, 10, plan, 0); + + Assert.Equal(Math.Sqrt(12 * 12 + 4 * 4) / 255d, redGreen); + Assert.Equal(Math.Sqrt(3 * 3 + 5 * 5) / 255d, blueAlpha); + } + + [Fact] + public void PixelsOutsideTheTileAreIgnored() + { + var source = ConvolutionScene.Uniform(200, 10, 255); + var plan = TilePlan.Create(64, 2, 0, 0, 200, 10); + + var (redGreen, _) = TileInput.Norms(source, 0, 0, 200, 10, plan, 1); + + var columns = Math.Min(64, 200 - (plan.OriginX(1) - 2)); + Assert.Equal(Math.Sqrt(2d * columns * 10), redGreen, 12); + } + + [Fact] + public void AShortSourceIsRejected() + { + var plan = TilePlan.Create(64, 2, 0, 0, 10, 10); + + Assert.Throws(() => TileInput.Norms(new byte[10], 0, 0, 2, 2, plan, 0)); + } +} diff --git a/SpectralConvolution/SpectralConvolution/TileInput.cs b/SpectralConvolution/SpectralConvolution/TileInput.cs new file mode 100644 index 0000000..2289509 --- /dev/null +++ b/SpectralConvolution/SpectralConvolution/TileInput.cs @@ -0,0 +1,41 @@ +namespace SpectralConvolution; + +internal static class TileInput +{ + const int Channels = 4; + + public static (double RedGreen, double BlueAlpha) Norms( + ReadOnlySpan bgra, + int sourceX, + int sourceY, + int sourceWidth, + int sourceHeight, + in TilePlan plan, + int tile) + { + if (bgra.Length < (long)sourceWidth * sourceHeight * Channels) + throw new ArgumentException(null, nameof(bgra)); + + var left = plan.OriginX(tile) - plan.Radius; + var top = plan.OriginY(tile) - plan.Radius; + var redGreen = 0L; + var blueAlpha = 0L; + var lastY = Math.Min(top + plan.Size, sourceY + sourceHeight); + var lastX = Math.Min(left + plan.Size, sourceX + sourceWidth); + for (var y = Math.Max(top, sourceY); y < lastY; y++) + { + for (var x = Math.Max(left, sourceX); x < lastX; x++) + { + var offset = ((y - sourceY) * sourceWidth + x - sourceX) * Channels; + long blue = bgra[offset]; + long green = bgra[offset + 1]; + long red = bgra[offset + 2]; + long alpha = bgra[offset + 3]; + redGreen += red * red + green * green; + blueAlpha += blue * blue + alpha * alpha; + } + } + + return (Math.Sqrt(redGreen) / 255d, Math.Sqrt(blueAlpha) / 255d); + } +}