| 1 |
/* |
| 2 |
libheif example application "heif-info". |
| 3 |
|
| 4 |
MIT License |
| 5 |
|
| 6 |
Copyright (c) 2017 Dirk Farin <[email protected]> |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
of this software and associated documentation files (the "Software"), to deal |
| 10 |
in the Software without restriction, including without limitation the rights |
| 11 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
copies of the Software, and to permit persons to whom the Software is |
| 13 |
furnished to do so, subject to the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included in all |
| 16 |
copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 |
SOFTWARE. |
| 25 |
*/ |
| 26 |
#include <cstdint> |
| 27 |
|
| 28 |
#include <errno.h> |
| 29 |
#include <string.h> |
| 30 |
#include <vector> |
| 31 |
|
| 32 |
#if defined(HAVE_UNISTD_H) |
| 33 |
|
| 34 |
#include <unistd.h> |
| 35 |
|
| 36 |
#else |
| 37 |
#define STDOUT_FILENO 1 |
| 38 |
#endif |
| 39 |
|
| 40 |
#include <libheif/heif.h> |
| 41 |
#include <libheif/heif_library.h> |
| 42 |
#include <libheif/heif_regions.h> |
| 43 |
#include <libheif/heif_properties.h> |
| 44 |
#include <libheif/heif_experimental.h> |
| 45 |
#include "libheif/heif_sequences.h" |
| 46 |
#include <libheif/heif_text.h> |
| 47 |
|
| 48 |
#include <fstream> |
| 49 |
#include <iostream> |
| 50 |
#include <sstream> |
| 51 |
#include <memory> |
| 52 |
#include <getopt.h> |
| 53 |
#include <cassert> |
| 54 |
#include <cstdio> |
| 55 |
#include <filesystem> |
| 56 |
#include "common.h" |
| 57 |
#include "libheif/heif_items.h" |
| 58 |
|
| 59 |
|
| 60 |
/* |
| 61 |
image: 20005 (1920x1080), primary |
| 62 |
thumbnail: 20010 (320x240) |
| 63 |
alpha channel: 20012 (1920x1080) |
| 64 |
metadata: Exif |
| 65 |
|
| 66 |
image: 1920x1080 (20005), primary |
| 67 |
thumbnail: 320x240 (20010) |
| 68 |
alpha channel: 1920x1080 (20012) |
| 69 |
|
| 70 |
info *file |
| 71 |
info -w 20012 -o out.265 *file |
| 72 |
info -d // dump |
| 73 |
*/ |
| 74 |
|
| 75 |
int option_disable_limits = 0; |
| 76 |
|
| 77 |
static option long_options[] = { |
| 78 |
//{"write-raw", required_argument, 0, 'w' }, |
| 79 |
//{"output", required_argument, 0, 'o' }, |
| 80 |
{(char* const) "dump-boxes", no_argument, 0, 'd'}, |
| 81 |
{(char* const) "disable-limits", no_argument, &option_disable_limits, 1}, |
| 82 |
{(char* const) "help", no_argument, 0, 'h'}, |
| 83 |
{(char* const) "version", no_argument, 0, 'v'}, |
| 84 |
{0, 0, 0, 0} |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
void show_help(const char* argv0) |
| 89 |
{ |
| 90 |
std::filesystem::path p(argv0); |
| 91 |
std::string filename = p.filename().string(); |
| 92 |
|
| 93 |
std::stringstream sstr; |
| 94 |
sstr << " " << filename << " libheif version: " << heif_get_version(); |
| 95 |
|
| 96 |
std::string title = sstr.str(); |
| 97 |
|
| 98 |
std::cerr << title << "\n" |
| 99 |
<< std::string(title.length() + 1, '-') << "\n" |
| 100 |
<< "Usage: " << filename << " [options] <HEIF-image>\n" |
| 101 |
<< "\n" |
| 102 |
"options:\n" |
| 103 |
//fprintf(stderr," -w, --write-raw ID write raw compressed data of image 'ID'\n"); |
| 104 |
//fprintf(stderr," -o, --output NAME output file name for image selected by -w\n"); |
| 105 |
" -d, --dump-boxes show a low-level dump of all MP4 file boxes\n" |
| 106 |
" --disable-limits disable all security limits (do not use in production environment)\n" |
| 107 |
" -h, --help show help\n" |
| 108 |
" -v, --version show version\n"; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
class LibHeifInitializer |
| 113 |
{ |
| 114 |
public: |
| 115 |
LibHeifInitializer() { heif_init(nullptr); } |
| 116 |
|
| 117 |
~LibHeifInitializer() { heif_deinit(); } |
| 118 |
}; |
| 119 |
|
| 120 |
|
| 121 |
int main(int argc, char** argv) |
| 122 |
{ |
| 123 |
// This takes care of initializing libheif and also deinitializing it at the end to free all resources. |
| 124 |
LibHeifInitializer initializer; |
| 125 |
|
| 126 |
bool dump_boxes = false; |
| 127 |
|
| 128 |
bool write_raw_image = false; |
| 129 |
heif_item_id raw_image_id; |
| 130 |
std::string output_filename = "output.265"; |
| 131 |
|
| 132 |
while (true) { |
| 133 |
int option_index = 0; |
| 134 |
int c = getopt_long(argc, argv, "dhv", long_options, &option_index); |
| 135 |
if (c == -1) |
| 136 |
break; |
| 137 |
|
| 138 |
switch (c) { |
| 139 |
case 'd': |
| 140 |
dump_boxes = true; |
| 141 |
break; |
| 142 |
case 'h': |
| 143 |
show_help(argv[0]); |
| 144 |
return 0; |
| 145 |
case 'w': |
| 146 |
write_raw_image = true; |
| 147 |
raw_image_id = atoi(optarg); |
| 148 |
break; |
| 149 |
case 'o': |
| 150 |
output_filename = optarg; |
| 151 |
break; |
| 152 |
case 'v': |
| 153 |
heif_examples::show_version(); |
| 154 |
return 0; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
if (optind != argc - 1) { |
| 159 |
show_help(argv[0]); |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
(void) raw_image_id; |
| 165 |
(void) write_raw_image; |
| 166 |
|
| 167 |
const char* input_filename = argv[optind]; |
| 168 |
|
| 169 |
// ============================================================================== |
| 170 |
// show MIME type |
| 171 |
|
| 172 |
{ |
| 173 |
const static int bufSize = 50; |
| 174 |
|
| 175 |
uint8_t buf[bufSize]; |
| 176 |
FILE* fh = fopen(input_filename, "rb"); |
| 177 |
if (fh) { |
| 178 |
std::cout << "MIME type: "; |
| 179 |
int n = (int) fread(buf, 1, bufSize, fh); |
| 180 |
const char* mime_type = heif_get_file_mime_type(buf, n); |
| 181 |
if (*mime_type == 0) { |
| 182 |
std::cout << "unknown\n"; |
| 183 |
} |
| 184 |
else { |
| 185 |
std::cout << mime_type << "\n"; |
| 186 |
} |
| 187 |
|
| 188 |
fclose(fh); |
| 189 |
|
| 190 |
char fourcc[5]; |
| 191 |
fourcc[4] = 0; |
| 192 |
heif_brand_to_fourcc(heif_read_main_brand(buf, bufSize), fourcc); |
| 193 |
std::cout << "main brand: " << fourcc << "\n"; |
| 194 |
|
| 195 |
heif_brand2* brands = nullptr; |
| 196 |
int nBrands = 0; |
| 197 |
heif_error err = heif_list_compatible_brands(buf, n, &brands, &nBrands); |
| 198 |
if (err.code) { |
| 199 |
std::cerr << "error reading brands: " << err.message << "\n"; |
| 200 |
} |
| 201 |
else { |
| 202 |
std::cout << "compatible brands: "; |
| 203 |
for (int i = 0; i < nBrands; i++) { |
| 204 |
heif_brand_to_fourcc(brands[i], fourcc); |
| 205 |
if (i > 0) { |
| 206 |
std::cout << ", "; |
| 207 |
} |
| 208 |
std::cout << fourcc; |
| 209 |
} |
| 210 |
|
| 211 |
std::cout << "\n"; |
| 212 |
|
| 213 |
heif_free_list_of_compatible_brands(brands); |
| 214 |
} |
| 215 |
} |
| 216 |
else { |
| 217 |
if (errno == ENOENT) { |
| 218 |
std::cerr << "Input file does not exist.\n"; |
| 219 |
exit(10); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
// ============================================================================== |
| 225 |
|
| 226 |
std::shared_ptr<heif_context> ctx(heif_context_alloc(), |
| 227 |
[](heif_context* c) { heif_context_free(c); }); |
| 228 |
if (!ctx) { |
| 229 |
fprintf(stderr, "Could not create context object\n"); |
| 230 |
return 1; |
| 231 |
} |
| 232 |
|
| 233 |
if (option_disable_limits) { |
| 234 |
heif_context_set_security_limits(ctx.get(), heif_get_disabled_security_limits()); |
| 235 |
} |
| 236 |
|
| 237 |
heif_error err; |
| 238 |
err = heif_context_read_from_file(ctx.get(), input_filename, nullptr); |
| 239 |
|
| 240 |
if (dump_boxes) { |
| 241 |
heif_context_debug_dump_boxes_to_file(ctx.get(), STDOUT_FILENO); // dump to stdout |
| 242 |
|
| 243 |
if (err.code != 0) { |
| 244 |
std::cerr << "Could not read HEIF/AVIF file: " << err.message << "\n"; |
| 245 |
return 1; |
| 246 |
} |
| 247 |
|
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
if (err.code != 0) { |
| 252 |
std::cerr << "Could not read HEIF/AVIF file: " << err.message << "\n"; |
| 253 |
return 1; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
// ============================================================================== |
| 258 |
|
| 259 |
|
| 260 |
int numImages = heif_context_get_number_of_top_level_images(ctx.get()); |
| 261 |
std::vector<heif_item_id> IDs(numImages); |
| 262 |
heif_context_get_list_of_top_level_image_IDs(ctx.get(), IDs.data(), numImages); |
| 263 |
|
| 264 |
for (int i = 0; i < numImages; i++) { |
| 265 |
std::cout << "\n"; |
| 266 |
|
| 267 |
heif_image_handle* handle; |
| 268 |
heif_error err = heif_context_get_image_handle(ctx.get(), IDs[i], &handle); |
| 269 |
if (err.code) { |
| 270 |
std::cerr << err.message << "\n"; |
| 271 |
return 10; |
| 272 |
} |
| 273 |
|
| 274 |
int width = heif_image_handle_get_width(handle); |
| 275 |
int height = heif_image_handle_get_height(handle); |
| 276 |
|
| 277 |
int primary = heif_image_handle_is_primary_image(handle); |
| 278 |
|
| 279 |
printf("image: %dx%d (id=%d)%s\n", width, height, IDs[i], primary ? ", primary" : ""); |
| 280 |
|
| 281 |
heif_image_tiling tiling; |
| 282 |
err = heif_image_handle_get_image_tiling(handle, true, &tiling); |
| 283 |
if (err.code) { |
| 284 |
std::cerr << "Error while trying to get image tiling information: " << err.message << "\n"; |
| 285 |
} |
| 286 |
else if (tiling.num_columns != 1 || tiling.num_rows != 1) { |
| 287 |
std::cout << " tiles: " << tiling.num_columns << "x" << tiling.num_rows |
| 288 |
<< ", tile size: " << tiling.tile_width << "x" << tiling.tile_height << "\n"; |
| 289 |
} |
| 290 |
|
| 291 |
heif_colorspace colorspace; |
| 292 |
heif_chroma chroma; |
| 293 |
err = heif_image_handle_get_preferred_decoding_colorspace(handle, &colorspace, &chroma); |
| 294 |
if (err.code) { |
| 295 |
std::cerr << err.message << "\n"; |
| 296 |
return 10; |
| 297 |
} |
| 298 |
|
| 299 |
printf(" colorspace: "); |
| 300 |
switch (colorspace) { |
| 301 |
case heif_colorspace_YCbCr: |
| 302 |
printf("YCbCr, "); |
| 303 |
break; |
| 304 |
case heif_colorspace_RGB: |
| 305 |
printf("RGB"); |
| 306 |
break; |
| 307 |
case heif_colorspace_monochrome: |
| 308 |
printf("monochrome"); |
| 309 |
break; |
| 310 |
case heif_colorspace_nonvisual: |
| 311 |
printf("non-visual"); |
| 312 |
break; |
| 313 |
default: |
| 314 |
printf("unknown"); |
| 315 |
break; |
| 316 |
} |
| 317 |
|
| 318 |
if (colorspace==heif_colorspace_YCbCr) { |
| 319 |
switch (chroma) { |
| 320 |
case heif_chroma_420: |
| 321 |
printf("4:2:0"); |
| 322 |
break; |
| 323 |
case heif_chroma_422: |
| 324 |
printf("4:2:2"); |
| 325 |
break; |
| 326 |
case heif_chroma_444: |
| 327 |
printf("4:4:4"); |
| 328 |
break; |
| 329 |
default: |
| 330 |
printf("unknown"); |
| 331 |
break; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
printf("\n"); |
| 336 |
|
| 337 |
// --- bit depth |
| 338 |
|
| 339 |
int luma_depth = heif_image_handle_get_luma_bits_per_pixel(handle); |
| 340 |
int chroma_depth = heif_image_handle_get_chroma_bits_per_pixel(handle); |
| 341 |
|
| 342 |
printf(" bit depth: "); |
| 343 |
if (chroma == heif_chroma_monochrome || luma_depth==chroma_depth) { |
| 344 |
printf("%d\n", luma_depth); |
| 345 |
} |
| 346 |
else { |
| 347 |
printf("%d,%d\n", luma_depth, chroma_depth); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
// --- thumbnails |
| 352 |
|
| 353 |
int nThumbnails = heif_image_handle_get_number_of_thumbnails(handle); |
| 354 |
std::vector<heif_item_id> thumbnailIDs(nThumbnails); |
| 355 |
|
| 356 |
nThumbnails = heif_image_handle_get_list_of_thumbnail_IDs(handle, thumbnailIDs.data(), nThumbnails); |
| 357 |
|
| 358 |
for (int thumbnailIdx = 0; thumbnailIdx < nThumbnails; thumbnailIdx++) { |
| 359 |
heif_image_handle* thumbnail_handle; |
| 360 |
err = heif_image_handle_get_thumbnail(handle, thumbnailIDs[thumbnailIdx], &thumbnail_handle); |
| 361 |
if (err.code) { |
| 362 |
std::cerr << err.message << "\n"; |
| 363 |
return 10; |
| 364 |
} |
| 365 |
|
| 366 |
int th_width = heif_image_handle_get_width(thumbnail_handle); |
| 367 |
int th_height = heif_image_handle_get_height(thumbnail_handle); |
| 368 |
|
| 369 |
printf(" thumbnail: %dx%d\n", th_width, th_height); |
| 370 |
|
| 371 |
heif_image_handle_release(thumbnail_handle); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
// --- color profile |
| 376 |
|
| 377 |
uint32_t profileType = heif_image_handle_get_color_profile_type(handle); |
| 378 |
printf(" color profile: %s\n", profileType ? heif_examples::fourcc_to_string(profileType).c_str() : "no"); |
| 379 |
|
| 380 |
|
| 381 |
// --- depth information |
| 382 |
|
| 383 |
bool has_depth = heif_image_handle_has_depth_image(handle); |
| 384 |
bool has_alpha = heif_image_handle_has_alpha_channel(handle); |
| 385 |
bool premultiplied_alpha = false; |
| 386 |
|
| 387 |
if (has_alpha) { |
| 388 |
premultiplied_alpha = heif_image_handle_is_premultiplied_alpha(handle); |
| 389 |
} |
| 390 |
|
| 391 |
printf(" alpha channel: %s %s\n", has_alpha ? "yes" : "no", |
| 392 |
premultiplied_alpha ? "(premultiplied)" : ""); |
| 393 |
printf(" depth channel: %s\n", has_depth ? "yes" : "no"); |
| 394 |
|
| 395 |
heif_item_id depth_id; |
| 396 |
int nDepthImages = heif_image_handle_get_list_of_depth_image_IDs(handle, &depth_id, 1); |
| 397 |
if (has_depth) { assert(nDepthImages == 1); } |
| 398 |
else { assert(nDepthImages == 0); } |
| 399 |
(void) nDepthImages; |
| 400 |
|
| 401 |
if (has_depth) { |
| 402 |
heif_image_handle* depth_handle; |
| 403 |
err = heif_image_handle_get_depth_image_handle(handle, depth_id, &depth_handle); |
| 404 |
if (err.code) { |
| 405 |
fprintf(stderr, "cannot get depth image: %s\n", err.message); |
| 406 |
return 1; |
| 407 |
} |
| 408 |
|
| 409 |
printf(" size: %dx%d\n", |
| 410 |
heif_image_handle_get_width(depth_handle), |
| 411 |
heif_image_handle_get_height(depth_handle)); |
| 412 |
|
| 413 |
int depth_luma_bpp = heif_image_handle_get_luma_bits_per_pixel(depth_handle); |
| 414 |
printf(" bits per pixel: %d\n", depth_luma_bpp); |
| 415 |
|
| 416 |
|
| 417 |
const heif_depth_representation_info* depth_info; |
| 418 |
if (heif_image_handle_get_depth_image_representation_info(handle, depth_id, &depth_info)) { |
| 419 |
|
| 420 |
printf(" z-near: "); |
| 421 |
if (depth_info->has_z_near) printf("%f\n", depth_info->z_near); else printf("undefined\n"); |
| 422 |
printf(" z-far: "); |
| 423 |
if (depth_info->has_z_far) printf("%f\n", depth_info->z_far); else printf("undefined\n"); |
| 424 |
printf(" d-min: "); |
| 425 |
if (depth_info->has_d_min) printf("%f\n", depth_info->d_min); else printf("undefined\n"); |
| 426 |
printf(" d-max: "); |
| 427 |
if (depth_info->has_d_max) printf("%f\n", depth_info->d_max); else printf("undefined\n"); |
| 428 |
|
| 429 |
printf(" representation: "); |
| 430 |
switch (depth_info->depth_representation_type) { |
| 431 |
case heif_depth_representation_type_uniform_inverse_Z: |
| 432 |
printf("inverse Z\n"); |
| 433 |
break; |
| 434 |
case heif_depth_representation_type_uniform_disparity: |
| 435 |
printf("uniform disparity\n"); |
| 436 |
break; |
| 437 |
case heif_depth_representation_type_uniform_Z: |
| 438 |
printf("uniform Z\n"); |
| 439 |
break; |
| 440 |
case heif_depth_representation_type_nonuniform_disparity: |
| 441 |
printf("non-uniform disparity\n"); |
| 442 |
break; |
| 443 |
default: |
| 444 |
printf("unknown\n"); |
| 445 |
} |
| 446 |
|
| 447 |
if (depth_info->has_d_min || depth_info->has_d_max) { |
| 448 |
printf(" disparity_reference_view: %d\n", depth_info->disparity_reference_view); |
| 449 |
} |
| 450 |
|
| 451 |
heif_depth_representation_info_free(depth_info); |
| 452 |
} |
| 453 |
|
| 454 |
heif_image_handle_release(depth_handle); |
| 455 |
} |
| 456 |
|
| 457 |
// --- metadata |
| 458 |
|
| 459 |
int numMetadata = heif_image_handle_get_number_of_metadata_blocks(handle, nullptr); |
| 460 |
printf("metadata:\n"); |
| 461 |
if (numMetadata > 0) { |
| 462 |
std::vector<heif_item_id> ids(numMetadata); |
| 463 |
heif_image_handle_get_list_of_metadata_block_IDs(handle, nullptr, ids.data(), numMetadata); |
| 464 |
|
| 465 |
for (int n = 0; n < numMetadata; n++) { |
| 466 |
std::string itemtype = heif_image_handle_get_metadata_type(handle, ids[n]); |
| 467 |
std::string contenttype = heif_image_handle_get_metadata_content_type(handle, ids[n]); |
| 468 |
std::string item_uri_type = heif_image_handle_get_metadata_item_uri_type(handle, ids[n]); |
| 469 |
std::string ID{"unknown"}; |
| 470 |
if (itemtype == "Exif") { |
| 471 |
ID = itemtype; |
| 472 |
} |
| 473 |
else if (itemtype == "uri ") { |
| 474 |
ID = itemtype + "/" + item_uri_type; |
| 475 |
} |
| 476 |
else if (contenttype == "application/rdf+xml") { |
| 477 |
ID = "XMP"; |
| 478 |
} |
| 479 |
else { |
| 480 |
ID = itemtype + "/" + contenttype; |
| 481 |
} |
| 482 |
|
| 483 |
printf(" %s: %zu bytes\n", ID.c_str(), heif_image_handle_get_metadata_size(handle, ids[n])); |
| 484 |
} |
| 485 |
} |
| 486 |
else { |
| 487 |
printf(" none\n"); |
| 488 |
} |
| 489 |
|
| 490 |
// --- transforms |
| 491 |
|
| 492 |
#define MAX_PROPERTIES 50 |
| 493 |
heif_property_id transforms[MAX_PROPERTIES]; |
| 494 |
int nTransforms = heif_item_get_transformation_properties(ctx.get(), |
| 495 |
IDs[i], |
| 496 |
transforms, |
| 497 |
MAX_PROPERTIES); |
| 498 |
printf("transformations:\n"); |
| 499 |
int image_width = heif_image_handle_get_ispe_width(handle); |
| 500 |
int image_height = heif_image_handle_get_ispe_height(handle); |
| 501 |
|
| 502 |
if (nTransforms) { |
| 503 |
for (int k = 0; k < nTransforms; k++) { |
| 504 |
switch (heif_item_get_property_type(ctx.get(), IDs[i], transforms[k])) { |
| 505 |
case heif_item_property_type_transform_mirror: |
| 506 |
printf(" mirror: %s\n", heif_item_get_property_transform_mirror(ctx.get(), IDs[i], transforms[k]) == heif_transform_mirror_direction_horizontal ? "horizontal" : "vertical"); |
| 507 |
break; |
| 508 |
case heif_item_property_type_transform_rotation: { |
| 509 |
int angle = heif_item_get_property_transform_rotation_ccw(ctx.get(), IDs[i], transforms[k]); |
| 510 |
printf(" angle (ccw): %d\n", angle); |
| 511 |
if (angle==90 || angle==270) { |
| 512 |
std::swap(image_width, image_height); |
| 513 |
} |
| 514 |
break; |
| 515 |
} |
| 516 |
case heif_item_property_type_transform_crop: { |
| 517 |
int left,top,right,bottom; |
| 518 |
heif_item_get_property_transform_crop_borders(ctx.get(), IDs[i], transforms[k], image_width, image_height, |
| 519 |
&left, &top, &right, &bottom); |
| 520 |
printf(" crop: left=%d top=%d right=%d bottom=%d\n", left,top,right,bottom); |
| 521 |
break; |
| 522 |
} |
| 523 |
default: |
| 524 |
assert(false); |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
else { |
| 529 |
printf(" none\n"); |
| 530 |
} |
| 531 |
|
| 532 |
// --- regions |
| 533 |
|
| 534 |
int numRegionItems = heif_image_handle_get_number_of_region_items(handle); |
| 535 |
printf("region annotations:\n"); |
| 536 |
if (numRegionItems > 0) { |
| 537 |
std::vector<heif_item_id> region_items(numRegionItems); |
| 538 |
heif_image_handle_get_list_of_region_item_ids(handle, region_items.data(), numRegionItems); |
| 539 |
for (heif_item_id region_item_id : region_items) { |
| 540 |
heif_region_item* region_item; |
| 541 |
err = heif_context_get_region_item(ctx.get(), region_item_id, ®ion_item); |
| 542 |
|
| 543 |
uint32_t reference_width, reference_height; |
| 544 |
heif_region_item_get_reference_size(region_item, &reference_width, &reference_height); |
| 545 |
int numRegions = heif_region_item_get_number_of_regions(region_item); |
| 546 |
printf(" id: %u, reference_width: %u, reference_height: %u, %d regions\n", |
| 547 |
region_item_id, |
| 548 |
reference_width, |
| 549 |
reference_height, |
| 550 |
numRegions); |
| 551 |
|
| 552 |
std::vector<heif_region*> regions(numRegions); |
| 553 |
numRegions = heif_region_item_get_list_of_regions(region_item, regions.data(), numRegions); |
| 554 |
for (int j = 0; j < numRegions; j++) { |
| 555 |
printf(" region %d\n", j); |
| 556 |
heif_region_type type = heif_region_get_type(regions[j]); |
| 557 |
if (type == heif_region_type_point) { |
| 558 |
int32_t x; |
| 559 |
int32_t y; |
| 560 |
heif_region_get_point(regions[j], &x, &y); |
| 561 |
printf(" point [x=%i, y=%i]\n", x, y); |
| 562 |
} |
| 563 |
else if (type == heif_region_type_rectangle) { |
| 564 |
int32_t x; |
| 565 |
int32_t y; |
| 566 |
uint32_t w; |
| 567 |
uint32_t h; |
| 568 |
heif_region_get_rectangle(regions[j], &x, &y, &w, &h); |
| 569 |
printf(" rectangle [x=%i, y=%i, w=%u, h=%u]\n", x, y, w, h); |
| 570 |
} |
| 571 |
else if (type == heif_region_type_ellipse) { |
| 572 |
int32_t x; |
| 573 |
int32_t y; |
| 574 |
uint32_t rx; |
| 575 |
uint32_t ry; |
| 576 |
heif_region_get_ellipse(regions[j], &x, &y, &rx, &ry); |
| 577 |
printf(" ellipse [x=%i, y=%i, r_x=%u, r_y=%u]\n", x, y, rx, ry); |
| 578 |
} |
| 579 |
else if (type == heif_region_type_polygon) { |
| 580 |
int32_t numPoints = heif_region_get_polygon_num_points(regions[j]); |
| 581 |
std::vector<int32_t> pts(numPoints*2); |
| 582 |
heif_region_get_polygon_points(regions[j], pts.data()); |
| 583 |
printf(" polygon ["); |
| 584 |
for (int p=0;p<numPoints;p++) { |
| 585 |
printf("(%d;%d)", pts[2*p+0], pts[2*p+1]); |
| 586 |
} |
| 587 |
printf("]\n"); |
| 588 |
} |
| 589 |
else if (type == heif_region_type_referenced_mask) { |
| 590 |
int32_t x; |
| 591 |
int32_t y; |
| 592 |
uint32_t w; |
| 593 |
uint32_t h; |
| 594 |
heif_item_id referenced_item; |
| 595 |
heif_region_get_referenced_mask_ID(regions[j], &x, &y, &w, &h, &referenced_item); |
| 596 |
printf(" referenced mask [x=%i, y=%i, w=%u, h=%u, item=%u]\n", x, y, w, h, referenced_item); |
| 597 |
} |
| 598 |
else if (type == heif_region_type_polyline) { |
| 599 |
int32_t numPoints = heif_region_get_polyline_num_points(regions[j]); |
| 600 |
std::vector<int32_t> pts(numPoints*2); |
| 601 |
heif_region_get_polyline_points(regions[j], pts.data()); |
| 602 |
printf(" polyline ["); |
| 603 |
for (int p=0;p<numPoints;p++) { |
| 604 |
printf("(%d;%d)", pts[2*p+0], pts[2*p+1]); |
| 605 |
} |
| 606 |
printf("]\n"); |
| 607 |
} |
| 608 |
else if (type == heif_region_type_inline_mask) { |
| 609 |
int32_t x; |
| 610 |
int32_t y; |
| 611 |
uint32_t w; |
| 612 |
uint32_t h; |
| 613 |
size_t data_len = heif_region_get_inline_mask_data_len(regions[j]); |
| 614 |
std::vector<uint8_t> mask_data(data_len); |
| 615 |
heif_region_get_inline_mask_data(regions[j], &x, &y, &w, &h, mask_data.data()); |
| 616 |
printf(" inline mask [x=%i, y=%i, w=%u, h=%u, data len=%zu]\n", x, y, w, h, mask_data.size()); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
heif_region_release_many(regions.data(), numRegions); |
| 621 |
heif_region_item_release(region_item); |
| 622 |
|
| 623 |
heif_property_id properties[MAX_PROPERTIES]; |
| 624 |
int nDescr = heif_item_get_properties_of_type(ctx.get(), |
| 625 |
region_item_id, |
| 626 |
heif_item_property_type_user_description, |
| 627 |
properties, |
| 628 |
MAX_PROPERTIES); |
| 629 |
|
| 630 |
for (int k = 0; k < nDescr; k++) { |
| 631 |
heif_property_user_description* udes; |
| 632 |
err = heif_item_get_property_user_description(ctx.get(), |
| 633 |
region_item_id, |
| 634 |
properties[k], |
| 635 |
&udes); |
| 636 |
if (err.code == 0) { |
| 637 |
printf(" user description:\n"); |
| 638 |
printf(" lang: %s\n", udes->lang); |
| 639 |
printf(" name: %s\n", udes->name); |
| 640 |
printf(" description: %s\n", udes->description); |
| 641 |
printf(" tags: %s\n", udes->tags); |
| 642 |
heif_property_user_description_release(udes); |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
} |
| 647 |
else { |
| 648 |
printf(" none\n"); |
| 649 |
} |
| 650 |
|
| 651 |
// --- text items |
| 652 |
int numTextItems = heif_image_handle_get_number_of_text_items(handle); |
| 653 |
printf("text items:\n"); |
| 654 |
|
| 655 |
if (numTextItems > 0) { |
| 656 |
std::vector<heif_item_id> text_items(numTextItems); |
| 657 |
heif_image_handle_get_list_of_text_item_ids(handle, text_items.data(), numTextItems); |
| 658 |
for (heif_item_id text_item_id : text_items) { |
| 659 |
struct heif_text_item* text_item; |
| 660 |
err = heif_context_get_text_item(ctx.get(), text_item_id, &text_item); |
| 661 |
const char* text_content = heif_text_item_get_content(text_item); |
| 662 |
printf(" text item: %s\n", text_content); |
| 663 |
heif_string_release(text_content); |
| 664 |
|
| 665 |
char* elng; |
| 666 |
err = heif_item_get_property_extended_language(ctx.get(), |
| 667 |
text_item_id, |
| 668 |
&elng); |
| 669 |
if (err.code == 0) { |
| 670 |
printf(" extended language: %s\n", elng); |
| 671 |
} |
| 672 |
heif_string_release(elng); |
| 673 |
} |
| 674 |
} |
| 675 |
else { |
| 676 |
printf(" none\n"); |
| 677 |
} |
| 678 |
|
| 679 |
// --- properties |
| 680 |
|
| 681 |
printf("properties:\n"); |
| 682 |
|
| 683 |
// user descriptions |
| 684 |
|
| 685 |
bool properties_shown = false; |
| 686 |
|
| 687 |
heif_property_id propertyIds[MAX_PROPERTIES]; |
| 688 |
int count; |
| 689 |
count = heif_item_get_properties_of_type(ctx.get(), IDs[i], heif_item_property_type_user_description, |
| 690 |
propertyIds, MAX_PROPERTIES); |
| 691 |
|
| 692 |
if (count > 0) { |
| 693 |
for (int p = 0; p < count; p++) { |
| 694 |
heif_property_user_description* udes; |
| 695 |
err = heif_item_get_property_user_description(ctx.get(), IDs[i], propertyIds[p], &udes); |
| 696 |
if (err.code) { |
| 697 |
std::cerr << "Error reading udes " << IDs[i] << "/" << propertyIds[p] << "\n"; |
| 698 |
} |
| 699 |
else { |
| 700 |
printf(" user description:\n"); |
| 701 |
printf(" lang: %s\n", udes->lang); |
| 702 |
printf(" name: %s\n", udes->name); |
| 703 |
printf(" description: %s\n", udes->description); |
| 704 |
printf(" tags: %s\n", udes->tags); |
| 705 |
properties_shown = true; |
| 706 |
|
| 707 |
heif_property_user_description_release(udes); |
| 708 |
} |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
// --- camera intrinsic and extrinsic parameters |
| 713 |
|
| 714 |
if (heif_image_handle_has_camera_intrinsic_matrix(handle)) { |
| 715 |
heif_camera_intrinsic_matrix matrix{}; |
| 716 |
heif_image_handle_get_camera_intrinsic_matrix(handle, &matrix); |
| 717 |
printf(" camera intrinsic matrix:\n"); |
| 718 |
printf(" focal length: %f; %f\n", matrix.focal_length_x, matrix.focal_length_y); |
| 719 |
printf(" principal point: %f; %f\n", matrix.principal_point_x, matrix.principal_point_y); |
| 720 |
printf(" skew: %f\n", matrix.skew); |
| 721 |
properties_shown = true; |
| 722 |
} |
| 723 |
|
| 724 |
if (heif_image_handle_has_camera_extrinsic_matrix(handle)) { |
| 725 |
heif_camera_extrinsic_matrix* matrix; |
| 726 |
heif_image_handle_get_camera_extrinsic_matrix(handle, &matrix); |
| 727 |
double rot[9]; |
| 728 |
heif_camera_extrinsic_matrix_get_rotation_matrix(matrix, rot); |
| 729 |
printf(" camera extrinsic matrix:\n"); |
| 730 |
printf(" rotation matrix:\n"); |
| 731 |
printf(" %6.3f %6.3f %6.3f\n", rot[0], rot[1], rot[2]); |
| 732 |
printf(" %6.3f %6.3f %6.3f\n", rot[3], rot[4], rot[5]); |
| 733 |
printf(" %6.3f %6.3f %6.3f\n", rot[6], rot[7], rot[8]); |
| 734 |
heif_camera_extrinsic_matrix_release(matrix); |
| 735 |
properties_shown = true; |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
uint32_t aspect_h, aspect_v; |
| 740 |
int has_pasp = heif_image_handle_get_pixel_aspect_ratio(handle, &aspect_h, &aspect_v); |
| 741 |
if (has_pasp) { |
| 742 |
std::cout << " pixel aspect ratio: " << aspect_h << "/" << aspect_v << "\n"; |
| 743 |
properties_shown = true; |
| 744 |
} |
| 745 |
|
| 746 |
heif_content_light_level clli{}; |
| 747 |
if (heif_image_handle_get_content_light_level(handle, &clli)) { |
| 748 |
std::cout << " content light level (clli):\n" |
| 749 |
<< " max content light level: " << clli.max_content_light_level << "\n" |
| 750 |
<< " max pic average light level: " << clli.max_pic_average_light_level << "\n"; |
| 751 |
properties_shown = true; |
| 752 |
} |
| 753 |
|
| 754 |
heif_mastering_display_colour_volume mdcv; |
| 755 |
if (heif_image_handle_get_mastering_display_colour_volume(handle, &mdcv)) { |
| 756 |
|
| 757 |
heif_decoded_mastering_display_colour_volume decoded_mdcv; |
| 758 |
err = heif_mastering_display_colour_volume_decode(&mdcv, &decoded_mdcv); |
| 759 |
|
| 760 |
std::cout << " mastering display color volume:\n" |
| 761 |
<< " display_primaries (x,y): " |
| 762 |
<< "(" << decoded_mdcv.display_primaries_x[0] << ";" << decoded_mdcv.display_primaries_y[0] << "), " |
| 763 |
<< "(" << decoded_mdcv.display_primaries_x[1] << ";" << decoded_mdcv.display_primaries_y[1] << "), " |
| 764 |
<< "(" << decoded_mdcv.display_primaries_x[2] << ";" << decoded_mdcv.display_primaries_y[2] << ")\n"; |
| 765 |
|
| 766 |
std::cout << " white point (x,y): (" << decoded_mdcv.white_point_x << ";" << decoded_mdcv.white_point_y << ")\n"; |
| 767 |
std::cout << " max display mastering luminance: " << decoded_mdcv.max_display_mastering_luminance << "\n"; |
| 768 |
std::cout << " min display mastering luminance: " << decoded_mdcv.min_display_mastering_luminance << "\n"; |
| 769 |
properties_shown = true; |
| 770 |
} |
| 771 |
|
| 772 |
// --- GIMI |
| 773 |
|
| 774 |
const char* id = heif_image_handle_get_gimi_content_id(handle); |
| 775 |
if (id) { |
| 776 |
std::cout << " GIMI content ID: " << id << "\n"; |
| 777 |
heif_string_release(id); |
| 778 |
properties_shown = true; |
| 779 |
} |
| 780 |
|
| 781 |
if (!properties_shown) { |
| 782 |
std::cout << "none\n"; |
| 783 |
} |
| 784 |
|
| 785 |
heif_image_handle_release(handle); |
| 786 |
} |
| 787 |
|
| 788 |
#if 0 |
| 789 |
std::cout << "num images: " << heif_context_get_number_of_top_level_images(ctx.get()) << "\n"; |
| 790 |
|
| 791 |
heif_image_handle* handle; |
| 792 |
err = heif_context_get_primary_image_handle(ctx.get(), &handle); |
| 793 |
if (err.code != 0) { |
| 794 |
std::cerr << "Could not get primage image handle: " << err.message << "\n"; |
| 795 |
return 1; |
| 796 |
} |
| 797 |
|
| 798 |
heif_image* image; |
| 799 |
err = heif_decode_image(handle, &image, heif_colorspace_undefined, heif_chroma_undefined, NULL); |
| 800 |
if (err.code != 0) { |
| 801 |
heif_image_handle_release(handle); |
| 802 |
std::cerr << "Could not decode primage image: " << err.message << "\n"; |
| 803 |
return 1; |
| 804 |
} |
| 805 |
|
| 806 |
heif_image_release(image); |
| 807 |
heif_image_handle_release(handle); |
| 808 |
#endif |
| 809 |
|
| 810 |
// ============================================================================== |
| 811 |
|
| 812 |
heif_context* context = ctx.get(); |
| 813 |
|
| 814 |
uint32_t nTracks = heif_context_number_of_sequence_tracks(context); |
| 815 |
|
| 816 |
if (nTracks > 0) { |
| 817 |
std::cout << "\n"; |
| 818 |
|
| 819 |
uint64_t timescale = heif_context_get_sequence_timescale(context); |
| 820 |
std::cout << "sequence time scale: " << timescale << " Hz\n"; |
| 821 |
|
| 822 |
uint64_t duration = heif_context_get_sequence_duration(context); |
| 823 |
std::cout << "sequence duration: " << ((double)duration)/(double)timescale << " seconds\n"; |
| 824 |
|
| 825 |
// TrackOptions |
| 826 |
|
| 827 |
std::vector<uint32_t> track_ids(nTracks); |
| 828 |
|
| 829 |
heif_context_get_track_ids(context, track_ids.data()); |
| 830 |
|
| 831 |
for (uint32_t id : track_ids) { |
| 832 |
heif_track* track = heif_context_get_track(context, id); |
| 833 |
|
| 834 |
heif_track_type handler = heif_track_get_track_handler_type(track); |
| 835 |
std::cout << "track " << id << "\n"; |
| 836 |
std::cout << " handler: '" << heif_examples::fourcc_to_string(handler) << "' = "; |
| 837 |
|
| 838 |
switch (handler) { |
| 839 |
case heif_track_type_image_sequence: |
| 840 |
std::cout << "image sequence\n"; |
| 841 |
break; |
| 842 |
case heif_track_type_video: |
| 843 |
std::cout << "video\n"; |
| 844 |
break; |
| 845 |
case heif_track_type_auxiliary: |
| 846 |
std::cout << "auxiliary "; |
| 847 |
{ |
| 848 |
heif_auxiliary_track_info_type type = heif_track_get_auxiliary_info_type(track); |
| 849 |
switch (type) { |
| 850 |
case heif_auxiliary_track_info_type_unknown: { |
| 851 |
const char* s = heif_track_get_auxiliary_info_type_urn(track); |
| 852 |
std::cout << "(unknown: " << s << ")\n"; |
| 853 |
heif_string_release(s); |
| 854 |
break; |
| 855 |
} |
| 856 |
case heif_auxiliary_track_info_type_alpha: |
| 857 |
std::cout << "(alpha)\n"; |
| 858 |
break; |
| 859 |
} |
| 860 |
} |
| 861 |
break; |
| 862 |
case heif_track_type_metadata: |
| 863 |
std::cout << "metadata\n"; |
| 864 |
break; |
| 865 |
default: |
| 866 |
std::cout << "unknown\n"; |
| 867 |
break; |
| 868 |
} |
| 869 |
|
| 870 |
if (handler == heif_track_type_video || |
| 871 |
handler == heif_track_type_image_sequence) { |
| 872 |
uint16_t w, h; |
| 873 |
heif_track_get_image_resolution(track, &w, &h); |
| 874 |
std::cout << " resolution: " << w << "x" << h << "\n"; |
| 875 |
} |
| 876 |
|
| 877 |
uint32_t sampleEntryType = heif_track_get_sample_entry_type_of_first_cluster(track); |
| 878 |
std::cout << " sample entry type: " << heif_examples::fourcc_to_string(sampleEntryType) << "\n"; |
| 879 |
|
| 880 |
if (sampleEntryType == heif_fourcc('u', 'r', 'i', 'm')) { |
| 881 |
const char* uri; |
| 882 |
err = heif_track_get_urim_sample_entry_uri_of_first_cluster(track, &uri); |
| 883 |
if (err.code) { |
| 884 |
std::cerr << "error reading urim-track uri: " << err.message << "\n"; |
| 885 |
} |
| 886 |
std::cout << " uri: " << uri << "\n"; |
| 887 |
heif_string_release(uri); |
| 888 |
} |
| 889 |
|
| 890 |
std::cout << " sample auxiliary information: "; |
| 891 |
int nSampleAuxTypes = heif_track_get_number_of_sample_aux_infos(track); |
| 892 |
|
| 893 |
std::vector<heif_sample_aux_info_type> aux_types(nSampleAuxTypes); |
| 894 |
heif_track_get_sample_aux_info_types(track, aux_types.data()); |
| 895 |
|
| 896 |
for (size_t i=0;i<aux_types.size();i++) { |
| 897 |
if (i) { std::cout << ", "; } |
| 898 |
std::cout << heif_examples::fourcc_to_string(aux_types[i].type); |
| 899 |
} |
| 900 |
|
| 901 |
if (nSampleAuxTypes==0) { |
| 902 |
std::cout << "---"; |
| 903 |
} |
| 904 |
std::cout << "\n"; |
| 905 |
|
| 906 |
|
| 907 |
size_t nRefTypes = heif_track_get_number_of_track_reference_types(track); |
| 908 |
|
| 909 |
if (nRefTypes > 0) { |
| 910 |
std::cout << " references:\n"; |
| 911 |
std::vector<uint32_t> refTypes(nRefTypes); |
| 912 |
heif_track_get_track_reference_types(track, refTypes.data()); |
| 913 |
|
| 914 |
for (uint32_t refType : refTypes) { |
| 915 |
std::cout << " " << heif_examples::fourcc_to_string(refType) << ": "; |
| 916 |
|
| 917 |
size_t n = heif_track_get_number_of_track_reference_of_type(track, refType); |
| 918 |
std::vector<uint32_t> track_ids(n); |
| 919 |
heif_track_get_references_from_track(track, refType, track_ids.data()); |
| 920 |
for (size_t i=0;i<n;i++) { |
| 921 |
if (i>0) std::cout << ", "; |
| 922 |
std::cout << "track#" << track_ids[i]; |
| 923 |
} |
| 924 |
std::cout << "\n"; |
| 925 |
} |
| 926 |
} |
| 927 |
|
| 928 |
const char* gimi_track_id = heif_track_get_gimi_track_content_id(track); |
| 929 |
if (gimi_track_id) { |
| 930 |
std::cout << " GIMI track id: " << gimi_track_id << "\n"; |
| 931 |
heif_string_release(gimi_track_id); |
| 932 |
} |
| 933 |
|
| 934 |
heif_track_release(track); |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
// ============================================================================== |
| 939 |
|
| 940 |
{ |
| 941 |
int nItems = heif_context_get_number_of_items(context); |
| 942 |
if (nItems > 0) { |
| 943 |
std::cout << "MIME items:\n"; |
| 944 |
std::vector<heif_item_id> item_ids(nItems); |
| 945 |
heif_context_get_list_of_item_IDs(context, item_ids.data(), nItems); |
| 946 |
|
| 947 |
for (auto id : item_ids) { |
| 948 |
uint32_t type = heif_item_get_item_type(context, id); |
| 949 |
if (type == heif_item_type_mime) { |
| 950 |
const char* mimeType = heif_item_get_mime_item_content_type(context, id); |
| 951 |
std::cout << " " << id << " : " << mimeType << "\n"; |
| 952 |
} |
| 953 |
} |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
return 0; |
| 958 |
} |