From 82a2b03ca20d70e8980251751f744b4301e8e5db Mon Sep 17 00:00:00 2001 From: dukesook Date: Tue, 21 May 2024 11:30:28 -0600 Subject: [PATCH 1/4] Reuse matching ispe box if one exists. --- libheif/file.cc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/libheif/file.cc b/libheif/file.cc index cea8f9fcbc..2310cf424e 100644 --- a/libheif/file.cc +++ b/libheif/file.cc @@ -966,6 +966,19 @@ std::shared_ptr HeifFile::add_new_infe_box(const char* item_type) void HeifFile::add_ispe_property(heif_item_id id, uint32_t width, uint32_t height) { + // Reuse matching ispe box if one exists. + auto properties = get_ipco_box()->get_all_child_boxes(); + for (uint16_t i = 0; i < properties.size(); i++) { + auto property = properties[i]; + if (property->get_short_type() == fourcc("ispe")) { + auto ispe = std::dynamic_pointer_cast(property); + if (ispe->get_width() == width && ispe->get_height() == height) { + m_ipma_box->add_property_for_item_ID(id, Box_ipma::PropertyAssociation{false, uint16_t(i + 1)}); + return; + } + } + } + auto ispe = std::make_shared(); ispe->set_size(width, height); From f22bcd054c58b1dc7200145fad3f2a51a2a15165 Mon Sep 17 00:00:00 2001 From: dukesook Date: Wed, 22 May 2024 08:15:45 -0600 Subject: [PATCH 2/4] Override the equals operator for class Box --- libheif/box.cc | 32 ++++++++++++++++++++++++++++++++ libheif/box.h | 7 +++++++ libheif/file.cc | 17 ++--------------- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/libheif/box.cc b/libheif/box.cc index e38e680224..b09ffc94bc 100644 --- a/libheif/box.cc +++ b/libheif/box.cc @@ -740,6 +740,38 @@ std::vector> Box::get_child_boxes(uint32_t short_type) cons } +int Box::find_or_append_child_box(const std::shared_ptr& box) +{ + for (size_t i = 0; i < m_children.size(); i++) { + if (Box::equal(m_children[i], box)) { + return i; + } + } + return append_child_box(box); +} + + +bool Box::operator==(const Box& other) const +{ + StreamWriter writer1; + StreamWriter writer2; + + this->write(writer1); + other.write(writer2); + + return writer1.get_data() == writer2.get_data(); +} + + +bool Box::equal(const std::shared_ptr& box1, const std::shared_ptr& box2) +{ + if (!box1 || !box2) { + return false; + } + return *box1 == *box2; +} + + Error Box::read_children(BitstreamRange& range, int max_number) { int count = 0; diff --git a/libheif/box.h b/libheif/box.h index 3a651f6ea9..2a037dc5dd 100644 --- a/libheif/box.h +++ b/libheif/box.h @@ -194,6 +194,13 @@ class Box : public BoxHeader return (int) m_children.size() - 1; } + int find_or_append_child_box(const std::shared_ptr& box); + + bool operator==(const Box& other) const; + + static bool equal(const std::shared_ptr& box1, const std::shared_ptr& box2); + + protected: virtual Error parse(BitstreamRange& range); diff --git a/libheif/file.cc b/libheif/file.cc index 2310cf424e..de91c1d3e4 100644 --- a/libheif/file.cc +++ b/libheif/file.cc @@ -966,23 +966,10 @@ std::shared_ptr HeifFile::add_new_infe_box(const char* item_type) void HeifFile::add_ispe_property(heif_item_id id, uint32_t width, uint32_t height) { - // Reuse matching ispe box if one exists. - auto properties = get_ipco_box()->get_all_child_boxes(); - for (uint16_t i = 0; i < properties.size(); i++) { - auto property = properties[i]; - if (property->get_short_type() == fourcc("ispe")) { - auto ispe = std::dynamic_pointer_cast(property); - if (ispe->get_width() == width && ispe->get_height() == height) { - m_ipma_box->add_property_for_item_ID(id, Box_ipma::PropertyAssociation{false, uint16_t(i + 1)}); - return; - } - } - } - auto ispe = std::make_shared(); ispe->set_size(width, height); - int index = m_ipco_box->append_child_box(ispe); + int index = m_ipco_box->find_or_append_child_box(ispe); m_ipma_box->add_property_for_item_ID(id, Box_ipma::PropertyAssociation{false, uint16_t(index + 1)}); } @@ -1080,7 +1067,7 @@ void HeifFile::add_pixi_property(heif_item_id id, uint8_t c1, uint8_t c2, uint8_ pixi->add_channel_bits(c3); } - int index = m_ipco_box->append_child_box(pixi); + int index = m_ipco_box->find_or_append_child_box(pixi); m_ipma_box->add_property_for_item_ID(id, Box_ipma::PropertyAssociation{false, uint16_t(index + 1)}); } From 437dcced5f58324989eab7d771bdeadc0d529c73 Mon Sep 17 00:00:00 2001 From: dukesook Date: Wed, 22 May 2024 08:25:39 -0600 Subject: [PATCH 3/4] Box operator== - quickly check if the 4cc's match before comparing binary data --- libheif/box.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libheif/box.cc b/libheif/box.cc index b09ffc94bc..452038676e 100644 --- a/libheif/box.cc +++ b/libheif/box.cc @@ -753,6 +753,10 @@ int Box::find_or_append_child_box(const std::shared_ptr& box) bool Box::operator==(const Box& other) const { + if (this->get_short_type() != other.get_short_type()) { + return false; + } + StreamWriter writer1; StreamWriter writer2; From a708d8adc8bd4986307f80d5822b61c96dce5699 Mon Sep 17 00:00:00 2001 From: dukesook Date: Wed, 22 May 2024 08:41:45 -0600 Subject: [PATCH 4/4] use a consistent datatype to supress warnings --- libheif/box.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libheif/box.cc b/libheif/box.cc index 452038676e..676718b7aa 100644 --- a/libheif/box.cc +++ b/libheif/box.cc @@ -742,7 +742,7 @@ std::vector> Box::get_child_boxes(uint32_t short_type) cons int Box::find_or_append_child_box(const std::shared_ptr& box) { - for (size_t i = 0; i < m_children.size(); i++) { + for (int i = 0; i < (int) m_children.size(); i++) { if (Box::equal(m_children[i], box)) { return i; }