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;
}
}