File: heif_info.cc

package info (click to toggle)
libheif 1.21.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 7,872 kB
  • sloc: cpp: 91,129; python: 3,032; sh: 1,048; ansic: 453; javascript: 160; makefile: 76
file content (958 lines) | stat: -rw-r--r-- 33,151 bytes parent folder | download | duplicates (2)
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
/*
  libheif example application "heif-info".

  MIT License

  Copyright (c) 2017 Dirk Farin <dirk.farin@gmail.com>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/
#include <cstdint>

#include <errno.h>
#include <string.h>
#include <vector>

#if defined(HAVE_UNISTD_H)

#include <unistd.h>

#else
#define STDOUT_FILENO 1
#endif

#include <libheif/heif.h>
#include <libheif/heif_library.h>
#include <libheif/heif_regions.h>
#include <libheif/heif_properties.h>
#include <libheif/heif_experimental.h>
#include "libheif/heif_sequences.h"
#include <libheif/heif_text.h>

#include <fstream>
#include <iostream>
#include <sstream>
#include <memory>
#include <getopt.h>
#include <cassert>
#include <cstdio>
#include <filesystem>
#include "common.h"
#include "libheif/heif_items.h"


/*
  image: 20005 (1920x1080), primary
    thumbnail: 20010 (320x240)
    alpha channel: 20012 (1920x1080)
    metadata: Exif

  image: 1920x1080 (20005), primary
    thumbnail: 320x240 (20010)
    alpha channel: 1920x1080 (20012)

info *file
info -w 20012 -o out.265 *file
info -d // dump
 */

int option_disable_limits = 0;

static option long_options[] = {
    //{"write-raw", required_argument, 0, 'w' },
    //{"output",    required_argument, 0, 'o' },
    {(char* const) "dump-boxes", no_argument, 0, 'd'},
    {(char* const) "disable-limits", no_argument, &option_disable_limits, 1},
    {(char* const) "help",       no_argument, 0, 'h'},
    {(char* const) "version",    no_argument, 0, 'v'},
    {0, 0,                                    0, 0}
};


void show_help(const char* argv0)
{
  std::filesystem::path p(argv0);
  std::string filename = p.filename().string();

  std::stringstream sstr;
  sstr << " " << filename << "  libheif version: " << heif_get_version();

  std::string title = sstr.str();

  std::cerr << title << "\n"
            << std::string(title.length() + 1, '-') << "\n"
            << "Usage: " << filename << " [options] <HEIF-image>\n"
            << "\n"
               "options:\n"
               //fprintf(stderr,"  -w, --write-raw ID   write raw compressed data of image 'ID'\n");
               //fprintf(stderr,"  -o, --output NAME    output file name for image selected by -w\n");
               "  -d, --dump-boxes     show a low-level dump of all MP4 file boxes\n"
               "      --disable-limits disable all security limits (do not use in production environment)\n"
               "  -h, --help           show help\n"
               "  -v, --version        show version\n";
}


class LibHeifInitializer
{
public:
  LibHeifInitializer() { heif_init(nullptr); }

  ~LibHeifInitializer() { heif_deinit(); }
};


