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"
22 #include "lib/raw_convert.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>
33 using boost::shared_ptr;
34 using boost::dynamic_pointer_cast;
35 using boost::lexical_cast;
37 ColourConversionEditor::ColourConversionEditor (wxWindow* parent)
38 : wxPanel (parent, wxID_ANY)
40 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
41 SetSizer (overall_sizer);
43 wxGridBagSizer* table = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
44 overall_sizer->Add (table, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
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));
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));
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));
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));
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));
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));
77 wxClientDC dc (parent);
78 wxSize size = dc.GetTextExtent (wxT ("-0.12345678901"));
81 wxTextValidator validator (wxFILTER_INCLUDE_CHAR_LIST);
84 wxString n (wxT ("0123456789.-"));
85 for (size_t i = 0; i < n.Length(); ++i) {
89 validator.SetIncludes (list);
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]);
99 table->Add (matrix_sizer, wxGBPosition (r, 1));
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
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));
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);
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));
133 _output_gamma->Bind (wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, boost::bind (&ColourConversionEditor::changed, this, _output_gamma));
137 ColourConversionEditor::set (ColourConversion conversion)
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 ());
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));
161 set_spin_ctrl (_output_gamma, dynamic_pointer_cast<const dcp::GammaTransferFunction> (conversion.out ())->gamma ());
165 ColourConversionEditor::get () const
167 ColourConversion conversion;
169 if (_input_gamma_linearised->GetValue ()) {
171 shared_ptr<dcp::ModifiedGammaTransferFunction> (
172 new dcp::ModifiedGammaTransferFunction (
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 ()))
183 shared_ptr<dcp::GammaTransferFunction> (
184 new dcp::GammaTransferFunction (
186 _input_gamma->GetValue ()
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 ());
199 matrix (i, j) = raw_convert<double> (v);
204 conversion.set_matrix (matrix);
206 conversion.set_out (shared_ptr<dcp::GammaTransferFunction> (new dcp::GammaTransferFunction (true, _output_gamma->GetValue ())));
212 ColourConversionEditor::changed ()
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);
225 ColourConversionEditor::changed (wxSpinCtrlDouble* sc)
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.
231 if (fabs (_last_spin_ctrl_value[sc] - sc->GetValue()) < 1e-3) {
239 ColourConversionEditor::set_spin_ctrl (wxSpinCtrlDouble* control, double value)
241 _last_spin_ctrl_value[control] = value;
242 control->SetValue (value);
246 ColourConversionEditor::set_text_ctrl (wxTextCtrl* control, double value)
251 control->SetValue (std_to_wx (s.str ()));