azure_kinect/
image.rs

1use crate::*;
2use azure_kinect_sys::k4a::*;
3use std::ptr;
4
5pub struct Image<'a> {
6    api: &'a azure_kinect_sys::api::Api,
7    pub(crate) handle: k4a_image_t,
8}
9
10impl<'a> Image<'a> {
11    pub(crate) fn from_handle(api: &azure_kinect_sys::api::Api, handle: k4a_image_t) -> Image {
12        Image { api, handle }
13    }
14
15    /// Create a blank image
16    #[deprecated(since = "0.2", note = "Factory::image_create")]
17    pub fn with_format(
18        factory: &'a Factory,
19        format: ImageFormat,
20        width_pixels: i32,
21        height_pixels: i32,
22        stride_bytes: i32,
23    ) -> Result<Image<'a>, Error> {
24        factory.image_create(format, width_pixels, height_pixels, stride_bytes)
25    }
26
27    /// Create an image from a pre-allocated buffer
28    #[deprecated(since = "0.2", note = "Factory::image_create_from_buffer")]
29    pub fn with_buffer(
30        factory: &'a Factory,
31        format: ImageFormat,
32        width_pixels: i32,
33        height_pixels: i32,
34        stride_bytes: i32,
35        buffer: *mut u8,
36        buffer_size: usize,
37        buffer_release_cb: Option<MemoryDestroyCallback>,
38        buffer_release_cb_context: *mut (),
39    ) -> Result<Image<'a>, Error> {
40        factory.image_create_from_buffer_native(
41            format,
42            width_pixels,
43            height_pixels,
44            stride_bytes,
45            buffer,
46            buffer_size,
47            buffer_release_cb,
48            buffer_release_cb_context,
49        )
50    }
51
52    /// Get the image buffer
53    pub fn get_buffer(&self) -> *const u8 {
54        unsafe { (self.api.funcs.k4a_image_get_buffer)(self.handle) }
55    }
56
57    /// Get the mutable image buffer
58    pub fn get_mut_buffer(&mut self) -> *mut u8 {
59        unsafe { (self.api.funcs.k4a_image_get_buffer)(self.handle) }
60    }
61
62    /// Get the image buffer size in bytes
63    pub fn get_size(&self) -> usize {
64        unsafe { (self.api.funcs.k4a_image_get_size)(self.handle) }
65    }
66
67    /// Get the image format of the image
68    pub fn get_format(&self) -> ImageFormat {
69        ImageFormat::from_primitive(unsafe { (self.api.funcs.k4a_image_get_format)(self.handle) })
70    }
71
72    /// Get the image width in pixels
73    pub fn get_width_pixels(&self) -> i32 {
74        unsafe { (self.api.funcs.k4a_image_get_width_pixels)(self.handle) }
75    }
76
77    /// Get the image height in pixels
78    pub fn get_height_pixels(&self) -> i32 {
79        unsafe { (self.api.funcs.k4a_image_get_height_pixels)(self.handle) }
80    }
81
82    /// Get the image stride in bytes
83    pub fn get_stride_bytes(&self) -> i32 {
84        unsafe { (self.api.funcs.k4a_image_get_stride_bytes)(self.handle) }
85    }
86
87    /// Get the image's device timestamp in microseconds
88    pub fn get_device_timestamp_usec(&self) -> u64 {
89        unsafe { (self.api.funcs.k4a_image_get_device_timestamp_usec)(self.handle) }
90    }
91
92    /// Get the image's system timestamp in nanoseconds
93    pub fn get_system_timestamp_nsec(&self) -> u64 {
94        unsafe { (self.api.funcs.k4a_image_get_system_timestamp_nsec)(self.handle) }
95    }
96
97    /// Get the image exposure time in microseconds
98    pub fn get_exposure_usec(&self) -> u64 {
99        unsafe { (self.api.funcs.k4a_image_get_exposure_usec)(self.handle) }
100    }
101
102    /// Get the image white balance in Kelvin (color images only)
103    pub fn get_white_balance(&self) -> u32 {
104        unsafe { (self.api.funcs.k4a_image_get_white_balance)(self.handle) }
105    }
106
107    /// Get the image's ISO speed (color images only)
108    pub fn get_iso_speed(&self) -> u32 {
109        unsafe { (self.api.funcs.k4a_image_get_iso_speed)(self.handle) }
110    }
111
112    /// Set the image's device timestamp in microseconds
113    pub fn set_device_timestamp_usec(&mut self, timestamp: u64) {
114        unsafe { (self.api.funcs.k4a_image_set_device_timestamp_usec)(self.handle, timestamp) }
115    }
116
117    /// Set the image's system timestamp in nanoseconds
118    pub fn set_system_timestamp_nsec(&mut self, timestamp: u64) {
119        unsafe { (self.api.funcs.k4a_image_set_system_timestamp_nsec)(self.handle, timestamp) }
120    }
121
122    /// Set the image exposure time in microseconds
123    pub fn set_exposure_usec(&mut self, exposure: u64) {
124        unsafe { (self.api.funcs.k4a_image_set_exposure_usec)(self.handle, exposure) }
125    }
126
127    /// Set the image white balance in Kelvin (color images only)
128    pub fn set_white_balance(&mut self, white_balance: u32) {
129        unsafe { (self.api.funcs.k4a_image_set_white_balance)(self.handle, white_balance) }
130    }
131
132    /// Set the image's ISO speed (color images only)
133    pub fn set_iso_speed(&mut self, iso_speed: u32) {
134        unsafe { (self.api.funcs.k4a_image_set_iso_speed)(self.handle, iso_speed) }
135    }
136}
137
138impl Drop for Image<'_> {
139    fn drop(&mut self) {
140        unsafe {
141            (self.api.funcs.k4a_image_release)(self.handle);
142        }
143        self.handle = ptr::null_mut();
144    }
145}
146
147impl Clone for Image<'_> {
148    fn clone(&self) -> Self {
149        unsafe {
150            (self.api.funcs.k4a_image_reference)(self.handle);
151        }
152        Image::from_handle(&self.api, self.handle)
153    }
154}