diff --git a/src/Titano.Core/Codecs/Dng/DngColorProfile.cs b/src/Titano.Core/Codecs/Dng/DngColorProfile.cs new file mode 100644 index 00000000..dfc7cdd0 --- /dev/null +++ b/src/Titano.Core/Codecs/Dng/DngColorProfile.cs @@ -0,0 +1,239 @@ +using Titano.Core.Mathematics; + +namespace Titano.Core.Codecs.Dng; + +/// Result of : white balance and the matrix to linear Rec.709. +/// Camera response to the scene white, largest entry 1 (white-balance multipliers are its reciprocals). +/// Scene white chromaticity found by the iterative solve. +/// Correlated colour temperature of (Robertson), in kelvin. +/// White-balanced camera RGB (raw ÷ neutral) → linear Rec.709 (D65), BaselineExposure included. +/// White-balanced camera RGB → CIE XYZ (D65 adapted, Y = 1 at white), for demosaic metrics. +public sealed record DngColorSolution(Vector3D CameraNeutral, Chromaticity White, double Temperature, Matrix3 BalancedToRec709, Matrix3 BalancedToXyz); + +/// +/// Camera colour description of a DNG (or of a native RAW with a tabulated matrix) and the colour math of DNG 1.6 +/// chapter 6 "Mapping Camera Color Space to CIE XYZ Space": ColorMatrix1/2 and CameraCalibration1/2 interpolated by +/// inverse correlated colour temperature between CalibrationIlluminant1/2, AnalogBalance, the iterative white solve for +/// AsShotNeutral (or the neutral implied by AsShotWhiteXY), ForwardMatrix1/2 when present, then Bradford adaptation from +/// the D50 profile connection space to D65 and the XYZ → Rec.709 matrix. +/// +public sealed class DngColorProfile +{ + /// Default scene white when the file records none: D55, as the DNG SDK assumes. + public static Chromaticity DefaultWhite { get; } = new(0.3324, 0.3474); + + public Matrix3? ColorMatrix1 { get; init; } + public Matrix3? ColorMatrix2 { get; init; } + public Matrix3? CameraCalibration1 { get; init; } + public Matrix3? CameraCalibration2 { get; init; } + public Matrix3? ForwardMatrix1 { get; init; } + public Matrix3? ForwardMatrix2 { get; init; } + + /// Exif LightSource codes of the two calibrations (0 = unknown). + public int CalibrationIlluminant1 { get; init; } + + public int CalibrationIlluminant2 { get; init; } + public Vector3D AnalogBalance { get; init; } = new(1, 1, 1); + public Vector3D? AsShotNeutral { get; init; } + public Chromaticity? AsShotWhite { get; init; } + + /// Exposure offset in EV applied to the rendered values (DNG BaselineExposure). + public double BaselineExposure { get; init; } + + /// Solves white balance and builds the output matrices. + /// No usable colour matrix (missing, singular or non-finite). + public DngColorSolution Solve(ICollection warnings) + { + if (ColorMatrix1 is not { } cm1 || !Usable(cm1)) throw new InvalidDataException("DNG: ColorMatrix1 missing or singular"); + Chromaticity white; + Vector3D neutral; + if (AsShotNeutral is { } asShot && asShot.X > 0 && asShot.Y > 0 && asShot.Z > 0) + { + neutral = Normalize(asShot); + white = NeutralToXy(neutral); + } + else + { + if (AsShotWhite is { } xy && xy.X > 0 && xy.Y > 0 && xy.X + xy.Y < 1) + { + white = xy; + } + else + { + white = DefaultWhite; + warnings.Add("DNG: no as-shot white balance; D55 assumed"); + } + neutral = Normalize(XyzToCamera(white) * white.ToXyz()); + } + // Clamp like the DNG SDK so a degenerate neutral cannot produce infinite multipliers. + neutral = new Vector3D(double.Clamp(neutral.X, 0.001, 1), double.Clamp(neutral.Y, 0.001, 1), double.Clamp(neutral.Z, 0.001, 1)); + + var cameraToPcs = CameraToPcs(white, neutral); + var d50 = Chromaticity.D50.ToXyz(); + var d65 = Chromaticity.D65.ToXyz(); + var balancedToD65 = Matrix3.BradfordAdaptation(d50, d65) * cameraToPcs * Matrix3.Diagonal(neutral.X, neutral.Y, neutral.Z); + var gain = double.Exp2(BaselineExposure); + var toRec709 = Scale(Matrix3.XyzToRec709 * balancedToD65, gain); + return new DngColorSolution(neutral, white, Temperature(white), toRec709, balancedToD65); + } + + /// XYZ → camera at the given scene white: AnalogBalance · CameraCalibration · ColorMatrix, both interpolated + /// by the white's inverse temperature (DNG §6 "Translating White Balance xy Coordinates to Camera Neutral"). + public Matrix3 XyzToCamera(Chromaticity white) + { + var g = Weight(white); + var cm = Blend(ColorMatrix1!.Value, ColorMatrix2, g); + var cc = Blend(CameraCalibration1 ?? Matrix3.Identity, CameraCalibration2 ?? (CameraCalibration1 is null ? null : Matrix3.Identity), g); + return Matrix3.Diagonal(AnalogBalance.X, AnalogBalance.Y, AnalogBalance.Z) * cc * cm; + } + + /// Iterative white solve of DNG §6 "Translating Camera Neutral Coordinates to White Balance xy Coordinates": + /// start at D50, map the neutral through the inverse of the matrix interpolated at the current estimate, repeat until it + /// moves less than 1e-7 (30 passes at most; a two-value oscillation is averaged). + public Chromaticity NeutralToXy(Vector3D neutral) + { + const int maxPasses = 30; + var last = Chromaticity.D50; + for (var pass = 0; pass < maxPasses; pass++) + { + if (!XyzToCamera(last).TryInvert(out var cameraToXyz)) return last; + var next = Chromaticity.FromXyz(cameraToXyz * neutral); + if (!double.IsFinite(next.X) || !double.IsFinite(next.Y) || next.Y <= 0) return last; + if (double.Abs(next.X - last.X) + double.Abs(next.Y - last.Y) < 1e-7) return next; + if (pass == maxPasses - 1) next = new Chromaticity((last.X + next.X) / 2, (last.Y + next.Y) / 2); + last = next; + } + return last; + } + + /// Camera → XYZ(D50) with the white-balanced neutral mapping to the D50 white (Y = 1): through the + /// interpolated ForwardMatrix when present, otherwise the inverse of ColorMatrix adapted from D50 to the scene white + /// (DNG §6 "Camera to XYZ (D50) Transform"). + private Matrix3 CameraToPcs(Chromaticity white, Vector3D neutral) + { + var d50 = Chromaticity.D50.ToXyz(); + if (ForwardMatrix1 is { } fm1 && Usable(fm1)) + { + var g = Weight(white); + var fm = NormalizeForward(Blend(fm1, ForwardMatrix2 is { } fm2 && Usable(fm2) ? fm2 : null, g), d50); + var cc = Blend(CameraCalibration1 ?? Matrix3.Identity, CameraCalibration2 ?? (CameraCalibration1 is null ? null : Matrix3.Identity), g); + var individualToReference = (Matrix3.Diagonal(AnalogBalance.X, AnalogBalance.Y, AnalogBalance.Z) * cc).Inverse(); + var reference = individualToReference * neutral; + return fm * Matrix3.Diagonal(1 / reference.X, 1 / reference.Y, 1 / reference.Z) * individualToReference; + } + var pcsToCamera = XyzToCamera(white) * Matrix3.BradfordAdaptation(d50, white.ToXyz()); + var atWhite = pcsToCamera * d50; + var scale = double.Max(atWhite.X, double.Max(atWhite.Y, atWhite.Z)); + if (!(scale > 0)) throw new InvalidDataException("DNG: colour matrix maps white outside the camera gamut"); + return Scale(pcsToCamera, 1 / scale).Inverse(); + } + + /// Weight of calibration 1 for a scene white: 1 at or below its temperature, 0 at or above calibration 2's, + /// linear in inverse temperature between (DNG §6 "Linear Interpolation Based on Correlated Color Temperature"). + private double Weight(Chromaticity white) + { + if (ColorMatrix2 is null) return 1; + var t1 = IlluminantTemperature(CalibrationIlluminant1); + var t2 = IlluminantTemperature(CalibrationIlluminant2); + if (t1 <= 0 || t2 <= 0 || t1 == t2) return 1; + var t = Temperature(white); + var swap = t1 > t2; + var (lo, hi) = swap ? (t2, t1) : (t1, t2); + double g; + if (t <= lo) g = 1; + else if (t >= hi) g = 0; + else g = (1 / t - 1 / hi) / (1 / lo - 1 / hi); + return swap ? 1 - g : g; + } + + /// Temperatures the DNG SDK assigns to the Exif LightSource codes (fluorescents use the band centre); 0 when unknown. + public static double IlluminantTemperature(int lightSource) => lightSource switch + { + 17 or 3 => 2850, + 24 => 3200, + 23 => 5000, + 20 or 1 or 9 or 4 or 18 => 5500, + 21 or 19 or 10 => 6500, + 22 or 11 => 7500, + 12 => (5700 + 7100) / 2.0, + 13 => (4600 + 5500) / 2.0, + 14 or 2 => (3800 + 4500) / 2.0, + 15 => (3250 + 3800) / 2.0, + 16 => (2600 + 3250) / 2.0, + _ => 0, + }; + + /// Correlated colour temperature by Robertson's method (isotemperature lines in CIE 1960 uv, + /// Wyszecki & Stiles table) as the DNG SDK computes it; results clamp to 1 667–100 000 K. + public static double Temperature(Chromaticity xy) + { + var d = 1.5 - xy.X + 6 * xy.Y; + var u = 2 * xy.X / d; + var v = 3 * xy.Y / d; + double lastDt = 0; + var table = Robertson; + for (var i = 1; i < table.Length; i++) + { + var (_, tu, tv, slope) = table[i]; + var len = double.Sqrt(1 + slope * slope); + double du = 1 / len, dv = slope / len; + var dt = -(u - tu) * dv + (v - tv) * du; + if (dt <= 0 || i == table.Length - 1) + { + if (dt > 0) dt = 0; + dt = -dt; + var f = i == 1 ? 0 : dt / (lastDt + dt); + return 1e6 / (table[i - 1].Mired * f + table[i].Mired * (1 - f)); + } + lastDt = dt; + } + return 1e6 / table[^1].Mired; + } + + /// Robertson (1968) isotemperature lines: mired, u, v, slope. + private static readonly (double Mired, double U, double V, double Slope)[] Robertson = + [ + (0, 0.18006, 0.26352, -0.24341), (10, 0.18066, 0.26589, -0.25479), (20, 0.18133, 0.26846, -0.26876), + (30, 0.18208, 0.27119, -0.28539), (40, 0.18293, 0.27407, -0.30470), (50, 0.18388, 0.27709, -0.32675), + (60, 0.18494, 0.28021, -0.35156), (70, 0.18611, 0.28342, -0.37915), (80, 0.18740, 0.28668, -0.40955), + (90, 0.18880, 0.28997, -0.44278), (100, 0.19032, 0.29326, -0.47888), (125, 0.19462, 0.30141, -0.58204), + (150, 0.19962, 0.30921, -0.70471), (175, 0.20525, 0.31647, -0.84901), (200, 0.21142, 0.32312, -1.0182), + (225, 0.21807, 0.32909, -1.2168), (250, 0.22511, 0.33439, -1.4512), (275, 0.23247, 0.33904, -1.7298), + (300, 0.24010, 0.34308, -2.0637), (325, 0.24702, 0.34655, -2.4681), (350, 0.25591, 0.34951, -2.9641), + (375, 0.26400, 0.35200, -3.5814), (400, 0.27218, 0.35407, -4.3633), (425, 0.28039, 0.35577, -5.3762), + (450, 0.28863, 0.35714, -6.7262), (475, 0.29685, 0.35823, -8.5955), (500, 0.30505, 0.35907, -11.324), + (525, 0.31320, 0.35968, -15.628), (550, 0.32129, 0.36011, -23.325), (575, 0.32931, 0.36038, -40.770), + (600, 0.33724, 0.36051, -116.45), + ]; + + /// Rows scaled so that the camera neutral (1, 1, 1) maps exactly to the D50 white (DNG SDK NormalizeForwardMatrix). + private static Matrix3 NormalizeForward(Matrix3 m, Vector3D d50) + { + var one = m * new Vector3D(1, 1, 1); + if (!(one.X > 0 && one.Y > 0 && one.Z > 0)) return m; + return Matrix3.Diagonal(d50.X / one.X, d50.Y / one.Y, d50.Z / one.Z) * m; + } + + private static Matrix3 Blend(Matrix3 a, Matrix3? b, double weightA) + { + if (b is not { } m || weightA >= 1) return a; + var wb = 1 - weightA; + return new Matrix3( + a.M11 * weightA + m.M11 * wb, a.M12 * weightA + m.M12 * wb, a.M13 * weightA + m.M13 * wb, + a.M21 * weightA + m.M21 * wb, a.M22 * weightA + m.M22 * wb, a.M23 * weightA + m.M23 * wb, + a.M31 * weightA + m.M31 * wb, a.M32 * weightA + m.M32 * wb, a.M33 * weightA + m.M33 * wb); + } + + private static Matrix3 Scale(Matrix3 m, double s) => new( + m.M11 * s, m.M12 * s, m.M13 * s, m.M21 * s, m.M22 * s, m.M23 * s, m.M31 * s, m.M32 * s, m.M33 * s); + + private static Vector3D Normalize(Vector3D v) + { + var max = double.Max(v.X, double.Max(v.Y, v.Z)); + if (!(max > 0) || !double.IsFinite(max)) throw new InvalidDataException("DNG: camera neutral is not positive"); + return new Vector3D(v.X / max, v.Y / max, v.Z / max); + } + + private static bool Usable(Matrix3 m) => + double.IsFinite(m.Determinant) && m.TryInvert(out _); +} diff --git a/src/Titano.Core/Codecs/Dng/DngOpcodes.cs b/src/Titano.Core/Codecs/Dng/DngOpcodes.cs new file mode 100644 index 00000000..ca0dba5f --- /dev/null +++ b/src/Titano.Core/Codecs/Dng/DngOpcodes.cs @@ -0,0 +1,712 @@ +using System.Buffers.Binary; +using Titano.Core.Codecs.Raw; +using Titano.Core.Imaging; + +namespace Titano.Core.Codecs.Dng; + +/// +/// Image an opcode list runs on (DNG 1.6 §7): list 1 sees the stored raw values divided by 65535, list 2 the linearised +/// mosaic (0 = black, 1 = white), list 3 the demosaiced planes. Coordinates of opcode parameters are full-resolution +/// stage coordinates; a binned (half-size) image covers stage pixels per buffer pixel. +/// +internal sealed class OpcodeImage(ImageBuffer pixels, int left, int top, int step, CfaPattern? cfa) +{ + public ImageBuffer Pixels { get; set; } = pixels; + + /// Stage coordinates of buffer pixel (0, 0)'s block. + public int Left { get; set; } = left; + + public int Top { get; set; } = top; + + /// Stage pixels per buffer pixel along each axis (1 at full resolution). + public int Step { get; } = step; + + /// CFA of a mosaic, anchored at buffer pixel (0, 0); null for planar images. + public CfaPattern? Cfa { get; set; } = cfa; + + /// Per-pixel clip flags that must stay aligned with (trimmed together). + public byte[]? ClipMask { get; set; } + + public int Width => Pixels.Width; + public int Height => Pixels.Height; + + /// Stage bounds covered by the buffer. + public RawRect Bounds => new(Left, Top, Left + Width * Step, Top + Height * Step); + + /// Stage coordinate of a buffer column's centre (integer at full resolution). + public double StageX(int x) => Left + x * Step + (Step - 1) / 2.0; + + public double StageY(int y) => Top + y * Step + (Step - 1) / 2.0; + + /// Buffer coordinate of a stage coordinate. + public double BufferX(double stageX) => (stageX - Left - (Step - 1) / 2.0) / Step; + + public double BufferY(double stageY) => (stageY - Top - (Step - 1) / 2.0) / Step; +} + +/// One parsed opcode. Flag bit 0 marks it optional, bit 1 "may be skipped for previews". +internal abstract class DngOpcode(uint id, uint flags) +{ + public uint Id { get; } = id; + public uint Flags { get; } = flags; + public bool Optional => (Flags & 1) != 0; + + /// True when the opcode works on value magnitudes in a way that does not commute with white-balance scaling. + public virtual bool NeedsUnbalancedValues => false; + + public abstract string Name { get; } + + public abstract void Apply(OpcodeImage image, ICollection warnings, CancellationToken cancellationToken); +} + +/// +/// DNG opcode lists (DNG 1.6 chapter 7, big-endian): WarpRectilinear (1), FixVignetteRadial (3), FixBadPixelsConstant (4), +/// FixBadPixelsList (5), TrimBounds (6), MapTable (7), MapPolynomial (8), GainMap (9), DeltaPerRow/Column (10/11) and +/// ScalePerRow/Column (12/13). Other opcodes (WarpFisheye, WarpRectilinear2, vendor extensions) are skipped with a warning +/// that says whether the file marked them optional. +/// +internal static class DngOpcodes +{ + private const int MaxOpcodes = 256; + private const long MaxGainMapPoints = 1 << 22; + + /// Parses an OpcodeList tag value; malformed opcodes are dropped with a warning. + public static List Parse(byte[]? data, int list, ICollection warnings) + { + var result = new List(); + if (data is null || data.Length < 4) return result; + var count = BinaryPrimitives.ReadUInt32BigEndian(data); + var pos = 4; + for (var i = 0; i < count && i < MaxOpcodes; i++) + { + if (pos + 16 > data.Length) + { + warnings.Add($"DNG: opcode list {list} truncated"); + break; + } + var id = BinaryPrimitives.ReadUInt32BigEndian(data.AsSpan(pos)); + var flags = BinaryPrimitives.ReadUInt32BigEndian(data.AsSpan(pos + 8)); + var size = BinaryPrimitives.ReadUInt32BigEndian(data.AsSpan(pos + 12)); + pos += 16; + if (size > data.Length - pos) + { + warnings.Add($"DNG: opcode list {list} truncated"); + break; + } + var reader = new ParamReader(data.AsSpan(pos, (int)size)); + pos += (int)size; + var op = Create(id, flags, ref reader); + if (op is null) + { + warnings.Add((flags & 1) != 0 + ? $"DNG: optional opcode {id} in list {list} not supported; skipped" + : $"DNG: required opcode {id} in list {list} not supported; skipped, rendering may differ"); + } + else if (!reader.Ok) + { + warnings.Add($"DNG: opcode {op.Name} in list {list} has malformed parameters; skipped"); + } + else + { + result.Add(op); + } + } + return result; + } + + /// Applies every opcode in order. + public static void Apply(IReadOnlyList opcodes, OpcodeImage image, ICollection warnings, CancellationToken cancellationToken) + { + foreach (var op in opcodes) + { + cancellationToken.ThrowIfCancellationRequested(); + op.Apply(image, warnings, cancellationToken); + } + } + + private static DngOpcode? Create(uint id, uint flags, ref ParamReader r) => id switch + { + 1 => WarpRectilinear.Read(flags, ref r), + 3 => new FixVignetteRadial(flags, r.F64(), r.F64(), r.F64(), r.F64(), r.F64(), r.F64(), r.F64()), + 4 => FixBadPixelsConstant.Read(flags, ref r), + 5 => FixBadPixelsList.Read(flags, ref r), + 6 => TrimBounds.Read(flags, ref r), + 7 => MapTable.Read(flags, ref r), + 8 => MapPolynomial.Read(flags, ref r), + 9 => GainMap.Read(flags, ref r), + 10 or 11 or 12 or 13 => PerLine.Read(id, flags, ref r), + _ => null, + }; + + /// Big-endian parameter reader that turns overruns into = false instead of throwing. + internal ref struct ParamReader(ReadOnlySpan data) + { + private readonly ReadOnlySpan _data = data; + private int _pos; + + public bool Ok { get; private set; } = true; + public int Remaining => _data.Length - _pos; + + public uint U32() => Take(4) ? BinaryPrimitives.ReadUInt32BigEndian(_data[(_pos - 4)..]) : 0; + + /// A 32-bit coordinate limited to the int range. + public uint Bounded() + { + var v = U32(); + if (v > int.MaxValue / 4) Ok = false; + return v; + } + + public ushort U16() => Take(2) ? BinaryPrimitives.ReadUInt16BigEndian(_data[(_pos - 2)..]) : (ushort)0; + + public float F32() => Take(4) ? BinaryPrimitives.ReadSingleBigEndian(_data[(_pos - 4)..]) : 0; + + public double F64() + { + var v = Take(8) ? BinaryPrimitives.ReadDoubleBigEndian(_data[(_pos - 8)..]) : 0; + if (!double.IsFinite(v)) Ok = false; + return v; + } + + public void Fail() => Ok = false; + + private bool Take(int n) + { + if (!Ok || _pos + n > _data.Length) + { + Ok = false; + return false; + } + _pos += n; + return true; + } + } + + /// Area of effect shared by the per-pixel opcodes: rectangle, plane range and row/column pitch. + internal readonly record struct AreaSpec(RawRect Area, int Plane, int Planes, int RowPitch, int ColPitch) + { + public static AreaSpec Read(ref ParamReader r) + { + var top = (int)r.Bounded(); + var left = (int)r.Bounded(); + var bottom = (int)r.Bounded(); + var right = (int)r.Bounded(); + var plane = (int)r.Bounded(); + var planes = (int)r.Bounded(); + var rowPitch = (int)r.Bounded(); + var colPitch = (int)r.Bounded(); + if (rowPitch < 1 || colPitch < 1 || planes < 1) r.Fail(); + return new AreaSpec(new RawRect(left, top, right, bottom), plane, planes, rowPitch, colPitch); + } + + /// Visits every affected (plane, buffer x, buffer y, stage x, stage y) on the pitch grid; rows run in + /// parallel, so may only touch its own pixel. + public void ForEach(OpcodeImage image, Action pixel, ICollection warnings, string name, CancellationToken cancellationToken) + { + if (image.Step != 1 && (RowPitch != 1 || ColPitch != 1)) + { + warnings.Add($"DNG: {name} with pitch cannot apply to a binned image; skipped"); + return; + } + var area = Area.Intersect(image.Bounds); + if (area.IsEmpty) return; + var lastPlane = int.Min(image.Pixels.Channels, Plane + Planes); + var firstRow = Area.Top + Ceil(area.Top - Area.Top, RowPitch); + var firstCol = Area.Left + Ceil(area.Left - Area.Left, ColPitch); + var rowStep = RowPitch * image.Step; + var colStep = ColPitch * image.Step; + var rows = (area.Bottom - firstRow + rowStep - 1) / rowStep; + if (rows <= 0 || firstCol >= area.Right) return; + var plane = Plane; + Parallel.For(0, rows, new ParallelOptions { CancellationToken = cancellationToken }, k => + { + var sy = firstRow + k * rowStep; + var y = (int)double.Round(image.BufferY(sy)); + if ((uint)y >= (uint)image.Height) return; + for (var p = plane; p < lastPlane; p++) + { + for (var sx = firstCol; sx < area.Right; sx += colStep) + { + var x = (int)double.Round(image.BufferX(sx)); + if ((uint)x < (uint)image.Width) pixel(p, x, y, sx, sy); + } + } + }); + } + + private static int Ceil(int offset, int pitch) => (offset + pitch - 1) / pitch * pitch; + } + + /// Distance from the normalised optical centre to the farthest corner of the stage bounds. + private static (double Cx, double Cy, double MaxDistance) Centre(RawRect bounds, double cx, double cy) + { + var x = bounds.Left + cx * bounds.Width; + var y = bounds.Top + cy * bounds.Height; + var m = 0.0; + foreach (var (px, py) in (ReadOnlySpan<(int, int)>)[(bounds.Left, bounds.Top), (bounds.Right, bounds.Top), (bounds.Left, bounds.Bottom), (bounds.Right, bounds.Bottom)]) + m = double.Max(m, double.Hypot(px - x, py - y)); + return (x, y, double.Max(m, 1e-9)); + } + + /// WarpRectilinear: radial (kr0–kr3) and tangential (kt0, kt1) distortion per plane, mapping each corrected + /// pixel to its source position; resampled with Catmull-Rom. + private sealed class WarpRectilinear(uint flags, double[][] coefficients, double cx, double cy) : DngOpcode(1, flags) + { + public override string Name => "WarpRectilinear"; + + public static WarpRectilinear? Read(uint flags, ref ParamReader r) + { + var n = r.U32(); + if (n is < 1 or > 4) + { + r.Fail(); + return new WarpRectilinear(flags, [], 0, 0); + } + var k = new double[n][]; + for (var i = 0; i < n; i++) k[i] = [r.F64(), r.F64(), r.F64(), r.F64(), r.F64(), r.F64()]; + return new WarpRectilinear(flags, k, r.F64(), r.F64()); + } + + public override void Apply(OpcodeImage image, ICollection warnings, CancellationToken cancellationToken) + { + if (image.Cfa is not null) + { + warnings.Add("DNG: WarpRectilinear on mosaic data is not supported; skipped"); + return; + } + var src = image.Pixels; + var dst = new ImageBuffer(src.Width, src.Height, src.Channels, clear: false); + var (cxs, cys, m) = Centre(image.Bounds, cx, cy); + Parallel.For(0, src.Height, new ParallelOptions { CancellationToken = cancellationToken }, y => + { + var sy = image.StageY(y); + for (var p = 0; p < src.Channels; p++) + { + var k = coefficients[int.Min(p, coefficients.Length - 1)]; + var plane = src.Plane(p); + var row = dst.Row(p, y); + for (var x = 0; x < src.Width; x++) + { + var dx = (image.StageX(x) - cxs) / m; + var dy = (sy - cys) / m; + var r2 = dx * dx + dy * dy; + var f = k[0] + r2 * (k[1] + r2 * (k[2] + r2 * k[3])); + var tx = k[4] * 2 * dx * dy + k[5] * (r2 + 2 * dx * dx); + var ty = k[5] * 2 * dx * dy + k[4] * (r2 + 2 * dy * dy); + var xs = image.BufferX(cxs + m * (f * dx + tx)); + var ys = image.BufferY(cys + m * (f * dy + ty)); + row[x] = Sampling.CatmullRom(plane, src.Width, src.Height, xs, ys); + } + } + }); + src.Dispose(); + image.Pixels = dst; + } + } + + /// FixVignetteRadial: gain 1 + k0·r² + k1·r⁴ + k2·r⁶ + k3·r⁸ + k4·r¹⁰ about the normalised centre. + private sealed class FixVignetteRadial(uint flags, double k0, double k1, double k2, double k3, double k4, double cx, double cy) : DngOpcode(3, flags) + { + public override string Name => "FixVignetteRadial"; + + public override void Apply(OpcodeImage image, ICollection warnings, CancellationToken cancellationToken) + { + var (cxs, cys, m) = Centre(image.Bounds, cx, cy); + var pixels = image.Pixels; + Parallel.For(0, pixels.Height, new ParallelOptions { CancellationToken = cancellationToken }, y => + { + var dy = (image.StageY(y) - cys) / m; + for (var x = 0; x < pixels.Width; x++) + { + var dx = (image.StageX(x) - cxs) / m; + var r2 = dx * dx + dy * dy; + var gain = (float)(1 + r2 * (k0 + r2 * (k1 + r2 * (k2 + r2 * (k3 + r2 * k4))))); + for (var p = 0; p < pixels.Channels; p++) pixels.Row(p, y)[x] *= gain; + } + }); + } + } + + /// FixBadPixelsConstant: pixels whose 16-bit value equals the constant are replaced from good neighbours. + /// The BayerPhase parameter is not needed: the frame's own CFA pattern identifies same-colour neighbours. + private sealed class FixBadPixelsConstant(uint flags, uint constant) : DngOpcode(4, flags) + { + public override string Name => "FixBadPixelsConstant"; + public override bool NeedsUnbalancedValues => true; + + public static FixBadPixelsConstant Read(uint flags, ref ParamReader r) + { + var constant = r.U32(); + r.U32(); + return new FixBadPixelsConstant(flags, constant); + } + + public override void Apply(OpcodeImage image, ICollection warnings, CancellationToken cancellationToken) + { + if (constant > 65535) return; + var pixels = image.Pixels; + var bad = new List<(int X, int Y)>(); + for (var y = 0; y < pixels.Height; y++) + { + for (var x = 0; x < pixels.Width; x++) + { + for (var p = 0; p < pixels.Channels; p++) + { + if (Sampling.Code16(pixels.Row(p, y)[x]) != constant) continue; + bad.Add((x, y)); + break; + } + } + } + BadPixelRepair.Repair(image, bad); + } + } + + /// FixBadPixelsList: listed points (row, column) and rectangles are replaced from good neighbours. + private sealed class FixBadPixelsList(uint flags, (int Row, int Col)[] points, RawRect[] rects) : DngOpcode(5, flags) + { + private const int MaxEntries = 1 << 22; + + public override string Name => "FixBadPixelsList"; + + public static FixBadPixelsList Read(uint flags, ref ParamReader r) + { + r.U32(); + var pointCount = r.U32(); + var rectCount = r.U32(); + if (pointCount > MaxEntries || rectCount > MaxEntries || (long)pointCount * 8 + (long)rectCount * 16 > r.Remaining) + { + r.Fail(); + return new FixBadPixelsList(flags, [], []); + } + var points = new (int, int)[pointCount]; + for (var i = 0; i < points.Length; i++) points[i] = ((int)r.Bounded(), (int)r.Bounded()); + var rects = new RawRect[rectCount]; + for (var i = 0; i < rects.Length; i++) + { + var top = (int)r.Bounded(); + var left = (int)r.Bounded(); + var bottom = (int)r.Bounded(); + var right = (int)r.Bounded(); + rects[i] = new RawRect(left, top, right, bottom); + } + return new FixBadPixelsList(flags, points, rects); + } + + public override void Apply(OpcodeImage image, ICollection warnings, CancellationToken cancellationToken) + { + if (image.Step != 1) + { + warnings.Add("DNG: FixBadPixelsList cannot apply to a binned image; skipped"); + return; + } + var bad = new List<(int X, int Y)>(); + var bounds = image.Bounds; + foreach (var (row, col) in points) + { + if (col >= bounds.Left && col < bounds.Right && row >= bounds.Top && row < bounds.Bottom) bad.Add((col - image.Left, row - image.Top)); + } + foreach (var rect in rects) + { + var r = rect.Intersect(bounds); + for (var y = r.Top; y < r.Bottom && bad.Count < MaxEntries; y++) + { + for (var x = r.Left; x < r.Right; x++) bad.Add((x - image.Left, y - image.Top)); + } + } + BadPixelRepair.Repair(image, bad); + } + } + + /// TrimBounds: crops the image to a stage rectangle. + private sealed class TrimBounds(uint flags, RawRect bounds) : DngOpcode(6, flags) + { + public override string Name => "TrimBounds"; + + public static TrimBounds Read(uint flags, ref ParamReader r) + { + var top = (int)r.Bounded(); + var left = (int)r.Bounded(); + var bottom = (int)r.Bounded(); + var right = (int)r.Bounded(); + return new TrimBounds(flags, new RawRect(left, top, right, bottom)); + } + + public override void Apply(OpcodeImage image, ICollection warnings, CancellationToken cancellationToken) + { + var b = bounds.Intersect(image.Bounds); + var x0 = (int)double.Ceiling(image.BufferX(b.Left) - 1e-9); + var y0 = (int)double.Ceiling(image.BufferY(b.Top) - 1e-9); + var x1 = int.Min(image.Width, (int)double.Floor(image.BufferX(b.Right - 1) + 1e-9) + 1); + var y1 = int.Min(image.Height, (int)double.Floor(image.BufferY(b.Bottom - 1) + 1e-9) + 1); + x0 = int.Max(0, x0); + y0 = int.Max(0, y0); + if (x1 - x0 <= 0 || y1 - y0 <= 0) + { + warnings.Add("DNG: TrimBounds outside the image; skipped"); + return; + } + if (x0 == 0 && y0 == 0 && x1 == image.Width && y1 == image.Height) return; + var src = image.Pixels; + var dst = new ImageBuffer(x1 - x0, y1 - y0, src.Channels, clear: false); + for (var p = 0; p < src.Channels; p++) + { + for (var y = y0; y < y1; y++) src.Row(p, y).Slice(x0, x1 - x0).CopyTo(dst.Row(p, y - y0)); + } + if (image.ClipMask is { } mask) + { + var trimmed = new byte[dst.PlaneLength]; + for (var y = y0; y < y1; y++) mask.AsSpan(y * src.Width + x0, x1 - x0).CopyTo(trimmed.AsSpan((y - y0) * dst.Width)); + image.ClipMask = trimmed; + } + if (image.Cfa is { } cfa) image.Cfa = cfa.Shift(x0, y0); + image.Left += x0 * image.Step; + image.Top += y0 * image.Step; + src.Dispose(); + image.Pixels = dst; + } + } + + /// MapTable: 16-bit lookup over the area (inputs beyond the table use its last entry). + private sealed class MapTable(uint flags, AreaSpec area, ushort[] table) : DngOpcode(7, flags) + { + public override string Name => "MapTable"; + public override bool NeedsUnbalancedValues => true; + + public static MapTable Read(uint flags, ref ParamReader r) + { + var area = AreaSpec.Read(ref r); + var size = r.U32(); + if (size is < 1 or > 65536 || size * 2 > r.Remaining) + { + r.Fail(); + return new MapTable(flags, area, [0]); + } + var table = new ushort[size]; + for (var i = 0; i < table.Length; i++) table[i] = r.U16(); + return new MapTable(flags, area, table); + } + + public override void Apply(OpcodeImage image, ICollection warnings, CancellationToken cancellationToken) + { + var pixels = image.Pixels; + var last = table.Length - 1; + area.ForEach(image, (p, x, y, _, _) => + { + var row = pixels.Row(p, y); + row[x] = table[int.Min(Sampling.Code16(row[x]), last)] / 65535f; + }, warnings, Name, cancellationToken); + } + } + + /// MapPolynomial: out = Σ cᵢ·inⁱ on normalised values. + private sealed class MapPolynomial(uint flags, AreaSpec area, double[] coefficients) : DngOpcode(8, flags) + { + public override string Name => "MapPolynomial"; + public override bool NeedsUnbalancedValues => true; + + public static MapPolynomial Read(uint flags, ref ParamReader r) + { + var area = AreaSpec.Read(ref r); + var degree = r.U32(); + if (degree > 8) + { + r.Fail(); + return new MapPolynomial(flags, area, [0]); + } + var c = new double[degree + 1]; + for (var i = 0; i < c.Length; i++) c[i] = r.F64(); + return new MapPolynomial(flags, area, c); + } + + public override void Apply(OpcodeImage image, ICollection warnings, CancellationToken cancellationToken) + { + var pixels = image.Pixels; + area.ForEach(image, (p, x, y, _, _) => + { + var row = pixels.Row(p, y); + double v = row[x], acc = 0; + for (var i = coefficients.Length - 1; i >= 0; i--) acc = acc * v + coefficients[i]; + row[x] = (float)acc; + }, warnings, Name, cancellationToken); + } + } + + /// GainMap: a grid of gains (normalised origin and spacing over the image bounds), bilinearly interpolated + /// and clamped at the grid edges, multiplied into the area. + private sealed class GainMap(uint flags, AreaSpec area, int pointsV, int pointsH, double spacingV, double spacingH, double originV, double originH, int mapPlanes, float[] gains) : DngOpcode(9, flags) + { + public override string Name => "GainMap"; + + public static GainMap Read(uint flags, ref ParamReader r) + { + var area = AreaSpec.Read(ref r); + var pv = r.U32(); + var ph = r.U32(); + var sv = r.F64(); + var sh = r.F64(); + var ov = r.F64(); + var oh = r.F64(); + var planes = r.U32(); + var count = (long)pv * ph * planes; + if (pv < 1 || ph < 1 || planes < 1 || count > MaxGainMapPoints || count * 4 > r.Remaining || !(sv > 0) || !(sh > 0)) + { + r.Fail(); + return new GainMap(flags, area, 1, 1, 1, 1, 0, 0, 1, [1]); + } + var gains = new float[count]; + for (var i = 0; i < gains.Length; i++) gains[i] = r.F32(); + return new GainMap(flags, area, (int)pv, (int)ph, sv, sh, ov, oh, (int)planes, gains); + } + + public override void Apply(OpcodeImage image, ICollection warnings, CancellationToken cancellationToken) + { + var pixels = image.Pixels; + var bounds = image.Bounds; + area.ForEach(image, (p, x, y, _, _) => + { + var plane = int.Min(p - area.Plane, mapPlanes - 1); + var (r0, r1, fr) = Index((image.StageY(y) - bounds.Top) / bounds.Height, originV, spacingV, pointsV); + var (c0, c1, fc) = Index((image.StageX(x) - bounds.Left) / bounds.Width, originH, spacingH, pointsH); + var g00 = gains[(r0 * pointsH + c0) * mapPlanes + plane]; + var g01 = gains[(r0 * pointsH + c1) * mapPlanes + plane]; + var g10 = gains[(r1 * pointsH + c0) * mapPlanes + plane]; + var g11 = gains[(r1 * pointsH + c1) * mapPlanes + plane]; + var top = g00 + (g01 - g00) * fc; + var bottom = g10 + (g11 - g10) * fc; + pixels.Row(p, y)[x] *= (float)(top + (bottom - top) * fr); + }, warnings, Name, cancellationToken); + } + + private static (int I0, int I1, double Fraction) Index(double position, double origin, double spacing, int points) + { + var f = (position - origin) / spacing; + if (!(f > 0)) return (0, 0, 0); + if (f >= points - 1) return (points - 1, points - 1, 0); + var i = (int)f; + return (i, i + 1, f - i); + } + } + + /// DeltaPerRow/Column (offsets, normalised units) and ScalePerRow/Column (gains), one value per pitch step. + private sealed class PerLine(uint id, uint flags, AreaSpec area, float[] values) : DngOpcode(id, flags) + { + public override string Name => Id switch { 10 => "DeltaPerRow", 11 => "DeltaPerColumn", 12 => "ScalePerRow", _ => "ScalePerColumn" }; + public override bool NeedsUnbalancedValues => Id is 10 or 11; + + public static PerLine Read(uint id, uint flags, ref ParamReader r) + { + var area = AreaSpec.Read(ref r); + var count = r.U32(); + if (count > 1 << 20 || count * 4 > r.Remaining) + { + r.Fail(); + return new PerLine(id, flags, area, []); + } + var values = new float[count]; + for (var i = 0; i < values.Length; i++) values[i] = r.F32(); + return new PerLine(id, flags, area, values); + } + + public override void Apply(OpcodeImage image, ICollection warnings, CancellationToken cancellationToken) + { + var pixels = image.Pixels; + var perRow = Id is 10 or 12; + var add = Id is 10 or 11; + area.ForEach(image, (p, x, y, sx, sy) => + { + var index = perRow ? (sy - area.Area.Top) / area.RowPitch : (sx - area.Area.Left) / area.ColPitch; + if ((uint)index >= (uint)values.Length) return; + ref var v = ref pixels.Row(p, y)[x]; + v = add ? v + values[index] : v * values[index]; + }, warnings, Name, cancellationToken); + } + } +} + +/// Bad-pixel replacement shared by the FixBadPixels opcodes: mean of the nearest good pixels of the same CFA colour +/// (5×5, widened to 9×9 when needed), or of the 8 neighbours per plane for planar images. +internal static class BadPixelRepair +{ + public static void Repair(OpcodeImage image, List<(int X, int Y)> bad) + { + if (bad.Count == 0) return; + var pixels = image.Pixels; + int w = pixels.Width, h = pixels.Height; + var isBad = new HashSet(bad.Count); + foreach (var (x, y) in bad) isBad.Add((long)y * w + x); + var replacements = new float[bad.Count * pixels.Channels]; + for (var i = 0; i < bad.Count; i++) + { + var (x, y) = bad[i]; + for (var p = 0; p < pixels.Channels; p++) + { + var plane = pixels.Plane(p); + var value = plane[y * w + x]; + for (var radius = image.Cfa is null ? 1 : 2; radius <= 4; radius += 2) + { + double sum = 0; + var n = 0; + for (var dy = -radius; dy <= radius; dy++) + { + for (var dx = -radius; dx <= radius; dx++) + { + int nx = x + dx, ny = y + dy; + if ((dx | dy) == 0 || (uint)nx >= (uint)w || (uint)ny >= (uint)h || isBad.Contains((long)ny * w + nx)) continue; + if (image.Cfa is { } cfa && cfa.ColorAt(nx, ny) != cfa.ColorAt(x, y)) continue; + sum += plane[ny * w + nx]; + n++; + } + } + if (n > 0) + { + value = (float)(sum / n); + break; + } + } + replacements[i * pixels.Channels + p] = value; + } + } + for (var i = 0; i < bad.Count; i++) + { + var (x, y) = bad[i]; + for (var p = 0; p < pixels.Channels; p++) pixels.Plane(p)[y * w + x] = replacements[i * pixels.Channels + p]; + // A repaired sample no longer carries the sensor's clipping (a hot pixel is not a highlight). + if (image.ClipMask is { } mask) mask[y * w + x] = 0; + } + } +} + +/// Sampling helpers for the opcode and pipeline stages. +internal static class Sampling +{ + /// Normalised value → 16-bit code (rounded, clamped), the integer domain of MapTable and FixBadPixelsConstant. + public static int Code16(float v) => v >= 1f ? 65535 : v > 0f ? (int)(v * 65535f + 0.5f) : 0; + + /// Catmull-Rom interpolation of a plane at a fractional position (edges clamped; far outside reads the edge). + public static float CatmullRom(ReadOnlySpan plane, int width, int height, double x, double y) + { + x = double.Clamp(x, -2, width + 1); + y = double.Clamp(y, -2, height + 1); + var ix = (int)double.Floor(x); + var iy = (int)double.Floor(y); + var (x0, x1, x2, x3) = Weights((float)(x - ix)); + var (y0, y1, y2, y3) = Weights((float)(y - iy)); + int c0 = int.Clamp(ix - 1, 0, width - 1), c1 = int.Clamp(ix, 0, width - 1), c2 = int.Clamp(ix + 1, 0, width - 1), c3 = int.Clamp(ix + 2, 0, width - 1); + var sum = 0f; + for (var j = 0; j < 4; j++) + { + var o = int.Clamp(iy - 1 + j, 0, height - 1) * width; + var r = x0 * plane[o + c0] + x1 * plane[o + c1] + x2 * plane[o + c2] + x3 * plane[o + c3]; + sum += r * (j switch { 0 => y0, 1 => y1, 2 => y2, _ => y3 }); + } + return sum; + } + + private static (float, float, float, float) Weights(float t) + { + var t2 = t * t; + var t3 = t2 * t; + return (0.5f * (-t3 + 2 * t2 - t), 0.5f * (3 * t3 - 5 * t2 + 2), 0.5f * (-3 * t3 + 4 * t2 + t), 0.5f * (t3 - t2)); + } +} diff --git a/src/Titano.Core/Codecs/Dng/DngReader.cs b/src/Titano.Core/Codecs/Dng/DngReader.cs new file mode 100644 index 00000000..786793d5 --- /dev/null +++ b/src/Titano.Core/Codecs/Dng/DngReader.cs @@ -0,0 +1,247 @@ +using Titano.Core.Codecs.Raw; +using Titano.Core.Codecs.Tiff; +using Titano.Core.Mathematics; +using Titano.Core.Metadata.Tiff; + +namespace Titano.Core.Codecs.Dng; + +/// +/// Reads the raw image description of a DNG (DNG 1.6 chapters 4–6) into a : the full-resolution +/// CFA (PhotometricInterpretation 32803) or LinearRaw (34892) IFD among IFD0 and its SubIFDs; compression 1, 7 (lossless +/// JPEG, including two-component interleaved tiles), 8 (Deflate with integer or floating-point predictors) and 34892 +/// (lossy JPEG); CFA pattern and plane colours; LinearizationTable; BlackLevel with BlackLevelRepeatDim and DeltaH/V; +/// WhiteLevel; ActiveArea; DefaultCropOrigin/Size and DefaultScale; the colour tags; OpcodeList1–3; Orientation. +/// Raster tags are taken from the raw IFD (falling back to IFD0), colour tags from IFD0 (falling back to the raw IFD). +/// +internal static class DngReader +{ + private const uint PhotometricCfa = 32803, PhotometricLinearRaw = 34892; + private const ushort CameraCalibrationSignature = 0xC6F3, ProfileCalibrationSignature = 0xC6F4; + private const long MaxPixels = 1L << 28; + private const int MaxSubIfds = 16; + + /// Not a DNG, no supported raw IFD, or inconsistent tags. + public static RawFrame Read(Stream stream) + { + var r = TiffReader.Open(stream) ?? throw new InvalidDataException("DNG: missing TIFF header"); + var chain = r.ReadChain(); + if (chain.Count == 0) throw new InvalidDataException("DNG: no image directory"); + var ifd0 = chain[0]; + if (!ifd0.Has(TiffTags.DngVersion)) throw new InvalidDataException("DNG: DNGVersion missing"); + var raw = FindRawIfd(r, chain) ?? throw new InvalidDataException("DNG: no full-resolution CFA or LinearRaw image"); + + var width = (int)long.Min(r.GetUInt(raw, TiffTags.ImageWidth) ?? 0, int.MaxValue); + var height = (int)long.Min(r.GetUInt(raw, TiffTags.ImageLength) ?? 0, int.MaxValue); + if (width <= 0 || height <= 0 || (long)width * height > MaxPixels) throw new InvalidDataException($"DNG: unsupported raster size {width}x{height}"); + var spp = (int)(r.GetUInt(raw, TiffTags.SamplesPerPixel) ?? 1); + var photometric = r.GetUInt(raw, TiffTags.PhotometricInterpretation); + var compression = r.GetUInt(raw, TiffTags.Compression) ?? 1; + if (compression is not (1 or 7 or 8 or 34892)) throw new InvalidDataException($"DNG: compression {compression} is not supported"); + var floating = (r.GetUInt(raw, TiffTags.SampleFormat) ?? 1) == 3; + var bits = (int)(r.GetUInt(raw, TiffTags.BitsPerSample) ?? 16); + + CfaPattern? cfa = null; + if (photometric == PhotometricCfa) + { + if (spp != 1) throw new InvalidDataException("DNG: CFA data must have one sample per pixel"); + cfa = ReadCfa(r, raw); + } + else if (spp is not (1 or 3)) + { + throw new InvalidDataException($"DNG: LinearRaw with {spp} samples per pixel is not supported"); + } + + var activeArea = new RawRect(0, 0, width, height); + if (Values(r, raw, ifd0, TiffTags.ActiveArea, 4) is [var top, var left, var bottom, var right, ..]) + { + activeArea = new RawRect((int)left, (int)top, (int)right, (int)bottom).Intersect(activeArea); + if (activeArea.IsEmpty) throw new InvalidDataException("DNG: empty ActiveArea"); + } + + var warnings = new List(); + var frame = new RawFrame + { + Decoder = "managed-dng", + Width = width, + Height = height, + SamplesPerPixel = spp, + Cfa = cfa, + ActiveArea = activeArea, + DefaultCrop = ReadCrop(r, raw, ifd0, activeArea), + DefaultScale = Values(r, raw, ifd0, TiffTags.DefaultScale, 2) is [var sh, var sv, ..] && sh > 0 && sv > 0 && sh < 16 && sv < 16 ? (sh, sv) : (1, 1), + Linearization = ReadLinearization(r, raw, ifd0), + Black = ReadBlack(r, raw, ifd0, spp, activeArea, warnings), + White = ReadWhite(r, raw, ifd0, spp, floating ? 1.0 : bits is >= 1 and <= 16 ? (1 << bits) - 1 : 65535), + Color = ReadColor(r, ifd0, raw, spp == 1 && cfa is null, warnings), + OpcodeList1 = Bytes(r, raw, ifd0, TiffTags.OpcodeList1), + OpcodeList2 = Bytes(r, raw, ifd0, TiffTags.OpcodeList2), + OpcodeList3 = Bytes(r, raw, ifd0, TiffTags.OpcodeList3), + Orientation = (int)(r.GetUInt(ifd0, TiffTags.Orientation) ?? 1) is var o and >= 1 and <= 8 ? o : 1, + Make = r.GetString(ifd0, TiffTags.Make), + Model = r.GetString(ifd0, TiffTags.Model) ?? r.GetString(ifd0, TiffTags.UniqueCameraModel), + Loader = new TiffRasterLoader(r, raw), + }; + frame.Warnings.AddRange(warnings); + frame.Warnings.AddRange(r.Warnings); + return frame; + } + + /// First IFD with NewSubfileType 0 and a CFA/LinearRaw photometric: IFD0, its SubIFDs (two levels), the chain. + private static Ifd? FindRawIfd(TiffReader r, IReadOnlyList chain) + { + var visited = new HashSet(); + foreach (var ifd in chain) visited.Add(ifd.Position); + var queue = new List<(Ifd Ifd, int Depth)> { (chain[0], 0) }; + for (var i = 0; i < queue.Count; i++) + { + var (ifd, depth) = queue[i]; + if (IsRaw(r, ifd)) return ifd; + if (depth < 2 && ifd.Entries.TryGetValue(TiffTags.SubIfds, out var sub) && sub.Count <= MaxSubIfds) + { + foreach (var offset in r.GetUInts(sub) ?? []) + { + if (r.ReadIfd(offset, visited) is { } child) queue.Add((child, depth + 1)); + } + } + } + foreach (var ifd in chain) + { + if (IsRaw(r, ifd)) return ifd; + } + return null; + } + + private static bool IsRaw(TiffReader r, Ifd ifd) => + (r.GetUInt(ifd, TiffTags.NewSubfileType) ?? 0) == 0 + && r.GetUInt(ifd, TiffTags.PhotometricInterpretation) is PhotometricCfa or PhotometricLinearRaw; + + private static CfaPattern ReadCfa(TiffReader r, Ifd raw) + { + if ((r.GetUInt(raw, TiffTags.CfaLayout) ?? 1) != 1) throw new InvalidDataException("DNG: non-rectangular CFA layouts are not supported"); + var dim = IfdValue.UInts(r, raw, TiffTags.CfaRepeatPatternDim) ?? [2, 2]; + if (dim.Length < 2) throw new InvalidDataException("DNG: bad CFARepeatPatternDim"); + int rows = (int)dim[0], cols = (int)dim[1]; + if (rows is < 1 or > CfaPattern.MaxSize || cols is < 1 or > CfaPattern.MaxSize) throw new InvalidDataException($"DNG: CFA pattern {rows}x{cols} is not supported"); + var pattern = IfdValue.UInts(r, raw, TiffTags.CfaPattern) ?? throw new InvalidDataException("DNG: CFAPattern missing"); + if (pattern.Length < rows * cols) throw new InvalidDataException("DNG: CFAPattern too short"); + var planeColor = IfdValue.UInts(r, raw, TiffTags.CfaPlaneColor) ?? [0, 1, 2]; + var colors = new byte[rows * cols]; + for (var i = 0; i < colors.Length; i++) + { + // CFAPattern holds colour codes (0 red, 1 green, 2 blue, 3+ CMY/white); CFAPlaneColor names the planes. + var code = pattern[i]; + if (code > 2 || planeColor.Length != 3 || Array.IndexOf(planeColor, code) < 0) + throw new InvalidDataException("DNG: only red/green/blue CFAs are supported"); + colors[i] = (byte)code; + } + return new CfaPattern(cols, rows, colors); + } + + private static RawRect? ReadCrop(TiffReader r, Ifd raw, Ifd ifd0, RawRect active) + { + if (Values(r, raw, ifd0, TiffTags.DefaultCropOrigin, 2) is not [var ox, var oy, ..] || Values(r, raw, ifd0, TiffTags.DefaultCropSize, 2) is not [var cw, var ch, ..]) + return null; + var crop = new RawRect((int)double.Round(ox), (int)double.Round(oy), (int)double.Round(ox + cw), (int)double.Round(oy + ch)) + .Intersect(new RawRect(0, 0, active.Width, active.Height)); + return crop.IsEmpty ? null : crop; + } + + private static ushort[]? ReadLinearization(TiffReader r, Ifd raw, Ifd ifd0) + { + var ifd = raw.Has(TiffTags.LinearizationTable) ? raw : ifd0; + if (!ifd.Entries.TryGetValue(TiffTags.LinearizationTable, out var e) || e.Count is 0 or > 65536) return null; + return r.GetUInts(e) is { } values ? Array.ConvertAll(values, v => (ushort)uint.Min(v, 65535)) : null; + } + + private static RawBlackLevel ReadBlack(TiffReader r, Ifd raw, Ifd ifd0, int spp, RawRect active, ICollection warnings) + { + var dim = Values(r, raw, ifd0, TiffTags.BlackLevelRepeatDim, 2) is [var dr, var dc, ..] ? ((int)dr, (int)dc) : (1, 1); + var (rows, cols) = dim; + if (rows is < 1 or > 64 || cols is < 1 or > 64) + { + warnings.Add("DNG: bad BlackLevelRepeatDim ignored"); + (rows, cols) = (1, 1); + } + var values = new float[rows * cols * spp]; + if (Doubles(r, raw, ifd0, TiffTags.BlackLevel, 1 << 14) is { Length: > 0 } black) + { + if (black.Length != values.Length && black.Length != 1) warnings.Add($"DNG: BlackLevel has {black.Length} values, {values.Length} expected"); + for (var i = 0; i < values.Length; i++) values[i] = (float)black[black.Length == values.Length ? i : i % black.Length]; + } + var deltaH = Doubles(r, raw, ifd0, TiffTags.BlackLevelDeltaH, 1 << 20) is { } h && h.Length >= active.Width ? Array.ConvertAll(h, v => (float)v) : null; + var deltaV = Doubles(r, raw, ifd0, TiffTags.BlackLevelDeltaV, 1 << 20) is { } v && v.Length >= active.Height ? Array.ConvertAll(v, x => (float)x) : null; + return new RawBlackLevel(rows, cols, values, deltaH, deltaV); + } + + private static double[] ReadWhite(TiffReader r, Ifd raw, Ifd ifd0, int spp, double fallback) + { + var white = new double[spp]; + var tag = Doubles(r, raw, ifd0, TiffTags.WhiteLevel, 16); + for (var i = 0; i < spp; i++) white[i] = tag is { Length: > 0 } t ? t[int.Min(i, t.Length - 1)] : fallback; + return white; + } + + private static DngColorProfile ReadColor(TiffReader r, Ifd ifd0, Ifd raw, bool monochrome, ICollection warnings) + { + Matrix3? M(ushort tag) => Doubles(r, ifd0, raw, tag, 64) is { Length: 9 } m ? new Matrix3(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]) : null; + Mathematics.Vector3D? V(ushort tag) => Doubles(r, ifd0, raw, tag, 16) is { Length: 3 } v ? new Mathematics.Vector3D(v[0], v[1], v[2]) : null; + var baseline = Doubles(r, ifd0, raw, TiffTags.BaselineExposure, 1) is [var b] && double.Abs(b) < 16 ? b : 0; + if (monochrome) return new DngColorProfile { BaselineExposure = baseline }; + + // CameraCalibration only applies when its signature matches the profile's (DNG §6, "CameraCalibrationSignature"). + var useCalibration = r.GetString(ifd0, CameraCalibrationSignature) == r.GetString(ifd0, ProfileCalibrationSignature); + if (!useCalibration && (ifd0.Has(TiffTags.CameraCalibration1) || ifd0.Has(TiffTags.CameraCalibration2))) + warnings.Add("DNG: CameraCalibration ignored (signature does not match the profile)"); + var cm1 = M(TiffTags.ColorMatrix1) ?? throw new InvalidDataException("DNG: ColorMatrix1 missing"); + return new DngColorProfile + { + ColorMatrix1 = cm1, + ColorMatrix2 = M(TiffTags.ColorMatrix2), + CameraCalibration1 = useCalibration ? M(TiffTags.CameraCalibration1) : null, + CameraCalibration2 = useCalibration ? M(TiffTags.CameraCalibration2) : null, + ForwardMatrix1 = M(TiffTags.ForwardMatrix1), + ForwardMatrix2 = M(TiffTags.ForwardMatrix2), + CalibrationIlluminant1 = (int)(r.GetUInt(ifd0, TiffTags.CalibrationIlluminant1) ?? 0), + CalibrationIlluminant2 = (int)(r.GetUInt(ifd0, TiffTags.CalibrationIlluminant2) ?? 0), + AnalogBalance = V(TiffTags.AnalogBalance) is { } ab && ab.X > 0 && ab.Y > 0 && ab.Z > 0 ? ab : new(1, 1, 1), + AsShotNeutral = V(TiffTags.AsShotNeutral), + AsShotWhite = Doubles(r, ifd0, raw, TiffTags.AsShotWhiteXy, 2) is [var x, var y] ? new Chromaticity(x, y) : null, + BaselineExposure = baseline, + }; + } + + private static byte[]? Bytes(TiffReader r, Ifd raw, Ifd ifd0, ushort tag) + { + var ifd = raw.Has(tag) ? raw : ifd0; + return ifd.Entries.TryGetValue(tag, out var e) ? r.ReadBytes(e) : null; + } + + /// Finite numeric values of a tag from the first IFD that has it, or null (also when there are more than ). + private static double[]? Doubles(TiffReader r, Ifd first, Ifd second, ushort tag, int max) + { + var ifd = first.Has(tag) ? first : second; + if (!ifd.Entries.TryGetValue(tag, out var e) || e.Count == 0 || e.Count > max) return null; + var values = r.GetDoubles(e); + if (values is null) return null; + foreach (var v in values) + { + if (!double.IsFinite(v)) return null; + } + return values; + } + + private static double[]? Values(TiffReader r, Ifd first, Ifd second, ushort tag, int count) => + Doubles(r, first, second, tag, 16) is { } v && v.Length >= count && v.All(x => x >= 0 && x < int.MaxValue / 2) ? v : null; +} + +/// Loads a TIFF/DNG raster IFD through . +internal sealed class TiffRasterLoader(TiffReader reader, Ifd ifd) : RawLoader +{ + public override RawSamples Load(Stream stream, int maxDegreeOfParallelism, CancellationToken cancellationToken) + { + var raster = TiffImageReader.ReadRaster(reader, ifd, new TiffDecodeOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }); + var samples = new RawSamples(raster.Width, raster.Height, raster.SamplesPerPixel, raster.Samples, raster.FloatSamples); + samples.Warnings.AddRange(raster.Warnings); + return samples; + } +} diff --git a/src/Titano.Core/Codecs/Raw/AhdDemosaic.cs b/src/Titano.Core/Codecs/Raw/AhdDemosaic.cs new file mode 100644 index 00000000..79008691 --- /dev/null +++ b/src/Titano.Core/Codecs/Raw/AhdDemosaic.cs @@ -0,0 +1,245 @@ +using System.Buffers; +using Titano.Core.Imaging; +using Titano.Core.Mathematics; + +namespace Titano.Core.Codecs.Raw; + +/// +/// Adaptive Homogeneity-Directed demosaicing (K. Hirakawa, T. W. Parks, "Adaptive homogeneity-directed demosaicing +/// algorithm", IEEE Trans. Image Processing 14(3), 2005), in the formulation of dcraw's ahd_interpolate: +/// green is interpolated horizontally and vertically (gradient-corrected, limited to its two neighbours), red and blue +/// follow by colour-difference interpolation in each direction, both candidates are converted to CIELab, and each pixel +/// takes the direction whose 3×3 neighbourhood is most homogeneous (Lab distances within adaptive luminance and chroma +/// thresholds), or the average on a tie. Works on float data in tiles processed in parallel; a 5-pixel border is +/// bilinear. +/// +internal static unsafe class AhdDemosaic +{ + private const int Ts = 256; + private const int LutSize = 1 << 16; + private const float LutScale = LutSize / 2f; + + /// CIELab f(t) for t in [0, 2): cube root above (6/29)³, the linear toe below. + private static readonly float[] LabF = BuildLabF(); + + /// White-balanced camera RGB → XYZ (white at Y = 1), used only for the Lab metric. + public static ImageBuffer Run(ImageBuffer mosaic, CfaPattern cfa, Matrix3 balancedToXyz, int maxDegreeOfParallelism, CancellationToken cancellationToken) + { + if (!cfa.IsBayer) throw new ArgumentException("Bayer pattern expected", nameof(cfa)); + int w = mosaic.Width, h = mosaic.Height; + if (w < 16 || h < 16) return Demosaic.Bilinear(mosaic, cfa, maxDegreeOfParallelism, cancellationToken); + var output = new ImageBuffer(w, h, 3, clear: false); + Demosaic.BilinearBorder(mosaic, cfa, output, 5); + + // Rows of the matrix are divided by the white's XYZ so that Lab sees X/Xn, Y/Yn, Z/Zn directly. + var white = balancedToXyz * new Vector3D(1, 1, 1); + var m = new float[9]; + double[] rows = [balancedToXyz.M11, balancedToXyz.M12, balancedToXyz.M13, balancedToXyz.M21, balancedToXyz.M22, balancedToXyz.M23, balancedToXyz.M31, balancedToXyz.M32, balancedToXyz.M33]; + double[] whites = [white.X, white.Y, white.Z]; + for (var i = 0; i < 9; i++) m[i] = (float)(rows[i] / (whites[i / 3] > 0 ? whites[i / 3] : 1)); + + var tops = new List(); + for (var top = 2; top < h - 5; top += Ts - 6) tops.Add(top); + var lefts = new List(); + for (var left = 2; left < w - 5; left += Ts - 6) lefts.Add(left); + var options = new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism, CancellationToken = cancellationToken }; + var colors = new int[4]; + for (var i = 0; i < 4; i++) colors[i] = cfa.ColorAt(i & 1, i >> 1); + Parallel.For(0, tops.Count * lefts.Count, options, () => new Workspace(), (t, _, ws) => + { + Tile(mosaic, output, colors, m, tops[t / lefts.Count], lefts[t % lefts.Count], ws); + return ws; + }, ws => ws.Dispose()); + return output; + } + + /// Per-thread tile buffers: two directional RGB and Lab images plus homogeneity counts. + private sealed class Workspace : IDisposable + { + public readonly float[] Rgb = ArrayPool.Shared.Rent(6 * Ts * Ts); + public readonly float[] Lab = ArrayPool.Shared.Rent(6 * Ts * Ts); + public readonly byte[] Homo = ArrayPool.Shared.Rent(2 * Ts * Ts); + + public void Dispose() + { + ArrayPool.Shared.Return(Rgb); + ArrayPool.Shared.Return(Lab); + ArrayPool.Shared.Return(Homo); + } + } + + private static void Tile(ImageBuffer mosaic, ImageBuffer output, int[] colors, float[] xyz, int top, int left, Workspace ws) + { + int w = mosaic.Width, h = mosaic.Height; + const int plane = Ts * Ts; + fixed (float* image = mosaic.Storage, rgbBase = ws.Rgb, labBase = ws.Lab, mat = xyz, lut = LabF) + fixed (byte* homoBase = ws.Homo) + fixed (float* outBase = output.Storage) + { + // Direction d: R, G, B planes at rgbBase + (3d + c)·plane; Lab likewise. + var rowEnd = int.Min(top + Ts, h - 2); + var colEnd = int.Min(left + Ts, w - 2); + + // 1. Green horizontally and vertically at red/blue sites. + for (var row = top; row < rowEnd; row++) + { + var col = left + (Color(colors, left, row) == 1 ? 1 : 0); + var gh = rgbBase + plane + (row - top) * Ts - left; + var gv = rgbBase + 4 * plane + (row - top) * Ts - left; + for (; col < colEnd; col += 2) + { + var pix = image + row * w + col; + var val = ((pix[-1] + pix[0] + pix[1]) * 2 - pix[-2] - pix[2]) * 0.25f; + gh[col] = Limit(val, pix[-1], pix[1]); + val = ((pix[-w] + pix[0] + pix[w]) * 2 - pix[-2 * w] - pix[2 * w]) * 0.25f; + gv[col] = Limit(val, pix[-w], pix[w]); + } + } + + // 2. Red and blue in each direction, then CIELab. + rowEnd = int.Min(top + Ts - 1, h - 3); + colEnd = int.Min(left + Ts - 1, w - 3); + for (var d = 0; d < 2; d++) + { + var rgb = rgbBase + 3 * d * plane; + var lab = labBase + 3 * d * plane; + for (var row = top + 1; row < rowEnd; row++) + { + for (var col = left + 1; col < colEnd; col++) + { + var pix = image + row * w + col; + var i = (row - top) * Ts + col - left; + var r = rgb + i; + var own = Color(colors, col, row); + float red, green, blue; + if (own == 1) + { + // Colour of the vertical neighbours; the horizontal ones carry the other one. + var vertical = Color(colors, col, row + 1); + var h0 = pix[0] + (pix[-1] + pix[1] - r[plane - 1] - r[plane + 1]) * 0.5f; + var v0 = pix[0] + (pix[-w] + pix[w] - r[plane - Ts] - r[plane + Ts]) * 0.5f; + green = pix[0]; + if (vertical == 0) (red, blue) = (v0, h0); + else (red, blue) = (h0, v0); + } + else + { + green = r[plane]; + var opposite = green + (pix[-w - 1] + pix[-w + 1] + pix[w - 1] + pix[w + 1] + - r[plane - Ts - 1] - r[plane - Ts + 1] - r[plane + Ts - 1] - r[plane + Ts + 1]) * 0.25f; + (red, blue) = own == 0 ? (pix[0], opposite) : (opposite, pix[0]); + } + red = float.Max(red, 0f); + blue = float.Max(blue, 0f); + r[0] = red; + r[plane] = green; + r[2 * plane] = blue; + var fx = F(lut, mat[0] * red + mat[1] * green + mat[2] * blue); + var fy = F(lut, mat[3] * red + mat[4] * green + mat[5] * blue); + var fz = F(lut, mat[6] * red + mat[7] * green + mat[8] * blue); + var l = lab + i; + l[0] = 116f * fy - 16f; + l[plane] = 500f * (fx - fy); + l[2 * plane] = 200f * (fy - fz); + } + } + } + + // 3. Homogeneity: neighbours within adaptive luminance and chroma thresholds. + new Span(homoBase, 2 * plane).Clear(); + rowEnd = int.Min(top + Ts - 2, h - 4); + colEnd = int.Min(left + Ts - 2, w - 4); + var labH = labBase; + var labV = labBase + 3 * plane; + for (var row = top + 2; row < rowEnd; row++) + { + for (var col = left + 2; col < colEnd; col++) + { + var i = (row - top) * Ts + col - left; + var lh = labH + i; + var lv = labV + i; + float lh0 = Diff(lh, -1), lh1 = Diff(lh, 1), lh2 = Diff(lh, -Ts), lh3 = Diff(lh, Ts); + float lv0 = Diff(lv, -1), lv1 = Diff(lv, 1), lv2 = Diff(lv, -Ts), lv3 = Diff(lv, Ts); + float ah0 = Chroma(lh, -1, plane), ah1 = Chroma(lh, 1, plane), ah2 = Chroma(lh, -Ts, plane), ah3 = Chroma(lh, Ts, plane); + float av0 = Chroma(lv, -1, plane), av1 = Chroma(lv, 1, plane), av2 = Chroma(lv, -Ts, plane), av3 = Chroma(lv, Ts, plane); + var leps = float.Min(float.Max(lh0, lh1), float.Max(lv2, lv3)); + var abeps = float.Min(float.Max(ah0, ah1), float.Max(av2, av3)); + homoBase[i] = (byte)(Count(lh0, ah0, leps, abeps) + Count(lh1, ah1, leps, abeps) + Count(lh2, ah2, leps, abeps) + Count(lh3, ah3, leps, abeps)); + homoBase[plane + i] = (byte)(Count(lv0, av0, leps, abeps) + Count(lv1, av1, leps, abeps) + Count(lv2, av2, leps, abeps) + Count(lv3, av3, leps, abeps)); + } + } + + // 4. Most homogeneous direction over 3×3 (average on a tie). + rowEnd = int.Min(top + Ts - 3, h - 5); + colEnd = int.Min(left + Ts - 3, w - 5); + var outPlane = w * h; + for (var row = top + 3; row < rowEnd; row++) + { + for (var col = left + 3; col < colEnd; col++) + { + var i = (row - top) * Ts + col - left; + int hm0 = 0, hm1 = 0; + for (var k = -Ts; k <= Ts; k += Ts) + { + var p = homoBase + i + k; + hm0 += p[-1] + p[0] + p[1]; + hm1 += p[plane - 1] + p[plane] + p[plane + 1]; + } + var o = outBase + row * w + col; + var a = rgbBase + i; + var b = rgbBase + 3 * plane + i; + if (hm0 > hm1) + { + o[0] = a[0]; + o[outPlane] = a[plane]; + o[2 * outPlane] = a[2 * plane]; + } + else if (hm1 > hm0) + { + o[0] = b[0]; + o[outPlane] = b[plane]; + o[2 * outPlane] = b[2 * plane]; + } + else + { + o[0] = (a[0] + b[0]) * 0.5f; + o[outPlane] = (a[plane] + b[plane]) * 0.5f; + o[2 * outPlane] = (a[2 * plane] + b[2 * plane]) * 0.5f; + } + } + } + } + } + + private static int Color(int[] colors, int x, int y) => colors[(y & 1) << 1 | x & 1]; + + private static float Limit(float v, float a, float b) => a < b ? float.Clamp(v, a, b) : float.Clamp(v, b, a); + + private static float F(float* lut, float t) + { + var i = (int)(t * LutScale); + return lut[i < 0 ? 0 : i >= LutSize ? LutSize - 1 : i]; + } + + private static float Diff(float* l, int offset) => float.Abs(l[0] - l[offset]); + + private static float Chroma(float* l, int offset, int plane) + { + var da = l[plane] - l[plane + offset]; + var db = l[2 * plane] - l[2 * plane + offset]; + return da * da + db * db; + } + + private static int Count(float l, float ab, float leps, float abeps) => l <= leps && ab <= abeps ? 1 : 0; + + private static float[] BuildLabF() + { + var lut = new float[LutSize]; + for (var i = 0; i < LutSize; i++) + { + var t = i / (double)LutScale; + lut[i] = (float)(t > 216.0 / 24389 ? double.Cbrt(t) : (24389.0 / 27 * t + 16) / 116); + } + return lut; + } +} diff --git a/src/Titano.Core/Codecs/Raw/CfaPattern.cs b/src/Titano.Core/Codecs/Raw/CfaPattern.cs new file mode 100644 index 00000000..5ec30b97 --- /dev/null +++ b/src/Titano.Core/Codecs/Raw/CfaPattern.cs @@ -0,0 +1,143 @@ +using System.Text; + +namespace Titano.Core.Codecs.Raw; + +/// +/// Colour filter array layout (DNG 1.6 §"CFARepeatPatternDim"/"CFAPattern"): the colour of every cell of a pattern +/// that repeats over the image, 0 = red, 1 = green, 2 = blue. The origin is the top-left pixel of the image the pattern +/// is attached to (the DNG active area); re-anchors it for a sub-image. +/// +public sealed class CfaPattern +{ + public const int MaxSize = 8; + + private readonly byte[] _colors; + + /// The size is outside 1–8, a colour is not 0–2, or a primary is missing. + public CfaPattern(int width, int height, ReadOnlySpan colors) + { + if (width is < 1 or > MaxSize || height is < 1 or > MaxSize || colors.Length < width * height) + throw new InvalidDataException($"CFA: unsupported pattern size {width}x{height}"); + Span seen = stackalloc bool[3]; + foreach (var c in colors[..(width * height)]) + { + if (c > 2) throw new InvalidDataException($"CFA: colour {c} is not red, green or blue"); + seen[c] = true; + } + if (!seen[0] || !seen[1] || !seen[2]) throw new InvalidDataException("CFA: pattern lacks a primary colour"); + (Width, Height, _colors) = Reduce(width, height, colors[..(width * height)]); + } + + /// Repeat width after reduction to the smallest period (a 4×4 Bayer description becomes 2×2). + public int Width { get; } + + public int Height { get; } + + /// A 2×2 pattern with one red, one blue and two diagonal greens. + public bool IsBayer => Width == 2 && Height == 2 && (_colors[0] == 1 && _colors[3] == 1 || _colors[1] == 1 && _colors[2] == 1); + + /// Block edge for colour binning (half-size decoding): 2 for Bayer, 3 when every aligned 3×3 block holds all + /// three primaries (X-Trans), 0 when the pattern cannot be binned. + public int BinSize + { + get + { + if (IsBayer) return 2; + if (Width % 3 != 0 || Height % 3 != 0) return 0; + for (var by = 0; by < Height; by += 3) + { + for (var bx = 0; bx < Width; bx += 3) + { + var mask = 0; + for (var y = 0; y < 3; y++) + { + for (var x = 0; x < 3; x++) mask |= 1 << ColorAt(bx + x, by + y); + } + if (mask != 7) return 0; + } + } + return 3; + } + } + + /// Colour at image coordinates (non-negative). + public int ColorAt(int x, int y) => _colors[y % Height * Width + x % Width]; + + /// Pattern of the sub-image whose top-left pixel is (, ) here. + public CfaPattern Shift(int dx, int dy) + { + var colors = new byte[Width * Height]; + for (var y = 0; y < Height; y++) + { + for (var x = 0; x < Width; x++) colors[y * Width + x] = (byte)ColorAt(x + PositiveModulo(dx, Width), y + PositiveModulo(dy, Height)); + } + return new CfaPattern(Width, Height, colors); + } + + /// Bayer pattern from its top-left 2×2 letters ("RGGB", "BGGR", "GRBG", "GBRG"). + public static CfaPattern Bayer(string layout) + { + if (layout.Length != 4) throw new ArgumentException("Four letters expected", nameof(layout)); + var colors = new byte[4]; + for (var i = 0; i < 4; i++) + { + colors[i] = char.ToUpperInvariant(layout[i]) switch + { + 'R' => 0, + 'G' => 1, + 'B' => 2, + _ => throw new ArgumentException("Letters must be R, G or B", nameof(layout)), + }; + } + return new CfaPattern(2, 2, colors); + } + + public override string ToString() + { + var sb = new StringBuilder(Width * Height + 8); + for (var i = 0; i < Width * Height; i++) sb.Append("RGB"[_colors[i]]); + return Width == 2 && Height == 2 ? sb.ToString() : $"{Width}x{Height}:{sb}"; + } + + private static int PositiveModulo(int value, int m) => (value % m + m) % m; + + private static (int Width, int Height, byte[] Colors) Reduce(int width, int height, ReadOnlySpan colors) + { + var w = width; + for (var p = 1; p < width; p++) + { + if (width % p == 0 && Periodic(colors, width, height, p, 0)) + { + w = p; + break; + } + } + var h = height; + for (var p = 1; p < height; p++) + { + if (height % p == 0 && Periodic(colors, width, height, 0, p)) + { + h = p; + break; + } + } + var reduced = new byte[w * h]; + for (var y = 0; y < h; y++) + { + for (var x = 0; x < w; x++) reduced[y * w + x] = colors[y * width + x]; + } + return (w, h, reduced); + } + + private static bool Periodic(ReadOnlySpan colors, int width, int height, int px, int py) + { + for (var y = 0; y < height; y++) + { + for (var x = 0; x < width; x++) + { + if (colors[y * width + x] != colors[(y + py) % height * width + (x + px) % width]) return false; + } + } + return true; + } +} diff --git a/src/Titano.Core/Codecs/Raw/Demosaic.cs b/src/Titano.Core/Codecs/Raw/Demosaic.cs new file mode 100644 index 00000000..ee3987a6 --- /dev/null +++ b/src/Titano.Core/Codecs/Raw/Demosaic.cs @@ -0,0 +1,202 @@ +using Titano.Core.Imaging; + +namespace Titano.Core.Codecs.Raw; + +/// +/// Fast CFA reconstructions: bilinear for Bayer mosaics (Draft quality), a colour-difference neighbourhood interpolation +/// for any other pattern (X-Trans), and colour binning (half/third size). Inputs are single-plane mosaics with the pattern +/// anchored at pixel (0, 0); outputs are R, G, B planes. +/// +internal static class Demosaic +{ + /// Bilinear interpolation of a Bayer mosaic: missing greens from the 4 orthogonal neighbours, red/blue from the + /// 2 or 4 nearest of their colour. Borders mirror around the edge pixel, which preserves the CFA phase. + public static ImageBuffer Bilinear(ImageBuffer mosaic, CfaPattern cfa, int maxDegreeOfParallelism, CancellationToken cancellationToken) + { + if (!cfa.IsBayer) throw new ArgumentException("Bayer pattern expected", nameof(cfa)); + int w = mosaic.Width, h = mosaic.Height; + var output = new ImageBuffer(w, h, 3, clear: false); + var options = new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism, CancellationToken = cancellationToken }; + Parallel.For(0, h, options, y => + { + var interior = y >= 1 && y < h - 1; + for (var x = 0; x < w; x++) + { + if (interior && x >= 1 && x < w - 1) BilinearInterior(mosaic, cfa, output, x, y); + else BilinearAt(mosaic, cfa, output, x, y); + } + }); + return output; + } + + /// Bilinear values for the pixels within of the border (AHD leaves them to this). + public static void BilinearBorder(ImageBuffer mosaic, CfaPattern cfa, ImageBuffer output, int band) + { + int w = mosaic.Width, h = mosaic.Height; + for (var y = 0; y < h; y++) + { + var edgeRow = y < band || y >= h - band; + for (var x = 0; x < w; x++) + { + if (!edgeRow && x == band) x = int.Max(x, w - band); + if (x < w) BilinearAt(mosaic, cfa, output, x, y); + } + } + } + + private static void BilinearInterior(ImageBuffer mosaic, CfaPattern cfa, ImageBuffer output, int x, int y) + { + var m = mosaic.Plane(0); + var w = mosaic.Width; + var i = y * w + x; + var c = cfa.ColorAt(x, y); + float r, g, b; + if (c == 1) + { + var horizontal = (m[i - 1] + m[i + 1]) * 0.5f; + var vertical = (m[i - w] + m[i + w]) * 0.5f; + g = m[i]; + if (cfa.ColorAt(x + 1, y) == 0) (r, b) = (horizontal, vertical); + else (r, b) = (vertical, horizontal); + } + else + { + g = (m[i - 1] + m[i + 1] + m[i - w] + m[i + w]) * 0.25f; + var diagonal = (m[i - w - 1] + m[i - w + 1] + m[i + w - 1] + m[i + w + 1]) * 0.25f; + (r, b) = c == 0 ? (m[i], diagonal) : (diagonal, m[i]); + } + output.Plane(0)[i] = r; + output.Plane(1)[i] = g; + output.Plane(2)[i] = b; + } + + private static void BilinearAt(ImageBuffer mosaic, CfaPattern cfa, ImageBuffer output, int x, int y) + { + Span sum = stackalloc float[3]; + Span count = stackalloc int[3]; + var own = cfa.ColorAt(x, y); + for (var dy = -1; dy <= 1; dy++) + { + for (var dx = -1; dx <= 1; dx++) + { + // Greens come from orthogonal neighbours only; the diagonal ones belong to the other green rows. + var sx = Mirror(x + dx, mosaic.Width); + var sy = Mirror(y + dy, mosaic.Height); + var c = cfa.ColorAt(sx, sy); + if (c == own && (dx | dy) != 0) continue; + if (c == 1 && dx != 0 && dy != 0) continue; + sum[c] += mosaic.Plane(0)[sy * mosaic.Width + sx]; + count[c]++; + } + } + var i = y * mosaic.Width + x; + for (var c = 0; c < 3; c++) output.Plane(c)[i] = count[c] > 0 ? sum[c] / count[c] : 0f; + } + + private static int Mirror(int v, int size) => v < 0 ? int.Min(-v, size - 1) : v >= size ? int.Max(2 * size - 2 - v, 0) : v; + + /// + /// Colour-difference interpolation for any CFA (used for X-Trans): green from the greens of the 3×3 neighbourhood, then + /// red and blue as interpolated green plus the mean (colour − green) difference of their own 3×3 samples (5×5 when the + /// 3×3 holds none). Simpler than Markesteijn's X-Trans algorithm but artefact-free on smooth content. + /// + public static ImageBuffer Neighbourhood(ImageBuffer mosaic, CfaPattern cfa, int maxDegreeOfParallelism, CancellationToken cancellationToken) + { + int w = mosaic.Width, h = mosaic.Height; + var output = new ImageBuffer(w, h, 3, clear: false); + var m = mosaic.Storage; + var green = output.Storage.AsMemory(w * h, w * h); + var options = new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism, CancellationToken = cancellationToken }; + Parallel.For(0, h, options, y => + { + var g = green.Span; + for (var x = 0; x < w; x++) + { + g[y * w + x] = cfa.ColorAt(x, y) == 1 ? m[y * w + x] : Mean(m, w, h, cfa, x, y, 1, null); + } + }); + Parallel.For(0, h, options, y => + { + var g = green.Span; + var red = output.Row(0, y); + var blue = output.Row(2, y); + for (var x = 0; x < w; x++) + { + var own = cfa.ColorAt(x, y); + var gv = g[y * w + x]; + red[x] = own == 0 ? m[y * w + x] : float.Max(0f, gv + Mean(m, w, h, cfa, x, y, 0, g)); + blue[x] = own == 2 ? m[y * w + x] : float.Max(0f, gv + Mean(m, w, h, cfa, x, y, 2, g)); + } + }); + return output; + } + + /// Mean of colour samples around (x, y) — of (sample − green) when + /// is given — over 3×3, widened to 5×5 when the 3×3 holds none. + private static float Mean(float[] m, int w, int h, CfaPattern cfa, int x, int y, int color, Span green) + { + for (var r = 1; r <= 2; r++) + { + var sum = 0f; + var n = 0; + for (var dy = -r; dy <= r; dy++) + { + var sy = y + dy; + if ((uint)sy >= (uint)h) continue; + for (var dx = -r; dx <= r; dx++) + { + var sx = x + dx; + if ((uint)sx >= (uint)w || (dx | dy) == 0 || cfa.ColorAt(sx, sy) != color) continue; + var i = sy * w + sx; + sum += green.IsEmpty ? m[i] : m[i] - green[i]; + n++; + } + } + if (n > 0) return sum / n; + } + return 0f; + } + + /// + /// Colour binning: each aligned × block becomes one RGB pixel holding the + /// mean of its samples of each colour (dcraw's half-size mode for Bayer; one third for X-Trans). Partial blocks at the + /// right/bottom edge are dropped. + /// + public static ImageBuffer Bin(ImageBuffer mosaic, CfaPattern cfa, int size, int maxDegreeOfParallelism, CancellationToken cancellationToken) + { + int w = mosaic.Width / size, h = mosaic.Height / size; + if (w < 1 || h < 1) throw new InvalidDataException("RAW: image too small to bin"); + var output = new ImageBuffer(w, h, 3, clear: false); + var src = mosaic.Storage; + var stride = mosaic.Width; + var options = new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism, CancellationToken = cancellationToken }; + Parallel.For(0, h, options, by => + { + Span sum = stackalloc float[3]; + Span count = stackalloc int[3]; + var r = output.Row(0, by); + var g = output.Row(1, by); + var b = output.Row(2, by); + for (var bx = 0; bx < w; bx++) + { + sum.Clear(); + count.Clear(); + for (var dy = 0; dy < size; dy++) + { + var y = by * size + dy; + for (var dx = 0; dx < size; dx++) + { + var x = bx * size + dx; + var c = cfa.ColorAt(x, y); + sum[c] += src[y * stride + x]; + count[c]++; + } + } + r[bx] = sum[0] / count[0]; + g[bx] = sum[1] / count[1]; + b[bx] = sum[2] / count[2]; + } + }); + return output; + } +} diff --git a/src/Titano.Core/Codecs/Raw/HighlightReconstruction.cs b/src/Titano.Core/Codecs/Raw/HighlightReconstruction.cs new file mode 100644 index 00000000..76c18f88 --- /dev/null +++ b/src/Titano.Core/Codecs/Raw/HighlightReconstruction.cs @@ -0,0 +1,120 @@ +using Titano.Core.Imaging; + +namespace Titano.Core.Codecs.Raw; + +/// +/// Clip-aware highlight reconstruction on white-balanced camera RGB. Per pixel, the channels whose raw samples clipped +/// (bit c of the clip map) are only known to be at least their clip level; they are raised to the mean of the unclipped +/// channels when that is larger (a near-white highlight that clipped green first comes back neutral instead of magenta), +/// then the pixel is desaturated toward a grey at its brightest channel as the unclipped channels approach their own clip +/// levels. Fully clipped pixels become that grey at no less than the highest clip level. Pixels without clipped samples +/// are untouched. +/// +internal static class HighlightReconstruction +{ + /// Fraction of its clip level at which an unclipped channel starts pulling the pixel toward white. + private const float DesaturationStart = 0.7f; + + /// White-balanced camera RGB, modified in place. + /// Per pixel: bit c set when channel c is built from clipped raw samples. + /// Clip level of each channel in 's units (the white-balance multipliers + /// when raw white is 1). + public static void Apply(ImageBuffer rgb, byte[] clipBits, ReadOnlySpan clipLevels, int maxDegreeOfParallelism, CancellationToken cancellationToken) + { + var levels = clipLevels.ToArray(); + var top = float.Max(levels[0], float.Max(levels[1], levels[2])); + int w = rgb.Width; + Parallel.For(0, rgb.Height, new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism, CancellationToken = cancellationToken }, y => + { + var r = rgb.Row(0, y); + var g = rgb.Row(1, y); + var b = rgb.Row(2, y); + var bits = clipBits.AsSpan(y * w, w); + Span v = stackalloc float[3]; + for (var x = 0; x < w; x++) + { + var clipped = bits[x] & 7; + if (clipped == 0) continue; + v[0] = r[x]; + v[1] = g[x]; + v[2] = b[x]; + if (clipped == 7) + { + var level = float.Max(top, float.Max(v[0], float.Max(v[1], v[2]))); + r[x] = g[x] = b[x] = level; + continue; + } + float sum = 0, closeness = float.MaxValue; + var n = 0; + for (var c = 0; c < 3; c++) + { + if ((clipped & 1 << c) != 0) continue; + sum += v[c]; + n++; + closeness = float.Min(closeness, v[c] / levels[c]); + } + var estimate = sum / n; + for (var c = 0; c < 3; c++) + { + if ((clipped & 1 << c) != 0) v[c] = float.Max(v[c], estimate); + } + var t = float.Clamp((closeness - DesaturationStart) / (1 - DesaturationStart), 0f, 1f); + t = t * t * (3 - 2 * t); + var grey = float.Max(v[0], float.Max(v[1], v[2])); + r[x] = v[0] + (grey - v[0]) * t; + g[x] = v[1] + (grey - v[1]) * t; + b[x] = v[2] + (grey - v[2]) * t; + } + }); + } + + /// Channel clip bits of a demosaiced image: the OR of the mosaic's clip flags over each pixel's 3×3 + /// neighbourhood (the samples its interpolation draws on). + public static byte[] Dilate(byte[] mask, int width, int height) + { + var horizontal = new byte[mask.Length]; + for (var y = 0; y < height; y++) + { + var o = y * width; + for (var x = 0; x < width; x++) + { + var v = mask[o + x]; + if (x > 0) v |= mask[o + x - 1]; + if (x < width - 1) v |= mask[o + x + 1]; + horizontal[o + x] = v; + } + } + var result = new byte[mask.Length]; + for (var y = 0; y < height; y++) + { + var o = y * width; + for (var x = 0; x < width; x++) + { + var v = horizontal[o + x]; + if (y > 0) v |= horizontal[o - width + x]; + if (y < height - 1) v |= horizontal[o + width + x]; + result[o + x] = v; + } + } + return result; + } + + /// Clip bits of a binned image: the OR over each × block. + public static byte[] Bin(byte[] mask, int width, int size, int outWidth, int outHeight) + { + var result = new byte[outWidth * outHeight]; + for (var by = 0; by < outHeight; by++) + { + for (var bx = 0; bx < outWidth; bx++) + { + byte v = 0; + for (var dy = 0; dy < size; dy++) + { + for (var dx = 0; dx < size; dx++) v |= mask[(by * size + dy) * width + bx * size + dx]; + } + result[by * outWidth + bx] = v; + } + } + return result; + } +} diff --git a/src/Titano.Core/Codecs/Raw/RawDecoder.cs b/src/Titano.Core/Codecs/Raw/RawDecoder.cs new file mode 100644 index 00000000..9e1dff47 --- /dev/null +++ b/src/Titano.Core/Codecs/Raw/RawDecoder.cs @@ -0,0 +1,126 @@ +using Titano.Core.Codecs.Dng; +using Titano.Core.Imaging; +using Titano.Core.Metadata; + +namespace Titano.Core.Codecs.Raw; + +/// Demosaic quality: AHD for renders and proxies, bilinear for thumbnails and analysis. +public enum DemosaicQuality +{ + /// Bilinear (Bayer) — fast, slightly soft, some colour fringing on fine detail. + Draft, + + /// Adaptive homogeneity-directed (Bayer); X-Trans always uses the neighbourhood interpolation. + Best, +} + +/// Options for . +public sealed record RawDecodeOptions +{ + public static RawDecodeOptions Default { get; } = new(); + + public DemosaicQuality Quality { get; init; } = DemosaicQuality.Best; + + /// Bin CFA blocks into RGB pixels instead of demosaicing: half size for Bayer, one third for X-Trans. + /// Ignored for linear (already demosaiced) data and for patterns that cannot be binned. + public bool Binning { get; init; } + + /// Rotate/flip according to the file's orientation. + public bool ApplyOrientation { get; init; } = true; + + /// Worker limit (-1 = all cores). + public int MaxDegreeOfParallelism { get; init; } = -1; +} + +/// What a RAW container holds, read without decoding its pixels. +/// Container format. +/// Decoder that would develop it ("managed-dng", "managed-cr2"…). +/// Developed width at full size, after crop and orientation. +/// Developed height at full size, after crop and orientation. +/// CFA pattern ("RGGB", "6x6:…") or "linear". +/// Exif orientation applied when requested. +public sealed record RawImageInfo(ImageFileFormat Format, string Decoder, string? Make, string? Model, int Width, int Height, string Layout, int Orientation); + +/// Result of : native support plus the reason when unsupported. +public sealed record RawProbe(RawImageInfo? Info, string? Reason, IReadOnlyList Warnings) +{ + public bool IsSupported => Info is not null; +} + +/// A developed RAW: linear Rec.709 (D65) RGB, BaselineExposure applied. Dispose to return the pixels to the pool. +public sealed class RawDecodeResult(ImageBuffer image, RawImageInfo info, IReadOnlyList warnings) : IDisposable +{ + public ImageBuffer Image { get; } = image; + public RawImageInfo Info { get; } = info; + public IReadOnlyList Warnings { get; } = warnings; + + public void Dispose() => Image.Dispose(); +} + +/// +/// Managed RAW decoding (BRIEF §8.2): DNG (CFA and LinearRaw) through , +/// developed by the shared pipeline. Bodies or encodings +/// without support are reported by so callers can fall back to the embedded preview. +/// +public static class RawDecoder +{ + /// Formats with a managed decoder (individual files may still be unsupported). + public static IReadOnlyList Formats { get; } = + [ImageFileFormat.Dng]; + + /// Parses the container and decides whether it can be developed natively; never reads the pixel payload. + public static RawProbe Probe(Stream stream, string? fileName = null) + { + ArgumentNullException.ThrowIfNull(stream); + var warnings = new List(); + try + { + var (format, frame) = Open(stream, fileName); + warnings.AddRange(frame.Warnings); + return new RawProbe(Describe(format, frame, RawDecodeOptions.Default), null, warnings); + } + catch (InvalidDataException ex) + { + return new RawProbe(null, ex.Message, warnings); + } + catch (IOException ex) + { + return new RawProbe(null, "I/O error: " + ex.Message, warnings); + } + } + + /// Develops the RAW in (readable and seekable). + /// Breaks signature ties between formats sharing a container (see ). + /// Malformed file, or a body/encoding without native support. + public static RawDecodeResult Decode(Stream stream, string? fileName = null, RawDecodeOptions? options = null, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(stream); + var o = options ?? RawDecodeOptions.Default; + var (format, frame) = Open(stream, fileName); + cancellationToken.ThrowIfCancellationRequested(); + var samples = frame.Loader.Load(stream, o.MaxDegreeOfParallelism, cancellationToken); + var warnings = new List(frame.Warnings); + warnings.AddRange(samples.Warnings); + var image = RawDeveloper.Develop(frame, samples, o, warnings, cancellationToken); + return new RawDecodeResult(image, Describe(format, frame, o), warnings); + } + + private static (ImageFileFormat Format, RawFrame Frame) Open(Stream stream, string? fileName) + { + if (!stream.CanSeek || !stream.CanRead) throw new ArgumentException("The stream must be readable and seekable.", nameof(stream)); + var format = ImageSignature.Detect(stream, fileName); + var frame = format switch + { + ImageFileFormat.Dng => DngReader.Read(stream), + _ => throw new InvalidDataException($"RAW: no managed decoder for {format}"), + }; + return (format, frame); + } + + private static RawImageInfo Describe(ImageFileFormat format, RawFrame frame, RawDecodeOptions options) + { + var (w, h) = frame.OutputSize; + if (options.ApplyOrientation && frame.Orientation is >= 5 and <= 8) (w, h) = (h, w); + return new RawImageInfo(format, frame.Decoder, frame.Make, frame.Model, w, h, frame.Cfa?.ToString() ?? "linear", frame.Orientation); + } +} diff --git a/src/Titano.Core/Codecs/Raw/RawDeveloper.cs b/src/Titano.Core/Codecs/Raw/RawDeveloper.cs new file mode 100644 index 00000000..94e0253d --- /dev/null +++ b/src/Titano.Core/Codecs/Raw/RawDeveloper.cs @@ -0,0 +1,316 @@ +using Titano.Core.Codecs.Dng; +using Titano.Core.Imaging; +using Titano.Core.Mathematics; + +namespace Titano.Core.Codecs.Raw; + +/// +/// The raw development pipeline shared by DNG and the native RAW readers (BRIEF §8.2; DNG 1.6 chapter 5 "Mapping Raw +/// Values to Linear Reference Values" and chapter 6): OpcodeList1 on stored values → linearisation table, black level +/// (repeat pattern + row/column deltas) and white scaling to 0..1 over the active area, with a clip map of saturated +/// samples → OpcodeList2 → white-balance multipliers → demosaic (AHD, bilinear, neighbourhood or binning) → highlight +/// reconstruction → OpcodeList3 → default crop → camera → linear Rec.709 matrix with BaselineExposure → default scale → +/// orientation. +/// +internal static class RawDeveloper +{ + /// Normalised level at or above which a sample counts as clipped. + private const float ClipThreshold = 0.999f; + + /// LibRaw's adjust_maximum_thr: the data maximum replaces the nominal white level above this fraction of it. + private const double AdjustMaximumThreshold = 0.75; + + public static ImageBuffer Develop(RawFrame frame, RawSamples samples, RawDecodeOptions options, ICollection warnings, CancellationToken cancellationToken) + { + if (samples.Width != frame.Width || samples.Height != frame.Height || samples.SamplesPerPixel != frame.SamplesPerPixel) + throw new InvalidDataException("RAW: decoded payload does not match the declared raster"); + var dop = options.MaxDegreeOfParallelism; + var spp = frame.SamplesPerPixel; + var active = frame.ActiveArea; + var black = samples.MeasuredBlack ?? frame.Black; + var white = (double[])frame.White.Clone(); + if (frame.AdjustWhiteToData) AdjustWhite(samples, active, white, warnings); + + var list1 = DngOpcodes.Parse(frame.OpcodeList1, 1, warnings); + ImageBuffer? stage1 = null; + if (list1.Count > 0) + { + stage1 = ToStage1(samples, dop, cancellationToken); + var image = new OpcodeImage(stage1, 0, 0, 1, spp == 1 ? frame.Cfa?.Shift(-active.Left, -active.Top) : null); + DngOpcodes.Apply(list1, image, warnings, cancellationToken); + stage1 = image.Pixels; + if (stage1.Width != samples.Width || stage1.Height != samples.Height) + { + stage1.Dispose(); + throw new InvalidDataException("RAW: OpcodeList1 changed the raster size"); + } + } + + ImageBuffer linear; + byte[]? clip; + try + { + (linear, clip) = Linearize(frame, samples, stage1, black, white, dop, cancellationToken); + } + finally + { + stage1?.Dispose(); + } + + var stage = new OpcodeImage(linear, 0, 0, 1, frame.Cfa) { ClipMask = clip }; + try + { + DngOpcodes.Apply(DngOpcodes.Parse(frame.OpcodeList2, 2, warnings), stage, warnings, cancellationToken); + return frame.Cfa is null && spp == 1 + ? DevelopMonochrome(frame, stage, options, warnings, cancellationToken) + : DevelopColour(frame, stage, options, warnings, cancellationToken); + } + finally + { + stage.Pixels.Dispose(); + } + } + + private static ImageBuffer DevelopColour(RawFrame frame, OpcodeImage stage, RawDecodeOptions options, ICollection warnings, CancellationToken cancellationToken) + { + var dop = options.MaxDegreeOfParallelism; + var colour = frame.Color.Solve(warnings); + var n = colour.CameraNeutral; + float[] wb = [(float)(1 / n.X), (float)(1 / n.Y), (float)(1 / n.Z)]; + + ImageBuffer rgb; + byte[]? bits = null; + var step = 1; + if (stage.Cfa is { } cfa) + { + ScaleMosaic(stage.Pixels, cfa, wb, dop, cancellationToken); + var bin = options.Binning ? cfa.BinSize : 0; + if (bin > 0) + { + rgb = Demosaic.Bin(stage.Pixels, cfa, bin, dop, cancellationToken); + step = bin; + if (stage.ClipMask is { } mask) bits = HighlightReconstruction.Bin(mask, stage.Width, bin, rgb.Width, rgb.Height); + } + else + { + rgb = !cfa.IsBayer ? Demosaic.Neighbourhood(stage.Pixels, cfa, dop, cancellationToken) + : options.Quality == DemosaicQuality.Best ? AhdDemosaic.Run(stage.Pixels, cfa, colour.BalancedToXyz, dop, cancellationToken) + : Demosaic.Bilinear(stage.Pixels, cfa, dop, cancellationToken); + if (stage.ClipMask is { } mask) bits = HighlightReconstruction.Dilate(mask, stage.Width, stage.Height); + } + } + else + { + rgb = stage.Pixels.Clone(); + for (var c = 0; c < 3; c++) Scale(rgb.Plane(c), wb[c]); + bits = stage.ClipMask; + } + + var image = new OpcodeImage(rgb, stage.Left, stage.Top, step, null); + try + { + if (bits is not null) HighlightReconstruction.Apply(rgb, bits, wb, dop, cancellationToken); + var list3 = DngOpcodes.Parse(frame.OpcodeList3, 3, warnings); + if (list3.Count > 0) + { + // Value-mapping opcodes are defined on unbalanced camera values; gains and warps commute with the multipliers. + var unbalance = list3.Exists(o => o.NeedsUnbalancedValues); + if (unbalance) ScalePlanes(image.Pixels, [1 / wb[0], 1 / wb[1], 1 / wb[2]]); + DngOpcodes.Apply(list3, image, warnings, cancellationToken); + if (unbalance) ScalePlanes(image.Pixels, wb); + } + using var cropped = Crop(frame, image); + ApplyMatrix(cropped, colour.BalancedToRec709, dop, cancellationToken); + return Finish(frame, cropped, options); + } + finally + { + image.Pixels.Dispose(); + } + } + + private static ImageBuffer DevelopMonochrome(RawFrame frame, OpcodeImage stage, RawDecodeOptions options, ICollection warnings, CancellationToken cancellationToken) + { + var image = new OpcodeImage(stage.Pixels.Clone(), stage.Left, stage.Top, 1, null); + try + { + DngOpcodes.Apply(DngOpcodes.Parse(frame.OpcodeList3, 3, warnings), image, warnings, cancellationToken); + using var cropped = Crop(frame, image); + Scale(cropped.Plane(0), (float)double.Exp2(frame.Color.BaselineExposure)); + using var rgb = PixelConvert.GrayToRgb(cropped); + return Finish(frame, rgb, options); + } + finally + { + image.Pixels.Dispose(); + } + } + + /// Default scale, then orientation; always returns a new buffer. + private static ImageBuffer Finish(RawFrame frame, ImageBuffer image, RawDecodeOptions options) + { + var (sh, sv) = frame.DefaultScale; + ImageBuffer? scaled = null; + if (sh != sv && sh > 0 && sv > 0) + { + // Only the ratio matters for square output pixels; stretch the axis with the larger factor. + var ratio = sh / sv; + var (w, h) = ratio > 1 ? ((int)double.Round(image.Width * ratio), image.Height) : (image.Width, (int)double.Round(image.Height / ratio)); + scaled = Resampler.Resize(image, int.Max(1, w), int.Max(1, h)); + } + var source = scaled ?? image; + try + { + return options.ApplyOrientation && frame.Orientation is >= 2 and <= 8 + ? PixelConvert.ApplyOrientation(source, frame.Orientation) + : source.Clone(); + } + finally + { + scaled?.Dispose(); + } + } + + /// Stored values as floats divided by 65535 (the stage-1 domain of the opcodes), sample planes split. + private static ImageBuffer ToStage1(RawSamples samples, int dop, CancellationToken cancellationToken) + { + int w = samples.Width, h = samples.Height, spp = samples.SamplesPerPixel; + var image = new ImageBuffer(w, h, spp == 1 ? 1 : 3, clear: false); + Parallel.For(0, h, new ParallelOptions { MaxDegreeOfParallelism = dop, CancellationToken = cancellationToken }, y => + { + for (var s = 0; s < image.Channels; s++) + { + var row = image.Row(s, y); + var o = (long)y * w * spp + s; + for (var x = 0; x < w; x++) row[x] = samples[o + (long)x * spp] / 65535f; + } + }); + return image; + } + + /// Linear reference values over the active area plus a clip map (bit = CFA colour or sample plane), or null + /// when nothing clipped. + private static (ImageBuffer Image, byte[]? Clip) Linearize(RawFrame frame, RawSamples samples, ImageBuffer? stage1, RawBlackLevel black, double[] white, int dop, CancellationToken cancellationToken) + { + var active = frame.ActiveArea; + int w = active.Width, h = active.Height, spp = frame.SamplesPerPixel; + var planes = spp == 1 ? 1 : 3; + var image = new ImageBuffer(w, h, planes, clear: false); + var clip = new byte[w * h]; + var anyClip = 0; + var table = frame.Linearization; + var scale = new float[planes]; + for (var s = 0; s < planes; s++) + { + var range = white[int.Min(s, white.Length - 1)] - black.Max(s, spp); + if (!(range > 0)) throw new InvalidDataException("RAW: white level not above black level"); + scale[s] = (float)(1 / range); + } + var cfa = frame.Cfa; + Parallel.For(0, h, new ParallelOptions { MaxDegreeOfParallelism = dop, CancellationToken = cancellationToken }, y => + { + var sy = active.Top + y; + var blackRow = y % black.RepeatRows * black.RepeatColumns * spp; + var deltaV = black.DeltaV is { } dv && y < dv.Length ? dv[y] : 0f; + var rowClip = 0; + for (var s = 0; s < planes; s++) + { + var row = image.Row(s, y); + var source = stage1 is null ? default : stage1.Row(s, sy); + var o = (long)sy * samples.Width * spp + s; + for (var x = 0; x < w; x++) + { + var sx = active.Left + x; + var v = source.IsEmpty ? samples[o + (long)sx * spp] : source[sx] * 65535f; + if (table is not null) v = table[int.Clamp((int)(v + 0.5f), 0, table.Length - 1)]; + var b = black.Values[blackRow + x % black.RepeatColumns * spp + s] + deltaV + + (black.DeltaH is { } dh && x < dh.Length ? dh[x] : 0f); + var value = (v - b) * scale[s]; + row[x] = value; + if (value >= ClipThreshold) + { + clip[y * w + x] |= (byte)(1 << (cfa is null ? s : cfa.ColorAt(x, y))); + rowClip = 1; + } + } + } + if (rowClip != 0) Volatile.Write(ref anyClip, 1); + }); + return (image, anyClip != 0 ? clip : null); + } + + private static void AdjustWhite(RawSamples samples, RawRect active, double[] white, ICollection warnings) + { + var max = 0f; + for (var y = active.Top; y < active.Bottom; y++) + { + var o = (long)y * samples.Width * samples.SamplesPerPixel; + for (var x = active.Left * samples.SamplesPerPixel; x < active.Right * samples.SamplesPerPixel; x++) max = float.Max(max, samples[o + x]); + } + for (var s = 0; s < white.Length; s++) + { + if (max > 0 && max < white[s] && max > white[s] * AdjustMaximumThreshold) white[s] = max; + } + if (max > white.Max()) warnings.Add($"RAW: samples exceed the white level ({max} > {white.Max()})"); + } + + private static void ScaleMosaic(ImageBuffer mosaic, CfaPattern cfa, float[] wb, int dop, CancellationToken cancellationToken) + { + Parallel.For(0, mosaic.Height, new ParallelOptions { MaxDegreeOfParallelism = dop, CancellationToken = cancellationToken }, y => + { + var row = mosaic.Row(0, y); + Span phase = stackalloc float[CfaPattern.MaxSize]; + for (var i = 0; i < cfa.Width; i++) phase[i] = wb[cfa.ColorAt(i, y)]; + for (var x = 0; x < row.Length; x++) row[x] *= phase[x % cfa.Width]; + }); + } + + private static void ScalePlanes(ImageBuffer image, float[] factors) + { + for (var c = 0; c < 3; c++) Scale(image.Plane(c), factors[c]); + } + + private static void Scale(Span values, float factor) + { + for (var i = 0; i < values.Length; i++) values[i] *= factor; + } + + /// Default crop (stage coordinates relative to the active area) intersected with what the opcodes left. + private static ImageBuffer Crop(RawFrame frame, OpcodeImage image) + { + var crop = frame.DefaultCrop ?? new RawRect(0, 0, frame.ActiveArea.Width, frame.ActiveArea.Height); + var area = crop.Intersect(image.Bounds); + if (area.IsEmpty) area = image.Bounds; + var x0 = int.Clamp((int)double.Ceiling(image.BufferX(area.Left) - 1e-9), 0, image.Width - 1); + var y0 = int.Clamp((int)double.Ceiling(image.BufferY(area.Top) - 1e-9), 0, image.Height - 1); + var x1 = int.Clamp((int)double.Floor(image.BufferX(area.Right - 1) + 1e-9) + 1, x0 + 1, image.Width); + var y1 = int.Clamp((int)double.Floor(image.BufferY(area.Bottom - 1) + 1e-9) + 1, y0 + 1, image.Height); + var src = image.Pixels; + var dst = new ImageBuffer(x1 - x0, y1 - y0, src.Channels, clear: false); + for (var c = 0; c < src.Channels; c++) + { + for (var y = y0; y < y1; y++) src.Row(c, y).Slice(x0, x1 - x0).CopyTo(dst.Row(c, y - y0)); + } + return dst; + } + + private static void ApplyMatrix(ImageBuffer image, Matrix3 m, int dop, CancellationToken cancellationToken) + { + float m11 = (float)m.M11, m12 = (float)m.M12, m13 = (float)m.M13; + float m21 = (float)m.M21, m22 = (float)m.M22, m23 = (float)m.M23; + float m31 = (float)m.M31, m32 = (float)m.M32, m33 = (float)m.M33; + Parallel.For(0, image.Height, new ParallelOptions { MaxDegreeOfParallelism = dop, CancellationToken = cancellationToken }, y => + { + var r = image.Row(0, y); + var g = image.Row(1, y); + var b = image.Row(2, y); + for (var x = 0; x < r.Length; x++) + { + float vr = r[x], vg = g[x], vb = b[x]; + r[x] = m11 * vr + m12 * vg + m13 * vb; + g[x] = m21 * vr + m22 * vg + m23 * vb; + b[x] = m31 * vr + m32 * vg + m33 * vb; + } + }); + } +} diff --git a/src/Titano.Core/Codecs/Raw/RawFrame.cs b/src/Titano.Core/Codecs/Raw/RawFrame.cs new file mode 100644 index 00000000..9d5e940b --- /dev/null +++ b/src/Titano.Core/Codecs/Raw/RawFrame.cs @@ -0,0 +1,141 @@ +using Titano.Core.Codecs.Dng; + +namespace Titano.Core.Codecs.Raw; + +/// Integer rectangle (left, top inclusive; right, bottom exclusive). +public readonly record struct RawRect(int Left, int Top, int Right, int Bottom) +{ + public int Width => Right - Left; + public int Height => Bottom - Top; + public bool IsEmpty => Width <= 0 || Height <= 0; + + public RawRect Intersect(RawRect other) => new( + int.Max(Left, other.Left), int.Max(Top, other.Top), int.Min(Right, other.Right), int.Min(Bottom, other.Bottom)); + + public override string ToString() => $"{Left},{Top} {Width}x{Height}"; +} + +/// Black level description (DNG BlackLevelRepeatDim, BlackLevel, BlackLevelDeltaH/V), relative to the active area. +internal sealed class RawBlackLevel +{ + public static RawBlackLevel Zero(int samplesPerPixel) => new(1, 1, new float[samplesPerPixel], null, null); + + public RawBlackLevel(int repeatRows, int repeatColumns, float[] values, float[]? deltaH, float[]? deltaV) + { + RepeatRows = repeatRows; + RepeatColumns = repeatColumns; + Values = values; + DeltaH = deltaH; + DeltaV = deltaV; + } + + public int RepeatRows { get; } + public int RepeatColumns { get; } + + /// rows × columns × samples-per-pixel values, sample index fastest. + public float[] Values { get; } + + public float[]? DeltaH { get; } + public float[]? DeltaV { get; } + + /// Largest black level of a sample plane, deltas included (the white-scaling reference of the DNG SDK). + public float Max(int sample, int samplesPerPixel) + { + var max = float.MinValue; + for (var i = sample; i < Values.Length; i += samplesPerPixel) max = float.Max(max, Values[i]); + return max + (DeltaH is { Length: > 0 } h ? MaxOf(h) : 0) + (DeltaV is { Length: > 0 } v ? MaxOf(v) : 0); + } + + private static float MaxOf(float[] values) + { + var max = float.MinValue; + foreach (var v in values) max = float.Max(max, v); + return max; + } +} + +/// Decoded raw samples as stored in the file: interleaved, × × +/// , integers or floats. +internal sealed class RawSamples(int width, int height, int samplesPerPixel, ushort[]? integers, float[]? floats) +{ + public int Width { get; } = width; + public int Height { get; } = height; + public int SamplesPerPixel { get; } = samplesPerPixel; + public ushort[]? Integers { get; } = integers; + public float[]? Floats { get; } = floats; + public List Warnings { get; } = []; + + /// Black level measured by the loader (e.g. from masked sensor columns), overriding the frame's own. + public RawBlackLevel? MeasuredBlack { get; set; } + + public float this[long index] => Integers is { } i ? i[index] : Floats![index]; +} + +/// Reads the pixel payload of one container; created by the container parser, run by the pipeline. +internal abstract class RawLoader +{ + /// The payload is malformed or uses an unsupported encoding. + public abstract RawSamples Load(Stream stream, int maxDegreeOfParallelism, CancellationToken cancellationToken); +} + +/// +/// Everything the development pipeline needs about one raw image, independent of the container: raster geometry, CFA, +/// linearisation, black/white levels, crops, colour profile, opcode lists and orientation. Built by the DNG reader and the +/// native RAW readers without touching the pixel payload. +/// +internal sealed class RawFrame +{ + /// Decoder name for logs ("managed-dng", "managed-cr2"…). + public required string Decoder { get; init; } + + public required int Width { get; init; } + public required int Height { get; init; } + + /// 1 for CFA and monochrome data, 3 for LinearRaw RGB. + public int SamplesPerPixel { get; init; } = 1; + + /// CFA layout anchored at the active area's top-left; null for linear (already demosaiced) data. + public CfaPattern? Cfa { get; init; } + + /// Image area holding valid pixels, in stored coordinates. + public required RawRect ActiveArea { get; init; } + + /// Final crop relative to the active area (DNG DefaultCropOrigin/Size); null = the whole active area. + public RawRect? DefaultCrop { get; init; } + + /// DNG DefaultScale (horizontal, vertical) for non-square pixels. + public (double H, double V) DefaultScale { get; init; } = (1, 1); + + public ushort[]? Linearization { get; init; } + public required RawBlackLevel Black { get; init; } + + /// White level per sample plane, in linearised units. + public required double[] White { get; init; } + + /// Lower the white level to the data maximum when that exceeds 75 % of it (LibRaw adjust_maximum_thr): native + /// RAWs state a nominal bit-depth maximum while sensors saturate earlier. + public bool AdjustWhiteToData { get; init; } + + public required DngColorProfile Color { get; init; } + public byte[]? OpcodeList1 { get; init; } + public byte[]? OpcodeList2 { get; init; } + public byte[]? OpcodeList3 { get; init; } + + /// Exif orientation 1–8. + public int Orientation { get; init; } = 1; + + public string? Make { get; init; } + public string? Model { get; init; } + public required RawLoader Loader { get; init; } + public List Warnings { get; } = []; + + /// Size of the developed image before orientation (active area or default crop, default scale applied). + public (int Width, int Height) OutputSize + { + get + { + var crop = DefaultCrop ?? new RawRect(0, 0, ActiveArea.Width, ActiveArea.Height); + return (int.Max(1, (int)double.Round(crop.Width * DefaultScale.H)), int.Max(1, (int)double.Round(crop.Height * DefaultScale.V))); + } + } +} diff --git a/tests/Titano.Core.Tests/Raw/DngDecoderTests.cs b/tests/Titano.Core.Tests/Raw/DngDecoderTests.cs new file mode 100644 index 00000000..d0a47334 --- /dev/null +++ b/tests/Titano.Core.Tests/Raw/DngDecoderTests.cs @@ -0,0 +1,224 @@ +using Titano.Core.Codecs.Dng; +using Titano.Core.Codecs.Raw; +using Titano.Core.Imaging; +using Titano.Core.Mathematics; +using Titano.Testing; + +namespace Titano.Core.Tests.Raw; + +/// Synthetic DNG round trips: a known linear scene is mosaicked through a known camera model, written with the +/// DNG feature under test, decoded, and compared with the scene. +public sealed class DngDecoderTests(TestContext context) +{ + private const int W = 96, H = 72; + + private static RawDecodeResult Decode(byte[] dng, RawDecodeOptions? options = null) => + RawDecoder.Decode(new MemoryStream(dng), "test.dng", options); + + [Theory] + [Case("RGGB")] + [Case("BGGR")] + [Case("GRBG")] + [Case("GBRG")] + public void BayerPatternsRoundTrip(string layout) + { + using var scene = SyntheticRaw.Smooth(W, H); + var dng = SyntheticDng.Write(scene, new DngSpec { Cfa = CfaPattern.Bayer(layout) }); + using var result = Decode(dng); + Assert.Equal(W, result.Image.Width); + Assert.Equal(H, result.Image.Height); + Assert.Equal(layout, result.Info.Layout); + var psnr = SyntheticRaw.Psnr(scene, result.Image, 4); + var de = SyntheticRaw.MeanDeltaE(scene, result.Image, 4); + context.Log($"{layout}: PSNR {psnr:F1} dB, mean ΔE {de:F2}"); + Assert.GreaterOrEqual(psnr, 40); + Assert.Less(de, 1.0); + } + + [Theory] + [Case(1, false)] + [Case(7, false)] + [Case(7, true)] + [Case(8, false)] + public void CompressionsDecodeIdentically(int compression, bool twoComponents) + { + using var scene = SyntheticRaw.Smooth(W, H); + using var reference = Decode(SyntheticDng.Write(scene, new DngSpec { Compression = 1 })); + using var result = Decode(SyntheticDng.Write(scene, new DngSpec { Compression = compression, TwoComponentTiles = twoComponents, RawInSubIfd = true })); + Assert.Empty(result.Warnings); + Assert.SequenceEqual(reference.Image.Data.ToArray(), result.Image.Data.ToArray()); + } + + [Test] + public void LinearRawRoundTripsExactly() + { + using var scene = SyntheticRaw.Smooth(W, H); + using var result = Decode(SyntheticDng.Write(scene, new DngSpec { Cfa = null })); + Assert.Equal("linear", result.Info.Layout); + Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 0), 60); + } + + [Test] + public void BlackLevelPatternAndDeltasAreSubtracted() + { + using var scene = SyntheticRaw.Smooth(W, H); + var deltaH = Enumerable.Range(0, W).Select(x => (double)(x % 7)).ToArray(); + var deltaV = Enumerable.Range(0, H).Select(y => y * 0.5).ToArray(); + var spec = new DngSpec { BlackRows = 2, BlackColumns = 2, Black = [500, 520, 530, 510], BlackDeltaH = deltaH, BlackDeltaV = deltaV, MarginLeft = 4, MarginTop = 2 }; + using var result = Decode(SyntheticDng.Write(scene, spec)); + Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 40); + } + + [Test] + public void LinearizationTableIsApplied() + { + using var scene = SyntheticRaw.Smooth(W, H); + // 4096 codes covering 0..16383 with a square law: dense in the shadows, sparse in the highlights. + var table = Enumerable.Range(0, 4096).Select(i => (ushort)double.Round(16383.0 * i * i / (4095.0 * 4095.0))).ToArray(); + using var result = Decode(SyntheticDng.Write(scene, new DngSpec { Linearization = table, Black = [0] })); + Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 38); + } + + [Test] + public void ActiveAreaAndDefaultCropSelectTheImage() + { + using var scene = SyntheticRaw.Smooth(W, H); + var spec = new DngSpec { MarginLeft = 6, MarginTop = 4, MarginRight = 2, MarginBottom = 3, Crop = new RawRect(8, 6, 8 + 64, 6 + 48) }; + using var result = Decode(SyntheticDng.Write(scene, spec)); + Assert.Equal(64, result.Image.Width); + Assert.Equal(48, result.Image.Height); + Assert.Equal(64, result.Info.Width); + using var expected = Crop(scene, 8, 6, 64, 48); + Assert.GreaterOrEqual(SyntheticRaw.Psnr(expected, result.Image, 2), 40); + } + + [Theory] + [Case(false)] + [Case(true)] + public void WhiteBalanceFromAsShotNeutralOrWhiteXy(bool whiteXy) + { + // Scene lit by tungsten (2850 K): decoding must adapt it back to the D65 scene. + using var scene = SyntheticRaw.Smooth(W, H); + var tungsten = new Chromaticity(0.44757, 0.40745); + using var result = Decode(SyntheticDng.Write(scene, new DngSpec { SceneWhite = tungsten, WriteWhiteXy = whiteXy })); + Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 38); + Assert.Less(SyntheticRaw.MeanDeltaE(scene, result.Image, 4), 1.5); + } + + [Test] + public void DualIlluminantMatricesInterpolateByTemperature() + { + var profile = new DngColorProfile + { + ColorMatrix1 = SyntheticRaw.TungstenMatrix, + CalibrationIlluminant1 = 17, + ColorMatrix2 = SyntheticRaw.CameraMatrix, + CalibrationIlluminant2 = 21, + }; + var warm = new Chromaticity(0.44757, 0.40745); + var mid = ColorTemperatureXy(4200); + Assert.Approximately(2850, DngColorProfile.Temperature(warm), 15); + Assert.Approximately(6504, DngColorProfile.Temperature(Chromaticity.D65), 30); + AssertMatrix(SyntheticRaw.TungstenMatrix, profile.XyzToCamera(warm), 1e-3); + AssertMatrix(SyntheticRaw.CameraMatrix, profile.XyzToCamera(Chromaticity.D65), 1e-9); + // Between the calibrations the weight is linear in inverse temperature. + var t = DngColorProfile.Temperature(mid); + var g = (1 / t - 1 / 6500.0) / (1 / 2850.0 - 1 / 6500.0); + Assert.Approximately(g * SyntheticRaw.TungstenMatrix.M11 + (1 - g) * SyntheticRaw.CameraMatrix.M11, profile.XyzToCamera(mid).M11, 1e-9); + + // A DNG with both matrices, shot at 4200 K, decodes back to the scene. + using var scene = SyntheticRaw.Smooth(W, H); + var spec = new DngSpec + { + ColorMatrix1 = SyntheticRaw.TungstenMatrix, + Illuminant1 = 17, + ColorMatrix2 = SyntheticRaw.CameraMatrix, + Illuminant2 = 21, + CaptureMatrix = profile.XyzToCamera(mid), + SceneWhite = mid, + }; + using var result = Decode(SyntheticDng.Write(scene, spec)); + Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 38); + } + + [Test] + public void ForwardMatrixMapsNeutralToWhite() + { + // ForwardMatrix = Rec.709 → XYZ(D50) with camera = linear Rec.709: a gray scene must stay gray. + var fm = Matrix3.BradfordAdaptation(Chromaticity.D65.ToXyz(), Chromaticity.D50.ToXyz()) * Matrix3.Rec709ToXyz; + using var scene = new ImageBuffer(32, 32); + scene.Data.Fill(0.4f); + var spec = new DngSpec { Cfa = null, ColorMatrix1 = Matrix3.XyzToRec709, ForwardMatrix1 = fm }; + using var result = Decode(SyntheticDng.Write(scene, spec)); + for (var c = 0; c < 3; c++) Assert.Approximately(0.4, result.Image[c, 16, 16], 2e-3); + } + + [Test] + public void BaselineExposureScalesOutput() + { + using var scene = SyntheticRaw.Smooth(W, H); + using var plain = Decode(SyntheticDng.Write(scene, new DngSpec { Cfa = null })); + using var bright = Decode(SyntheticDng.Write(scene, new DngSpec { Cfa = null, BaselineExposure = 1 })); + Assert.Approximately(2 * plain.Image[1, 40, 30], bright.Image[1, 40, 30], 1e-4); + } + + [Theory] + [Case((ushort)6)] + [Case((ushort)3)] + public void OrientationIsApplied(ushort orientation) + { + using var scene = SyntheticRaw.Smooth(W, H); + var dng = SyntheticDng.Write(scene, new DngSpec { Cfa = null, Orientation = orientation }); + using var oriented = Decode(dng); + using var stored = Decode(dng, new RawDecodeOptions { ApplyOrientation = false }); + using var expected = PixelConvert.ApplyOrientation(stored.Image, orientation); + Assert.Equal(expected.Width, oriented.Image.Width); + Assert.Equal(expected.Width, oriented.Info.Width); + Assert.SequenceEqual(expected.Data.ToArray(), oriented.Image.Data.ToArray()); + } + + [Theory] + [Case(DemosaicQuality.Best)] + [Case(DemosaicQuality.Draft)] + public void BinningHalvesTheSize(DemosaicQuality quality) + { + using var scene = SyntheticRaw.Smooth(W, H); + using var result = Decode(SyntheticDng.Write(scene, new DngSpec()), new RawDecodeOptions { Binning = true, Quality = quality }); + Assert.Equal(W / 2, result.Image.Width); + Assert.Equal(H / 2, result.Image.Height); + using var expected = Resampler.Resize(scene, W / 2, H / 2, ResampleFilter.Box); + Assert.GreaterOrEqual(SyntheticRaw.Psnr(expected, result.Image, 1), 34); + } + + [Test] + public void ProbeDescribesWithoutDecoding() + { + using var scene = SyntheticRaw.Smooth(W, H); + var dng = SyntheticDng.Write(scene, new DngSpec { Orientation = 6, Crop = new RawRect(2, 2, 66, 50) }); + var probe = RawDecoder.Probe(new MemoryStream(dng), "x.dng"); + Assert.True(probe.IsSupported); + Assert.Equal(48, probe.Info!.Width); + Assert.Equal(64, probe.Info.Height); + Assert.Equal("managed-dng", probe.Info.Decoder); + Assert.Equal("Synthetic", probe.Info.Model); + } + + internal static ImageBuffer Crop(ImageBuffer source, int x0, int y0, int w, int h) + { + var result = new ImageBuffer(w, h, source.Channels); + for (var c = 0; c < source.Channels; c++) + { + for (var y = 0; y < h; y++) source.Row(c, y0 + y).Slice(x0, w).CopyTo(result.Row(c, y)); + } + return result; + } + + private static Chromaticity ColorTemperatureXy(double kelvin) => Titano.Core.Analysis.ColorTemperature.PlanckianChromaticity(kelvin); + + private static void AssertMatrix(Matrix3 expected, Matrix3 actual, double tolerance) + { + double[] e = [expected.M11, expected.M12, expected.M13, expected.M21, expected.M22, expected.M23, expected.M31, expected.M32, expected.M33]; + double[] a = [actual.M11, actual.M12, actual.M13, actual.M21, actual.M22, actual.M23, actual.M31, actual.M32, actual.M33]; + for (var i = 0; i < 9; i++) Assert.Approximately(e[i], a[i], tolerance); + } +} diff --git a/tests/Titano.Core.Tests/Raw/DngOpcodeTests.cs b/tests/Titano.Core.Tests/Raw/DngOpcodeTests.cs new file mode 100644 index 00000000..07d242c2 --- /dev/null +++ b/tests/Titano.Core.Tests/Raw/DngOpcodeTests.cs @@ -0,0 +1,221 @@ +using Titano.Core.Codecs.Raw; +using Titano.Core.Imaging; +using Titano.Testing; + +namespace Titano.Core.Tests.Raw; + +/// DNG opcode lists: raw data is distorted by the inverse of an opcode, the decoder must undo it. +public sealed class DngOpcodeTests(TestContext context) +{ + private const int W = 96, H = 72; + + private static RawDecodeResult Decode(byte[] dng) => RawDecoder.Decode(new MemoryStream(dng), "op.dng"); + + /// Bilinear grid interpolation as DNG §7 GainMap defines it (independent of the decoder's implementation). + private static double GainAt(double[,] grid, double spacingV, double spacingH, int x, int y) + { + int rows = grid.GetLength(0), cols = grid.GetLength(1); + double fr = double.Clamp(y / (double)H / spacingV, 0, rows - 1), fc = double.Clamp(x / (double)W / spacingH, 0, cols - 1); + int r0 = (int)fr, c0 = (int)fc, r1 = int.Min(r0 + 1, rows - 1), c1 = int.Min(c0 + 1, cols - 1); + double a = fr - r0, b = fc - c0; + return (grid[r0, c0] * (1 - b) + grid[r0, c1] * b) * (1 - a) + (grid[r1, c0] * (1 - b) + grid[r1, c1] * b) * a; + } + + [Test] + public void GainMapRestoresShading() + { + double[,] grid = { { 1.6, 1.3, 1.6 }, { 1.2, 1.0, 1.25 }, { 1.7, 1.35, 1.5 } }; + var ops = new OpcodeListBuilder().Add(9, 0, p => + { + OpcodeListBuilder.Area(p, 0, 0, H, W); + p.U32(3).U32(3).F64(0.5).F64(0.5).F64(0).F64(0).U32(1); + for (var r = 0; r < 3; r++) + { + for (var c = 0; c < 3; c++) p.F32((float)grid[r, c]); + } + }).Build(); + using var scene = SyntheticRaw.Smooth(W, H); + var spec = new DngSpec { OpcodeList2 = ops, RawTransform = (x, y, v) => (float)(v / GainAt(grid, 0.5, 0.5, x, y)) }; + using var result = Decode(SyntheticDng.Write(scene, spec)); + Assert.Empty(result.Warnings); + Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 38); + } + + [Test] + public void GainMapWithPitchTargetsOneCfaColour() + { + // Red sites of RGGB (even rows and columns) were attenuated by 1/1.5. + var ops = new OpcodeListBuilder().Add(9, 0, p => + { + OpcodeListBuilder.Area(p, 0, 0, H, W, rowPitch: 2, colPitch: 2); + p.U32(1).U32(1).F64(1).F64(1).F64(0).F64(0).U32(1).F32(1.5f); + }).Build(); + using var scene = SyntheticRaw.Smooth(W, H); + var spec = new DngSpec { OpcodeList2 = ops, RawTransform = (x, y, v) => x % 2 == 0 && y % 2 == 0 ? v / 1.5f : v }; + using var result = Decode(SyntheticDng.Write(scene, spec)); + Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 38); + } + + [Test] + public void FixVignetteRadialRestoresFalloff() + { + double k0 = 0.4, k1 = 0.2; + var ops = new OpcodeListBuilder().Add(3, 0, p => p.F64(k0).F64(k1).F64(0).F64(0).F64(0).F64(0.5).F64(0.5)).Build(); + var m = double.Hypot(W / 2.0, H / 2.0); + using var scene = SyntheticRaw.Smooth(W, H); + var spec = new DngSpec + { + OpcodeList2 = ops, + RawTransform = (x, y, v) => + { + var r2 = ((x - W / 2.0) * (x - W / 2.0) + (y - H / 2.0) * (y - H / 2.0)) / (m * m); + return (float)(v / (1 + k0 * r2 + k1 * r2 * r2)); + }, + }; + using var result = Decode(SyntheticDng.Write(scene, spec)); + Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 38); + } + + [Theory] + [Case(false)] + [Case(true)] + public void WarpRectilinearMovesPointsToTheirCorrectedPositions(bool mosaic) + { + // Corrected position p maps to source position f(p) (radial + tangential model, DNG §7 WarpRectilinear). + const int w = 160, h = 120; + double[] k = [1.0, 0.06, -0.01, 0.0, 0.002, -0.0015]; + double cx = 0.48, cy = 0.53; + var ops = new OpcodeListBuilder().Add(1, 0, p => + { + p.U32(1); + foreach (var v in k) p.F64(v); + p.F64(cx).F64(cy); + }).Build(); + double ox = cx * w, oy = cy * h; + var m = new[] { (0.0, 0.0), (w, 0.0), (0.0, h), (w, h) }.Max(c => double.Hypot(c.Item1 - ox, c.Item2 - oy)); + (double X, double Y) Source(double x, double y) + { + double dx = (x - ox) / m, dy = (y - oy) / m, r2 = dx * dx + dy * dy; + var f = k[0] + r2 * (k[1] + r2 * (k[2] + r2 * k[3])); + return (ox + m * (f * dx + k[4] * 2 * dx * dy + k[5] * (r2 + 2 * dx * dx)), oy + m * (f * dy + k[5] * 2 * dx * dy + k[4] * (r2 + 2 * dy * dy))); + } + (double X, double Y)[] targets = [(20, 18), (130, 25), (80, 60), (30, 100), (140, 104), (60, 40)]; + using var distorted = new ImageBuffer(w, h); + foreach (var (tx, ty) in targets) + { + var (sx, sy) = Source(tx, ty); + for (var y = 0; y < h; y++) + { + for (var x = 0; x < w; x++) + { + var g = (float)(0.6 * double.Exp(-((x - sx) * (x - sx) + (y - sy) * (y - sy)) / (2 * 2.0 * 2.0))); + for (var c = 0; c < 3; c++) distorted[c, x, y] += g; + } + } + } + for (var i = 0; i < distorted.Data.Length; i++) distorted.Data[i] += 0.02f; + var spec = new DngSpec { Cfa = mosaic ? CfaPattern.Bayer("RGGB") : null, OpcodeList3 = ops }; + using var result = Decode(SyntheticDng.Write(distorted, spec)); + Assert.Empty(result.Warnings); + foreach (var (tx, ty) in targets) + { + var (x, y) = Centroid(result.Image, (int)tx, (int)ty, 6); + context.Log($"target ({tx}, {ty}) → ({x:F3}, {y:F3})"); + Assert.Within(tx, ty, x, y, mosaic ? 0.2 : 0.1); + } + } + + [Test] + public void TrimBoundsAndDefaultCropCompose() + { + var ops = new OpcodeListBuilder().Add(6, 0, p => p.U32(4).U32(10).U32(60).U32(90)).Build(); + using var scene = SyntheticRaw.Smooth(W, H); + using var result = Decode(SyntheticDng.Write(scene, new DngSpec { OpcodeList3 = ops, Crop = new RawRect(0, 0, 80, 64) })); + Assert.Equal(70, result.Image.Width); + Assert.Equal(56, result.Image.Height); + using var expected = DngDecoderTests.Crop(scene, 10, 4, 70, 56); + Assert.GreaterOrEqual(SyntheticRaw.Psnr(expected, result.Image, 2), 38); + } + + [Test] + public void FixBadPixelsConstantAndListRepairMosaic() + { + (int X, int Y)[] dead = [(10, 10), (41, 20), (70, 33)]; + (int X, int Y)[] hot = [(30, 50), (55, 12)]; + var list1 = new OpcodeListBuilder().Add(4, 0, p => p.U32(0).U32(0)).Build(); + var list2 = new OpcodeListBuilder().Add(5, 0, p => + { + p.U32(0).U32((uint)hot.Length).U32(1); + foreach (var (x, y) in hot) p.U32((uint)y).U32((uint)x); + p.U32(60).U32(80).U32(62).U32(83); + }).Build(); + using var scene = SyntheticRaw.Smooth(W, H); + var spec = new DngSpec + { + OpcodeList1 = list1, + OpcodeList2 = list2, + RawTransform = (x, y, v) => dead.Contains((x, y)) ? -1f : hot.Contains((x, y)) || (y is >= 60 and < 62 && x is >= 80 and < 83) ? 1f : v, + }; + using var result = Decode(SyntheticDng.Write(scene, spec)); + Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 38); + } + + [Test] + public void MapTableAndMapPolynomialInvertEncodings() + { + // Raw stored as v² is mapped back by a square-root table; then halved values are doubled by a polynomial. + var table = Enumerable.Range(0, 65536).Select(i => (ushort)double.Round(double.Sqrt(i / 65535.0) * 65535)).ToArray(); + var ops = new OpcodeListBuilder() + .Add(7, 0, p => + { + OpcodeListBuilder.Area(p, 0, 0, H, W); + p.U32((uint)table.Length); + foreach (var t in table) p.U16(t); + }) + .Add(8, 0, p => + { + OpcodeListBuilder.Area(p, 0, 0, H, W); + p.U32(1).F64(0).F64(2); + }) + .Build(); + using var scene = SyntheticRaw.Smooth(W, H); + var spec = new DngSpec { Black = [0], OpcodeList2 = ops, RawTransform = (_, _, v) => float.Max(0, v / 2) * float.Max(0, v / 2) }; + using var result = Decode(SyntheticDng.Write(scene, spec)); + var psnr = SyntheticRaw.Psnr(scene, result.Image, 4); + context.Log($"MapTable+MapPolynomial PSNR {psnr:F1} dB"); + Assert.GreaterOrEqual(psnr, 36); + } + + [Test] + public void UnknownAndMalformedOpcodesWarnAndContinue() + { + var ops = new OpcodeListBuilder() + .Add(99, 1, p => p.U32(1)) + .Add(98, 0, p => p.U32(1)) + .Add(9, 0, p => p.U32(1)) + .Build(); + using var scene = SyntheticRaw.Smooth(W, H); + using var result = Decode(SyntheticDng.Write(scene, new DngSpec { OpcodeList3 = ops })); + Assert.Contains(result.Warnings, w => w.Contains("optional opcode 99")); + Assert.Contains(result.Warnings, w => w.Contains("required opcode 98")); + Assert.Contains(result.Warnings, w => w.Contains("GainMap") && w.Contains("malformed")); + Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 40); + } + + /// Intensity-weighted centroid above the background in a window around (x, y). + private static (double X, double Y) Centroid(ImageBuffer image, int cx, int cy, int radius) + { + double sum = 0, sx = 0, sy = 0; + for (var y = cy - radius; y <= cy + radius; y++) + { + for (var x = cx - radius; x <= cx + radius; x++) + { + var v = double.Max(0, image[1, x, y] - 0.05); + sum += v; + sx += v * x; + sy += v * y; + } + } + return (sx / sum, sy / sum); + } +} diff --git a/tests/Titano.Core.Tests/Raw/HighlightTests.cs b/tests/Titano.Core.Tests/Raw/HighlightTests.cs new file mode 100644 index 00000000..eb142ba6 --- /dev/null +++ b/tests/Titano.Core.Tests/Raw/HighlightTests.cs @@ -0,0 +1,75 @@ +using Titano.Core.Codecs.Raw; +using Titano.Core.Imaging; +using Titano.Testing; + +namespace Titano.Core.Tests.Raw; + +/// Clip-aware highlight reconstruction on decoded DNGs. +public sealed class HighlightTests(TestContext context) +{ + private const int W = 96, H = 64; + + /// Scene with three gray patches: unclipped (0.4), green clipped only (1.3) and fully clipped (3.0). + private static ImageBuffer Patches() + { + var scene = new ImageBuffer(W, H); + for (var y = 0; y < H; y++) + { + for (var x = 0; x < W; x++) + { + var v = x < 32 ? 0.4f : x < 64 ? 1.3f : 3.0f; + for (var c = 0; c < 3; c++) scene[c, x, y] = v; + } + } + return scene; + } + + [Theory] + [Case(DemosaicQuality.Best, false)] + [Case(DemosaicQuality.Draft, true)] + public void ClippedGrayStaysNeutral(DemosaicQuality quality, bool binning) + { + using var scene = Patches(); + var dng = SyntheticDng.Write(scene, new DngSpec()); + using var result = RawDecoder.Decode(new MemoryStream(dng), "h.dng", new RawDecodeOptions { Quality = quality, Binning = binning }); + var s = binning ? 2 : 1; + var (r0, g0, b0) = Pixel(result.Image, 16 / s, 32 / s); + var (r1, g1, b1) = Pixel(result.Image, 48 / s, 32 / s); + var (r2, g2, b2) = Pixel(result.Image, 80 / s, 32 / s); + context.Log($"0.4 → ({r0:F3}, {g0:F3}, {b0:F3}); 1.3 → ({r1:F3}, {g1:F3}, {b1:F3}); 3.0 → ({r2:F3}, {g2:F3}, {b2:F3})"); + + // Unclipped: exact. + Assert.Approximately(0.4, g0, 0.01); + Assert.Approximately(r0, b0, 0.01); + + // Only green clipped: rebuilt from red and blue, neutral and at its true level (no magenta cast). + Assert.Approximately(1.3, g1, 0.03); + Assert.Approximately(1.3, r1, 0.03); + Assert.Approximately(1.3, b1, 0.03); + + // Everything clipped: neutral, at least as bright as the partially clipped patch. + Assert.Approximately(r2, g2, 1e-3); + Assert.Approximately(b2, g2, 1e-3); + Assert.GreaterOrEqual(g2, g1); + } + + [Test] + public void SaturatedColourKeepsItsHue() + { + // A red highlight clips the red channel while green and blue stay low: it must stay red, not turn white. + using var scene = new ImageBuffer(W, H); + for (var i = 0; i < scene.PlaneLength; i++) + { + scene.Plane(0)[i] = 5f; + scene.Plane(1)[i] = 0.15f; + scene.Plane(2)[i] = 0.1f; + } + using var result = RawDecoder.Decode(new MemoryStream(SyntheticDng.Write(scene, new DngSpec())), "red.dng"); + var (r, g, b) = Pixel(result.Image, W / 2, H / 2); + context.Log($"red highlight → ({r:F3}, {g:F3}, {b:F3})"); + Assert.Greater(r, 4 * g); + Assert.Greater(r, 4 * b); + } + + private static (float R, float G, float B) Pixel(ImageBuffer image, int x, int y) => (image[0, x, y], image[1, x, y], image[2, x, y]); +} diff --git a/tests/Titano.Core.Tests/Raw/RawPerformanceTests.cs b/tests/Titano.Core.Tests/Raw/RawPerformanceTests.cs new file mode 100644 index 00000000..0d2048ac --- /dev/null +++ b/tests/Titano.Core.Tests/Raw/RawPerformanceTests.cs @@ -0,0 +1,94 @@ +using System.Buffers.Binary; +using System.Diagnostics; +using System.Reflection; +using Titano.Core.Codecs.Raw; +using Titano.Core.Codecs.Tiff; +using Titano.Core.Metadata.Tiff; +using Titano.Testing; + +namespace Titano.Core.Tests.Raw; + +/// 24-megapixel Bayer DNG development timing: AHD at full size and binned Draft at half size, all cores. +[Serial] +[Category("Performance")] +public sealed class RawPerformanceTests(TestContext context) +{ + private const int W = 6000, H = 4000; + + [Test] + [Timeout(60_000)] + public void Develops24MegapixelDngWithinBudget() + { + // A Debug build disables the JIT optimiser in Titano.Core: timings would measure the build, not the pipeline. + if (typeof(RawDecoder).Assembly.GetCustomAttribute()?.IsJITOptimizerDisabled == true) + TestContext.Skip("timing gate needs an optimised build (dotnet run -c Release)"); + var dng = Mosaic(); + var ahd = Best(dng, RawDecodeOptions.Default, out var size); + Assert.Equal(W, size.Width); + var half = Best(dng, new RawDecodeOptions { Quality = DemosaicQuality.Draft, Binning = true }, out var halfSize); + Assert.Equal(W / 2, halfSize.Width); + context.Log($"24 MP DNG: AHD full size {ahd.TotalMilliseconds:F0} ms, binned half size {half.TotalMilliseconds:F0} ms ({Environment.ProcessorCount} cores)"); + Console.WriteLine($"[perf] {context.LogText.Trim()}"); + Assert.Less(ahd.TotalSeconds, 4.0); + Assert.Less(half.TotalSeconds, 1.0); + } + + /// Best of two runs (the machine is shared; the first run also pays JIT warm-up). + private static TimeSpan Best(byte[] dng, RawDecodeOptions options, out (int Width, int Height) size) + { + var best = TimeSpan.MaxValue; + size = default; + for (var run = 0; run < 2; run++) + { + var sw = Stopwatch.StartNew(); + using var result = RawDecoder.Decode(new MemoryStream(dng, writable: false), "perf.dng", options); + best = sw.Elapsed < best ? sw.Elapsed : best; + size = (result.Image.Width, result.Image.Height); + } + return best; + } + + /// Uncompressed 14-bit RGGB DNG with photo-like texture (smooth illumination plus detail and noise). + private static byte[] Mosaic() + { + var data = new byte[W * H * 2]; + Parallel.For(0, H, y => + { + var state = (uint)(y * 2654435761u + 7); + for (var x = 0; x < W; x++) + { + state ^= state << 13; + state ^= state >> 17; + state ^= state << 5; + var colour = (y & 1) * 2 + (x & 1); + var level = 1500 + 5000 * (0.5 + 0.5 * double.Sin(x * 0.01) * double.Cos(y * 0.013)) * (colour is 1 or 2 ? 1.0 : 0.6) + + 800 * double.Sin(x * 0.4 + y * 0.3) + (state & 127); + BinaryPrimitives.WriteUInt16LittleEndian(data.AsSpan((y * W + x) * 2), (ushort)double.Clamp(level, 0, 16383)); + } + }); + using var output = new MemoryStream(); + output.Write("II*\0\0\0\0\0"u8); + var ifd = new TiffDirectory(); + ifd.Long(TiffTags.NewSubfileType, 0); + ifd.Long(TiffTags.ImageWidth, W); + ifd.Long(TiffTags.ImageLength, H); + ifd.Short(TiffTags.BitsPerSample, 16); + ifd.Short(TiffTags.Compression, 1); + ifd.Short(TiffTags.PhotometricInterpretation, 32803); + ifd.Short(TiffTags.SamplesPerPixel, 1); + ifd.Long(TiffTags.StripOffsets, 8); + ifd.Long(TiffTags.StripByteCounts, (uint)data.Length); + ifd.Long(TiffTags.RowsPerStrip, H); + ifd.Short(TiffTags.CfaRepeatPatternDim, 2, 2); + ifd.Bytes(TiffTags.CfaPattern, 0, 1, 1, 2); + ifd.Bytes(TiffTags.DngVersion, 1, 4, 0, 0); + ifd.Short(TiffTags.BlackLevel, 1024); + ifd.Short(TiffTags.WhiteLevel, 16383); + ifd.SRational(TiffTags.ColorMatrix1, (6722, 10000), (-635, 10000), (-963, 10000), (-4287, 10000), (12460, 10000), (2028, 10000), (-908, 10000), (2162, 10000), (5668, 10000)); + ifd.Short(TiffTags.CalibrationIlluminant1, 21); + ifd.Rational(TiffTags.AsShotNeutral, (500u, 1000u), (1000u, 1000u), (700u, 1000u)); + output.Write(data); + TiffWriter.WriteDirectory(output, 0, ifd); + return output.ToArray(); + } +} diff --git a/tests/Titano.Core.Tests/Raw/SyntheticRaw.cs b/tests/Titano.Core.Tests/Raw/SyntheticRaw.cs new file mode 100644 index 00000000..0a47f74f --- /dev/null +++ b/tests/Titano.Core.Tests/Raw/SyntheticRaw.cs @@ -0,0 +1,454 @@ +using System.Buffers.Binary; +using System.IO.Compression; +using Titano.Core.Codecs.Dng; +using Titano.Core.Codecs.Jpeg; +using Titano.Core.Codecs.Raw; +using Titano.Core.Codecs.Tiff; +using Titano.Core.Imaging; +using Titano.Core.Mathematics; +using Titano.Core.Metadata.Tiff; + +namespace Titano.Core.Tests.Raw; + +/// Synthetic scenes and the camera model used to turn them into raw data with known answers. +internal static class SyntheticRaw +{ + /// Canon EOS 5D Mark III ColorMatrix (XYZ → camera, D65), the adobe_coeff values divided by 10 000. + public static Matrix3 CameraMatrix { get; } = new(0.6722, -0.0635, -0.0963, -0.4287, 1.2460, 0.2028, -0.0908, 0.2162, 0.5668); + + /// A second (tungsten) matrix for interpolation tests: the D65 one with a warmer bias. + public static Matrix3 TungstenMatrix { get; } = new(0.7300, -0.1100, -0.0800, -0.4000, 1.2000, 0.2400, -0.0500, 0.1500, 0.7400); + + /// Smooth colourful linear Rec.709 scene with values in roughly 0.03–0.7. + public static ImageBuffer Smooth(int width, int height) + { + var image = new ImageBuffer(width, height); + for (var y = 0; y < height; y++) + { + for (var x = 0; x < width; x++) + { + double u = x / (double)width, v = y / (double)height; + image[0, x, y] = (float)(0.25 + 0.2 * double.Sin(2 * double.Pi * (u * 1.3 + 0.1)) * double.Cos(2 * double.Pi * v * 0.7)); + image[1, x, y] = (float)(0.3 + 0.2 * double.Cos(2 * double.Pi * (u * 0.8 - v * 0.9))); + image[2, x, y] = (float)(0.2 + 0.15 * double.Sin(2 * double.Pi * (v * 1.1 + u * 0.4 + 0.3))); + } + } + return image; + } + + /// Gray scene with a sharp vertical edge (dark left, bright right) and a sharp horizontal one. + public static ImageBuffer Edges(int width, int height) + { + var image = new ImageBuffer(width, height); + for (var y = 0; y < height; y++) + { + for (var x = 0; x < width; x++) + { + var v = (x >= width / 2 ? 0.6f : 0.08f) * (y >= height / 2 ? 0.5f : 1f); + for (var c = 0; c < 3; c++) image[c, x, y] = v; + } + } + return image; + } + + /// Camera response of a scene lit by : Bradford(D65 → white) · Rec.709 → XYZ, then + /// XYZ → camera, scaled so the white's camera neutral peaks at 1. Returns per-pixel camera RGB and that neutral. + public static (float[][] Camera, Vector3D Neutral) ToCamera(ImageBuffer scene, Matrix3 xyzToCamera, Chromaticity white) + { + var whiteXyz = white.ToXyz(); + var neutral = xyzToCamera * whiteXyz; + var s = double.Max(neutral.X, double.Max(neutral.Y, neutral.Z)); + var m = xyzToCamera * Matrix3.BradfordAdaptation(Chromaticity.D65.ToXyz(), whiteXyz) * Matrix3.Rec709ToXyz; + var camera = new float[3][]; + for (var c = 0; c < 3; c++) camera[c] = new float[scene.PlaneLength]; + for (var i = 0; i < scene.PlaneLength; i++) + { + var v = m * new Vector3D(scene.Plane(0)[i], scene.Plane(1)[i], scene.Plane(2)[i]); + camera[0][i] = (float)(v.X / s); + camera[1][i] = (float)(v.Y / s); + camera[2][i] = (float)(v.Z / s); + } + return (camera, new Vector3D(neutral.X / s, neutral.Y / s, neutral.Z / s)); + } + + /// PSNR in dB between two RGB buffers on sRGB-encoded values, ignoring pixels. + public static double Psnr(ImageBuffer expected, ImageBuffer actual, int border) + { + double sum = 0; + long n = 0; + for (var c = 0; c < 3; c++) + { + for (var y = border; y < expected.Height - border; y++) + { + for (var x = border; x < expected.Width - border; x++) + { + var d = ColorSpace.EncodeFast(expected[c, x, y]) - ColorSpace.EncodeFast(actual[c, x, y]); + sum += d * d; + n++; + } + } + } + return 10 * double.Log10(1 / double.Max(sum / n, 1e-12)); + } + + /// Mean CIE76 ΔE between two linear Rec.709 buffers (D65 white), ignoring pixels. + public static double MeanDeltaE(ImageBuffer expected, ImageBuffer actual, int border) + { + double sum = 0; + long n = 0; + for (var y = border; y < expected.Height - border; y++) + { + for (var x = border; x < expected.Width - border; x++) + { + var a = Lab(expected, x, y); + var b = Lab(actual, x, y); + sum += double.Sqrt((a.L - b.L) * (a.L - b.L) + (a.A - b.A) * (a.A - b.A) + (a.B - b.B) * (a.B - b.B)); + n++; + } + } + return sum / n; + } + + private static (double L, double A, double B) Lab(ImageBuffer image, int x, int y) + { + var xyz = Matrix3.Rec709ToXyz * new Vector3D(image[0, x, y], image[1, x, y], image[2, x, y]); + var w = Chromaticity.D65.ToXyz(); + static double F(double t) => t > 216.0 / 24389 ? double.Cbrt(t) : (24389.0 / 27 * t + 16) / 116; + double fx = F(xyz.X / w.X), fy = F(xyz.Y / w.Y), fz = F(xyz.Z / w.Z); + return (116 * fy - 16, 500 * (fx - fy), 200 * (fy - fz)); + } +} + +/// Description of a synthetic DNG written by . +internal sealed record DngSpec +{ + /// CFA pattern anchored at the active area; null writes a 3-sample LinearRaw image. + public CfaPattern? Cfa { get; init; } = CfaPattern.Bayer("RGGB"); + + /// Masked border around the scene (active area offset); the crop is relative to the active area. + public int MarginLeft { get; init; } + + public int MarginTop { get; init; } + public int MarginRight { get; init; } + public int MarginBottom { get; init; } + + /// DefaultCrop relative to the active area (null: none). + public RawRect? Crop { get; init; } + + public int White { get; init; } = 16383; + + /// Black pattern (rows × columns) or a single value. + public int BlackRows { get; init; } = 1; + + public int BlackColumns { get; init; } = 1; + public double[] Black { get; init; } = [512]; + public double[]? BlackDeltaH { get; init; } + public double[]? BlackDeltaV { get; init; } + + /// Forward linearisation table (stored code → linear value). + public ushort[]? Linearization { get; init; } + + /// 1 (uncompressed), 7 (lossless JPEG) or 8 (Deflate with horizontal predictor). + public int Compression { get; init; } = 7; + + /// Lossless JPEG tiles encode CFA rows as two interleaved components at half width (as the DNG SDK does). + public bool TwoComponentTiles { get; init; } = true; + + public int TileSize { get; init; } = 64; + + public Matrix3 ColorMatrix1 { get; init; } = SyntheticRaw.CameraMatrix; + + /// XYZ → camera used to synthesise the raw values (default: ). + public Matrix3? CaptureMatrix { get; init; } + + public int Illuminant1 { get; init; } = 21; + public Matrix3? ColorMatrix2 { get; init; } + public int Illuminant2 { get; init; } + public Matrix3? ForwardMatrix1 { get; init; } + + /// Scene illuminant; the file records it as AsShotNeutral or, with , AsShotWhiteXY. + public Chromaticity SceneWhite { get; init; } = Chromaticity.D65; + + public bool WriteWhiteXy { get; init; } + public double BaselineExposure { get; init; } + public byte[]? OpcodeList1 { get; init; } + public byte[]? OpcodeList2 { get; init; } + public byte[]? OpcodeList3 { get; init; } + public ushort Orientation { get; init; } = 1; + + /// Put the raw image in a SubIFD behind an IFD0 thumbnail, like camera DNGs. + public bool RawInSubIfd { get; init; } + + /// Raw-value transform applied after mosaicking (e.g. the inverse of a gain map), stored-unit domain. + public Func? RawTransform { get; init; } +} + +/// Writes synthetic DNGs from a linear scene with a known camera model (test-side writer). +internal static class SyntheticDng +{ + public static byte[] Write(ImageBuffer scene, DngSpec spec) + { + var (camera, neutral) = SyntheticRaw.ToCamera(scene, spec.CaptureMatrix ?? spec.ColorMatrix1, spec.SceneWhite); + int sw = scene.Width, sh = scene.Height; + int w = sw + spec.MarginLeft + spec.MarginRight, h = sh + spec.MarginTop + spec.MarginBottom; + var spp = spec.Cfa is null ? 3 : 1; + var samples = new ushort[w * h * spp]; + for (var y = 0; y < h; y++) + { + for (var x = 0; x < w; x++) + { + int ax = x - spec.MarginLeft, ay = y - spec.MarginTop; + var inside = ax >= 0 && ay >= 0 && ax < sw && ay < sh; + for (var s = 0; s < spp; s++) + { + var black = BlackAt(spec, ax, ay, s, spp); + double linear = black; + if (inside) + { + var c = spec.Cfa?.ColorAt(ax, ay) ?? s; + var raw = camera[c][ay * sw + ax]; + if (spec.RawTransform is { } t) raw = t(ax, ay, raw); + linear = black + raw * (spec.White - black); + } + linear = double.Clamp(linear, 0, spec.White); + samples[(y * w + x) * spp + s] = spec.Linearization is { } table ? Inverse(table, linear) : (ushort)double.Round(linear); + } + } + } + return Assemble(spec, samples, w, h, spp, neutral); + } + + /// Black level of the spec at active-area coordinates (outside the area: the pattern without deltas). + public static double BlackAt(DngSpec spec, int ax, int ay, int s, int spp) + { + var row = ((ay % spec.BlackRows) + spec.BlackRows) % spec.BlackRows; + var col = ((ax % spec.BlackColumns) + spec.BlackColumns) % spec.BlackColumns; + var b = spec.Black.Length == 1 ? spec.Black[0] : spec.Black[(row * spec.BlackColumns + col) * spp + s]; + if (spec.BlackDeltaH is { } dh && ax >= 0 && ax < dh.Length) b += dh[ax]; + if (spec.BlackDeltaV is { } dv && ay >= 0 && ay < dv.Length) b += dv[ay]; + return b; + } + + private static ushort Inverse(ushort[] table, double value) + { + var best = 0; + for (var i = 1; i < table.Length; i++) + { + if (double.Abs(table[i] - value) < double.Abs(table[best] - value)) best = i; + } + return (ushort)best; + } + + private static byte[] Assemble(DngSpec spec, ushort[] samples, int w, int h, int spp, Vector3D neutral) + { + using var output = new MemoryStream(); + output.Write("II*\0\0\0\0\0"u8); + var raw = new TiffDirectory(); + raw.Long(TiffTags.NewSubfileType, 0); + raw.Long(TiffTags.ImageWidth, (uint)w); + raw.Long(TiffTags.ImageLength, (uint)h); + raw.Short(TiffTags.BitsPerSample, [.. Enumerable.Repeat((ushort)16, spp)]); + raw.Short(TiffTags.Compression, (ushort)spec.Compression); + raw.Short(TiffTags.PhotometricInterpretation, (ushort)(spec.Cfa is null ? 34892 : 32803)); + raw.Short(TiffTags.SamplesPerPixel, (ushort)spp); + raw.Short(TiffTags.PlanarConfiguration, 1); + if (spec.Compression == 8) raw.Short(TiffTags.Predictor, 2); + WriteData(spec, raw, output, samples, w, h, spp); + if (spec.Cfa is { } cfa) + { + raw.Short(TiffTags.CfaRepeatPatternDim, (ushort)cfa.Height, (ushort)cfa.Width); + var pattern = new byte[cfa.Width * cfa.Height]; + for (var i = 0; i < pattern.Length; i++) pattern[i] = (byte)cfa.ColorAt(i % cfa.Width, i / cfa.Width); + raw.Bytes(TiffTags.CfaPattern, pattern); + raw.Bytes(TiffTags.CfaPlaneColor, 0, 1, 2); + raw.Short(TiffTags.CfaLayout, 1); + } + if (spec.MarginLeft + spec.MarginTop + spec.MarginRight + spec.MarginBottom > 0) + raw.Long(TiffTags.ActiveArea, (uint)spec.MarginTop, (uint)spec.MarginLeft, (uint)(h - spec.MarginBottom), (uint)(w - spec.MarginRight)); + if (spec.Crop is { } crop) + { + raw.Long(TiffTags.DefaultCropOrigin, (uint)crop.Left, (uint)crop.Top); + raw.Long(TiffTags.DefaultCropSize, (uint)crop.Width, (uint)crop.Height); + } + if (spec.Linearization is { } table) raw.Short(TiffTags.LinearizationTable, table); + if (spec.BlackRows * spec.BlackColumns > 1) raw.Short(TiffTags.BlackLevelRepeatDim, (ushort)spec.BlackRows, (ushort)spec.BlackColumns); + raw.Rational(TiffTags.BlackLevel, [.. spec.Black.Select(b => ((uint)double.Round(b * 100), 100u))]); + if (spec.BlackDeltaH is { } dh) raw.SRational(TiffTags.BlackLevelDeltaH, [.. dh.Select(d => ((int)double.Round(d * 100), 100))]); + if (spec.BlackDeltaV is { } dv) raw.SRational(TiffTags.BlackLevelDeltaV, [.. dv.Select(d => ((int)double.Round(d * 100), 100))]); + raw.Long(TiffTags.WhiteLevel, [.. Enumerable.Repeat((uint)spec.White, spp)]); + if (spec.OpcodeList1 is { } o1) raw.Add(TiffTags.OpcodeList1, TiffType.Undefined, o1.Length, o1); + if (spec.OpcodeList2 is { } o2) raw.Add(TiffTags.OpcodeList2, TiffType.Undefined, o2.Length, o2); + if (spec.OpcodeList3 is { } o3) raw.Add(TiffTags.OpcodeList3, TiffType.Undefined, o3.Length, o3); + + var ifd0 = raw; + if (spec.RawInSubIfd) + { + ifd0 = new TiffDirectory(); + ifd0.Long(TiffTags.NewSubfileType, 1); + ifd0.Long(TiffTags.ImageWidth, 4); + ifd0.Long(TiffTags.ImageLength, 4); + ifd0.Short(TiffTags.BitsPerSample, 8, 8, 8); + ifd0.Short(TiffTags.Compression, 1); + ifd0.Short(TiffTags.PhotometricInterpretation, 2); + ifd0.Short(TiffTags.SamplesPerPixel, 3); + ifd0.Long(TiffTags.StripOffsets, 8); + ifd0.Long(TiffTags.StripByteCounts, 48); + ifd0.Child(TiffTags.SubIfds, raw); + } + ifd0.Ascii(TiffTags.Make, "Titano"); + ifd0.Ascii(TiffTags.Model, "Synthetic"); + ifd0.Short(TiffTags.Orientation, spec.Orientation); + ifd0.Bytes(TiffTags.DngVersion, 1, 6, 0, 0); + ifd0.Bytes(TiffTags.DngBackwardVersion, 1, 1, 0, 0); + ifd0.Ascii(TiffTags.UniqueCameraModel, "Titano Synthetic"); + ifd0.SRational(TiffTags.ColorMatrix1, Rationals(spec.ColorMatrix1)); + ifd0.Short(TiffTags.CalibrationIlluminant1, (ushort)spec.Illuminant1); + if (spec.ColorMatrix2 is { } cm2) + { + ifd0.SRational(TiffTags.ColorMatrix2, Rationals(cm2)); + ifd0.Short(TiffTags.CalibrationIlluminant2, (ushort)spec.Illuminant2); + } + if (spec.ForwardMatrix1 is { } fm) ifd0.SRational(TiffTags.ForwardMatrix1, Rationals(fm)); + if (spec.WriteWhiteXy) + { + ifd0.Rational(TiffTags.AsShotWhiteXy, ((uint)double.Round(spec.SceneWhite.X * 1e6), 1_000_000u), ((uint)double.Round(spec.SceneWhite.Y * 1e6), 1_000_000u)); + } + else + { + ifd0.Rational(TiffTags.AsShotNeutral, R(neutral.X), R(neutral.Y), R(neutral.Z)); + } + ifd0.SRational(TiffTags.BaselineExposure, ((int)double.Round(spec.BaselineExposure * 1000), 1000)); + TiffWriter.WriteDirectory(output, 0, ifd0); + return output.ToArray(); + } + + private static (uint, uint) R(double v) => ((uint)double.Round(v * 1_000_000), 1_000_000u); + + private static (int, int)[] Rationals(Matrix3 m) => + [.. new[] { m.M11, m.M12, m.M13, m.M21, m.M22, m.M23, m.M31, m.M32, m.M33 }.Select(v => ((int)double.Round(v * 1_000_000), 1_000_000))]; + + private static void WriteData(DngSpec spec, TiffDirectory raw, Stream output, ushort[] samples, int w, int h, int spp) + { + if (spec.Compression == 7) + { + var t = spec.TileSize; + int across = (w + t - 1) / t, down = (h + t - 1) / t; + var offsets = new uint[across * down]; + var counts = new uint[across * down]; + for (var ty = 0; ty < down; ty++) + { + for (var tx = 0; tx < across; tx++) + { + var tile = new ushort[t * t * spp]; + for (var y = 0; y < t; y++) + { + for (var x = 0; x < t; x++) + { + int sx = int.Min(tx * t + x, w - 1), sy = int.Min(ty * t + y, h - 1); + for (var s = 0; s < spp; s++) tile[(y * t + x) * spp + s] = samples[(sy * w + sx) * spp + s]; + } + } + var twoComponents = spec.TwoComponentTiles && spp == 1; + var jpeg = twoComponents ? JpegLosslessEncoder.Encode(tile, t / 2, t, 2, 16) : JpegLosslessEncoder.Encode(tile, t, t, spp, 16); + offsets[ty * across + tx] = (uint)output.Position; + counts[ty * across + tx] = (uint)jpeg.Length; + output.Write(jpeg); + if (output.Position % 2 != 0) output.WriteByte(0); + } + } + raw.Long(TiffTags.TileWidth, (uint)t); + raw.Long(TiffTags.TileLength, (uint)t); + raw.Long(TiffTags.TileOffsets, offsets); + raw.Long(TiffTags.TileByteCounts, counts); + return; + } + var bytes = new byte[samples.Length * 2]; + if (spec.Compression == 8) + { + // Horizontal differencing per row (Predictor 2), then zlib. + var rowSamples = w * spp; + var diff = new ushort[samples.Length]; + for (var y = 0; y < h; y++) + { + for (var i = 0; i < rowSamples; i++) + { + var k = y * rowSamples + i; + diff[k] = i < spp ? samples[k] : (ushort)(samples[k] - samples[k - spp]); + } + } + for (var i = 0; i < diff.Length; i++) BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(2 * i), diff[i]); + using var ms = new MemoryStream(); + using (var z = new ZLibStream(ms, CompressionLevel.Fastest, leaveOpen: true)) z.Write(bytes); + bytes = ms.ToArray(); + } + else + { + for (var i = 0; i < samples.Length; i++) BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(2 * i), samples[i]); + } + raw.Long(TiffTags.StripOffsets, (uint)output.Position); + raw.Long(TiffTags.StripByteCounts, (uint)bytes.Length); + raw.Long(TiffTags.RowsPerStrip, (uint)h); + output.Write(bytes); + } +} + +/// Big-endian opcode list builder for tests (DNG 1.6 chapter 7 layout). +internal sealed class OpcodeListBuilder +{ + private readonly List _ops = []; + + public OpcodeListBuilder Add(uint id, uint flags, Action parameters) + { + var p = new BigEndian(); + parameters(p); + var body = p.ToArray(); + var head = new BigEndian().U32(id).U32(0x01030000).U32(flags).U32((uint)body.Length).ToArray(); + _ops.Add([.. head, .. body]); + return this; + } + + public byte[] Build() => [.. new BigEndian().U32((uint)_ops.Count).ToArray(), .. _ops.SelectMany(o => o)]; + + /// Area spec (top, left, bottom, right, plane, planes, row pitch, column pitch). + public static void Area(BigEndian p, int top, int left, int bottom, int right, int plane = 0, int planes = 1, int rowPitch = 1, int colPitch = 1) => + p.U32((uint)top).U32((uint)left).U32((uint)bottom).U32((uint)right).U32((uint)plane).U32((uint)planes).U32((uint)rowPitch).U32((uint)colPitch); + + internal sealed class BigEndian + { + private readonly MemoryStream _ms = new(); + + public BigEndian U32(uint v) + { + Span b = stackalloc byte[4]; + BinaryPrimitives.WriteUInt32BigEndian(b, v); + _ms.Write(b); + return this; + } + + public BigEndian U16(ushort v) + { + Span b = stackalloc byte[2]; + BinaryPrimitives.WriteUInt16BigEndian(b, v); + _ms.Write(b); + return this; + } + + public BigEndian F32(float v) + { + Span b = stackalloc byte[4]; + BinaryPrimitives.WriteSingleBigEndian(b, v); + _ms.Write(b); + return this; + } + + public BigEndian F64(double v) + { + Span b = stackalloc byte[8]; + BinaryPrimitives.WriteDoubleBigEndian(b, v); + _ms.Write(b); + return this; + } + + public byte[] ToArray() => _ms.ToArray(); + } +}