Skip to content

Commit

Permalink
Merge pull request #1175 from dukesook/unique_ispe
Browse files Browse the repository at this point in the history
Reuse matching ispe box if one exists.
  • Loading branch information
farindk authored Jun 10, 2024
2 parents f0c1a86 + a708d8a commit 1e75661
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
36 changes: 36 additions & 0 deletions libheif/box.cc
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,42 @@ std::vector<std::shared_ptr<Box>> Box::get_child_boxes(uint32_t short_type) cons
}


int Box::find_or_append_child_box(const std::shared_ptr<Box>& box)
{
for (int i = 0; i < (int) 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
{
if (this->get_short_type() != other.get_short_type()) {
return false;
}

StreamWriter writer1;
StreamWriter writer2;

this->write(writer1);
other.write(writer2);

return writer1.get_data() == writer2.get_data();
}


bool Box::equal(const std::shared_ptr<Box>& box1, const std::shared_ptr<Box>& box2)
{
if (!box1 || !box2) {
return false;
}
return *box1 == *box2;
}


Error Box::read_children(BitstreamRange& range, int max_number)
{
int count = 0;
Expand Down
7 changes: 7 additions & 0 deletions libheif/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>& box);

bool operator==(const Box& other) const;

static bool equal(const std::shared_ptr<Box>& box1, const std::shared_ptr<Box>& box2);


protected:
virtual Error parse(BitstreamRange& range);

Expand Down
4 changes: 2 additions & 2 deletions libheif/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ void HeifFile::add_ispe_property(heif_item_id id, uint32_t width, uint32_t heigh
auto ispe = std::make_shared<Box_ispe>();
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)});
}
Expand Down Expand Up @@ -1067,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)});
}
Expand Down

0 comments on commit 1e75661

Please sign in to comment.