Optimise opening of colour conversion editor slightly.
[dcpomatic.git] / src / wx / colour_conversion_editor.cc
1 /*
2     Copyright (C) 2013-2015 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 #include <iostream>
31
32 using std::string;
33 using std::cout;
34 using boost::shared_ptr;
35 using boost::dynamic_pointer_cast;
36 using boost::lexical_cast;
37
38 ColourConversionEditor::ColourConversionEditor (wxWindow* parent, bool yuv)
39         : wxPanel (parent, wxID_ANY)
40         , _ignore_chromaticity_changed (false)
41 {
42         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
43         SetSizer (overall_sizer);
44
45         wxGridBagSizer* table = new wxGridBagSizer (DCPOMATIC_SIZER_Y_GAP - 3, DCPOMATIC_SIZER_X_GAP);
46         overall_sizer->Add (table, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
47
48         int r = 0;
49
50         subhead (table, this, _("Input gamma correction"), r);
51
52         _input_gamma_linearised = new wxCheckBox (this, wxID_ANY, _("Linearise input gamma curve for small values"));
53         table->Add (_input_gamma_linearised, wxGBPosition (r, 0), wxGBSpan (1, 2));
54         ++r;
55
56         add_label_to_sizer (table, this, _("Input gamma"), true, wxGBPosition (r, 0));
57         _input_gamma = new wxSpinCtrlDouble (this);
58         table->Add (_input_gamma, wxGBPosition (r, 1));
59         ++r;
60
61         add_label_to_sizer (table, this, _("Input power"), true, wxGBPosition (r, 0));
62         {
63                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
64                 _input_power = new wxSpinCtrlDouble (this);
65                 s->Add (_input_power, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
66                 add_label_to_sizer (s, this, _("threshold"), true);
67                 _input_threshold = new wxTextCtrl (this, wxID_ANY, wxT (""));
68                 s->Add (_input_threshold, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
69                 add_label_to_sizer (s, this, _("A"), true);
70                 _input_A = new wxTextCtrl (this, wxID_ANY, wxT (""));
71                 s->Add (_input_A, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
72                 add_label_to_sizer (s, this, _("B"), true);
73                 _input_B = new wxTextCtrl (this, wxID_ANY, wxT (""));
74                 s->Add (_input_B, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
75                 table->Add (s, wxGBPosition (r, 1), wxGBSpan (1, 3));
76         }
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         wxStaticText* yuv_heading = subhead (table, this, _("YUV to RGB conversion"), r);
97
98         wxStaticText* yuv_label = add_label_to_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         if (!yuv) {
106                 yuv_heading->Enable (false);
107                 yuv_label->Enable (false);
108                 _yuv_to_rgb->Enable (false);
109         }
110
111         /* RGB to XYZ conversion */
112
113         subhead (table, this, _("RGB to XYZ conversion"), r);
114
115         add_label_to_sizer (table, this, _("x"), false, wxGBPosition (r, 1));
116         add_label_to_sizer (table, this, _("y"), false, wxGBPosition (r, 2));
117         add_label_to_sizer (table, this, _("Matrix"), false, wxGBPosition (r, 3));
118         ++r;
119
120         add_label_to_sizer (table, this, _("Red chromaticity"), true, wxGBPosition (r, 0));
121         _red_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
122         table->Add (_red_x, wxGBPosition (r, 1));
123         _red_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
124         table->Add (_red_y, wxGBPosition (r, 2));
125         ++r;
126
127         add_label_to_sizer (table, this, _("Green chromaticity"), true, wxGBPosition (r, 0));
128         _green_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
129         table->Add (_green_x, wxGBPosition (r, 1));
130         _green_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
131         table->Add (_green_y, wxGBPosition (r, 2));
132         ++r;
133
134         add_label_to_sizer (table, this, _("Blue chromaticity"), true, wxGBPosition (r, 0));
135         _blue_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
136         table->Add (_blue_x, wxGBPosition (r, 1));
137         _blue_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
138         table->Add (_blue_y, wxGBPosition (r, 2));
139         ++r;
140
141         add_label_to_sizer (table, this, _("White point"), true, wxGBPosition (r, 0));
142         _white_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
143         table->Add (_white_x, wxGBPosition (r, 1));
144         _white_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
145         table->Add (_white_y, wxGBPosition (r, 2));
146         ++r;
147
148         size = dc.GetTextExtent (wxT ("0.12345678"));
149         size.SetHeight (-1);
150
151         wxFlexGridSizer* rgb_to_xyz_sizer = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
152         for (int i = 0; i < 3; ++i) {
153                 for (int j = 0; j < 3; ++j) {
154                         _rgb_to_xyz[i][j] = new wxStaticText (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0);
155                         rgb_to_xyz_sizer->Add (_rgb_to_xyz[i][j]);
156                 }
157         }
158         table->Add (rgb_to_xyz_sizer, wxGBPosition (r - 4, 3), wxGBSpan (4, 1));
159
160         /* White point adjustment */
161
162         size = dc.GetTextExtent (wxT ("-0.12345678901"));
163         size.SetHeight (-1);
164
165         subhead (table, this, _("White point adjustment"), r);
166
167         _adjust_white = new wxCheckBox (this, wxID_ANY, _("Adjust white point to"));
168         table->Add (_adjust_white, wxGBPosition (r, 0));
169         _adjusted_white_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
170         table->Add (_adjusted_white_x, wxGBPosition (r, 1));
171         _adjusted_white_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
172         table->Add (_adjusted_white_y, wxGBPosition (r, 2));
173         ++r;
174
175         add_label_to_sizer (table, this, wxT (""), false, wxGBPosition (r, 0));
176         ++r;
177
178         size = dc.GetTextExtent (wxT ("0.12345678"));
179         size.SetHeight (-1);
180
181         wxFlexGridSizer* bradford_sizer = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
182         for (int i = 0; i < 3; ++i) {
183                 for (int j = 0; j < 3; ++j) {
184                         _bradford[i][j] = new wxStaticText (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0);
185                         bradford_sizer->Add (_bradford[i][j]);
186                 }
187         }
188         table->Add (bradford_sizer, wxGBPosition (r - 2, 3), wxGBSpan (2, 1));
189
190         _input_gamma->SetRange (0.1, 4.0);
191         _input_gamma->SetDigits (2);
192         _input_gamma->SetIncrement (0.1);
193         _input_power->SetRange (0.1, 4.0);
194         _input_power->SetDigits (6);
195         _input_power->SetIncrement (0.1);
196
197         _input_gamma->Bind (wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, boost::bind (&ColourConversionEditor::changed, this, _input_gamma));
198         _input_gamma_linearised->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&ColourConversionEditor::changed, this));
199         _input_power->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
200         _input_threshold->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
201         _input_A->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
202         _input_B->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
203         _red_x->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
204         _red_y->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
205         _green_x->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
206         _green_y->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
207         _blue_x->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
208         _blue_y->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
209         _white_x->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
210         _white_y->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
211         _adjust_white->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&ColourConversionEditor::adjusted_white_changed, this));
212         _adjusted_white_x->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::adjusted_white_changed, this));
213         _adjusted_white_y->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::adjusted_white_changed, this));
214         _yuv_to_rgb->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&ColourConversionEditor::changed, this));
215 }
216
217 wxStaticText *
218 ColourConversionEditor::subhead (wxGridBagSizer* sizer, wxWindow* parent, wxString text, int& row) const
219 {
220         wxStaticText* m = new wxStaticText (parent, wxID_ANY, text);
221         wxFont font (*wxNORMAL_FONT);
222         font.SetWeight (wxFONTWEIGHT_BOLD);
223         m->SetFont (font);
224         sizer->Add (m, wxGBPosition (row, 0), wxGBSpan (1, 3), wxALIGN_CENTER_VERTICAL | wxTOP, 12);
225         ++row;
226         return m;
227 }
228
229 void
230 ColourConversionEditor::set (ColourConversion conversion)
231 {
232         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.in ())) {
233                 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.in ());
234                 _input_gamma_linearised->SetValue (false);
235                 set_spin_ctrl (_input_gamma, tf->gamma ());
236         } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (conversion.in ())) {
237                 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (conversion.in ());
238                 /* Arbitrary default; not used in this case (greyed out) */
239                 _input_gamma->SetValue (2.2);
240                 _input_gamma_linearised->SetValue (true);
241                 set_spin_ctrl (_input_power, tf->power ());
242                 set_text_ctrl (_input_threshold, tf->threshold ());
243                 set_text_ctrl (_input_A, tf->A ());
244                 set_text_ctrl (_input_B, tf->B ());
245         }
246
247         _yuv_to_rgb->SetSelection (conversion.yuv_to_rgb ());
248
249         _ignore_chromaticity_changed = true;
250
251         SafeStringStream s;
252         s.setf (std::ios::fixed, std::ios::floatfield);
253         s.precision (6);
254
255         s << conversion.red().x;
256         _red_x->SetValue (std_to_wx (s.str ()));
257
258         s.str ("");
259         s << conversion.red().y;
260         _red_y->SetValue (std_to_wx (s.str ()));
261
262         s.str ("");
263         s << conversion.green().x;
264         _green_x->SetValue (std_to_wx (s.str ()));
265
266         s.str ("");
267         s << conversion.green().y;
268         _green_y->SetValue (std_to_wx (s.str ()));
269
270         s.str ("");
271         s << conversion.blue().x;
272         _blue_x->SetValue (std_to_wx (s.str ()));
273
274         s.str ("");
275         s << conversion.blue().y;
276         _blue_y->SetValue (std_to_wx (s.str ()));
277
278         s.str ("");
279         s << conversion.white().x;
280         _white_x->SetValue (std_to_wx (s.str ()));
281
282         s.str ("");
283         s << conversion.white().y;
284         _white_y->SetValue (std_to_wx (s.str ()));
285
286         _ignore_chromaticity_changed = false;
287
288         if (conversion.adjusted_white ()) {
289                 _adjust_white->SetValue (true);
290                 s.str ("");
291                 s << conversion.adjusted_white().get().x;
292                 _adjusted_white_x->SetValue (std_to_wx (s.str ()));
293                 s.str ("");
294                 s << conversion.adjusted_white().get().y;
295                 _adjusted_white_y->SetValue (std_to_wx (s.str ()));
296         } else {
297                 _adjust_white->SetValue (false);
298         }
299
300         update_rgb_to_xyz ();
301         update_bradford ();
302         changed ();
303 }
304
305 ColourConversion
306 ColourConversionEditor::get () const
307 {
308         ColourConversion conversion;
309
310         if (_input_gamma_linearised->GetValue ()) {
311                 conversion.set_in (
312                         shared_ptr<dcp::ModifiedGammaTransferFunction> (
313                                 new dcp::ModifiedGammaTransferFunction (
314                                         _input_power->GetValue (),
315                                         raw_convert<double> (wx_to_std (_input_threshold->GetValue ())),
316                                         raw_convert<double> (wx_to_std (_input_A->GetValue ())),
317                                         raw_convert<double> (wx_to_std (_input_B->GetValue ()))
318                                         )
319                                 )
320                         );
321         } else {
322                 conversion.set_in (
323                         shared_ptr<dcp::GammaTransferFunction> (new dcp::GammaTransferFunction (_input_gamma->GetValue ()))
324                         );
325         }
326
327         conversion.set_yuv_to_rgb (static_cast<dcp::YUVToRGB> (_yuv_to_rgb->GetSelection ()));
328
329         conversion.set_red (
330                 dcp::Chromaticity (raw_convert<double> (wx_to_std (_red_x->GetValue ())), raw_convert<double> (wx_to_std (_red_y->GetValue ())))
331                 );
332         conversion.set_green (
333                 dcp::Chromaticity (raw_convert<double> (wx_to_std (_green_x->GetValue ())), raw_convert<double> (wx_to_std (_green_y->GetValue ())))
334                 );
335         conversion.set_blue (
336                 dcp::Chromaticity (raw_convert<double> (wx_to_std (_blue_x->GetValue ())), raw_convert<double> (wx_to_std (_blue_y->GetValue ())))
337                 );
338         conversion.set_white (
339                 dcp::Chromaticity (raw_convert<double> (wx_to_std (_white_x->GetValue ())), raw_convert<double> (wx_to_std (_white_y->GetValue ())))
340                 );
341
342         if (_adjust_white->GetValue ()) {
343                 conversion.set_adjusted_white (
344                         dcp::Chromaticity (
345                                 raw_convert<double> (wx_to_std (_adjusted_white_x->GetValue ())),
346                                 raw_convert<double> (wx_to_std (_adjusted_white_y->GetValue ()))
347                                 )
348                         );
349         } else {
350                 conversion.unset_adjusted_white ();
351         }
352
353         conversion.set_out (shared_ptr<dcp::GammaTransferFunction> (new dcp::GammaTransferFunction (2.6)));
354
355         return conversion;
356 }
357
358 void
359 ColourConversionEditor::changed ()
360 {
361         bool const lin = _input_gamma_linearised->GetValue ();
362         _input_gamma->Enable (!lin);
363         _input_power->Enable (lin);
364         _input_threshold->Enable (lin);
365         _input_A->Enable (lin);
366         _input_B->Enable (lin);
367
368         Changed ();
369 }
370
371 void
372 ColourConversionEditor::chromaticity_changed ()
373 {
374         if (_ignore_chromaticity_changed) {
375                 return;
376         }
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 }