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