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