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