libde265/
image.rs

1use std::any::Any;
2
3use crate::ChromaFormat;
4
5// #[repr(C)]
6// #[derive(Debug, Copy, Clone)]
7// pub struct de265_image_spec {
8//     pub format: de265_image_format,
9//     pub width: ::std::os::raw::c_int,
10//     pub height: ::std::os::raw::c_int,
11//     pub alignment: ::std::os::raw::c_int,
12//     pub crop_left: ::std::os::raw::c_int,
13//     pub crop_right: ::std::os::raw::c_int,
14//     pub crop_top: ::std::os::raw::c_int,
15//     pub croibp_bottom: ::std::os::raw::c_int,
16//     pub visible_width: ::std::os::raw::c_int,
17//     pub visible_height: ::std::os::raw::c_int,
18// }
19
20pub struct Image {
21    pub(crate) inner: *const libde265_sys::de265_image,
22}
23
24impl Image {
25    #[inline]
26    pub fn get_image_width(&self, channel: i32) -> u32 {
27        unsafe { libde265_sys::de265_get_image_width(self.inner, channel) as u32 }
28    }
29
30    #[inline]
31    pub fn get_image_height(&self, channel: i32) -> u32 {
32        unsafe { libde265_sys::de265_get_image_height(self.inner, channel) as u32 }
33    }
34
35    #[inline]
36    pub fn get_chroma_format(&self) -> ChromaFormat {
37        ChromaFormat::try_from(unsafe { libde265_sys::de265_get_chroma_format(self.inner) })
38            .unwrap()
39    }
40
41    #[inline]
42    pub fn get_bits_per_pixel(&self, channel: i32) -> i32 {
43        unsafe { libde265_sys::de265_get_bits_per_pixel(self.inner, channel) }
44    }
45
46    #[inline]
47    pub fn get_image_plane(&self, channel: i32) -> (&[u8], usize) {
48        let mut stride = 0i32;
49        let x = unsafe { libde265_sys::de265_get_image_plane(self.inner, channel, &mut stride) };
50
51        let size = self.get_image_width(channel) * self.get_image_height(channel);
52
53        (
54            unsafe { std::slice::from_raw_parts(x, size as usize) },
55            stride as usize,
56        )
57    }
58
59    #[inline]
60    pub fn get_image_plane_user_data(&self, channel: i32) -> Option<&dyn Any> {
61        let user_data =
62            unsafe { libde265_sys::de265_get_image_plane_user_data(self.inner, channel) };
63
64        if user_data.is_null() {
65            None
66        } else {
67            Some(unsafe { &mut *user_data })
68        }
69    }
70
71    #[inline]
72    pub fn get_image_plane_user_data_mut(&mut self, channel: i32) -> Option<&mut dyn Any> {
73        let user_data =
74            unsafe { libde265_sys::de265_get_image_plane_user_data(self.inner, channel) };
75
76        if user_data.is_null() {
77            None
78        } else {
79            Some(unsafe { &mut *user_data })
80        }
81    }
82
83    #[inline]
84    pub fn get_image_user_data(&self) -> Option<&dyn Any> {
85        let user_data = unsafe { libde265_sys::de265_get_image_user_data(self.inner) };
86
87        if user_data.is_null() {
88            None
89        } else {
90            Some(unsafe { &*user_data })
91        }
92    }
93
94    #[inline]
95    pub fn get_image_user_data_mut(&mut self) -> Option<&mut dyn Any> {
96        let user_data = unsafe { libde265_sys::de265_get_image_user_data(self.inner) };
97
98        if user_data.is_null() {
99            None
100        } else {
101            Some(unsafe { &mut *user_data })
102        }
103    }
104
105    // #[inline]
106    // pub fn set_image_user_data(&mut self, user_data: *mut ::std::os::raw::c_void) {
107    //     let user_data = unsafe { libde265_sys::de265_set_image_user_data(self.inner, user_data) };
108    // }
109
110    // #[inline]
111    // pub fn set_image_plane(
112    //     &mut self,
113    //     chroma_idx: ::std::os::raw::c_int,
114    //     mem: *mut ::std::os::raw::c_void,
115    //     stride: ::std::os::raw::c_int,
116    //     userdata: *mut ::std::os::raw::c_void,
117    // ) {
118    //     let user_data = unsafe { libde265_sys::de265_set_image_plane(self.inner, user_data) };
119    // }
120
121    #[inline]
122    pub fn get_image_pts(&self) -> i64 {
123        unsafe { libde265_sys::de265_get_image_PTS(self.inner) }
124    }
125
126    #[inline]
127    pub fn get_image_nal_header(&self) {
128        let mut nal_unit_type: i32 = 0;
129        let mut nal_unit_name: *const i8 = std::ptr::null_mut();
130        let mut nuh_layer_id: i32 = 0;
131        let mut nuh_temporal_id: i32 = 0;
132
133        unsafe {
134            libde265_sys::de265_get_image_NAL_header(
135                self.inner,
136                &mut nal_unit_type,
137                &mut nal_unit_name,
138                &mut nuh_layer_id,
139                &mut nuh_temporal_id,
140            )
141        };
142    }
143
144    #[inline]
145    pub fn get_image_full_range_flag(&self) -> i32 {
146        unsafe { libde265_sys::de265_get_image_full_range_flag(self.inner) }
147    }
148
149    #[inline]
150    pub fn get_image_colour_primaries(&self) -> i32 {
151        unsafe { libde265_sys::de265_get_image_colour_primaries(self.inner) }
152    }
153
154    #[inline]
155    pub fn get_image_transfer_characteristics(&self) -> i32 {
156        unsafe { libde265_sys::de265_get_image_transfer_characteristics(self.inner) }
157    }
158
159    #[inline]
160    pub fn get_image_matrix_coefficients(&self) -> i32 {
161        unsafe { libde265_sys::de265_get_image_matrix_coefficients(self.inner) }
162    }
163}