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