1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use failure::Error;
use mat::*;
use std::ffi::CString;
use std::mem;
use std::os::raw::{c_char, c_int, c_void};
use std::ptr;
extern "C" {
fn cv_named_window(name: *const c_char, flags: WindowFlag);
fn cv_destroy_window(name: *const c_char);
fn cv_set_mouse_callback(
name: *const c_char,
on_mouse: extern "C" fn(e: MouseEventType, x: c_int, y: c_int, f: c_int, data: *mut c_void),
userdata: *mut c_void,
);
fn cv_imshow(name: *const c_char, cmat: *mut CMat);
fn cv_wait_key(delay_ms: c_int) -> c_int;
}
pub fn highgui_named_window(name: &str, flags: WindowFlag) -> Result<(), Error> {
let s = CString::new(name)?;
unsafe {
cv_named_window(s.as_ptr(), flags);
}
Ok(())
}
pub fn highgui_destroy_window(name: &str) {
let s = CString::new(name).unwrap();
unsafe {
cv_destroy_window((&s).as_ptr());
}
}
pub type MouseCallbackData = *mut c_void;
pub type MouseCallback = fn(MouseEventType, c_int, c_int, c_int, MouseCallbackData);
pub fn highgui_set_mouse_callback(name: &str, on_mouse: MouseCallback, user_data: *mut c_void) -> Result<(), Error> {
struct CallbackWrapper {
cb: Box<MouseCallback>,
data: *mut c_void,
}
extern "C" fn _mouse_callback(e: MouseEventType, x: c_int, y: c_int, f: c_int, ud: *mut c_void) {
let cb_wrapper = unsafe { ptr::read(ud as *mut CallbackWrapper) };
let true_callback = *(cb_wrapper.cb);
true_callback(e, x, y, f, cb_wrapper.data);
mem::forget(cb_wrapper.cb);
}
let box_wrapper: Box<CallbackWrapper> = Box::new(CallbackWrapper {
cb: Box::new(on_mouse),
data: user_data,
});
let box_wrapper_raw = Box::into_raw(box_wrapper) as *mut c_void;
let s = CString::new(name)?;
unsafe {
cv_set_mouse_callback(s.as_ptr(), _mouse_callback, box_wrapper_raw);
}
Ok(())
}
#[repr(C)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum WindowFlag {
Normal = 0x00000000,
Autosize = 0x00000001,
Opengl = 0x00001000,
FreeRatio = 0x00000100,
}
#[repr(C)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum MouseEventType {
MouseMove = 0,
LButtonDown = 1,
RButtonDown = 2,
MButtonDown = 3,
LButtonUp = 4,
RButtonUp = 5,
MButtonUp = 6,
LButtonClick = 7,
RButtonClick = 8,
MButtonClick = 9,
MouseWheel = 10,
MouseHWheel = 11,
}
pub trait Show {
fn show(&self, name: &str, delay: c_int) -> Result<(), Error>;
}
impl Show for Mat {
fn show(&self, name: &str, delay: c_int) -> Result<(), Error> {
let s = CString::new(name)?;
unsafe {
cv_imshow((&s).as_ptr(), self.inner);
cv_wait_key(delay);
}
Ok(())
}
}