mirror of
https://github.com/storytold/lightcraft.git
synced 2026-12-05 00:11:17 -05:00
LR-PROF-CAMERACOLOR: avoid CR3 correspondence buffer clones
Estimate the established framing fallback from borrowed sensor data and defer its mask resampling until fallback selection. Preserve both distinct geometry searches and all original/fallback training and held-out gates. Add deterministic allocation, pixel, search-budget and successful-selection regressions. Reintroducing source clones fails the allocation assertion. Full cargo xtask ci passes all nine gates with Rust 1.95.0, actual Metal and WASM; capability skips are recorded separately. Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
@@ -143,14 +143,8 @@ pub(super) fn proxies(
|
||||
clipped = clipped.into_oriented(raw.orientation);
|
||||
reference = reference.into_oriented(raw.orientation);
|
||||
sensor.map_in_place(to_working);
|
||||
// Judge the external warp against the established CR3 framing fallback, not
|
||||
// against unregistered sensor pixels. This preserves an already better fit.
|
||||
let (fallback, fallback_mask) = align_cr3_framing(sensor.clone(), clipped.clone(), &reference);
|
||||
let proposed =
|
||||
embedded.filter(|correction| transform.matrix_is_fallback && lens.is_none() && correction.warp.is_some()).and_then(|correction| {
|
||||
align_profile_geometry_against(&sensor, &clipped, &reference, correction, lightcraft_geom::Orientation::Normal, Some(&fallback))
|
||||
});
|
||||
(sensor, clipped) = proposed.unwrap_or((fallback, fallback_mask));
|
||||
let correction = embedded.filter(|correction| transform.matrix_is_fallback && lens.is_none() && correction.warp.is_some());
|
||||
(sensor, clipped) = align_cr3_geometry(sensor, clipped, &reference, correction);
|
||||
}
|
||||
let mut sensor = fit(&sensor, size, size, Filter::Box);
|
||||
let mut reference_small = reference_at(&reference, &sensor);
|
||||
@@ -207,10 +201,17 @@ pub(super) fn estimate_cr3_framing(sensor: &Rgb32f, reference: &Rgb32f) -> Optio
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
static FRAMING_SEARCHES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
static LAST_GEOMETRY_SCORES: std::cell::Cell<Option<[f64; 4]>> = const { std::cell::Cell::new(None) };
|
||||
static LAST_FRAMED_SCORES: std::cell::Cell<Option<[f64; 4]>> = const { std::cell::Cell::new(None) };
|
||||
}
|
||||
|
||||
/// Read and reset this thread's framing-search budget, independent of elapsed time.
|
||||
#[cfg(test)]
|
||||
pub(super) fn take_framing_search_count() -> usize {
|
||||
FRAMING_SEARCHES.replace(0)
|
||||
}
|
||||
|
||||
/// Return this test thread's last `[train_before, train_after, held_before, held_after]`
|
||||
/// scores against the original sensor geometry.
|
||||
#[cfg(test)]
|
||||
@@ -230,6 +231,7 @@ pub(super) fn geometry_framed_scores() -> Option<[f64; 4]> {
|
||||
fn estimate_framing_against(sensor: &Rgb32f, reference: &Rgb32f, original: Option<&Rgb32f>, framed: Option<&Rgb32f>) -> Option<Cr3Framing> {
|
||||
#[cfg(test)]
|
||||
{
|
||||
FRAMING_SEARCHES.set(FRAMING_SEARCHES.get() + 1);
|
||||
LAST_GEOMETRY_SCORES.set(None);
|
||||
LAST_FRAMED_SCORES.set(None);
|
||||
}
|
||||
@@ -402,19 +404,44 @@ pub(super) fn estimate_profile_framing(candidate: &Rgb32f, reference: &Rgb32f, o
|
||||
estimate_framing_against(candidate, reference, Some(original), None)
|
||||
}
|
||||
|
||||
/// The sensor proxy and its clip mask (same size), both moved to the camera JPEG's framing.
|
||||
fn align_cr3_framing(sensor: Rgb32f, clipped: Rgb32f, reference: &Rgb32f) -> (Rgb32f, Rgb32f) {
|
||||
let Some(framing) = estimate_cr3_framing(&sensor, reference) else { return (sensor, clipped) };
|
||||
let apply = |image: &Rgb32f| {
|
||||
let (w, h) = (image.width, image.height);
|
||||
let mut aligned = Rgb32f::new(w, h);
|
||||
for (i, pixel) in aligned.data.iter_mut().enumerate() {
|
||||
let (x, y) = framing.source((i % w) as f32 + 0.5, (i / w) as f32 + 0.5, w, h);
|
||||
*pixel = if x >= 0.5 && y >= 0.5 && x <= w as f32 - 0.5 && y <= h as f32 - 0.5 { image.sample_bilinear(x, y) } else { [f32::NAN; 3] };
|
||||
}
|
||||
aligned
|
||||
};
|
||||
(apply(&sensor), apply(&clipped))
|
||||
/// Choose CR3 correspondence without copying the unregistered sensor or mask. The
|
||||
/// unwarped fallback and warped proposal need separate searches: their pixels and
|
||||
/// support rules differ, and the proposal must preserve the accepted fallback.
|
||||
pub(super) fn align_cr3_geometry(
|
||||
sensor: Rgb32f,
|
||||
clipped: Rgb32f,
|
||||
reference: &Rgb32f,
|
||||
correction: Option<&lightcraft_develop::EmbeddedLens>,
|
||||
) -> (Rgb32f, Rgb32f) {
|
||||
let framing = estimate_cr3_framing(&sensor, reference);
|
||||
let fallback = framing.map(|framing| apply_cr3_framing(&sensor, framing));
|
||||
if let Some(proposed) = correction.and_then(|correction| {
|
||||
align_profile_geometry_against(
|
||||
&sensor,
|
||||
&clipped,
|
||||
reference,
|
||||
correction,
|
||||
lightcraft_geom::Orientation::Normal,
|
||||
Some(fallback.as_ref().unwrap_or(&sensor)),
|
||||
)
|
||||
}) {
|
||||
return proposed;
|
||||
}
|
||||
// The mask is needed only if the fallback wins; when no framing is accepted,
|
||||
// return both original allocations unchanged.
|
||||
let clipped = if let Some(framing) = framing { apply_cr3_framing(&clipped, framing) } else { clipped };
|
||||
(fallback.unwrap_or(sensor), clipped)
|
||||
}
|
||||
|
||||
/// Move one image to the accepted camera framing, preserving invalid border samples.
|
||||
fn apply_cr3_framing(image: &Rgb32f, framing: Cr3Framing) -> Rgb32f {
|
||||
let (w, h) = (image.width, image.height);
|
||||
let mut aligned = Rgb32f::new(w, h);
|
||||
for (i, pixel) in aligned.data.iter_mut().enumerate() {
|
||||
let (x, y) = framing.source((i % w) as f32 + 0.5, (i / w) as f32 + 0.5, w, h);
|
||||
*pixel = if x >= 0.5 && y >= 0.5 && x <= w as f32 - 0.5 && y <= h as f32 - 0.5 { image.sample_bilinear(x, y) } else { [f32::NAN; 3] };
|
||||
}
|
||||
aligned
|
||||
}
|
||||
|
||||
/// Apply a shared source map to colour or a nonnegative clip mask. Invalid borders
|
||||
|
||||
@@ -2678,6 +2678,50 @@ fn cr3_framing_only_does_not_report_profile_gate_scores() {
|
||||
assert!(super::proxy::geometry_framed_scores().is_none());
|
||||
}
|
||||
|
||||
/// Rejected registration must preserve both input allocations and pixel bits. A
|
||||
/// missing/ineligible warp uses only the fallback search; an eligible warp adds
|
||||
/// exactly one search on different (warped) pixels, never repeats either search.
|
||||
#[test]
|
||||
fn cr3_unchanged_geometry_preserves_allocations_and_bounds_search_work() {
|
||||
for (correction, searches) in [(None, 1), (Some(Default::default()), 1), (Some(synthetic_profile_lens()), 2)] {
|
||||
let sensor = profile_geometry_scene(384, 256);
|
||||
let reference = sensor.clone();
|
||||
let mut mask = Rgb32f::new(sensor.width, sensor.height);
|
||||
mask.data[17] = [1.0; 3];
|
||||
let sensor_ptr = sensor.data.as_ptr();
|
||||
let mask_ptr = mask.data.as_ptr();
|
||||
let mask_bits: Vec<_> = mask.data.iter().map(|p| p.map(f32::to_bits)).collect();
|
||||
super::proxy::take_framing_search_count();
|
||||
let (aligned, aligned_mask) = super::proxy::align_cr3_geometry(sensor, mask, &reference, correction.as_ref());
|
||||
assert_eq!(super::proxy::take_framing_search_count(), searches, "bounded search budget for missing or rejected geometry");
|
||||
assert_eq!(aligned.data.as_ptr(), sensor_ptr, "no accepted framing or warp: retain sensor allocation");
|
||||
assert_eq!(aligned_mask.data.as_ptr(), mask_ptr, "no accepted framing or warp: retain mask allocation");
|
||||
assert_eq!(aligned.data, reference.data);
|
||||
assert_eq!(aligned_mask.data.iter().map(|p| p.map(f32::to_bits)).collect::<Vec<_>>(), mask_bits);
|
||||
}
|
||||
}
|
||||
|
||||
/// Exercise selection through the same orchestration as file-local and pooled
|
||||
/// fitting, including the proposal's comparisons with the framed fallback.
|
||||
#[test]
|
||||
fn cr3_geometry_selects_matching_profile_with_one_search_per_geometry() {
|
||||
let sensor = profile_geometry_scene(384, 256);
|
||||
let lens = synthetic_profile_lens();
|
||||
let framing = Cr3Framing { scale: 0.95, offset: [0.006, -0.004] };
|
||||
let expected = profile_geometry_target(&sensor, &lens, framing);
|
||||
let mut reference = expected.clone();
|
||||
reference.map_in_place(|p| p.map(|v| v.powf(0.7)));
|
||||
let mask = Rgb32f::new(sensor.width, sensor.height);
|
||||
let expected_mask = profile_geometry_target(&mask, &lens, framing);
|
||||
super::proxy::take_framing_search_count();
|
||||
let (aligned, aligned_mask) = super::proxy::align_cr3_geometry(sensor, mask, &reference, Some(&lens));
|
||||
assert_eq!(super::proxy::take_framing_search_count(), 2);
|
||||
assert!(super::proxy::geometry_gate_scores().is_some(), "proposal passed original geometry gates");
|
||||
assert!(super::proxy::geometry_framed_scores().is_some(), "proposal was compared with fallback on both splits");
|
||||
assert_profile_geometry_mask(&aligned, &aligned_mask, &expected_mask);
|
||||
assert!(geometry_rms(&aligned, &expected) < 0.008, "matching proposal selected despite nonlinear JPEG tone");
|
||||
}
|
||||
|
||||
/// Return a deliberately strong radial warp for synthetic correspondence tests.
|
||||
fn synthetic_profile_lens() -> lightcraft_develop::EmbeddedLens {
|
||||
lightcraft_develop::EmbeddedLens {
|
||||
|
||||
Reference in New Issue
Block a user