feat(raw): DNG development pipeline with AHD demosaic, opcodes and DNG colour

Reads CFA and LinearRaw DNGs (compression 1/7/8/34892), linearises with
LinearizationTable, BlackLevel (repeat + deltas) and WhiteLevel over the
ActiveArea, applies OpcodeList1-3 (GainMap, WarpRectilinear,
FixVignetteRadial, FixBadPixels*, TrimBounds, MapTable/MapPolynomial,
Delta/ScalePer*), solves white balance per DNG 1.6 chapter 6 (CCT
interpolation, ForwardMatrix, Bradford to D65), demosaics with AHD
(Hirakawa & Parks 2005), bilinear, a neighbourhood interpolation for
non-Bayer CFAs or binning, and reconstructs clipped highlights.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 10:56:34 +00:00
1 parent d3d1c5db0d
commit 1b3a691f76
15 files changed
+3559

No files matched your search

+141
View File
@@ -0,0 +1,141 @@
using Titano.Core.Codecs.Dng;
namespace Titano.Core.Codecs.Raw;
/// <summary>Integer rectangle (left, top inclusive; right, bottom exclusive).</summary>
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}";
}
/// <summary>Black level description (DNG BlackLevelRepeatDim, BlackLevel, BlackLevelDeltaH/V), relative to the active area.</summary>
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; }
/// <summary>rows × columns × samples-per-pixel values, sample index fastest.</summary>
public float[] Values { get; }
public float[]? DeltaH { get; }
public float[]? DeltaV { get; }
/// <summary>Largest black level of a sample plane, deltas included (the white-scaling reference of the DNG SDK).</summary>
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;
}
}
/// <summary>Decoded raw samples as stored in the file: interleaved, <see cref="Width"/> × <see cref="Height"/> ×
/// <see cref="SamplesPerPixel"/>, integers or floats.</summary>
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<string> Warnings { get; } = [];
/// <summary>Black level measured by the loader (e.g. from masked sensor columns), overriding the frame's own.</summary>
public RawBlackLevel? MeasuredBlack { get; set; }
public float this[long index] => Integers is { } i ? i[index] : Floats![index];
}
/// <summary>Reads the pixel payload of one container; created by the container parser, run by the pipeline.</summary>
internal abstract class RawLoader
{
/// <exception cref="InvalidDataException">The payload is malformed or uses an unsupported encoding.</exception>
public abstract RawSamples Load(Stream stream, int maxDegreeOfParallelism, CancellationToken cancellationToken);
}
/// <summary>
/// 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.
/// </summary>
internal sealed class RawFrame
{
/// <summary>Decoder name for logs ("managed-dng", "managed-cr2"…).</summary>
public required string Decoder { get; init; }
public required int Width { get; init; }
public required int Height { get; init; }
/// <summary>1 for CFA and monochrome data, 3 for LinearRaw RGB.</summary>
public int SamplesPerPixel { get; init; } = 1;
/// <summary>CFA layout anchored at the active area's top-left; null for linear (already demosaiced) data.</summary>
public CfaPattern? Cfa { get; init; }
/// <summary>Image area holding valid pixels, in stored coordinates.</summary>
public required RawRect ActiveArea { get; init; }
/// <summary>Final crop relative to the active area (DNG DefaultCropOrigin/Size); null = the whole active area.</summary>
public RawRect? DefaultCrop { get; init; }
/// <summary>DNG DefaultScale (horizontal, vertical) for non-square pixels.</summary>
public (double H, double V) DefaultScale { get; init; } = (1, 1);
public ushort[]? Linearization { get; init; }
public required RawBlackLevel Black { get; init; }
/// <summary>White level per sample plane, in linearised units.</summary>
public required double[] White { get; init; }
/// <summary>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.</summary>
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; }
/// <summary>Exif orientation 1–8.</summary>
public int Orientation { get; init; } = 1;
public string? Make { get; init; }
public string? Model { get; init; }
public required RawLoader Loader { get; init; }
public List<string> Warnings { get; } = [];
/// <summary>Size of the developed image before orientation (active area or default crop, default scale applied).</summary>
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)));
}
}
}