use image::GrayImage;
use crate::calibration::CameraCalibration;
use crate::error::{ComposeError, FrameField};
pub const HIDDEN_BELOW: f32 = 0.6;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Located {
pub offset_px: [f64; 2],
pub score: f32,
}
pub fn check_frame(
calibration: &CameraCalibration,
size: [u32; 2],
camera_snapshot_url: &str,
) -> Result<(), ComposeError> {
if size != calibration.image_size_px {
let [ew, eh] = calibration.image_size_px;
return Err(ComposeError::FrameMismatch {
field: FrameField::Resolution,
expected: format!("{ew}x{eh}"),
got: format!("{}x{}", size[0], size[1]),
});
}
if camera_snapshot_url != calibration.camera_snapshot_url {
return Err(ComposeError::FrameMismatch {
field: FrameField::Stream,
expected: calibration.camera_snapshot_url.clone(),
got: camera_snapshot_url.to_owned(),
});
}
Ok(())
}
pub const VIEW_SEARCH_RADIUS_PX: u32 = 96;
#[must_use]
pub fn locate_landmarks(calibration: &CameraCalibration, frame: &GrayImage) -> Vec<Located> {
calibration
.landmarks
.iter()
.zip(calibration.patches())
.map(|(landmark, patch)| {
locate(
frame,
patch,
landmark.center_px,
i64::from(landmark.search_radius_px),
)
})
.collect()
}
#[must_use]
pub fn locate_landmarks_within(
calibration: &CameraCalibration,
frame: &GrayImage,
radius_px: u32,
) -> Vec<Located> {
calibration
.landmarks
.iter()
.zip(calibration.patches())
.map(|(landmark, patch)| locate(frame, patch, landmark.center_px, i64::from(radius_px)))
.collect()
}
pub fn check_drift(calibration: &CameraCalibration, frame: &GrayImage) -> Result<(), ComposeError> {
let found = locate_landmarks(calibration, frame);
let visible: Vec<(&str, f64)> = calibration
.landmarks
.iter()
.zip(&found)
.filter(|(_, located)| located.score >= HIDDEN_BELOW)
.map(|(landmark, located)| {
(
landmark.name.as_str(),
located.offset_px[0].hypot(located.offset_px[1]),
)
})
.collect();
if visible.len() < 2 {
return Err(ComposeError::DriftUnverified);
}
let moved: Vec<&(&str, f64)> = visible
.iter()
.filter(|(_, offset)| *offset > calibration.drift_tolerance_px)
.collect();
if moved.len() >= 2 {
let (landmark, offset_px) =
moved.iter().copied().fold(
moved[0],
|far, next| if next.1 > far.1 { next } else { far },
);
return Err(ComposeError::CameraMoved {
landmark: (*landmark).to_owned(),
offset_px: *offset_px,
});
}
Ok(())
}
fn locate(frame: &GrayImage, patch: &GrayImage, center: [f64; 2], radius: i64) -> Located {
let hidden = Located {
offset_px: [0.0, 0.0],
score: -1.0,
};
let (pw, ph) = (i64::from(patch.width()), i64::from(patch.height()));
let (fw, fh) = (i64::from(frame.width()), i64::from(frame.height()));
#[expect(
clippy::cast_possible_truncation,
reason = "a landmark's centre is a pixel of a frame of at most a few thousand pixels"
)]
let (left, top) = (
center[0].round() as i64 - pw / 2,
center[1].round() as i64 - ph / 2,
);
let template: Vec<f64> = patch
.as_raw()
.iter()
.map(|value| f64::from(*value))
.collect();
let count = u64::try_from(template.len()).unwrap_or(u64::MAX);
let mean = template.iter().sum::<f64>() / f64::from(u32::try_from(count).unwrap_or(u32::MAX));
let centred: Vec<f64> = template.iter().map(|value| value - mean).collect();
let template_norm = centred
.iter()
.map(|value| value * value)
.sum::<f64>()
.sqrt();
if template_norm < 1e-3 {
return hidden;
}
let pixels = frame.as_raw();
#[expect(
clippy::cast_possible_truncation,
reason = "a centred grey value lies within ±255"
)]
let weights: Vec<f32> = centred.iter().map(|value| *value as f32).collect();
let side = usize::try_from(2 * radius + 1).unwrap_or(0);
let mut scores = vec![f32::NEG_INFINITY; side * side];
let width = usize::try_from(fw).unwrap_or(0);
let pw_us = usize::try_from(pw).unwrap_or(0);
let patch_rows = usize::try_from(ph).unwrap_or(0);
let region = Region::around(left, top, radius, [pw, ph], [fw, fh]);
let (sums, squares) = region.integrals(pixels, width);
for dy in -radius..=radius {
for dx in -radius..=radius {
let (x0, y0) = (left + dx, top + dy);
if x0 < 0 || y0 < 0 || x0 + pw > fw || y0 + ph > fh {
continue;
}
let (x0, y0) = (
usize::try_from(x0).unwrap_or(0),
usize::try_from(y0).unwrap_or(0),
);
let sum = region.window(&sums, x0, y0, pw_us, patch_rows);
let square_sum = region.window(&squares, x0, y0, pw_us, patch_rows);
let spread_squared =
u128::from(count) * u128::from(square_sum) - u128::from(sum) * u128::from(sum);
if spread_squared == 0 {
continue;
}
let mut cross = 0.0_f64;
for (row, weights) in weights.chunks_exact(pw_us).enumerate() {
let start = (y0 + row) * width + x0;
cross += f64::from(dot(&pixels[start..start + pw_us], weights));
}
#[expect(
clippy::cast_precision_loss,
reason = "a window's scaled spread is far below 2^53 for any patch a calibration holds"
)]
let spread = (spread_squared as f64).sqrt()
/ f64::from(u32::try_from(count).unwrap_or(u32::MAX)).sqrt();
let at = usize::try_from((dy + radius) * (2 * radius + 1) + dx + radius).unwrap_or(0);
#[expect(
clippy::cast_possible_truncation,
reason = "a correlation lies within -1 to 1"
)]
let score = (cross / (spread * template_norm)) as f32;
scores[at] = score;
}
}
let Some((best, score)) = scores
.iter()
.copied()
.enumerate()
.filter(|(_, score)| score.is_finite())
.reduce(|best, next| if next.1 > best.1 { next } else { best })
else {
return hidden;
};
Located {
offset_px: refined_peak(&scores, side, best, score, radius),
score,
}
}
fn dot(pixels: &[u8], weights: &[f32]) -> f32 {
let mut lanes = [0.0_f32; 8];
let (pixel_chunks, pixel_rest) = pixels.as_chunks::<8>();
let (weight_chunks, weight_rest) = weights.as_chunks::<8>();
for ([p0, p1, p2, p3, p4, p5, p6, p7], [w0, w1, w2, w3, w4, w5, w6, w7]) in
pixel_chunks.iter().zip(weight_chunks)
{
lanes = [
lanes[0] + f32::from(*p0) * w0,
lanes[1] + f32::from(*p1) * w1,
lanes[2] + f32::from(*p2) * w2,
lanes[3] + f32::from(*p3) * w3,
lanes[4] + f32::from(*p4) * w4,
lanes[5] + f32::from(*p5) * w5,
lanes[6] + f32::from(*p6) * w6,
lanes[7] + f32::from(*p7) * w7,
];
}
let rest: f32 = pixel_rest
.iter()
.zip(weight_rest)
.map(|(pixel, weight)| f32::from(*pixel) * weight)
.sum();
lanes.iter().sum::<f32>() + rest
}
struct Region {
origin: [usize; 2],
size: [usize; 2],
}
impl Region {
fn around(left: i64, top: i64, radius: i64, patch: [i64; 2], frame: [i64; 2]) -> Self {
let clamp = |value: i64, most: i64| usize::try_from(value.clamp(0, most)).unwrap_or(0);
let (x0, y0) = (
clamp(left - radius, frame[0]),
clamp(top - radius, frame[1]),
);
let (x1, y1) = (
clamp(left + radius + patch[0], frame[0]),
clamp(top + radius + patch[1], frame[1]),
);
Self {
origin: [x0, y0],
size: [x1.saturating_sub(x0), y1.saturating_sub(y0)],
}
}
fn integrals(&self, pixels: &[u8], width: usize) -> (Vec<u64>, Vec<u64>) {
let stride = self.size[0] + 1;
let mut sums = vec![0_u64; stride * (self.size[1] + 1)];
let mut squares = sums.clone();
for row in 0..self.size[1] {
let (mut along, mut along_squares) = (0_u64, 0_u64);
let start = (self.origin[1] + row) * width + self.origin[0];
for (column, pixel) in pixels[start..start + self.size[0]].iter().enumerate() {
let value = u64::from(*pixel);
along += value;
along_squares += value * value;
let at = (row + 1) * stride + column + 1;
sums[at] = sums[at - stride] + along;
squares[at] = squares[at - stride] + along_squares;
}
}
(sums, squares)
}
fn window(&self, integral: &[u64], x: usize, y: usize, w: usize, h: usize) -> u64 {
let stride = self.size[0] + 1;
let (x, y) = (x - self.origin[0], y - self.origin[1]);
integral[(y + h) * stride + x + w] + integral[y * stride + x]
- integral[y * stride + x + w]
- integral[(y + h) * stride + x]
}
}
fn refined_peak(scores: &[f32], side: usize, best: usize, score: f32, radius: i64) -> [f64; 2] {
let (row, column) = (best / side, best % side);
let refine = |before: Option<f32>, after: Option<f32>| match (before, after) {
(Some(a), Some(c)) if a.is_finite() && c.is_finite() => {
let curve = a - 2.0 * score + c;
if curve.abs() > 1e-9 {
f64::from(0.5 * (a - c) / curve)
} else {
0.0
}
}
_ => 0.0,
};
let at = |r: usize, c: usize| scores.get(r * side + c).copied();
let sub_x = refine(
column.checked_sub(1).and_then(|c| at(row, c)),
(column + 1 < side).then(|| at(row, column + 1)).flatten(),
);
let sub_y = refine(
row.checked_sub(1).and_then(|r| at(r, column)),
(row + 1 < side).then(|| at(row + 1, column)).flatten(),
);
let radius = f64::from(u32::try_from(radius).unwrap_or(0));
let whole = |index: usize| f64::from(u32::try_from(index).unwrap_or(0)) - radius;
[whole(column) + sub_x, whole(row) + sub_y]
}