Remove user-configurable colour conversion presets.
[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 "lib/raw_convert.h"
23 #include "wx_util.h"
24 #include "colour_conversion_editor.h"
25 #include <dcp/gamma_transfer_function.h>
26 #include <dcp/modified_gamma_transfer_function.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
37 ColourConversionEditor::ColourConversionEditor (wxWindow* parent)
38         : wxPanel (parent, wxID_ANY)
39 {
40         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
41         SetSizer (overall_sizer);
42
43         wxGridBagSizer* table = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
44         overall_sizer->Add (table, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
45
46         int r = 0;
47
48         subhead (table, this, _("Input gamma correction"), r);
49
50         _input_gamma_linearised = new wxCheckBox (this, wxID_ANY, _("Linearise input gamma curve for low values"));
51         table->Add (_input_gamma_linearised, wxGBPosition (r, 0), wxGBSpan (1, 2));
52         ++r;
53
54         add_label_to_grid_bag_sizer (table, this, _("Input gamma"), true, wxGBPosition (r, 0));
55         _input_gamma = new wxSpinCtrlDouble (this);
56         table->Add (_input_gamma, wxGBPosition (r, 1));
57         ++r;
58
59         add_label_to_grid_bag_sizer (table, this, _("Input power"), true, wxGBPosition (r, 0));
60         _input_power = new wxSpinCtrlDouble (this);
61         table->Add (_input_power, wxGBPosition (r, 1));
62         ++r;
63
64         add_label_to_grid_bag_sizer (table, this, _("Input threshold"), true, wxGBPosition (r, 0));
65         _input_threshold = new wxTextCtrl (this, wxID_ANY, wxT (""));
66         table->Add (_input_threshold, wxGBPosition (r, 1));
67         ++r;
68
69         add_label_to_grid_bag_sizer (table, this, _("Input A value"), true, wxGBPosition (r, 0));
70         _input_A = new wxTextCtrl (this, wxID_ANY, wxT (""));
71         table->Add (_input_A, wxGBPosition (r, 1));
72         ++r;
73
74         add_label_to_grid_bag_sizer (table, this, _("Input B value"), true, wxGBPosition (r, 0));
75         _input_B = new wxTextCtrl (this, wxID_ANY, wxT (""));
76         table->Add (_input_B, wxGBPosition (r, 1));
77         ++r;
78         
79         wxClientDC dc (parent);
80         wxSize size = dc.GetTextExtent (wxT ("-0.12345678901"));
81         size.SetHeight (-1);
82
83         wxTextValidator validator (wxFILTER_INCLUDE_CHAR_LIST);
84         wxArrayString list;
85
86         wxString n (wxT ("0123456789.-"));
87         for (size_t i = 0; i < n.Length(); ++i) {
88                 list.Add (n[i]);
89         }
90
91         validator.SetIncludes (list);
92
93
94         /* YUV to RGB conversion */
95
96         subhead (table, this, _("YUV to RGB conversion"), r);
97         
98         add_label_to_grid_bag_sizer (table, this, _("YUV to RGB matrix"), true, wxGBPosition (r, 0));
99         _yuv_to_rgb = new wxChoice (this, wxID_ANY);
100         _yuv_to_rgb->Append (_("Rec. 601"));
101         _yuv_to_rgb->Append (_("Rec. 709"));
102         table->Add (_yuv_to_rgb, wxGBPosition (r, 1));
103         ++r;
104
105         /* RGB to XYZ conversion */
106
107         subhead (table, this, _("RGB to XYZ conversion"), r);
108
109         add_label_to_grid_bag_sizer (table, this, _("x"), false, wxGBPosition (r, 1));
110         add_label_to_grid_bag_sizer (table, this, _("y"), false, wxGBPosition (r, 2));
111         add_label_to_grid_bag_sizer (table, this, _("Matrix"), false, wxGBPosition (r, 3));
112         ++r;
113
114         add_label_to_grid_bag_sizer (table, this, _("Red chromaticity"), true, wxGBPosition (r, 0));
115         _red_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
116         table->Add (_red_x, wxGBPosition (r, 1));
117         _red_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
118         table->Add (_red_y, wxGBPosition (r, 2));
119         ++r;
120
121         add_label_to_grid_bag_sizer (table, this, _("Green chromaticity"), true, wxGBPosition (r, 0));
122         _green_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
123         table->Add (_green_x, wxGBPosition (r, 1));
124         _green_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
125         table->Add (_green_y, wxGBPosition (r, 2));
126         ++r;
127
128         add_label_to_grid_bag_sizer (table, this, _("Blue chromaticity"), true, wxGBPosition (r, 0));
129         _blue_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
130         table->Add (_blue_x, wxGBPosition (r, 1));
131         _blue_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
132         table->Add (_blue_y, wxGBPosition (r, 2));
133         ++r;
134
135         add_label_to_grid_bag_sizer (table, this, _("White point"), true, wxGBPosition (r, 0));
136         _white_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
137         table->Add (_white_x, wxGBPosition (r, 1));
138         _white_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
139         table->Add (_white_y, wxGBPosition (r, 2));
140         ++r;
141
142         size = dc.GetTextExtent (wxT ("0.12345678"));
143         size.SetHeight (-1);
144         
145         wxFlexGridSizer* rgb_to_xyz_sizer = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
146         for (int i = 0; i < 3; ++i) {
147                 for (int j = 0; j < 3; ++j) {
148                         _rgb_to_xyz[i][j] = new wxStaticText (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0);
149                         rgb_to_xyz_sizer->Add (_rgb_to_xyz[i][j]);
150                 }
151         }
152         table->Add (rgb_to_xyz_sizer, wxGBPosition (r - 4, 3), wxGBSpan (4, 1));
153
154         /* White point adjustment */
155
156         size = dc.GetTextExtent (wxT ("-0.12345678901"));
157         size.SetHeight (-1);
158
159         subhead (table, this, _("White point adjustment"), r);
160
161         _adjust_white = new wxCheckBox (this, wxID_ANY, _("Adjust white point to"));
162         table->Add (_adjust_white, wxGBPosition (r, 0));
163         _adjusted_white_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
164         table->Add (_adjusted_white_x, wxGBPosition (r, 1));
165         _adjusted_white_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
166         table->Add (_adjusted_white_y, wxGBPosition (r, 2));
167         ++r;
168
169         add_label_to_grid_bag_sizer (table, this, wxT (""), false, wxGBPosition (r, 0));
170         ++r;
171
172         size = dc.GetTextExtent (wxT ("0.12345678"));
173         size.SetHeight (-1);
174         
175         wxFlexGridSizer* bradford_sizer = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
176         for (int i = 0; i < 3; ++i) {
177                 for (int j = 0; j < 3; ++j) {
178                         _bradford[i][j] = new wxStaticText (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0);
179                         bradford_sizer->Add (_bradford[i][j]);
180                 }
181         }
182         table->Add (bradford_sizer, wxGBPosition (r - 2, 3), wxGBSpan (2, 1));
183
184         subhead (table, this, _("Output gamma correction"), r);
185         
186         add_label_to_grid_bag_sizer (table, this, _("Output gamma"), true, wxGBPosition (r, 0));
187         wxBoxSizer* output_sizer = new wxBoxSizer (wxHORIZONTAL);
188         /// TRANSLATORS: this means the mathematical reciprocal operation, i.e. we are dividing 1 by the control that
189         /// comes after it.
190         add_label_to_sizer (output_sizer, this, _("1 / "), false);
191         _output_gamma = new wxSpinCtrlDouble (this);
192         output_sizer->Add (_output_gamma);
193         table->Add (output_sizer, wxGBPosition (r, 1), wxGBSpan (1, 2));
194         ++r;
195
196         _input_gamma->SetRange (0.1, 4.0);
197         _input_gamma->SetDigits (2);
198         _input_gamma->SetIncrement (0.1);
199         _input_power->SetRange (0.1, 4.0);
200         _input_power->SetDigits (6);
201         _input_power->SetIncrement (0.1);
202         _output_gamma->SetRange (0.1, 4.0);
203         _output_gamma->SetDigits (2);
204         _output_gamma->SetIncrement (0.1);
205
206         _input_gamma->Bind (wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, boost::bind (&ColourConversionEditor::changed, this, _input_gamma));
207         _input_gamma_linearised->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&ColourConversionEditor::changed, this));
208         _input_power->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
209         _input_threshold->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
210         _input_A->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
211         _input_B->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
212         _red_x->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
213         _red_y->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
214         _green_x->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
215         _green_y->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
216         _blue_x->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
217         _blue_y->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
218         _white_x->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
219         _white_y->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
220         _adjust_white->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&ColourConversionEditor::adjusted_white_changed, this));
221         _adjusted_white_x->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::adjusted_white_changed, this));
222         _adjusted_white_y->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::adjusted_white_changed, this));
223         _yuv_to_rgb->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&ColourConversionEditor::changed, this));
224         _output_gamma->Bind (wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, boost::bind (&ColourConversionEditor::changed, this, _output_gamma));
225 }
226
227 void
228 ColourConversionEditor::subhead (wxGridBagSizer* sizer, wxWindow* parent, wxString text, int& row) const
229 {
230         wxStaticText* m = new wxStaticText (parent, wxID_ANY, wxT (""));
231         m->SetLabelMarkup ("<b>" + text + "</b>");
232         sizer->Add (m, wxGBPosition (row, 0), wxGBSpan (1, 3), wxALIGN_CENTER_VERTICAL | wxTOP, 12);
233         ++row;
234 }
235
236 void
237 ColourConversionEditor::set (ColourConversion conversion)
238 {
239         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.in ())) {
240                 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.in ());
241                 _input_gamma_linearised->SetValue (false);
242                 set_spin_ctrl (_input_gamma, tf->gamma ());
243         } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (conversion.in ())) {
244                 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (conversion.in ());
245                 /* Arbitrary default; not used in this case (greyed out) */
246                 _input_gamma->SetValue (2.2);
247                 _input_gamma_linearised->SetValue (true);
248                 set_spin_ctrl (_input_power, tf->power ());
249                 set_text_ctrl (_input_threshold, tf->threshold ());
250                 set_text_ctrl (_input_A, tf->A ());
251                 set_text_ctrl (_input_B, tf->B ());
252         }
253
254         _yuv_to_rgb->SetSelection (conversion.yuv_to_rgb ());
255
256         SafeStringStream s;
257         s.setf (std::ios::fixed, std::ios::floatfield);
258         s.precision (6);
259
260         s << conversion.red().x;
261         _red_x->SetValue (std_to_wx (s.str ()));
262
263         s.str ("");
264         s << conversion.red().y;
265         _red_y->SetValue (std_to_wx (s.str ()));
266
267         s.str ("");
268         s << conversion.green().x;
269         _green_x->SetValue (std_to_wx (s.str ()));
270
271         s.str ("");
272         s << conversion.green().y;
273         _green_y->SetValue (std_to_wx (s.str ()));
274
275         s.str ("");
276         s << conversion.blue().x;
277         _blue_x->SetValue (std_to_wx (s.str ()));
278
279         s.str ("");
280         s << conversion.blue().y;
281         _blue_y->SetValue (std_to_wx (s.str ()));
282
283         s.str ("");
284         s << conversion.white().x;
285         _white_x->SetValue (std_to_wx (s.str ()));
286
287         s.str ("");
288         s << conversion.white().y;
289         _white_y->SetValue (std_to_wx (s.str ()));
290
291         if (conversion.adjusted_white ()) {
292                 _adjust_white->SetValue (true);
293                 s.str ("");
294                 s << conversion.adjusted_white().get().x;
295                 _adjusted_white_x->SetValue (std_to_wx (s.str ()));
296                 s.str ("");
297                 s << conversion.adjusted_white().get().y;
298                 _adjusted_white_y->SetValue (std_to_wx (s.str ()));
299         } else {
300                 _adjust_white->SetValue (false);
301         }
302
303         update_rgb_to_xyz ();
304         update_bradford ();
305         
306         set_spin_ctrl (_output_gamma, dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.out ())->gamma ());
307 }
308
309 ColourConversion
310 ColourConversionEditor::get () const
311 {
312         ColourConversion conversion;
313
314         if (_input_gamma_linearised->GetValue ()) {
315                 conversion.set_in (
316                         shared_ptr<dcp::ModifiedGammaTransferFunction> (
317                                 new dcp::ModifiedGammaTransferFunction (
318                                         _input_power->GetValue (),
319                                         raw_convert<double> (wx_to_std (_input_threshold->GetValue ())),
320                                         raw_convert<double> (wx_to_std (_input_A->GetValue ())),
321                                         raw_convert<double> (wx_to_std (_input_B->GetValue ()))
322                                         )
323                                 )
324                         );
325         } else {
326                 conversion.set_in (
327                         shared_ptr<dcp::GammaTransferFunction> (new dcp::GammaTransferFunction (_input_gamma->GetValue ()))
328                         );
329         }
330
331         conversion.set_yuv_to_rgb (static_cast<dcp::YUVToRGB> (_yuv_to_rgb->GetSelection ()));
332
333         conversion.set_red (
334                 dcp::Chromaticity (raw_convert<double> (wx_to_std (_red_x->GetValue ())), raw_convert<double> (wx_to_std (_red_y->GetValue ())))
335                 );
336         conversion.set_green (
337                 dcp::Chromaticity (raw_convert<double> (wx_to_std (_green_x->GetValue ())), raw_convert<double> (wx_to_std (_green_y->GetValue ())))
338                 );
339         conversion.set_blue (
340                 dcp::Chromaticity (raw_convert<double> (wx_to_std (_blue_x->GetValue ())), raw_convert<double> (wx_to_std (_blue_y->GetValue ())))
341                 );
342         conversion.set_white (
343                 dcp::Chromaticity (raw_convert<double> (wx_to_std (_white_x->GetValue ())), raw_convert<double> (wx_to_std (_white_y->GetValue ())))
344                 );
345
346         if (_adjust_white->GetValue ()) {
347                 conversion.set_adjusted_white (
348                         dcp::Chromaticity (
349                                 raw_convert<double> (wx_to_std (_adjusted_white_x->GetValue ())),
350                                 raw_convert<double> (wx_to_std (_adjusted_white_y->GetValue ()))
351                                 )
352                         );
353         } else {
354                 conversion.unset_adjusted_white ();
355         }
356
357         conversion.set_out (shared_ptr<dcp::GammaTransferFunction> (new dcp::GammaTransferFunction (_output_gamma->GetValue ())));
358
359         return conversion;
360 }
361
362 void
363 ColourConversionEditor::changed ()
364 {
365         bool const lin = _input_gamma_linearised->GetValue ();
366         _input_gamma->Enable (!lin);
367         _input_power->Enable (lin);
368         _input_threshold->Enable (lin);
369         _input_A->Enable (lin);
370         _input_B->Enable (lin);
371         
372         Changed ();
373 }
374
375 void
376 ColourConversionEditor::chromaticity_changed ()
377 {
378         update_rgb_to_xyz ();
379         changed ();
380 }
381
382 void
383 ColourConversionEditor::adjusted_white_changed ()
384 {
385         update_bradford ();
386         changed ();
387 }
388
389 void
390 ColourConversionEditor::update_bradford ()
391 {
392         _adjusted_white_x->Enable (_adjust_white->GetValue ());
393         _adjusted_white_y->Enable (_adjust_white->GetValue ());
394         
395         boost::numeric::ublas::matrix<double> m = get().bradford ();
396         for (int i = 0; i < 3; ++i) {
397                 for (int j = 0; j < 3; ++j) {
398                         SafeStringStream s;
399                         s.setf (std::ios::fixed, std::ios::floatfield);
400                         s.precision (7);
401                         s << m (i, j);
402                         _bradford[i][j]->SetLabel (std_to_wx (s.str ()));
403                 }
404         }
405 }
406
407 void
408 ColourConversionEditor::update_rgb_to_xyz ()
409 {
410         boost::numeric::ublas::matrix<double> m = get().rgb_to_xyz ();
411         for (int i = 0; i < 3; ++i) {
412                 for (int j = 0; j < 3; ++j) {
413                         SafeStringStream s;
414                         s.setf (std::ios::fixed, std::ios::floatfield);
415                         s.precision (7);
416                         s << m (i, j);
417                         _rgb_to_xyz[i][j]->SetLabel (std_to_wx (s.str ()));
418                 }
419         }
420 }
421
422 void
423 ColourConversionEditor::changed (wxSpinCtrlDouble* sc)
424 {
425         /* On OS X, it seems that in some cases when a wxSpinCtrlDouble loses focus
426            it emits an erroneous changed signal, which messes things up.
427            Check for that here.
428         */
429         if (fabs (_last_spin_ctrl_value[sc] - sc->GetValue()) < 1e-3) {
430                 return;
431         }
432         
433         Changed ();
434 }
435
436 void
437 ColourConversionEditor::set_spin_ctrl (wxSpinCtrlDouble* control, double value)
438 {
439         _last_spin_ctrl_value[control] = value;
440         control->SetValue (value);
441 }
442
443 void
444 ColourConversionEditor::set_text_ctrl (wxTextCtrl* control, double value)
445 {
446         SafeStringStream s;
447         s.precision (7);
448         s << value;
449         control->SetValue (std_to_wx (s.str ()));
450 }