From acdc8d9837fb75924ef25bebb964f363356c3d9c Mon Sep 17 00:00:00 2001 From: Dirk Farin Date: Mon, 23 Sep 2024 00:36:26 +0200 Subject: [PATCH] simplify code by replacing dynamic_casts with get_property<> function calls --- libheif/codecs/avif.cc | 30 ++++------------------------ libheif/codecs/grid.cc | 6 +----- libheif/codecs/hevc.cc | 29 ++++----------------------- libheif/codecs/jpeg.cc | 24 ++++------------------ libheif/codecs/jpeg2000.cc | 27 ++----------------------- libheif/codecs/uncompressed_image.cc | 18 ++++++----------- libheif/codecs/vvc.cc | 20 ++++--------------- 7 files changed, 25 insertions(+), 129 deletions(-) diff --git a/libheif/codecs/avif.cc b/libheif/codecs/avif.cc index 5c3b0f9f67..a7e5b7efc6 100644 --- a/libheif/codecs/avif.cc +++ b/libheif/codecs/avif.cc @@ -610,39 +610,17 @@ Result ImageItem_AVIF::encode(const std::shared_ptr> ImageItem_AVIF::read_bitstream_configuration_data(heif_item_id itemId) const { - std::vector data; - - // --- get properties for this image - - std::vector> properties; - auto ipma_box = get_file()->get_ipma_box(); - Error err = get_file()->get_ipco_box()->get_properties_for_item_ID(itemId, ipma_box, properties); - if (err) - { - return err; - } - // --- get codec configuration - std::shared_ptr av1C_box; - for (auto &prop : properties) - { - if (prop->get_short_type() == fourcc("av1C")) - { - av1C_box = std::dynamic_pointer_cast(prop); - if (av1C_box) - { - break; - } - } - } - + std::shared_ptr av1C_box = get_file()->get_property(get_id()); if (!av1C_box) { return Error(heif_error_Invalid_input, heif_suberror_No_av1C_box); } - else if (!av1C_box->get_headers(&data)) + + std::vector data; + if (!av1C_box->get_headers(&data)) { return Error(heif_error_Invalid_input, heif_suberror_No_item_data); diff --git a/libheif/codecs/grid.cc b/libheif/codecs/grid.cc index c76862c7db..b75fa78c49 100644 --- a/libheif/codecs/grid.cc +++ b/libheif/codecs/grid.cc @@ -241,11 +241,7 @@ Result> ImageItem_Grid::decode_full_grid_image(c } } - - auto ipma = get_file()->get_ipma_box(); - auto ipco = get_file()->get_ipco_box(); - auto pixi_box = ipco->get_property_for_item_ID(get_id(), ipma, fourcc("pixi")); - auto pixi = std::dynamic_pointer_cast(pixi_box); + //auto pixi = get_file()->get_property(get_id()); const uint32_t w = grid.get_width(); const uint32_t h = grid.get_height(); diff --git a/libheif/codecs/hevc.cc b/libheif/codecs/hevc.cc index 1dcfc88934..4bdd0153a6 100644 --- a/libheif/codecs/hevc.cc +++ b/libheif/codecs/hevc.cc @@ -725,37 +725,16 @@ Result ImageItem_HEVC::encode(const std::shared_ptr> ImageItem_HEVC::read_bitstream_configuration_data(heif_item_id itemId) const { - std::vector data; - - // --- get properties for this image - std::vector> properties; - auto ipma_box = get_file()->get_ipma_box(); - Error err = get_file()->get_ipco_box()->get_properties_for_item_ID(itemId, ipma_box, properties); - if (err) - { - return err; - } - // --- get codec configuration - std::shared_ptr hvcC_box; - for (auto &prop : properties) - { - if (prop->get_short_type() == fourcc("hvcC")) - { - hvcC_box = std::dynamic_pointer_cast(prop); - if (hvcC_box) - { - break; - } - } - } - + std::shared_ptr hvcC_box = get_file()->get_property(get_id()); if (!hvcC_box) { return Error{heif_error_Invalid_input, heif_suberror_No_hvcC_box}; } - else if (!hvcC_box->get_headers(&data)) { + + std::vector data; + if (!hvcC_box->get_headers(&data)) { return Error{heif_error_Invalid_input, heif_suberror_No_item_data}; } diff --git a/libheif/codecs/jpeg.cc b/libheif/codecs/jpeg.cc index 75a5f0c13e..636c64e844 100644 --- a/libheif/codecs/jpeg.cc +++ b/libheif/codecs/jpeg.cc @@ -165,30 +165,14 @@ Result ImageItem_JPEG::encode(const std::shared_ptr> ImageItem_JPEG::read_bitstream_configuration_data(heif_item_id itemId) const { - std::vector data; - - // --- check if 'jpgC' is present - std::vector> properties; - auto ipma_box = get_file()->get_ipma_box(); - Error err = get_file()->get_ipco_box()->get_properties_for_item_ID(itemId, ipma_box, properties); - if (err) { - return err; - } - // --- get codec configuration - std::shared_ptr jpgC_box; - for (auto& prop : properties) { - if (prop->get_short_type() == fourcc("jpgC")) { - jpgC_box = std::dynamic_pointer_cast(prop); - if (jpgC_box) { - data = jpgC_box->get_data(); - break; - } - } + std::shared_ptr jpgC_box = get_file()->get_property(get_id()); + if (jpgC_box) { + return jpgC_box->get_data(); } - return data; + return std::vector{}; } diff --git a/libheif/codecs/jpeg2000.cc b/libheif/codecs/jpeg2000.cc index b601aff39c..7ae9eea245 100644 --- a/libheif/codecs/jpeg2000.cc +++ b/libheif/codecs/jpeg2000.cc @@ -525,32 +525,9 @@ Result ImageItem_JPEG2000::encode(const std::shared_p Result> ImageItem_JPEG2000::read_bitstream_configuration_data(heif_item_id itemId) const { - std::vector data; - - // --- get properties for this image - - std::vector> properties; - auto ipma_box = get_file()->get_ipma_box(); - Error err = get_file()->get_ipco_box()->get_properties_for_item_ID(itemId, ipma_box, properties); - if (err) { - return err; - } - // --- get codec configuration - std::shared_ptr j2kH_box; - for (auto &prop : properties) - { - if (prop->get_short_type() == fourcc("j2kH")) - { - j2kH_box = std::dynamic_pointer_cast(prop); - if (j2kH_box) - { - break; - } - } - } - + std::shared_ptr j2kH_box = get_file()->get_property(get_id()); if (!j2kH_box) { // TODO - Correctly Find the j2kH box @@ -562,7 +539,7 @@ Result> ImageItem_JPEG2000::read_bitstream_configuration_da // heif_suberror_No_item_data); // } - return data; + return std::vector{}; } diff --git a/libheif/codecs/uncompressed_image.cc b/libheif/codecs/uncompressed_image.cc index a53a812ffd..305f3e7cd7 100644 --- a/libheif/codecs/uncompressed_image.cc +++ b/libheif/codecs/uncompressed_image.cc @@ -310,16 +310,13 @@ Error UncompressedImageCodec::get_heif_chroma_uncompressed(const std::shared_ptr int UncompressedImageCodec::get_luma_bits_per_pixel_from_configuration_unci(const HeifFile& heif_file, heif_item_id imageID) { - auto ipco = heif_file.get_ipco_box(); - auto ipma = heif_file.get_ipma_box(); + std::shared_ptr uncC_box = heif_file.get_property(imageID); + std::shared_ptr cmpd_box = heif_file.get_property(imageID); - auto box1 = ipco->get_property_for_item_ID(imageID, ipma, fourcc("uncC")); - std::shared_ptr uncC_box = std::dynamic_pointer_cast(box1); - auto box2 = ipco->get_property_for_item_ID(imageID, ipma, fourcc("cmpd")); - std::shared_ptr cmpd_box = std::dynamic_pointer_cast(box2); if (!uncC_box) { return -1; } + if (!cmpd_box) { if (isKnownUncompressedFrameConfigurationBoxProfile(uncC_box)) { return 8; @@ -362,17 +359,14 @@ int UncompressedImageCodec::get_luma_bits_per_pixel_from_configuration_unci(cons int UncompressedImageCodec::get_chroma_bits_per_pixel_from_configuration_unci(const HeifFile& heif_file, heif_item_id imageID) { - auto ipco = heif_file.get_ipco_box(); - auto ipma = heif_file.get_ipma_box(); + std::shared_ptr uncC_box = heif_file.get_property(imageID); + std::shared_ptr cmpd_box = heif_file.get_property(imageID); - auto box1 = ipco->get_property_for_item_ID(imageID, ipma, fourcc("uncC")); - std::shared_ptr uncC_box = std::dynamic_pointer_cast(box1); - auto box2 = ipco->get_property_for_item_ID(imageID, ipma, fourcc("cmpd")); - std::shared_ptr cmpd_box = std::dynamic_pointer_cast(box2); if (uncC_box && uncC_box->get_version() == 1) { // All of the version 1 cases are 8 bit return 8; } + if (!uncC_box || !cmpd_box) { return -1; } diff --git a/libheif/codecs/vvc.cc b/libheif/codecs/vvc.cc index 18874f0bc1..4f9748ccce 100644 --- a/libheif/codecs/vvc.cc +++ b/libheif/codecs/vvc.cc @@ -620,8 +620,6 @@ Result ImageItem_VVC::encode(const std::shared_ptr> ImageItem_VVC::read_bitstream_configuration_data(heif_item_id itemId) const { - std::vector data; - // --- get properties for this image std::vector> properties; @@ -634,26 +632,16 @@ Result> ImageItem_VVC::read_bitstream_configuration_data(he // --- get codec configuration - std::shared_ptr vvcC_box; - for (auto &prop : properties) - { - if (prop->get_short_type() == fourcc("vvcC")) - { - vvcC_box = std::dynamic_pointer_cast(prop); - if (vvcC_box) - { - break; - } - } - } - + std::shared_ptr vvcC_box = get_file()->get_property(get_id()); if (!vvcC_box) { assert(false); return Error(heif_error_Invalid_input, heif_suberror_No_vvcC_box); } - else if (!vvcC_box->get_headers(&data)) + + std::vector data; + if (!vvcC_box->get_headers(&data)) { return Error(heif_error_Invalid_input, heif_suberror_No_item_data);