apple_vision/contours/
mod.rs1use core::ffi::c_char;
4use core::ptr;
5use std::ffi::CString;
6use std::path::Path;
7
8use crate::error::{from_swift, VisionError};
9use crate::ffi;
10use crate::face_landmarks::LandmarkPoint;
11
12#[derive(Debug, Clone, PartialEq)]
15pub struct Contour {
16 pub points: Vec<LandmarkPoint>,
18 pub child_count: isize,
20 pub aspect_ratio: f32,
22}
23
24#[derive(Debug, Clone, Copy)]
26pub struct ContourOptions {
27 pub contrast_adjustment: f32,
30 pub detects_dark_on_light: bool,
33}
34
35impl Default for ContourOptions {
36 fn default() -> Self {
37 Self {
38 contrast_adjustment: 2.0,
39 detects_dark_on_light: true,
40 }
41 }
42}
43
44pub fn detect_contours_in_path(
52 path: impl AsRef<Path>,
53 options: ContourOptions,
54) -> Result<Vec<Contour>, VisionError> {
55 let path_str = path
56 .as_ref()
57 .to_str()
58 .ok_or_else(|| VisionError::InvalidArgument("non-UTF-8 path".into()))?;
59 let path_c = CString::new(path_str)
60 .map_err(|e| VisionError::InvalidArgument(format!("path NUL byte: {e}")))?;
61
62 let mut out_array: *mut core::ffi::c_void = ptr::null_mut();
63 let mut out_count: usize = 0;
64 let mut err_msg: *mut c_char = ptr::null_mut();
65 let status = unsafe {
66 ffi::vn_detect_contours_in_path(
67 path_c.as_ptr(),
68 options.contrast_adjustment,
69 options.detects_dark_on_light,
70 &mut out_array,
71 &mut out_count,
72 &mut err_msg,
73 )
74 };
75 if status != ffi::status::OK {
76 return Err(unsafe { from_swift(status, err_msg) });
77 }
78 if out_array.is_null() || out_count == 0 {
79 return Ok(Vec::new());
80 }
81 let typed = out_array.cast::<ffi::ContourRaw>();
82 let mut v = Vec::with_capacity(out_count);
83 for i in 0..out_count {
84 let raw = unsafe { &*typed.add(i) };
85 let mut pts = Vec::with_capacity(raw.point_count);
86 for k in 0..raw.point_count {
87 let x = unsafe { *raw.point_xs.add(k) };
88 let y = unsafe { *raw.point_ys.add(k) };
89 pts.push(LandmarkPoint { x, y });
90 }
91 v.push(Contour {
92 points: pts,
93 child_count: raw.child_count,
94 aspect_ratio: raw.aspect_ratio,
95 });
96 }
97 unsafe { ffi::vn_contours_free(out_array, out_count) };
98 Ok(v)
99}