Separate out management of controls.
[dcpomatic.git] / src / wx / video_waveform_plot.cc
1 /*
2     Copyright (C) 2015-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "video_waveform_plot.h"
22 #include "film_viewer.h"
23 #include "wx_util.h"
24 #include "lib/image.h"
25 #include "lib/film.h"
26 #include "lib/dcp_video.h"
27 #include <dcp/locale_convert.h>
28 #include <dcp/openjpeg_image.h>
29 #include <wx/rawbmp.h>
30 #include <wx/graphics.h>
31 #include <boost/bind.hpp>
32 #include <iostream>
33
34 using std::cout;
35 using std::min;
36 using std::max;
37 using std::string;
38 using boost::weak_ptr;
39 using boost::shared_ptr;
40 using dcp::locale_convert;
41
42 int const VideoWaveformPlot::_vertical_margin = 8;
43 int const VideoWaveformPlot::_pixel_values = 4096;
44 int const VideoWaveformPlot::_x_axis_width = 52;
45
46 VideoWaveformPlot::VideoWaveformPlot (wxWindow* parent, weak_ptr<const Film> film, weak_ptr<FilmViewer> viewer)
47         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
48         , _film (film)
49         , _dirty (true)
50         , _enabled (false)
51         , _component (0)
52         , _contrast (0)
53 {
54 #ifndef __WXOSX__
55         SetDoubleBuffered (true);
56 #endif
57
58         shared_ptr<FilmViewer> fv = viewer.lock ();
59         DCPOMATIC_ASSERT (fv);
60
61         _viewer_connection = fv->ImageChanged.connect (boost::bind (&VideoWaveformPlot::set_image, this, _1));
62
63         Bind (wxEVT_PAINT, boost::bind (&VideoWaveformPlot::paint, this));
64         Bind (wxEVT_SIZE,  boost::bind (&VideoWaveformPlot::sized, this, _1));
65         Bind (wxEVT_MOTION, boost::bind (&VideoWaveformPlot::mouse_moved, this, _1));
66
67         SetMinSize (wxSize (640, 512));
68         SetBackgroundColour (wxColour (0, 0, 0));
69 }
70
71 void
72 VideoWaveformPlot::paint ()
73 {
74         wxPaintDC dc (this);
75
76         if (_dirty) {
77                 create_waveform ();
78                 _dirty = false;
79         }
80
81         if (!_waveform) {
82                 return;
83         }
84
85         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
86         if (!gc) {
87                 return;
88         }
89
90         int const height = _waveform->size().height;
91
92         gc->SetPen (wxPen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
93
94         gc->SetFont (gc->CreateFont (*wxSMALL_FONT, wxColour (255, 255, 255)));
95         double label_width;
96         double label_height;
97         double label_descent;
98         double label_leading;
99         gc->GetTextExtent (wxT ("1024"), &label_width, &label_height, &label_descent, &label_leading);
100
101         double extra[3];
102         double w;
103         gc->GetTextExtent (wxT ("0"), &w, &label_height, &label_descent, &label_leading);
104         extra[0] = label_width - w;
105         gc->GetTextExtent (wxT ("64"), &w, &label_height, &label_descent, &label_leading);
106         extra[1] = label_width - w;
107         gc->GetTextExtent (wxT ("512"), &w, &label_height, &label_descent, &label_leading);
108         extra[2] = label_width - w;
109
110         int label_gaps = 2;
111         while (height / label_gaps > 64) {
112                 label_gaps *= 2;
113         }
114
115         for (int i = 0; i < label_gaps + 1; ++i) {
116                 wxGraphicsPath p = gc->CreatePath ();
117                 int const y = _vertical_margin + height - (i * height / label_gaps) - 1;
118                 p.MoveToPoint (label_width + 8, y);
119                 p.AddLineToPoint (_x_axis_width - 4, y);
120                 gc->StrokePath (p);
121                 int x = 4;
122                 int const n = i * _pixel_values / label_gaps;
123                 if (n < 10) {
124                         x += extra[0];
125                 } else if (n < 100) {
126                         x += extra[1];
127                 } else if (n < 1000) {
128                         x += extra[2];
129                 }
130                 gc->DrawText (std_to_wx (locale_convert<string> (n)), x, y - (label_height / 2));
131         }
132
133         wxImage waveform (_waveform->size().width, height, _waveform->data()[0], true);
134         wxBitmap bitmap (waveform);
135         gc->DrawBitmap (bitmap, _x_axis_width, _vertical_margin, _waveform->size().width, height);
136
137         delete gc;
138 }
139
140 void
141 VideoWaveformPlot::create_waveform ()
142 {
143         _waveform.reset ();
144
145         if (!_image) {
146                 return;
147         }
148
149         dcp::Size const image_size = _image->size();
150         int const waveform_height = GetSize().GetHeight() - _vertical_margin * 2;
151         _waveform.reset (new Image (AV_PIX_FMT_RGB24, dcp::Size (image_size.width, waveform_height), true));
152
153         for (int x = 0; x < image_size.width; ++x) {
154
155                 /* Work out one vertical `slice' of waveform pixels.  Each value in
156                    strip is the number of samples in image with the corresponding group of
157                    values.
158                 */
159                 int strip[waveform_height];
160                 memset (strip, 0, waveform_height * sizeof(int));
161
162                 int* ip = _image->data (_component) + x;
163                 for (int y = 0; y < image_size.height; ++y) {
164                         strip[*ip * waveform_height / _pixel_values]++;
165                         ip += image_size.width;
166                 }
167
168                 /* Copy slice into the waveform */
169                 uint8_t* wp = _waveform->data()[0] + x * 3;
170                 for (int y = waveform_height - 1; y >= 0; --y) {
171                         wp[0] = wp[1] = wp[2] = min (255, (strip[y] * 255 / waveform_height) * _contrast);
172                         wp += _waveform->stride()[0];
173                 }
174         }
175
176         _waveform = _waveform->scale (
177                 dcp::Size (GetSize().GetWidth() - _x_axis_width, waveform_height),
178                 dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, false, false
179                 );
180 }
181
182 static void
183 note ()
184 {
185
186 }
187
188 void
189 VideoWaveformPlot::set_image (weak_ptr<PlayerVideo> image)
190 {
191         if (!_enabled) {
192                 return;
193         }
194
195         shared_ptr<PlayerVideo> pv = image.lock ();
196         _image = DCPVideo::convert_to_xyz (pv, boost::bind (&note));
197         _dirty = true;
198         Refresh ();
199 }
200
201 void
202 VideoWaveformPlot::sized (wxSizeEvent &)
203 {
204         _dirty = true;
205 }
206
207 void
208 VideoWaveformPlot::set_enabled (bool e)
209 {
210         _enabled = e;
211 }
212
213 void
214 VideoWaveformPlot::set_component (int c)
215 {
216         _component = c;
217         _dirty = true;
218         Refresh ();
219 }
220
221 /** Set `contrast', i.e. a fudge multiplication factor to make low-level signals easier to see,
222  *  between 0 and 256.
223  */
224 void
225 VideoWaveformPlot::set_contrast (int b)
226 {
227         _contrast = b;
228         _dirty = true;
229         Refresh ();
230 }
231
232 void
233 VideoWaveformPlot::mouse_moved (wxMouseEvent& ev)
234 {
235         if (!_image) {
236                 return;
237         }
238
239         if (_dirty) {
240                 create_waveform ();
241                 _dirty = false;
242         }
243
244         shared_ptr<const Film> film = _film.lock ();
245         if (!film) {
246                 return;
247         }
248
249         dcp::Size const full = film->frame_size ();
250
251         double const xs = static_cast<double> (full.width) / _waveform->size().width;
252         int const x1 = max (0, min (full.width - 1, int (floor (ev.GetPosition().x - _x_axis_width - 0.5) * xs)));
253         int const x2 = max (0, min (full.width - 1, int (floor (ev.GetPosition().x - _x_axis_width + 0.5) * xs)));
254
255         double const ys = static_cast<double> (_pixel_values) / _waveform->size().height;
256         int const fy = _waveform->size().height - (ev.GetPosition().y - _vertical_margin);
257         int const y1 = max (0, min (_pixel_values - 1, int (floor (fy - 0.5) * ys)));
258         int const y2 = max (0, min (_pixel_values - 1, int (floor (fy + 0.5) * ys)));
259
260         MouseMoved (x1, x2, y1, y2);
261 }