From 1e64957df8769c04f9a0a87f3729d268d72aa733 Mon Sep 17 00:00:00 2001 From: Dirk Farin Date: Thu, 3 Oct 2024 00:49:31 +0200 Subject: [PATCH] replace all get_item_type() with get_item_type_4cc() --- libheif/api/libheif/heif_items.cc | 14 +++----- libheif/codecs/image_item.cc | 28 +++++++-------- libheif/context.cc | 60 +++++++++++++++---------------- libheif/file.cc | 45 ++++++++++++++--------- libheif/file.h | 2 ++ 5 files changed, 77 insertions(+), 72 deletions(-) diff --git a/libheif/api/libheif/heif_items.cc b/libheif/api/libheif/heif_items.cc index dd96dd4ae4..1fd383f8ae 100644 --- a/libheif/api/libheif/heif_items.cc +++ b/libheif/api/libheif/heif_items.cc @@ -61,13 +61,7 @@ int heif_context_get_list_of_item_IDs(const struct heif_context* ctx, uint32_t heif_context_get_item_type(const struct heif_context* ctx, heif_item_id item_id) { - auto type = ctx->context->get_heif_file()->get_item_type(item_id); - if (type.empty()) { - return 0; - } - else { - return fourcc(type.c_str()); - } + return ctx->context->get_heif_file()->get_item_type_4cc(item_id); } @@ -88,7 +82,7 @@ const char* heif_context_get_mime_item_content_type(const struct heif_context* c auto infe = ctx->context->get_heif_file()->get_infe_box(item_id); if (!infe) { return nullptr; } - if (infe->get_item_type() != "mime") { + if (infe->get_item_type_4cc() != fourcc("mime")) { return nullptr; } @@ -100,7 +94,7 @@ const char* heif_context_get_mime_item_content_encoding(const struct heif_contex auto infe = ctx->context->get_heif_file()->get_infe_box(item_id); if (!infe) { return nullptr; } - if (infe->get_item_type() != "mime") { + if (infe->get_item_type_4cc() != fourcc("mime")) { return nullptr; } @@ -113,7 +107,7 @@ const char* heif_context_get_uri_item_uri_type(const struct heif_context* ctx, h auto infe = ctx->context->get_heif_file()->get_infe_box(item_id); if (!infe) { return nullptr; } - if (infe->get_item_type() != "uri ") { + if (infe->get_item_type_4cc() != fourcc("uri ")) { return nullptr; } diff --git a/libheif/codecs/image_item.cc b/libheif/codecs/image_item.cc index a39dae2554..a4f855fd7b 100644 --- a/libheif/codecs/image_item.cc +++ b/libheif/codecs/image_item.cc @@ -101,46 +101,46 @@ heif_compression_format ImageItem::compression_format_from_fourcc_infe_type(uint std::shared_ptr ImageItem::alloc_for_infe_box(HeifContext* ctx, const std::shared_ptr& infe) { - std::string item_type = infe->get_item_type(); + uint32_t item_type = infe->get_item_type_4cc(); heif_item_id id = infe->get_item_ID(); - if (item_type == "jpeg" || - (item_type == "mime" && infe->get_content_type() == "image/jpeg")) { + if (item_type == fourcc("jpeg") || + (item_type == fourcc("mime") && infe->get_content_type() == "image/jpeg")) { return std::make_shared(ctx, id); } - else if (item_type == "hvc1") { + else if (item_type == fourcc("hvc1")) { return std::make_shared(ctx, id); } - else if (item_type == "av01") { + else if (item_type == fourcc("av01")) { return std::make_shared(ctx, id); } - else if (item_type == "vvc1") { + else if (item_type == fourcc("vvc1")) { return std::make_shared(ctx, id); } - else if (item_type == "avc1") { + else if (item_type == fourcc("avc1")) { return std::make_shared(ctx, id); } #if WITH_UNCOMPRESSED_CODEC - else if (item_type == "unci") { + else if (item_type == fourcc("unci")) { return std::make_shared(ctx, id); } #endif - else if (item_type == "j2k1") { + else if (item_type == fourcc("j2k1")) { return std::make_shared(ctx, id); } - else if (item_type == "mski") { + else if (item_type == fourcc("mski")) { return std::make_shared(ctx, id); } - else if (item_type == "grid") { + else if (item_type == fourcc("grid")) { return std::make_shared(ctx, id); } - else if (item_type == "iovl") { + else if (item_type == fourcc("iovl")) { return std::make_shared(ctx, id); } - else if (item_type == "iden") { + else if (item_type == fourcc("iden")) { return std::make_shared(ctx, id); } - else if (item_type == "tild") { + else if (item_type == fourcc("tild")) { return std::make_shared(ctx, id); } else { diff --git a/libheif/context.cc b/libheif/context.cc index 0553f8cbfc..f34fb01b6e 100644 --- a/libheif/context.cc +++ b/libheif/context.cc @@ -230,21 +230,21 @@ std::string HeifContext::debug_dump_boxes() const } -static bool item_type_is_image(const std::string& item_type, const std::string& content_type) +static bool item_type_is_image(uint32_t item_type, const std::string& content_type) { - return (item_type == "hvc1" || - item_type == "av01" || - item_type == "grid" || - item_type == "tild" || - item_type == "iden" || - item_type == "iovl" || - item_type == "avc1" || - item_type == "unci" || - item_type == "vvc1" || - item_type == "jpeg" || - (item_type == "mime" && content_type == "image/jpeg") || - item_type == "j2k1" || - item_type == "mski"); + return (item_type == fourcc("hvc1") || + item_type == fourcc("av01") || + item_type == fourcc("grid") || + item_type == fourcc("tild") || + item_type == fourcc("iden") || + item_type == fourcc("iovl") || + item_type == fourcc("avc1") || + item_type == fourcc("unci") || + item_type == fourcc("vvc1") || + item_type == fourcc("jpeg") || + (item_type == fourcc("mime") && content_type == "image/jpeg") || + item_type == fourcc("j2k1") || + item_type == fourcc("mski")); } @@ -629,7 +629,7 @@ Error HeifContext::interpret_heif_file() auto& image = pair.second; std::shared_ptr infe = m_heif_file->get_infe_box(image->get_id()); - if (infe->get_item_type() == "hvc1") { + if (infe->get_item_type_4cc() == fourcc("hvc1")) { auto ipma = m_heif_file->get_ipma_box(); auto ipco = m_heif_file->get_ipco_box(); @@ -640,7 +640,7 @@ Error HeifContext::interpret_heif_file() "No hvcC property in hvc1 type image"); } } - if (infe->get_item_type() == "vvc1") { + if (infe->get_item_type_4cc() == fourcc("vvc1")) { auto ipma = m_heif_file->get_ipma_box(); auto ipco = m_heif_file->get_ipco_box(); @@ -669,7 +669,7 @@ Error HeifContext::interpret_heif_file() break; } - if (infe_box->get_item_type() == "grid") { + if (infe_box->get_item_type_4cc() == fourcc("grid")) { std::vector image_references = iref_box->get_references(id, fourcc("dimg")); if (image_references.empty()) { @@ -698,12 +698,12 @@ Error HeifContext::interpret_heif_file() // --- read metadata and assign to image for (heif_item_id id : image_IDs) { - std::string item_type = m_heif_file->get_item_type(id); + uint32_t item_type = m_heif_file->get_item_type_4cc(id); std::string content_type = m_heif_file->get_content_type(id); // 'rgan': skip region annotations, handled next // 'iden': iden images are no metadata - if (item_type_is_image(item_type, content_type) || item_type == "rgan") { + if (item_type_is_image(item_type, content_type) || item_type == fourcc("rgan")) { continue; } @@ -713,13 +713,13 @@ Error HeifContext::interpret_heif_file() std::shared_ptr metadata = std::make_shared(); metadata->item_id = id; - metadata->item_type = item_type; + metadata->item_type = to_fourcc(item_type); metadata->content_type = content_type; metadata->item_uri_type = item_uri_type; Error err = m_heif_file->get_compressed_image_data(id, &(metadata->m_data)); if (err) { - if (item_type == "Exif" || item_type == "mime") { + if (item_type == fourcc("Exif") || item_type == fourcc("mime")) { // these item types should have data return err; } @@ -771,8 +771,8 @@ Error HeifContext::interpret_heif_file() // --- read region item and assign to image(s) for (heif_item_id id : image_IDs) { - std::string item_type = m_heif_file->get_item_type(id); - if (item_type != "rgan") { + uint32_t item_type = m_heif_file->get_item_type_4cc(id); + if (item_type != fourcc("rgan")) { continue; } @@ -875,8 +875,8 @@ bool HeifContext::has_alpha(heif_item_id ID) const // TODO: move this into ImageItem - std::string image_type = m_heif_file->get_item_type(ID); - if (image_type == "grid") { + uint32_t image_type = m_heif_file->get_item_type_4cc(ID); + if (image_type == fourcc("grid")) { std::vector grid_data; Error error = m_heif_file->get_compressed_image_data(ID, &grid_data); if (error) { @@ -937,10 +937,10 @@ bool HeifContext::has_alpha(heif_item_id ID) const Error HeifContext::get_id_of_non_virtual_child_image(heif_item_id id, heif_item_id& out) const { - std::string image_type = m_heif_file->get_item_type(id); - if (image_type == "grid" || - image_type == "iden" || - image_type == "iovl") { + uint32_t image_type = m_heif_file->get_item_type_4cc(id); + if (image_type == fourcc("grid") || + image_type == fourcc("iden") || + image_type == fourcc("iovl")) { auto iref_box = m_heif_file->get_iref_box(); if (!iref_box) { return Error(heif_error_Invalid_input, @@ -974,8 +974,6 @@ Result> HeifContext::decode_image(heif_item_id I const struct heif_decoding_options& options, bool decode_only_tile, uint32_t tx, uint32_t ty) const { - std::string image_type = m_heif_file->get_item_type(ID); - std::shared_ptr imginfo; if (m_all_images.find(ID) != m_all_images.end()) { imginfo = m_all_images.find(ID)->second; diff --git a/libheif/file.cc b/libheif/file.cc index 9a524b32c8..edcd051352 100644 --- a/libheif/file.cc +++ b/libheif/file.cc @@ -489,6 +489,17 @@ std::string HeifFile::get_item_type(heif_item_id ID) const } +uint32_t HeifFile::get_item_type_4cc(heif_item_id ID) const +{ + auto infe_box = get_infe_box(ID); + if (!infe_box) { + return 0; + } + + return infe_box->get_item_type_4cc(); +} + + std::string HeifFile::get_content_type(heif_item_id ID) const { auto infe_box = get_infe_box(ID); @@ -590,43 +601,43 @@ Error HeifFile::get_compressed_image_data(heif_item_id ID, std::vector* } - std::string item_type = infe_box->get_item_type(); + uint32_t item_type = infe_box->get_item_type_4cc(); std::string content_type = infe_box->get_content_type(); // --- get coded image data pointers - if (item_type == "hvc1") { + if (item_type == fourcc("hvc1")) { // --- --- --- HEVC assert(false); } - else if (item_type == "vvc1") { + else if (item_type == fourcc("vvc1")) { // --- --- --- VVC assert(false); } - else if (item_type == "av01") { + else if (item_type == fourcc("av01")) { assert(false); } - else if (item_type == "jpeg" || - (item_type == "mime" && get_content_type(ID) == "image/jpeg")) { + else if (item_type == fourcc("jpeg") || + (item_type == fourcc("mime") && get_content_type(ID) == "image/jpeg")) { assert(false); } - else if (item_type == "j2k1") { + else if (item_type == fourcc("j2k1")) { assert(false); } #if WITH_UNCOMPRESSED_CODEC - else if (item_type == "unci") { + else if (item_type == fourcc("unci")) { assert(false); // return get_compressed_image_data_uncompressed(ID, data, item); } #endif else if (true || // fallback case for all kinds of generic metadata (e.g. 'iptc') - item_type == "grid" || - item_type == "iovl" || - item_type == "Exif" || - (item_type == "mime" && content_type == "application/rdf+xml")) { + item_type == fourcc("grid") || + item_type == fourcc("iovl") || + item_type == fourcc("Exif") || + (item_type == fourcc("mime") && content_type == "application/rdf+xml")) { Error error; bool read_uncompressed = true; - if (item_type == "mime") { + if (item_type == fourcc("mime")) { std::string encoding = infe_box->get_content_encoding(); if (encoding == "compress_zlib") { #if HAVE_ZLIB @@ -725,12 +736,12 @@ Error HeifFile::get_item_data(heif_item_id ID, std::vector* out_data, h heif_suberror_Nonexisting_item_referenced}; } - std::string item_type = infe_box->get_item_type(); + uint32_t item_type = infe_box->get_item_type_4cc(); std::string content_type = infe_box->get_content_type(); // --- non 'mime' data (uncompressed) - if (item_type != "mime") { + if (item_type != fourcc("mime")) { if (out_compression) { *out_compression = heif_metadata_compression_off; } @@ -1037,7 +1048,7 @@ Error HeifFile::set_item_data(const std::shared_ptr& item, const uint8 // only set metadata compression for MIME type data which has 'content_encoding' field if (compression != heif_metadata_compression_off && - item->get_item_type() != "mime") { + item->get_item_type_4cc() != fourcc("mime")) { // TODO: error, compression not supported } @@ -1081,7 +1092,7 @@ Error HeifFile::set_precompressed_item_data(const std::shared_ptr& ite { // only set metadata compression for MIME type data which has 'content_encoding' field if (!content_encoding.empty() && - item->get_item_type() != "mime") { + item->get_item_type_4cc() != fourcc("mime")) { // TODO: error, compression not supported } diff --git a/libheif/file.h b/libheif/file.h index 258d8dcadb..47f1c2247d 100644 --- a/libheif/file.h +++ b/libheif/file.h @@ -84,6 +84,8 @@ class HeifFile std::string get_item_type(heif_item_id ID) const; + uint32_t get_item_type_4cc(heif_item_id ID) const; + std::string get_content_type(heif_item_id ID) const; std::string get_item_uri_type(heif_item_id ID) const;