Skip to content

Commit

Permalink
alternative implementation of PR #1015
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Oct 30, 2023
1 parent 8cacd4d commit 127c1ca
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 deletions.
6 changes: 3 additions & 3 deletions examples/heif_enc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 1 addition & 13 deletions libheif/color-conversion/colorconversion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -520,19 +520,7 @@ std::shared_ptr<HeifPixelImage> convert_colorspace(const std::shared_ptr<HeifPix
input_state.nclx_profile = *input->get_color_profile_nclx();
}

// If some input nclx values are unspecified, use values that match sRGB 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_709_5);
}

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_709_5);
}

if (input_state.nclx_profile.get_transfer_characteristics() == heif_transfer_characteristic_unspecified) {
input_state.nclx_profile.set_transfer_characteristics(heif_transfer_characteristic_IEC_61966_2_1);
}
input_state.nclx_profile.replace_undefined_values_with_defaults();

std::set<enum heif_channel> channels = input->get_channel_set();
assert(!channels.empty());
Expand Down
32 changes: 26 additions & 6 deletions libheif/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2429,6 +2429,29 @@ static bool nclx_profile_matches_spec(heif_colorspace colorspace,
}


static std::shared_ptr<color_profile_nclx> compute_target_nclx_profile(const std::shared_ptr<HeifPixelImage>& image, const heif_color_profile_nclx* output_nclx_profile)
{
auto target_nclx_profile = std::make_shared<color_profile_nclx>();

// 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<HeifPixelImage>& image,
struct heif_encoder* encoder,
const struct heif_encoding_options& options,
Expand All @@ -2444,8 +2467,7 @@ Error HeifContext::encode_image_as_hevc(const std::shared_ptr<HeifPixelImage>& i
heif_colorspace colorspace = image->get_colorspace();
heif_chroma chroma = image->get_chroma_format();

auto target_nclx_profile = std::make_shared<color_profile_nclx>();
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);
Expand Down Expand Up @@ -2652,8 +2674,7 @@ Error HeifContext::encode_image_as_av1(const std::shared_ptr<HeifPixelImage>& im
heif_colorspace colorspace = image->get_colorspace();
heif_chroma chroma = image->get_chroma_format();

auto target_nclx_profile = std::make_shared<color_profile_nclx>();
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);
Expand Down Expand Up @@ -2828,8 +2849,7 @@ Error HeifContext::encode_image_as_jpeg2000(const std::shared_ptr<HeifPixelImage
auto nclx_profile = std::dynamic_pointer_cast<const color_profile_nclx>(color_profile);
*/

auto target_nclx_profile = std::make_shared<color_profile_nclx>();
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);
Expand Down
16 changes: 16 additions & 0 deletions libheif/nclx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions libheif/nclx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 127c1ca

Please sign in to comment.