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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
extern crate libc;
use self::libc::{c_void, c_char};
use std::ptr;
use std::mem;
use std::ffi::CString;
use std::ops::Deref;
use std::slice;

#[repr(C)]
#[derive(PartialEq, Debug)]
enum OniStatus {
	Ok             = 0,
	Error          = 1,
	NotImplemented = 2,
	NotSupported   = 3,
	BadParameter   = 4,
	OutOfFlow      = 5,
	NoDevice       = 6,
	TimeOut        = 102,
}

#[repr(C)]
#[derive(Debug)]
pub enum OniError {
	Error          = 1,
	NotImplemented = 2,
	NotSupported   = 3,
	BadParameter   = 4,
	OutOfFlow      = 5,
	NoDevice       = 6,
	TimeOut        = 102,
}

#[repr(C)]
#[derive(Debug)]
pub enum OniSensorType {
	IR    = 1,
	Color = 2,
	Depth = 3,

}

#[repr(C)]
#[derive(Debug)]
pub enum OniPixelFormat {
	// Depth
	Depth1mm   = 100,
	Depth100um = 101,
	Shift92    = 102,
	Shift93    = 103,

	// Color
	RGB888     = 200,
	YUV422     = 201,
	Gray8      = 202,
	Gray16     = 203,
	JPEG       = 204,
	YUYV       = 205,
}

#[repr(C)]
#[derive(Debug)]
pub struct OniFrame {
	pub dataSize: i32,
	data: *mut c_void,

	sensorType: OniSensorType,
	timestamp: u64,
	frameIndex: i32,

	pub width: i32,
	pub height: i32,

	videoMode: OniVideoMode,
	croppingEnabled: i32,
	cropOriginX: i32,
	cropOriginY: i32,

	stride: i32,
}

#[repr(C)]
#[derive(Debug)]
pub struct OniVideoMode {
	pixelFormat: OniPixelFormat,
	resolutionX: i32,
	resolutionY: i32,
	fps: i32,
}

#[link(name = "OpenNI2")]
extern "C" {
    fn oniInitialize(apiVersion: i32) -> OniStatus;
    fn oniShutdown();
    fn oniDeviceOpen(uri: *const c_char, device: *mut *mut c_void) -> OniStatus;
    fn oniDeviceClose(device: *mut c_void) -> OniStatus;
    fn oniDeviceCreateStream(device: *mut c_void, sensorType: OniSensorType, pStream: *mut *mut c_void) -> OniStatus;

    fn oniStreamStart(stream: *mut c_void) -> OniStatus;
    fn oniStreamReadFrame(stream: *mut c_void, pFrame: *mut *mut OniFrame) -> OniStatus;
    fn oniStreamStop(stream: *mut c_void);
    fn oniStreamDestroy(stream: *mut c_void);

    fn oniFrameRelease(pFrame: *mut OniFrame);
}

macro_rules! status2result {
    ($code:expr) => { status2result!($code, ()) };
    ($code:expr, $ret:expr) => {
        match $code {
            OniStatus::Ok => Ok($ret),
            other => Err(unsafe { mem::transmute(other) }) // TODO make this safe
        }
    }
}

pub fn initialize() -> Result<(),OniError> {
    status2result!(unsafe { oniInitialize(2) })
}

pub fn shutdown() {
    unsafe { oniShutdown() }
}

#[derive(Debug)]
pub struct Device {
    pdev: *mut c_void
}

#[derive(Debug)]
pub struct VideoStream {
    pvs: *mut c_void
}

#[derive(Debug)]
pub struct Frame {
    pf: *mut OniFrame
}

macro_rules! c_str {
    ($s:expr) => {
        CString::new($s).unwrap().as_ptr()
    }
}

impl Device {
    pub fn new(uri: Option<&str>) -> Result<Device,OniError> {
        let mut dev = Device { pdev: ptr::null_mut() };
        let c_uri = match uri {
            Some(u) => c_str!(u),
            None    => ptr::null()
        };
        status2result!(unsafe { oniDeviceOpen(c_uri, &mut dev.pdev) }, dev)
    }

    pub fn close(&mut self) {
        unsafe { oniDeviceClose(self.pdev); } // TODO this returns an error which I am ignoring
    }

    pub unsafe fn null() -> Device {
        Device { pdev: ptr::null_mut() }
    }
}

impl VideoStream {
    pub fn new(dev: &Device, sensor_type: OniSensorType) -> Result<VideoStream,OniError> {
        let mut vs = VideoStream { pvs: ptr::null_mut() };
        status2result!(unsafe { oniDeviceCreateStream(dev.pdev, sensor_type, &mut vs.pvs) }, vs)
    }
    pub unsafe fn null() -> VideoStream {
        VideoStream { pvs: ptr::null_mut() }
    }
    pub fn start(&self) -> Result<(),OniError> {
        status2result!(unsafe { oniStreamStart(self.pvs) })
    }
    pub fn readFrame(&self) -> Result<Frame,OniError> {
        let mut pframe: *mut OniFrame = ptr::null_mut();
        try!(status2result!(unsafe { oniStreamReadFrame(self.pvs, &mut pframe) }));
        Ok(Frame { pf: unsafe { ptr::read(&pframe) } })
    }
    pub fn stop(&self) {
        unsafe { oniStreamStop(self.pvs) }
    }
    pub fn destroy(&self) {
        unsafe { oniStreamDestroy(self.pvs) }
    }
}

impl Frame {
    pub fn data<T>(&self) -> &[T] {
        &unsafe { slice::from_raw_parts(self.data as *const T, self.dataSize as usize) }
    }
}

impl Deref for Frame {
    type Target = OniFrame;

    fn deref(&self) -> &OniFrame {
        unsafe { &*self.pf }
    }
}

impl Drop for Frame {
    fn drop(&mut self) {
        unsafe { oniFrameRelease(self.pf) }
    }
}