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
use super::*;
use std::ptr;

pub struct Image<'a> {
    factory: &'a Factory,
    pub(crate) handle: k4a_image_t,
}

impl Image<'_> {
    pub(crate) fn from_handle(factory: &Factory, handle: k4a_image_t) -> Image {
        Image {
            factory: factory,
            handle: handle,
        }
    }

    /// Create a blank image
    pub fn with_format(
        factory: &Factory,
        format: k4a_image_format_t,
        width_pixels: i32,
        height_pixels: i32,
        stride_bytes: i32,
    ) -> Result<Image, Error> {
        let mut handle: k4a_image_t = ptr::null_mut();
        Error::from((factory.k4a_image_create)(
            format,
            width_pixels,
            height_pixels,
            stride_bytes,
            &mut handle,
        ))
        .to_result_fn(&|| Image::from_handle(factory, handle))
    }

    /// Create an image from a pre-allocated buffer
    pub fn with_buffer(
        factory: &Factory,
        format: k4a_image_format_t,
        width_pixels: i32,
        height_pixels: i32,
        stride_bytes: i32,
        buffer: *mut u8,
        buffer_size: usize,
        buffer_release_cb: k4a_memory_destroy_cb_t,
        buffer_release_cb_context: *mut (),
    ) -> Result<Image, Error> {
        let mut handle: k4a_image_t = ptr::null_mut();
        Error::from((factory.k4a_image_create_from_buffer)(
            format,
            width_pixels,
            height_pixels,
            stride_bytes,
            buffer,
            buffer_size,
            buffer_release_cb,
            buffer_release_cb_context,
            &mut handle,
        ))
        .to_result_fn(&|| Image::from_handle(factory, handle))
    }

    /// Get the image buffer
    pub fn get_buffer(&self) -> *const u8 {
        (self.factory.k4a_image_get_buffer)(self.handle)
    }

    /// Get the mutable image buffer
    pub fn get_mut_buffer(&mut self) -> *mut u8 {
        (self.factory.k4a_image_get_buffer)(self.handle)
    }

    /// Get the image buffer size in bytes
    pub fn get_size(&self) -> usize {
        (self.factory.k4a_image_get_size)(self.handle)
    }

    /// Get the image format of the image
    pub fn get_format(&self) -> k4a_image_format_t {
        (self.factory.k4a_image_get_format)(self.handle)
    }

    /// Get the image width in pixels
    pub fn get_width_pixels(&self) -> i32 {
        (self.factory.k4a_image_get_width_pixels)(self.handle)
    }

    /// Get the image height in pixels
    pub fn get_height_pixels(&self) -> i32 {
        (self.factory.k4a_image_get_height_pixels)(self.handle)
    }

    /// Get the image stride in bytes
    pub fn get_stride_bytes(&self) -> i32 {
        (self.factory.k4a_image_get_stride_bytes)(self.handle)
    }

    /// Get the image's device timestamp in microseconds
    pub fn get_device_timestamp_usec(&self) -> u64 {
        (self.factory.k4a_image_get_device_timestamp_usec)(self.handle)
    }

    /// Get the image's system timestamp in nanoseconds
    pub fn get_system_timestamp_nsec(&self) -> u64 {
        (self.factory.k4a_image_get_system_timestamp_nsec)(self.handle)
    }

    /// Get the image exposure time in microseconds
    pub fn get_exposure_usec(&self) -> u64 {
        (self.factory.k4a_image_get_exposure_usec)(self.handle)
    }

    /// Get the image white balance in Kelvin (color images only)
    pub fn get_white_balance(&self) -> u32 {
        (self.factory.k4a_image_get_white_balance)(self.handle)
    }

    /// Get the image's ISO speed (color images only)
    pub fn get_iso_speed(&self) -> u32 {
        (self.factory.k4a_image_get_iso_speed)(self.handle)
    }

    /// Set the image's device timestamp in microseconds
    pub fn set_device_timestamp_usec(&mut self, timestamp: u64) {
        (self.factory.k4a_image_set_device_timestamp_usec)(self.handle, timestamp)
    }

    /// Set the image's system timestamp in nanoseconds
    pub fn set_system_timestamp_nsec(&self, timestamp: u64) {
        (self.factory.k4a_image_set_system_timestamp_nsec)(self.handle, timestamp)
    }

    /// Set the image exposure time in microseconds
    pub fn set_exposure_usec(&mut self, exposure: u64) {
        (self.factory.k4a_image_set_exposure_usec)(self.handle, exposure)
    }

    /// Set the image white balance in Kelvin (color images only)
    pub fn set_white_balance(&mut self, white_balance: u32) {
        (self.factory.k4a_image_set_white_balance)(self.handle, white_balance)
    }

    /// Set the image's ISO speed (color images only)
    pub fn set_iso_speed(&mut self, iso_speed: u32) {
        (self.factory.k4a_image_set_iso_speed)(self.handle, iso_speed)
    }
}

impl Drop for Image<'_> {
    fn drop(&mut self) {
        (self.factory.k4a_image_release)(self.handle);
        self.handle = ptr::null_mut();
    }
}

impl Clone for Image<'_> {
    fn clone(&self) -> Self {
        (self.factory.k4a_image_reference)(self.handle);
        Image::from_handle(self.factory, self.handle)
    }
}