From de41052649fb53d72f8af24d8af370fd8a531c8f Mon Sep 17 00:00:00 2001 From: Dirk Farin Date: Mon, 30 Oct 2023 13:46:27 +0100 Subject: [PATCH] alternative implementation of PR #1015 --- examples/heif_enc.cc | 6 ++-- libheif/color-conversion/colorconversion.cc | 14 +-------- libheif/context.cc | 32 +++++++++++++++++---- libheif/nclx.cc | 22 ++++++++++++-- libheif/nclx.h | 2 ++ 5 files changed, 51 insertions(+), 25 deletions(-) diff --git a/examples/heif_enc.cc b/examples/heif_enc.cc index 5760ccecd4d..a62f878d189 100644 --- a/examples/heif_enc.cc +++ b/examples/heif_enc.cc @@ -64,9 +64,9 @@ int metadata_compression = 0; const char* encoderId = nullptr; std::string chroma_downsampling; -uint16_t nclx_matrix_coefficients = 6; -uint16_t nclx_colour_primaries = 2; -uint16_t nclx_transfer_characteristic = 2; +uint16_t nclx_matrix_coefficients = 1; +uint16_t nclx_colour_primaries = 1; +uint16_t nclx_transfer_characteristic = 13; int nclx_full_range = true; std::string property_pitm_description; diff --git a/libheif/color-conversion/colorconversion.cc b/libheif/color-conversion/colorconversion.cc index 8afb75733dc..6802c636e86 100644 --- a/libheif/color-conversion/colorconversion.cc +++ b/libheif/color-conversion/colorconversion.cc @@ -521,19 +521,7 @@ std::shared_ptr convert_colorspace(const std::shared_ptrget_color_profile_nclx(); } - // If some input nclx values are unspecified, use CCIR-601 values as default. - - if (input_state.nclx_profile.get_matrix_coefficients() == heif_matrix_coefficients_unspecified) { - input_state.nclx_profile.set_matrix_coefficients(heif_matrix_coefficients_ITU_R_BT_601_6); - } - - if (input_state.nclx_profile.get_colour_primaries() == heif_color_primaries_unspecified) { - input_state.nclx_profile.set_colour_primaries(heif_color_primaries_ITU_R_BT_601_6); - } - - if (input_state.nclx_profile.get_transfer_characteristics() == heif_color_primaries_unspecified) { - input_state.nclx_profile.set_transfer_characteristics(heif_transfer_characteristic_ITU_R_BT_601_6); - } + input_state.nclx_profile.replace_undefined_values_with_defaults(); std::set channels = input->get_channel_set(); assert(!channels.empty()); diff --git a/libheif/context.cc b/libheif/context.cc index 32077bb087c..ec850c8e746 100644 --- a/libheif/context.cc +++ b/libheif/context.cc @@ -2429,6 +2429,29 @@ static bool nclx_profile_matches_spec(heif_colorspace colorspace, } +static std::shared_ptr compute_target_nclx_profile(const std::shared_ptr& image, const heif_color_profile_nclx* output_nclx_profile) +{ + auto target_nclx_profile = std::make_shared(); + + // If there is an output NCLX specified, use that. + if (output_nclx_profile) { + target_nclx_profile->set_from_heif_color_profile_nclx(output_nclx_profile); + } + // Otherwise, if there is an input NCLX, keep that. + else if (auto input_nclx = image->get_color_profile_nclx()) { + *target_nclx_profile = *input_nclx; + } + // Otherwise, just use the defaults (set below) + else { + target_nclx_profile->set_undefined(); + } + + target_nclx_profile->replace_undefined_values_with_defaults(); + + return target_nclx_profile; +} + + Error HeifContext::encode_image_as_hevc(const std::shared_ptr& image, struct heif_encoder* encoder, const struct heif_encoding_options& options, @@ -2444,8 +2467,7 @@ Error HeifContext::encode_image_as_hevc(const std::shared_ptr& i heif_colorspace colorspace = image->get_colorspace(); heif_chroma chroma = image->get_chroma_format(); - auto target_nclx_profile = std::make_shared(); - target_nclx_profile->set_from_heif_color_profile_nclx(options.output_nclx_profile); + auto target_nclx_profile = compute_target_nclx_profile(image, options.output_nclx_profile); if (encoder->plugin->plugin_api_version >= 2) { encoder->plugin->query_input_colorspace2(encoder->encoder, &colorspace, &chroma); @@ -2652,8 +2674,7 @@ Error HeifContext::encode_image_as_av1(const std::shared_ptr& im heif_colorspace colorspace = image->get_colorspace(); heif_chroma chroma = image->get_chroma_format(); - auto target_nclx_profile = std::make_shared(); - target_nclx_profile->set_from_heif_color_profile_nclx(options.output_nclx_profile); + auto target_nclx_profile = compute_target_nclx_profile(image, options.output_nclx_profile); if (encoder->plugin->plugin_api_version >= 2) { encoder->plugin->query_input_colorspace2(encoder->encoder, &colorspace, &chroma); @@ -2828,8 +2849,7 @@ Error HeifContext::encode_image_as_jpeg2000(const std::shared_ptr(color_profile); */ - auto target_nclx_profile = std::make_shared(); - target_nclx_profile->set_from_heif_color_profile_nclx(options.output_nclx_profile); + auto target_nclx_profile = compute_target_nclx_profile(image, options.output_nclx_profile); if (encoder->plugin->plugin_api_version >= 2) { encoder->plugin->query_input_colorspace2(encoder->encoder, &colorspace, &chroma); diff --git a/libheif/nclx.cc b/libheif/nclx.cc index 46d0bdf4546..22d99a849af 100644 --- a/libheif/nclx.cc +++ b/libheif/nclx.cc @@ -312,9 +312,9 @@ void color_profile_nclx::free_nclx_color_profile(struct heif_color_profile_nclx* void color_profile_nclx::set_default() { - m_colour_primaries = 2; - m_transfer_characteristics = 2; - m_matrix_coefficients = 6; + m_colour_primaries = 1; + m_transfer_characteristics = 13; + m_matrix_coefficients = 1; m_full_range_flag = true; } @@ -339,6 +339,22 @@ void color_profile_nclx::set_from_heif_color_profile_nclx(const struct heif_colo } +void color_profile_nclx::replace_undefined_values_with_defaults() +{ + if (m_matrix_coefficients == heif_matrix_coefficients_unspecified) { + m_matrix_coefficients = heif_matrix_coefficients_ITU_R_BT_709_5; + } + + if (m_colour_primaries == heif_color_primaries_unspecified) { + m_colour_primaries = heif_color_primaries_ITU_R_BT_709_5; + } + + if (m_transfer_characteristics == heif_color_primaries_unspecified) { + m_transfer_characteristics = heif_transfer_characteristic_IEC_61966_2_1; + } +} + + Error Box_colr::parse(BitstreamRange& range) { StreamReader::grow_status status; diff --git a/libheif/nclx.h b/libheif/nclx.h index 9d6bb666bb8..12c0448057b 100644 --- a/libheif/nclx.h +++ b/libheif/nclx.h @@ -159,6 +159,8 @@ class color_profile_nclx : public color_profile void set_from_heif_color_profile_nclx(const struct heif_color_profile_nclx* nclx); + void replace_undefined_values_with_defaults(); + private: uint16_t m_colour_primaries = heif_color_primaries_unspecified; uint16_t m_transfer_characteristics = heif_transfer_characteristic_unspecified;