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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use std::ffi::CString;
use std::mem::MaybeUninit;
use std::os::raw::c_char;
use std::ptr;
use four_cc::FourCC;
use libheif_sys as lh;
use crate::utils::cstr_to_str;
use crate::{
ColorProfileNCLX, ColorProfileRaw, ColorProfileType, HeifError, HeifErrorCode,
HeifErrorSubCode, Result,
};
pub struct ImageHandle {
pub(crate) inner: *mut lh::heif_image_handle,
}
pub type ItemId = lh::heif_item_id;
impl ImageHandle {
pub(crate) fn new(handle: *mut lh::heif_image_handle) -> Self {
ImageHandle { inner: handle }
}
pub fn width(&self) -> u32 {
unsafe { lh::heif_image_handle_get_width(self.inner) as _ }
}
pub fn height(&self) -> u32 {
unsafe { lh::heif_image_handle_get_height(self.inner) as _ }
}
pub fn has_alpha_channel(&self) -> bool {
unsafe { lh::heif_image_handle_has_alpha_channel(self.inner) != 0 }
}
pub fn is_premultiplied_alpha(&self) -> bool {
unsafe { lh::heif_image_handle_is_premultiplied_alpha(self.inner) != 0 }
}
pub fn is_primary(&self) -> bool {
unsafe { lh::heif_image_handle_is_primary_image(self.inner) != 0 }
}
pub fn luma_bits_per_pixel(&self) -> u8 {
unsafe { lh::heif_image_handle_get_luma_bits_per_pixel(self.inner) as _ }
}
pub fn chroma_bits_per_pixel(&self) -> u8 {
unsafe { lh::heif_image_handle_get_chroma_bits_per_pixel(self.inner) as _ }
}
pub fn ispe_width(&self) -> i32 {
unsafe { lh::heif_image_handle_get_ispe_width(self.inner) as _ }
}
pub fn ispe_height(&self) -> i32 {
unsafe { lh::heif_image_handle_get_ispe_height(self.inner) as _ }
}
pub fn has_depth_image(&self) -> bool {
unsafe { lh::heif_image_handle_has_depth_image(self.inner) != 0 }
}
pub fn number_of_depth_images(&self) -> i32 {
unsafe { lh::heif_image_handle_get_number_of_depth_images(self.inner) }
}
pub fn depth_image_ids(&self, item_ids: &mut [ItemId]) -> usize {
if item_ids.is_empty() {
0
} else {
unsafe {
lh::heif_image_handle_get_list_of_depth_image_IDs(
self.inner,
item_ids.as_mut_ptr(),
item_ids.len() as _,
) as usize
}
}
}
pub fn depth_image_handle(&self, depth_image_id: ItemId) -> Result<Self> {
let mut out_depth_handler = MaybeUninit::<_>::uninit();
let err = unsafe {
lh::heif_image_handle_get_depth_image_handle(
self.inner,
depth_image_id,
out_depth_handler.as_mut_ptr(),
)
};
HeifError::from_heif_error(err)?;
let out_depth_handler = unsafe { out_depth_handler.assume_init() };
Ok(ImageHandle {
inner: out_depth_handler,
})
}
pub fn number_of_thumbnails(&self) -> usize {
unsafe { lh::heif_image_handle_get_number_of_thumbnails(self.inner) as _ }
}
pub fn thumbnail_ids(&self, item_ids: &mut [ItemId]) -> usize {
if item_ids.is_empty() {
0
} else {
unsafe {
lh::heif_image_handle_get_list_of_thumbnail_IDs(
self.inner,
item_ids.as_mut_ptr(),
item_ids.len() as _,
) as usize
}
}
}
pub fn thumbnail(&self, thumbnail_id: ItemId) -> Result<Self> {
let mut out_thumbnail_handler = MaybeUninit::<_>::uninit();
let err = unsafe {
lh::heif_image_handle_get_thumbnail(
self.inner,
thumbnail_id,
out_thumbnail_handler.as_mut_ptr(),
)
};
HeifError::from_heif_error(err)?;
let out_thumbnail_handler = unsafe { out_thumbnail_handler.assume_init() };
Ok(ImageHandle {
inner: out_thumbnail_handler,
})
}
fn convert_type_filter<T>(type_filter: T) -> Option<CString>
where
T: Into<FourCC>,
{
let type_filter = type_filter.into();
if type_filter.0.contains(&0) {
None
} else {
CString::new(type_filter.to_string()).ok()
}
}
pub fn number_of_metadata_blocks<T>(&self, type_filter: T) -> i32
where
T: Into<FourCC>,
{
let c_type_filter = Self::convert_type_filter(type_filter);
let filter_ptr: *const c_char = match &c_type_filter {
Some(s) => s.as_ptr(),
None => ptr::null(),
};
unsafe { lh::heif_image_handle_get_number_of_metadata_blocks(self.inner, filter_ptr) }
}
pub fn metadata_block_ids<T>(&self, item_ids: &mut [ItemId], type_filter: T) -> usize
where
T: Into<FourCC>,
{
if item_ids.is_empty() {
0
} else {
let c_type_filter = Self::convert_type_filter(type_filter);
let filter_ptr: *const c_char = match &c_type_filter {
Some(s) => s.as_ptr(),
None => ptr::null(),
};
unsafe {
lh::heif_image_handle_get_list_of_metadata_block_IDs(
self.inner,
filter_ptr,
item_ids.as_mut_ptr(),
item_ids.len() as _,
) as usize
}
}
}
pub fn metadata_type(&self, metadata_id: ItemId) -> Option<&str> {
let c_type: *const c_char =
unsafe { lh::heif_image_handle_get_metadata_type(self.inner, metadata_id) };
cstr_to_str(c_type)
}
pub fn metadata_content_type(&self, metadata_id: ItemId) -> Option<&str> {
let c_type =
unsafe { lh::heif_image_handle_get_metadata_content_type(self.inner, metadata_id) };
cstr_to_str(c_type)
}
pub fn metadata_size(&self, metadata_id: ItemId) -> usize {
unsafe { lh::heif_image_handle_get_metadata_size(self.inner, metadata_id) }
}
pub fn metadata(&self, metadata_id: ItemId) -> Result<Vec<u8>> {
let size = self.metadata_size(metadata_id);
if size == 0 {
return Err(HeifError {
code: HeifErrorCode::UsageError,
sub_code: HeifErrorSubCode::NonExistingItemReferenced,
message: "".to_string(),
});
}
let mut result: Vec<u8> = Vec::with_capacity(size);
unsafe {
let err =
lh::heif_image_handle_get_metadata(self.inner, metadata_id, result.as_ptr() as _);
HeifError::from_heif_error(err)?;
result.set_len(size);
}
Ok(result)
}
pub fn color_profile_raw(&self) -> Option<ColorProfileRaw> {
let size = unsafe { lh::heif_image_handle_get_raw_color_profile_size(self.inner) };
if size == 0 {
return None;
}
let mut result: Vec<u8> = Vec::with_capacity(size);
let err = unsafe {
lh::heif_image_handle_get_raw_color_profile(self.inner, result.as_ptr() as _)
};
if err.code != 0 {
return None;
}
unsafe {
result.set_len(size);
}
let c_profile_type = unsafe { lh::heif_image_handle_get_color_profile_type(self.inner) };
let profile_type = ColorProfileType::from(c_profile_type);
Some(ColorProfileRaw {
typ: profile_type,
data: result,
})
}
pub fn color_profile_nclx(&self) -> Option<ColorProfileNCLX> {
let mut profile_ptr = MaybeUninit::<_>::uninit();
let err = unsafe {
lh::heif_image_handle_get_nclx_color_profile(self.inner, profile_ptr.as_mut_ptr())
};
if err.code != 0 {
return None;
}
let profile_ptr = unsafe { profile_ptr.assume_init() };
if profile_ptr.is_null() {
return None;
}
Some(ColorProfileNCLX { inner: profile_ptr })
}
}
impl Drop for ImageHandle {
fn drop(&mut self) {
unsafe { lh::heif_image_handle_release(self.inner) };
}
}