2 Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "lib/colour_conversion.h"
21 #include "lib/safe_stringstream.h"
23 #include "colour_conversion_editor.h"
24 #include <dcp/gamma_transfer_function.h>
25 #include <dcp/modified_gamma_transfer_function.h>
26 #include <dcp/raw_convert.h>
27 #include <wx/spinctrl.h>
28 #include <wx/gbsizer.h>
29 #include <boost/lexical_cast.hpp>
33 using boost::shared_ptr;
34 using boost::dynamic_pointer_cast;
35 using boost::lexical_cast;
36 using dcp::raw_convert;
38 ColourConversionEditor::ColourConversionEditor (wxWindow* parent)
39 : wxPanel (parent, wxID_ANY)
41 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
42 SetSizer (overall_sizer);
44 wxGridBagSizer* table = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
45 overall_sizer->Add (table, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
49 _input_gamma_linearised = new wxCheckBox (this, wxID_ANY, _("Linearise input gamma curve for low values"));
50 table->Add (_input_gamma_linearised, wxGBPosition (r, 0), wxGBSpan (1, 2));
53 add_label_to_grid_bag_sizer (table, this, _("Input gamma"), true, wxGBPosition (r, 0));
54 _input_gamma = new wxSpinCtrlDouble (this);
55 table->Add (_input_gamma, wxGBPosition (r, 1));
58 add_label_to_grid_bag_sizer (table, this, _("Input power"), true, wxGBPosition (r, 0));
59 _input_power = new wxSpinCtrlDouble (this);
60 table->Add (_input_power, wxGBPosition (r, 1));
63 add_label_to_grid_bag_sizer (table, this, _("Input threshold"), true, wxGBPosition (r, 0));
64 _input_threshold = new wxTextCtrl (this, wxID_ANY, wxT (""));
65 table->Add (_input_threshold, wxGBPosition (r, 1));
68 add_label_to_grid_bag_sizer (table, this, _("Input A value"), true, wxGBPosition (r, 0));
69 _input_A = new wxTextCtrl (this, wxID_ANY, wxT (""));
70 table->Add (_input_A, wxGBPosition (r, 1));
73 add_label_to_grid_bag_sizer (table, this, _("Input B value"), true, wxGBPosition (r, 0));
74 _input_B = new wxTextCtrl (this, wxID_ANY, wxT (""));
75 table->Add (_input_B, wxGBPosition (r, 1));
78 wxClientDC dc (parent);
79 wxSize size = dc.GetTextExtent (wxT ("-0.12345678901"));
82 wxTextValidator validator (wxFILTER_INCLUDE_CHAR_LIST);
85 wxString n (wxT ("0123456789.-"));
86 for (size_t i = 0; i < n.Length(); ++i) {
90 validator.SetIncludes (list);
92 add_label_to_grid_bag_sizer (table, this, _("Matrix"), true, wxGBPosition (r, 0));
93 wxFlexGridSizer* matrix_sizer = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
94 for (int i = 0; i < 3; ++i) {
95 for (int j = 0; j < 3; ++j) {
96 _matrix[i][j] = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
97 matrix_sizer->Add (_matrix[i][j]);
100 table->Add (matrix_sizer, wxGBPosition (r, 1));
103 add_label_to_grid_bag_sizer (table, this, _("Output gamma"), true, wxGBPosition (r, 0));
104 wxBoxSizer* output_sizer = new wxBoxSizer (wxHORIZONTAL);
105 /// TRANSLATORS: this means the mathematical reciprocal operation, i.e. we are dividing 1 by the control that
107 add_label_to_sizer (output_sizer, this, _("1 / "), false);
108 _output_gamma = new wxSpinCtrlDouble (this);
109 output_sizer->Add (_output_gamma);
110 table->Add (output_sizer, wxGBPosition (r, 1));
113 _input_gamma->SetRange (0.1, 4.0);
114 _input_gamma->SetDigits (2);
115 _input_gamma->SetIncrement (0.1);
116 _input_power->SetRange (0.1, 4.0);
117 _input_power->SetDigits (2);
118 _input_power->SetIncrement (0.1);
119 _output_gamma->SetRange (0.1, 4.0);
120 _output_gamma->SetDigits (2);
121 _output_gamma->SetIncrement (0.1);
123 _input_gamma->Bind (wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, boost::bind (&ColourConversionEditor::changed, this, _input_gamma));
124 _input_gamma_linearised->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&ColourConversionEditor::changed, this));
125 _input_power->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
126 _input_threshold->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
127 _input_A->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
128 _input_B->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
129 for (int i = 0; i < 3; ++i) {
130 for (int j = 0; j < 3; ++j) {
131 _matrix[i][j]->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ColourConversionEditor::changed, this));
134 _output_gamma->Bind (wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, boost::bind (&ColourConversionEditor::changed, this, _output_gamma));
138 ColourConversionEditor::set (ColourConversion conversion)
140 if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.in ())) {
141 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.in ());
142 _input_gamma_linearised->SetValue (false);
143 set_spin_ctrl (_input_gamma, tf->gamma ());
144 } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (conversion.in ())) {
145 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (conversion.in ());
146 /* Arbitrary default */
147 _input_gamma->SetValue (2.2);
148 _input_gamma_linearised->SetValue (true);
149 set_spin_ctrl (_input_power, tf->power ());
150 set_text_ctrl (_input_threshold, tf->threshold ());
151 set_text_ctrl (_input_A, tf->A ());
152 set_text_ctrl (_input_B, tf->B ());
155 boost::numeric::ublas::matrix<double> matrix = conversion.matrix ();
156 for (int i = 0; i < 3; ++i) {
157 for (int j = 0; j < 3; ++j) {
158 set_text_ctrl (_matrix[i][j], matrix(i, j));
162 set_spin_ctrl (_output_gamma, dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.out ())->gamma ());
166 ColourConversionEditor::get () const
168 ColourConversion conversion;
170 if (_input_gamma_linearised->GetValue ()) {
172 shared_ptr<dcp::ModifiedGammaTransferFunction> (
173 new dcp::ModifiedGammaTransferFunction (
175 _input_power->GetValue (),
176 raw_convert<double> (wx_to_std (_input_threshold->GetValue ())),
177 raw_convert<double> (wx_to_std (_input_A->GetValue ())),
178 raw_convert<double> (wx_to_std (_input_B->GetValue ()))
184 shared_ptr<dcp::GammaTransferFunction> (
185 new dcp::GammaTransferFunction (
187 _input_gamma->GetValue ()
193 boost::numeric::ublas::matrix<double> matrix (3, 3);
194 for (int i = 0; i < 3; ++i) {
195 for (int j = 0; j < 3; ++j) {
196 string const v = wx_to_std (_matrix[i][j]->GetValue ());
200 matrix (i, j) = raw_convert<double> (v);
205 conversion.set_matrix (matrix);
207 conversion.set_out (shared_ptr<dcp::GammaTransferFunction> (new dcp::GammaTransferFunction (true, _output_gamma->GetValue ())));
213 ColourConversionEditor::changed ()
215 bool const lin = _input_gamma_linearised->GetValue ();
216 _input_gamma->Enable (!lin);
217 _input_power->Enable (lin);
218 _input_threshold->Enable (lin);
219 _input_A->Enable (lin);
220 _input_B->Enable (lin);
226 ColourConversionEditor::changed (wxSpinCtrlDouble* sc)
228 /* On OS X, it seems that in some cases when a wxSpinCtrlDouble loses focus
229 it emits an erroneous changed signal, which messes things up.
232 if (fabs (_last_spin_ctrl_value[sc] - sc->GetValue()) < 1e-3) {
240 ColourConversionEditor::set_spin_ctrl (wxSpinCtrlDouble* control, double value)
242 _last_spin_ctrl_value[control] = value;
243 control->SetValue (value);
247 ColourConversionEditor::set_text_ctrl (wxTextCtrl* control, double value)
252 control->SetValue (std_to_wx (s.str ()));