azure_kinect/
transformation.rs

1#![allow(non_upper_case_globals)]
2
3use crate::enums::CalibrationType;
4use crate::*;
5use azure_kinect_sys::k4a::*;
6use std::ptr;
7
8#[allow(dead_code)]
9pub struct Transformation<'a> {
10    factory: &'a Factory,
11    handle: k4a_transformation_t,
12    color_resolution: Dimension,
13    depth_resolution: Dimension,
14}
15
16impl<'a> Transformation<'a> {
17    #[deprecated(since = "0.2", note = "Factory::transformation_create")]
18    pub fn new(factory: &'a Factory, calibration: &'a Calibration) -> Transformation<'a> {
19        factory.transformation_create(calibration)
20    }
21
22    pub(crate) fn from_handle(
23        factory: &'a Factory,
24        handle: k4a_transformation_t,
25        calibration: &'a Calibration,
26    ) -> Transformation<'a> {
27        Transformation {
28            factory,
29            handle,
30            color_resolution: Dimension {
31                width: calibration
32                    .calibration
33                    .color_camera_calibration
34                    .resolution_width,
35                height: calibration
36                    .calibration
37                    .color_camera_calibration
38                    .resolution_height,
39            },
40            depth_resolution: Dimension {
41                width: calibration
42                    .calibration
43                    .depth_camera_calibration
44                    .resolution_width,
45                height: calibration
46                    .calibration
47                    .depth_camera_calibration
48                    .resolution_height,
49            },
50        }
51    }
52
53    pub fn depth_image_to_color_camera_exist_image(
54        &self,
55        depth_image: &Image,
56        transformed_depth_image: &mut Image,
57    ) -> Result<(), Error> {
58        Error::from_k4a_result_t(unsafe {
59            (self
60                .factory
61                .api
62                .funcs
63                .k4a_transformation_depth_image_to_color_camera)(
64                self.handle,
65                depth_image.handle,
66                transformed_depth_image.handle,
67            )
68        })
69        .to_result(())
70    }
71
72    pub fn depth_image_to_color_camera(&self, depth_image: &Image) -> Result<Image, Error> {
73        let mut transformed_depth_image = self.factory.image_create(
74            ImageFormat::Depth16,
75            self.color_resolution.width,
76            self.color_resolution.height,
77            self.color_resolution.width * (std::mem::size_of::<u16>() as i32),
78        )?;
79        self.depth_image_to_color_camera_exist_image(depth_image, &mut transformed_depth_image)?;
80        Ok(transformed_depth_image)
81    }
82
83    pub fn depth_image_to_color_camera_custom_exist_image(
84        &self,
85        depth_image: &Image,
86        custom_image: &Image,
87        transformed_depth_image: &mut Image,
88        transformed_custom_image: &mut Image,
89        interpolation_type: TransformationInterpolationType,
90        invalid_custom_value: u32,
91    ) -> Result<(), Error> {
92        Error::from_k4a_result_t(unsafe {
93            (self
94                .factory
95                .api
96                .funcs
97                .k4a_transformation_depth_image_to_color_camera_custom)(
98                self.handle,
99                depth_image.handle,
100                custom_image.handle,
101                transformed_depth_image.handle,
102                transformed_custom_image.handle,
103                interpolation_type.into(),
104                invalid_custom_value,
105            )
106        })
107        .to_result(())
108    }
109
110    pub fn depth_image_to_color_camera_custom(
111        &self,
112        depth_image: &Image,
113        custom_image: &Image,
114        interpolation_type: TransformationInterpolationType,
115        invalid_custom_value: u32,
116    ) -> Result<(Image, Image), Error> {
117        let bytes_per_pixel: usize = match custom_image.get_format() {
118            ImageFormat::Custom8 => std::mem::size_of::<i8>(),
119            ImageFormat::Custom16 => std::mem::size_of::<i16>(),
120            _ => return Err(Error::Failed),
121        };
122
123        let mut transformed_depth_image = self.factory.image_create(
124            ImageFormat::Depth16,
125            self.color_resolution.width,
126            self.color_resolution.height,
127            self.color_resolution.width * (std::mem::size_of::<u16>() as i32),
128        )?;
129
130        let mut transformed_custom_image = self.factory.image_create(
131            custom_image.get_format(),
132            self.color_resolution.width,
133            self.color_resolution.height,
134            self.color_resolution.width * (bytes_per_pixel as i32),
135        )?;
136
137        self.depth_image_to_color_camera_custom_exist_image(
138            depth_image,
139            custom_image,
140            &mut transformed_depth_image,
141            &mut transformed_custom_image,
142            interpolation_type.into(),
143            invalid_custom_value,
144        )?;
145        Ok((transformed_depth_image, transformed_custom_image))
146    }
147
148    pub fn color_image_to_depth_camera_exist_image(
149        &self,
150        depth_image: &Image,
151        color_image: &Image,
152        transformed_color_image: &mut Image,
153    ) -> Result<(), Error> {
154        Error::from_k4a_result_t(unsafe {
155            (self
156                .factory
157                .api
158                .funcs
159                .k4a_transformation_color_image_to_depth_camera)(
160                self.handle,
161                depth_image.handle,
162                color_image.handle,
163                transformed_color_image.handle,
164            )
165        })
166        .to_result(())
167    }
168
169    pub fn color_image_to_depth_camera(
170        &self,
171        depth_image: &Image,
172        color_image: &Image,
173    ) -> Result<Image, Error> {
174        let mut transformed_color_image = self.factory.image_create(
175            ImageFormat::BGRA32,
176            self.color_resolution.width,
177            self.color_resolution.height,
178            self.color_resolution.width * (std::mem::size_of::<u8>() as i32) * 4,
179        )?;
180
181        self.color_image_to_depth_camera_exist_image(
182            depth_image,
183            color_image,
184            &mut transformed_color_image,
185        )?;
186        Ok(transformed_color_image)
187    }
188
189    pub fn depth_image_to_point_cloud_exist_image(
190        &self,
191        depth_image: &Image,
192        camera: CalibrationType,
193        xyz_image: &mut Image,
194    ) -> Result<(), Error> {
195        Error::from_k4a_result_t(unsafe {
196            (self
197                .factory
198                .api
199                .funcs
200                .k4a_transformation_depth_image_to_point_cloud)(
201                self.handle,
202                depth_image.handle,
203                camera.into(),
204                xyz_image.handle,
205            )
206        })
207        .to_result(())
208    }
209
210    pub fn depth_image_to_point_cloud(
211        &self,
212        depth_image: &Image,
213        camera: CalibrationType,
214    ) -> Result<Image, Error> {
215        let mut xyz_image = self.factory.image_create(
216            ImageFormat::Custom,
217            self.color_resolution.width,
218            self.color_resolution.height,
219            self.color_resolution.width * (std::mem::size_of::<u16>() as i32) * 3,
220        )?;
221        self.depth_image_to_point_cloud_exist_image(depth_image, camera, &mut xyz_image)?;
222        Ok(xyz_image)
223    }
224}
225
226impl Drop for Transformation<'_> {
227    fn drop(&mut self) {
228        unsafe {
229            (self.factory.api.funcs.k4a_transformation_destroy)(self.handle);
230        }
231        self.handle = ptr::null_mut();
232    }
233}