use core::ffi::c_char;
use core::ptr;
use std::ffi::CString;
use std::path::Path;
use crate::error::{from_swift, VisionError};
use crate::ffi;
use crate::face_landmarks::LandmarkPoint;
#[derive(Debug, Clone, PartialEq)]
pub struct Contour {
pub points: Vec<LandmarkPoint>,
pub child_count: isize,
pub aspect_ratio: f32,
}
#[derive(Debug, Clone, Copy)]
pub struct ContourOptions {
pub contrast_adjustment: f32,
pub detects_dark_on_light: bool,
}
impl Default for ContourOptions {
fn default() -> Self {
Self {
contrast_adjustment: 2.0,
detects_dark_on_light: true,
}
}
}
pub fn detect_contours_in_path(
path: impl AsRef<Path>,
options: ContourOptions,
) -> Result<Vec<Contour>, VisionError> {
let path_str = path
.as_ref()
.to_str()
.ok_or_else(|| VisionError::InvalidArgument("non-UTF-8 path".into()))?;
let path_c = CString::new(path_str)
.map_err(|e| VisionError::InvalidArgument(format!("path NUL byte: {e}")))?;
let mut out_array: *mut core::ffi::c_void = ptr::null_mut();
let mut out_count: usize = 0;
let mut err_msg: *mut c_char = ptr::null_mut();
let status = unsafe {
ffi::vn_detect_contours_in_path(
path_c.as_ptr(),
options.contrast_adjustment,
options.detects_dark_on_light,
&mut out_array,
&mut out_count,
&mut err_msg,
)
};
if status != ffi::status::OK {
return Err(unsafe { from_swift(status, err_msg) });
}
if out_array.is_null() || out_count == 0 {
return Ok(Vec::new());
}
let typed = out_array.cast::<ffi::ContourRaw>();
let mut v = Vec::with_capacity(out_count);
for i in 0..out_count {
let raw = unsafe { &*typed.add(i) };
let mut pts = Vec::with_capacity(raw.point_count);
for k in 0..raw.point_count {
let x = unsafe { *raw.point_xs.add(k) };
let y = unsafe { *raw.point_ys.add(k) };
pts.push(LandmarkPoint { x, y });
}
v.push(Contour {
points: pts,
child_count: raw.child_count,
aspect_ratio: raw.aspect_ratio,
});
}
unsafe { ffi::vn_contours_free(out_array, out_count) };
Ok(v)
}