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