using Titano.Core.Imaging;
namespace Titano.Core.Analysis;
/// Independent deflicker curves for the sky and land regions.
public sealed record RegionDeflickerResult(DeflickerResult Sky, DeflickerResult Land);
///
/// Region deflicker (BRIEF ยง11.2): sky and land are measured through the soft , each gets its
/// own target curve, and the two corrections are blended per pixel through the mask.
/// Use it when the regions flicker independently (moving clouds, city lights) and the mask confidence is high.
///
public static class RegionDeflicker
{
/// Trimmed-mean log2 luminance of the sky and land regions of one analysis image.
public static (double Sky, double Land) Measure(ImageBuffer analysisImage, RegionMask mask, double trimLow = 0.02, double trimHigh = 0.02)
{
ArgumentNullException.ThrowIfNull(analysisImage);
ArgumentNullException.ThrowIfNull(mask);
if (mask.Width != analysisImage.Width || mask.Height != analysisImage.Height)
throw new ArgumentException("Mask and image sizes differ.", nameof(mask));
return (FrameStatistics.MeasureLogLuminance(analysisImage, mask.Sky, trimLow, trimHigh),
FrameStatistics.MeasureLogLuminance(analysisImage, mask.Land, trimLow, trimHigh));
}
public static RegionDeflickerResult Compute(ReadOnlySpan skyLogLuminance, ReadOnlySpan landLogLuminance, DeflickerOptions? options = null, ReadOnlySpan intentionalOffsets = default)
{
if (skyLogLuminance.Length != landLogLuminance.Length) throw new ArgumentException("Series lengths differ.", nameof(landLogLuminance));
return new RegionDeflickerResult(
DeflickerEngine.Compute(skyLogLuminance, options, intentionalOffsets),
DeflickerEngine.Compute(landLogLuminance, options, intentionalOffsets));
}
/// Applies one frame's sky and land corrections (EV) in place; the mask is resampled to the image size
/// when needed (resample once up front when rendering many frames).
public static void Apply(ImageBuffer image, RegionMask mask, double skyEv, double landEv, ChannelGains? gains = null, double? softClipKnee = null)
{
ArgumentNullException.ThrowIfNull(image);
ArgumentNullException.ThrowIfNull(mask);
var fitted = mask.Resample(image.Width, image.Height);
ExposureProcessor.ApplyRegion(image, fitted.Sky, skyEv, landEv, gains, softClipKnee);
}
}