| 1 |
/* |
| 2 |
libheif example application. |
| 3 |
|
| 4 |
MIT License |
| 5 |
|
| 6 |
Copyright (c) 2017-2024 Dirk Farin <[email protected]> |
| 7 |
Copyright (c) 2017 struktur AG, Joachim Bauch <[email protected]> |
| 8 |
|
| 9 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 |
of this software and associated documentation files (the "Software"), to deal |
| 11 |
in the Software without restriction, including without limitation the rights |
| 12 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 |
copies of the Software, and to permit persons to whom the Software is |
| 14 |
furnished to do so, subject to the following conditions: |
| 15 |
|
| 16 |
The above copyright notice and this permission notice shall be included in all |
| 17 |
copies or substantial portions of the Software. |
| 18 |
|
| 19 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 |
SOFTWARE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <cstring> |
| 29 |
#include <getopt.h> |
| 30 |
#include <unordered_set> |
| 31 |
#include <filesystem> |
| 32 |
#include "libheif/heif_items.h" |
| 33 |
#include "libheif/heif_experimental.h" |
| 34 |
#include "libheif/heif_sequences.h" |
| 35 |
|
| 36 |
#if defined(HAVE_UNISTD_H) |
| 37 |
|
| 38 |
#include <unistd.h> |
| 39 |
|
| 40 |
#endif |
| 41 |
|
| 42 |
#include <fstream> |
| 43 |
#include <iostream> |
| 44 |
#include <iomanip> |
| 45 |
#include <sstream> |
| 46 |
#include <cassert> |
| 47 |
#include <algorithm> |
| 48 |
#include <vector> |
| 49 |
#include <array> |
| 50 |
#include <cctype> |
| 51 |
|
| 52 |
#include <libheif/heif.h> |
| 53 |
#include <libheif/heif_tai_timestamps.h> |
| 54 |
|
| 55 |
#include "heifio/encoder.h" |
| 56 |
|
| 57 |
#if HAVE_LIBJPEG |
| 58 |
#include "heifio/encoder_jpeg.h" |
| 59 |
#endif |
| 60 |
|
| 61 |
#if HAVE_LIBPNG |
| 62 |
#include "heifio/encoder_png.h" |
| 63 |
#endif |
| 64 |
|
| 65 |
#if HAVE_LIBTIFF |
| 66 |
#include "heifio/encoder_tiff.h" |
| 67 |
#endif |
| 68 |
|
| 69 |
#include "../heifio/encoder_y4m.h" |
| 70 |
#include "common.h" |
| 71 |
#include "common_utils.h" |
| 72 |
|
| 73 |
#if defined(_MSC_VER) |
| 74 |
#include "getopt.h" |
| 75 |
#endif |
| 76 |
|
| 77 |
#define UNUSED(x) (void)x |
| 78 |
|
| 79 |
|
| 80 |
static void show_help(const char* argv0) |
| 81 |
{ |
| 82 |
std::filesystem::path p(argv0); |
| 83 |
std::string filename = p.filename().string(); |
| 84 |
|
| 85 |
std::stringstream sstr; |
| 86 |
sstr << " " << filename << " libheif version: " << heif_get_version(); |
| 87 |
|
| 88 |
std::string title = sstr.str(); |
| 89 |
|
| 90 |
std::cerr << title << "\n" |
| 91 |
<< std::string(title.length() + 1, '-') << "\n" |
| 92 |
<< "Usage: " << filename << " [options] <input-image> [output-image]\n" |
| 93 |
<< "\n" |
| 94 |
"The program determines the output file format from the output filename suffix.\n" |
| 95 |
"These suffixes are recognized: jpg, jpeg, png, tif, tiff, y4m. If no output filename is specified, 'jpg' is used.\n" |
| 96 |
"\n" |
| 97 |
"Options:\n" |
| 98 |
" -h, --help show help\n" |
| 99 |
" -v, --version show version\n" |
| 100 |
" -q, --quality quality (for JPEG output)\n" |
| 101 |
" -o, --output FILENAME write output to FILENAME (optional)\n" |
| 102 |
" -d, --decoder ID use a specific decoder (see --list-decoders)\n" |
| 103 |
" --with-aux also write auxiliary images (e.g. depth images)\n" |
| 104 |
" --with-xmp write XMP metadata to file (output filename with .xmp suffix)\n" |
| 105 |
" --with-exif write EXIF metadata to file (output filename with .exif suffix)\n" |
| 106 |
" --skip-exif-offset skip EXIF metadata offset bytes\n" |
| 107 |
" --no-colons replace ':' characters in auxiliary image filenames with '_'\n" |
| 108 |
" --list-decoders list all available decoders (built-in and plugins)\n" |
| 109 |
" --tiles output all image tiles as separate images\n" |
| 110 |
" --quiet do not output status messages to console\n" |
| 111 |
" -S, --sequence decode image sequence instead of still image\n" |
| 112 |
" --ignore-editlist show the raw media sequence timeline without repetitions\n" |
| 113 |
" -C, --chroma-upsampling ALGO Force chroma upsampling algorithm (nn = nearest-neighbor / bilinear)\n" |
| 114 |
" --png-compression-level # Set to integer between 0 (fastest) and 9 (best). Use -1 for default.\n" |
| 115 |
" --transparency-composition-mode MODE Controls how transparent images are rendered when the output format\n" |
| 116 |
" support transparency. MODE must be one of: white, black, checkerboard.\n" |
| 117 |
" --disable-limits disable all security limits (do not use in production environment)\n" |
| 118 |
" --codec-threads # number of threads to use in the codec plugin (0 = default)\n" |
| 119 |
" --extract-mime-item TYPE extract the MIME item with the given content type into a file (mime-item.data)\n"; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
class ContextReleaser |
| 124 |
{ |
| 125 |
public: |
| 126 |
ContextReleaser(heif_context* ctx) : ctx_(ctx) |
| 127 |
{} |
| 128 |
|
| 129 |
~ContextReleaser() |
| 130 |
{ |
| 131 |
heif_context_free(ctx_); |
| 132 |
} |
| 133 |
|
| 134 |
private: |
| 135 |
heif_context* ctx_; |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
int option_quiet = 0; |
| 140 |
int option_aux = 0; |
| 141 |
int option_no_colons = 0; |
| 142 |
int option_with_xmp = 0; |
| 143 |
int option_with_exif = 0; |
| 144 |
int option_skip_exif_offset = 0; |
| 145 |
int option_list_decoders = 0; |
| 146 |
int option_png_compression_level = -1; // use zlib default |
| 147 |
int option_output_tiles = 0; |
| 148 |
int option_disable_limits = 0; |
| 149 |
int option_sequence = 0; |
| 150 |
int option_ignore_editlist = 0; |
| 151 |
int option_num_codec_threads = 0; |
| 152 |
std::string option_extract_mime_item_type; |
| 153 |
std::string output_filename; |
| 154 |
|
| 155 |
std::string chroma_upsampling; |
| 156 |
std::string transparency_composition_mode = "checkerboard"; |
| 157 |
|
| 158 |
#define OPTION_PNG_COMPRESSION_LEVEL 1000 |
| 159 |
#define OPTION_TRANSPARENCY_COMPOSITION_MODE 1001 |
| 160 |
#define OPTION_CODEC_THREADS 1002 |
| 161 |
#define OPTION_EXTRACT_MIME_ITEM 1003 |
| 162 |
|
| 163 |
static option long_options[] = { |
| 164 |
{(char* const) "quality", required_argument, 0, 'q'}, |
| 165 |
{(char* const) "strict", no_argument, 0, 's'}, |
| 166 |
{(char* const) "decoder", required_argument, 0, 'd'}, |
| 167 |
{(char* const) "output", required_argument, 0, 'o'}, |
| 168 |
{(char* const) "quiet", no_argument, &option_quiet, 1}, |
| 169 |
{(char* const) "with-aux", no_argument, &option_aux, 1}, |
| 170 |
{(char* const) "with-xmp", no_argument, &option_with_xmp, 1}, |
| 171 |
{(char* const) "with-exif", no_argument, &option_with_exif, 1}, |
| 172 |
{(char* const) "skip-exif-offset", no_argument, &option_skip_exif_offset, 1}, |
| 173 |
{(char* const) "no-colons", no_argument, &option_no_colons, 1}, |
| 174 |
{(char* const) "list-decoders", no_argument, &option_list_decoders, 1}, |
| 175 |
{(char* const) "tiles", no_argument, &option_output_tiles, 1}, |
| 176 |
{(char* const) "sequence", no_argument, &option_sequence, 1}, |
| 177 |
{(char* const) "help", no_argument, 0, 'h'}, |
| 178 |
{(char* const) "chroma-upsampling", required_argument, 0, 'C'}, |
| 179 |
{(char* const) "png-compression-level", required_argument, 0, OPTION_PNG_COMPRESSION_LEVEL}, |
| 180 |
{(char* const) "transparency-composition-mode", required_argument, 0, OPTION_TRANSPARENCY_COMPOSITION_MODE}, |
| 181 |
{(char* const) "version", no_argument, 0, 'v'}, |
| 182 |
{(char* const) "disable-limits", no_argument, &option_disable_limits, 1}, |
| 183 |
{(char* const) "ignore-editlist", no_argument, &option_ignore_editlist, 1}, |
| 184 |
{(char* const) "codec-threads", required_argument, 0, OPTION_CODEC_THREADS}, |
| 185 |
{(char* const) "extract-mime-item", required_argument, 0, OPTION_EXTRACT_MIME_ITEM}, |
| 186 |
{nullptr, no_argument, nullptr, 0} |
| 187 |
}; |
| 188 |
|
| 189 |
|
| 190 |
#define MAX_DECODERS 20 |
| 191 |
|
| 192 |
bool is_integer_string(const char* s) |
| 193 |
{ |
| 194 |
if (strlen(s)==0) { |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
if (!(isdigit(s[0]) || s[0]=='-')) { |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
for (size_t i=strlen(s)-1; i>=1 ; i--) { |
| 203 |
if (!isdigit(s[i])) { |
| 204 |
return false; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
return true; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
void show_png_compression_level_usage_warning() |
| 213 |
{ |
| 214 |
fprintf(stderr, "Invalid PNG compression level. Has to be between 0 (fastest) and 9 (best).\n" |
| 215 |
"You can also use -1 to use the default compression level.\n"); |
| 216 |
} |
| 217 |
|
| 218 |
std::string sanitizeFilename(const std::string& filename) { |
| 219 |
static const std::unordered_set<char> invalidChars = {'\\','/','*','?','"','<','>','|', ':'}; |
| 220 |
std::string sanitized; |
| 221 |
sanitized.reserve(filename.size()); // avoid reallocations |
| 222 |
|
| 223 |
for (char c : filename) { |
| 224 |
if (invalidChars.find(c) != invalidChars.end()) { |
| 225 |
sanitized += '_'; |
| 226 |
} else { |
| 227 |
sanitized += c; |
| 228 |
} |
| 229 |
} |
| 230 |
return sanitized; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
int decode_single_image(heif_image_handle* handle, |
| 235 |
std::string filename_stem, |
| 236 |
std::string filename_suffix, |
| 237 |
heif_decoding_options* decode_options, |
| 238 |
std::unique_ptr<Encoder>& encoder) |
| 239 |
{ |
| 240 |
int bit_depth = heif_image_handle_get_luma_bits_per_pixel(handle); |
| 241 |
if (bit_depth < 0) { |
| 242 |
std::cerr << "Input image has undefined bit-depth\n"; |
| 243 |
return 1; |
| 244 |
} |
| 245 |
|
| 246 |
int has_alpha = heif_image_handle_has_alpha_channel(handle); |
| 247 |
|
| 248 |
heif_image* image; |
| 249 |
heif_error err; |
| 250 |
err = heif_decode_image(handle, |
| 251 |
&image, |
| 252 |
encoder->colorspace(has_alpha), |
| 253 |
encoder->chroma(has_alpha, bit_depth), |
| 254 |
decode_options); |
| 255 |
if (err.code) { |
| 256 |
std::cerr << "Could not decode image: " |
| 257 |
<< err.message << "\n"; |
| 258 |
return 1; |
| 259 |
} |
| 260 |
|
| 261 |
// show decoding warnings |
| 262 |
|
| 263 |
for (int i = 0;; i++) { |
| 264 |
int n = heif_image_get_decoding_warnings(image, i, &err, 1); |
| 265 |
if (n == 0) { |
| 266 |
break; |
| 267 |
} |
| 268 |
|
| 269 |
std::cerr << "Warning: " << err.message << "\n"; |
| 270 |
} |
| 271 |
|
| 272 |
if (image) { |
| 273 |
std::string filename = filename_stem + '.' + filename_suffix; |
| 274 |
|
| 275 |
bool written = encoder->Encode(handle, image, filename); |
| 276 |
if (!written) { |
| 277 |
fprintf(stderr, "could not write image\n"); |
| 278 |
} |
| 279 |
else { |
| 280 |
if (!option_quiet) { |
| 281 |
std::cout << "Written to " << filename << "\n"; |
| 282 |
} |
| 283 |
} |
| 284 |
heif_image_release(image); |
| 285 |
|
| 286 |
|
| 287 |
if (option_aux) { |
| 288 |
int has_depth = heif_image_handle_has_depth_image(handle); |
| 289 |
if (has_depth) { |
| 290 |
heif_item_id depth_id; |
| 291 |
int nDepthImages = heif_image_handle_get_list_of_depth_image_IDs(handle, &depth_id, 1); |
| 292 |
assert(nDepthImages == 1); |
| 293 |
(void) nDepthImages; |
| 294 |
|
| 295 |
heif_image_handle* depth_handle = nullptr; |
| 296 |
err = heif_image_handle_get_depth_image_handle(handle, depth_id, &depth_handle); |
| 297 |
if (err.code) { |
| 298 |
std::cerr << "Could not read depth channel\n"; |
| 299 |
return 1; |
| 300 |
} |
| 301 |
|
| 302 |
int depth_bit_depth = heif_image_handle_get_luma_bits_per_pixel(depth_handle); |
| 303 |
|
| 304 |
heif_image* depth_image; |
| 305 |
err = heif_decode_image(depth_handle, |
| 306 |
&depth_image, |
| 307 |
encoder->colorspace(false), |
| 308 |
encoder->chroma(false, depth_bit_depth), |
| 309 |
nullptr); |
| 310 |
if (err.code) { |
| 311 |
heif_image_handle_release(depth_handle); |
| 312 |
std::cerr << "Could not decode depth image: " << err.message << "\n"; |
| 313 |
return 1; |
| 314 |
} |
| 315 |
|
| 316 |
std::ostringstream s; |
| 317 |
s << filename_stem; |
| 318 |
s << "-depth."; |
| 319 |
s << filename_suffix; |
| 320 |
|
| 321 |
written = encoder->Encode(depth_handle, depth_image, s.str()); |
| 322 |
if (!written) { |
| 323 |
fprintf(stderr, "could not write depth image\n"); |
| 324 |
} |
| 325 |
else { |
| 326 |
if (!option_quiet) { |
| 327 |
std::cout << "Depth image written to " << s.str() << "\n"; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
heif_image_release(depth_image); |
| 332 |
heif_image_handle_release(depth_handle); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
// --- aux images |
| 338 |
|
| 339 |
if (option_aux) { |
| 340 |
int nAuxImages = heif_image_handle_get_number_of_auxiliary_images(handle, LIBHEIF_AUX_IMAGE_FILTER_OMIT_ALPHA | LIBHEIF_AUX_IMAGE_FILTER_OMIT_DEPTH); |
| 341 |
if (nAuxImages > 0) { |
| 342 |
|
| 343 |
std::vector<heif_item_id> auxIDs(nAuxImages); |
| 344 |
heif_image_handle_get_list_of_auxiliary_image_IDs(handle, |
| 345 |
LIBHEIF_AUX_IMAGE_FILTER_OMIT_ALPHA | LIBHEIF_AUX_IMAGE_FILTER_OMIT_DEPTH, |
| 346 |
auxIDs.data(), nAuxImages); |
| 347 |
|
| 348 |
for (heif_item_id auxId : auxIDs) { |
| 349 |
|
| 350 |
heif_image_handle* aux_handle = nullptr; |
| 351 |
err = heif_image_handle_get_auxiliary_image_handle(handle, auxId, &aux_handle); |
| 352 |
if (err.code) { |
| 353 |
std::cerr << "Could not read auxiliary image\n"; |
| 354 |
return 1; |
| 355 |
} |
| 356 |
|
| 357 |
int aux_bit_depth = heif_image_handle_get_luma_bits_per_pixel(aux_handle); |
| 358 |
|
| 359 |
heif_image* aux_image = nullptr; |
| 360 |
err = heif_decode_image(aux_handle, |
| 361 |
&aux_image, |
| 362 |
encoder->colorspace(false), |
| 363 |
encoder->chroma(false, aux_bit_depth), |
| 364 |
nullptr); |
| 365 |
if (err.code) { |
| 366 |
heif_image_handle_release(aux_handle); |
| 367 |
std::cerr << "Could not decode auxiliary image: " << err.message << "\n"; |
| 368 |
return 1; |
| 369 |
} |
| 370 |
|
| 371 |
const char* auxTypeC = nullptr; |
| 372 |
err = heif_image_handle_get_auxiliary_type(aux_handle, &auxTypeC); |
| 373 |
if (err.code) { |
| 374 |
heif_image_release(aux_image); |
| 375 |
heif_image_handle_release(aux_handle); |
| 376 |
std::cerr << "Could not get type of auxiliary image: " << err.message << "\n"; |
| 377 |
return 1; |
| 378 |
} |
| 379 |
|
| 380 |
std::string auxType = std::string(auxTypeC); |
| 381 |
|
| 382 |
heif_image_handle_release_auxiliary_type(aux_handle, &auxTypeC); |
| 383 |
|
| 384 |
if (option_no_colons) { |
| 385 |
std::replace(auxType.begin(), auxType.end(), ':', '_'); |
| 386 |
} |
| 387 |
|
| 388 |
std::ostringstream s; |
| 389 |
s << filename_stem; |
| 390 |
s << "-" + sanitizeFilename(auxType) + "."; |
| 391 |
s << filename_suffix; |
| 392 |
|
| 393 |
std::string auxFilename = s.str(); |
| 394 |
|
| 395 |
written = encoder->Encode(aux_handle, aux_image, auxFilename); |
| 396 |
if (!written) { |
| 397 |
fprintf(stderr, "could not write auxiliary image\n"); |
| 398 |
} |
| 399 |
else { |
| 400 |
if (!option_quiet) { |
| 401 |
std::cout << "Auxiliary image written to " << auxFilename << "\n"; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
heif_image_release(aux_image); |
| 406 |
heif_image_handle_release(aux_handle); |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
// --- write metadata |
| 413 |
|
| 414 |
if (option_with_xmp || option_with_exif) { |
| 415 |
int numMetadata = heif_image_handle_get_number_of_metadata_blocks(handle, nullptr); |
| 416 |
if (numMetadata > 0) { |
| 417 |
std::vector<heif_item_id> ids(numMetadata); |
| 418 |
heif_image_handle_get_list_of_metadata_block_IDs(handle, nullptr, ids.data(), numMetadata); |
| 419 |
|
| 420 |
for (int n = 0; n < numMetadata; n++) { |
| 421 |
|
| 422 |
// check whether metadata block is XMP |
| 423 |
|
| 424 |
std::string itemtype = heif_image_handle_get_metadata_type(handle, ids[n]); |
| 425 |
std::string contenttype = heif_image_handle_get_metadata_content_type(handle, ids[n]); |
| 426 |
|
| 427 |
if (option_with_xmp && contenttype == "application/rdf+xml") { |
| 428 |
// read XMP data to memory array |
| 429 |
|
| 430 |
size_t xmpSize = heif_image_handle_get_metadata_size(handle, ids[n]); |
| 431 |
std::vector<uint8_t> xmp(xmpSize); |
| 432 |
err = heif_image_handle_get_metadata(handle, ids[n], xmp.data()); |
| 433 |
if (err.code) { |
| 434 |
std::cerr << "Could not read XMP metadata: " << err.message << "\n"; |
| 435 |
return 1; |
| 436 |
} |
| 437 |
|
| 438 |
// write XMP data to file |
| 439 |
|
| 440 |
std::string xmp_filename = filename_stem + ".xmp"; |
| 441 |
std::ofstream ostr(xmp_filename.c_str()); |
| 442 |
ostr.write((char*) xmp.data(), xmpSize); |
| 443 |
} |
| 444 |
else if (option_with_exif && itemtype == "Exif") { |
| 445 |
// read EXIF data to memory array |
| 446 |
|
| 447 |
size_t exifSize = heif_image_handle_get_metadata_size(handle, ids[n]); |
| 448 |
std::vector<uint8_t> exif(exifSize); |
| 449 |
err = heif_image_handle_get_metadata(handle, ids[n], exif.data()); |
| 450 |
if (err.code) { |
| 451 |
std::cerr << "Could not read EXIF metadata: " << err.message << "\n"; |
| 452 |
return 1; |
| 453 |
} |
| 454 |
|
| 455 |
uint32_t offset = 0; |
| 456 |
if (option_skip_exif_offset) { |
| 457 |
if (exifSize < 4) { |
| 458 |
std::cerr << "Invalid EXIF metadata, it is too small.\n"; |
| 459 |
return 1; |
| 460 |
} |
| 461 |
|
| 462 |
offset = four_bytes_to_uint32(exif[0], exif[1], exif[2], exif[3]); |
| 463 |
offset += 4; |
| 464 |
|
| 465 |
if (offset >= exifSize) { |
| 466 |
std::cerr << "Invalid EXIF metadata, offset out of range.\n"; |
| 467 |
return 1; |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
// write EXIF data to file |
| 472 |
|
| 473 |
std::string exif_filename = filename_stem + ".exif"; |
| 474 |
std::ofstream ostr(exif_filename.c_str()); |
| 475 |
ostr.write((char*) exif.data() + offset, exifSize - offset); |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
return 0; |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
int digits_for_integer(uint32_t v) |
| 487 |
{ |
| 488 |
int digits=1; |
| 489 |
|
| 490 |
while (v>=10) { |
| 491 |
digits++; |
| 492 |
v /= 10; |
| 493 |
} |
| 494 |
|
| 495 |
return digits; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
int decode_image_tiles(heif_image_handle* handle, |
| 500 |
std::string filename_stem, |
| 501 |
std::string filename_suffix, |
| 502 |
heif_decoding_options* decode_options, |
| 503 |
std::unique_ptr<Encoder>& encoder) |
| 504 |
{ |
| 505 |
heif_image_tiling tiling; |
| 506 |
|
| 507 |
heif_image_handle_get_image_tiling(handle, !decode_options->ignore_transformations, &tiling); |
| 508 |
if (tiling.num_columns == 1 && tiling.num_rows == 1) { |
| 509 |
return decode_single_image(handle, filename_stem, filename_suffix, decode_options, encoder); |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
int bit_depth = heif_image_handle_get_luma_bits_per_pixel(handle); |
| 514 |
if (bit_depth < 0) { |
| 515 |
std::cerr << "Input image has undefined bit-depth\n"; |
| 516 |
return 1; |
| 517 |
} |
| 518 |
|
| 519 |
int has_alpha = heif_image_handle_has_alpha_channel(handle); |
| 520 |
|
| 521 |
int digits_tx = digits_for_integer(tiling.num_columns-1); |
| 522 |
int digits_ty = digits_for_integer(tiling.num_rows-1); |
| 523 |
|
| 524 |
for (uint32_t ty = 0; ty < tiling.num_rows; ty++) |
| 525 |
for (uint32_t tx = 0; tx < tiling.num_columns; tx++) { |
| 526 |
heif_image* image; |
| 527 |
heif_error err; |
| 528 |
err = heif_image_handle_decode_image_tile(handle, |
| 529 |
&image, |
| 530 |
encoder->colorspace(has_alpha), |
| 531 |
encoder->chroma(has_alpha, bit_depth), |
| 532 |
decode_options, tx, ty); |
| 533 |
if (err.code) { |
| 534 |
std::cerr << "Could not decode image tile: " |
| 535 |
<< err.message << "\n"; |
| 536 |
return 1; |
| 537 |
} |
| 538 |
|
| 539 |
// show decoding warnings |
| 540 |
|
| 541 |
for (int i = 0;; i++) { |
| 542 |
int n = heif_image_get_decoding_warnings(image, i, &err, 1); |
| 543 |
if (n == 0) { |
| 544 |
break; |
| 545 |
} |
| 546 |
|
| 547 |
std::cerr << "Warning: " << err.message << "\n"; |
| 548 |
} |
| 549 |
|
| 550 |
if (image) { |
| 551 |
std::stringstream filename_str; |
| 552 |
filename_str << filename_stem << "-" |
| 553 |
<< std::setfill('0') << std::setw(digits_ty) << ty << '-' |
| 554 |
<< std::setfill('0') << std::setw(digits_tx) << tx << "." << filename_suffix; |
| 555 |
|
| 556 |
std::string filename = filename_str.str(); |
| 557 |
|
| 558 |
bool written = encoder->Encode(handle, image, filename); |
| 559 |
if (!written) { |
| 560 |
fprintf(stderr, "could not write image\n"); |
| 561 |
} |
| 562 |
else { |
| 563 |
if (!option_quiet) { |
| 564 |
std::cout << "Written to " << filename << "\n"; |
| 565 |
} |
| 566 |
} |
| 567 |
heif_image_release(image); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
return 0; |
| 572 |
} |
| 573 |
|
| 574 |
static int max_value_progress = 0; |
| 575 |
|
| 576 |
void start_progress(enum heif_progress_step step, int max_progress, void* progress_user_data) |
| 577 |
{ |
| 578 |
max_value_progress = max_progress; |
| 579 |
} |
| 580 |
|
| 581 |
void on_progress(enum heif_progress_step step, int progress, void* progress_user_data) |
| 582 |
{ |
| 583 |
std::cout << "decoding image... " << progress * 100 / max_value_progress << "%\r"; |
| 584 |
std::cout.flush(); |
| 585 |
} |
| 586 |
|
| 587 |
void end_progress(enum heif_progress_step step, void* progress_user_data) |
| 588 |
{ |
| 589 |
std::cout << "\n"; |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
class LibHeifInitializer { |
| 594 |
public: |
| 595 |
LibHeifInitializer() { heif_init(nullptr); } |
| 596 |
~LibHeifInitializer() { heif_deinit(); } |
| 597 |
}; |
| 598 |
|
| 599 |
|
| 600 |
int main(int argc, char** argv) |
| 601 |
{ |
| 602 |
// This takes care of initializing libheif and also deinitializing it at the end to free all resources. |
| 603 |
LibHeifInitializer initializer; |
| 604 |
|
| 605 |
int quality = -1; // Use default quality. |
| 606 |
bool strict_decoding = false; |
| 607 |
const char* decoder_id = nullptr; |
| 608 |
|
| 609 |
UNUSED(quality); // The quality will only be used by encoders that support it. |
| 610 |
//while ((opt = getopt(argc, argv, "q:s")) != -1) { |
| 611 |
while (true) { |
| 612 |
int option_index = 0; |
| 613 |
int c = getopt_long(argc, argv, "hq:sd:C:vo:S", long_options, &option_index); |
| 614 |
if (c == -1) { |
| 615 |
break; |
| 616 |
} |
| 617 |
|
| 618 |
switch (c) { |
| 619 |
case 'q': |
| 620 |
quality = atoi(optarg); |
| 621 |
break; |
| 622 |
case 'd': |
| 623 |
decoder_id = optarg; |
| 624 |
break; |
| 625 |
case 's': |
| 626 |
strict_decoding = true; |
| 627 |
break; |
| 628 |
case '?': |
| 629 |
std::cerr << "\n"; |
| 630 |
[[fallthrough]]; |
| 631 |
case 'h': |
| 632 |
show_help(argv[0]); |
| 633 |
return 0; |
| 634 |
case 'C': |
| 635 |
chroma_upsampling = optarg; |
| 636 |
if (chroma_upsampling != "nn" && |
| 637 |
chroma_upsampling != "nearest-neighbor" && |
| 638 |
chroma_upsampling != "bilinear") { |
| 639 |
fprintf(stderr, "Undefined chroma upsampling algorithm.\n"); |
| 640 |
exit(5); |
| 641 |
} |
| 642 |
if (chroma_upsampling == "nn") { // abbreviation |
| 643 |
chroma_upsampling = "nearest-neighbor"; |
| 644 |
} |
| 645 |
break; |
| 646 |
case OPTION_PNG_COMPRESSION_LEVEL: |
| 647 |
if (!is_integer_string(optarg)) { |
| 648 |
show_png_compression_level_usage_warning(); |
| 649 |
exit(5); |
| 650 |
} |
| 651 |
option_png_compression_level = std::stoi(optarg); |
| 652 |
if (option_png_compression_level < -1 || option_png_compression_level > 9) { |
| 653 |
show_png_compression_level_usage_warning(); |
| 654 |
exit(5); |
| 655 |
} |
| 656 |
break; |
| 657 |
case OPTION_TRANSPARENCY_COMPOSITION_MODE: |
| 658 |
if (strcmp(optarg, "white") == 0 || |
| 659 |
strcmp(optarg, "black") == 0 || |
| 660 |
strcmp(optarg, "checkerboard") == 0) { |
| 661 |
transparency_composition_mode = optarg; |
| 662 |
} |
| 663 |
else { |
| 664 |
fprintf(stderr, "Unknown transparency composition mode. Must be one of: white, black, checkerboard.\n"); |
| 665 |
exit(5); |
| 666 |
} |
| 667 |
break; |
| 668 |
case 'v': |
| 669 |
heif_examples::show_version(); |
| 670 |
return 0; |
| 671 |
case 'o': |
| 672 |
output_filename = optarg; |
| 673 |
break; |
| 674 |
case 'S': |
| 675 |
option_sequence = 1; |
| 676 |
break; |
| 677 |
case OPTION_CODEC_THREADS: |
| 678 |
option_num_codec_threads = atoi(optarg); |
| 679 |
break; |
| 680 |
case OPTION_EXTRACT_MIME_ITEM: |
| 681 |
option_extract_mime_item_type = optarg; |
| 682 |
break; |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
if (option_list_decoders) { |
| 687 |
heif_examples::list_all_decoders(); |
| 688 |
return 0; |
| 689 |
} |
| 690 |
|
| 691 |
if (optind >= argc || optind + 2 < argc) { |
| 692 |
// Need at least input filename as additional argument, but not more as two filenames. |
| 693 |
show_help(argv[0]); |
| 694 |
return 5; |
| 695 |
} |
| 696 |
|
| 697 |
std::string input_filename(argv[optind++]); |
| 698 |
std::string output_filename_stem; |
| 699 |
std::string output_filename_suffix; |
| 700 |
|
| 701 |
if (output_filename.empty()) { |
| 702 |
if (optind == argc) { |
| 703 |
std::string input_stem; |
| 704 |
size_t dot_pos = input_filename.rfind('.'); |
| 705 |
if (dot_pos != std::string::npos) { |
| 706 |
input_stem = input_filename.substr(0, dot_pos); |
| 707 |
} |
| 708 |
else { |
| 709 |
input_stem = input_filename; |
| 710 |
} |
| 711 |
|
| 712 |
output_filename = input_stem + ".jpg"; |
| 713 |
} |
| 714 |
else if (optind == argc-1) { |
| 715 |
output_filename = argv[optind]; |
| 716 |
} |
| 717 |
else { |
| 718 |
assert(false); |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
std::unique_ptr<Encoder> encoder; |
| 723 |
|
| 724 |
size_t dot_pos = output_filename.rfind('.'); |
| 725 |
if (dot_pos != std::string::npos) { |
| 726 |
output_filename_stem = output_filename.substr(0,dot_pos); |
| 727 |
std::string suffix_lowercase = output_filename.substr(dot_pos + 1); |
| 728 |
|
| 729 |
std::transform(suffix_lowercase.begin(), suffix_lowercase.end(), |
| 730 |
suffix_lowercase.begin(), ::tolower); |
| 731 |
|
| 732 |
output_filename_suffix = suffix_lowercase; |
| 733 |
|
| 734 |
if (suffix_lowercase == "jpg" || suffix_lowercase == "jpeg") { |
| 735 |
#if HAVE_LIBJPEG |
| 736 |
static const int kDefaultJpegQuality = 90; |
| 737 |
if (quality == -1) { |
| 738 |
quality = kDefaultJpegQuality; |
| 739 |
} |
| 740 |
encoder.reset(new JpegEncoder(quality)); |
| 741 |
#else |
| 742 |
fprintf(stderr, "JPEG support has not been compiled in.\n"); |
| 743 |
return 1; |
| 744 |
#endif // HAVE_LIBJPEG |
| 745 |
} |
| 746 |
|
| 747 |
if (suffix_lowercase == "png") { |
| 748 |
#if HAVE_LIBPNG |
| 749 |
auto pngEncoder = new PngEncoder(); |
| 750 |
pngEncoder->set_compression_level(option_png_compression_level); |
| 751 |
encoder.reset(pngEncoder); |
| 752 |
#else |
| 753 |
fprintf(stderr, "PNG support has not been compiled in.\n"); |
| 754 |
return 1; |
| 755 |
#endif // HAVE_LIBPNG |
| 756 |
} |
| 757 |
|
| 758 |
if (suffix_lowercase == "tif" || suffix_lowercase == "tiff") { |
| 759 |
#if HAVE_LIBTIFF |
| 760 |
encoder.reset(new TiffEncoder()); |
| 761 |
#else |
| 762 |
fprintf(stderr, "TIFF support has not been compiled in.\n"); |
| 763 |
return 1; |
| 764 |
#endif // HAVE_LIBTIFF |
| 765 |
} |
| 766 |
|
| 767 |
if (suffix_lowercase == "y4m") { |
| 768 |
encoder.reset(new Y4MEncoder()); |
| 769 |
} |
| 770 |
} |
| 771 |
else { |
| 772 |
output_filename_stem = output_filename; |
| 773 |
output_filename_suffix = "jpg"; |
| 774 |
} |
| 775 |
|
| 776 |
if (!encoder) { |
| 777 |
fprintf(stderr, "Unknown file type in %s\n", output_filename.c_str()); |
| 778 |
return 1; |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
// --- check whether input is a supported HEIF file |
| 783 |
|
| 784 |
if (int ret = heif_examples::check_for_valid_input_HEIF_file(input_filename)) { |
| 785 |
return ret; |
| 786 |
} |
| 787 |
|
| 788 |
// --- read the HEIF file |
| 789 |
|
| 790 |
heif_context* ctx = heif_context_alloc(); |
| 791 |
if (!ctx) { |
| 792 |
fprintf(stderr, "Could not create context object\n"); |
| 793 |
return 1; |
| 794 |
} |
| 795 |
|
| 796 |
if (option_disable_limits) { |
| 797 |
heif_context_set_security_limits(ctx, heif_get_disabled_security_limits()); |
| 798 |
} |
| 799 |
|
| 800 |
ContextReleaser cr(ctx); |
| 801 |
heif_error err; |
| 802 |
err = heif_context_read_from_file(ctx, input_filename.c_str(), nullptr); |
| 803 |
if (err.code != 0) { |
| 804 |
std::cerr << "Could not read HEIF/AVIF file: " << err.message << "\n"; |
| 805 |
return 1; |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
if (option_sequence) { |
| 810 |
if (!heif_context_has_sequence(ctx)) { |
| 811 |
std::cerr << "File contains no image sequence\n"; |
| 812 |
return 1; |
| 813 |
} |
| 814 |
|
| 815 |
int nTracks = heif_context_number_of_sequence_tracks(ctx); |
| 816 |
std::cout << "number of tracks: " << nTracks << "\n"; |
| 817 |
|
| 818 |
std::vector<uint32_t> track_ids(nTracks); |
| 819 |
heif_context_get_track_ids(ctx, track_ids.data()); |
| 820 |
|
| 821 |
for (uint32_t id : track_ids) { |
| 822 |
heif_track* track = heif_context_get_track(ctx, id); |
| 823 |
std::cout << "#" << id << " : " << heif_examples::fourcc_to_string(heif_track_get_track_handler_type(track)); |
| 824 |
|
| 825 |
if (heif_track_get_track_handler_type(track) == heif_track_type_image_sequence || |
| 826 |
heif_track_get_track_handler_type(track) == heif_track_type_video || |
| 827 |
heif_track_get_track_handler_type(track) == heif_track_type_auxiliary) { |
| 828 |
uint16_t w,h; |
| 829 |
heif_track_get_image_resolution(track, &w, &h); |
| 830 |
std::cout << " " << w << "x" << h; |
| 831 |
} |
| 832 |
else { |
| 833 |
uint32_t sample_entry_type = heif_track_get_sample_entry_type_of_first_cluster(track); |
| 834 |
std::cout << "\n sample entry type: " << heif_examples::fourcc_to_string(sample_entry_type); |
| 835 |
if (sample_entry_type == heif_fourcc('u', 'r', 'i', 'm')) { |
| 836 |
const char* uri; |
| 837 |
err = heif_track_get_urim_sample_entry_uri_of_first_cluster(track, &uri); |
| 838 |
if (err.code) { |
| 839 |
std::cerr << "Cannot read 'urim'-track URI: " << err.message << "\n"; |
| 840 |
} |
| 841 |
std::cout << "\n URI: " << uri; |
| 842 |
heif_string_release(uri); |
| 843 |
} |
| 844 |
|
| 845 |
// get metadata track samples |
| 846 |
|
| 847 |
for (;;) { |
| 848 |
heif_raw_sequence_sample* sample; |
| 849 |
err = heif_track_get_next_raw_sequence_sample(track, &sample); |
| 850 |
if (err.code != 0) { |
| 851 |
break; |
| 852 |
} |
| 853 |
|
| 854 |
size_t dataSize; |
| 855 |
const uint8_t* data = heif_raw_sequence_sample_get_data(sample, &dataSize); |
| 856 |
|
| 857 |
std::cout << "\n raw sample: "; |
| 858 |
for (uint32_t i = 0; i < dataSize; i++) { |
| 859 |
std::cout << " " << std::hex << (int)data[i]; |
| 860 |
} |
| 861 |
|
| 862 |
heif_raw_sequence_sample_release(sample); |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
std::cout << "\n"; |
| 867 |
|
| 868 |
heif_track_release(track); |
| 869 |
} |
| 870 |
|
| 871 |
std::unique_ptr<heif_decoding_options, void(*)(heif_decoding_options*)> decode_options(heif_decoding_options_alloc(), heif_decoding_options_free); |
| 872 |
encoder->UpdateDecodingOptions(nullptr, decode_options.get()); |
| 873 |
decode_options->ignore_sequence_editlist = option_ignore_editlist; |
| 874 |
decode_options->strict_decoding = strict_decoding; |
| 875 |
decode_options->decoder_id = decoder_id; |
| 876 |
decode_options->num_codec_threads = option_num_codec_threads; |
| 877 |
|
| 878 |
heif_track* track = heif_context_get_track(ctx, 0); |
| 879 |
|
| 880 |
const char* track_contentId = heif_track_get_gimi_track_content_id(track); |
| 881 |
if (track_contentId) { |
| 882 |
std::cout << "track content ID: " << track_contentId << "\n"; |
| 883 |
heif_string_release(track_contentId); |
| 884 |
} |
| 885 |
|
| 886 |
const heif_tai_clock_info* taic = heif_track_get_tai_clock_info_of_first_cluster(track); |
| 887 |
if (taic) { |
| 888 |
std::cout << "taic: " << taic->time_uncertainty << " / " << taic->clock_resolution << " / " |
| 889 |
<< taic->clock_drift_rate << " / " << int(taic->clock_type) << "\n"; |
| 890 |
} |
| 891 |
|
| 892 |
int with_alpha = heif_track_has_alpha_channel(track); |
| 893 |
|
| 894 |
for (int i=0; ;i++) { |
| 895 |
heif_image* out_image = nullptr; |
| 896 |
int bit_depth = 8; // TODO |
| 897 |
err = heif_track_decode_next_image(track, &out_image, |
| 898 |
encoder->colorspace(false), |
| 899 |
encoder->chroma(with_alpha, bit_depth), |
| 900 |
decode_options.get()); |
| 901 |
if (err.code == heif_error_End_of_sequence) { |
| 902 |
break; |
| 903 |
} |
| 904 |
else if (err.code) { |
| 905 |
std::cerr << err.message << "\n"; |
| 906 |
return 1; |
| 907 |
} |
| 908 |
|
| 909 |
std::cout << "sample duration " << heif_image_get_duration(out_image) << "\n"; |
| 910 |
|
| 911 |
const char* contentID = heif_image_get_gimi_sample_content_id(out_image); |
| 912 |
if (contentID) { |
| 913 |
std::cout << "content ID " << contentID << "\n"; |
| 914 |
heif_string_release(contentID); |
| 915 |
} |
| 916 |
|
| 917 |
heif_tai_timestamp_packet* timestamp; |
| 918 |
err = heif_image_get_tai_timestamp(out_image, ×tamp); |
| 919 |
if (err.code) { |
| 920 |
std::cerr << err.message << "\n"; |
| 921 |
return 10; |
| 922 |
} |
| 923 |
else if (timestamp) { |
| 924 |
std::cout << "TAI timestamp: " << timestamp->tai_timestamp << "\n"; |
| 925 |
heif_tai_timestamp_packet_release(timestamp); |
| 926 |
} |
| 927 |
|
| 928 |
std::ostringstream s; |
| 929 |
s << output_filename_stem; |
| 930 |
s << "-" << i+1; |
| 931 |
s << "." << output_filename_suffix; |
| 932 |
std::string numbered_filename = s.str(); |
| 933 |
|
| 934 |
bool written = encoder->Encode(nullptr, out_image, numbered_filename); |
| 935 |
if (!written) { |
| 936 |
fprintf(stderr, "could not write image\n"); |
| 937 |
} |
| 938 |
else { |
| 939 |
if (!option_quiet) { |
| 940 |
std::cout << "Written to " << numbered_filename << "\n"; |
| 941 |
} |
| 942 |
} |
| 943 |
heif_image_release(out_image); |
| 944 |
} |
| 945 |
|
| 946 |
heif_track_release(track); |
| 947 |
|
| 948 |
return 0; |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
int num_images = heif_context_get_number_of_top_level_images(ctx); |
| 953 |
if (num_images == 0) { |
| 954 |
fprintf(stderr, "File doesn't contain any images\n"); |
| 955 |
return 1; |
| 956 |
} |
| 957 |
|
| 958 |
if (!option_quiet) { |
| 959 |
std::cout << "File contains " << num_images << " image" << (num_images>1 ? "s" : "") << "\n"; |
| 960 |
} |
| 961 |
|
| 962 |
std::vector<heif_item_id> image_IDs(num_images); |
| 963 |
num_images = heif_context_get_list_of_top_level_image_IDs(ctx, image_IDs.data(), num_images); |
| 964 |
|
| 965 |
|
| 966 |
std::string filename; |
| 967 |
size_t image_index = 1; // Image filenames are "1" based. |
| 968 |
|
| 969 |
for (int idx = 0; idx < num_images; ++idx) { |
| 970 |
|
| 971 |
std::string numbered_output_filename_stem; |
| 972 |
|
| 973 |
if (num_images > 1) { |
| 974 |
std::ostringstream s; |
| 975 |
s << output_filename_stem; |
| 976 |
s << "-" << image_index; |
| 977 |
numbered_output_filename_stem = s.str(); |
| 978 |
|
| 979 |
s << "." << output_filename_suffix; |
| 980 |
filename = s.str(); |
| 981 |
} |
| 982 |
else { |
| 983 |
filename = output_filename; |
| 984 |
numbered_output_filename_stem = output_filename_stem; |
| 985 |
} |
| 986 |
|
| 987 |
struct heif_image_handle* handle; |
| 988 |
err = heif_context_get_image_handle(ctx, image_IDs[idx], &handle); |
| 989 |
if (err.code) { |
| 990 |
std::cerr << "Could not read HEIF/AVIF image " << idx << ": " |
| 991 |
<< err.message << "\n"; |
| 992 |
return 1; |
| 993 |
} |
| 994 |
|
| 995 |
std::unique_ptr<heif_decoding_options, void(*)(heif_decoding_options*)> decode_options(heif_decoding_options_alloc(), heif_decoding_options_free); |
| 996 |
encoder->UpdateDecodingOptions(handle, decode_options.get()); |
| 997 |
|
| 998 |
decode_options->strict_decoding = strict_decoding; |
| 999 |
decode_options->decoder_id = decoder_id; |
| 1000 |
decode_options->num_codec_threads = option_num_codec_threads; |
| 1001 |
|
| 1002 |
if (!option_quiet) { |
| 1003 |
decode_options->start_progress = start_progress; |
| 1004 |
decode_options->on_progress = on_progress; |
| 1005 |
decode_options->end_progress = end_progress; |
| 1006 |
} |
| 1007 |
|
| 1008 |
if (chroma_upsampling=="nearest-neighbor") { |
| 1009 |
decode_options->color_conversion_options.preferred_chroma_upsampling_algorithm = heif_chroma_upsampling_nearest_neighbor; |
| 1010 |
decode_options->color_conversion_options.only_use_preferred_chroma_algorithm = true; |
| 1011 |
} |
| 1012 |
else if (chroma_upsampling=="bilinear") { |
| 1013 |
decode_options->color_conversion_options.preferred_chroma_upsampling_algorithm = heif_chroma_upsampling_bilinear; |
| 1014 |
decode_options->color_conversion_options.only_use_preferred_chroma_algorithm = true; |
| 1015 |
} |
| 1016 |
|
| 1017 |
int ret; |
| 1018 |
|
| 1019 |
auto* color_conversion_options_ext = heif_color_conversion_options_ext_alloc(); |
| 1020 |
decode_options->color_conversion_options_ext = color_conversion_options_ext; |
| 1021 |
|
| 1022 |
|
| 1023 |
// If output file format does not support transparency, render in the selected composition mode. |
| 1024 |
|
| 1025 |
if (!encoder->supports_alpha()) { |
| 1026 |
if (transparency_composition_mode == "white") { |
| 1027 |
color_conversion_options_ext->alpha_composition_mode = heif_alpha_composition_mode_solid_color; |
| 1028 |
color_conversion_options_ext->background_red = 0xFFFFU; |
| 1029 |
color_conversion_options_ext->background_green = 0xFFFFU; |
| 1030 |
color_conversion_options_ext->background_blue = 0xFFFFU; |
| 1031 |
} |
| 1032 |
else if (transparency_composition_mode == "black") { |
| 1033 |
color_conversion_options_ext->alpha_composition_mode = heif_alpha_composition_mode_solid_color; |
| 1034 |
color_conversion_options_ext->background_red = 0; |
| 1035 |
color_conversion_options_ext->background_green = 0; |
| 1036 |
color_conversion_options_ext->background_blue = 0; |
| 1037 |
} |
| 1038 |
else if (transparency_composition_mode == "checkerboard") { |
| 1039 |
color_conversion_options_ext->alpha_composition_mode = heif_alpha_composition_mode_checkerboard; |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
if (option_output_tiles) { |
| 1044 |
ret = decode_image_tiles(handle, numbered_output_filename_stem, output_filename_suffix, decode_options.get(), encoder); |
| 1045 |
} |
| 1046 |
else { |
| 1047 |
ret = decode_single_image(handle, numbered_output_filename_stem, output_filename_suffix, decode_options.get(), encoder); |
| 1048 |
} |
| 1049 |
|
| 1050 |
heif_color_conversion_options_ext_free(color_conversion_options_ext); |
| 1051 |
|
| 1052 |
if (ret) { |
| 1053 |
heif_image_handle_release(handle); |
| 1054 |
return ret; |
| 1055 |
} |
| 1056 |
|
| 1057 |
heif_image_handle_release(handle); |
| 1058 |
|
| 1059 |
image_index++; |
| 1060 |
} |
| 1061 |
|
| 1062 |
|
| 1063 |
// --- extract MIME item data |
| 1064 |
|
| 1065 |
if (!option_extract_mime_item_type.empty()) { |
| 1066 |
int nItems = heif_context_get_number_of_items(ctx); |
| 1067 |
if (nItems > 0) { |
| 1068 |
std::vector<heif_item_id> item_ids(nItems); |
| 1069 |
heif_context_get_list_of_item_IDs(ctx, item_ids.data(), nItems); |
| 1070 |
|
| 1071 |
for (auto id : item_ids) { |
| 1072 |
uint32_t type = heif_item_get_item_type(ctx, id); |
| 1073 |
if (type == heif_item_type_mime) { |
| 1074 |
const char* mimeType = heif_item_get_mime_item_content_type(ctx, id); |
| 1075 |
if (option_extract_mime_item_type == mimeType) { |
| 1076 |
uint8_t* data = nullptr; |
| 1077 |
size_t data_size; |
| 1078 |
heif_item_get_item_data(ctx, id, nullptr, &data, &data_size); |
| 1079 |
std::ofstream ostr("mime-item.data"); |
| 1080 |
ostr.write((const char*)data, data_size); |
| 1081 |
heif_release_item_data(ctx, &data); |
| 1082 |
} |
| 1083 |
} |
| 1084 |
} |
| 1085 |
} |
| 1086 |
|
| 1087 |
} |
| 1088 |
|
| 1089 |
return 0; |
| 1090 |
} |