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