Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Sep 22, 2024
1 parent 06a5629 commit 1f48f78
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 90 deletions.
25 changes: 0 additions & 25 deletions libheif/box.cc
Original file line number Diff line number Diff line change
Expand Up @@ -827,31 +827,6 @@ Error Box::write(StreamWriter& writer) const
}


std::shared_ptr<Box> Box::get_child_box(uint32_t short_type) const
{
for (auto& box : m_children) {
if (box->get_short_type() == short_type) {
return box;
}
}

return nullptr;
}


std::vector<std::shared_ptr<Box>> Box::get_child_boxes(uint32_t short_type) const
{
std::vector<std::shared_ptr<Box>> result;
for (auto& box : m_children) {
if (box->get_short_type() == short_type) {
result.push_back(box);
}
}

return result;
}


bool Box::operator==(const Box& other) const
{
if (this->get_short_type() != other.get_short_type()) {
Expand Down
29 changes: 21 additions & 8 deletions libheif/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,32 @@ class Box : public BoxHeader

std::string dump(Indent&) const override;

std::shared_ptr<Box> get_child_box(uint32_t short_type) const;
template<typename T> [[nodiscard]] std::shared_ptr<T> get_child_box() const
{
// TODO: we could remove the dynamic_cast<> by adding the fourcc type of each Box
// as a "constexpr uint32_t Box::short_type", compare to that and use static_cast<>
for (auto& box : m_children) {
if (auto typed_box = std::dynamic_pointer_cast<T>(box)) {
return typed_box;
}
}

return nullptr;
}


std::vector<std::shared_ptr<Box>> get_child_boxes(uint32_t short_type) const;

template<typename T>
std::vector<std::shared_ptr<T>> get_typed_child_boxes(uint32_t short_type) const
std::vector<std::shared_ptr<T>> get_child_boxes() const
{
auto boxes = get_child_boxes(short_type);
std::vector<std::shared_ptr<T>> typedBoxes;
for (const auto& box : boxes) {
typedBoxes.push_back(std::dynamic_pointer_cast<T>(box));
std::vector<std::shared_ptr<T>> result;
for (auto& box : m_children) {
if (auto typed_box = std::dynamic_pointer_cast<T>(box)) {
result.push_back(typed_box);
}
}
return typedBoxes;

return result;
}

const std::vector<std::shared_ptr<Box>>& get_all_child_boxes() const { return m_children; }
Expand Down
21 changes: 6 additions & 15 deletions libheif/codecs/image_item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -851,8 +851,7 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem::decode_image(const struct hei
error = ipco_box->get_properties_for_item_ID(m_id, ipma_box, properties);

for (const auto& property : properties) {
if (property->get_short_type() == fourcc("irot")) {
auto rot = std::dynamic_pointer_cast<Box_irot>(property);
if (auto rot = std::dynamic_pointer_cast<Box_irot>(property)) {
auto rotateResult = img->rotate_ccw(rot->get_rotation());
if (rotateResult.error) {
return error;
Expand All @@ -862,8 +861,7 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem::decode_image(const struct hei
}


if (property->get_short_type() == fourcc("imir")) {
auto mirror = std::dynamic_pointer_cast<Box_imir>(property);
if (auto mirror = std::dynamic_pointer_cast<Box_imir>(property)) {
auto mirrorResult = img->mirror_inplace(mirror->get_mirror_direction());
if (mirrorResult.error) {
return error;
Expand All @@ -872,8 +870,7 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem::decode_image(const struct hei
}


if (property->get_short_type() == fourcc("clap")) {
auto clap = std::dynamic_pointer_cast<Box_clap>(property);
if (auto clap = std::dynamic_pointer_cast<Box_clap>(property)) {
std::shared_ptr<HeifPixelImage> clap_img;

uint32_t img_width = img->get_width();
Expand Down Expand Up @@ -990,27 +987,21 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem::decode_image(const struct hei

// CLLI

auto clli_box = ipco_box->get_property_for_item_ID(m_id, ipma_box, fourcc("clli"));
auto clli = std::dynamic_pointer_cast<Box_clli>(clli_box);

auto clli = get_file()->get_property<Box_clli>(m_id);
if (clli) {
img->set_clli(clli->clli);
}

// MDCV

auto mdcv_box = ipco_box->get_property_for_item_ID(m_id, ipma_box, fourcc("mdcv"));
auto mdcv = std::dynamic_pointer_cast<Box_mdcv>(mdcv_box);

auto mdcv = get_file()->get_property<Box_mdcv>(m_id);
if (mdcv) {
img->set_mdcv(mdcv->mdcv);
}

// PASP

auto pasp_box = ipco_box->get_property_for_item_ID(m_id, ipma_box, fourcc("pasp"));
auto pasp = std::dynamic_pointer_cast<Box_pasp>(pasp_box);

auto pasp = get_file()->get_property<Box_pasp>(m_id);
if (pasp) {
img->set_pixel_ratio(pasp->hSpacing, pasp->vSpacing);
}
Expand Down
35 changes: 12 additions & 23 deletions libheif/codecs/mask_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,23 @@ Error MaskImageCodec::decode_mask_image(const HeifContext* context,
std::shared_ptr<HeifPixelImage>& img,
const std::vector<uint8_t>& data)
{
std::vector<std::shared_ptr<Box>> item_properties;
Error error = context->get_heif_file()->get_properties(ID, item_properties);
if (error) {
return error;
}
std::shared_ptr<Box_mskC> mskC;
std::shared_ptr<Box_ispe> ispe = context->get_heif_file()->get_property<Box_ispe>(ID);
std::shared_ptr<Box_mskC> mskC = context->get_heif_file()->get_property<Box_mskC>(ID);

uint32_t width = 0;
uint32_t height = 0;
bool found_ispe = false;
for (const auto& prop : item_properties) {
auto ispe = std::dynamic_pointer_cast<Box_ispe>(prop);
if (ispe) {
width = ispe->get_width();
height = ispe->get_height();
error = context->check_resolution(width, height);
if (error) {
return error;
}

found_ispe = true;
}

auto maybe_mskC = std::dynamic_pointer_cast<Box_mskC>(prop);
if (maybe_mskC) {
mskC = maybe_mskC;
if (ispe) {
width = ispe->get_width();
height = ispe->get_height();

Error error = context->check_resolution(width, height);
if (error) {
return error;
}
}
if (!found_ispe || !mskC) {

if (!ispe || !mskC) {
return Error(heif_error_Unsupported_feature,
heif_suberror_Unsupported_data_version,
"Missing required box for mask codec");
Expand Down
34 changes: 15 additions & 19 deletions libheif/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ Error HeifFile::parse_heif_file()
}


m_hdlr_box = std::dynamic_pointer_cast<Box_hdlr>(m_meta_box->get_child_box(fourcc("hdlr")));
m_hdlr_box = m_meta_box->get_child_box<Box_hdlr>();
if (STRICT_PARSING && !m_hdlr_box) {
return Error(heif_error_Invalid_input,
heif_suberror_No_hdlr_box);
Expand All @@ -363,25 +363,25 @@ Error HeifFile::parse_heif_file()

// --- find mandatory boxes needed for image decoding

m_pitm_box = std::dynamic_pointer_cast<Box_pitm>(m_meta_box->get_child_box(fourcc("pitm")));
m_pitm_box = m_meta_box->get_child_box<Box_pitm>();
if (!m_pitm_box) {
return Error(heif_error_Invalid_input,
heif_suberror_No_pitm_box);
}

m_iprp_box = std::dynamic_pointer_cast<Box_iprp>(m_meta_box->get_child_box(fourcc("iprp")));
m_iprp_box = m_meta_box->get_child_box<Box_iprp>();
if (!m_iprp_box) {
return Error(heif_error_Invalid_input,
heif_suberror_No_iprp_box);
}

m_ipco_box = std::dynamic_pointer_cast<Box_ipco>(m_iprp_box->get_child_box(fourcc("ipco")));
m_ipco_box = m_iprp_box->get_child_box<Box_ipco>();
if (!m_ipco_box) {
return Error(heif_error_Invalid_input,
heif_suberror_No_ipco_box);
}

auto ipma_boxes = m_iprp_box->get_typed_child_boxes<Box_ipma>(fourcc("ipma"));
auto ipma_boxes = m_iprp_box->get_child_boxes<Box_ipma>();
if (ipma_boxes.empty()) {
return Error(heif_error_Invalid_input,
heif_suberror_No_ipma_box);
Expand All @@ -391,37 +391,36 @@ Error HeifFile::parse_heif_file()
}
m_ipma_box = ipma_boxes[0];

m_iloc_box = std::dynamic_pointer_cast<Box_iloc>(m_meta_box->get_child_box(fourcc("iloc")));
m_iloc_box = m_meta_box->get_child_box<Box_iloc>();
if (!m_iloc_box) {
return Error(heif_error_Invalid_input,
heif_suberror_No_iloc_box);
}

m_idat_box = std::dynamic_pointer_cast<Box_idat>(m_meta_box->get_child_box(fourcc("idat")));
m_idat_box = m_meta_box->get_child_box<Box_idat>();

m_iref_box = std::dynamic_pointer_cast<Box_iref>(m_meta_box->get_child_box(fourcc("iref")));
m_iref_box = m_meta_box->get_child_box<Box_iref>();
if (m_iref_box) {
Error error = check_for_ref_cycle(get_primary_image_ID(), m_iref_box);
if (error) {
return error;
}
}

m_iinf_box = std::dynamic_pointer_cast<Box_iinf>(m_meta_box->get_child_box(fourcc("iinf")));
m_iinf_box = m_meta_box->get_child_box<Box_iinf>();
if (!m_iinf_box) {
return Error(heif_error_Invalid_input,
heif_suberror_No_iinf_box);
}

m_grpl_box = std::dynamic_pointer_cast<Box_grpl>(m_meta_box->get_child_box(fourcc("grpl")));
m_grpl_box = m_meta_box->get_child_box<Box_grpl>();


// --- build list of images

std::vector<std::shared_ptr<Box>> infe_boxes = m_iinf_box->get_child_boxes(fourcc("infe"));
std::vector<std::shared_ptr<Box_infe>> infe_boxes = m_iinf_box->get_child_boxes<Box_infe>();

for (auto& box : infe_boxes) {
std::shared_ptr<Box_infe> infe_box = std::dynamic_pointer_cast<Box_infe>(box);
for (auto& infe_box : infe_boxes) {
if (!infe_box) {
return Error(heif_error_Invalid_input,
heif_suberror_No_infe_box);
Expand Down Expand Up @@ -531,26 +530,23 @@ heif_chroma HeifFile::get_image_chroma_from_configuration(heif_item_id imageID)
{
// HEVC

auto box = m_ipco_box->get_property_for_item_ID(imageID, m_ipma_box, fourcc("hvcC"));
std::shared_ptr<Box_hvcC> hvcC_box = std::dynamic_pointer_cast<Box_hvcC>(box);
std::shared_ptr<Box_hvcC> hvcC_box = get_property<Box_hvcC>(imageID);
if (hvcC_box) {
return (heif_chroma) (hvcC_box->get_configuration().chroma_format);
}


// VVC

box = m_ipco_box->get_property_for_item_ID(imageID, m_ipma_box, fourcc("vvcC"));
std::shared_ptr<Box_vvcC> vvcC_box = std::dynamic_pointer_cast<Box_vvcC>(box);
std::shared_ptr<Box_vvcC> vvcC_box = get_property<Box_vvcC>(imageID);
if (vvcC_box) {
return (heif_chroma) (vvcC_box->get_configuration().chroma_format_idc);
}


// AV1

box = m_ipco_box->get_property_for_item_ID(imageID, m_ipma_box, fourcc("av1C"));
std::shared_ptr<Box_av1C> av1C_box = std::dynamic_pointer_cast<Box_av1C>(box);
std::shared_ptr<Box_av1C> av1C_box = get_property<Box_av1C>(imageID);
if (av1C_box) {
Box_av1C::configuration config = av1C_box->get_configuration();
if (config.chroma_subsampling_x == 1 &&
Expand Down

0 comments on commit 1f48f78

Please sign in to comment.