mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 05:37:04 +00:00
craft-develop: make the sensor clip mask public for callers that demosaic themselves (#3178)
* craft-develop: make the sensor clip mask public for callers that demosaic themselves * craft-develop: reject clip-mask dimensions whose product overflows
This commit is contained in:
@@ -168,7 +168,7 @@ impl RawDevelopExt for RawImage {
|
||||
|
||||
fn develop_balanced_masked(&self, method: Method, wb: [f32; 3], clip: f32) -> Result<(Rgb32f, Option<Vec<u8>>)> {
|
||||
let n = self.normalized()?;
|
||||
let mask = clip_mask(self, &n, method, clip);
|
||||
let mask = raw_clip_mask(self, &n, method, clip);
|
||||
Ok((develop_full(self, n, method, Some(wb)), mask))
|
||||
}
|
||||
|
||||
@@ -253,17 +253,34 @@ fn scale_mosaic(n: &mut Normalized, method: Method, gains: [f32; 3]) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn clip_mask(raw: &RawImage, n: &Normalized, method: Method, clip: f32) -> Option<Vec<u8>> {
|
||||
fn raw_clip_mask(raw: &RawImage, n: &Normalized, method: Method, clip: f32) -> Option<Vec<u8>> {
|
||||
let crop = raw.develop_crop(n.width, n.height);
|
||||
if !raw.opcodes.list3.is_empty() || stretched_height(crop.height, raw.pixel_aspect) != crop.height {
|
||||
return None;
|
||||
}
|
||||
clip_mask(n, crop, method, clip)
|
||||
}
|
||||
|
||||
/// The [`highlight::reconstruct_masked`] mask for the output of [`demosaic()`] with `method` on the single-plane CFA
|
||||
/// mosaic `n`, cropped to `crop` (one entry per pixel of `crop`, row-major). `n` holds sensor values normalised so
|
||||
/// that 1.0 is white and before white balance; samples at or above `clip` count as clipped. Use it when the mosaic
|
||||
/// is demosaicked by the caller (for example white-balanced first) rather than through [`RawDevelopExt`].
|
||||
/// `None` when nothing is clipped, `n` is not a single-plane RGB CFA mosaic, `crop` does not fit inside it, or `clip`
|
||||
/// is not a positive finite level.
|
||||
pub fn clip_mask(n: &Normalized, crop: Rect, method: Method, clip: f32) -> Option<Vec<u8>> {
|
||||
use highlight::{CLIPPED_BITS, FROM_CLIPPED_BITS};
|
||||
|
||||
// This radius covers samples read directly by the directional interpolators. Estimates
|
||||
// that build on interpolated green can depend on samples farther away.
|
||||
const REACH: usize = 2;
|
||||
|
||||
let cfa = n.cfa.as_ref().filter(|_| n.cpp == 1)?;
|
||||
let cfa = n.cfa.as_ref().filter(|c| n.cpp == 1 && cfa_valid(c) && c.pattern.iter().all(|&p| p <= 2))?;
|
||||
let (w, h) = (n.width, n.height);
|
||||
let crop = raw.develop_crop(w, h);
|
||||
if !clip.is_finite() || clip <= 0.0 || !raw.opcodes.list3.is_empty() || stretched_height(crop.height, raw.pixel_aspect) != crop.height {
|
||||
let fits = crop.width > 0
|
||||
&& crop.height > 0
|
||||
&& crop.x.checked_add(crop.width).is_some_and(|r| r <= w)
|
||||
&& crop.y.checked_add(crop.height).is_some_and(|b| b <= h);
|
||||
if !clip.is_finite() || clip <= 0.0 || !fits || w.checked_mul(h) != Some(n.data.len()) {
|
||||
return None;
|
||||
}
|
||||
let is_clipped = |v: &f32| *v >= clip;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Original synthetic RAW scenes from storytold/lightcraft#1463, generated in these tests.
|
||||
|
||||
use craft_develop::raster::Rgb32f;
|
||||
use craft_develop::sensor::{Method, RawDevelopExt, highlight};
|
||||
use craft_develop::sensor::{self, Method, RawDevelopExt, highlight};
|
||||
use craft_raw::opcodes::{Opcode, OpcodeLists};
|
||||
use craft_raw::{BlackLevel, Cfa, ColorData, Orientation, RawData, RawFormat, RawImage, Rect};
|
||||
|
||||
@@ -355,6 +355,30 @@ 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 a_caller_demosaicking_its_own_mosaic_gets_the_same_clip_mask() {
|
||||
let (w, h) = (40usize, 30usize);
|
||||
let light = photograph(w, h, |x, y| if (x - 18.0).powi(2) + (y - 14.0).powi(2) < 49.0 { 6.0 } else { 0.4 });
|
||||
let mut cases = layouts();
|
||||
cases.push(("RGGB bilinear".into(), Cfa::bayer("RGGB").unwrap(), Method::Bilinear));
|
||||
for (name, cfa, method) in cases {
|
||||
let mut raw = raw_of(w, h, &cfa, DAYLIGHT, |x, y| [light[y * w + x]; 3]);
|
||||
raw.crop = Rect::new(2, 1, 33, 27);
|
||||
let (_, expected) = raw.develop_balanced_masked(method, DAYLIGHT, CLIP).unwrap();
|
||||
let n = raw.normalized().unwrap();
|
||||
let crop = raw.develop_crop(n.width, n.height);
|
||||
assert_eq!(sensor::clip_mask(&n, crop, method, CLIP), expected, "{name}");
|
||||
assert!(expected.is_some(), "{name}: clipped samples");
|
||||
let outside = Rect::new(crop.x + 1, crop.y, n.width - crop.x, crop.height);
|
||||
assert!(sensor::clip_mask(&n, outside, method, CLIP).is_none(), "{name}: a crop past the mosaic");
|
||||
let mut four_colour = n.clone();
|
||||
four_colour.cfa.as_mut().unwrap().pattern[0] = 3;
|
||||
assert!(sensor::clip_mask(&four_colour, crop, method, CLIP).is_none(), "{name}: a colour index past blue");
|
||||
let huge = sensor::Normalized { width: usize::MAX, height: 2, data: Vec::new(), ..n.clone() };
|
||||
assert!(sensor::clip_mask(&huge, Rect::new(0, 0, 1, 1), method, CLIP).is_none(), "{name}: dimensions past usize");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppg_clip_mask_marks_same_colour_dependencies() {
|
||||
let cfa = Cfa::bayer("RGGB").unwrap();
|
||||
|
||||
Reference in New Issue
Block a user