Remove output gamma configuration option after discussions with Dennis.
[dcpomatic.git] / src / wx / colour_conversion_editor.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "lib/colour_conversion.h"
21 #include "lib/safe_stringstream.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 <boost/lexical_cast.hpp>
30
31 using std::string;
32 using std::cout;
33 using boost::shared_ptr;
34 using boost::dynamic_pointer_cast;
35 using boost::lexical_cast;
36
37 ColourConversionEditor::ColourConversionEditor (wxWindow* parent)
38         : wxPanel (parent, wxID_ANY)
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_grid_bag_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_grid_bag_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         subhead (table, this, _("YUV to RGB conversion"), r);
95
96         add_label_to_grid_bag_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         /* RGB to XYZ conversion */
104
105         subhead (table, this, _("RGB to XYZ conversion"), r);
106
107         add_label_to_grid_bag_sizer (table, this, _("x"), false, wxGBPosition (r, 1));
108         add_label_to_grid_bag_sizer (table, this, _("y"), false, wxGBPosition (r, 2));
109         add_label_to_grid_bag_sizer (table, this, _("Matrix"), false, wxGBPosition (r, 3));
110         ++r;
111
112         add_label_to_grid_bag_sizer (table, this, _("Red chromaticity"), true, wxGBPosition (r, 0));
113         _red_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
114         table->Add (_red_x, wxGBPosition (r, 1));
115         _red_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
116         table->Add (_red_y, wxGBPosition (r, 2));
117         ++r;
118
119         add_label_to_grid_bag_sizer (table, this, _("Green chromaticity"), true, wxGBPosition (r, 0));
120         _green_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
121         table->Add (_green_x, wxGBPosition (r, 1));
122         _green_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
123         table->Add (_green_y, wxGBPosition (r, 2));
124         ++r;
125
126         add_label_to_grid_bag_sizer (table, this, _("Blue chromaticity"), true, wxGBPosition (r, 0));
127         _blue_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
128         table->Add (_blue_x, wxGBPosition (r, 1));
129         _blue_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
130         table->Add (_blue_y, wxGBPosition (r, 2));
131         ++r;
132
133         add_label_to_grid_bag_sizer (table, this, _("White point"), true, wxGBPosition (r, 0));
134         _white_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
135         table->Add (_white_x, wxGBPosition (r, 1));
136         _white_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
137         table->Add (_white_y, wxGBPosition (r, 2));
138         ++r;
139
140         size = dc.GetTextExtent (wxT ("0.12345678"));
141         size.SetHeight (-1);
142
143         wxFlexGridSizer* rgb_to_xyz_sizer = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
144         for (int i = 0; i < 3; ++i) {
145                 for (int j = 0; j < 3; ++j) {
146                         _rgb_to_xyz[i][j] = new wxStaticText (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0);
147                         rgb_to_xyz_sizer->Add (_rgb_to_xyz[i][j]);
148                 }
149         }
150         table->Add (rgb_to_xyz_sizer, wxGBPosition (r - 4, 3), wxGBSpan (4, 1));
151
152         /* White point adjustment */
153
154         size = dc.GetTextExtent (wxT ("-0.12345678901"));
155         size.SetHeight (-1);
156
157         subhead (table, this, _("White point adjustment"), r);
158
159         _adjust_white = new wxCheckBox (this, wxID_ANY, _("Adjust white point to"));
160         table->Add (_adjust_white, wxGBPosition (r, 0));
161         _adjusted_white_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
162         table->Add (_adjusted_white_x, wxGBPosition (r, 1));
163         _adjusted_white_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
164         table->Add (_adjusted_white_y, wxGBPosition (r, 2));
165         ++r;
166
167         add_label_to_grid_bag_sizer (table, this, wxT (""), false, wxGBPosition (r, 0));
168         ++r;
169
170         size = dc.GetTextExtent (wxT ("0.12345678"));
171         size.SetHeight (-1);
172
173         wxFlexGridSizer* bradford_sizer = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
174         for (int i = 0; i < 3; ++i) {
175                 for (int j = 0; j < 3; ++j) {
176                         _bradford[i][j] = new wxStaticText (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0);
177                         bradford_sizer->Add (_bradford[i][j]);
178                 }
179         }
180         table->Add (bradford_sizer, wxGBPosition (r - 2, 3), wxGBSpan (2, 1));
181
182         _input_gamma->SetRange (0.1, 4.0);
183         _input_gamma->SetDigits (2);
184         _input_gamma->SetIncrement (0.1);
185         _input_power->SetRange (0.1, 4.0);
186         _input_power->SetDigits (6);
187         _input_power->SetIncrement (0.1);
188
189         _input_gamma->Bind (wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, boost::bind (&ColourConversionEditor::changed, this, _input_gamma));
190         _input_gamma_linearised->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&ColourConversionEditor::changed, this));
191         _input_power->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
192         _input_threshold->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
193         _input_A->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
194         _input_B->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
195         _red_x->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
196         _red_y->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
197         _green_x->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
198         _green_y->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
199         _blue_x->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
200         _blue_y->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
201         _white_x->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
202         _white_y->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::chromaticity_changed, this));
203         _adjust_white->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&ColourConversionEditor::adjusted_white_changed, this));
204         _adjusted_white_x->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::adjusted_white_changed, this));
205         _adjusted_white_y->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::adjusted_white_changed, this));
206         _yuv_to_rgb->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&ColourConversionEditor::changed, this));
207 }
208
209 void
210 ColourConversionEditor::subhead (wxGridBagSizer* sizer, wxWindow* parent, wxString text, int& row) const
211 {
212         wxStaticText* m = new wxStaticText (parent, wxID_ANY, wxT (""));
213         m->SetLabelMarkup ("<b>" + text + "</b>");
214         sizer->Add (m, wxGBPosition (row, 0), wxGBSpan (1, 3), wxALIGN_CENTER_VERTICAL | wxTOP, 12);
215         ++row;
216 }
217
218 void
219 ColourConversionEditor::set (ColourConversion conversion)
220 {
221         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.in ())) {
222                 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.in ());
223                 _input_gamma_linearised->SetValue (false);
224                 set_spin_ctrl (_input_gamma, tf->gamma ());
225         } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (conversion.in ())) {
226                 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (conversion.in ());
227                 /* Arbitrary default; not used in this case (greyed out) */
228                 _input_gamma->SetValue (2.2);
229                 _input_gamma_linearised->SetValue (true);
230                 set_spin_ctrl (_input_power, tf->power ());
231                 set_text_ctrl (_input_threshold, tf->threshold ());
232                 set_text_ctrl (_input_A, tf->A ());
233                 set_text_ctrl (_input_B, tf->B ());
234         }
235
236         _yuv_to_rgb->SetSelection (conversion.yuv_to_rgb ());
237
238         SafeStringStream s;
239         s.setf (std::ios::fixed, std::ios::floatfield);
240         s.precision (6);
241
242         s << conversion.red().x;
243         _red_x->SetValue (std_to_wx (s.str ()));
244
245         s.str ("");
246         s << conversion.red().y;
247         _red_y->SetValue (std_to_wx (s.str ()));
248
249         s.str ("");
250         s << conversion.green().x;
251         _green_x->SetValue (std_to_wx (s.str ()));
252
253         s.str ("");
254         s << conversion.green().y;
255         _green_y->SetValue (std_to_wx (s.str ()));
256
257         s.str ("");
258         s << conversion.blue().x;
259         _blue_x->SetValue (std_to_wx (s.str ()));
260
261         s.str ("");
262         s << conversion.blue().y;
263         _blue_y->SetValue (std_to_wx (s.str ()));
264
265         s.str ("");
266         s << conversion.white().x;
267         _white_x->SetValue (std_to_wx (s.str ()));
268
269         s.str ("");
270         s << conversion.white().y;
271         _white_y->SetValue (std_to_wx (s.str ()));
272
273         if (conversion.adjusted_white ()) {
274                 _adjust_white->SetValue (true);
275                 s.str ("");
276                 s << conversion.adjusted_white().get().x;
277                 _adjusted_white_x->SetValue (std_to_wx (s.str ()));
278                 s.str ("");
279                 s << conversion.adjusted_white().get().y;
280                 _adjusted_white_y->SetValue (std_to_wx (s.str ()));
281         } else {
282                 _adjust_white->SetValue (false);
283         }
284
285         update_rgb_to_xyz ();
286         update_bradford ();
287 }
288
289 ColourConversion
290 ColourConversionEditor::get () const
291 {
292         ColourConversion conversion;
293
294         if (_input_gamma_linearised->GetValue ()) {
295                 conversion.set_in (
296                         shared_ptr<dcp::ModifiedGammaTransferFunction> (
297                                 new dcp::ModifiedGammaTransferFunction (
298                                         _input_power->GetValue (),
299                                         raw_convert<double> (wx_to_std (_input_threshold->GetValue ())),
300                                         raw_convert<double> (wx_to_std (_input_A->GetValue ())),
301                                         raw_convert<double> (wx_to_std (_input_B->GetValue ()))
302                                         )
303                                 )
304                         );
305         } else {
306                 conversion.set_in (
307                         shared_ptr<dcp::GammaTransferFunction> (new dcp::GammaTransferFunction (_input_gamma->GetValue ()))
308                         );
309         }
310
311         conversion.set_yuv_to_rgb (static_cast<dcp::YUVToRGB> (_yuv_to_rgb->GetSelection ()));
312
313         conversion.set_red (
314                 dcp::Chromaticity (raw_convert<double> (wx_to_std (_red_x->GetValue ())), raw_convert<double> (wx_to_std (_red_y->GetValue ())))
315                 );
316         conversion.set_green (
317                 dcp::Chromaticity (raw_convert<double> (wx_to_std (_green_x->GetValue ())), raw_convert<double> (wx_to_std (_green_y->GetValue ())))
318                 );
319         conversion.set_blue (
320                 dcp::Chromaticity (raw_convert<double> (wx_to_std (_blue_x->GetValue ())), raw_convert<double> (wx_to_std (_blue_y->GetValue ())))
321                 );
322         conversion.set_white (
323                 dcp::Chromaticity (raw_convert<double> (wx_to_std (_white_x->GetValue ())), raw_convert<double> (wx_to_std (_white_y->GetValue ())))
324                 );
325
326         if (_adjust_white->GetValue ()) {
327                 conversion.set_adjusted_white (
328                         dcp::Chromaticity (
329                                 raw_convert<double> (wx_to_std (_adjusted_white_x->GetValue ())),
330                                 raw_convert<double> (wx_to_std (_adjusted_white_y->GetValue ()))
331                                 )
332                         );
333         } else {
334                 conversion.unset_adjusted_white ();
335         }
336
337         conversion.set_out (shared_ptr<dcp::GammaTransferFunction> (new dcp::GammaTransferFunction (2.6)));
338
339         return conversion;
340 }
341
342 void
343 ColourConversionEditor::changed ()
344 {
345         bool const lin = _input_gamma_linearised->GetValue ();
346         _input_gamma->Enable (!lin);
347         _input_power->Enable (lin);
348         _input_threshold->Enable (lin);
349         _input_A->Enable (lin);
350         _input_B->Enable (lin);
351
352         Changed ();
353 }
354
355 void
356 ColourConversionEditor::chromaticity_changed ()
357 {
358         update_rgb_to_xyz ();
359         changed ();
360 }
361
362 void
363 ColourConversionEditor::adjusted_white_changed ()
364 {
365         update_bradford ();
366         changed ();
367 }
368
369 void
370 ColourConversionEditor::update_bradford ()
371 {
372         _adjusted_white_x->Enable (_adjust_white->GetValue ());
373         _adjusted_white_y->Enable (_adjust_white->GetValue ());
374
375         boost::numeric::ublas::matrix<double> m = get().bradford ();
376         for (int i = 0; i < 3; ++i) {
377                 for (int j = 0; j < 3; ++j) {
378                         SafeStringStream s;
379                         s.setf (std::ios::fixed, std::ios::floatfield);
380                         s.precision (7);
381                         s << m (i, j);
382                         _bradford[i][j]->SetLabel (std_to_wx (s.str ()));
383                 }
384         }
385 }
386
387 void
388 ColourConversionEditor::update_rgb_to_xyz ()
389 {
390         boost::numeric::ublas::matrix<double> m = get().rgb_to_xyz ();
391         for (int i = 0; i < 3; ++i) {
392                 for (int j = 0; j < 3; ++j) {
393                         SafeStringStream s;
394                         s.setf (std::ios::fixed, std::ios::floatfield);
395                         s.precision (7);
396                         s << m (i, j);
397                         _rgb_to_xyz[i][j]->SetLabel (std_to_wx (s.str ()));
398                 }
399         }
400 }
401
402 void
403 ColourConversionEditor::changed (wxSpinCtrlDouble* sc)
404 {
405         /* On OS X, it seems that in some cases when a wxSpinCtrlDouble loses focus
406            it emits an erroneous changed signal, which messes things up.
407            Check for that here.
408         */
409         if (fabs (_last_spin_ctrl_value[sc] - sc->GetValue()) < 1e-3) {
410                 return;
411         }
412
413         Changed ();
414 }
415
416 void
417 ColourConversionEditor::set_spin_ctrl (wxSpinCtrlDouble* control, double value)
418 {
419         _last_spin_ctrl_value[control] = value;
420         control->SetValue (value);
421 }
422
423 void
424 ColourConversionEditor::set_text_ctrl (wxTextCtrl* control, double value)
425 {
426         SafeStringStream s;
427         s.precision (7);
428         s << value;
429         control->SetValue (std_to_wx (s.str ()));
430 }