orbbec_sdk/
frame.rs

1//! Frame module
2use crate::{Format, error::OrbbecError, sys::frame::OBFrame};
3
4/// Frame trait
5pub trait Frame: From<OBFrame> + AsRef<OBFrame> {}
6
7/// Single video frame
8pub struct VideoFrame {
9    inner: OBFrame,
10}
11
12impl VideoFrame {
13    /// Get the raw data of the video frame
14    pub fn raw_data(&self) -> &[u8] {
15        // Unwrap is safe here because internal pointer is guaranteed to be valid
16        // SDK only returns error for this function if pointer is NULL
17        // Ref: https://github.com/orbbec/OrbbecSDK_v2/blob/815ae047cc977a1f7edd2b97b69ff6cd29f510b3/src/impl/Frame.cpp#L219
18        self.inner.get_data().unwrap()
19    }
20
21    /// Get the width of the video frame
22    pub fn width(&self) -> u16 {
23        // Unwrap is safe here because internal pointer is guaranteed to be valid and a video frame
24        // SDK only returns error for this function if pointer is NULL or not a video frame
25        // Ref: https://github.com/orbbec/OrbbecSDK_v2/blob/815ae047cc977a1f7edd2b97b69ff6cd29f510b3/src/impl/Frame.cpp#L129
26        self.inner.get_video_width().map(|w| w as u16).unwrap()
27    }
28
29    /// Get the height of the video frame
30    pub fn height(&self) -> u16 {
31        // Unwrap is safe here because internal pointer is guaranteed to be valid and a video frame
32        // SDK only returns error for this function if pointer is NULL or not a video frame
33        // Ref: https://github.com/orbbec/OrbbecSDK_v2/blob/815ae047cc977a1f7edd2b97b69ff6cd29f510b3/src/impl/Frame.cpp#L138
34        self.inner.get_video_height().map(|h| h as u16).unwrap()
35    }
36
37    /// Get the format of the video frame data
38    pub fn format(&self) -> Format {
39        // Unwrap is safe here because internal pointer is guaranteed to be valid and a video frame
40        // SDK only returns error for this function if pointer is NULL or not a video frame
41        // Ref: https://github.com/orbbec/OrbbecSDK_v2/blob/815ae047cc977a1f7edd2b97b69ff6cd29f510b3/src/impl/Frame.cpp#L147
42        self.inner.get_format().unwrap()
43    }
44}
45
46impl From<OBFrame> for VideoFrame {
47    fn from(frame: OBFrame) -> Self {
48        VideoFrame { inner: frame }
49    }
50}
51
52impl AsRef<OBFrame> for VideoFrame {
53    fn as_ref(&self) -> &OBFrame {
54        &self.inner
55    }
56}
57
58impl Frame for VideoFrame {}
59
60/// A container of multiple frames.
61pub struct FrameSet {
62    inner: OBFrame,
63}
64
65impl FrameSet {
66    /// Get the depth frame from the frameset
67    pub fn get_depth_frame(&self) -> Result<Option<VideoFrame>, OrbbecError> {
68        self.inner
69            .get_depth_frame()
70            .map_err(OrbbecError::from)
71            .map(|frame| frame.map(VideoFrame::from))
72    }
73
74    /// Get the color frame from the frameset
75    pub fn get_color_frame(&self) -> Result<Option<VideoFrame>, OrbbecError> {
76        self.inner
77            .get_color_frame()
78            .map_err(OrbbecError::from)
79            .map(|frame| frame.map(VideoFrame::from))
80    }
81}
82
83impl From<OBFrame> for FrameSet {
84    fn from(frame: OBFrame) -> Self {
85        FrameSet { inner: frame }
86    }
87}
88
89impl AsRef<OBFrame> for FrameSet {
90    fn as_ref(&self) -> &OBFrame {
91        &self.inner
92    }
93}
94
95impl Frame for FrameSet {}