Add our own raw_convert that uses SafeStringStream.
[dcpomatic.git] / src / wx / colour_conversion_editor.cc
1 /*
2     Copyright (C) 2013-2014 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_X_GAP, DCPOMATIC_SIZER_Y_GAP);
44         overall_sizer->Add (table, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
45
46         int r = 0;
47
48         _input_gamma_linearised = new wxCheckBox (this, wxID_ANY, _("Linearise input gamma curve for low values"));
49         table->Add (_input_gamma_linearised, wxGBPosition (r, 0), wxGBSpan (1, 2));
50         ++r;
51
52         add_label_to_grid_bag_sizer (table, this, _("Input gamma"), true, wxGBPosition (r, 0));
53         _input_gamma = new wxSpinCtrlDouble (this);
54         table->Add (_input_gamma, wxGBPosition (r, 1));
55         ++r;
56
57         add_label_to_grid_bag_sizer (table, this, _("Input power"), true, wxGBPosition (r, 0));
58         _input_power = new wxSpinCtrlDouble (this);
59         table->Add (_input_power, wxGBPosition (r, 1));
60         ++r;
61
62         add_label_to_grid_bag_sizer (table, this, _("Input threshold"), true, wxGBPosition (r, 0));
63         _input_threshold = new wxTextCtrl (this, wxID_ANY, wxT (""));
64         table->Add (_input_threshold, wxGBPosition (r, 1));
65         ++r;
66
67         add_label_to_grid_bag_sizer (table, this, _("Input A value"), true, wxGBPosition (r, 0));
68         _input_A = new wxTextCtrl (this, wxID_ANY, wxT (""));
69         table->Add (_input_A, wxGBPosition (r, 1));
70         ++r;
71
72         add_label_to_grid_bag_sizer (table, this, _("Input B value"), true, wxGBPosition (r, 0));
73         _input_B = new wxTextCtrl (this, wxID_ANY, wxT (""));
74         table->Add (_input_B, wxGBPosition (r, 1));
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         add_label_to_grid_bag_sizer (table, this, _("Matrix"), true, wxGBPosition (r, 0));
92         wxFlexGridSizer* matrix_sizer = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
93         for (int i = 0; i < 3; ++i) {
94                 for (int j = 0; j < 3; ++j) {
95                         _matrix[i][j] = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
96                         matrix_sizer->Add (_matrix[i][j]);
97                 }
98         }
99         table->Add (matrix_sizer, wxGBPosition (r, 1));
100         ++r;
101
102         add_label_to_grid_bag_sizer (table, this, _("Output gamma"), true, wxGBPosition (r, 0));
103         wxBoxSizer* output_sizer = new wxBoxSizer (wxHORIZONTAL);
104         /// TRANSLATORS: this means the mathematical reciprocal operation, i.e. we are dividing 1 by the control that
105         /// comes after it.
106         add_label_to_sizer (output_sizer, this, _("1 / "), false);
107         _output_gamma = new wxSpinCtrlDouble (this);
108         output_sizer->Add (_output_gamma);
109         table->Add (output_sizer, wxGBPosition (r, 1));
110         ++r;
111
112         _input_gamma->SetRange (0.1, 4.0);
113         _input_gamma->SetDigits (2);
114         _input_gamma->SetIncrement (0.1);
115         _input_power->SetRange (0.1, 4.0);
116         _input_power->SetDigits (2);
117         _input_power->SetIncrement (0.1);
118         _output_gamma->SetRange (0.1, 4.0);
119         _output_gamma->SetDigits (2);
120         _output_gamma->SetIncrement (0.1);
121
122         _input_gamma->Bind (wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, boost::bind (&ColourConversionEditor::changed, this, _input_gamma));
123         _input_gamma_linearised->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&ColourConversionEditor::changed, this));
124         _input_power->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
125         _input_threshold->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
126         _input_A->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
127         _input_B->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
128         for (int i = 0; i < 3; ++i) {
129                 for (int j = 0; j < 3; ++j) {
130                         _matrix[i][j]->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
131                 }
132         }
133         _output_gamma->Bind (wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, boost::bind (&ColourConversionEditor::changed, this, _output_gamma));
134 }
135
136 void
137 ColourConversionEditor::set (ColourConversion conversion)
138 {
139         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.in ())) {
140                 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.in ());
141                 _input_gamma_linearised->SetValue (false);
142                 set_spin_ctrl (_input_gamma, tf->gamma ());
143         } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (conversion.in ())) {
144                 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (conversion.in ());
145                 /* Arbitrary default */
146                 _input_gamma->SetValue (2.2);
147                 _input_gamma_linearised->SetValue (true);
148                 set_spin_ctrl (_input_power, tf->power ());
149                 set_text_ctrl (_input_threshold, tf->threshold ());
150                 set_text_ctrl (_input_A, tf->A ());
151                 set_text_ctrl (_input_B, tf->B ());
152         }
153
154         boost::numeric::ublas::matrix<double> matrix = conversion.matrix ();
155         for (int i = 0; i < 3; ++i) {
156                 for (int j = 0; j < 3; ++j) {
157                         set_text_ctrl (_matrix[i][j], matrix(i, j));
158                 }
159         }
160         
161         set_spin_ctrl (_output_gamma, dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.out ())->gamma ());
162 }
163
164 ColourConversion
165 ColourConversionEditor::get () const
166 {
167         ColourConversion conversion;
168
169         if (_input_gamma_linearised->GetValue ()) {
170                 conversion.set_in (
171                         shared_ptr<dcp::ModifiedGammaTransferFunction> (
172                                 new dcp::ModifiedGammaTransferFunction (
173                                         false,
174                                         _input_power->GetValue (),
175                                         raw_convert<double> (wx_to_std (_input_threshold->GetValue ())),
176                                         raw_convert<double> (wx_to_std (_input_A->GetValue ())),
177                                         raw_convert<double> (wx_to_std (_input_B->GetValue ()))
178                                         )
179                                 )
180                         );
181         } else {
182                 conversion.set_in (
183                         shared_ptr<dcp::GammaTransferFunction> (
184                                 new dcp::GammaTransferFunction (
185                                         false,
186                                         _input_gamma->GetValue ()
187                                         )
188                                 )
189                         );
190         }
191
192         boost::numeric::ublas::matrix<double> matrix (3, 3);
193         for (int i = 0; i < 3; ++i) {
194                 for (int j = 0; j < 3; ++j) {
195                         string const v = wx_to_std (_matrix[i][j]->GetValue ());
196                         if (v.empty ()) {
197                                 matrix (i, j) = 0;
198                         } else {
199                                 matrix (i, j) = raw_convert<double> (v);
200                         }
201                 }
202         }
203
204         conversion.set_matrix (matrix);
205
206         conversion.set_out (shared_ptr<dcp::GammaTransferFunction> (new dcp::GammaTransferFunction (true, _output_gamma->GetValue ())));
207
208         return conversion;
209 }
210
211 void
212 ColourConversionEditor::changed ()
213 {
214         bool const lin = _input_gamma_linearised->GetValue ();
215         _input_gamma->Enable (!lin);
216         _input_power->Enable (lin);
217         _input_threshold->Enable (lin);
218         _input_A->Enable (lin);
219         _input_B->Enable (lin);
220         
221         Changed ();
222 }
223
224 void
225 ColourConversionEditor::changed (wxSpinCtrlDouble* sc)
226 {
227         /* On OS X, it seems that in some cases when a wxSpinCtrlDouble loses focus
228            it emits an erroneous changed signal, which messes things up.
229            Check for that here.
230         */
231         if (fabs (_last_spin_ctrl_value[sc] - sc->GetValue()) < 1e-3) {
232                 return;
233         }
234         
235         Changed ();
236 }
237
238 void
239 ColourConversionEditor::set_spin_ctrl (wxSpinCtrlDouble* control, double value)
240 {
241         _last_spin_ctrl_value[control] = value;
242         control->SetValue (value);
243 }
244
245 void
246 ColourConversionEditor::set_text_ctrl (wxTextCtrl* control, double value)
247 {
248         SafeStringStream s;
249         s.precision (7);
250         s << value;
251         control->SetValue (std_to_wx (s.str ()));
252 }