Each container reader builds a RawFrame for the shared pipeline: CR2 sliced lossless JPEG with SensorInfo crop, masked-column black and ColorData WB; NEF uncompressed and Nikon Huffman (lossy type 1, lossless, lossy type 2 incl. split); ARW uncompressed and ARW2 cRAW with tone curve; PEF uncompressed and Pentax Huffman; RAF uncompressed Bayer and X-Trans with FujiIFD. Colour comes from a dcraw adobe_coeff matrix table; bodies without an entry are reported unsupported so the caller uses the embedded preview. Native RAWs detect the sensor's saturation level for clipping only, never rescaling per frame. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
142 lines
6.0 KiB
C#
142 lines
6.0 KiB
C#
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>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).</summary>
|
||
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; }
|
||
|
||
/// <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)));
|
||
}
|
||
}
|
||
}
|