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