Skip to main content

apple_vision/ffi/
mod.rs

1//! Raw FFI declarations matching the Swift bridge in
2//! `swift-bridge/Sources/VisionBridge/Vision.swift`.
3
4#![allow(missing_docs, non_camel_case_types)]
5
6use core::ffi::{c_char, c_void};
7
8/// Mirrors `VNRecognizedTextRaw` in Vision.swift. Layout-compatible.
9#[repr(C)]
10pub struct RecognizedTextRaw {
11    pub text: *mut c_char,
12    pub confidence: f32,
13    pub bbox_x: f64,
14    pub bbox_y: f64,
15    pub bbox_w: f64,
16    pub bbox_h: f64,
17}
18
19/// Mirrors `VNDetectedFaceRaw` in Vision.swift. Layout-compatible.
20#[repr(C)]
21pub struct DetectedFaceRaw {
22    pub bbox_x: f64,
23    pub bbox_y: f64,
24    pub bbox_w: f64,
25    pub bbox_h: f64,
26    pub confidence: f32,
27    pub roll: f32,
28    pub yaw: f32,
29    pub pitch: f32,
30}
31
32/// Mirrors `VNDetectedBarcodeRaw` in Vision.swift. Layout-compatible.
33#[repr(C)]
34pub struct DetectedBarcodeRaw {
35    pub payload: *mut c_char,
36    pub symbology: *mut c_char,
37    pub confidence: f32,
38    pub bbox_x: f64,
39    pub bbox_y: f64,
40    pub bbox_w: f64,
41    pub bbox_h: f64,
42}
43
44/// Mirrors `VNSaliencyRegionRaw` in Vision.swift. Layout-compatible.
45#[repr(C)]
46pub struct SaliencyRegionRaw {
47    pub confidence: f32,
48    pub bbox_x: f64,
49    pub bbox_y: f64,
50    pub bbox_w: f64,
51    pub bbox_h: f64,
52}
53
54/// Mirrors `VNFaceLandmarksRaw` in Vision.swift. Layout-compatible.
55///
56/// All `*_count` fields are NUMBER OF POINTS; each point buffer is an
57/// interleaved `[x0, y0, x1, y1, …]` array of doubles, length
58/// `count * 2`. A NULL pointer + 0 count means the region wasn't
59/// produced for this face.
60#[repr(C)]
61pub struct FaceLandmarksRaw {
62    pub bbox_x: f64,
63    pub bbox_y: f64,
64    pub bbox_w: f64,
65    pub bbox_h: f64,
66    pub confidence: f32,
67    pub roll: f32,
68    pub yaw: f32,
69    pub pitch: f32,
70
71    pub face_contour: *mut f64,
72    pub face_contour_count: usize,
73    pub left_eye: *mut f64,
74    pub left_eye_count: usize,
75    pub right_eye: *mut f64,
76    pub right_eye_count: usize,
77    pub left_eyebrow: *mut f64,
78    pub left_eyebrow_count: usize,
79    pub right_eyebrow: *mut f64,
80    pub right_eyebrow_count: usize,
81    pub nose: *mut f64,
82    pub nose_count: usize,
83    pub nose_crest: *mut f64,
84    pub nose_crest_count: usize,
85    pub median_line: *mut f64,
86    pub median_line_count: usize,
87    pub outer_lips: *mut f64,
88    pub outer_lips_count: usize,
89    pub inner_lips: *mut f64,
90    pub inner_lips_count: usize,
91    pub left_pupil: *mut f64,
92    pub left_pupil_count: usize,
93    pub right_pupil: *mut f64,
94    pub right_pupil_count: usize,
95}
96
97/// Mirrors `VNPoseObservationRaw` in Vision.swift. Layout-compatible.
98#[repr(C)]
99pub struct PoseObservationRaw {
100    pub bbox_x: f64,
101    pub bbox_y: f64,
102    pub bbox_w: f64,
103    pub bbox_h: f64,
104    pub confidence: f32,
105    pub joint_names: *mut *mut c_char,
106    pub joint_xs: *mut f64,
107    pub joint_ys: *mut f64,
108    pub joint_confidences: *mut f32,
109    pub joint_count: usize,
110}
111
112/// Mirrors `VNContourRaw` in Vision.swift. Layout-compatible.
113#[repr(C)]
114pub struct ContourRaw {
115    pub point_xs: *mut f64,
116    pub point_ys: *mut f64,
117    pub point_count: usize,
118    pub child_count: isize,
119    pub aspect_ratio: f32,
120}
121
122/// Mirrors `VNRecognizedAnimalRaw` in Vision.swift. Layout-compatible.
123#[repr(C)]
124pub struct RecognizedAnimalRaw {
125    pub identifier: *mut c_char,
126    pub confidence: f32,
127    pub bbox_x: f64,
128    pub bbox_y: f64,
129    pub bbox_w: f64,
130    pub bbox_h: f64,
131}
132
133extern "C" {
134    pub fn vn_string_free(s: *mut c_char);
135
136    pub fn vn_recognize_text_in_path(
137        path: *const c_char,
138        recognition_level: i32,
139        uses_language_correction: bool,
140        out_array: *mut *mut c_void,
141        out_count: *mut usize,
142        out_error_message: *mut *mut c_char,
143    ) -> i32;
144
145    pub fn vn_recognize_text_in_pixel_buffer(
146        pixel_buffer: *mut c_void,
147        recognition_level: i32,
148        uses_language_correction: bool,
149        out_array: *mut *mut c_void,
150        out_count: *mut usize,
151        out_error_message: *mut *mut c_char,
152    ) -> i32;
153
154    pub fn vn_recognized_text_free(array: *mut c_void, count: usize);
155
156    pub fn vn_detect_faces_in_path(
157        path: *const c_char,
158        out_array: *mut *mut c_void,
159        out_count: *mut usize,
160        out_error_message: *mut *mut c_char,
161    ) -> i32;
162
163    pub fn vn_detect_faces_in_pixel_buffer(
164        pixel_buffer: *mut c_void,
165        out_array: *mut *mut c_void,
166        out_count: *mut usize,
167        out_error_message: *mut *mut c_char,
168    ) -> i32;
169
170    pub fn vn_detected_faces_free(array: *mut c_void, count: usize);
171
172    pub fn vn_detect_barcodes_in_path(
173        path: *const c_char,
174        out_array: *mut *mut c_void,
175        out_count: *mut usize,
176        out_error_message: *mut *mut c_char,
177    ) -> i32;
178
179    pub fn vn_detected_barcodes_free(array: *mut c_void, count: usize);
180
181    pub fn vn_attention_saliency_in_path(
182        path: *const c_char,
183        out_array: *mut *mut c_void,
184        out_count: *mut usize,
185        out_error_message: *mut *mut c_char,
186    ) -> i32;
187
188    pub fn vn_saliency_regions_free(array: *mut c_void, count: usize);
189
190    pub fn vn_detect_face_landmarks_in_path(
191        path: *const c_char,
192        out_array: *mut *mut c_void,
193        out_count: *mut usize,
194        out_error_message: *mut *mut c_char,
195    ) -> i32;
196
197    pub fn vn_face_landmarks_free(array: *mut c_void, count: usize);
198
199    pub fn vn_detect_human_body_pose_in_path(
200        path: *const c_char,
201        out_array: *mut *mut c_void,
202        out_count: *mut usize,
203        out_error_message: *mut *mut c_char,
204    ) -> i32;
205
206    pub fn vn_detect_human_hand_pose_in_path(
207        path: *const c_char,
208        max_hands: usize,
209        out_array: *mut *mut c_void,
210        out_count: *mut usize,
211        out_error_message: *mut *mut c_char,
212    ) -> i32;
213
214    pub fn vn_pose_observations_free(array: *mut c_void, count: usize);
215
216    pub fn vn_detect_contours_in_path(
217        path: *const c_char,
218        contrast_adjustment: f32,
219        detects_dark_on_light: bool,
220        out_array: *mut *mut c_void,
221        out_count: *mut usize,
222        out_error_message: *mut *mut c_char,
223    ) -> i32;
224
225    pub fn vn_contours_free(array: *mut c_void, count: usize);
226
227    pub fn vn_recognize_animals_in_path(
228        path: *const c_char,
229        out_array: *mut *mut c_void,
230        out_count: *mut usize,
231        out_error_message: *mut *mut c_char,
232    ) -> i32;
233
234    pub fn vn_recognized_animals_free(array: *mut c_void, count: usize);
235
236    pub fn vn_test_helper_render_text_png(
237        text: *const c_char,
238        width: i32,
239        height: i32,
240        output_path: *const c_char,
241    ) -> i32;
242}
243
244pub mod status {
245    pub const OK: i32 = 0;
246    pub const INVALID_ARGUMENT: i32 = -1;
247    pub const IMAGE_LOAD_FAILED: i32 = -2;
248    pub const REQUEST_FAILED: i32 = -3;
249    pub const UNKNOWN: i32 = -99;
250}
251
252// silence unused
253const _: () = {
254    let _ = core::mem::size_of::<*mut c_void>();
255};