Skip to main content

apple_vision/contours/
mod.rs

1//! Edge contour detection (`VNDetectContoursRequest`).
2
3use 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/// A single closed contour, represented as an ordered list of points
13/// in normalised image coordinates.
14#[derive(Debug, Clone, PartialEq)]
15pub struct Contour {
16    /// Vertices in path order, normalised to `0.0..=1.0`.
17    pub points: Vec<LandmarkPoint>,
18    /// Number of child (nested) contours inside this one.
19    pub child_count: isize,
20    /// Bounding-box width / height of this contour.
21    pub aspect_ratio: f32,
22}
23
24/// Options for `detect_contours_in_path`.
25#[derive(Debug, Clone, Copy)]
26pub struct ContourOptions {
27    /// `0.0..=1.0` — Apple's per-pixel contrast adjustment before
28    /// detection. Default 2.0 in the SDK; we mirror that.
29    pub contrast_adjustment: f32,
30    /// If `true`, look for dark shapes against a light background.
31    /// If `false`, light shapes against a dark background.
32    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
44/// Detect top-level contours in the image at `path`. Only the
45/// outermost (top-level) contours are returned; nested contours are
46/// counted but not enumerated.
47///
48/// # Errors
49///
50/// Returns [`VisionError::ImageLoadFailed`] / [`VisionError::RequestFailed`].
51pub 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}