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