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; }
/// Find the clip threshold in the data: native RAWs state a nominal bit-depth maximum while sensors saturate
/// earlier. Only clip detection uses it; the white level keeps scaling every frame the same (no flicker).
public bool DetectSaturation { 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)));
}
}
}