using Chronos.Core.Codecs.Raw; using Chronos.Core.Imaging; using Chronos.Core.Mathematics; using Chronos.Testing; namespace Chronos.Core.Tests.Raw; /// Demosaic accuracy against the full-colour scene a mosaic was sampled from. public sealed class DemosaicTests(TestContext context) { /// Fujifilm X-Trans layout (6×6). internal static readonly CfaPattern XTrans = new(6, 6, [ 1, 1, 0, 1, 1, 2, 1, 1, 2, 1, 1, 0, 2, 0, 1, 0, 2, 1, 1, 1, 2, 1, 1, 0, 1, 1, 0, 1, 1, 2, 0, 2, 1, 2, 0, 1, ]); /// Smooth colour scene modelled like a photograph: luminance texture at 10–30 px periods multiplied by a /// slowly varying chroma field (high frequencies correlate across channels, as in natural images). private static ImageBuffer Scene(int w, int h) { var image = new ImageBuffer(w, h); for (var y = 0; y < h; y++) { for (var x = 0; x < w; x++) { var l = 0.3 + 0.1 * double.Sin(x * 0.31 + y * 0.12) + 0.06 * double.Sin(y * 0.45 - x * 0.2) + 0.04 * double.Cos((x - y) * 0.6); image[0, x, y] = (float)(l * (1 + 0.35 * double.Sin(x * 0.02 + 0.5))); image[1, x, y] = (float)(l * (1 + 0.15 * double.Cos(y * 0.03))); image[2, x, y] = (float)(l * (1 + 0.35 * double.Cos(x * 0.015 - y * 0.02))); } } return image; } internal static ImageBuffer Mosaic(ImageBuffer scene, CfaPattern cfa) { var m = new ImageBuffer(scene.Width, scene.Height, 1); for (var y = 0; y < scene.Height; y++) { for (var x = 0; x < scene.Width; x++) m[0, x, y] = scene[cfa.ColorAt(x, y), x, y]; } return m; } [Theory] [Case("RGGB")] [Case("GBRG")] public void AhdAndBilinearMeetTheirAccuracyGates(string layout) { var cfa = CfaPattern.Bayer(layout); using var scene = Scene(256, 192); using var mosaic = Mosaic(scene, cfa); using var ahd = AhdDemosaic.Run(mosaic, cfa, Matrix3.Rec709ToXyz, -1, default); using var bilinear = Demosaic.Bilinear(mosaic, cfa, -1, default); var pAhd = SyntheticRaw.Psnr(scene, ahd, 2); var pBilinear = SyntheticRaw.Psnr(scene, bilinear, 2); context.Log($"{layout}: AHD {pAhd:F1} dB, bilinear {pBilinear:F1} dB"); Assert.GreaterOrEqual(pAhd, 38); Assert.GreaterOrEqual(pBilinear, 32); Assert.Greater(pAhd, pBilinear); } [Test] public void AhdDoesNotZipperAlongEdges() { var cfa = CfaPattern.Bayer("RGGB"); using var scene = SyntheticRaw.Edges(128, 128); using var mosaic = Mosaic(scene, cfa); using var ahd = AhdDemosaic.Run(mosaic, cfa, Matrix3.Rec709ToXyz, -1, default); using var bilinear = Demosaic.Bilinear(mosaic, cfa, -1, default); var (zipAhd, fringeAhd) = EdgeArtefacts(ahd); var (zipBilinear, fringeBilinear) = EdgeArtefacts(bilinear); context.Log($"zipper AHD {zipAhd:F5} bilinear {zipBilinear:F5}; fringe AHD {fringeAhd:F5} bilinear {fringeBilinear:F5}"); Assert.Less(zipAhd, 0.002); Assert.Less(fringeAhd, 0.002); Assert.Less(fringeAhd, fringeBilinear); } [Test] public void XTransNeighbourhoodInterpolationAndBinning() { using var scene = Scene(240, 180); using var mosaic = Mosaic(scene, XTrans); Assert.Equal(3, XTrans.BinSize); Assert.False(XTrans.IsBayer); using var full = Demosaic.Neighbourhood(mosaic, XTrans, -1, default); var psnr = SyntheticRaw.Psnr(scene, full, 2); using var third = Demosaic.Bin(mosaic, XTrans, 3, -1, default); using var expected = Resampler.Resize(scene, 80, 60, ResampleFilter.Box); var binned = SyntheticRaw.Psnr(expected, third, 1); context.Log($"X-Trans neighbourhood {psnr:F1} dB, binned {binned:F1} dB"); Assert.GreaterOrEqual(psnr, 34); Assert.GreaterOrEqual(binned, 34); } [Test] public void PatternReductionAndShift() { var bayer = new CfaPattern(4, 4, [0, 1, 0, 1, 1, 2, 1, 2, 0, 1, 0, 1, 1, 2, 1, 2]); Assert.Equal(2, bayer.Width); Assert.True(bayer.IsBayer); Assert.Equal("RGGB", bayer.ToString()); Assert.Equal("GRBG", bayer.Shift(1, 0).ToString()); Assert.Equal("BGGR", bayer.Shift(-1, -1).ToString()); Assert.Throws(() => new CfaPattern(2, 2, [0, 1, 1, 1])); Assert.Throws(() => new CfaPattern(2, 2, [0, 1, 3, 2])); } /// Zipper: second difference along each edge; fringe: chroma |R−G| + |B−G| next to the edges of a gray scene. private static (double Zipper, double Fringe) EdgeArtefacts(ImageBuffer image) { double zipper = 0, fringe = 0; var n = 0; int w = image.Width, h = image.Height; for (var d = -2; d <= 1; d++) { for (var k = 8; k < h - 8; k++) { if (k is > 56 and < 72) continue; // Vertical edge at x = w/2 (columns w/2-2 .. w/2+1), and horizontal edge at y = h/2. int vx = w / 2 + d, hy = h / 2 + d; for (var c = 0; c < 3; c++) { zipper += double.Abs(image[c, vx, k] - (image[c, vx, k - 1] + image[c, vx, k + 1]) / 2); zipper += double.Abs(image[c, k, hy] - (image[c, k - 1, hy] + image[c, k + 1, hy]) / 2); } fringe += double.Abs(image[0, vx, k] - image[1, vx, k]) + double.Abs(image[2, vx, k] - image[1, vx, k]); fringe += double.Abs(image[0, k, hy] - image[1, k, hy]) + double.Abs(image[2, k, hy] - image[1, k, hy]); n += 2; } } return (zipper / (3 * n), fringe / n); } }