embrace raw pointers for ffi

This commit is contained in:
Jules Youngberg 2022-06-15 20:01:41 -07:00
parent d310625503
commit b82a7ff370
2 changed files with 20 additions and 87 deletions

View File

@ -116,75 +116,6 @@ fn bindgen_test_layout_mediagraph_Output() {
); );
} }
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct mediagraph_Feature {
pub data: *mut mediagraph_Landmark,
}
#[test]
fn bindgen_test_layout_mediagraph_Feature() {
assert_eq!(
::std::mem::size_of::<mediagraph_Feature>(),
8usize,
concat!("Size of: ", stringify!(mediagraph_Feature))
);
assert_eq!(
::std::mem::align_of::<mediagraph_Feature>(),
8usize,
concat!("Alignment of ", stringify!(mediagraph_Feature))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<mediagraph_Feature>())).data as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(mediagraph_Feature),
"::",
stringify!(data)
)
);
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct mediagraph_FeatureList {
pub num_features: u8,
pub features: *mut mediagraph_Feature,
}
#[test]
fn bindgen_test_layout_mediagraph_FeatureList() {
assert_eq!(
::std::mem::size_of::<mediagraph_FeatureList>(),
16usize,
concat!("Size of: ", stringify!(mediagraph_FeatureList))
);
assert_eq!(
::std::mem::align_of::<mediagraph_FeatureList>(),
8usize,
concat!("Alignment of ", stringify!(mediagraph_FeatureList))
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<mediagraph_FeatureList>())).num_features as *const _ as usize
},
0usize,
concat!(
"Offset of field: ",
stringify!(mediagraph_FeatureList),
"::",
stringify!(num_features)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<mediagraph_FeatureList>())).features as *const _ as usize },
8usize,
concat!(
"Offset of field: ",
stringify!(mediagraph_FeatureList),
"::",
stringify!(features)
)
);
}
#[repr(C)]
pub struct mediagraph_Detector__bindgen_vtable(::std::os::raw::c_void); pub struct mediagraph_Detector__bindgen_vtable(::std::os::raw::c_void);
#[repr(C)] #[repr(C)]
#[derive(Debug)] #[derive(Debug)]
@ -227,13 +158,14 @@ extern "C" {
pub fn mediagraph_Detector_Detector_destructor(this: *mut mediagraph_Detector); pub fn mediagraph_Detector_Detector_destructor(this: *mut mediagraph_Detector);
} }
extern "C" { extern "C" {
#[link_name = "\u{1}__ZN10mediagraph8Detector7ProcessEPhii"] #[link_name = "\u{1}__ZN10mediagraph8Detector7ProcessEPhiiS1_"]
pub fn mediagraph_Detector_Process( pub fn mediagraph_Detector_Process(
this: *mut ::std::os::raw::c_void, this: *mut ::std::os::raw::c_void,
data: *mut u8, data: *mut u8,
width: ::std::os::raw::c_int, width: ::std::os::raw::c_int,
height: ::std::os::raw::c_int, height: ::std::os::raw::c_int,
) -> *mut mediagraph_FeatureList; num_features: *mut u8,
) -> *mut mediagraph_Landmark;
} }
#[repr(C)] #[repr(C)]
pub struct mediagraph_Effect__bindgen_vtable(::std::os::raw::c_void); pub struct mediagraph_Effect__bindgen_vtable(::std::os::raw::c_void);

View File

@ -24,8 +24,6 @@ use bindings::*;
type mFeatureType = mediagraph_FeatureType; type mFeatureType = mediagraph_FeatureType;
type mOutput = mediagraph_Output; type mOutput = mediagraph_Output;
type mFeature = mediagraph_Feature;
type mFeatureList = mediagraph_FeatureList;
/// The type of visual feature made up of landmarks. /// The type of visual feature made up of landmarks.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -161,33 +159,36 @@ impl Detector {
/// Processes the input frame, returns a slice of landmarks if any are detected. /// Processes the input frame, returns a slice of landmarks if any are detected.
pub fn process(&mut self, input: &Mat) -> Vec<Vec<Vec<Landmark>>> { pub fn process(&mut self, input: &Mat) -> Vec<Vec<Vec<Landmark>>> {
let mut data = input.clone(); let mut data = input.clone();
let mut num_features = vec![0; self.outputs.len()];
let results = unsafe { let results = unsafe {
let nf_data = num_features.as_mut_slice();
mediagraph_Detector_Process( mediagraph_Detector_Process(
self.graph as *mut std::ffi::c_void, self.graph as *mut std::ffi::c_void,
data.data_mut(), data.data_mut(),
data.cols(), data.cols(),
data.rows(), data.rows(),
nf_data.as_mut_ptr(),
) )
}; };
let mut landmarks = vec![]; let mut landmarks = vec![];
let mut data_index = 0;
let feature_lists = for (i, &count) in num_features.iter().enumerate() {
unsafe { std::slice::from_raw_parts(results, self.outputs.len() as usize) };
for (i, feature_list) in feature_lists.iter().enumerate() {
let num_landmarks = self.outputs[i].type_.num_landmarks();
let mut fl = vec![]; let mut fl = vec![];
let features = unsafe { if count == 0 {
std::slice::from_raw_parts( landmarks.push(fl);
feature_list.features, continue;
feature_list.num_features as usize, }
)
};
for feature in features.iter() { let num_landmarks = self.outputs[i].type_.num_landmarks();
let landmarks = unsafe { std::slice::from_raw_parts(feature.data, num_landmarks) };
fl.push(landmarks.to_vec()); for _ in 0..count {
let l =
unsafe { std::slice::from_raw_parts(results.add(data_index), num_landmarks) };
data_index += num_landmarks;
fl.push(l.to_vec());
} }
landmarks.push(fl); landmarks.push(fl);