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
121 lines
5.1 KiB
C#
121 lines
5.1 KiB
C#
using Titano.Core.Imaging;
|
||
|
||
namespace Titano.Core.Codecs.Raw;
|
||
|
||
/// <summary>
|
||
/// 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.
|
||
/// </summary>
|
||
internal static class HighlightReconstruction
|
||
{
|
||
/// <summary>Fraction of its clip level at which an unclipped channel starts pulling the pixel toward white.</summary>
|
||
private const float DesaturationStart = 0.7f;
|
||
|
||
/// <param name="rgb">White-balanced camera RGB, modified in place.</param>
|
||
/// <param name="clipBits">Per pixel: bit c set when channel c is built from clipped raw samples.</param>
|
||
/// <param name="clipLevels">Clip level of each channel in <paramref name="rgb"/>'s units (the white-balance multipliers
|
||
/// when raw white is 1).</param>
|
||
public static void Apply(ImageBuffer rgb, byte[] clipBits, ReadOnlySpan<float> 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<float> 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;
|
||
}
|
||
});
|
||
}
|
||
|
||
/// <summary>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).</summary>
|
||
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;
|
||
}
|
||
|
||
/// <summary>Clip bits of a binned image: the OR over each <paramref name="size"/>×<paramref name="size"/> block.</summary>
|
||
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;
|
||
}
|
||
}
|