/* Copyright (C) 2013-2021 Carl Hetherington This file is part of DCP-o-matic. DCP-o-matic is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. DCP-o-matic is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with DCP-o-matic. If not, see . */ #include "colour_conversion.h" #include "config.h" #include "digester.h" #include "util.h" #include #include #include #include #include #include #include #include LIBDCP_DISABLE_WARNINGS #include LIBDCP_ENABLE_WARNINGS #include #include #include "i18n.h" using std::cout; using std::dynamic_pointer_cast; using std::list; using std::make_shared; using std::shared_ptr; using std::string; using std::vector; using boost::optional; using dcp::raw_convert; vector PresetColourConversion::_presets; ColourConversion::ColourConversion() : dcp::ColourConversion(dcp::ColourConversion::srgb_to_xyz()) { } ColourConversion::ColourConversion(dcp::ColourConversion conversion_) : dcp::ColourConversion(conversion_) { } ColourConversion::ColourConversion(cxml::NodePtr node, int version) { shared_ptr in; if (version >= 32) { /* Version 2.x */ auto in_node = node->node_child("InputTransferFunction"); auto in_type = in_node->string_child("Type"); if (in_type == "Gamma") { _in = make_shared(in_node->number_child("Gamma")); } else if (in_type == "ModifiedGamma") { _in = make_shared( in_node->number_child("Power"), in_node->number_child("Threshold"), in_node->number_child("A"), in_node->number_child("B") ); } else if (in_type == "SGamut3") { _in = make_shared(); } } else { /* Version 1.x */ if (node->bool_child("InputGammaLinearised")) { _in.reset(new dcp::ModifiedGammaTransferFunction(node->number_child("InputGamma"), 0.04045, 0.055, 12.92)); } else { _in.reset(new dcp::GammaTransferFunction(node->number_child("InputGamma"))); } } _yuv_to_rgb = static_cast(node->optional_number_child("YUVToRGB").get_value_or(static_cast(dcp::YUVToRGB::REC601))); auto m = node->node_children("Matrix"); if (!m.empty()) { /* Read in old nodes and convert them to chromaticities */ boost::numeric::ublas::matrix C(3, 3); for (auto i: m) { int const ti = i->number_attribute("i"); int const tj = i->number_attribute("j"); C(ti, tj) = raw_convert(i->content()); } double const rd = C(0, 0) + C(1, 0) + C(2, 0); _red = dcp::Chromaticity(C(0, 0) / rd, C(1, 0) / rd); double const gd = C(0, 1) + C(1, 1) + C(2, 1); _green = dcp::Chromaticity(C(0, 1) / gd, C(1, 1) / gd); double const bd = C(0, 2) + C(1, 2) + C(2, 2); _blue = dcp::Chromaticity(C(0, 2) / bd, C(1, 2) / bd); double const wd = C(0, 0) + C(0, 1) + C(0, 2) + C(1, 0) + C(1, 1) + C(1, 2) + C(2, 0) + C(2, 1) + C(2, 2); _white = dcp::Chromaticity((C(0, 0) + C(0, 1) + C(0, 2)) / wd,(C(1, 0) + C(1, 1) + C(1, 2)) / wd); } else { /* New-style chromaticities */ _red = dcp::Chromaticity(node->number_child("RedX"), node->number_child("RedY")); _green = dcp::Chromaticity(node->number_child("GreenX"), node->number_child("GreenY")); _blue = dcp::Chromaticity(node->number_child("BlueX"), node->number_child("BlueY")); _white = dcp::Chromaticity(node->number_child("WhiteX"), node->number_child("WhiteY")); if (node->optional_node_child("AdjustedWhiteX")) { _adjusted_white = dcp::Chromaticity( node->number_child("AdjustedWhiteX"), node->number_child("AdjustedWhiteY") ); } } if (auto gamma = node->optional_number_child("OutputGamma")) { _out = make_shared(node->number_child("OutputGamma")); } else { _out = make_shared(); } } boost::optional ColourConversion::from_xml(cxml::NodePtr node, int version) { if (!node->optional_node_child("InputTransferFunction")) { return boost::optional(); } return ColourConversion(node, version); } void ColourConversion::as_xml(xmlpp::Element* element) const { auto in_node = cxml::add_child(element, "InputTransferFunction"); if (dynamic_pointer_cast(_in)) { auto tf = dynamic_pointer_cast(_in); cxml::add_text_child(in_node, "Type", "Gamma"); cxml::add_text_child(in_node, "Gamma", fmt::to_string(tf->gamma())); } else if (dynamic_pointer_cast(_in)) { auto tf = dynamic_pointer_cast(_in); cxml::add_text_child(in_node, "Type", "ModifiedGamma"); cxml::add_text_child(in_node, "Power", fmt::to_string(tf->power())); cxml::add_text_child(in_node, "Threshold", fmt::to_string(tf->threshold())); cxml::add_text_child(in_node, "A", fmt::to_string(tf->A())); cxml::add_text_child(in_node, "B", fmt::to_string(tf->B())); } else if (dynamic_pointer_cast(_in)) { cxml::add_text_child(in_node, "Type", "SGamut3"); } cxml::add_text_child(element, "YUVToRGB", fmt::to_string(static_cast(_yuv_to_rgb))); cxml::add_text_child(element, "RedX", fmt::to_string(_red.x)); cxml::add_text_child(element, "RedY", fmt::to_string(_red.y)); cxml::add_text_child(element, "GreenX", fmt::to_string(_green.x)); cxml::add_text_child(element, "GreenY", fmt::to_string(_green.y)); cxml::add_text_child(element, "BlueX", fmt::to_string(_blue.x)); cxml::add_text_child(element, "BlueY", fmt::to_string(_blue.y)); cxml::add_text_child(element, "WhiteX", fmt::to_string(_white.x)); cxml::add_text_child(element, "WhiteY", fmt::to_string(_white.y)); if (_adjusted_white) { cxml::add_text_child(element, "AdjustedWhiteX", fmt::to_string(_adjusted_white.get().x)); cxml::add_text_child(element, "AdjustedWhiteY", fmt::to_string(_adjusted_white.get().y)); } if (auto gf = dynamic_pointer_cast(_out)) { cxml::add_text_child(element, "OutputGamma", fmt::to_string(gf->gamma())); } } optional ColourConversion::preset() const { auto presets = PresetColourConversion::all(); size_t i = 0; while (i < presets.size() && presets[i].conversion != *this) { ++i; } if (i >= presets.size()) { return {}; } return i; } string ColourConversion::identifier() const { Digester digester; if (dynamic_pointer_cast(_in)) { auto tf = dynamic_pointer_cast(_in); digester.add(tf->gamma()); } else if (dynamic_pointer_cast(_in)) { shared_ptr tf = dynamic_pointer_cast(_in); digester.add(tf->power()); digester.add(tf->threshold()); digester.add(tf->A()); digester.add(tf->B()); } digester.add(_red.x); digester.add(_red.y); digester.add(_green.x); digester.add(_green.y); digester.add(_blue.x); digester.add(_blue.y); digester.add(_white.x); digester.add(_white.y); if (_adjusted_white) { digester.add(_adjusted_white.get().x); digester.add(_adjusted_white.get().y); } digester.add(static_cast(_yuv_to_rgb)); if (auto gf = dynamic_pointer_cast(_out)) { digester.add(gf->gamma()); } return digester.get(); } PresetColourConversion::PresetColourConversion() : name(_("Untitled")) { } PresetColourConversion::PresetColourConversion(string n, string i, dcp::ColourConversion conversion_) : conversion(conversion_) , name(n) , id(i) { } PresetColourConversion::PresetColourConversion(cxml::NodePtr node, int version) : conversion(node, version) , name(node->string_child("Name")) { } bool operator==(ColourConversion const & a, ColourConversion const & b) { return a.about_equal(b, 1e-6); } bool operator!=(ColourConversion const & a, ColourConversion const & b) { return !(a == b); } bool operator==(PresetColourConversion const & a, PresetColourConversion const & b) { return a.name == b.name && a.conversion == b.conversion; } void PresetColourConversion::setup_colour_conversion_presets() { _presets.push_back(PresetColourConversion(_("sRGB"), "srgb", dcp::ColourConversion::srgb_to_xyz())); _presets.push_back(PresetColourConversion(_("Rec. 601"), "rec601", dcp::ColourConversion::rec601_to_xyz())); _presets.push_back(PresetColourConversion(_("Rec. 709"), "rec709", dcp::ColourConversion::rec709_to_xyz())); _presets.push_back(PresetColourConversion(_("P3 DCI (~6300K)"), "p3", dcp::ColourConversion::p3_dci_to_xyz())); _presets.push_back(PresetColourConversion(_("P3 D65 (~6500K)"), "p3-d65", dcp::ColourConversion::p3_d65_to_xyz())); _presets.push_back(PresetColourConversion(_("P3 D60 (~6000K)"), "p3-d60", dcp::ColourConversion::p3_d60_to_xyz())); _presets.push_back(PresetColourConversion(_("Rec. 1886"), "rec1886", dcp::ColourConversion::rec1886_to_xyz())); _presets.push_back(PresetColourConversion(_("Rec. 2020"), "rec2020", dcp::ColourConversion::rec2020_to_xyz())); _presets.push_back(PresetColourConversion(_("S-Gamut3/S-Log3"), "sgamut3", dcp::ColourConversion::s_gamut3_to_xyz())); } PresetColourConversion PresetColourConversion::from_id(string s) { for (auto const& i: _presets) { if (i.id == s) { return i; } } DCPOMATIC_ASSERT(false); }