-
-
Notifications
You must be signed in to change notification settings - Fork 295
/
heif_context.cc
1501 lines (1120 loc) · 39.2 KB
/
heif_context.cc
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* HEIF codec.
* Copyright (c) 2017 struktur AG, Dirk Farin <farin@struktur.de>
*
* This file is part of libheif.
*
* libheif is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* libheif is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libheif. If not, see <http://www.gnu.org/licenses/>.
*/
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif
#include <assert.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <limits>
#include <utility>
#include <math.h>
#if ENABLE_PARALLEL_TILE_DECODING
#include <future>
#endif
#include "heif_context.h"
#include "heif_file.h"
#include "heif_image.h"
#include "heif_api_structs.h"
#include "heif_limits.h"
#include "heif_hevc.h"
#include "heif_plugin_registry.h"
#if HAVE_LIBDE265
#include "heif_decoder_libde265.h"
#endif
#if HAVE_X265
#include "heif_encoder_x265.h"
#endif
using namespace heif;
heif_encoder::heif_encoder(std::shared_ptr<heif::HeifContext> _context,
const struct heif_encoder_plugin* _plugin)
: context(_context),
plugin(_plugin)
{
}
heif_encoder::~heif_encoder()
{
release();
}
void heif_encoder::release()
{
if (encoder) {
plugin->free_encoder(encoder);
encoder = nullptr;
}
}
struct heif_error heif_encoder::alloc()
{
if (encoder == nullptr) {
struct heif_error error = plugin->new_encoder(&encoder);
// TODO: error handling
return error;
}
struct heif_error err = { heif_error_Ok, heif_suberror_Unspecified, kSuccess };
return err;
}
static int32_t readvec_signed(const std::vector<uint8_t>& data,int& ptr,int len)
{
const uint32_t high_bit = 0x80<<((len-1)*8);
uint32_t val=0;
while (len--) {
val <<= 8;
val |= data[ptr++];
}
bool negative = (val & high_bit) != 0;
val &= ~high_bit;
if (negative) {
return -(high_bit-val);
}
else {
return val;
}
return val;
}
static uint32_t readvec(const std::vector<uint8_t>& data,int& ptr,int len)
{
uint32_t val=0;
while (len--) {
val <<= 8;
val |= data[ptr++];
}
return val;
}
class ImageGrid
{
public:
Error parse(const std::vector<uint8_t>& data);
std::string dump() const;
uint32_t get_width() const { return m_output_width; }
uint32_t get_height() const { return m_output_height; }
uint16_t get_rows() const { return m_rows; }
uint16_t get_columns() const { return m_columns; }
private:
uint16_t m_rows;
uint16_t m_columns;
uint32_t m_output_width;
uint32_t m_output_height;
};
Error ImageGrid::parse(const std::vector<uint8_t>& data)
{
if (data.size() < 8) {
return Error(heif_error_Invalid_input,
heif_suberror_Invalid_grid_data,
"Less than 8 bytes of data");
}
uint8_t version = data[0];
(void)version; // version is unused
uint8_t flags = data[1];
int field_size = ((flags & 1) ? 32 : 16);
m_rows = static_cast<uint16_t>(data[2] +1);
m_columns = static_cast<uint16_t>(data[3] +1);
if (field_size == 32) {
if (data.size() < 12) {
return Error(heif_error_Invalid_input,
heif_suberror_Invalid_grid_data,
"Grid image data incomplete");
}
m_output_width = ((data[4] << 24) |
(data[5] << 16) |
(data[6] << 8) |
(data[7]));
m_output_height = ((data[ 8] << 24) |
(data[ 9] << 16) |
(data[10] << 8) |
(data[11]));
}
else {
m_output_width = ((data[4] << 8) |
(data[5]));
m_output_height = ((data[ 6] << 8) |
(data[ 7]));
}
return Error::Ok;
}
std::string ImageGrid::dump() const
{
std::ostringstream sstr;
sstr << "rows: " << m_rows << "\n"
<< "columns: " << m_columns << "\n"
<< "output width: " << m_output_width << "\n"
<< "output height: " << m_output_height << "\n";
return sstr.str();
}
class ImageOverlay
{
public:
Error parse(size_t num_images, const std::vector<uint8_t>& data);
std::string dump() const;
void get_background_color(uint16_t col[4]) const;
uint32_t get_canvas_width() const { return m_width; }
uint32_t get_canvas_height() const { return m_height; }
size_t get_num_offsets() const { return m_offsets.size(); }
void get_offset(size_t image_index, int32_t* x, int32_t* y) const;
private:
uint8_t m_version;
uint8_t m_flags;
uint16_t m_background_color[4];
uint32_t m_width;
uint32_t m_height;
struct Offset {
int32_t x,y;
};
std::vector<Offset> m_offsets;
};
Error ImageOverlay::parse(size_t num_images, const std::vector<uint8_t>& data)
{
Error eofError(heif_error_Invalid_input,
heif_suberror_Invalid_grid_data,
"Overlay image data incomplete");
if (data.size() < 2 + 4*2) {
return eofError;
}
m_version = data[0];
m_flags = data[1];
if (m_version != 0) {
std::stringstream sstr;
sstr << "Overlay image data version " << m_version << " is not implemented yet";
return Error(heif_error_Unsupported_feature,
heif_suberror_Unsupported_data_version,
sstr.str());
}
int field_len = ((m_flags & 1) ? 4 : 2);
int ptr=2;
if (ptr + 4*2 + 2*field_len + num_images*2*field_len > data.size()) {
return eofError;
}
for (int i=0;i<4;i++) {
uint16_t color = static_cast<uint16_t>(readvec(data,ptr,2));
m_background_color[i] = color;
}
m_width = readvec(data,ptr,field_len);
m_height = readvec(data,ptr,field_len);
m_offsets.resize(num_images);
for (size_t i=0;i<num_images;i++) {
m_offsets[i].x = readvec_signed(data,ptr,field_len);
m_offsets[i].y = readvec_signed(data,ptr,field_len);
}
return Error::Ok;
}
std::string ImageOverlay::dump() const
{
std::stringstream sstr;
sstr << "version: " << ((int)m_version) << "\n"
<< "flags: " << ((int)m_flags) << "\n"
<< "background color: " << m_background_color[0]
<< ";" << m_background_color[1]
<< ";" << m_background_color[2]
<< ";" << m_background_color[3] << "\n"
<< "canvas size: " << m_width << "x" << m_height << "\n"
<< "offsets: ";
for (const Offset& offset : m_offsets) {
sstr << offset.x << ";" << offset.y << " ";
}
sstr << "\n";
return sstr.str();
}
void ImageOverlay::get_background_color(uint16_t col[4]) const
{
for (int i=0;i<4;i++) {
col[i] = m_background_color[i];
}
}
void ImageOverlay::get_offset(size_t image_index, int32_t* x, int32_t* y) const
{
assert(image_index>=0 && image_index<m_offsets.size());
assert(x && y);
*x = m_offsets[image_index].x;
*y = m_offsets[image_index].y;
}
HeifContext::HeifContext()
{
#if HAVE_LIBDE265
heif::register_decoder(get_decoder_plugin_libde265());
#endif
#if HAVE_X265
heif::register_encoder(get_encoder_plugin_x265());
#endif
reset_to_empty_heif();
}
HeifContext::~HeifContext()
{
}
Error HeifContext::read_from_file(const char* input_filename)
{
m_heif_file = std::make_shared<HeifFile>();
Error err = m_heif_file->read_from_file(input_filename);
if (err) {
return err;
}
return interpret_heif_file();
}
Error HeifContext::read_from_memory(const void* data, size_t size)
{
m_heif_file = std::make_shared<HeifFile>();
Error err = m_heif_file->read_from_memory(data,size);
if (err) {
return err;
}
return interpret_heif_file();
}
void HeifContext::reset_to_empty_heif()
{
m_heif_file = std::make_shared<HeifFile>();
m_heif_file->new_empty_file();
m_all_images.clear();
m_top_level_images.clear();
m_primary_image.reset();
}
void HeifContext::write(StreamWriter& writer)
{
m_heif_file->write(writer);
}
std::string HeifContext::debug_dump_boxes() const
{
return m_heif_file->debug_dump_boxes();
}
void HeifContext::register_decoder(const heif_decoder_plugin* decoder_plugin)
{
if (decoder_plugin->init_plugin) {
(*decoder_plugin->init_plugin)();
}
m_decoder_plugins.insert(decoder_plugin);
}
const struct heif_decoder_plugin* HeifContext::get_decoder(enum heif_compression_format type) const
{
int highest_priority = 0;
const struct heif_decoder_plugin* best_plugin = nullptr;
// search global plugins
for (const auto* plugin : s_decoder_plugins) {
int priority = plugin->does_support_format(type);
if (priority > highest_priority) {
highest_priority = priority;
best_plugin = plugin;
}
}
// search context-local plugins (DEPRECATED)
for (const auto* plugin : m_decoder_plugins) {
int priority = plugin->does_support_format(type);
if (priority > highest_priority) {
highest_priority = priority;
best_plugin = plugin;
}
}
return best_plugin;
}
static bool item_type_is_image(const std::string& item_type)
{
return (item_type=="hvc1" ||
item_type=="grid" ||
item_type=="iden" ||
item_type=="iovl");
}
void HeifContext::remove_top_level_image(std::shared_ptr<Image> image)
{
std::vector<std::shared_ptr<Image>> new_list;
for (auto img : m_top_level_images) {
if (img != image) {
new_list.push_back(img);
}
}
m_top_level_images = new_list;
}
Error HeifContext::interpret_heif_file()
{
m_all_images.clear();
m_top_level_images.clear();
m_primary_image.reset();
// --- reference all non-hidden images
std::vector<heif_item_id> image_IDs = m_heif_file->get_item_IDs();
for (heif_item_id id : image_IDs) {
auto infe_box = m_heif_file->get_infe_box(id);
if (!infe_box) {
// TODO(farindk): Should we return an error instead of skipping the invalid id?
continue;
}
if (item_type_is_image(infe_box->get_item_type())) {
auto image = std::make_shared<Image>(this, id);
m_all_images.insert(std::make_pair(id, image));
if (!infe_box->is_hidden_item()) {
if (id==m_heif_file->get_primary_image_ID()) {
image->set_primary(true);
m_primary_image = image;
}
m_top_level_images.push_back(image);
}
}
}
if (!m_primary_image) {
return Error(heif_error_Invalid_input,
heif_suberror_Nonexisting_item_referenced,
"'pitm' box references a non-existing image");
}
// --- remove thumbnails from top-level images and assign to their respective image
auto iref_box = m_heif_file->get_iref_box();
if (iref_box) {
// m_top_level_images.clear();
for (auto& pair : m_all_images) {
auto& image = pair.second;
uint32_t type = iref_box->get_reference_type(image->get_id());
if (type==fourcc("thmb")) {
// --- this is a thumbnail image, attach to the main image
std::vector<heif_item_id> refs = iref_box->get_references(image->get_id());
if (refs.size() != 1) {
return Error(heif_error_Invalid_input,
heif_suberror_Unspecified,
"Too many thumbnail references");
}
image->set_is_thumbnail_of(refs[0]);
auto master_iter = m_all_images.find(refs[0]);
if (master_iter == m_all_images.end()) {
return Error(heif_error_Invalid_input,
heif_suberror_Nonexisting_item_referenced,
"Thumbnail references a non-existing image");
}
if (master_iter->second->is_thumbnail()) {
return Error(heif_error_Invalid_input,
heif_suberror_Nonexisting_item_referenced,
"Thumbnail references another thumbnail");
}
master_iter->second->add_thumbnail(image);
remove_top_level_image(image);
}
else if (type==fourcc("auxl")) {
// --- this is an auxiliary image
// check whether it is an alpha channel and attach to the main image if yes
std::vector<Box_ipco::Property> properties;
Error err = m_heif_file->get_properties(image->get_id(), properties);
if (err) {
return err;
}
std::shared_ptr<Box_auxC> auxC_property;
for (const auto& property : properties) {
auto auxC = std::dynamic_pointer_cast<Box_auxC>(property.property);
if (auxC) {
auxC_property = auxC;
}
}
if (!auxC_property) {
std::stringstream sstr;
sstr << "No auxC property for image " << image->get_id();
return Error(heif_error_Invalid_input,
heif_suberror_Auxiliary_image_type_unspecified,
sstr.str());
}
std::vector<heif_item_id> refs = iref_box->get_references(image->get_id());
if (refs.size() != 1) {
return Error(heif_error_Invalid_input,
heif_suberror_Unspecified,
"Too many auxiliary image references");
}
// alpha channel
if (auxC_property->get_aux_type() == "urn:mpeg:avc:2015:auxid:1" ||
auxC_property->get_aux_type() == "urn:mpeg:hevc:2015:auxid:1") {
image->set_is_alpha_channel_of(refs[0]);
auto master_iter = m_all_images.find(refs[0]);
master_iter->second->set_alpha_channel(image);
}
// depth channel
if (auxC_property->get_aux_type() == "urn:mpeg:hevc:2015:auxid:2") {
image->set_is_depth_channel_of(refs[0]);
auto master_iter = m_all_images.find(refs[0]);
master_iter->second->set_depth_channel(image);
auto subtypes = auxC_property->get_subtypes();
std::vector<std::shared_ptr<SEIMessage>> sei_messages;
Error err = decode_hevc_aux_sei_messages(subtypes, sei_messages);
for (auto& msg : sei_messages) {
auto depth_msg = std::dynamic_pointer_cast<SEIMessage_depth_representation_info>(msg);
if (depth_msg) {
image->set_depth_representation_info(*depth_msg);
}
}
}
remove_top_level_image(image);
}
else {
// 'image' is a normal image, keep it as a top-level image
}
}
}
// --- read through properties for each image and extract image resolutions
for (auto& pair : m_all_images) {
auto& image = pair.second;
std::vector<Box_ipco::Property> properties;
Error err = m_heif_file->get_properties(pair.first, properties);
if (err) {
return err;
}
bool ispe_read = false;
for (const auto& prop : properties) {
auto ispe = std::dynamic_pointer_cast<Box_ispe>(prop.property);
if (ispe) {
uint32_t width = ispe->get_width();
uint32_t height = ispe->get_height();
// --- check whether the image size is "too large"
if (width >= static_cast<uint32_t>(MAX_IMAGE_WIDTH) ||
height >= static_cast<uint32_t>(MAX_IMAGE_HEIGHT)) {
std::stringstream sstr;
sstr << "Image size " << width << "x" << height << " exceeds the maximum image size "
<< MAX_IMAGE_WIDTH << "x" << MAX_IMAGE_HEIGHT << "\n";
return Error(heif_error_Memory_allocation_error,
heif_suberror_Security_limit_exceeded,
sstr.str());
}
image->set_resolution(width, height);
ispe_read = true;
}
if (ispe_read) {
auto clap = std::dynamic_pointer_cast<Box_clap>(prop.property);
if (clap) {
image->set_resolution( clap->get_width_rounded(),
clap->get_height_rounded() );
}
auto irot = std::dynamic_pointer_cast<Box_irot>(prop.property);
if (irot) {
if (irot->get_rotation()==90 ||
irot->get_rotation()==270) {
// swap width and height
image->set_resolution( image->get_height(),
image->get_width() );
}
}
}
}
}
// --- read metadata and assign to image
for (heif_item_id id : image_IDs) {
std::string item_type = m_heif_file->get_item_type(id);
if (item_type == "Exif") {
std::shared_ptr<ImageMetadata> metadata = std::make_shared<ImageMetadata>();
metadata->item_id = id;
metadata->item_type = item_type;
Error err = m_heif_file->get_compressed_image_data(id, &(metadata->m_data));
if (err) {
return err;
}
//std::cerr.write((const char*)data.data(), data.size());
// --- assign metadata to the image
if (iref_box) {
uint32_t type = iref_box->get_reference_type(id);
if (type == fourcc("cdsc")) {
std::vector<uint32_t> refs = iref_box->get_references(id);
if (refs.size() != 1) {
return Error(heif_error_Invalid_input,
heif_suberror_Unspecified,
"Exif data not correctly assigned to image");
}
uint32_t exif_image_id = refs[0];
auto img_iter = m_all_images.find(exif_image_id);
if (img_iter == m_all_images.end()) {
return Error(heif_error_Invalid_input,
heif_suberror_Nonexisting_item_referenced,
"Exif data assigned to non-existing image");
}
img_iter->second->add_metadata(metadata);
}
}
}
}
return Error::Ok;
}
HeifContext::Image::Image(HeifContext* context, heif_item_id id)
: m_heif_context(context),
m_id(id)
{
}
HeifContext::Image::~Image()
{
}
Error HeifContext::Image::decode_image(std::shared_ptr<HeifPixelImage>& img,
heif_colorspace colorspace,
heif_chroma chroma,
const struct heif_decoding_options* options) const
{
Error err = m_heif_context->decode_image(m_id, img, options);
if (err) {
return err;
}
heif_chroma target_chroma = (chroma == heif_chroma_undefined ?
img->get_chroma_format() :
chroma);
heif_colorspace target_colorspace = (colorspace == heif_colorspace_undefined ?
img->get_colorspace() :
colorspace);
bool different_chroma = (target_chroma != img->get_chroma_format());
bool different_colorspace = (target_colorspace != img->get_colorspace());
if (different_chroma || different_colorspace) {
img = img->convert_colorspace(target_colorspace, target_chroma);
if (!img) {
return Error(heif_error_Unsupported_feature, heif_suberror_Unsupported_color_conversion);
}
}
return err;
}
Error HeifContext::decode_image(heif_item_id ID,
std::shared_ptr<HeifPixelImage>& img,
const struct heif_decoding_options* options) const
{
std::string image_type = m_heif_file->get_item_type(ID);
Error error;
// --- decode image, depending on its type
if (image_type == "hvc1") {
const struct heif_decoder_plugin* decoder_plugin = get_decoder(heif_compression_HEVC);
if (!decoder_plugin) {
return Error(heif_error_Unsupported_feature, heif_suberror_Unsupported_codec);
}
std::vector<uint8_t> data;
error = m_heif_file->get_compressed_image_data(ID, &data);
if (error) {
return error;
}
void* decoder;
struct heif_error err = decoder_plugin->new_decoder(&decoder);
if (err.code != heif_error_Ok) {
return Error(err.code, err.subcode, err.message);
}
err = decoder_plugin->push_data(decoder, data.data(), data.size());
if (err.code != heif_error_Ok) {
decoder_plugin->free_decoder(decoder);
return Error(err.code, err.subcode, err.message);
}
//std::shared_ptr<HeifPixelImage>* decoded_img;
heif_image* decoded_img = nullptr;
err = decoder_plugin->decode_image(decoder, &decoded_img);
if (err.code != heif_error_Ok) {
decoder_plugin->free_decoder(decoder);
return Error(err.code, err.subcode, err.message);
}
if (!decoded_img) {
// TODO(farindk): The plugin should return an error in this case.
decoder_plugin->free_decoder(decoder);
return Error(heif_error_Decoder_plugin_error, heif_suberror_Unspecified);
}
img = std::move(decoded_img->image);
heif_image_release(decoded_img);
decoder_plugin->free_decoder(decoder);
#if 0
FILE* fh = fopen("out.bin", "wb");
fwrite(data.data(), 1, data.size(), fh);
fclose(fh);
#endif
}
else if (image_type == "grid") {
std::vector<uint8_t> data;
error = m_heif_file->get_compressed_image_data(ID, &data);
if (error) {
return error;
}
error = decode_full_grid_image(ID, img, data);
if (error) {
return error;
}
}
else if (image_type == "iden") {
error = decode_derived_image(ID, img);
if (error) {
return error;
}
}
else if (image_type == "iovl") {
std::vector<uint8_t> data;
error = m_heif_file->get_compressed_image_data(ID, &data);
if (error) {
return error;
}
error = decode_overlay_image(ID, img, data);
if (error) {
return error;
}
}
else {
// Should not reach this, was already rejected by "get_image_data".
return Error(heif_error_Unsupported_feature,
heif_suberror_Unsupported_image_type);
}
// --- add alpha channel, if available
// TODO: this if statement is probably wrong. When we have a tiled image with alpha
// channel, then the alpha images should be associated with their respective tiles.
// However, the tile images are not part of the m_all_images list.
// Fix this, when we have a test image available.
if (m_all_images.find(ID) != m_all_images.end()) {
const auto imginfo = m_all_images.find(ID)->second;
std::shared_ptr<Image> alpha_image = imginfo->get_alpha_channel();
if (alpha_image) {
std::shared_ptr<HeifPixelImage> alpha;
Error err = alpha_image->decode_image(alpha);
if (err) {
return err;
}
// TODO: check that sizes are the same and that we have an Y channel
// BUT: is there any indication in the standard that the alpha channel should have the same size?
img->transfer_plane_from_image_as(alpha, heif_channel_Y, heif_channel_Alpha);
}
}
// --- apply image transformations
if (!options || options->ignore_transformations == false) {
std::vector<Box_ipco::Property> properties;
auto ipco_box = m_heif_file->get_ipco_box();
auto ipma_box = m_heif_file->get_ipma_box();
error = ipco_box->get_properties_for_item_ID(ID, ipma_box, properties);
for (const auto& property : properties) {
auto rot = std::dynamic_pointer_cast<Box_irot>(property.property);
if (rot) {
std::shared_ptr<HeifPixelImage> rotated_img;
error = img->rotate_ccw(rot->get_rotation(), rotated_img);
if (error) {
return error;
}
img = rotated_img;
}
auto mirror = std::dynamic_pointer_cast<Box_imir>(property.property);
if (mirror) {
error = img->mirror_inplace(mirror->get_mirror_axis() == Box_imir::MirrorAxis::Horizontal);
if (error) {
return error;
}
}
auto clap = std::dynamic_pointer_cast<Box_clap>(property.property);
if (clap) {
std::shared_ptr<HeifPixelImage> clap_img;
int img_width = img->get_width();
int img_height = img->get_height();
assert(img_width >= 0);
assert(img_height >= 0);
int left = clap->left_rounded(img_width);
int right = clap->right_rounded(img_width);
int top = clap->top_rounded(img_height);
int bottom = clap->bottom_rounded(img_height);
if (left < 0) { left = 0; }
if (top < 0) { top = 0; }
if (right >= img_width) { right = img_width-1; }
if (bottom >= img_height) { bottom = img_height-1; }
if (left >= right ||
top >= bottom) {
return Error(heif_error_Invalid_input,
heif_suberror_Invalid_clean_aperture);
}
std::shared_ptr<HeifPixelImage> cropped_img;
error = img->crop(left,right,top,bottom, cropped_img);
if (error) {
return error;
}
img = cropped_img;
}
}
}
return Error::Ok;
}
// TODO: this function only works with YCbCr images, chroma 4:2:0, and 8 bpp at the moment
// It will crash badly if we get anything else.
Error HeifContext::decode_full_grid_image(heif_item_id ID,
std::shared_ptr<HeifPixelImage>& img,
const std::vector<uint8_t>& grid_data) const
{
ImageGrid grid;
grid.parse(grid_data);
// std::cout << grid.dump();
auto iref_box = m_heif_file->get_iref_box();
if (!iref_box) {
return Error(heif_error_Invalid_input,
heif_suberror_No_iref_box,
"No iref box available, but needed for grid image");
}
std::vector<heif_item_id> image_references = iref_box->get_references(ID);
if ((int)image_references.size() != grid.get_rows() * grid.get_columns()) {
std::stringstream sstr;
sstr << "Tiled image with " << grid.get_rows() << "x" << grid.get_columns() << "="
<< (grid.get_rows() * grid.get_columns()) << " tiles, but only "
<< image_references.size() << " tile images in file";
return Error(heif_error_Invalid_input,
heif_suberror_Missing_grid_images,
sstr.str());
}
// --- generate image of full output size
int w = grid.get_width();
int h = grid.get_height();
int bpp = 8; // TODO: how do we know ?
if (w >= MAX_IMAGE_WIDTH || h >= MAX_IMAGE_HEIGHT) {
std::stringstream sstr;
sstr << "Image size " << w << "x" << h << " exceeds the maximum image size "
<< MAX_IMAGE_WIDTH << "x" << MAX_IMAGE_HEIGHT << "\n";
return Error(heif_error_Memory_allocation_error,
heif_suberror_Security_limit_exceeded,
sstr.str());
}
img = std::make_shared<HeifPixelImage>();