X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fcolour_conversion.cc;h=f1e6258122fbf3cebb7638be85e38ecedb4f8705;hb=refs%2Fheads%2Fno-add-child;hp=31d4cc41a6dc3f33836015a9d87b3a68a0a39ab2;hpb=e60bb3e51bd1508b149e6b8f6608f09b5196ae26;p=dcpomatic.git diff --git a/src/lib/colour_conversion.cc b/src/lib/colour_conversion.cc index 31d4cc41a..f1e625812 100644 --- a/src/lib/colour_conversion.cc +++ b/src/lib/colour_conversion.cc @@ -1,47 +1,58 @@ /* - Copyright (C) 2013-2015 Carl Hetherington + Copyright (C) 2013-2021 Carl Hetherington - This program is free software; you can redistribute it and/or modify + 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. - This program is distributed in the hope that it will be useful, + 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 this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with DCP-o-matic. If not, see . */ -#include "config.h" + #include "colour_conversion.h" +#include "config.h" +#include "digester.h" #include "util.h" -#include "md5_digester.h" -#include "raw_convert.h" #include -#include #include +#include #include +#include +#include +#include #include +LIBDCP_DISABLE_WARNINGS #include -#include +LIBDCP_ENABLE_WARNINGS +#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::cout; using std::vector; -using boost::shared_ptr; using boost::optional; -using boost::dynamic_pointer_cast; +using dcp::raw_convert; + vector PresetColourConversion::_presets; + ColourConversion::ColourConversion () : dcp::ColourConversion (dcp::ColourConversion::srgb_to_xyz ()) { @@ -62,17 +73,19 @@ ColourConversion::ColourConversion (cxml::NodePtr node, int version) /* Version 2.x */ - cxml::ConstNodePtr in_node = node->node_child ("InputTransferFunction"); - string in_type = in_node->string_child ("Type"); + auto in_node = node->node_child ("InputTransferFunction"); + auto in_type = in_node->string_child ("Type"); if (in_type == "Gamma") { - _in.reset (new dcp::GammaTransferFunction (in_node->number_child ("Gamma"))); + _in = make_shared(in_node->number_child ("Gamma")); } else if (in_type == "ModifiedGamma") { - _in.reset (new dcp::ModifiedGammaTransferFunction ( - in_node->number_child ("Power"), - in_node->number_child ("Threshold"), - in_node->number_child ("A"), - in_node->number_child ("B") - )); + _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 { @@ -80,22 +93,22 @@ ColourConversion::ColourConversion (cxml::NodePtr node, int version) /* Version 1.x */ if (node->bool_child ("InputGammaLinearised")) { - _in.reset (new dcp::ModifiedGammaTransferFunction (node->number_child ("InputGamma"), 0.04045, 0.055, 12.92)); + _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"))); + _in.reset (new dcp::GammaTransferFunction (node->number_child ("InputGamma"))); } } - _yuv_to_rgb = static_cast (node->optional_number_child("YUVToRGB").get_value_or (dcp::YUV_TO_RGB_REC601)); + _yuv_to_rgb = static_cast(node->optional_number_child("YUVToRGB").get_value_or(static_cast(dcp::YUVToRGB::REC601))); - list m = node->node_children ("Matrix"); + 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 (list::iterator i = m.begin(); i != m.end(); ++i) { - int const ti = (*i)->number_attribute ("i"); - int const tj = (*i)->number_attribute ("j"); - C(ti, tj) = raw_convert ((*i)->content ()); + 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); @@ -119,7 +132,12 @@ ColourConversion::ColourConversion (cxml::NodePtr node, int version) } } - _out.reset (new dcp::GammaTransferFunction (node->number_child ("OutputGamma"))); + auto gamma = node->optional_number_child("OutputGamma"); + if (gamma) { + _out = make_shared(node->number_child("OutputGamma")); + } else { + _out = make_shared(); + } } boost::optional @@ -133,49 +151,54 @@ ColourConversion::from_xml (cxml::NodePtr node, int version) } void -ColourConversion::as_xml (xmlpp::Node* node) const +ColourConversion::as_xml(xmlpp::Element* element) const { - xmlpp::Node* in_node = node->add_child ("InputTransferFunction"); + auto in_node = cxml::add_child(element, "InputTransferFunction"); if (dynamic_pointer_cast (_in)) { - shared_ptr tf = dynamic_pointer_cast (_in); - in_node->add_child("Type")->add_child_text ("Gamma"); - in_node->add_child("Gamma")->add_child_text (raw_convert (tf->gamma ())); + auto tf = dynamic_pointer_cast (_in); + cxml::add_text_child(in_node, "Type", "Gamma"); + cxml::add_text_child(in_node, "Gamma", raw_convert(tf->gamma())); } else if (dynamic_pointer_cast (_in)) { - shared_ptr tf = dynamic_pointer_cast (_in); - in_node->add_child("Type")->add_child_text ("ModifiedGamma"); - in_node->add_child("Power")->add_child_text (raw_convert (tf->power ())); - in_node->add_child("Threshold")->add_child_text (raw_convert (tf->threshold ())); - in_node->add_child("A")->add_child_text (raw_convert (tf->A ())); - in_node->add_child("B")->add_child_text (raw_convert (tf->B ())); + auto tf = dynamic_pointer_cast (_in); + cxml::add_text_child(in_node, "Type", "ModifiedGamma"); + cxml::add_text_child(in_node, "Power", raw_convert(tf->power ())); + cxml::add_text_child(in_node, "Threshold", raw_convert(tf->threshold ())); + cxml::add_text_child(in_node, "A", raw_convert(tf->A())); + cxml::add_text_child(in_node, "B", raw_convert(tf->B())); + } else if (dynamic_pointer_cast(_in)) { + cxml::add_text_child(in_node, "Type", "SGamut3"); } - node->add_child("RedX")->add_child_text (raw_convert (_red.x)); - node->add_child("RedY")->add_child_text (raw_convert (_red.y)); - node->add_child("GreenX")->add_child_text (raw_convert (_green.x)); - node->add_child("GreenY")->add_child_text (raw_convert (_green.y)); - node->add_child("BlueX")->add_child_text (raw_convert (_blue.x)); - node->add_child("BlueY")->add_child_text (raw_convert (_blue.y)); - node->add_child("WhiteX")->add_child_text (raw_convert (_white.x)); - node->add_child("WhiteY")->add_child_text (raw_convert (_white.y)); + cxml::add_text_child(element, "YUVToRGB", raw_convert(static_cast(_yuv_to_rgb))); + cxml::add_text_child(element, "RedX", raw_convert(_red.x)); + cxml::add_text_child(element, "RedY", raw_convert(_red.y)); + cxml::add_text_child(element, "GreenX", raw_convert(_green.x)); + cxml::add_text_child(element, "GreenY", raw_convert(_green.y)); + cxml::add_text_child(element, "BlueX", raw_convert(_blue.x)); + cxml::add_text_child(element, "BlueY", raw_convert(_blue.y)); + cxml::add_text_child(element, "WhiteX", raw_convert(_white.x)); + cxml::add_text_child(element, "WhiteY", raw_convert(_white.y)); if (_adjusted_white) { - node->add_child("AdjustedWhiteX")->add_child_text (raw_convert (_adjusted_white.get().x)); - node->add_child("AdjustedWhiteY")->add_child_text (raw_convert (_adjusted_white.get().y)); + cxml::add_text_child(element, "AdjustedWhiteX", raw_convert(_adjusted_white.get().x)); + cxml::add_text_child(element, "AdjustedWhiteY", raw_convert(_adjusted_white.get().y)); } - node->add_child("OutputGamma")->add_child_text (raw_convert (dynamic_pointer_cast (_out)->gamma ())); + if (auto gf = dynamic_pointer_cast(_out)) { + cxml::add_text_child(element, "OutputGamma", raw_convert(gf->gamma())); + } } optional ColourConversion::preset () const { - vector presets = PresetColourConversion::all (); + auto presets = PresetColourConversion::all (); size_t i = 0; - while (i < presets.size() && (presets[i].conversion != *this)) { + while (i < presets.size() && presets[i].conversion != *this) { ++i; } if (i >= presets.size ()) { - return optional (); + return {}; } return i; @@ -184,7 +207,7 @@ ColourConversion::preset () const string ColourConversion::identifier () const { - MD5Digester digester; + Digester digester; if (dynamic_pointer_cast (_in)) { shared_ptr tf = dynamic_pointer_cast (_in); @@ -211,7 +234,12 @@ ColourConversion::identifier () const digester.add (_adjusted_white.get().y); } - digester.add (dynamic_pointer_cast (_out)->gamma ()); + digester.add(static_cast(_yuv_to_rgb)); + + auto gf = dynamic_pointer_cast (_out); + if (gf) { + digester.add (gf->gamma ()); + } return digester.get (); } @@ -262,12 +290,15 @@ PresetColourConversion::setup_colour_conversion_presets () _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"), "p3", dcp::ColourConversion::p3_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) { - BOOST_FOREACH (PresetColourConversion const& i, _presets) { + for (auto const& i: _presets) { if (i.id == s) { return i; } @@ -275,4 +306,3 @@ PresetColourConversion::from_id (string s) DCPOMATIC_ASSERT (false); } -