2 Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "check_box.h"
23 #include "colour_conversion_editor.h"
24 #include "static_text.h"
26 #include "lib/colour_conversion.h"
27 #include <dcp/gamma_transfer_function.h>
28 #include <dcp/identity_transfer_function.h>
29 #include <dcp/locale_convert.h>
30 #include <dcp/modified_gamma_transfer_function.h>
31 #include <dcp/s_gamut3_transfer_function.h>
32 #include <dcp/warnings.h>
33 LIBDCP_DISABLE_WARNINGS
34 #include <wx/gbsizer.h>
35 #include <wx/numformatter.h>
36 #include <wx/spinctrl.h>
37 LIBDCP_ENABLE_WARNINGS
40 using std::dynamic_pointer_cast;
41 using std::make_shared;
44 using dcp::locale_convert;
47 int const ColourConversionEditor::INPUT_GAMMA = 0;
48 int const ColourConversionEditor::INPUT_GAMMA_LINEARISED = 1;
49 int const ColourConversionEditor::INPUT_SGAMUT3 = 2;
52 ColourConversionEditor::ColourConversionEditor (wxWindow* parent, bool yuv)
53 : wxPanel (parent, wxID_ANY)
54 , _ignore_chromaticity_changed (false)
56 auto overall_sizer = new wxBoxSizer (wxVERTICAL);
57 SetSizer (overall_sizer);
59 auto table = new wxGridBagSizer (DCPOMATIC_SIZER_Y_GAP - 3, DCPOMATIC_SIZER_X_GAP);
60 overall_sizer->Add (table, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
64 subhead (table, this, _("Input gamma correction"), r);
66 add_label_to_sizer (table, this, _("Input transfer function"), true, wxGBPosition (r, 0));
67 _input = new wxChoice (this, wxID_ANY);
68 _input->Append (_("Simple gamma"));
69 _input->Append (_("Simple gamma, linearised for small values"));
70 _input->Append (_("S-Gamut3"));
71 table->Add (_input, wxGBPosition (r, 1), wxGBSpan (1, 2));
74 add_label_to_sizer (table, this, _("Input gamma"), true, wxGBPosition (r, 0));
75 _input_gamma = new wxSpinCtrlDouble (this);
76 table->Add (_input_gamma, wxGBPosition (r, 1));
79 add_label_to_sizer (table, this, _("Input power"), true, wxGBPosition (r, 0));
81 auto s = new wxBoxSizer (wxHORIZONTAL);
82 _input_power = new wxSpinCtrlDouble (this);
83 s->Add (_input_power, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
84 add_label_to_sizer (s, this, _("threshold"), true, 0, wxALIGN_CENTRE_VERTICAL | wxLEFT | wxRIGHT);
85 _input_threshold = new wxTextCtrl (this, wxID_ANY, wxT (""));
86 s->Add (_input_threshold, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
87 add_label_to_sizer (s, this, _("A"), true, 0, wxALIGN_CENTRE_VERTICAL | wxLEFT | wxRIGHT);
88 _input_A = new wxTextCtrl (this, wxID_ANY, wxT (""));
89 s->Add (_input_A, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
90 add_label_to_sizer (s, this, _("B"), true, 0, wxALIGN_CENTRE_VERTICAL | wxLEFT | wxRIGHT);
91 _input_B = new wxTextCtrl (this, wxID_ANY, wxT (""));
92 s->Add (_input_B, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
93 table->Add (s, wxGBPosition (r, 1), wxGBSpan (1, 3));
97 wxClientDC dc (parent);
98 auto size = dc.GetTextExtent(wxT("-0.12345678901"));
101 wxTextValidator validator (wxFILTER_INCLUDE_CHAR_LIST);
104 wxString n (wxT("0123456789-"));
105 n.Append(wxNumberFormatter::GetDecimalSeparator());
106 for (size_t i = 0; i < n.Length(); ++i) {
110 validator.SetIncludes (list);
113 /* YUV to RGB conversion */
115 auto yuv_heading = subhead (table, this, _("YUV to RGB conversion"), r);
117 auto yuv_label = add_label_to_sizer (table, this, _("YUV to RGB matrix"), true, wxGBPosition (r, 0));
118 _yuv_to_rgb = new wxChoice (this, wxID_ANY);
119 _yuv_to_rgb->Append (_("Rec. 601"));
120 _yuv_to_rgb->Append (_("Rec. 709"));
121 table->Add (_yuv_to_rgb, wxGBPosition (r, 1));
125 yuv_heading->Enable (false);
126 yuv_label->Enable (false);
127 _yuv_to_rgb->Enable (false);
130 /* RGB to XYZ conversion */
132 subhead (table, this, _("RGB to XYZ conversion"), r);
134 add_label_to_sizer (table, this, _("x"), false, wxGBPosition (r, 1));
135 add_label_to_sizer (table, this, _("y"), false, wxGBPosition (r, 2));
136 add_label_to_sizer (table, this, _("Matrix"), false, wxGBPosition (r, 3));
139 add_label_to_sizer (table, this, _("Red chromaticity"), true, wxGBPosition (r, 0));
140 _red_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
141 table->Add (_red_x, wxGBPosition (r, 1));
142 _red_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
143 table->Add (_red_y, wxGBPosition (r, 2));
146 add_label_to_sizer (table, this, _("Green chromaticity"), true, wxGBPosition (r, 0));
147 _green_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
148 table->Add (_green_x, wxGBPosition (r, 1));
149 _green_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
150 table->Add (_green_y, wxGBPosition (r, 2));
153 add_label_to_sizer (table, this, _("Blue chromaticity"), true, wxGBPosition (r, 0));
154 _blue_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
155 table->Add (_blue_x, wxGBPosition (r, 1));
156 _blue_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
157 table->Add (_blue_y, wxGBPosition (r, 2));
160 add_label_to_sizer (table, this, _("White point"), true, wxGBPosition (r, 0));
161 _white_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
162 table->Add (_white_x, wxGBPosition (r, 1));
163 _white_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
164 table->Add (_white_y, wxGBPosition (r, 2));
167 size = dc.GetTextExtent (wxT ("0.12345678"));
170 auto rgb_to_xyz_sizer = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
171 for (int i = 0; i < 3; ++i) {
172 for (int j = 0; j < 3; ++j) {
173 _rgb_to_xyz[i][j] = new StaticText (this, wxT (""), wxDefaultPosition, size, 0);
174 rgb_to_xyz_sizer->Add (_rgb_to_xyz[i][j]);
177 table->Add (rgb_to_xyz_sizer, wxGBPosition (r - 4, 3), wxGBSpan (4, 1));
179 /* White point adjustment */
181 size = dc.GetTextExtent (wxT ("-0.12345678901"));
184 subhead (table, this, _("White point adjustment"), r);
186 _adjust_white = new CheckBox (this, _("Adjust white point to"));
187 table->Add (_adjust_white, wxGBPosition (r, 0), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
188 _adjusted_white_x = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
189 table->Add (_adjusted_white_x, wxGBPosition (r, 1));
190 _adjusted_white_y = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator);
191 table->Add (_adjusted_white_y, wxGBPosition (r, 2));
194 add_label_to_sizer (table, this, wxT (""), false, wxGBPosition (r, 0));
197 size = dc.GetTextExtent (wxT ("0.12345678"));
200 auto bradford_sizer = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
201 for (int i = 0; i < 3; ++i) {
202 for (int j = 0; j < 3; ++j) {
203 _bradford[i][j] = new StaticText (this, wxT (""), wxDefaultPosition, size, 0);
204 bradford_sizer->Add (_bradford[i][j]);
207 table->Add (bradford_sizer, wxGBPosition (r - 2, 3), wxGBSpan (2, 1));
210 /* Output transfer function */
212 subhead (table, this, _("Output gamma correction"), r);
214 _output = new CheckBox (this, _("Inverse 2.6 gamma correction on output"));
215 table->Add (_output, wxGBPosition (r, 0), wxGBSpan (1, 2));
217 _input_gamma->SetRange (0.1, 4.0);
218 _input_gamma->SetDigits (2);
219 _input_gamma->SetIncrement (0.1);
220 _input_power->SetRange (0.1, 4.0);
221 _input_power->SetDigits (6);
222 _input_power->SetIncrement (0.1);
224 _input->Bind (wxEVT_CHOICE, bind (&ColourConversionEditor::changed, this));
225 _input_gamma->Bind (wxEVT_SPINCTRLDOUBLE, bind (&ColourConversionEditor::changed, this, _input_gamma));
226 _input_power->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::changed, this));
227 _input_threshold->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::changed, this));
228 _input_A->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::changed, this));
229 _input_B->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::changed, this));
230 _red_x->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::chromaticity_changed, this));
231 _red_y->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::chromaticity_changed, this));
232 _green_x->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::chromaticity_changed, this));
233 _green_y->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::chromaticity_changed, this));
234 _blue_x->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::chromaticity_changed, this));
235 _blue_y->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::chromaticity_changed, this));
236 _white_x->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::chromaticity_changed, this));
237 _white_y->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::chromaticity_changed, this));
238 _adjust_white->Bind (wxEVT_CHECKBOX, bind (&ColourConversionEditor::adjusted_white_changed, this));
239 _adjusted_white_x->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::adjusted_white_changed, this));
240 _adjusted_white_y->Bind (wxEVT_TEXT, bind (&ColourConversionEditor::adjusted_white_changed, this));
241 _yuv_to_rgb->Bind (wxEVT_CHOICE, bind (&ColourConversionEditor::changed, this));
242 _output->Bind (wxEVT_CHECKBOX, bind (&ColourConversionEditor::changed, this));
247 ColourConversionEditor::subhead (wxGridBagSizer* sizer, wxWindow* parent, wxString text, int& row) const
249 auto m = new StaticText (parent, text);
250 wxFont font (*wxNORMAL_FONT);
251 font.SetWeight (wxFONTWEIGHT_BOLD);
253 sizer->Add (m, wxGBPosition (row, 0), wxGBSpan (1, 3), wxALIGN_CENTER_VERTICAL | wxTOP, 12);
260 ColourConversionEditor::set (ColourConversion conversion)
262 if (dynamic_pointer_cast<const dcp::GammaTransferFunction>(conversion.in())) {
263 auto tf = dynamic_pointer_cast<const dcp::GammaTransferFunction>(conversion.in());
264 checked_set (_input, 0);
265 set_spin_ctrl (_input_gamma, tf->gamma ());
266 } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (conversion.in ())) {
267 auto tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction>(conversion.in());
268 checked_set (_input, 1);
269 /* Arbitrary default; not used in this case (greyed out) */
270 _input_gamma->SetValue (2.2);
271 set_spin_ctrl (_input_power, tf->power ());
272 set_text_ctrl (_input_threshold, tf->threshold ());
273 set_text_ctrl (_input_A, tf->A ());
274 set_text_ctrl (_input_B, tf->B ());
275 } else if (dynamic_pointer_cast<const dcp::SGamut3TransferFunction>(conversion.in())) {
276 checked_set (_input, 2);
279 _yuv_to_rgb->SetSelection (static_cast<int>(conversion.yuv_to_rgb()));
281 _ignore_chromaticity_changed = true;
284 snprintf (buffer, sizeof (buffer), "%.6f", conversion.red().x);
285 _red_x->SetValue (std_to_wx (buffer));
286 snprintf (buffer, sizeof (buffer), "%.6f", conversion.red().y);
287 _red_y->SetValue (std_to_wx (buffer));
288 snprintf (buffer, sizeof (buffer), "%.6f", conversion.green().x);
289 _green_x->SetValue (std_to_wx (buffer));
290 snprintf (buffer, sizeof (buffer), "%.6f", conversion.green().y);
291 _green_y->SetValue (std_to_wx (buffer));
292 snprintf (buffer, sizeof (buffer), "%.6f", conversion.blue().x);
293 _blue_x->SetValue (std_to_wx (buffer));
294 snprintf (buffer, sizeof (buffer), "%.6f", conversion.blue().y);
295 _blue_y->SetValue (std_to_wx (buffer));
296 snprintf (buffer, sizeof (buffer), "%.6f", conversion.white().x);
297 _white_x->SetValue (std_to_wx (buffer));
298 snprintf (buffer, sizeof (buffer), "%.6f", conversion.white().y);
299 _white_y->SetValue (std_to_wx (buffer));
301 _ignore_chromaticity_changed = false;
303 if (conversion.adjusted_white ()) {
304 _adjust_white->SetValue (true);
305 snprintf (buffer, sizeof (buffer), "%.6f", conversion.adjusted_white().get().x);
306 _adjusted_white_x->SetValue (std_to_wx (buffer));
307 snprintf (buffer, sizeof (buffer), "%.6f", conversion.adjusted_white().get().y);
308 _adjusted_white_y->SetValue (std_to_wx (buffer));
310 _adjust_white->SetValue (false);
313 _output->SetValue (static_cast<bool>(dynamic_pointer_cast<const dcp::GammaTransferFunction>(conversion.out())));
315 update_rgb_to_xyz ();
322 ColourConversionEditor::get () const
324 ColourConversion conversion;
326 switch (_input->GetSelection ()) {
329 make_shared<dcp::GammaTransferFunction>(_input_gamma->GetValue())
332 case INPUT_GAMMA_LINEARISED:
333 /* Linearised gamma */
335 make_shared<dcp::ModifiedGammaTransferFunction>(
336 _input_power->GetValue (),
337 locale_convert<double>(wx_to_std(_input_threshold->GetValue())),
338 locale_convert<double>(wx_to_std(_input_A->GetValue())),
339 locale_convert<double>(wx_to_std(_input_B->GetValue()))
345 conversion.set_in (make_shared<dcp::SGamut3TransferFunction>());
349 conversion.set_yuv_to_rgb (static_cast<dcp::YUVToRGB>(_yuv_to_rgb->GetSelection()));
352 dcp::Chromaticity(locale_convert<double>(wx_to_std(_red_x->GetValue())), locale_convert<double>(wx_to_std(_red_y->GetValue())))
354 conversion.set_green (
355 dcp::Chromaticity(locale_convert<double>(wx_to_std(_green_x->GetValue())), locale_convert<double>(wx_to_std(_green_y->GetValue())))
357 conversion.set_blue (
358 dcp::Chromaticity(locale_convert<double>(wx_to_std(_blue_x->GetValue())), locale_convert<double>(wx_to_std(_blue_y->GetValue())))
360 conversion.set_white (
361 dcp::Chromaticity(locale_convert<double>(wx_to_std(_white_x->GetValue())), locale_convert<double>(wx_to_std(_white_y->GetValue())))
364 if (_adjust_white->GetValue()) {
365 conversion.set_adjusted_white(
367 locale_convert<double>(wx_to_std(_adjusted_white_x->GetValue())),
368 locale_convert<double>(wx_to_std(_adjusted_white_y->GetValue()))
372 conversion.unset_adjusted_white ();
375 if (_output->GetValue ()) {
376 conversion.set_out (make_shared<dcp::GammaTransferFunction>(2.6));
378 conversion.set_out (make_shared<dcp::IdentityTransferFunction>());
386 ColourConversionEditor::changed ()
388 int const in = _input->GetSelection();
389 _input_gamma->Enable (in == INPUT_GAMMA);
390 _input_power->Enable (in == INPUT_GAMMA_LINEARISED);
391 _input_threshold->Enable (in == INPUT_GAMMA_LINEARISED);
392 _input_A->Enable (in == INPUT_GAMMA_LINEARISED);
393 _input_B->Enable (in == INPUT_GAMMA_LINEARISED);
400 ColourConversionEditor::chromaticity_changed ()
402 if (_ignore_chromaticity_changed) {
406 update_rgb_to_xyz ();
412 ColourConversionEditor::adjusted_white_changed ()
420 ColourConversionEditor::update_bradford ()
422 _adjusted_white_x->Enable (_adjust_white->GetValue ());
423 _adjusted_white_y->Enable (_adjust_white->GetValue ());
425 auto m = get().bradford();
426 for (int i = 0; i < 3; ++i) {
427 for (int j = 0; j < 3; ++j) {
429 snprintf (buffer, sizeof (buffer), "%.7f", m (i, j));
430 _bradford[i][j]->SetLabel (std_to_wx (buffer));
437 ColourConversionEditor::update_rgb_to_xyz ()
439 auto m = get().rgb_to_xyz();
440 for (int i = 0; i < 3; ++i) {
441 for (int j = 0; j < 3; ++j) {
443 snprintf (buffer, sizeof (buffer), "%.7f", m (i, j));
444 _rgb_to_xyz[i][j]->SetLabel (std_to_wx (buffer));
451 ColourConversionEditor::changed (wxSpinCtrlDouble* sc)
453 /* On OS X, it seems that in some cases when a wxSpinCtrlDouble loses focus
454 it emits an erroneous changed signal, which messes things up.
457 if (fabs(_last_spin_ctrl_value[sc] - sc->GetValue()) < 1e-3) {
466 ColourConversionEditor::set_spin_ctrl (wxSpinCtrlDouble* control, double value)
468 _last_spin_ctrl_value[control] = value;
469 control->SetValue (value);
474 ColourConversionEditor::set_text_ctrl (wxTextCtrl* control, double value)
477 snprintf (buffer, sizeof (buffer), "%.7f", value);
478 control->SetValue (std_to_wx (buffer));