Accessor for ClosedCaptionsDialog.
[dcpomatic.git] / src / wx / colour_conversion_editor.cc
1 /*
2     Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "wx_util.h"
22 #include "static_text.h"
23 #include "colour_conversion_editor.h"
24 #include "check_box.h"
25 #include "lib/colour_conversion.h"
26 #include <dcp/locale_convert.h>
27 #include <dcp/gamma_transfer_function.h>
28 #include <dcp/identity_transfer_function.h>
29 #include <dcp/s_gamut3_transfer_function.h>
30 #include <dcp/modified_gamma_transfer_function.h>
31 #include <wx/spinctrl.h>
32 #include <wx/gbsizer.h>
33 #include <iostream>
34
35 using std::string;
36 using std::cout;
37 using boost::shared_ptr;
38 using boost::dynamic_pointer_cast;
39 using boost::bind;
40 using dcp::locale_convert;
41
42 int const ColourConversionEditor::INPUT_GAMMA = 0;
43 int const ColourConversionEditor::INPUT_GAMMA_LINEARISED = 1;
44 int const ColourConversionEditor::INPUT_SGAMUT3 = 2;
45
46 ColourConversionEditor::ColourConversionEditor (wxWindow* parent, bool yuv)
47         : wxPanel (parent, wxID_ANY)
48         , _ignore_chromaticity_changed (false)
49 {
50         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
51         SetSizer (overall_sizer);
52
53         wxGridBagSizer* table = new wxGridBagSizer (DCPOMATIC_SIZER_Y_GAP - 3, DCPOMATIC_SIZER_X_GAP);
54         overall_sizer->Add (table, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
55
56         int r = 0;
57
58         subhead (table, this, _("Input gamma correction"), r);
59
60         add_label_to_sizer (table, this, _("Input transfer function"), true, wxGBPosition (r, 0));
61         _input = new wxChoice (this, wxID_ANY);
62         _input->Append (_("Simple gamma"));
63         _input->Append (_("Simple gamma, linearised for small values"));
64         _input->Append (_("S-Gamut3"));
65         table->Add (_input, wxGBPosition (r, 1), wxGBSpan (1, 2));
66         ++r;
67
68         add_label_to_sizer (table, this, _("Input gamma"), true, wxGBPosition (r, 0));
69         _input_gamma = new wxSpinCtrlDouble (this);
70         table->Add (_input_gamma, wxGBPosition (r, 1));
71         ++r;
72
73         add_label_to_sizer (table, this, _("Input power"), true, wxGBPosition (r, 0));
74         {
75                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
76                 _input_power = new wxSpinCtrlDouble (this);
77                 s->Add (_input_power, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
78                 add_label_to_sizer (s, this, _("threshold"), true);
79                 _input_threshold = new wxTextCtrl (this, wxID_ANY, wxT (""));
80                 s->Add (_input_threshold, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
81                 add_label_to_sizer (s, this, _("A"), true);
82                 _input_A = new wxTextCtrl (this, wxID_ANY, wxT (""));
83                 s->Add (_input_A, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
84                 add_label_to_sizer (s, this, _("B"), true);
85                 _input_B = new wxTextCtrl (this, wxID_ANY, wxT (""));
86                 s->Add (_input_B, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
87                 table->Add (s, wxGBPosition (r, 1), wxGBSpan (1, 3));
88         }
89         ++r;
90
91         wxClientDC dc (parent);
92         wxSize size = dc.GetTextExtent (wxT ("-0.12345678901"));
93         size.SetHeight (-1);
94
95         wxTextValidator validator (wxFILTER_INCLUDE_CHAR_LIST);
96         wxArrayString list;
97
98         wxString n (wxT ("0123456789.-"));
99         for (size_t i = 0; i < n.Length(); ++i) {
100                 list.Add (n[i]);
101         }
102
103         validator.SetIncludes (list);
104
105
106         /* YUV to RGB conversion */
107
108         wxStaticText* yuv_heading = subhead (table, this, _("YUV to RGB conversion"), r);
109
110         wxStaticText* yuv_label = add_label_to_sizer (table, this, _("YUV to RGB matrix"), true, wxGBPosition (r, 0));
111         _yuv_to_rgb = new wxChoice (this, wxID_ANY);
112         _yuv_to_rgb->Append (_("Rec. 601"));
113         _yuv_to_rgb->Append (_("Rec. 709"));
114         table->Add (_yuv_to_rgb, wxGBPosition (r, 1));
115         ++r;
116
117         if (!yuv) {
118                 yuv_heading->Enable (false);
119                 yuv_label->Enable (false);
120                 _yuv_to_rgb->Enable (false);
121         }
122
123         /* RGB to XYZ conversion */
124
125         subhead (table, this, _("RGB to XYZ conversion"), r);
126
127         add_label_to_sizer (table, this, _("x"), false, wxGBPosition (r, 1));
128         add_label_to_sizer (table, this, _("y"), false, wxGBPosition (r, 2));
129         add_label_to_sizer (table, this, _("Matrix"), false, wxGBPosition (r, 3));
130         ++r;
131
132         add_label_to_sizer (table, this, _("Red chromaticity"), true, wxGBPosition (r, 0));
133         _red_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
134         table->Add (_red_x, wxGBPosition (r, 1));
135         _red_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
136         table->Add (_red_y, wxGBPosition (r, 2));
137         ++r;
138
139         add_label_to_sizer (table, this, _("Green chromaticity"), true, wxGBPosition (r, 0));
140         _green_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
141         table->Add (_green_x, wxGBPosition (r, 1));
142         _green_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
143         table->Add (_green_y, wxGBPosition (r, 2));
144         ++r;
145
146         add_label_to_sizer (table, this, _("Blue chromaticity"), true, wxGBPosition (r, 0));
147         _blue_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
148         table->Add (_blue_x, wxGBPosition (r, 1));
149         _blue_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
150         table->Add (_blue_y, wxGBPosition (r, 2));
151         ++r;
152
153         add_label_to_sizer (table, this, _("White point"), true, wxGBPosition (r, 0));
154         _white_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
155         table->Add (_white_x, wxGBPosition (r, 1));
156         _white_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
157         table->Add (_white_y, wxGBPosition (r, 2));
158         ++r;
159
160         size = dc.GetTextExtent (wxT ("0.12345678"));
161         size.SetHeight (-1);
162
163         wxFlexGridSizer* rgb_to_xyz_sizer = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
164         for (int i = 0; i < 3; ++i) {
165                 for (int j = 0; j < 3; ++j) {
166                         _rgb_to_xyz[i][j] = new StaticText (this, wxT (""), wxDefaultPosition, size, 0);
167                         rgb_to_xyz_sizer->Add (_rgb_to_xyz[i][j]);
168                 }
169         }
170         table->Add (rgb_to_xyz_sizer, wxGBPosition (r - 4, 3), wxGBSpan (4, 1));
171
172         /* White point adjustment */
173
174         size = dc.GetTextExtent (wxT ("-0.12345678901"));
175         size.SetHeight (-1);
176
177         subhead (table, this, _("White point adjustment"), r);
178
179         _adjust_white = new CheckBox (this, _("Adjust white point to"));
180         table->Add (_adjust_white, wxGBPosition (r, 0));
181         _adjusted_white_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
182         table->Add (_adjusted_white_x, wxGBPosition (r, 1));
183         _adjusted_white_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
184         table->Add (_adjusted_white_y, wxGBPosition (r, 2));
185         ++r;
186
187         add_label_to_sizer (table, this, wxT (""), false, wxGBPosition (r, 0));
188         ++r;
189
190         size = dc.GetTextExtent (wxT ("0.12345678"));
191         size.SetHeight (-1);
192
193         wxFlexGridSizer* bradford_sizer = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
194         for (int i = 0; i < 3; ++i) {
195                 for (int j = 0; j < 3; ++j) {
196                         _bradford[i][j] = new StaticText (this, wxT (""), wxDefaultPosition, size, 0);
197                         bradford_sizer->Add (_bradford[i][j]);
198                 }
199         }
200         table->Add (bradford_sizer, wxGBPosition (r - 2, 3), wxGBSpan (2, 1));
201         ++r;
202
203         /* Output transfer function */
204
205         subhead (table, this, _("Output gamma correction"), r);
206
207         _output = new CheckBox (this, _("Inverse 2.6 gamma correction on output"));
208         table->Add (_output, wxGBPosition (r, 0), wxGBSpan (1, 2));
209
210         _input_gamma->SetRange (0.1, 4.0);
211         _input_gamma->SetDigits (2);
212         _input_gamma->SetIncrement (0.1);
213         _input_power->SetRange (0.1, 4.0);
214         _input_power->SetDigits (6);
215         _input_power->SetIncrement (0.1);
216
217         _input->Bind (wxEVT_CHOICE, bind (&ColourConversionEditor::changed, this));
218         _input_gamma->Bind (wxEVT_SPINCTRLDOUBLE, bind (&ColourConversionEditor::changed, this, _input_gamma));
219         _input_power->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::changed, this));
220         _input_threshold->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::changed, this));
221         _input_A->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::changed, this));
222         _input_B->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::changed, this));
223         _red_x->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::chromaticity_changed, this));
224         _red_y->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::chromaticity_changed, this));
225         _green_x->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::chromaticity_changed, this));
226         _green_y->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::chromaticity_changed, this));
227         _blue_x->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::chromaticity_changed, this));
228         _blue_y->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::chromaticity_changed, this));
229         _white_x->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::chromaticity_changed, this));
230         _white_y->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::chromaticity_changed, this));
231         _adjust_white->Bind (wxEVT_CHECKBOX, bind (&ColourConversionEditor::adjusted_white_changed, this));
232         _adjusted_white_x->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::adjusted_white_changed, this));
233         _adjusted_white_y->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::adjusted_white_changed, this));
234         _yuv_to_rgb->Bind (wxEVT_CHOICE, bind (&ColourConversionEditor::changed, this));
235         _output->Bind (wxEVT_CHECKBOX, bind (&ColourConversionEditor::changed, this));
236 }
237
238 wxStaticText *
239 ColourConversionEditor::subhead (wxGridBagSizer* sizer, wxWindow* parent, wxString text, int& row) const
240 {
241         wxStaticText* m = new StaticText (parent, text);
242         wxFont font (*wxNORMAL_FONT);
243         font.SetWeight (wxFONTWEIGHT_BOLD);
244         m->SetFont (font);
245         sizer->Add (m, wxGBPosition (row, 0), wxGBSpan (1, 3), wxALIGN_CENTER_VERTICAL | wxTOP, 12);
246         ++row;
247         return m;
248 }
249
250 void
251 ColourConversionEditor::set (ColourConversion conversion)
252 {
253         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.in ())) {
254                 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.in ());
255                 checked_set (_input, 0);
256                 set_spin_ctrl (_input_gamma, tf->gamma ());
257         } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (conversion.in ())) {
258                 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (conversion.in ());
259                 checked_set (_input, 1);
260                 /* Arbitrary default; not used in this case (greyed out) */
261                 _input_gamma->SetValue (2.2);
262                 set_spin_ctrl (_input_power, tf->power ());
263                 set_text_ctrl (_input_threshold, tf->threshold ());
264                 set_text_ctrl (_input_A, tf->A ());
265                 set_text_ctrl (_input_B, tf->B ());
266         } else if (dynamic_pointer_cast<const dcp::SGamut3TransferFunction> (conversion.in ())) {
267                 checked_set (_input, 2);
268         }
269
270         _yuv_to_rgb->SetSelection (conversion.yuv_to_rgb ());
271
272         _ignore_chromaticity_changed = true;
273
274         char buffer[256];
275         snprintf (buffer, sizeof (buffer), "%.6f", conversion.red().x);
276         _red_x->SetValue (std_to_wx (buffer));
277         snprintf (buffer, sizeof (buffer), "%.6f", conversion.red().y);
278         _red_y->SetValue (std_to_wx (buffer));
279         snprintf (buffer, sizeof (buffer), "%.6f", conversion.green().x);
280         _green_x->SetValue (std_to_wx (buffer));
281         snprintf (buffer, sizeof (buffer), "%.6f", conversion.green().y);
282         _green_y->SetValue (std_to_wx (buffer));
283         snprintf (buffer, sizeof (buffer), "%.6f", conversion.blue().x);
284         _blue_x->SetValue (std_to_wx (buffer));
285         snprintf (buffer, sizeof (buffer), "%.6f", conversion.blue().y);
286         _blue_y->SetValue (std_to_wx (buffer));
287         snprintf (buffer, sizeof (buffer), "%.6f", conversion.white().x);
288         _white_x->SetValue (std_to_wx (buffer));
289         snprintf (buffer, sizeof (buffer), "%.6f", conversion.white().y);
290         _white_y->SetValue (std_to_wx (buffer));
291
292         _ignore_chromaticity_changed = false;
293
294         if (conversion.adjusted_white ()) {
295                 _adjust_white->SetValue (true);
296                 snprintf (buffer, sizeof (buffer), "%.6f", conversion.adjusted_white().get().x);
297                 _adjusted_white_x->SetValue (std_to_wx (buffer));
298                 snprintf (buffer, sizeof (buffer), "%.6f", conversion.adjusted_white().get().y);
299                 _adjusted_white_y->SetValue (std_to_wx (buffer));
300         } else {
301                 _adjust_white->SetValue (false);
302         }
303
304         _output->SetValue (static_cast<bool> (dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.out ())));
305
306         update_rgb_to_xyz ();
307         update_bradford ();
308         changed ();
309 }
310
311 ColourConversion
312 ColourConversionEditor::get () const
313 {
314         ColourConversion conversion;
315
316         switch (_input->GetSelection ()) {
317         case INPUT_GAMMA:
318                 conversion.set_in (
319                         shared_ptr<dcp::GammaTransferFunction> (new dcp::GammaTransferFunction (_input_gamma->GetValue ()))
320                         );
321                 break;
322         case INPUT_GAMMA_LINEARISED:
323                 /* Linearised gamma */
324                 conversion.set_in (
325                         shared_ptr<dcp::ModifiedGammaTransferFunction> (
326                                 new dcp::ModifiedGammaTransferFunction (
327                                         _input_power->GetValue (),
328                                         locale_convert<double> (wx_to_std (_input_threshold->GetValue ())),
329                                         locale_convert<double> (wx_to_std (_input_A->GetValue ())),
330                                         locale_convert<double> (wx_to_std (_input_B->GetValue ()))
331                                         )
332                                 )
333                         );
334                 break;
335         case INPUT_SGAMUT3:
336                 /* SGamut3 */
337                 conversion.set_in (shared_ptr<dcp::SGamut3TransferFunction> (new dcp::SGamut3TransferFunction ()));
338                 break;
339         }
340
341         conversion.set_yuv_to_rgb (static_cast<dcp::YUVToRGB> (_yuv_to_rgb->GetSelection ()));
342
343         conversion.set_red (
344                 dcp::Chromaticity (locale_convert<double> (wx_to_std (_red_x->GetValue ())), locale_convert<double> (wx_to_std (_red_y->GetValue ())))
345                 );
346         conversion.set_green (
347                 dcp::Chromaticity (locale_convert<double> (wx_to_std (_green_x->GetValue ())), locale_convert<double> (wx_to_std (_green_y->GetValue ())))
348                 );
349         conversion.set_blue (
350                 dcp::Chromaticity (locale_convert<double> (wx_to_std (_blue_x->GetValue ())), locale_convert<double> (wx_to_std (_blue_y->GetValue ())))
351                 );
352         conversion.set_white (
353                 dcp::Chromaticity (locale_convert<double> (wx_to_std (_white_x->GetValue ())), locale_convert<double> (wx_to_std (_white_y->GetValue ())))
354                 );
355
356         if (_adjust_white->GetValue ()) {
357                 conversion.set_adjusted_white (
358                         dcp::Chromaticity (
359                                 locale_convert<double> (wx_to_std (_adjusted_white_x->GetValue ())),
360                                 locale_convert<double> (wx_to_std (_adjusted_white_y->GetValue ()))
361                                 )
362                         );
363         } else {
364                 conversion.unset_adjusted_white ();
365         }
366
367         if (_output->GetValue ()) {
368                 conversion.set_out (shared_ptr<dcp::GammaTransferFunction> (new dcp::GammaTransferFunction (2.6)));
369         } else {
370                 conversion.set_out (shared_ptr<dcp::IdentityTransferFunction> (new dcp::IdentityTransferFunction ()));
371         }
372
373         return conversion;
374 }
375
376 void
377 ColourConversionEditor::changed ()
378 {
379         int const in = _input->GetSelection();
380         _input_gamma->Enable (in == INPUT_GAMMA);
381         _input_power->Enable (in == INPUT_GAMMA_LINEARISED);
382         _input_threshold->Enable (in == INPUT_GAMMA_LINEARISED);
383         _input_A->Enable (in == INPUT_GAMMA_LINEARISED);
384         _input_B->Enable (in == INPUT_GAMMA_LINEARISED);
385
386         Changed ();
387 }
388
389 void
390 ColourConversionEditor::chromaticity_changed ()
391 {
392         if (_ignore_chromaticity_changed) {
393                 return;
394         }
395
396         update_rgb_to_xyz ();
397         changed ();
398 }
399
400 void
401 ColourConversionEditor::adjusted_white_changed ()
402 {
403         update_bradford ();
404         changed ();
405 }
406
407 void
408 ColourConversionEditor::update_bradford ()
409 {
410         _adjusted_white_x->Enable (_adjust_white->GetValue ());
411         _adjusted_white_y->Enable (_adjust_white->GetValue ());
412
413         boost::numeric::ublas::matrix<double> m = get().bradford ();
414         for (int i = 0; i < 3; ++i) {
415                 for (int j = 0; j < 3; ++j) {
416                         char buffer[256];
417                         snprintf (buffer, sizeof (buffer), "%.7f", m (i, j));
418                         _bradford[i][j]->SetLabel (std_to_wx (buffer));
419                 }
420         }
421 }
422
423 void
424 ColourConversionEditor::update_rgb_to_xyz ()
425 {
426         boost::numeric::ublas::matrix<double> m = get().rgb_to_xyz ();
427         for (int i = 0; i < 3; ++i) {
428                 for (int j = 0; j < 3; ++j) {
429                         char buffer[256];
430                         snprintf (buffer, sizeof (buffer), "%.7f", m (i, j));
431                         _rgb_to_xyz[i][j]->SetLabel (std_to_wx (buffer));
432                 }
433         }
434 }
435
436 void
437 ColourConversionEditor::changed (wxSpinCtrlDouble* sc)
438 {
439         /* On OS X, it seems that in some cases when a wxSpinCtrlDouble loses focus
440            it emits an erroneous changed signal, which messes things up.
441            Check for that here.
442         */
443         if (fabs (_last_spin_ctrl_value[sc] - sc->GetValue()) < 1e-3) {
444                 return;
445         }
446
447         Changed ();
448 }
449
450 void
451 ColourConversionEditor::set_spin_ctrl (wxSpinCtrlDouble* control, double value)
452 {
453         _last_spin_ctrl_value[control] = value;
454         control->SetValue (value);
455 }
456
457 void
458 ColourConversionEditor::set_text_ctrl (wxTextCtrl* control, double value)
459 {
460         char buffer[256];
461         snprintf (buffer, sizeof (buffer), "%.7f", value);
462         control->SetValue (std_to_wx (buffer));
463 }