/src/libheif/libheif/context.cc
Line | Count | Source |
1 | | /* |
2 | | * HEIF codec. |
3 | | * Copyright (c) 2017 Dirk Farin <dirk.farin@gmail.com> |
4 | | * |
5 | | * This file is part of libheif. |
6 | | * |
7 | | * libheif is free software: you can redistribute it and/or modify |
8 | | * it under the terms of the GNU Lesser General Public License as |
9 | | * published by the Free Software Foundation, either version 3 of |
10 | | * the License, or (at your option) any later version. |
11 | | * |
12 | | * libheif is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public License |
18 | | * along with libheif. If not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include "box.h" |
22 | | #include "error.h" |
23 | | #include "libheif/heif.h" |
24 | | #include "region.h" |
25 | | #include "brands.h" |
26 | | #include <cstdint> |
27 | | #include <cassert> |
28 | | #include <cstring> |
29 | | #include <algorithm> |
30 | | #include <iostream> |
31 | | #include <limits> |
32 | | #include <cmath> |
33 | | #include <deque> |
34 | | #include "image-items/image_item.h" |
35 | | #include <codecs/hevc_boxes.h> |
36 | | #include "sequences/track.h" |
37 | | #include "sequences/track_visual.h" |
38 | | #include "sequences/track_metadata.h" |
39 | | #include "libheif/heif_sequences.h" |
40 | | |
41 | | #if ENABLE_PARALLEL_TILE_DECODING |
42 | | #include <future> |
43 | | #endif |
44 | | |
45 | | #include "context.h" |
46 | | #include "file.h" |
47 | | #include "pixelimage.h" |
48 | | #include "api_structs.h" |
49 | | #include "security_limits.h" |
50 | | #include "compression.h" |
51 | | #include "color-conversion/colorconversion.h" |
52 | | #include "plugin_registry.h" |
53 | | #include "image-items/hevc.h" |
54 | | #include "image-items/vvc.h" |
55 | | #include "image-items/avif.h" |
56 | | #include "image-items/jpeg.h" |
57 | | #include "image-items/mask_image.h" |
58 | | #include "image-items/jpeg2000.h" |
59 | | #include "image-items/grid.h" |
60 | | #include "image-items/overlay.h" |
61 | | #include "image-items/tiled.h" |
62 | | |
63 | | #if WITH_UNCOMPRESSED_CODEC |
64 | | #include "image-items/unc_image.h" |
65 | | #endif |
66 | | #include "text.h" |
67 | | |
68 | | |
69 | | heif_encoder::heif_encoder(const heif_encoder_plugin* _plugin) |
70 | 0 | : plugin(_plugin) |
71 | 0 | { |
72 | |
|
73 | 0 | } |
74 | | |
75 | | heif_encoder::~heif_encoder() |
76 | 0 | { |
77 | 0 | release(); |
78 | 0 | } |
79 | | |
80 | | void heif_encoder::release() |
81 | 0 | { |
82 | 0 | if (encoder) { |
83 | 0 | plugin->free_encoder(encoder); |
84 | 0 | encoder = nullptr; |
85 | 0 | } |
86 | 0 | } |
87 | | |
88 | | |
89 | | heif_error heif_encoder::alloc() |
90 | 0 | { |
91 | 0 | if (encoder == nullptr) { |
92 | 0 | heif_error error = plugin->new_encoder(&encoder); |
93 | | // TODO: error handling |
94 | 0 | return error; |
95 | 0 | } |
96 | | |
97 | 0 | return {heif_error_Ok, heif_suberror_Unspecified, Error::kSuccess}; |
98 | 0 | } |
99 | | |
100 | | |
101 | | HeifContext::HeifContext() |
102 | 773 | : m_memory_tracker(&m_limits) |
103 | 773 | { |
104 | 773 | const char* security_limits_variable = getenv("LIBHEIF_SECURITY_LIMITS"); |
105 | | |
106 | 773 | if (security_limits_variable && (strcmp(security_limits_variable, "off") == 0 || |
107 | 0 | strcmp(security_limits_variable, "OFF") == 0)) { |
108 | 0 | m_limits = disabled_security_limits; |
109 | 0 | } |
110 | 773 | else { |
111 | 773 | m_limits = global_security_limits; |
112 | 773 | } |
113 | | |
114 | 773 | reset_to_empty_heif(); |
115 | 773 | } |
116 | | |
117 | | |
118 | | HeifContext::~HeifContext() |
119 | 773 | { |
120 | | // Break circular references between Images (when a faulty input image has circular image references) |
121 | 2.61k | for (auto& it : m_all_images) { |
122 | 2.61k | std::shared_ptr<ImageItem> image = it.second; |
123 | 2.61k | image->clear(); |
124 | 2.61k | } |
125 | 773 | } |
126 | | |
127 | | |
128 | | static void copy_security_limits(heif_security_limits* dst, const heif_security_limits* src) |
129 | 0 | { |
130 | 0 | dst->max_image_size_pixels = src->max_image_size_pixels; |
131 | 0 | dst->max_number_of_tiles = src->max_number_of_tiles; |
132 | 0 | dst->max_bayer_pattern_pixels = src->max_bayer_pattern_pixels; |
133 | 0 | dst->max_items = src->max_items; |
134 | |
|
135 | 0 | dst->max_color_profile_size = src->max_color_profile_size; |
136 | 0 | dst->max_memory_block_size = src->max_memory_block_size; |
137 | |
|
138 | 0 | dst->max_components = src->max_components; |
139 | |
|
140 | 0 | dst->max_iloc_extents_per_item = src->max_iloc_extents_per_item; |
141 | 0 | dst->max_size_entity_group = src->max_size_entity_group; |
142 | |
|
143 | 0 | dst->max_children_per_box = src->max_children_per_box; |
144 | |
|
145 | 0 | if (src->version >= 2) { |
146 | 0 | dst->max_sample_description_box_entries = src->max_sample_description_box_entries; |
147 | 0 | dst->max_sample_group_description_box_entries = src->max_sample_group_description_box_entries; |
148 | 0 | } |
149 | 0 | } |
150 | | |
151 | | |
152 | | void HeifContext::set_security_limits(const heif_security_limits* limits) |
153 | 0 | { |
154 | | // copy default limits |
155 | 0 | if (limits->version < global_security_limits.version) { |
156 | 0 | copy_security_limits(&m_limits, &global_security_limits); |
157 | 0 | } |
158 | | |
159 | | // overwrite with input limits |
160 | 0 | copy_security_limits(&m_limits, limits); |
161 | 0 | } |
162 | | |
163 | | |
164 | | Error HeifContext::read(const std::shared_ptr<StreamReader>& reader) |
165 | 0 | { |
166 | 0 | m_heif_file = std::make_shared<HeifFile>(); |
167 | 0 | m_heif_file->set_security_limits(&m_limits); |
168 | 0 | Error err = m_heif_file->read(reader); |
169 | 0 | if (err) { |
170 | 0 | return err; |
171 | 0 | } |
172 | | |
173 | 0 | return interpret_heif_file(); |
174 | 0 | } |
175 | | |
176 | | Error HeifContext::read_from_file(const char* input_filename) |
177 | 0 | { |
178 | 0 | m_heif_file = std::make_shared<HeifFile>(); |
179 | 0 | m_heif_file->set_security_limits(&m_limits); |
180 | 0 | Error err = m_heif_file->read_from_file(input_filename); |
181 | 0 | if (err) { |
182 | 0 | return err; |
183 | 0 | } |
184 | | |
185 | 0 | return interpret_heif_file(); |
186 | 0 | } |
187 | | |
188 | | Error HeifContext::read_from_memory(const void* data, size_t size, bool copy) |
189 | 773 | { |
190 | 773 | m_heif_file = std::make_shared<HeifFile>(); |
191 | 773 | m_heif_file->set_security_limits(&m_limits); |
192 | 773 | Error err = m_heif_file->read_from_memory(data, size, copy); |
193 | 773 | if (err) { |
194 | 304 | return err; |
195 | 304 | } |
196 | | |
197 | 469 | return interpret_heif_file(); |
198 | 773 | } |
199 | | |
200 | | void HeifContext::reset_to_empty_heif() |
201 | 773 | { |
202 | 773 | m_heif_file = std::make_shared<HeifFile>(); |
203 | 773 | m_heif_file->set_security_limits(&m_limits); |
204 | 773 | m_heif_file->new_empty_file(); |
205 | | |
206 | 773 | m_all_images.clear(); |
207 | 773 | m_top_level_images.clear(); |
208 | 773 | m_primary_image.reset(); |
209 | 773 | } |
210 | | |
211 | | |
212 | | std::vector<std::shared_ptr<ImageItem>> HeifContext::get_top_level_images(bool return_error_images) |
213 | 706 | { |
214 | 706 | if (return_error_images) { |
215 | 706 | return m_top_level_images; |
216 | 706 | } |
217 | 0 | else { |
218 | 0 | std::vector<std::shared_ptr<ImageItem>> filtered; |
219 | 0 | for (auto& item : m_top_level_images) { |
220 | 0 | if (!item->get_item_error()) { |
221 | 0 | filtered.push_back(item); |
222 | 0 | } |
223 | 0 | } |
224 | |
|
225 | 0 | return filtered; |
226 | 0 | } |
227 | 706 | } |
228 | | |
229 | | |
230 | | std::shared_ptr<ImageItem> HeifContext::get_image(heif_item_id id, bool return_error_images) |
231 | 3.93k | { |
232 | 3.93k | auto iter = m_all_images.find(id); |
233 | 3.93k | if (iter == m_all_images.end()) { |
234 | 8 | return nullptr; |
235 | 8 | } |
236 | 3.93k | else { |
237 | 3.93k | if (iter->second->get_item_error() && !return_error_images) { |
238 | 0 | return nullptr; |
239 | 0 | } |
240 | 3.93k | else { |
241 | 3.93k | return iter->second; |
242 | 3.93k | } |
243 | 3.93k | } |
244 | 3.93k | } |
245 | | |
246 | | |
247 | | std::shared_ptr<ImageItem> HeifContext::get_primary_image(bool return_error_image) |
248 | 358 | { |
249 | 358 | if (m_primary_image == nullptr) |
250 | 0 | return nullptr; |
251 | 358 | else if (!return_error_image && m_primary_image->get_item_error()) |
252 | 0 | return nullptr; |
253 | 358 | else |
254 | 358 | return m_primary_image; |
255 | 358 | } |
256 | | |
257 | | |
258 | | std::shared_ptr<const ImageItem> HeifContext::get_primary_image(bool return_error_image) const |
259 | 0 | { |
260 | 0 | return const_cast<HeifContext*>(this)->get_primary_image(return_error_image); |
261 | 0 | } |
262 | | |
263 | | |
264 | | bool HeifContext::is_image(heif_item_id ID) const |
265 | 347 | { |
266 | 347 | return m_all_images.contains(ID); |
267 | 347 | } |
268 | | |
269 | | |
270 | | std::shared_ptr<RegionItem> HeifContext::add_region_item(uint32_t reference_width, uint32_t reference_height) |
271 | 0 | { |
272 | 0 | std::shared_ptr<Box_infe> box = m_heif_file->add_new_infe_box(fourcc("rgan")); |
273 | 0 | box->set_hidden_item(true); |
274 | |
|
275 | 0 | auto regionItem = std::make_shared<RegionItem>(box->get_item_ID(), reference_width, reference_height); |
276 | 0 | add_region_item(regionItem); |
277 | |
|
278 | 0 | return regionItem; |
279 | 0 | } |
280 | | |
281 | | void HeifContext::add_region_referenced_mask_ref(heif_item_id region_item_id, heif_item_id mask_item_id) |
282 | 0 | { |
283 | 0 | m_heif_file->add_iref_reference(region_item_id, fourcc("mask"), {mask_item_id}); |
284 | 0 | } |
285 | | |
286 | | |
287 | | static uint64_t rescale(uint64_t duration, uint32_t old_base, uint32_t new_base) |
288 | 0 | { |
289 | | // prevent division by zero |
290 | | // TODO: we might emit an error in this case |
291 | 0 | if (old_base == 0) { |
292 | 0 | return 0; |
293 | 0 | } |
294 | | |
295 | 0 | return duration * new_base / old_base; |
296 | 0 | } |
297 | | |
298 | | |
299 | | void HeifContext::write(StreamWriter& writer) |
300 | 0 | { |
301 | | // --- finalize some parameters |
302 | |
|
303 | 0 | uint64_t max_sequence_duration = 0; |
304 | 0 | if (auto mvhd = m_heif_file->get_mvhd_box()) { |
305 | 0 | for (const auto& track : m_tracks) { |
306 | 0 | track.second->finalize_track(); |
307 | | |
308 | | // rescale track duration to movie timescale units |
309 | |
|
310 | 0 | uint64_t track_duration_in_media_units = track.second->get_duration_in_media_units(); |
311 | 0 | uint32_t media_timescale = track.second->get_timescale(); |
312 | |
|
313 | 0 | uint32_t mvhd_timescale = m_heif_file->get_mvhd_box()->get_time_scale(); |
314 | 0 | if (mvhd_timescale == 0) { |
315 | 0 | mvhd_timescale = track.second->get_timescale(); |
316 | 0 | m_heif_file->get_mvhd_box()->set_time_scale(mvhd_timescale); |
317 | 0 | } |
318 | |
|
319 | 0 | uint64_t movie_duration = rescale(track_duration_in_media_units, media_timescale, mvhd_timescale); |
320 | | |
321 | | // sequence repetitions |
322 | |
|
323 | 0 | if (m_sequence_repetitions == heif_sequence_maximum_number_of_repetitions) { |
324 | 0 | movie_duration = std::numeric_limits<uint64_t>::max(); |
325 | 0 | } |
326 | 0 | else { |
327 | 0 | if (std::numeric_limits<uint64_t>::max() / m_sequence_repetitions < movie_duration) { |
328 | 0 | movie_duration = std::numeric_limits<uint64_t>::max(); |
329 | 0 | } |
330 | 0 | else { |
331 | 0 | movie_duration *= m_sequence_repetitions; |
332 | 0 | } |
333 | 0 | } |
334 | |
|
335 | 0 | if (m_sequence_repetitions != 1) { |
336 | 0 | track.second->enable_edit_list_repeat_mode(true); |
337 | 0 | } |
338 | |
|
339 | 0 | track.second->set_track_duration_in_movie_units(movie_duration); |
340 | |
|
341 | 0 | max_sequence_duration = std::max(max_sequence_duration, movie_duration); |
342 | 0 | } |
343 | |
|
344 | 0 | mvhd->set_duration(max_sequence_duration); |
345 | 0 | } |
346 | | |
347 | | // --- serialize regions |
348 | |
|
349 | 0 | for (auto& image : m_all_images) { |
350 | 0 | for (auto region : image.second->get_region_item_ids()) { |
351 | 0 | m_heif_file->add_iref_reference(region, |
352 | 0 | fourcc("cdsc"), {image.first}); |
353 | 0 | } |
354 | 0 | } |
355 | |
|
356 | 0 | for (auto& region : m_region_items) { |
357 | 0 | std::vector<uint8_t> data_array; |
358 | 0 | Error err = region->encode(data_array); |
359 | | // TODO: err |
360 | |
|
361 | 0 | m_heif_file->append_iloc_data(region->item_id, data_array, 0); |
362 | 0 | } |
363 | | |
364 | | // --- serialise text items |
365 | |
|
366 | 0 | for (auto& image : m_all_images) { |
367 | 0 | for (auto text_item_id : image.second->get_text_item_ids()) { |
368 | 0 | m_heif_file->add_iref_reference(text_item_id, fourcc("text"), {image.first}); |
369 | 0 | } |
370 | 0 | } |
371 | |
|
372 | 0 | for (auto& text_item : m_text_items) { |
373 | 0 | auto encodeResult = text_item->encode(); |
374 | 0 | if (encodeResult) { |
375 | 0 | m_heif_file->append_iloc_data(text_item->get_item_id(), *encodeResult, 1); |
376 | 0 | } |
377 | 0 | } |
378 | | |
379 | | // --- post-process images |
380 | |
|
381 | 0 | for (auto& img : m_all_images) { |
382 | 0 | img.second->process_before_write(); |
383 | 0 | } |
384 | | |
385 | | // --- sort item properties |
386 | |
|
387 | 0 | if (auto ipma = m_heif_file->get_ipma_box()) { |
388 | 0 | ipma->sort_properties(m_heif_file->get_ipco_box()); |
389 | 0 | } |
390 | | |
391 | | // --- derive box versions |
392 | |
|
393 | 0 | m_heif_file->derive_box_versions(); |
394 | | |
395 | | // --- determine brands |
396 | |
|
397 | 0 | heif_brand2 main_brand; |
398 | 0 | std::vector<heif_brand2> compatible_brands; |
399 | 0 | compatible_brands = compute_compatible_brands(this, &main_brand); |
400 | | |
401 | | // Note: major brand should be repeated in the compatible brands, according to this: |
402 | | // ISOBMFF (ISO/IEC 14496-12:2020) § K.4: |
403 | | // NOTE This document requires that the major brand be repeated in the compatible-brands, |
404 | | // but this requirement is relaxed in the 'profiles' parameter for compactness. |
405 | | // See https://github.com/strukturag/libheif/issues/478 |
406 | |
|
407 | 0 | auto ftyp = m_heif_file->get_ftyp_box(); |
408 | | |
409 | | // set major brand if not set manually yet |
410 | 0 | if (ftyp->get_major_brand() == 0) { |
411 | 0 | ftyp->set_major_brand(main_brand); |
412 | 0 | } |
413 | |
|
414 | 0 | ftyp->set_minor_version(0); |
415 | 0 | for (auto brand : compatible_brands) { |
416 | 0 | ftyp->add_compatible_brand(brand); |
417 | 0 | } |
418 | | |
419 | | // --- write to file |
420 | |
|
421 | 0 | m_heif_file->write(writer); |
422 | 0 | } |
423 | | |
424 | | std::string HeifContext::debug_dump_boxes() const |
425 | 0 | { |
426 | 0 | return m_heif_file->debug_dump_boxes(); |
427 | 0 | } |
428 | | |
429 | | |
430 | | static bool item_type_is_image(uint32_t item_type, const std::string& content_type) |
431 | 5.26k | { |
432 | 5.26k | return (item_type == fourcc("hvc1") || |
433 | 4.42k | item_type == fourcc("av01") || |
434 | 3.71k | item_type == fourcc("grid") || |
435 | 3.52k | item_type == fourcc("tili") || |
436 | 3.51k | item_type == fourcc("iden") || |
437 | 3.40k | item_type == fourcc("iovl") || |
438 | 3.21k | item_type == fourcc("avc1") || |
439 | 3.20k | item_type == fourcc("unci") || |
440 | 3.19k | item_type == fourcc("vvc1") || |
441 | 3.19k | item_type == fourcc("jpeg") || |
442 | 3.16k | (item_type == fourcc("mime") && content_type == "image/jpeg") || |
443 | 3.16k | item_type == fourcc("j2k1") || |
444 | 3.16k | item_type == fourcc("mski")); |
445 | 5.26k | } |
446 | | |
447 | | |
448 | | void HeifContext::remove_top_level_image(const std::shared_ptr<ImageItem>& image) |
449 | 99 | { |
450 | 99 | std::vector<std::shared_ptr<ImageItem>> new_list; |
451 | | |
452 | 304 | for (const auto& img : m_top_level_images) { |
453 | 304 | if (img != image) { |
454 | 228 | new_list.push_back(img); |
455 | 228 | } |
456 | 304 | } |
457 | | |
458 | 99 | m_top_level_images = std::move(new_list); |
459 | 99 | } |
460 | | |
461 | | |
462 | | Error HeifContext::interpret_heif_file() |
463 | 469 | { |
464 | 469 | if (m_heif_file->has_images()) { |
465 | 469 | Error err = interpret_heif_file_images(); |
466 | 469 | if (err) { |
467 | 111 | return err; |
468 | 111 | } |
469 | 469 | } |
470 | | |
471 | 358 | if (m_heif_file->has_sequences()) { |
472 | 0 | Error err = interpret_heif_file_sequences(); |
473 | 0 | if (err) { |
474 | 0 | return err; |
475 | 0 | } |
476 | 0 | } |
477 | | |
478 | 358 | return Error::Ok; |
479 | 358 | } |
480 | | |
481 | | |
482 | | Error HeifContext::interpret_heif_file_images() |
483 | 469 | { |
484 | 469 | m_all_images.clear(); |
485 | 469 | m_top_level_images.clear(); |
486 | 469 | m_primary_image.reset(); |
487 | | |
488 | | |
489 | | // --- reference all non-hidden images |
490 | | |
491 | 469 | std::vector<heif_item_id> image_IDs = m_heif_file->get_item_IDs(); |
492 | | |
493 | 6.13k | for (heif_item_id id : image_IDs) { |
494 | 6.13k | auto infe_box = m_heif_file->get_infe_box(id); |
495 | 6.13k | if (!infe_box) { |
496 | | // TODO(farindk): Should we return an error instead of skipping the invalid id? |
497 | 0 | continue; |
498 | 0 | } |
499 | | |
500 | 6.13k | auto imageItem = ImageItem::alloc_for_infe_box(this, infe_box); |
501 | 6.13k | if (!imageItem) { |
502 | | // It is no imageItem item, skip it. |
503 | 3.52k | continue; |
504 | 3.52k | } |
505 | | |
506 | 2.61k | std::vector<std::shared_ptr<Box>> properties; |
507 | 2.61k | Error err = m_heif_file->get_properties(id, properties); |
508 | 2.61k | if (err) { |
509 | 794 | imageItem = std::make_shared<ImageItem_Error>(imageItem->get_infe_type(), id, err); |
510 | 794 | } |
511 | | |
512 | 2.61k | imageItem->set_properties(properties); |
513 | | |
514 | 2.61k | err = imageItem->initialize_decoder(); |
515 | 2.61k | if (err) { |
516 | 447 | imageItem = std::make_shared<ImageItem_Error>(imageItem->get_infe_type(), id, err); |
517 | 447 | imageItem->set_properties(properties); |
518 | 447 | } |
519 | | |
520 | 2.61k | m_all_images.insert(std::make_pair(id, imageItem)); |
521 | | |
522 | 2.61k | if (!infe_box->is_hidden_item()) { |
523 | 1.31k | if (id == m_heif_file->get_primary_image_ID()) { |
524 | 456 | imageItem->set_primary(true); |
525 | 456 | m_primary_image = imageItem; |
526 | 456 | } |
527 | | |
528 | 1.31k | m_top_level_images.push_back(imageItem); |
529 | 1.31k | } |
530 | | |
531 | 2.61k | imageItem->set_decoder_input_data(); |
532 | 2.61k | } |
533 | | |
534 | 469 | if (!m_primary_image) { |
535 | 13 | return Error(heif_error_Invalid_input, |
536 | 13 | heif_suberror_Nonexisting_item_referenced, |
537 | 13 | "'pitm' box references an unsupported or non-existing image"); |
538 | 13 | } |
539 | | |
540 | | |
541 | | // --- process image properties |
542 | | |
543 | 2.38k | for (auto& pair : m_all_images) { |
544 | 2.38k | auto& image = pair.second; |
545 | | |
546 | 2.38k | if (image->get_item_error()) { |
547 | 1.09k | continue; |
548 | 1.09k | } |
549 | | |
550 | 1.29k | std::vector<std::shared_ptr<Box>> properties; |
551 | | |
552 | 1.29k | Error err = m_heif_file->get_properties(pair.first, properties); |
553 | 1.29k | if (err) { |
554 | 0 | return err; |
555 | 0 | } |
556 | | |
557 | | |
558 | | // --- are there any 'essential' properties that we did not parse? |
559 | | |
560 | 4.81k | for (const auto& prop : properties) { |
561 | 4.81k | if (std::dynamic_pointer_cast<Box_other>(prop) && |
562 | 1.67k | get_heif_file()->get_ipco_box()->is_property_essential_for_item(pair.first, prop, get_heif_file()->get_ipma_box())) { |
563 | | |
564 | 6 | std::stringstream sstr; |
565 | 6 | sstr << "could not parse item property '" << prop->get_type_string() << "'"; |
566 | 6 | return {heif_error_Unsupported_feature, heif_suberror_Unsupported_essential_property, sstr.str()}; |
567 | 6 | } |
568 | 4.81k | } |
569 | | |
570 | | |
571 | | // --- Are there any parse errors in optional properties? Attach the errors as warnings to the images. |
572 | | |
573 | 1.28k | bool ignore_nonfatal_parse_errors = false; // TODO: this should be a user option. Where should we put this (heif_decoding_options, or while creating the context) ? |
574 | | |
575 | 4.80k | for (const auto& prop : properties) { |
576 | 4.80k | if (auto errorbox = std::dynamic_pointer_cast<Box_Error>(prop)) { |
577 | 362 | parse_error_fatality fatality = errorbox->get_parse_error_fatality(); |
578 | | |
579 | 362 | if (fatality == parse_error_fatality::optional || |
580 | 362 | (fatality == parse_error_fatality::ignorable && ignore_nonfatal_parse_errors)) { |
581 | 362 | image->add_decoding_warning(errorbox->get_error()); |
582 | 362 | } |
583 | 0 | else { |
584 | 0 | return errorbox->get_error(); |
585 | 0 | } |
586 | 362 | } |
587 | 4.80k | } |
588 | | |
589 | | |
590 | | // --- extract image resolution |
591 | | |
592 | 1.28k | bool ispe_read = false; |
593 | 4.80k | for (const auto& prop : properties) { |
594 | 4.80k | auto ispe = std::dynamic_pointer_cast<Box_ispe>(prop); |
595 | 4.80k | if (ispe) { |
596 | 857 | uint32_t width = ispe->get_width(); |
597 | 857 | uint32_t height = ispe->get_height(); |
598 | | |
599 | 857 | if (width == 0 || height == 0) { |
600 | 0 | return {heif_error_Invalid_input, |
601 | 0 | heif_suberror_Invalid_image_size, |
602 | 0 | "Zero image width or height"}; |
603 | 0 | } |
604 | | |
605 | 857 | image->set_resolution(width, height); |
606 | 857 | ispe_read = true; |
607 | 857 | } |
608 | 4.80k | } |
609 | | |
610 | | // Note: usually, we would like to check here if an `ispe` property exists as this is mandatory. |
611 | | // We want to do this if decoding_options.strict_decoding is set, but we cannot because we have no decoding_options |
612 | | // when parsing the file structure. |
613 | | |
614 | 1.28k | if (!ispe_read) { |
615 | 450 | image->add_decoding_warning({heif_error_Invalid_input, heif_suberror_No_ispe_property}); |
616 | 450 | } |
617 | | |
618 | | |
619 | 4.80k | for (const auto& prop : properties) { |
620 | 4.80k | auto colr = std::dynamic_pointer_cast<Box_colr>(prop); |
621 | 4.80k | if (colr) { |
622 | 630 | auto profile = colr->get_color_profile(); |
623 | 630 | image->set_color_profile(profile); |
624 | 630 | continue; |
625 | 630 | } |
626 | | |
627 | 4.17k | auto cmin = std::dynamic_pointer_cast<Box_cmin>(prop); |
628 | 4.17k | if (cmin) { |
629 | 0 | if (!ispe_read) { |
630 | 0 | return {heif_error_Invalid_input, heif_suberror_No_ispe_property}; |
631 | 0 | } |
632 | | |
633 | 0 | image->set_intrinsic_matrix(cmin->get_intrinsic_matrix()); |
634 | 0 | } |
635 | | |
636 | 4.17k | auto cmex = std::dynamic_pointer_cast<Box_cmex>(prop); |
637 | 4.17k | if (cmex) { |
638 | 0 | image->set_extrinsic_matrix(cmex->get_extrinsic_matrix()); |
639 | 0 | } |
640 | 4.17k | } |
641 | | |
642 | | |
643 | 4.80k | for (const auto& prop : properties) { |
644 | 4.80k | auto clap = std::dynamic_pointer_cast<Box_clap>(prop); |
645 | 4.80k | if (clap) { |
646 | 0 | image->set_resolution(clap->get_width_rounded(), |
647 | 0 | clap->get_height_rounded()); |
648 | |
|
649 | 0 | if (image->has_intrinsic_matrix()) { |
650 | 0 | image->get_intrinsic_matrix().apply_clap(clap.get(), image->get_width(), image->get_height()); |
651 | 0 | } |
652 | 0 | } |
653 | | |
654 | 4.80k | auto imir = std::dynamic_pointer_cast<Box_imir>(prop); |
655 | 4.80k | if (imir) { |
656 | 0 | if (!ispe_read) { |
657 | 0 | return {heif_error_Invalid_input, heif_suberror_No_ispe_property}; |
658 | 0 | } |
659 | | |
660 | 0 | image->get_intrinsic_matrix().apply_imir(imir.get(), image->get_width(), image->get_height()); |
661 | 0 | } |
662 | | |
663 | 4.80k | auto irot = std::dynamic_pointer_cast<Box_irot>(prop); |
664 | 4.80k | if (irot) { |
665 | 131 | if (irot->get_rotation_ccw() == 90 || |
666 | 131 | irot->get_rotation_ccw() == 270) { |
667 | 41 | if (!ispe_read) { |
668 | 0 | return {heif_error_Invalid_input, heif_suberror_No_ispe_property}; |
669 | 0 | } |
670 | | |
671 | | // swap width and height |
672 | 41 | image->set_resolution(image->get_height(), |
673 | 41 | image->get_width()); |
674 | 41 | } |
675 | | |
676 | | // TODO: apply irot to camera extrinsic matrix |
677 | 131 | } |
678 | 4.80k | } |
679 | 1.28k | } |
680 | | |
681 | | |
682 | | // --- remove auxiliary from top-level images and assign to their respective image |
683 | | |
684 | 450 | auto iref_box = m_heif_file->get_iref_box(); |
685 | 450 | if (iref_box) { |
686 | | // m_top_level_images.clear(); |
687 | | |
688 | 1.93k | for (auto& pair : m_all_images) { |
689 | 1.93k | auto& image = pair.second; |
690 | | |
691 | 1.93k | std::vector<Box_iref::Reference> references = iref_box->get_references_from(image->get_id()); |
692 | | |
693 | 1.93k | for (const Box_iref::Reference& ref : references) { |
694 | 564 | uint32_t type = ref.header.get_short_type(); |
695 | | |
696 | 564 | if (type == fourcc("thmb")) { |
697 | | // --- this is a thumbnail image, attach to the main image |
698 | |
|
699 | 0 | std::vector<heif_item_id> refs = ref.to_item_ID; |
700 | 0 | for (heif_item_id ref: refs) { |
701 | 0 | image->set_is_thumbnail(); |
702 | |
|
703 | 0 | auto master_iter = m_all_images.find(ref); |
704 | 0 | if (master_iter == m_all_images.end()) { |
705 | 0 | return Error(heif_error_Invalid_input, |
706 | 0 | heif_suberror_Nonexisting_item_referenced, |
707 | 0 | "Thumbnail references a non-existing image"); |
708 | 0 | } |
709 | | |
710 | 0 | if (master_iter->second->is_thumbnail()) { |
711 | 0 | return Error(heif_error_Invalid_input, |
712 | 0 | heif_suberror_Nonexisting_item_referenced, |
713 | 0 | "Thumbnail references another thumbnail"); |
714 | 0 | } |
715 | | |
716 | 0 | if (image.get() == master_iter->second.get()) { |
717 | 0 | return Error(heif_error_Invalid_input, |
718 | 0 | heif_suberror_Nonexisting_item_referenced, |
719 | 0 | "Recursive thumbnail image detected"); |
720 | 0 | } |
721 | 0 | master_iter->second->add_thumbnail(image); |
722 | 0 | } |
723 | 0 | remove_top_level_image(image); |
724 | 0 | } |
725 | 564 | else if (type == fourcc("auxl")) { |
726 | | |
727 | | // --- this is an auxiliary image |
728 | | // check whether it is an alpha channel and attach to the main image if yes |
729 | | |
730 | 67 | std::shared_ptr<Box_auxC> auxC_property = image->get_property<Box_auxC>(); |
731 | 67 | if (!auxC_property) { |
732 | 1 | std::stringstream sstr; |
733 | 1 | sstr << "No auxC property for image " << image->get_id(); |
734 | 1 | return Error(heif_error_Invalid_input, |
735 | 1 | heif_suberror_Auxiliary_image_type_unspecified, |
736 | 1 | sstr.str()); |
737 | 1 | } |
738 | | |
739 | 66 | std::vector<heif_item_id> refs = ref.to_item_ID; |
740 | | |
741 | | // alpha channel |
742 | | |
743 | 66 | if (auxC_property->get_aux_type() == "urn:mpeg:avc:2015:auxid:1" || // HEIF (avc) |
744 | 59 | auxC_property->get_aux_type() == "urn:mpeg:hevc:2015:auxid:1" || // HEIF (h265) |
745 | 58 | auxC_property->get_aux_type() == "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha") { // MIAF |
746 | | |
747 | 61 | for (heif_item_id ref: refs) { |
748 | 61 | auto master_iter = m_all_images.find(ref); |
749 | 61 | if (master_iter == m_all_images.end()) { |
750 | | |
751 | 26 | if (!m_heif_file->has_item_with_id(ref)) { |
752 | 8 | return Error(heif_error_Invalid_input, |
753 | 8 | heif_suberror_Nonexisting_item_referenced, |
754 | 8 | "Non-existing alpha image referenced"); |
755 | 8 | } |
756 | | |
757 | 18 | continue; |
758 | 26 | } |
759 | | |
760 | 35 | auto master_img = master_iter->second; |
761 | | |
762 | 35 | if (image.get() == master_img.get()) { |
763 | 0 | return Error(heif_error_Invalid_input, |
764 | 0 | heif_suberror_Nonexisting_item_referenced, |
765 | 0 | "Recursive alpha image detected"); |
766 | 0 | } |
767 | | |
768 | 35 | image->set_is_alpha_channel(); |
769 | 35 | master_img->set_alpha_channel(image); |
770 | 35 | } |
771 | 29 | } |
772 | | |
773 | | |
774 | | // depth channel |
775 | | |
776 | 58 | if (auxC_property->get_aux_type() == "urn:mpeg:hevc:2015:auxid:2" || // HEIF |
777 | 50 | auxC_property->get_aux_type() == "urn:mpeg:mpegB:cicp:systems:auxiliary:depth") { // AVIF |
778 | 12 | image->set_is_depth_channel(); |
779 | | |
780 | 42 | for (heif_item_id ref: refs) { |
781 | 42 | auto master_iter = m_all_images.find(ref); |
782 | 42 | if (master_iter == m_all_images.end()) { |
783 | | |
784 | 35 | if (!m_heif_file->has_item_with_id(ref)) { |
785 | 12 | return Error(heif_error_Invalid_input, |
786 | 12 | heif_suberror_Nonexisting_item_referenced, |
787 | 12 | "Non-existing depth image referenced"); |
788 | 12 | } |
789 | | |
790 | 23 | continue; |
791 | 35 | } |
792 | 7 | if (image.get() == master_iter->second.get()) { |
793 | 0 | return Error(heif_error_Invalid_input, |
794 | 0 | heif_suberror_Nonexisting_item_referenced, |
795 | 0 | "Recursive depth image detected"); |
796 | 0 | } |
797 | 7 | master_iter->second->set_depth_channel(image); |
798 | | |
799 | 7 | const auto& subtypes = auxC_property->get_subtypes(); |
800 | | |
801 | 7 | if (!subtypes.empty()) { |
802 | 4 | std::vector<std::shared_ptr<SEIMessage>> sei_messages; |
803 | 4 | Error err = decode_hevc_aux_sei_messages(subtypes, sei_messages); |
804 | 4 | if (err) { |
805 | 0 | return err; |
806 | 0 | } |
807 | | |
808 | 4 | for (auto& msg : sei_messages) { |
809 | 0 | auto depth_msg = std::dynamic_pointer_cast<SEIMessage_depth_representation_info>(msg); |
810 | 0 | if (depth_msg) { |
811 | 0 | image->set_depth_representation_info(*depth_msg); |
812 | 0 | } |
813 | 0 | } |
814 | 4 | } |
815 | 7 | } |
816 | 12 | } |
817 | | |
818 | | |
819 | | // --- generic aux image |
820 | | |
821 | 46 | image->set_is_aux_image(auxC_property->get_aux_type()); |
822 | | |
823 | 119 | for (heif_item_id ref: refs) { |
824 | 119 | auto master_iter = m_all_images.find(ref); |
825 | 119 | if (master_iter == m_all_images.end()) { |
826 | | |
827 | 60 | if (!m_heif_file->has_item_with_id(ref)) { |
828 | 15 | return Error(heif_error_Invalid_input, |
829 | 15 | heif_suberror_Nonexisting_item_referenced, |
830 | 15 | "Non-existing aux image referenced"); |
831 | 15 | } |
832 | | |
833 | 45 | continue; |
834 | 60 | } |
835 | 59 | if (image.get() == master_iter->second.get()) { |
836 | 0 | return Error(heif_error_Invalid_input, |
837 | 0 | heif_suberror_Nonexisting_item_referenced, |
838 | 0 | "Recursive aux image detected"); |
839 | 0 | } |
840 | | |
841 | 59 | master_iter->second->add_aux_image(image); |
842 | | |
843 | 59 | remove_top_level_image(image); |
844 | 59 | } |
845 | 46 | } |
846 | 497 | else { |
847 | | // 'image' is a normal image, keep it as a top-level image |
848 | 497 | } |
849 | 564 | } |
850 | 1.93k | } |
851 | 345 | } |
852 | | |
853 | | |
854 | | // --- check that HEVC images have an hvcC property |
855 | | |
856 | 2.14k | for (auto& pair : m_all_images) { |
857 | 2.14k | auto& image = pair.second; |
858 | | |
859 | 2.14k | if (image->get_item_error()) { |
860 | 1.03k | continue; |
861 | 1.03k | } |
862 | | |
863 | 1.11k | std::shared_ptr<Box_infe> infe = m_heif_file->get_infe_box(image->get_id()); |
864 | 1.11k | if (infe->get_item_type_4cc() == fourcc("hvc1")) { |
865 | | |
866 | 431 | auto ipma = m_heif_file->get_ipma_box(); |
867 | 431 | auto ipco = m_heif_file->get_ipco_box(); |
868 | | |
869 | 431 | if (!ipco->get_property_for_item_ID(image->get_id(), ipma, fourcc("hvcC"))) { |
870 | 1 | return Error(heif_error_Invalid_input, |
871 | 1 | heif_suberror_No_hvcC_box, |
872 | 1 | "No hvcC property in hvc1 type image"); |
873 | 1 | } |
874 | 431 | } |
875 | 1.10k | if (infe->get_item_type_4cc() == fourcc("vvc1")) { |
876 | |
|
877 | 0 | auto ipma = m_heif_file->get_ipma_box(); |
878 | 0 | auto ipco = m_heif_file->get_ipco_box(); |
879 | |
|
880 | 0 | if (!ipco->get_property_for_item_ID(image->get_id(), ipma, fourcc("vvcC"))) { |
881 | 0 | return Error(heif_error_Invalid_input, |
882 | 0 | heif_suberror_No_vvcC_box, |
883 | 0 | "No vvcC property in vvc1 type image"); |
884 | 0 | } |
885 | 0 | } |
886 | 1.10k | } |
887 | | |
888 | | |
889 | | // --- assign color profile from grid tiles to main image when main image has no profile assigned |
890 | | |
891 | 2.08k | for (auto& pair : m_all_images) { |
892 | 2.08k | auto& image = pair.second; |
893 | 2.08k | auto id = pair.first; |
894 | | |
895 | 2.08k | if (image->get_item_error()) { |
896 | 1.00k | continue; |
897 | 1.00k | } |
898 | | |
899 | 1.07k | auto infe_box = m_heif_file->get_infe_box(id); |
900 | 1.07k | if (!infe_box) { |
901 | 0 | continue; |
902 | 0 | } |
903 | | |
904 | 1.07k | if (!iref_box) { |
905 | 56 | break; |
906 | 56 | } |
907 | | |
908 | 1.01k | if (infe_box->get_item_type_4cc() == fourcc("grid")) { |
909 | 97 | std::vector<heif_item_id> image_references = iref_box->get_references(id, fourcc("dimg")); |
910 | | |
911 | 97 | if (image_references.empty()) { |
912 | 0 | continue; // TODO: can this every happen? |
913 | 0 | } |
914 | | |
915 | 97 | auto tileId = image_references.front(); |
916 | | |
917 | 97 | auto iter = m_all_images.find(tileId); |
918 | 97 | if (iter == m_all_images.end()) { |
919 | 65 | continue; // invalid grid entry |
920 | 65 | } |
921 | | |
922 | 32 | auto tile_img = iter->second; |
923 | 32 | if (image->get_color_profile_icc() == nullptr && tile_img->get_color_profile_icc()) { |
924 | 0 | image->set_color_profile(tile_img->get_color_profile_icc()); |
925 | 0 | } |
926 | | |
927 | 32 | if (!image->has_nclx_color_profile() && tile_img->has_nclx_color_profile()) { |
928 | 0 | image->set_color_profile_nclx(tile_img->get_color_profile_nclx()); |
929 | 0 | } |
930 | 32 | } |
931 | 1.01k | } |
932 | | |
933 | | |
934 | | // --- read metadata and assign to image |
935 | | |
936 | 5.26k | for (heif_item_id id : image_IDs) { |
937 | 5.26k | uint32_t item_type = m_heif_file->get_item_type_4cc(id); |
938 | 5.26k | std::string content_type = m_heif_file->get_content_type(id); |
939 | | |
940 | | // 'rgan': skip region annotations, handled next |
941 | | // 'iden': iden images are no metadata |
942 | 5.26k | if (item_type_is_image(item_type, content_type) || item_type == fourcc("rgan")) { |
943 | 2.47k | continue; |
944 | 2.47k | } |
945 | | |
946 | 2.79k | std::string item_uri_type = m_heif_file->get_item_uri_type(id); |
947 | | |
948 | | // we now assign all kinds of metadata to the image, not only 'Exif' and 'XMP' |
949 | | |
950 | 2.79k | std::shared_ptr<ImageMetadata> metadata = std::make_shared<ImageMetadata>(); |
951 | 2.79k | metadata->item_id = id; |
952 | 2.79k | metadata->item_type = fourcc_to_string(item_type); |
953 | 2.79k | metadata->content_type = content_type; |
954 | 2.79k | metadata->item_uri_type = std::move(item_uri_type); |
955 | | |
956 | 2.79k | auto metadataResult = m_heif_file->get_uncompressed_item_data(id); |
957 | 2.79k | if (!metadataResult) { |
958 | 2.59k | if (item_type == fourcc("Exif") || item_type == fourcc("mime")) { |
959 | | // these item types should have data |
960 | 2 | return metadataResult.error(); |
961 | 2 | } |
962 | 2.59k | else { |
963 | | // anything else is probably something that we don't understand yet |
964 | 2.59k | continue; |
965 | 2.59k | } |
966 | 2.59k | } |
967 | 198 | else { |
968 | 198 | metadata->m_data = *metadataResult; |
969 | 198 | } |
970 | | |
971 | | // --- assign metadata to the image |
972 | | |
973 | 198 | if (iref_box) { |
974 | 121 | std::vector<heif_item_id> references = iref_box->get_references(id, fourcc("cdsc")); |
975 | 121 | for (heif_item_id exif_image_id : references) { |
976 | 2 | auto img_iter = m_all_images.find(exif_image_id); |
977 | 2 | if (img_iter == m_all_images.end()) { |
978 | 2 | if (!m_heif_file->has_item_with_id(exif_image_id)) { |
979 | 2 | return Error(heif_error_Invalid_input, |
980 | 2 | heif_suberror_Nonexisting_item_referenced, |
981 | 2 | "Metadata assigned to non-existing image"); |
982 | 2 | } |
983 | | |
984 | 0 | continue; |
985 | 2 | } |
986 | 0 | img_iter->second->add_metadata(metadata); |
987 | 0 | } |
988 | 121 | } |
989 | 198 | } |
990 | | |
991 | | // --- set premultiplied alpha flag |
992 | | |
993 | 5.22k | for (heif_item_id id : image_IDs) { |
994 | 5.22k | if (iref_box) { |
995 | 3.43k | std::vector<heif_item_id> references = iref_box->get_references(id, fourcc("prem")); |
996 | 3.43k | for (heif_item_id ref : references) { |
997 | 0 | (void)ref; |
998 | |
|
999 | 0 | heif_item_id color_image_id = id; |
1000 | 0 | auto img_iter = m_all_images.find(color_image_id); |
1001 | 0 | if (img_iter == m_all_images.end()) { |
1002 | 0 | return Error(heif_error_Invalid_input, |
1003 | 0 | heif_suberror_Nonexisting_item_referenced, |
1004 | 0 | "`prem` link assigned to non-existing image"); |
1005 | 0 | } |
1006 | | |
1007 | 0 | img_iter->second->set_is_premultiplied_alpha(true); |
1008 | 0 | } |
1009 | 3.43k | } |
1010 | 5.22k | } |
1011 | | |
1012 | | // --- read region item and assign to image(s) |
1013 | | |
1014 | 4.98k | for (heif_item_id id : image_IDs) { |
1015 | 4.98k | uint32_t item_type = m_heif_file->get_item_type_4cc(id); |
1016 | 4.98k | if (item_type != fourcc("rgan")) { |
1017 | 4.67k | continue; |
1018 | 4.67k | } |
1019 | | |
1020 | 312 | std::shared_ptr<RegionItem> region_item = std::make_shared<RegionItem>(); |
1021 | 312 | region_item->item_id = id; |
1022 | | |
1023 | 312 | Result regionDataResult = m_heif_file->get_uncompressed_item_data(id); |
1024 | 312 | if (!regionDataResult) { |
1025 | 36 | return regionDataResult.error(); |
1026 | 36 | } |
1027 | 276 | region_item->parse(*regionDataResult); |
1028 | | |
1029 | 276 | if (iref_box) { |
1030 | 226 | std::vector<Box_iref::Reference> references = iref_box->get_references_from(id); |
1031 | 226 | for (const auto& ref : references) { |
1032 | 102 | if (ref.header.get_short_type() == fourcc("cdsc")) { |
1033 | 5 | std::vector<uint32_t> refs = ref.to_item_ID; |
1034 | 5 | for (uint32_t ref : refs) { |
1035 | 5 | uint32_t image_id = ref; |
1036 | 5 | auto img_iter = m_all_images.find(image_id); |
1037 | 5 | if (img_iter == m_all_images.end()) { |
1038 | 5 | return Error(heif_error_Invalid_input, |
1039 | 5 | heif_suberror_Nonexisting_item_referenced, |
1040 | 5 | "Region item assigned to non-existing image"); |
1041 | 5 | } |
1042 | 0 | img_iter->second->add_region_item_id(id); |
1043 | 0 | m_region_items.push_back(region_item); |
1044 | 0 | } |
1045 | 5 | } |
1046 | | |
1047 | | /* When the geometry 'mask' of a region is represented by a mask stored in |
1048 | | * another image item the image item containing the mask shall be identified |
1049 | | * by an item reference of type 'mask' from the region item to the image item |
1050 | | * containing the mask. */ |
1051 | 97 | if (ref.header.get_short_type() == fourcc("mask")) { |
1052 | 85 | std::vector<uint32_t> refs = ref.to_item_ID; |
1053 | 85 | size_t mask_index = 0; |
1054 | 1.47k | for (int j = 0; j < region_item->get_number_of_regions(); j++) { |
1055 | 1.39k | if (region_item->get_regions()[j]->getRegionType() == heif_region_type_referenced_mask) { |
1056 | 50 | std::shared_ptr<RegionGeometry_ReferencedMask> mask_geometry = std::dynamic_pointer_cast<RegionGeometry_ReferencedMask>(region_item->get_regions()[j]); |
1057 | | |
1058 | 50 | if (mask_index >= refs.size()) { |
1059 | 1 | return Error(heif_error_Invalid_input, |
1060 | 1 | heif_suberror_Unspecified, |
1061 | 1 | "Region mask reference with non-existing mask image reference"); |
1062 | 1 | } |
1063 | | |
1064 | 49 | uint32_t mask_image_id = refs[mask_index]; |
1065 | 49 | if (!is_image(mask_image_id)) { |
1066 | 8 | return Error(heif_error_Invalid_input, |
1067 | 8 | heif_suberror_Unspecified, |
1068 | 8 | "Region mask referenced item is not an image"); |
1069 | 8 | } |
1070 | | |
1071 | 41 | auto mask_image = get_image(mask_image_id, true); |
1072 | 41 | if (auto error = mask_image->get_item_error()) { |
1073 | 1 | return error; |
1074 | 1 | } |
1075 | | |
1076 | 40 | mask_geometry->referenced_item = mask_image_id; |
1077 | 40 | if (mask_geometry->width == 0) { |
1078 | 2 | mask_geometry->width = mask_image->get_ispe_width(); |
1079 | 2 | } |
1080 | 40 | if (mask_geometry->height == 0) { |
1081 | 5 | mask_geometry->height = mask_image->get_ispe_height(); |
1082 | 5 | } |
1083 | 40 | mask_index += 1; |
1084 | 40 | remove_top_level_image(mask_image); |
1085 | 40 | } |
1086 | 1.39k | } |
1087 | 85 | } |
1088 | 97 | } |
1089 | 226 | } |
1090 | 276 | } |
1091 | | |
1092 | | // --- read text item and assign to image(s) |
1093 | 4.62k | for (heif_item_id id : image_IDs) { |
1094 | 4.62k | uint32_t item_type = m_heif_file->get_item_type_4cc(id); |
1095 | 4.62k | if (item_type != fourcc("mime")) { // TODO: && content_type starts with "text/" ? |
1096 | 4.61k | continue; |
1097 | 4.61k | } |
1098 | 6 | std::shared_ptr<TextItem> text_item = std::make_shared<TextItem>(); |
1099 | 6 | text_item->set_item_id(id); |
1100 | | |
1101 | 6 | auto textDataResult = m_heif_file->get_uncompressed_item_data(id); |
1102 | 6 | if (!textDataResult) { |
1103 | 0 | return textDataResult.error(); |
1104 | 0 | } |
1105 | | |
1106 | 6 | text_item->parse(*textDataResult); |
1107 | 6 | if (iref_box) { |
1108 | 2 | std::vector<Box_iref::Reference> references = iref_box->get_references_from(id); |
1109 | 2 | for (const auto& ref : references) { |
1110 | 2 | if (ref.header.get_short_type() == fourcc("text")) { |
1111 | 0 | std::vector<uint32_t> refs = ref.to_item_ID; |
1112 | 0 | for (uint32_t ref : refs) { |
1113 | 0 | uint32_t image_id = ref; |
1114 | 0 | auto img_iter = m_all_images.find(image_id); |
1115 | 0 | if (img_iter == m_all_images.end()) { |
1116 | 0 | return Error(heif_error_Invalid_input, |
1117 | 0 | heif_suberror_Nonexisting_item_referenced, |
1118 | 0 | "Text item assigned to non-existing image"); |
1119 | 0 | } |
1120 | 0 | img_iter->second->add_text_item_id(id); |
1121 | 0 | m_text_items.push_back(text_item); |
1122 | 0 | } |
1123 | 0 | } |
1124 | 2 | } |
1125 | 2 | } |
1126 | 6 | } |
1127 | | |
1128 | 358 | return Error::Ok; |
1129 | 358 | } |
1130 | | |
1131 | | |
1132 | | bool HeifContext::has_alpha(heif_item_id ID) const |
1133 | 0 | { |
1134 | 0 | auto imgIter = m_all_images.find(ID); |
1135 | 0 | if (imgIter == m_all_images.end()) { |
1136 | 0 | return false; |
1137 | 0 | } |
1138 | | |
1139 | 0 | auto img = imgIter->second; |
1140 | | |
1141 | | // --- has the image an auxiliary alpha image? |
1142 | |
|
1143 | 0 | if (img->get_alpha_channel() != nullptr) { |
1144 | 0 | return true; |
1145 | 0 | } |
1146 | | |
1147 | 0 | if (img->has_coded_alpha_channel()) { |
1148 | 0 | return true; |
1149 | 0 | } |
1150 | | |
1151 | 0 | heif_colorspace colorspace; |
1152 | 0 | heif_chroma chroma; |
1153 | 0 | Error err = img->get_coded_image_colorspace(&colorspace, &chroma); |
1154 | 0 | if (err) { |
1155 | 0 | return false; |
1156 | 0 | } |
1157 | | |
1158 | 0 | if (chroma == heif_chroma_interleaved_RGBA || |
1159 | 0 | chroma == heif_chroma_interleaved_RRGGBBAA_BE || |
1160 | 0 | chroma == heif_chroma_interleaved_RRGGBBAA_LE) { |
1161 | 0 | return true; |
1162 | 0 | } |
1163 | | |
1164 | | // --- if the image is a 'grid', check if there is alpha in any of the tiles |
1165 | | |
1166 | | // TODO: move this into ImageItem |
1167 | | |
1168 | 0 | uint32_t image_type = m_heif_file->get_item_type_4cc(ID); |
1169 | 0 | if (image_type == fourcc("grid")) { |
1170 | |
|
1171 | 0 | Result gridDataResult = m_heif_file->get_uncompressed_item_data(ID); |
1172 | 0 | if (!gridDataResult) { |
1173 | 0 | return false; |
1174 | 0 | } |
1175 | | |
1176 | 0 | ImageGrid grid; |
1177 | 0 | err = grid.parse(*gridDataResult); |
1178 | 0 | if (err) { |
1179 | 0 | return false; |
1180 | 0 | } |
1181 | | |
1182 | | |
1183 | 0 | auto iref_box = m_heif_file->get_iref_box(); |
1184 | |
|
1185 | 0 | if (!iref_box) { |
1186 | 0 | return false; |
1187 | 0 | } |
1188 | | |
1189 | 0 | std::vector<heif_item_id> image_references = iref_box->get_references(ID, fourcc("dimg")); |
1190 | |
|
1191 | 0 | if ((int) image_references.size() != grid.get_rows() * grid.get_columns()) { |
1192 | 0 | return false; |
1193 | 0 | } |
1194 | | |
1195 | | |
1196 | | // --- check that all image IDs are valid images |
1197 | | |
1198 | 0 | for (heif_item_id tile_id : image_references) { |
1199 | 0 | if (!is_image(tile_id)) { |
1200 | 0 | return false; |
1201 | 0 | } |
1202 | 0 | } |
1203 | | |
1204 | | // --- check whether at least one tile has an alpha channel |
1205 | | |
1206 | 0 | bool has_alpha = false; |
1207 | |
|
1208 | 0 | for (heif_item_id tile_id : image_references) { |
1209 | 0 | auto iter = m_all_images.find(tile_id); |
1210 | 0 | if (iter == m_all_images.end()) { |
1211 | 0 | return false; |
1212 | 0 | } |
1213 | | |
1214 | 0 | const std::shared_ptr<ImageItem> tileImg = iter->second; |
1215 | |
|
1216 | 0 | has_alpha |= tileImg->get_alpha_channel() != nullptr; |
1217 | 0 | } |
1218 | | |
1219 | 0 | return has_alpha; |
1220 | 0 | } |
1221 | 0 | else { |
1222 | | // TODO: what about overlays ? |
1223 | 0 | return false; |
1224 | 0 | } |
1225 | 0 | } |
1226 | | |
1227 | | |
1228 | | Error HeifContext::get_id_of_non_virtual_child_image(heif_item_id id, heif_item_id& out) const |
1229 | 0 | { |
1230 | 0 | uint32_t image_type = m_heif_file->get_item_type_4cc(id); |
1231 | 0 | if (image_type == fourcc("grid") || |
1232 | 0 | image_type == fourcc("iden") || |
1233 | 0 | image_type == fourcc("iovl")) { |
1234 | 0 | auto iref_box = m_heif_file->get_iref_box(); |
1235 | 0 | if (!iref_box) { |
1236 | 0 | return Error(heif_error_Invalid_input, |
1237 | 0 | heif_suberror_No_item_data, |
1238 | 0 | "Derived image does not reference any other image items"); |
1239 | 0 | } |
1240 | | |
1241 | 0 | std::vector<heif_item_id> image_references = iref_box->get_references(id, fourcc("dimg")); |
1242 | | |
1243 | | // TODO: check whether this really can be recursive (e.g. overlay of grid images) |
1244 | |
|
1245 | 0 | if (image_references.empty() || image_references[0] == id) { |
1246 | 0 | return Error(heif_error_Invalid_input, |
1247 | 0 | heif_suberror_No_item_data, |
1248 | 0 | "Derived image does not reference any other image items"); |
1249 | 0 | } |
1250 | 0 | else { |
1251 | 0 | return get_id_of_non_virtual_child_image(image_references[0], out); |
1252 | 0 | } |
1253 | 0 | } |
1254 | 0 | else { |
1255 | 0 | if (!m_all_images.contains(id)) { |
1256 | 0 | std::stringstream sstr; |
1257 | 0 | sstr << "Image item " << id << " referenced, but it does not exist\n"; |
1258 | |
|
1259 | 0 | return Error(heif_error_Invalid_input, |
1260 | 0 | heif_suberror_Nonexisting_item_referenced, |
1261 | 0 | sstr.str()); |
1262 | 0 | } |
1263 | 0 | else if (dynamic_cast<ImageItem_Error*>(m_all_images.find(id)->second.get())) { |
1264 | | // Should er return an error here or leave it to the follow-up code to detect that? |
1265 | 0 | } |
1266 | | |
1267 | 0 | out = id; |
1268 | 0 | return Error::Ok; |
1269 | 0 | } |
1270 | 0 | } |
1271 | | |
1272 | | |
1273 | | Result<std::shared_ptr<HeifPixelImage>> HeifContext::decode_image(heif_item_id ID, |
1274 | | heif_colorspace out_colorspace, |
1275 | | heif_chroma out_chroma, |
1276 | | const heif_decoding_options& options, |
1277 | | bool decode_only_tile, uint32_t tx, uint32_t ty) const |
1278 | 728 | { |
1279 | 728 | std::shared_ptr<ImageItem> imgitem; |
1280 | 728 | if (m_all_images.contains(ID)) { |
1281 | 728 | imgitem = m_all_images.find(ID)->second; |
1282 | 728 | } |
1283 | | |
1284 | | // Note: this may happen, for example when an 'iden' image references a non-existing image item. |
1285 | 728 | if (imgitem == nullptr) { |
1286 | 0 | return Error(heif_error_Invalid_input, heif_suberror_Nonexisting_item_referenced); |
1287 | 0 | } |
1288 | | |
1289 | | |
1290 | 728 | auto decodingResult = imgitem->decode_image(options, decode_only_tile, tx, ty); |
1291 | 728 | if (!decodingResult) { |
1292 | 664 | return decodingResult.error(); |
1293 | 664 | } |
1294 | | |
1295 | 64 | std::shared_ptr<HeifPixelImage> img = *decodingResult; |
1296 | | |
1297 | | |
1298 | | // --- convert to output chroma format |
1299 | | |
1300 | 64 | auto img_result = convert_to_output_colorspace(img, out_colorspace, out_chroma, options); |
1301 | 64 | if (!img_result) { |
1302 | 0 | return img_result.error(); |
1303 | 0 | } |
1304 | 64 | else { |
1305 | 64 | img = *img_result; |
1306 | 64 | } |
1307 | | |
1308 | 64 | img->add_warnings(imgitem->get_decoding_warnings()); |
1309 | | |
1310 | 64 | return img; |
1311 | 64 | } |
1312 | | |
1313 | | |
1314 | | bool nclx_color_profile_equal(std::optional<nclx_profile> a, |
1315 | | const heif_color_profile_nclx* b) |
1316 | 64 | { |
1317 | 64 | if (!a && b==nullptr) { |
1318 | 0 | return true; |
1319 | 0 | } |
1320 | | |
1321 | 64 | heif_color_profile_nclx* default_nclx = nullptr; |
1322 | | |
1323 | 64 | if (!a || b==nullptr) { |
1324 | 64 | default_nclx = heif_nclx_color_profile_alloc(); |
1325 | | |
1326 | 64 | if (!a) { |
1327 | 0 | a = nclx_profile::defaults(); |
1328 | 0 | } |
1329 | | |
1330 | 64 | if (b==nullptr) { |
1331 | 64 | b = default_nclx; |
1332 | 64 | } |
1333 | 64 | } |
1334 | | |
1335 | 64 | bool equal = true; |
1336 | 64 | if (a->m_matrix_coefficients != b->matrix_coefficients || |
1337 | 0 | a->m_colour_primaries != b->color_primaries || |
1338 | 0 | a->m_transfer_characteristics != b->transfer_characteristics || |
1339 | 64 | a->m_full_range_flag != b->full_range_flag) { |
1340 | 64 | equal = false; |
1341 | 64 | } |
1342 | | |
1343 | 64 | if (default_nclx) { |
1344 | 64 | heif_nclx_color_profile_free(default_nclx); |
1345 | 64 | } |
1346 | | |
1347 | 64 | return equal; |
1348 | 64 | } |
1349 | | |
1350 | | |
1351 | | Result<std::shared_ptr<HeifPixelImage>> HeifContext::convert_to_output_colorspace(std::shared_ptr<HeifPixelImage> img, |
1352 | | heif_colorspace out_colorspace, |
1353 | | heif_chroma out_chroma, |
1354 | | const heif_decoding_options& options) const |
1355 | 64 | { |
1356 | 64 | heif_colorspace target_colorspace = (out_colorspace == heif_colorspace_undefined ? |
1357 | 0 | img->get_colorspace() : |
1358 | 64 | out_colorspace); |
1359 | | |
1360 | 64 | heif_chroma target_chroma = (out_chroma == heif_chroma_undefined ? |
1361 | 64 | img->get_chroma_format() : out_chroma); |
1362 | | |
1363 | 64 | bool different_chroma = (target_chroma != img->get_chroma_format()); |
1364 | 64 | bool different_colorspace = (target_colorspace != img->get_colorspace()); |
1365 | | |
1366 | 64 | uint8_t img_bpp = img->get_visual_image_bits_per_pixel(); |
1367 | 64 | uint8_t converted_output_bpp = (options.convert_hdr_to_8bit && img_bpp > 8) ? 8 : 0 /* keep input depth */; |
1368 | | |
1369 | 64 | nclx_profile img_nclx = img->get_color_profile_nclx_with_fallback(); |
1370 | 64 | bool different_nclx = !nclx_color_profile_equal(img_nclx, options.output_image_nclx_profile); |
1371 | | |
1372 | 64 | if (different_chroma || |
1373 | 0 | different_colorspace || |
1374 | 0 | converted_output_bpp || |
1375 | 0 | different_nclx || |
1376 | 64 | (img->has_alpha() && options.color_conversion_options_ext && options.color_conversion_options_ext->alpha_composition_mode != heif_alpha_composition_mode_none)) { |
1377 | | |
1378 | 64 | nclx_profile output_profile; |
1379 | 64 | if (options.output_image_nclx_profile) { |
1380 | 0 | output_profile.set_matrix_coefficients(options.output_image_nclx_profile->matrix_coefficients); |
1381 | 0 | output_profile.set_colour_primaries(options.output_image_nclx_profile->color_primaries); |
1382 | 0 | output_profile.set_full_range_flag(options.output_image_nclx_profile->full_range_flag); |
1383 | 0 | } |
1384 | 64 | else { |
1385 | 64 | output_profile.set_sRGB_defaults(); |
1386 | 64 | } |
1387 | | |
1388 | 64 | return convert_colorspace(img, target_colorspace, target_chroma, output_profile, converted_output_bpp, |
1389 | 64 | options.color_conversion_options, options.color_conversion_options_ext, |
1390 | 64 | get_security_limits()); |
1391 | 64 | } |
1392 | 0 | else { |
1393 | 0 | return img; |
1394 | 0 | } |
1395 | 64 | } |
1396 | | |
1397 | | |
1398 | | static Result<std::shared_ptr<HeifPixelImage>> |
1399 | | create_alpha_image_from_image_alpha_channel(const std::shared_ptr<HeifPixelImage>& image, |
1400 | | const heif_security_limits* limits) |
1401 | 0 | { |
1402 | | // --- generate alpha image |
1403 | |
|
1404 | 0 | std::shared_ptr<HeifPixelImage> alpha_image = std::make_shared<HeifPixelImage>(); |
1405 | 0 | alpha_image->create(image->get_width(), image->get_height(), |
1406 | 0 | heif_colorspace_monochrome, heif_chroma_monochrome); |
1407 | |
|
1408 | 0 | if (image->has_channel(heif_channel_Alpha)) { |
1409 | 0 | alpha_image->copy_new_plane_from(image, heif_channel_Alpha, heif_channel_Y, limits); |
1410 | 0 | } |
1411 | 0 | else if (image->get_chroma_format() == heif_chroma_interleaved_RGBA) { |
1412 | 0 | if (auto err = alpha_image->extract_alpha_from_RGBA(image, limits)) { |
1413 | 0 | return err; |
1414 | 0 | } |
1415 | 0 | } |
1416 | | // TODO: 16 bit |
1417 | | |
1418 | | // --- set nclx profile with full-range flag |
1419 | | |
1420 | 0 | nclx_profile nclx = nclx_profile::undefined(); |
1421 | 0 | nclx.set_full_range_flag(true); // this is the default, but just to be sure in case the defaults change |
1422 | 0 | alpha_image->set_color_profile_nclx(nclx); |
1423 | |
|
1424 | 0 | return alpha_image; |
1425 | 0 | } |
1426 | | |
1427 | | |
1428 | | Result<std::shared_ptr<ImageItem>> HeifContext::encode_image(const std::shared_ptr<HeifPixelImage>& pixel_image, |
1429 | | heif_encoder* encoder, |
1430 | | const heif_encoding_options& in_options, |
1431 | | heif_image_input_class input_class) |
1432 | 0 | { |
1433 | 0 | std::shared_ptr<ImageItem> output_image_item = ImageItem::alloc_for_compression_format(this, encoder->plugin->compression_format); |
1434 | | |
1435 | |
|
1436 | | #if 0 |
1437 | | // TODO: the hdlr box is not the right place for comments |
1438 | | // m_heif_file->set_hdlr_library_info(encoder->plugin->get_plugin_name()); |
1439 | | |
1440 | | case heif_compression_mask: { |
1441 | | error = encode_image_as_mask(pixel_image, |
1442 | | encoder, |
1443 | | options, |
1444 | | input_class, |
1445 | | out_image); |
1446 | | } |
1447 | | break; |
1448 | | |
1449 | | default: |
1450 | | return Error(heif_error_Encoder_plugin_error, heif_suberror_Unsupported_codec); |
1451 | | } |
1452 | | #endif |
1453 | | |
1454 | | |
1455 | | // --- check whether we have to convert the image color space |
1456 | | |
1457 | | // The reason for doing the color conversion here is that the input might be an RGBA image and the color conversion |
1458 | | // will extract the alpha plane anyway. We can reuse that plane below instead of having to do a new conversion. |
1459 | |
|
1460 | 0 | heif_encoding_options options = in_options; |
1461 | |
|
1462 | 0 | std::shared_ptr<HeifPixelImage> colorConvertedImage; |
1463 | |
|
1464 | 0 | if (output_image_item->get_encoder()) { |
1465 | 0 | if (const auto* nclx = output_image_item->get_encoder()->get_forced_output_nclx()) { |
1466 | 0 | options.output_nclx_profile = const_cast<heif_color_profile_nclx*>(nclx); |
1467 | 0 | } |
1468 | |
|
1469 | 0 | Result<std::shared_ptr<HeifPixelImage>> srcImageResult; |
1470 | 0 | srcImageResult = output_image_item->get_encoder()->convert_colorspace_for_encoding(pixel_image, |
1471 | 0 | encoder, |
1472 | 0 | options, |
1473 | 0 | get_security_limits()); |
1474 | 0 | if (!srcImageResult) { |
1475 | 0 | return srcImageResult.error(); |
1476 | 0 | } |
1477 | | |
1478 | 0 | colorConvertedImage = *srcImageResult; |
1479 | 0 | } |
1480 | 0 | else { |
1481 | 0 | colorConvertedImage = pixel_image; |
1482 | 0 | } |
1483 | | |
1484 | 0 | Error err = output_image_item->encode_to_item(this, |
1485 | 0 | colorConvertedImage, |
1486 | 0 | encoder, options, input_class); |
1487 | 0 | if (err) { |
1488 | 0 | return err; |
1489 | 0 | } |
1490 | | |
1491 | 0 | insert_image_item(output_image_item->get_id(), output_image_item); |
1492 | | |
1493 | | |
1494 | | // --- if there is an alpha channel, add it as an additional image |
1495 | |
|
1496 | 0 | if (options.save_alpha_channel && |
1497 | 0 | colorConvertedImage->has_alpha() && |
1498 | 0 | output_image_item->get_auxC_alpha_channel_type() != nullptr) { // does not need a separate alpha aux image |
1499 | | |
1500 | | // --- generate alpha image |
1501 | | // TODO: can we directly code a monochrome image instead of the dummy color channels? |
1502 | |
|
1503 | 0 | std::shared_ptr<HeifPixelImage> alpha_image; |
1504 | 0 | auto alpha_image_result = create_alpha_image_from_image_alpha_channel(colorConvertedImage, get_security_limits()); |
1505 | 0 | if (!alpha_image_result) { |
1506 | 0 | return alpha_image_result.error(); |
1507 | 0 | } |
1508 | | |
1509 | 0 | alpha_image = *alpha_image_result; |
1510 | | |
1511 | | |
1512 | | // --- encode the alpha image |
1513 | |
|
1514 | 0 | auto alphaEncodingResult = encode_image(alpha_image, encoder, options, |
1515 | 0 | heif_image_input_class_alpha); |
1516 | 0 | if (!alphaEncodingResult) { |
1517 | 0 | return alphaEncodingResult.error(); |
1518 | 0 | } |
1519 | | |
1520 | 0 | std::shared_ptr<ImageItem> heif_alpha_image = *alphaEncodingResult; |
1521 | |
|
1522 | 0 | m_heif_file->add_iref_reference(heif_alpha_image->get_id(), fourcc("auxl"), {output_image_item->get_id()}); |
1523 | 0 | m_heif_file->set_auxC_property(heif_alpha_image->get_id(), output_image_item->get_auxC_alpha_channel_type()); |
1524 | |
|
1525 | 0 | if (pixel_image->is_premultiplied_alpha()) { |
1526 | 0 | m_heif_file->add_iref_reference(output_image_item->get_id(), fourcc("prem"), {heif_alpha_image->get_id()}); |
1527 | 0 | } |
1528 | 0 | } |
1529 | | |
1530 | 0 | std::vector<std::shared_ptr<Box>> properties; |
1531 | 0 | err = m_heif_file->get_properties(output_image_item->get_id(), properties); |
1532 | 0 | if (err) { |
1533 | 0 | return err; |
1534 | 0 | } |
1535 | 0 | output_image_item->set_properties(properties); |
1536 | | |
1537 | | //m_heif_file->set_brand(encoder->plugin->compression_format, |
1538 | | // output_image_item->is_miaf_compatible()); |
1539 | |
|
1540 | 0 | return output_image_item; |
1541 | 0 | } |
1542 | | |
1543 | | |
1544 | | void HeifContext::set_primary_image(const std::shared_ptr<ImageItem>& image) |
1545 | 0 | { |
1546 | | // update heif context |
1547 | |
|
1548 | 0 | if (m_primary_image) { |
1549 | 0 | m_primary_image->set_primary(false); |
1550 | 0 | } |
1551 | |
|
1552 | 0 | image->set_primary(true); |
1553 | 0 | m_primary_image = image; |
1554 | | |
1555 | | |
1556 | | // update pitm box in HeifFile |
1557 | |
|
1558 | 0 | m_heif_file->set_primary_item_id(image->get_id()); |
1559 | 0 | } |
1560 | | |
1561 | | |
1562 | | Error HeifContext::assign_thumbnail(const std::shared_ptr<ImageItem>& master_image, |
1563 | | const std::shared_ptr<ImageItem>& thumbnail_image) |
1564 | 0 | { |
1565 | 0 | m_heif_file->add_iref_reference(thumbnail_image->get_id(), |
1566 | 0 | fourcc("thmb"), {master_image->get_id()}); |
1567 | |
|
1568 | 0 | return Error::Ok; |
1569 | 0 | } |
1570 | | |
1571 | | |
1572 | | Result<std::shared_ptr<ImageItem>> HeifContext::encode_thumbnail(const std::shared_ptr<HeifPixelImage>& image, |
1573 | | heif_encoder* encoder, |
1574 | | const heif_encoding_options& options, |
1575 | | int bbox_size) |
1576 | 0 | { |
1577 | 0 | int orig_width = image->get_width(); |
1578 | 0 | int orig_height = image->get_height(); |
1579 | |
|
1580 | 0 | int thumb_width, thumb_height; |
1581 | |
|
1582 | 0 | if (orig_width <= bbox_size && orig_height <= bbox_size) { |
1583 | | // original image is smaller than thumbnail size -> do not encode any thumbnail |
1584 | |
|
1585 | 0 | return Error::Ok; |
1586 | 0 | } |
1587 | 0 | else if (orig_width > orig_height) { |
1588 | 0 | thumb_height = orig_height * bbox_size / orig_width; |
1589 | 0 | thumb_width = bbox_size; |
1590 | 0 | } |
1591 | 0 | else { |
1592 | 0 | thumb_width = orig_width * bbox_size / orig_height; |
1593 | 0 | thumb_height = bbox_size; |
1594 | 0 | } |
1595 | | |
1596 | | |
1597 | | // round size to even width and height |
1598 | | |
1599 | 0 | thumb_width &= ~1; |
1600 | 0 | thumb_height &= ~1; |
1601 | | |
1602 | |
|
1603 | 0 | std::shared_ptr<HeifPixelImage> thumbnail_image; |
1604 | 0 | Error error = image->scale_nearest_neighbor(thumbnail_image, thumb_width, thumb_height, get_security_limits()); |
1605 | 0 | if (error) { |
1606 | 0 | return error; |
1607 | 0 | } |
1608 | | |
1609 | 0 | auto encodingResult = encode_image(thumbnail_image, |
1610 | 0 | encoder, options, |
1611 | 0 | heif_image_input_class_thumbnail); |
1612 | 0 | if (!encodingResult) { |
1613 | 0 | return encodingResult.error(); |
1614 | 0 | } |
1615 | | |
1616 | 0 | return *encodingResult; |
1617 | 0 | } |
1618 | | |
1619 | | |
1620 | | Error HeifContext::add_exif_metadata(const std::shared_ptr<ImageItem>& master_image, const void* data, int size) |
1621 | 0 | { |
1622 | | // find location of TIFF header |
1623 | 0 | uint32_t offset = 0; |
1624 | 0 | const char* tiffmagic1 = "MM\0*"; |
1625 | 0 | const char* tiffmagic2 = "II*\0"; |
1626 | 0 | while (offset + 4 < (unsigned int) size) { |
1627 | 0 | if (!memcmp((uint8_t*) data + offset, tiffmagic1, 4)) break; |
1628 | 0 | if (!memcmp((uint8_t*) data + offset, tiffmagic2, 4)) break; |
1629 | 0 | offset++; |
1630 | 0 | } |
1631 | 0 | if (offset >= (unsigned int) size) { |
1632 | 0 | return Error(heif_error_Usage_error, |
1633 | 0 | heif_suberror_Invalid_parameter_value, |
1634 | 0 | "Could not find location of TIFF header in Exif metadata."); |
1635 | 0 | } |
1636 | | |
1637 | | |
1638 | 0 | std::vector<uint8_t> data_array; |
1639 | 0 | data_array.resize(size + 4); |
1640 | 0 | data_array[0] = (uint8_t) ((offset >> 24) & 0xFF); |
1641 | 0 | data_array[1] = (uint8_t) ((offset >> 16) & 0xFF); |
1642 | 0 | data_array[2] = (uint8_t) ((offset >> 8) & 0xFF); |
1643 | 0 | data_array[3] = (uint8_t) ((offset) & 0xFF); |
1644 | 0 | memcpy(data_array.data() + 4, data, size); |
1645 | | |
1646 | |
|
1647 | 0 | return add_generic_metadata(master_image, |
1648 | 0 | data_array.data(), (int) data_array.size(), |
1649 | 0 | fourcc("Exif"), nullptr, nullptr, heif_metadata_compression_off, nullptr); |
1650 | 0 | } |
1651 | | |
1652 | | |
1653 | | Error HeifContext::add_XMP_metadata(const std::shared_ptr<ImageItem>& master_image, const void* data, int size, |
1654 | | heif_metadata_compression compression) |
1655 | 0 | { |
1656 | 0 | return add_generic_metadata(master_image, data, size, fourcc("mime"), "application/rdf+xml", nullptr, compression, nullptr); |
1657 | 0 | } |
1658 | | |
1659 | | |
1660 | | Error HeifContext::add_generic_metadata(const std::shared_ptr<ImageItem>& master_image, const void* data, int size, |
1661 | | uint32_t item_type, const char* content_type, const char* item_uri_type, heif_metadata_compression compression, |
1662 | | heif_item_id* out_item_id) |
1663 | 0 | { |
1664 | | // create an infe box describing what kind of data we are storing (this also creates a new ID) |
1665 | |
|
1666 | 0 | auto metadata_infe_box = m_heif_file->add_new_infe_box(item_type); |
1667 | 0 | metadata_infe_box->set_hidden_item(true); |
1668 | 0 | if (content_type != nullptr) { |
1669 | 0 | metadata_infe_box->set_content_type(content_type); |
1670 | 0 | } |
1671 | |
|
1672 | 0 | heif_item_id metadata_id = metadata_infe_box->get_item_ID(); |
1673 | 0 | if (out_item_id) { |
1674 | 0 | *out_item_id = metadata_id; |
1675 | 0 | } |
1676 | | |
1677 | | |
1678 | | // we assign this data to the image |
1679 | |
|
1680 | 0 | m_heif_file->add_iref_reference(metadata_id, |
1681 | 0 | fourcc("cdsc"), {master_image->get_id()}); |
1682 | | |
1683 | | |
1684 | | // --- metadata compression |
1685 | |
|
1686 | 0 | if (compression == heif_metadata_compression_auto) { |
1687 | 0 | compression = heif_metadata_compression_off; // currently, we don't use header compression by default |
1688 | 0 | } |
1689 | | |
1690 | | // only set metadata compression for MIME type data which has 'content_encoding' field |
1691 | 0 | if (compression != heif_metadata_compression_off && |
1692 | 0 | item_type != fourcc("mime")) { |
1693 | | // TODO: error, compression not supported |
1694 | 0 | } |
1695 | | |
1696 | |
|
1697 | 0 | std::vector<uint8_t> data_array; |
1698 | 0 | if (compression == heif_metadata_compression_zlib) { |
1699 | 0 | #if HAVE_ZLIB |
1700 | 0 | data_array = compress_zlib((const uint8_t*) data, size); |
1701 | 0 | metadata_infe_box->set_content_encoding("compress_zlib"); |
1702 | | #else |
1703 | | return Error(heif_error_Unsupported_feature, |
1704 | | heif_suberror_Unsupported_header_compression_method); |
1705 | | #endif |
1706 | 0 | } |
1707 | 0 | else if (compression == heif_metadata_compression_deflate) { |
1708 | 0 | #if HAVE_ZLIB |
1709 | 0 | data_array = compress_zlib((const uint8_t*) data, size); |
1710 | 0 | metadata_infe_box->set_content_encoding("deflate"); |
1711 | | #else |
1712 | | return Error(heif_error_Unsupported_feature, |
1713 | | heif_suberror_Unsupported_header_compression_method); |
1714 | | #endif |
1715 | 0 | } |
1716 | 0 | else { |
1717 | | // uncompressed data, plain copy |
1718 | |
|
1719 | 0 | data_array.resize(size); |
1720 | 0 | memcpy(data_array.data(), data, size); |
1721 | 0 | } |
1722 | | |
1723 | | // copy the data into the file, store the pointer to it in an iloc box entry |
1724 | |
|
1725 | 0 | m_heif_file->append_iloc_data(metadata_id, data_array, 0); |
1726 | |
|
1727 | 0 | return Error::Ok; |
1728 | 0 | } |
1729 | | |
1730 | | |
1731 | | heif_property_id HeifContext::add_property(heif_item_id targetItem, std::shared_ptr<Box> property, bool essential) |
1732 | 0 | { |
1733 | 0 | heif_property_id id; |
1734 | |
|
1735 | 0 | if (auto img = get_image(targetItem, false)) { |
1736 | 0 | id = img->add_property(property, essential); |
1737 | 0 | } |
1738 | 0 | else { |
1739 | 0 | id = m_heif_file->add_property(targetItem, property, essential); |
1740 | 0 | } |
1741 | |
|
1742 | 0 | return id; |
1743 | 0 | } |
1744 | | |
1745 | | |
1746 | | Result<heif_item_id> HeifContext::add_pyramid_group(const std::vector<heif_item_id>& layer_item_ids) |
1747 | 0 | { |
1748 | 0 | struct pymd_entry |
1749 | 0 | { |
1750 | 0 | std::shared_ptr<ImageItem> item; |
1751 | 0 | uint32_t width = 0; |
1752 | 0 | }; |
1753 | | |
1754 | | // --- sort all images by size |
1755 | |
|
1756 | 0 | std::vector<pymd_entry> pymd_entries; |
1757 | 0 | for (auto id : layer_item_ids) { |
1758 | 0 | auto image_item = get_image(id, true); |
1759 | 0 | if (auto error = image_item->get_item_error()) { |
1760 | 0 | return error; |
1761 | 0 | } |
1762 | | |
1763 | 0 | pymd_entry entry; |
1764 | 0 | entry.item = image_item; |
1765 | 0 | entry.width = image_item->get_width(); |
1766 | 0 | pymd_entries.emplace_back(entry); |
1767 | 0 | } |
1768 | | |
1769 | 0 | std::sort(pymd_entries.begin(), pymd_entries.end(), [](const pymd_entry& a, const pymd_entry& b) { |
1770 | 0 | return a.width < b.width; |
1771 | 0 | }); |
1772 | | |
1773 | | |
1774 | | // --- generate pymd box |
1775 | |
|
1776 | 0 | auto pymd = std::make_shared<Box_pymd>(); |
1777 | 0 | std::vector<Box_pymd::LayerInfo> layers; |
1778 | 0 | std::vector<heif_item_id> ids; |
1779 | |
|
1780 | 0 | auto base_item = pymd_entries.back().item; |
1781 | |
|
1782 | 0 | uint32_t tile_w=0, tile_h=0; |
1783 | 0 | base_item->get_tile_size(tile_w, tile_h); |
1784 | |
|
1785 | 0 | uint32_t last_width=0, last_height=0; |
1786 | |
|
1787 | 0 | for (const auto& entry : pymd_entries) { |
1788 | 0 | auto layer_item = entry.item; |
1789 | |
|
1790 | 0 | if (false) { |
1791 | | // according to pymd definition, we should check that all layers have the same tile size |
1792 | 0 | uint32_t item_tile_w = 0, item_tile_h = 0; |
1793 | 0 | base_item->get_tile_size(item_tile_w, item_tile_h); |
1794 | 0 | if (item_tile_w != tile_w || item_tile_h != tile_h) { |
1795 | | // TODO: add warning that tile sizes are not the same |
1796 | 0 | } |
1797 | 0 | } |
1798 | |
|
1799 | 0 | heif_image_tiling tiling = layer_item->get_heif_image_tiling(); |
1800 | |
|
1801 | 0 | if (tiling.image_width < last_width || tiling.image_height < last_height) { |
1802 | 0 | return Error{ |
1803 | 0 | heif_error_Invalid_input, |
1804 | 0 | heif_suberror_Invalid_parameter_value, |
1805 | 0 | "Multi-resolution pyramid images have to be provided ordered from smallest to largest." |
1806 | 0 | }; |
1807 | 0 | } |
1808 | | |
1809 | 0 | last_width = tiling.image_width; |
1810 | 0 | last_height = tiling.image_height; |
1811 | |
|
1812 | 0 | Box_pymd::LayerInfo layer{}; |
1813 | 0 | layer.layer_binning = (uint16_t)(base_item->get_width() / tiling.image_width); |
1814 | 0 | layer.tiles_in_layer_row_minus1 = static_cast<uint16_t>(tiling.num_rows - 1); |
1815 | 0 | layer.tiles_in_layer_column_minus1 = static_cast<uint16_t>(tiling.num_columns - 1); |
1816 | 0 | layers.push_back(layer); |
1817 | 0 | ids.push_back(layer_item->get_id()); |
1818 | 0 | } |
1819 | | |
1820 | 0 | heif_item_id group_id = m_heif_file->get_unused_item_id(); |
1821 | |
|
1822 | 0 | pymd->set_group_id(group_id); |
1823 | 0 | pymd->set_layers((uint16_t)tile_w, (uint16_t)tile_h, layers, ids); |
1824 | |
|
1825 | 0 | m_heif_file->add_entity_group_box(pymd); |
1826 | | |
1827 | | // add back-references to base image |
1828 | |
|
1829 | 0 | for (size_t i = 0; i < ids.size() - 1; i++) { |
1830 | 0 | m_heif_file->add_iref_reference(ids[i], fourcc("base"), {ids.back()}); |
1831 | 0 | } |
1832 | |
|
1833 | 0 | return {group_id}; |
1834 | 0 | } |
1835 | | |
1836 | | |
1837 | | Error HeifContext::interpret_heif_file_sequences() |
1838 | 0 | { |
1839 | 0 | m_tracks.clear(); |
1840 | | |
1841 | | |
1842 | | // --- reference all non-hidden images |
1843 | |
|
1844 | 0 | auto moov = m_heif_file->get_moov_box(); |
1845 | 0 | assert(moov); |
1846 | | |
1847 | 0 | auto mvhd = moov->get_child_box<Box_mvhd>(); |
1848 | 0 | if (!mvhd) { |
1849 | 0 | assert(false); // TODO |
1850 | 0 | } |
1851 | | |
1852 | 0 | auto tracks = moov->get_child_boxes<Box_trak>(); |
1853 | 0 | for (const auto& track_box : tracks) { |
1854 | 0 | auto track = Track::alloc_track(this, track_box); |
1855 | 0 | if (!track) { |
1856 | 0 | return {heif_error_Invalid_input, |
1857 | 0 | heif_suberror_Unspecified, |
1858 | 0 | "Unknown track handler or track error"}; |
1859 | 0 | } |
1860 | 0 | m_tracks.insert({track->get_id(), track}); |
1861 | |
|
1862 | 0 | if (track->is_visual_track() && m_visual_track_id == 0) { |
1863 | 0 | m_visual_track_id = track->get_id(); |
1864 | 0 | } |
1865 | 0 | } |
1866 | | |
1867 | | // --- post-parsing initialization |
1868 | | |
1869 | 0 | std::vector<std::shared_ptr<Track>> all_tracks; |
1870 | 0 | for (auto& track : m_tracks) { |
1871 | 0 | all_tracks.push_back(track.second); |
1872 | 0 | } |
1873 | |
|
1874 | 0 | for (auto& track : m_tracks) { |
1875 | 0 | track.second->initialize_after_parsing(this, all_tracks); |
1876 | 0 | } |
1877 | |
|
1878 | 0 | return Error::Ok; |
1879 | 0 | } |
1880 | | |
1881 | | |
1882 | | std::vector<uint32_t> HeifContext::get_track_IDs() const |
1883 | 0 | { |
1884 | 0 | std::vector<uint32_t> ids; |
1885 | |
|
1886 | 0 | for (const auto& track : m_tracks) { |
1887 | 0 | ids.push_back(track.first); |
1888 | 0 | } |
1889 | |
|
1890 | 0 | return ids; |
1891 | 0 | } |
1892 | | |
1893 | | |
1894 | | Result<std::shared_ptr<Track>> HeifContext::get_track(uint32_t track_id) |
1895 | 0 | { |
1896 | 0 | assert(has_sequence()); |
1897 | | |
1898 | 0 | if (track_id != 0) { |
1899 | 0 | auto iter = m_tracks.find(track_id); |
1900 | 0 | if (iter == m_tracks.end()) { |
1901 | 0 | return Error{heif_error_Usage_error, |
1902 | 0 | heif_suberror_Unspecified, |
1903 | 0 | "Invalid track id"}; |
1904 | 0 | } |
1905 | | |
1906 | 0 | return iter->second; |
1907 | 0 | } |
1908 | | |
1909 | 0 | if (m_visual_track_id != 0) { |
1910 | 0 | return m_tracks[m_visual_track_id]; |
1911 | 0 | } |
1912 | | |
1913 | 0 | return m_tracks.begin()->second; |
1914 | 0 | } |
1915 | | |
1916 | | |
1917 | | Result<std::shared_ptr<const Track>> HeifContext::get_track(uint32_t track_id) const |
1918 | 0 | { |
1919 | 0 | auto result = const_cast<HeifContext*>(this)->get_track(track_id); |
1920 | 0 | if (!result) { |
1921 | 0 | return result.error(); |
1922 | 0 | } |
1923 | 0 | else { |
1924 | 0 | Result<std::shared_ptr<const Track>> my_result(*result); |
1925 | 0 | return my_result; |
1926 | 0 | } |
1927 | 0 | } |
1928 | | |
1929 | | |
1930 | | uint32_t HeifContext::get_sequence_timescale() const |
1931 | 0 | { |
1932 | 0 | auto mvhd = m_heif_file->get_mvhd_box(); |
1933 | 0 | if (!mvhd) { |
1934 | 0 | return 0; |
1935 | 0 | } |
1936 | | |
1937 | 0 | return mvhd->get_time_scale(); |
1938 | 0 | } |
1939 | | |
1940 | | |
1941 | | void HeifContext::set_sequence_timescale(uint32_t timescale) |
1942 | 0 | { |
1943 | 0 | get_heif_file()->init_for_sequence(); |
1944 | |
|
1945 | 0 | auto mvhd = m_heif_file->get_mvhd_box(); |
1946 | | |
1947 | | /* unnecessary, since mvhd duration is set during writing |
1948 | | |
1949 | | uint32_t old_timescale = mvhd->get_time_scale(); |
1950 | | if (old_timescale != 0) { |
1951 | | uint64_t scaled_duration = mvhd->get_duration() * timescale / old_timescale; |
1952 | | mvhd->set_duration(scaled_duration); |
1953 | | } |
1954 | | */ |
1955 | |
|
1956 | 0 | mvhd->set_time_scale(timescale); |
1957 | 0 | } |
1958 | | |
1959 | | |
1960 | | void HeifContext::set_number_of_sequence_repetitions(uint32_t repetitions) |
1961 | 0 | { |
1962 | 0 | m_sequence_repetitions = repetitions; |
1963 | 0 | } |
1964 | | |
1965 | | |
1966 | | uint64_t HeifContext::get_sequence_duration() const |
1967 | 0 | { |
1968 | 0 | auto mvhd = m_heif_file->get_mvhd_box(); |
1969 | 0 | if (!mvhd) { |
1970 | 0 | return 0; |
1971 | 0 | } |
1972 | | |
1973 | 0 | return mvhd->get_duration(); |
1974 | 0 | } |
1975 | | |
1976 | | |
1977 | | Result<std::shared_ptr<Track_Visual>> HeifContext::add_visual_sequence_track(const TrackOptions* options, |
1978 | | uint32_t handler_type, |
1979 | | uint16_t width, uint16_t height) |
1980 | 0 | { |
1981 | 0 | m_heif_file->init_for_sequence(); |
1982 | |
|
1983 | 0 | std::shared_ptr<Track_Visual> trak = std::make_shared<Track_Visual>(this, 0, width, height, options, handler_type); |
1984 | 0 | m_tracks.insert({trak->get_id(), trak}); |
1985 | |
|
1986 | 0 | return trak; |
1987 | 0 | } |
1988 | | |
1989 | | |
1990 | | Result<std::shared_ptr<class Track_Metadata>> HeifContext::add_uri_metadata_sequence_track(const TrackOptions* options, |
1991 | | std::string uri) |
1992 | 0 | { |
1993 | 0 | m_heif_file->init_for_sequence(); |
1994 | |
|
1995 | 0 | std::shared_ptr<Track_Metadata> trak = std::make_shared<Track_Metadata>(this, 0, uri, options); |
1996 | 0 | m_tracks.insert({trak->get_id(), trak}); |
1997 | |
|
1998 | 0 | return trak; |
1999 | 0 | } |
2000 | | |
2001 | | std::shared_ptr<TextItem> HeifContext::add_text_item(const char* content_type, const char* text) |
2002 | 0 | { |
2003 | 0 | std::shared_ptr<Box_infe> box = m_heif_file->add_new_infe_box(fourcc("mime")); |
2004 | 0 | box->set_hidden_item(true); |
2005 | 0 | box->set_content_type(std::string(content_type)); |
2006 | 0 | auto textItem = std::make_shared<TextItem>(box->get_item_ID(), text); |
2007 | 0 | add_text_item(textItem); |
2008 | 0 | return textItem; |
2009 | 0 | } |