Extract common code out into kdm_for_screen()
[dcpomatic.git] / src / lib / colour_conversion.cc
index 3c076b030ed19ee7d483f130287b8e73c3303270..2e052060e2b247ad9f26dbd51c54399b952f9c91 100644 (file)
@@ -1,33 +1,37 @@
 /*
-    Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2013-2020 Carl Hetherington <cth@carlh.net>
 
-    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 <http://www.gnu.org/licenses/>.
 
 */
 
 #include "config.h"
 #include "colour_conversion.h"
 #include "util.h"
-#include "md5_digester.h"
-#include "raw_convert.h"
+#include "digester.h"
+#include <dcp/raw_convert.h>
 #include <dcp/chromaticity.h>
-#include <dcp/colour_matrix.h>
 #include <dcp/gamma_transfer_function.h>
 #include <dcp/modified_gamma_transfer_function.h>
+#include <dcp/identity_transfer_function.h>
+#include <dcp/s_gamut3_transfer_function.h>
 #include <libcxml/cxml.h>
 #include <libxml++/libxml++.h>
+#include <boost/foreach.hpp>
+#include <iostream>
 
 #include "i18n.h"
 
@@ -38,17 +42,20 @@ using std::vector;
 using boost::shared_ptr;
 using boost::optional;
 using boost::dynamic_pointer_cast;
+using dcp::raw_convert;
+
+vector<PresetColourConversion> 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)
@@ -70,21 +77,23 @@ ColourConversion::ColourConversion (cxml::NodePtr node, int version)
                                           in_node->number_child<double> ("A"),
                                           in_node->number_child<double> ("B")
                                           ));
+               } else if (in_type == "SGamut3") {
+                       _in.reset (new dcp::SGamut3TransferFunction ());
                }
 
        } else {
 
                /* Version 1.x */
-               
+
                if (node->bool_child ("InputGammaLinearised")) {
-                       _in.reset (new dcp::ModifiedGammaTransferFunction (node->number_child<float> ("InputGamma"), 0.04045, 0.055, 12.92));
+                       _in.reset (new dcp::ModifiedGammaTransferFunction (node->number_child<double> ("InputGamma"), 0.04045, 0.055, 12.92));
                } else {
-                       _in.reset (new dcp::GammaTransferFunction (node->number_child<float> ("InputGamma")));
+                       _in.reset (new dcp::GammaTransferFunction (node->number_child<double> ("InputGamma")));
                }
        }
 
        _yuv_to_rgb = static_cast<dcp::YUVToRGB> (node->optional_number_child<int>("YUVToRGB").get_value_or (dcp::YUV_TO_RGB_REC601));
-       
+
        list<cxml::NodePtr> m = node->node_children ("Matrix");
        if (!m.empty ()) {
                /* Read in old <Matrix> nodes and convert them to chromaticities */
@@ -114,9 +123,14 @@ ColourConversion::ColourConversion (cxml::NodePtr node, int version)
                                node->number_child<double> ("AdjustedWhiteX"), node->number_child<double> ("AdjustedWhiteY")
                                );
                }
-       }       
-       
-       _out.reset (new dcp::GammaTransferFunction (node->number_child<double> ("OutputGamma")));
+       }
+
+       optional<double> gamma = node->optional_number_child<double> ("OutputGamma");
+       if (gamma) {
+               _out.reset (new dcp::GammaTransferFunction (node->number_child<double> ("OutputGamma")));
+       } else {
+               _out.reset (new dcp::IdentityTransferFunction ());
+       }
 }
 
 boost::optional<ColourConversion>
@@ -144,8 +158,11 @@ ColourConversion::as_xml (xmlpp::Node* node) const
                in_node->add_child("Threshold")->add_child_text (raw_convert<string> (tf->threshold ()));
                in_node->add_child("A")->add_child_text (raw_convert<string> (tf->A ()));
                in_node->add_child("B")->add_child_text (raw_convert<string> (tf->B ()));
+       } else if (dynamic_pointer_cast<const dcp::SGamut3TransferFunction> (_in)) {
+               in_node->add_child("Type")->add_child_text ("SGamut3");
        }
 
+       node->add_child("YUVToRGB")->add_child_text (raw_convert<string> (static_cast<int> (_yuv_to_rgb)));
        node->add_child("RedX")->add_child_text (raw_convert<string> (_red.x));
        node->add_child("RedY")->add_child_text (raw_convert<string> (_red.y));
        node->add_child("GreenX")->add_child_text (raw_convert<string> (_green.x));
@@ -159,15 +176,18 @@ ColourConversion::as_xml (xmlpp::Node* node) const
                node->add_child("AdjustedWhiteY")->add_child_text (raw_convert<string> (_adjusted_white.get().y));
        }
 
-       node->add_child("OutputGamma")->add_child_text (raw_convert<string> (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out)->gamma ()));
+       if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out)) {
+               shared_ptr<const dcp::GammaTransferFunction> gf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out);
+               node->add_child("OutputGamma")->add_child_text (raw_convert<string> (gf->gamma ()));
+       }
 }
 
 optional<size_t>
 ColourConversion::preset () const
 {
-       vector<PresetColourConversion> presets = Config::instance()->colour_conversions ();
+       vector<PresetColourConversion> presets = PresetColourConversion::all ();
        size_t i = 0;
-       while (i < presets.size() && (presets[i].conversion != *this)) {
+       while (i < presets.size() && presets[i].conversion != *this) {
                ++i;
        }
 
@@ -181,7 +201,7 @@ ColourConversion::preset () const
 string
 ColourConversion::identifier () const
 {
-       MD5Digester digester;
+       Digester digester;
 
        if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in)) {
                shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in);
@@ -208,8 +228,11 @@ ColourConversion::identifier () const
                digester.add (_adjusted_white.get().y);
        }
 
-       digester.add (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out)->gamma ());
-       
+       shared_ptr<const dcp::GammaTransferFunction> gf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out);
+       if (gf) {
+               digester.add (gf->gamma ());
+       }
+
        return digester.get ();
 }
 
@@ -219,9 +242,10 @@ PresetColourConversion::PresetColourConversion ()
 
 }
 
-PresetColourConversion::PresetColourConversion (string n, dcp::ColourConversion conversion_)
+PresetColourConversion::PresetColourConversion (string n, string i, dcp::ColourConversion conversion_)
        : conversion (conversion_)
        , name (n)
+       , id (i)
 {
 
 }
@@ -233,13 +257,6 @@ PresetColourConversion::PresetColourConversion (cxml::NodePtr node, int version)
 
 }
 
-void
-PresetColourConversion::as_xml (xmlpp::Node* node) const
-{
-       conversion.as_xml (node);
-       node->add_child("Name")->add_child_text (name);
-}
-
 bool
 operator== (ColourConversion const & a, ColourConversion const & b)
 {
@@ -257,3 +274,27 @@ 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"), "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) {
+               if (i.id == s) {
+                       return i;
+               }
+       }
+
+       DCPOMATIC_ASSERT (false);
+}