int main(int argc, char** argv)
{
  // This takes care of initializing libheif and also deinitializing it at the end to free all resources.
  LibHeifInitializer initializer;

  bool dump_boxes = false;

  bool write_raw_image = false;
  heif_item_id raw_image_id;
  std::string output_filename = "output.265";

  while (true) {
    int option_index = 0;
    int c = getopt_long(argc, argv, "dhv", long_options, &option_index);
    if (c == -1)
      break;

    switch (c) {
      case 'd':
        dump_boxes = true;
        break;
      case 'h':
        show_help(argv[0]);
        return 0;
      case 'w':
        write_raw_image = true;
        raw_image_id = atoi(optarg);
        break;
      case 'o':
        output_filename = optarg;
        break;
      case 'v':
        heif_examples::show_version();
        return 0;
    }
  }

  if (optind != argc - 1) {
    show_help(argv[0]);
    return 0;
  }


  (void) raw_image_id;
  (void) write_raw_image;

  const char* input_filename = argv[optind];

  // ==============================================================================
  //   show MIME type

  {
    const static int bufSize = 50;

    uint8_t buf[bufSize];
    FILE* fh = fopen(input_filename, "rb");
    if (fh) {
      std::cout << "MIME type: ";
      int n = (int) fread(buf, 1, bufSize, fh);
      const char* mime_type = heif_get_file_mime_type(buf, n);
      if (*mime_type == 0) {
        std::cout << "unknown\n";
      }
      else {
        std::cout << mime_type << "\n";
      }

      fclose(fh);

      char fourcc[5];
      fourcc[4] = 0;
      heif_brand_to_fourcc(heif_read_main_brand(buf, bufSize), fourcc);
      std::cout << "main brand: " << fourcc << "\n";

      heif_brand2* brands = nullptr;
      int nBrands = 0;
      heif_error err = heif_list_compatible_brands(buf, n, &brands, &nBrands);
      if (err.code) {
        std::cerr << "error reading brands: " << err.message << "\n";
      }
      else {
        std::cout << "compatible brands: ";
        for (int i = 0; i < nBrands; i++) {
          heif_brand_to_fourcc(brands[i], fourcc);
          if (i > 0) {
            std::cout << ", ";
          }
          std::cout << fourcc;
        }

        std::cout << "\n";

        heif_free_list_of_compatible_brands(brands);
      }
    }
    else {
      if (errno == ENOENT) {
        std::cerr << "Input file does not exist.\n";
        exit(10);
      }
    }
  }

  // ==============================================================================

  std::shared_ptr<heif_context> ctx(heif_context_alloc(),
                                    [](heif_context* c) { heif_context_free(c); });
  if (!ctx) {
    fprintf(stderr, "Could not create context object\n");
    return 1;
  }

  if (option_disable_limits) {
    heif_context_set_security_limits(ctx.get(), heif_get_disabled_security_limits());
  }

  heif_error err;
  err = heif_context_read_from_file(ctx.get(), input_filename, nullptr);

  if (dump_boxes) {
    heif_context_debug_dump_boxes_to_file(ctx.get(), STDOUT_FILENO); // dump to stdout

    if (err.code != 0) {
      std::cerr << "Could not read HEIF/AVIF file: " << err.message << "\n";
      return 1;
    }

    return 0;
  }

  if (err.code != 0) {
    std::cerr << "Could not read HEIF/AVIF file: " << err.message << "\n";
    return 1;
  }


  // ==============================================================================


  int numImages = heif_context_get_number_of_top_level_images(ctx.get());
  std::vector<heif_item_id> IDs(numImages);
  heif_context_get_list_of_top_level_image_IDs(ctx.get(), IDs.data(), numImages);

  for (int i = 0; i < numImages; i++) {
    std::cout << "\n";

    heif_image_handle* handle;
    heif_error err = heif_context_get_image_handle(ctx.get(), IDs[i], &handle);
    if (err.code) {
      std::cerr << err.message << "\n";
      return 10;
    }

    int width = heif_image_handle_get_width(handle);
    int height = heif_image_handle_get_height(handle);

    int primary = heif_image_handle_is_primary_image(handle);

    printf("image: %dx%d (id=%d)%s\n", width, height, IDs[i], primary ? ", primary" : "");

    heif_image_tiling tiling;
    err = heif_image_handle_get_image_tiling(handle, true, &tiling);
    if (err.code) {
      std::cerr << "Error while trying to get image tiling information: " << err.message << "\n";
    }
    else if (tiling.num_columns != 1 || tiling.num_rows != 1) {
      std::cout << "  tiles: " << tiling.num_columns << "x" << tiling.num_rows
                << ", tile size: " << tiling.tile_width << "x" << tiling.tile_height << "\n";
    }

    heif_colorspace colorspace;
    heif_chroma chroma;
    err = heif_image_handle_get_preferred_decoding_colorspace(handle, &colorspace, &chroma);
    if (err.code) {
      std::cerr << err.message << "\n";
      return 10;
    }

    printf("  colorspace: ");
    switch (colorspace) {
      case heif_colorspace_YCbCr:
        printf("YCbCr, ");
        break;
      case heif_colorspace_RGB:
        printf("RGB");
        break;
      case heif_colorspace_monochrome:
        printf("monochrome");
        break;
      case heif_colorspace_nonvisual:
        printf("non-visual");
        break;
      default:
        printf("unknown");
        break;
    }

    if (colorspace==heif_colorspace_YCbCr) {
      switch (chroma) {
        case heif_chroma_420:
          printf("4:2:0");
          break;
        case heif_chroma_422:
          printf("4:2:2");
          break;
        case heif_chroma_444:
          printf("4:4:4");
          break;
        default:
          printf("unknown");
          break;
      }
    }

    printf("\n");

    // --- bit depth

    int luma_depth = heif_image_handle_get_luma_bits_per_pixel(handle);
    int chroma_depth = heif_image_handle_get_chroma_bits_per_pixel(handle);

    printf("  bit depth: ");
    if (chroma == heif_chroma_monochrome || luma_depth==chroma_depth) {
      printf("%d\n", luma_depth);
    }
    else {
      printf("%d,%d\n", luma_depth, chroma_depth);
    }


    // --- thumbnails

    int nThumbnails = heif_image_handle_get_number_of_thumbnails(handle);
    std::vector<heif_item_id> thumbnailIDs(nThumbnails);

    nThumbnails = heif_image_handle_get_list_of_thumbnail_IDs(handle, thumbnailIDs.data(), nThumbnails);

    for (int thumbnailIdx = 0; thumbnailIdx < nThumbnails; thumbnailIdx++) {
      heif_image_handle* thumbnail_handle;
      err = heif_image_handle_get_thumbnail(handle, thumbnailIDs[thumbnailIdx], &thumbnail_handle);
      if (err.code) {
        std::cerr << err.message << "\n";
        return 10;
      }

      int th_width = heif_image_handle_get_width(thumbnail_handle);
      int th_height = heif_image_handle_get_height(thumbnail_handle);

      printf("  thumbnail: %dx%d\n", th_width, th_height);

      heif_image_handle_release(thumbnail_handle);
    }


    // --- color profile

    uint32_t profileType = heif_image_handle_get_color_profile_type(handle);
    printf("  color profile: %s\n", profileType ? heif_examples::fourcc_to_string(profileType).c_str() : "no");


    // --- depth information

    bool has_depth = heif_image_handle_has_depth_image(handle);
    bool has_alpha = heif_image_handle_has_alpha_channel(handle);
    bool premultiplied_alpha = false;

    if (has_alpha) {
      premultiplied_alpha = heif_image_handle_is_premultiplied_alpha(handle);
    }

    printf("  alpha channel: %s %s\n", has_alpha ? "yes" : "no",
           premultiplied_alpha ? "(premultiplied)" : "");
    printf("  depth channel: %s\n", has_depth ? "yes" : "no");

    heif_item_id depth_id;
    int nDepthImages = heif_image_handle_get_list_of_depth_image_IDs(handle, &depth_id, 1);
    if (has_depth) { assert(nDepthImages == 1); }
    else { assert(nDepthImages == 0); }
    (void) nDepthImages;

    if (has_depth) {
      heif_image_handle* depth_handle;
      err = heif_image_handle_get_depth_image_handle(handle, depth_id, &depth_handle);
      if (err.code) {
        fprintf(stderr, "cannot get depth image: %s\n", err.message);
        return 1;
      }

      printf("    size: %dx%d\n",
             heif_image_handle_get_width(depth_handle),
             heif_image_handle_get_height(depth_handle));

      int depth_luma_bpp = heif_image_handle_get_luma_bits_per_pixel(depth_handle);
      printf("    bits per pixel: %d\n", depth_luma_bpp);


      const heif_depth_representation_info* depth_info;
      if (heif_image_handle_get_depth_image_representation_info(handle, depth_id, &depth_info)) {

        printf("    z-near: ");
        if (depth_info->has_z_near) printf("%f\n", depth_info->z_near); else printf("undefined\n");
        printf("    z-far:  ");
        if (depth_info->has_z_far) printf("%f\n", depth_info->z_far); else printf("undefined\n");
        printf("    d-min:  ");
        if (depth_info->has_d_min) printf("%f\n", depth_info->d_min); else printf("undefined\n");
        printf("    d-max:  ");
        if (depth_info->has_d_max) printf("%f\n", depth_info->d_max); else printf("undefined\n");

        printf("    representation: ");
        switch (depth_info->depth_representation_type) {
          case heif_depth_representation_type_uniform_inverse_Z:
            printf("inverse Z\n");
            break;
          case heif_depth_representation_type_uniform_disparity:
            printf("uniform disparity\n");
            break;
          case heif_depth_representation_type_uniform_Z:
            printf("uniform Z\n");
            break;
          case heif_depth_representation_type_nonuniform_disparity:
            printf("non-uniform disparity\n");
            break;
          default:
            printf("unknown\n");
        }

        if (depth_info->has_d_min || depth_info->has_d_max) {
          printf("    disparity_reference_view: %d\n", depth_info->disparity_reference_view);
        }

        heif_depth_representation_info_free(depth_info);
      }

      heif_image_handle_release(depth_handle);
    }

    // --- metadata

    int numMetadata = heif_image_handle_get_number_of_metadata_blocks(handle, nullptr);
    printf("metadata:\n");
    if (numMetadata > 0) {
      std::vector<heif_item_id> ids(numMetadata);
      heif_image_handle_get_list_of_metadata_block_IDs(handle, nullptr, ids.data(), numMetadata);

      for (int n = 0; n < numMetadata; n++) {
        std::string itemtype = heif_image_handle_get_metadata_type(handle, ids[n]);
        std::string contenttype = heif_image_handle_get_metadata_content_type(handle, ids[n]);
        std::string item_uri_type = heif_image_handle_get_metadata_item_uri_type(handle, ids[n]);
        std::string ID{"unknown"};
        if (itemtype == "Exif") {
          ID = itemtype;
        }
        else if (itemtype == "uri ") {
          ID = itemtype + "/" + item_uri_type;
        }
        else if (contenttype == "application/rdf+xml") {
          ID = "XMP";
        }
        else {
          ID = itemtype + "/" + contenttype;
        }

        printf("  %s: %zu bytes\n", ID.c_str(), heif_image_handle_get_metadata_size(handle, ids[n]));
      }
    }
    else {
      printf("  none\n");
    }

    // --- transforms

#define MAX_PROPERTIES 50
    heif_property_id transforms[MAX_PROPERTIES];
    int nTransforms = heif_item_get_transformation_properties(ctx.get(),
                                                              IDs[i],
                                                              transforms,
                                                              MAX_PROPERTIES);
    printf("transformations:\n");
    int image_width = heif_image_handle_get_ispe_width(handle);
    int image_height = heif_image_handle_get_ispe_height(handle);

    if (nTransforms) {
      for (int k = 0; k < nTransforms; k++) {
        switch (heif_item_get_property_type(ctx.get(), IDs[i], transforms[k])) {
          case heif_item_property_type_transform_mirror:
            printf("  mirror: %s\n", heif_item_get_property_transform_mirror(ctx.get(), IDs[i], transforms[k]) == heif_transform_mirror_direction_horizontal ? "horizontal" : "vertical");
            break;
          case heif_item_property_type_transform_rotation: {
            int angle = heif_item_get_property_transform_rotation_ccw(ctx.get(), IDs[i], transforms[k]);
            printf("  angle (ccw): %d\n", angle);
            if (angle==90 || angle==270) {
              std::swap(image_width, image_height);
            }
            break;
          }
          case heif_item_property_type_transform_crop: {
            int left,top,right,bottom;
            heif_item_get_property_transform_crop_borders(ctx.get(), IDs[i], transforms[k], image_width, image_height,
                                                          &left, &top, &right, &bottom);
            printf("  crop: left=%d top=%d right=%d bottom=%d\n", left,top,right,bottom);
            break;
          }
          default:
            assert(false);
        }
      }
    }
    else {
      printf("  none\n");
    }

    // --- regions

    int numRegionItems = heif_image_handle_get_number_of_region_items(handle);
    printf("region annotations:\n");
    if (numRegionItems > 0) {
      std::vector<heif_item_id> region_items(numRegionItems);
      heif_image_handle_get_list_of_region_item_ids(handle, region_items.data(), numRegionItems);
      for (heif_item_id region_item_id : region_items) {
        heif_region_item* region_item;
        err = heif_context_get_region_item(ctx.get(), region_item_id, &region_item);

        uint32_t reference_width, reference_height;
        heif_region_item_get_reference_size(region_item, &reference_width, &reference_height);
        int numRegions = heif_region_item_get_number_of_regions(region_item);
        printf("  id: %u, reference_width: %u, reference_height: %u, %d regions\n",
               region_item_id,
               reference_width,
               reference_height,
               numRegions);

        std::vector<heif_region*> regions(numRegions);
        numRegions = heif_region_item_get_list_of_regions(region_item, regions.data(), numRegions);
        for (int j = 0; j < numRegions; j++) {
          printf("    region %d\n", j);
          heif_region_type type = heif_region_get_type(regions[j]);
          if (type == heif_region_type_point) {
            int32_t x;
            int32_t y;
            heif_region_get_point(regions[j], &x, &y);
            printf("      point [x=%i, y=%i]\n", x, y);
          }
          else if (type == heif_region_type_rectangle) {
            int32_t x;
            int32_t y;
            uint32_t w;
            uint32_t h;
            heif_region_get_rectangle(regions[j], &x, &y, &w, &h);
            printf("      rectangle [x=%i, y=%i, w=%u, h=%u]\n", x, y, w, h);
          }
          else if (type == heif_region_type_ellipse) {
            int32_t x;
            int32_t y;
            uint32_t rx;
            uint32_t ry;
            heif_region_get_ellipse(regions[j], &x, &y, &rx, &ry);
            printf("      ellipse [x=%i, y=%i, r_x=%u, r_y=%u]\n", x, y, rx, ry);
          }
          else if (type == heif_region_type_polygon) {
            int32_t numPoints = heif_region_get_polygon_num_points(regions[j]);
            std::vector<int32_t> pts(numPoints*2);
            heif_region_get_polygon_points(regions[j], pts.data());
            printf("      polygon [");
            for (int p=0;p<numPoints;p++) {
              printf("(%d;%d)", pts[2*p+0], pts[2*p+1]);
            }
            printf("]\n");
          }
          else if (type == heif_region_type_referenced_mask) {
            int32_t x;
            int32_t y;
            uint32_t w;
            uint32_t h;
            heif_item_id referenced_item;
            heif_region_get_referenced_mask_ID(regions[j], &x, &y, &w, &h, &referenced_item);
            printf("      referenced mask [x=%i, y=%i, w=%u, h=%u, item=%u]\n", x, y, w, h, referenced_item);
          }
          else if (type == heif_region_type_polyline) {
            int32_t numPoints = heif_region_get_polyline_num_points(regions[j]);
            std::vector<int32_t> pts(numPoints*2);
            heif_region_get_polyline_points(regions[j], pts.data());
            printf("      polyline [");
            for (int p=0;p<numPoints;p++) {
              printf("(%d;%d)", pts[2*p+0], pts[2*p+1]);
            }
            printf("]\n");
          }
          else if (type == heif_region_type_inline_mask) {
            int32_t x;
            int32_t y;
            uint32_t w;
            uint32_t h;
            size_t data_len = heif_region_get_inline_mask_data_len(regions[j]);
            std::vector<uint8_t> mask_data(data_len);
            heif_region_get_inline_mask_data(regions[j], &x, &y, &w, &h, mask_data.data());
            printf("      inline mask [x=%i, y=%i, w=%u, h=%u, data len=%zu]\n", x, y, w, h, mask_data.size());
          }
      }

        heif_region_release_many(regions.data(), numRegions);
        heif_region_item_release(region_item);

        heif_property_id properties[MAX_PROPERTIES];
        int nDescr = heif_item_get_properties_of_type(ctx.get(),
                                                      region_item_id,
                                                      heif_item_property_type_user_description,
                                                      properties,
                                                      MAX_PROPERTIES);

        for (int k = 0; k < nDescr; k++) {
          heif_property_user_description* udes;
          err = heif_item_get_property_user_description(ctx.get(),
                                                        region_item_id,
                                                        properties[k],
                                                        &udes);
          if (err.code == 0) {
            printf("    user description:\n");
            printf("      lang: %s\n", udes->lang);
            printf("      name: %s\n", udes->name);
            printf("      description: %s\n", udes->description);
            printf("      tags: %s\n", udes->tags);
            heif_property_user_description_release(udes);
          }
        }
      }
    }
    else {
      printf("  none\n");
    }

    // --- text items
    int numTextItems = heif_image_handle_get_number_of_text_items(handle);
    printf("text items:\n");

    if (numTextItems > 0) {
      std::vector<heif_item_id> text_items(numTextItems);
      heif_image_handle_get_list_of_text_item_ids(handle, text_items.data(), numTextItems);
      for (heif_item_id text_item_id : text_items) {
        struct heif_text_item* text_item;
        err = heif_context_get_text_item(ctx.get(), text_item_id, &text_item);
        const char* text_content = heif_text_item_get_content(text_item);
        printf("  text item: %s\n", text_content);
        heif_string_release(text_content);

        char* elng;
        err = heif_item_get_property_extended_language(ctx.get(),
                                                       text_item_id,
                                                       &elng);
        if (err.code == 0) {
          printf("    extended language: %s\n", elng);
        }
        heif_string_release(elng);
      }
    }
    else {
      printf("  none\n");
    }

    // --- properties

    printf("properties:\n");

    // user descriptions

    bool properties_shown = false;

    heif_property_id propertyIds[MAX_PROPERTIES];
    int count;
    count = heif_item_get_properties_of_type(ctx.get(), IDs[i], heif_item_property_type_user_description,
                                             propertyIds, MAX_PROPERTIES);

    if (count > 0) {
      for (int p = 0; p < count; p++) {
        heif_property_user_description* udes;
        err = heif_item_get_property_user_description(ctx.get(), IDs[i], propertyIds[p], &udes);
        if (err.code) {
          std::cerr << "Error reading udes " << IDs[i] << "/" << propertyIds[p] << "\n";
        }
        else {
          printf("  user description:\n");
          printf("    lang: %s\n", udes->lang);
          printf("    name: %s\n", udes->name);
          printf("    description: %s\n", udes->description);
          printf("    tags: %s\n", udes->tags);
          properties_shown = true;

          heif_property_user_description_release(udes);
        }
      }
    }

    // --- camera intrinsic and extrinsic parameters

    if (heif_image_handle_has_camera_intrinsic_matrix(handle)) {
      heif_camera_intrinsic_matrix matrix{};
      heif_image_handle_get_camera_intrinsic_matrix(handle, &matrix);
      printf("  camera intrinsic matrix:\n");
      printf("    focal length: %f; %f\n", matrix.focal_length_x, matrix.focal_length_y);
      printf("    principal point: %f; %f\n", matrix.principal_point_x, matrix.principal_point_y);
      printf("    skew: %f\n", matrix.skew);
      properties_shown = true;
    }

    if (heif_image_handle_has_camera_extrinsic_matrix(handle)) {
      heif_camera_extrinsic_matrix* matrix;
      heif_image_handle_get_camera_extrinsic_matrix(handle, &matrix);
      double rot[9];
      heif_camera_extrinsic_matrix_get_rotation_matrix(matrix, rot);
      printf("  camera extrinsic matrix:\n");
      printf("    rotation matrix:\n");
      printf("      %6.3f %6.3f %6.3f\n", rot[0], rot[1], rot[2]);
      printf("      %6.3f %6.3f %6.3f\n", rot[3], rot[4], rot[5]);
      printf("      %6.3f %6.3f %6.3f\n", rot[6], rot[7], rot[8]);
      heif_camera_extrinsic_matrix_release(matrix);
      properties_shown = true;
    }


    uint32_t aspect_h, aspect_v;
    int has_pasp = heif_image_handle_get_pixel_aspect_ratio(handle, &aspect_h, &aspect_v);
    if (has_pasp) {
      std::cout << "  pixel aspect ratio: " << aspect_h << "/" << aspect_v << "\n";
      properties_shown = true;
    }

    heif_content_light_level clli{};
    if (heif_image_handle_get_content_light_level(handle, &clli)) {
      std::cout << "  content light level (clli):\n"
                << "    max content light level: " << clli.max_content_light_level << "\n"
                << "    max pic average light level: " << clli.max_pic_average_light_level << "\n";
      properties_shown = true;
    }

    heif_mastering_display_colour_volume mdcv;
    if (heif_image_handle_get_mastering_display_colour_volume(handle, &mdcv)) {

      heif_decoded_mastering_display_colour_volume decoded_mdcv;
      err = heif_mastering_display_colour_volume_decode(&mdcv, &decoded_mdcv);

      std::cout << "  mastering display color volume:\n"
                << "    display_primaries (x,y): "
                << "(" << decoded_mdcv.display_primaries_x[0] << ";" << decoded_mdcv.display_primaries_y[0] << "), "
                << "(" << decoded_mdcv.display_primaries_x[1] << ";" << decoded_mdcv.display_primaries_y[1] << "), "
                << "(" << decoded_mdcv.display_primaries_x[2] << ";" << decoded_mdcv.display_primaries_y[2] << ")\n";

      std::cout << "    white point (x,y): (" << decoded_mdcv.white_point_x << ";" << decoded_mdcv.white_point_y << ")\n";
      std::cout << "    max display mastering luminance: " << decoded_mdcv.max_display_mastering_luminance << "\n";
      std::cout << "    min display mastering luminance: " << decoded_mdcv.min_display_mastering_luminance << "\n";
      properties_shown = true;
    }

    // --- GIMI

    const char* id = heif_image_handle_get_gimi_content_id(handle);
    if (id) {
      std::cout << "  GIMI content ID: " << id << "\n";
      heif_string_release(id);
      properties_shown = true;
    }

    if (!properties_shown) {
      std::cout << "none\n";
    }

    heif_image_handle_release(handle);
  }

#if 0
  std::cout << "num images: " << heif_context_get_number_of_top_level_images(ctx.get()) << "\n";

  heif_image_handle* handle;
  err = heif_context_get_primary_image_handle(ctx.get(), &handle);
  if (err.code != 0) {
    std::cerr << "Could not get primage image handle: " << err.message << "\n";
    return 1;
  }

  heif_image* image;
  err = heif_decode_image(handle, &image, heif_colorspace_undefined, heif_chroma_undefined, NULL);
  if (err.code != 0) {
    heif_image_handle_release(handle);
    std::cerr << "Could not decode primage image: " << err.message << "\n";
    return 1;
  }

  heif_image_release(image);
  heif_image_handle_release(handle);
#endif

  // ==============================================================================

  heif_context* context = ctx.get();

  uint32_t nTracks = heif_context_number_of_sequence_tracks(context);

  if (nTracks > 0) {
    std::cout << "\n";

    uint64_t timescale = heif_context_get_sequence_timescale(context);
    std::cout << "sequence time scale: " << timescale << " Hz\n";

    uint64_t duration = heif_context_get_sequence_duration(context);
    std::cout << "sequence duration: " << ((double)duration)/(double)timescale << " seconds\n";

              //    TrackOptions

    std::vector<uint32_t> track_ids(nTracks);

    heif_context_get_track_ids(context, track_ids.data());

    for (uint32_t id : track_ids) {
      heif_track* track = heif_context_get_track(context, id);

      heif_track_type handler = heif_track_get_track_handler_type(track);
      std::cout << "track " << id << "\n";
      std::cout << "  handler: '" << heif_examples::fourcc_to_string(handler) << "' = ";

      switch (handler) {
        case heif_track_type_image_sequence:
          std::cout << "image sequence\n";
          break;
        case heif_track_type_video:
          std::cout << "video\n";
          break;
        case heif_track_type_auxiliary:
          std::cout << "auxiliary ";
          {
            heif_auxiliary_track_info_type type = heif_track_get_auxiliary_info_type(track);
            switch (type) {
              case heif_auxiliary_track_info_type_unknown: {
                const char* s = heif_track_get_auxiliary_info_type_urn(track);
                std::cout << "(unknown: " << s << ")\n";
                heif_string_release(s);
                break;
              }
              case heif_auxiliary_track_info_type_alpha:
                std::cout << "(alpha)\n";
                break;
            }
          }
          break;
        case heif_track_type_metadata:
          std::cout << "metadata\n";
          break;
        default:
          std::cout << "unknown\n";
          break;
      }

      if (handler == heif_track_type_video ||
          handler == heif_track_type_image_sequence) {
        uint16_t w, h;
        heif_track_get_image_resolution(track, &w, &h);
        std::cout << "  resolution: " << w << "x" << h << "\n";
      }

      uint32_t sampleEntryType = heif_track_get_sample_entry_type_of_first_cluster(track);
      std::cout << "  sample entry type: " << heif_examples::fourcc_to_string(sampleEntryType) << "\n";

      if (sampleEntryType == heif_fourcc('u', 'r', 'i', 'm')) {
        const char* uri;
        err = heif_track_get_urim_sample_entry_uri_of_first_cluster(track, &uri);
        if (err.code) {
          std::cerr << "error reading urim-track uri: " << err.message << "\n";
        }
        std::cout << "  uri: " << uri << "\n";
        heif_string_release(uri);
      }

      std::cout << "  sample auxiliary information: ";
      int nSampleAuxTypes = heif_track_get_number_of_sample_aux_infos(track);

      std::vector<heif_sample_aux_info_type> aux_types(nSampleAuxTypes);
      heif_track_get_sample_aux_info_types(track, aux_types.data());

      for (size_t i=0;i<aux_types.size();i++) {
        if (i) { std::cout << ", "; }
        std::cout << heif_examples::fourcc_to_string(aux_types[i].type);
      }

      if (nSampleAuxTypes==0) {
        std::cout << "---";
      }
      std::cout << "\n";


      size_t nRefTypes = heif_track_get_number_of_track_reference_types(track);

      if (nRefTypes > 0) {
        std::cout << "  references:\n";
        std::vector<uint32_t> refTypes(nRefTypes);
        heif_track_get_track_reference_types(track, refTypes.data());

        for (uint32_t refType : refTypes) {
          std::cout << "    " << heif_examples::fourcc_to_string(refType) << ": ";

          size_t n = heif_track_get_number_of_track_reference_of_type(track, refType);
          std::vector<uint32_t> track_ids(n);
          heif_track_get_references_from_track(track, refType, track_ids.data());
          for (size_t i=0;i<n;i++) {
            if (i>0) std::cout << ", ";
            std::cout << "track#" << track_ids[i];
          }
          std::cout << "\n";
        }
      }

      const char* gimi_track_id = heif_track_get_gimi_track_content_id(track);
      if (gimi_track_id) {
        std::cout << "  GIMI track id: " << gimi_track_id << "\n";
        heif_string_release(gimi_track_id);
      }

      heif_track_release(track);
    }
  }

  // ==============================================================================

  {
    int nItems = heif_context_get_number_of_items(context);
    if (nItems > 0) {
      std::cout << "MIME items:\n";
      std::vector<heif_item_id> item_ids(nItems);
      heif_context_get_list_of_item_IDs(context, item_ids.data(), nItems);

      for (auto id : item_ids) {
        uint32_t type = heif_item_get_item_type(context, id);
        if (type == heif_item_type_mime) {
          const char* mimeType = heif_item_get_mime_item_content_type(context, id);
          std::cout << "  " << id << " : " << mimeType << "\n";
        }
      }
    }
  }

  return 0;
}