Skip to content

Commit 803b8f5

Browse files
committed
sort properties such that descriptive properties precede transformative properties (#1443)
1 parent 77ed09e commit 803b8f5

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

libheif/box.cc

+21
Original file line numberDiff line numberDiff line change
@@ -3147,6 +3147,27 @@ void Box_ipma::insert_entries_from_other_ipma_box(const Box_ipma& b)
31473147
}
31483148

31493149

3150+
void Box_ipma::sort_properties(const std::shared_ptr<Box_ipco>& ipco)
3151+
{
3152+
auto properties = ipco->get_all_child_boxes();
3153+
3154+
for (auto& item : m_entries) {
3155+
size_t nAssoc = item.associations.size();
3156+
3157+
// simple Bubble sort as a stable sorting algorithm
3158+
3159+
for (size_t n = 0; n < nAssoc - 1; n++)
3160+
for (size_t i = 0; i < nAssoc - 1 - n; i++) {
3161+
// If transformative property preceds descriptive property, swap them.
3162+
if (properties[item.associations[i].property_index - 1]->is_transformative_property() &&
3163+
!properties[item.associations[i + 1].property_index - 1]->is_transformative_property()) {
3164+
std::swap(item.associations[i], item.associations[i+1]);
3165+
}
3166+
}
3167+
}
3168+
}
3169+
3170+
31503171
Error Box_auxC::parse(BitstreamRange& range, const heif_security_limits* limits)
31513172
{
31523173
parse_full_box_header(range);

libheif/box.h

+11
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ class Box : public BoxHeader
260260

261261
void set_is_essential(bool flag) { m_is_essential = flag; }
262262

263+
virtual bool is_transformative_property() const { return false; } // only used for properties
264+
263265
protected:
264266
virtual Error parse(BitstreamRange& range, const heif_security_limits* limits);
265267

@@ -802,6 +804,9 @@ class Box_ipma : public FullBox
802804

803805
void insert_entries_from_other_ipma_box(const Box_ipma& b);
804806

807+
// sorts properties such that descriptive properties precede the transformative properties
808+
void sort_properties(const std::shared_ptr<Box_ipco>&);
809+
805810
protected:
806811
Error parse(BitstreamRange& range, const heif_security_limits*) override;
807812

@@ -854,6 +859,8 @@ class Box_irot : public Box
854859

855860
bool is_essential() const override { return true; }
856861

862+
bool is_transformative_property() const override { return true; }
863+
857864
std::string dump(Indent&) const override;
858865

859866
int get_rotation_ccw() const { return m_rotation; }
@@ -883,6 +890,8 @@ class Box_imir : public Box
883890

884891
bool is_essential() const override { return true; }
885892

893+
bool is_transformative_property() const override { return true; }
894+
886895
heif_transform_mirror_direction get_mirror_direction() const { return m_axis; }
887896

888897
void set_mirror_direction(heif_transform_mirror_direction dir) { m_axis = dir; }
@@ -911,6 +920,8 @@ class Box_clap : public Box
911920

912921
bool is_essential() const override { return true; }
913922

923+
bool is_transformative_property() const override { return true; }
924+
914925
std::string dump(Indent&) const override;
915926

916927
int left_rounded(uint32_t image_width) const; // first column

libheif/context.cc

+4
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ void HeifContext::write(StreamWriter& writer)
276276
img.second->process_before_write();
277277
}
278278

279+
// --- sort item properties
280+
281+
m_heif_file->get_ipma_box()->sort_properties(m_heif_file->get_ipco_box());
282+
279283
// --- write to file
280284

281285
m_heif_file->write(writer);

0 commit comments

Comments
 (0)