Various libdcp API changes.
[dcpomatic.git] / src / wx / colour_conversion_editor.cc
1 /*
2     Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "lib/colour_conversion.h"
21 #include "lib/safe_stringstream.h"
22 #include "wx_util.h"
23 #include "colour_conversion_editor.h"
24 #include <dcp/gamma_transfer_function.h>
25 #include <dcp/modified_gamma_transfer_function.h>
26 #include <dcp/raw_convert.h>
27 #include <wx/spinctrl.h>
28 #include <wx/gbsizer.h>
29 #include <boost/lexical_cast.hpp>
30
31 using std::string;
32 using std::cout;
33 using boost::shared_ptr;
34 using boost::dynamic_pointer_cast;
35 using boost::lexical_cast;
36 using dcp::raw_convert;
37
38 ColourConversionEditor::ColourConversionEditor (wxWindow* parent)
39         : wxPanel (parent, wxID_ANY)
40 {
41         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
42         SetSizer (overall_sizer);
43
44         wxGridBagSizer* table = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
45         overall_sizer->Add (table, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
46
47         int r = 0;
48
49         _input_gamma_linearised = new wxCheckBox (this, wxID_ANY, _("Linearise input gamma curve for low values"));
50         table->Add (_input_gamma_linearised, wxGBPosition (r, 0), wxGBSpan (1, 2));
51         ++r;
52
53         add_label_to_grid_bag_sizer (table, this, _("Input gamma"), true, wxGBPosition (r, 0));
54         _input_gamma = new wxSpinCtrlDouble (this);
55         table->Add (_input_gamma, wxGBPosition (r, 1));
56         ++r;
57
58         add_label_to_grid_bag_sizer (table, this, _("Input power"), true, wxGBPosition (r, 0));
59         _input_power = new wxSpinCtrlDouble (this);
60         table->Add (_input_power, wxGBPosition (r, 1));
61         ++r;
62
63         add_label_to_grid_bag_sizer (table, this, _("Input threshold"), true, wxGBPosition (r, 0));
64         _input_threshold = new wxTextCtrl (this, wxID_ANY, wxT (""));
65         table->Add (_input_threshold, wxGBPosition (r, 1));
66         ++r;
67
68         add_label_to_grid_bag_sizer (table, this, _("Input A value"), true, wxGBPosition (r, 0));
69         _input_A = new wxTextCtrl (this, wxID_ANY, wxT (""));
70         table->Add (_input_A, wxGBPosition (r, 1));
71         ++r;
72
73         add_label_to_grid_bag_sizer (table, this, _("Input B value"), true, wxGBPosition (r, 0));
74         _input_B = new wxTextCtrl (this, wxID_ANY, wxT (""));
75         table->Add (_input_B, wxGBPosition (r, 1));
76         ++r;
77         
78         wxClientDC dc (parent);
79         wxSize size = dc.GetTextExtent (wxT ("-0.12345678901"));
80         size.SetHeight (-1);
81
82         wxTextValidator validator (wxFILTER_INCLUDE_CHAR_LIST);
83         wxArrayString list;
84
85         wxString n (wxT ("0123456789.-"));
86         for (size_t i = 0; i < n.Length(); ++i) {
87                 list.Add (n[i]);
88         }
89
90         validator.SetIncludes (list);
91
92         add_label_to_grid_bag_sizer (table, this, _("Matrix"), true, wxGBPosition (r, 0));
93         wxFlexGridSizer* matrix_sizer = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
94         for (int i = 0; i < 3; ++i) {
95                 for (int j = 0; j < 3; ++j) {
96                         _matrix[i][j] = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
97                         matrix_sizer->Add (_matrix[i][j]);
98                 }
99         }
100         table->Add (matrix_sizer, wxGBPosition (r, 1));
101         ++r;
102
103         add_label_to_grid_bag_sizer (table, this, _("Output gamma"), true, wxGBPosition (r, 0));
104         wxBoxSizer* output_sizer = new wxBoxSizer (wxHORIZONTAL);
105         /// TRANSLATORS: this means the mathematical reciprocal operation, i.e. we are dividing 1 by the control that
106         /// comes after it.
107         add_label_to_sizer (output_sizer, this, _("1 / "), false);
108         _output_gamma = new wxSpinCtrlDouble (this);
109         output_sizer->Add (_output_gamma);
110         table->Add (output_sizer, wxGBPosition (r, 1));
111         ++r;
112
113         _input_gamma->SetRange (0.1, 4.0);
114         _input_gamma->SetDigits (2);
115         _input_gamma->SetIncrement (0.1);
116         _input_power->SetRange (0.1, 4.0);
117         _input_power->SetDigits (2);
118         _input_power->SetIncrement (0.1);
119         _output_gamma->SetRange (0.1, 4.0);
120         _output_gamma->SetDigits (2);
121         _output_gamma->SetIncrement (0.1);
122
123         _input_gamma->Bind (wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, boost::bind (&ColourConversionEditor::changed, this, _input_gamma));
124         _input_gamma_linearised->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&ColourConversionEditor::changed, this));
125         _input_power->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
126         _input_threshold->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
127         _input_A->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
128         _input_B->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
129         for (int i = 0; i < 3; ++i) {
130                 for (int j = 0; j < 3; ++j) {
131                         _matrix[i][j]->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
132                 }
133         }
134         _output_gamma->Bind (wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, boost::bind (&ColourConversionEditor::changed, this, _output_gamma));
135 }
136
137 void
138 ColourConversionEditor::set (ColourConversion conversion)
139 {
140         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.in ())) {
141                 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.in ());
142                 _input_gamma_linearised->SetValue (false);
143                 set_spin_ctrl (_input_gamma, tf->gamma ());
144         } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (conversion.in ())) {
145                 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (conversion.in ());
146                 /* Arbitrary default */
147                 _input_gamma->SetValue (2.2);
148                 _input_gamma_linearised->SetValue (true);
149                 set_spin_ctrl (_input_power, tf->power ());
150                 set_text_ctrl (_input_threshold, tf->threshold ());
151                 set_text_ctrl (_input_A, tf->A ());
152                 set_text_ctrl (_input_B, tf->B ());
153         }
154
155         boost::numeric::ublas::matrix<double> matrix = conversion.matrix ();
156         for (int i = 0; i < 3; ++i) {
157                 for (int j = 0; j < 3; ++j) {
158                         set_text_ctrl (_matrix[i][j], matrix(i, j));
159                 }
160         }
161         
162         set_spin_ctrl (_output_gamma, dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.out ())->gamma ());
163 }
164
165 ColourConversion
166 ColourConversionEditor::get () const
167 {
168         ColourConversion conversion;
169
170         if (_input_gamma_linearised->GetValue ()) {
171                 conversion.set_in (
172                         shared_ptr<dcp::ModifiedGammaTransferFunction> (
173                                 new dcp::ModifiedGammaTransferFunction (
174                                         false,
175                                         _input_power->GetValue (),
176                                         raw_convert<double> (wx_to_std (_input_threshold->GetValue ())),
177                                         raw_convert<double> (wx_to_std (_input_A->GetValue ())),
178                                         raw_convert<double> (wx_to_std (_input_B->GetValue ()))
179                                         )
180                                 )
181                         );
182         } else {
183                 conversion.set_in (
184                         shared_ptr<dcp::GammaTransferFunction> (
185                                 new dcp::GammaTransferFunction (
186                                         false,
187                                         _input_gamma->GetValue ()
188                                         )
189                                 )
190                         );
191         }
192
193         boost::numeric::ublas::matrix<double> matrix (3, 3);
194         for (int i = 0; i < 3; ++i) {
195                 for (int j = 0; j < 3; ++j) {
196                         string const v = wx_to_std (_matrix[i][j]->GetValue ());
197                         if (v.empty ()) {
198                                 matrix (i, j) = 0;
199                         } else {
200                                 matrix (i, j) = raw_convert<double> (v);
201                         }
202                 }
203         }
204
205         conversion.set_matrix (matrix);
206
207         conversion.set_out (shared_ptr<dcp::GammaTransferFunction> (new dcp::GammaTransferFunction (true, _output_gamma->GetValue ())));
208
209         return conversion;
210 }
211
212 void
213 ColourConversionEditor::changed ()
214 {
215         bool const lin = _input_gamma_linearised->GetValue ();
216         _input_gamma->Enable (!lin);
217         _input_power->Enable (lin);
218         _input_threshold->Enable (lin);
219         _input_A->Enable (lin);
220         _input_B->Enable (lin);
221         
222         Changed ();
223 }
224
225 void
226 ColourConversionEditor::changed (wxSpinCtrlDouble* sc)
227 {
228         /* On OS X, it seems that in some cases when a wxSpinCtrlDouble loses focus
229            it emits an erroneous changed signal, which messes things up.
230            Check for that here.
231         */
232         if (fabs (_last_spin_ctrl_value[sc] - sc->GetValue()) < 1e-3) {
233                 return;
234         }
235         
236         Changed ();
237 }
238
239 void
240 ColourConversionEditor::set_spin_ctrl (wxSpinCtrlDouble* control, double value)
241 {
242         _last_spin_ctrl_value[control] = value;
243         control->SetValue (value);
244 }
245
246 void
247 ColourConversionEditor::set_text_ctrl (wxTextCtrl* control, double value)
248 {
249         SafeStringStream s;
250         s.precision (7);
251         s << value;
252         control->SetValue (std_to_wx (s.str ()));
253 }