mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
craft-develop: retain same-colour clipping dependencies
This commit is contained in:
@@ -370,10 +370,8 @@ pub fn reconstruct_with(img: &mut Rgb32f, wb: [f32; 3], clip: f32, white: [f32;
|
||||
pub const CLIPPED_R: u8 = 1;
|
||||
pub const CLIPPED_G: u8 = 2;
|
||||
pub const CLIPPED_B: u8 = 4;
|
||||
/// Bits of a [`reconstruct_masked`] mask entry: which channels of the pixel were interpolated (not measured) from
|
||||
/// samples of their colour some of which clipped. Below the clip their value is unknown, neither the true one nor a
|
||||
/// bound of it, and is rebuilt from the pixel's measured channels; at or above it, a lower bound as for
|
||||
/// [`CLIPPED_R`] (green interpolated from clipped greens in a partly clipped sky).
|
||||
/// Bits of a [`reconstruct_masked`] mask entry for the colours of nearby clipped samples that can affect interpolation.
|
||||
/// In a directional mask, a clipped sample of the measured colour can still affect the interpolated channels.
|
||||
pub const FROM_CLIPPED_R: u8 = 8;
|
||||
pub const FROM_CLIPPED_G: u8 = 16;
|
||||
pub const FROM_CLIPPED_B: u8 = 32;
|
||||
@@ -390,10 +388,12 @@ pub(super) const FROM_CLIPPED_BITS: [u8; 3] = [FROM_CLIPPED_R, FROM_CLIPPED_G, F
|
||||
/// true value, so one clipped specular sample among many unclipped ones neither escapes reconstruction nor
|
||||
/// (as the block's maximum would) lifts the channel to the clip level, which reads as a coloured speckle (#548).
|
||||
///
|
||||
/// For a full-size demosaic ([`super::RawDevelopExt::develop_balanced_masked`]), the mask marks colours
|
||||
/// interpolated from clipped samples. Below the clip, an interpolated channel marked this way is unknown.
|
||||
/// A directional mask also records the measured colour. In that case, the other interpolated channel may
|
||||
/// depend on the same clipped samples, so reconstruction treats it as unknown below the clip too.
|
||||
/// For a full-size demosaic ([`super::RawDevelopExt::develop_balanced_masked`]), the mask records the colours
|
||||
/// of clipped samples that can affect interpolation. In a directional mask, it also records the measured
|
||||
/// colour so reconstruction can treat only the interpolated channels below clip as unknown. A dependency on the
|
||||
/// measured colour alone triggers that treatment when every channel is below clip. This preserves partly clipped
|
||||
/// detail when another channel is already saturated. A bilinear mask marks each interpolated channel separately.
|
||||
/// Interpolated values at or above clip still count as lower bounds.
|
||||
/// Reconstruction uses the known channels and limits brightness changes near strongly coloured highlights.
|
||||
///
|
||||
/// A mask of the wrong length is ignored (clipping is then told by the values alone).
|
||||
@@ -427,10 +427,11 @@ fn masked(m: u8, p: &[f32; 3], clip: f32) -> Clipping {
|
||||
let bit = |b: u8| m & b != 0;
|
||||
let saturated = std::array::from_fn(|c| p[c] >= clip || bit(CLIPPED_BITS[c]));
|
||||
let mut unknown: [bool; 3] = std::array::from_fn(|c| bit(FROM_CLIPPED_BITS[c]) && !saturated[c]);
|
||||
if let Some(own) = ((m >> MEASURED_SHIFT) as usize).checked_sub(1).filter(|&c| !saturated[c])
|
||||
&& unknown.iter().any(|&u| u)
|
||||
{
|
||||
unknown = std::array::from_fn(|c| c != own && !saturated[c]);
|
||||
if let Some(own) = ((m >> MEASURED_SHIFT) as usize).checked_sub(1).filter(|&c| !saturated[c]) {
|
||||
let from_measured_colour = std::mem::take(&mut unknown[own]);
|
||||
if unknown.iter().any(|&u| u) || (from_measured_colour && !saturated.iter().any(|&c| c)) {
|
||||
unknown = std::array::from_fn(|c| c != own && !saturated[c]);
|
||||
}
|
||||
}
|
||||
Clipping { clipped: std::array::from_fn(|c| saturated[c] || unknown[c]), saturated }
|
||||
}
|
||||
|
||||
@@ -299,15 +299,13 @@ fn clip_mask(raw: &RawImage, n: &Normalized, method: Method, clip: f32) -> Optio
|
||||
for (ox, bits) in out.iter_mut().enumerate() {
|
||||
let x = crop.x + ox;
|
||||
let colour = cfa.color_at(x, y);
|
||||
let from = *bits & !FROM_CLIPPED_BITS[colour as usize];
|
||||
*bits = if is_clipped(&n.data[y * w + x]) {
|
||||
if is_clipped(&n.data[y * w + x]) {
|
||||
// Keep the interpolated values around a clipped measurement. They can carry thin dark detail.
|
||||
CLIPPED_BITS[colour as usize]
|
||||
} else if from != 0 {
|
||||
from | ((colour + 1) << highlight::MEASURED_SHIFT)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
*bits = CLIPPED_BITS[colour as usize];
|
||||
} else if *bits != 0 {
|
||||
// A same-colour sample can affect the other channels through the green estimates.
|
||||
*bits |= (colour + 1) << highlight::MEASURED_SHIFT;
|
||||
}
|
||||
}
|
||||
});
|
||||
Some(mask)
|
||||
|
||||
@@ -346,7 +346,7 @@ fn the_clip_mask_marks_the_colours_interpolated_from_clipped_samples() {
|
||||
assert_eq!(at(10, 8), highlight::CLIPPED_R, "the saturated red site");
|
||||
assert_eq!(at(4, 4), highlight::FROM_CLIPPED_G | (1 << highlight::MEASURED_SHIFT), "red beside saturated green");
|
||||
assert_eq!(at(11, 8), highlight::FROM_CLIPPED_R | (2 << highlight::MEASURED_SHIFT), "green beside saturated red");
|
||||
assert_eq!(at(5, 6), 0, "an unclipped green site near the clipped green site");
|
||||
assert_ne!(at(5, 6) & highlight::FROM_CLIPPED_G, 0, "clipped green can affect the other channels at this green site");
|
||||
assert_eq!(at(2, 8), 0, "a site away from both clipped samples");
|
||||
// An unclipped mosaic or an OpcodeList3 needs no mask.
|
||||
let clean = raw_of(w, h, &cfa, [1.0; 3], |_, _| [0.3; 3]);
|
||||
@@ -355,6 +355,37 @@ fn the_clip_mask_marks_the_colours_interpolated_from_clipped_samples() {
|
||||
assert!(raw.develop_balanced_masked(Method::Ahd, DAYLIGHT, CLIP).unwrap().1.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppg_clip_mask_marks_same_colour_dependencies() {
|
||||
let cfa = Cfa::bayer("RGGB").unwrap();
|
||||
let wb = [1.0; 3];
|
||||
let mut raw = raw_of(16, 12, &cfa, wb, |x, y| {
|
||||
let level = match (x, y) {
|
||||
(5, 4) | (6, 3) => 0.26,
|
||||
(7, 4) => 0.35,
|
||||
(8, 4) => 0.4,
|
||||
(6, 5) => 0.49,
|
||||
_ => 0.3,
|
||||
};
|
||||
[level; 3]
|
||||
});
|
||||
let clean = raw.develop(Method::Ppg).unwrap().get(6, 4);
|
||||
if let RawData::F32(samples) = &mut raw.data {
|
||||
samples[4 * raw.width + 4] = 1.0;
|
||||
}
|
||||
let (mut image, mask) = raw.develop_balanced_masked(Method::Ppg, wb, CLIP).unwrap();
|
||||
let changed = image.get(6, 4);
|
||||
assert!(changed[1] > clean[1] && changed.iter().all(|&v| v < CLIP));
|
||||
let mask = mask.unwrap();
|
||||
let entry = mask[4 * image.width + 6];
|
||||
assert_ne!(entry & highlight::FROM_CLIPPED_R, 0, "the clipped red sample changed interpolated green");
|
||||
assert_eq!(entry >> highlight::MEASURED_SHIFT, cfa.color_at(6, 4) + 1);
|
||||
assert_eq!(mask[4 * image.width + 10], 0, "no clipped sample is within reach");
|
||||
highlight::reconstruct_masked(&mut image, wb, CLIP, wb, &mask);
|
||||
assert_eq!(image.get(6, 4)[0], clean[0], "reconstruction keeps measured red");
|
||||
assert_ne!(image.get(6, 4)[1], changed[1], "reconstruction can use measured red to rebuild green");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bilinear_clip_mask_marks_only_actual_taps() {
|
||||
let cfa = Cfa::bayer("RGGB").unwrap();
|
||||
|
||||
Reference in New Issue
Block a user