Fix build.
[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 "control_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, ControlFilmViewer* 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         _viewer_connection = viewer->ImageChanged.connect (boost::bind (&VideoWaveformPlot::set_image, this, _1));
59
60         Bind (wxEVT_PAINT, boost::bind (&VideoWaveformPlot::paint, this));
61         Bind (wxEVT_SIZE,  boost::bind (&VideoWaveformPlot::sized, this, _1));
62         Bind (wxEVT_MOTION, boost::bind (&VideoWaveformPlot::mouse_moved, this, _1));
63
64         SetMinSize (wxSize (640, 512));
65         SetBackgroundColour (wxColour (0, 0, 0));
66 }
67
68 void
69 VideoWaveformPlot::paint ()
70 {
71         wxPaintDC dc (this);
72
73         if (_dirty) {
74                 create_waveform ();
75                 _dirty = false;
76         }
77
78         if (!_waveform) {
79                 return;
80         }
81
82         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
83         if (!gc) {
84                 return;
85         }
86
87         int const height = _waveform->size().height;
88
89         gc->SetPen (wxPen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
90
91         gc->SetFont (gc->CreateFont (*wxSMALL_FONT, wxColour (255, 255, 255)));
92         double label_width;
93         double label_height;
94         double label_descent;
95         double label_leading;
96         gc->GetTextExtent (wxT ("1024"), &label_width, &label_height, &label_descent, &label_leading);
97
98         double extra[3];
99         double w;
100         gc->GetTextExtent (wxT ("0"), &w, &label_height, &label_descent, &label_leading);
101         extra[0] = label_width - w;
102         gc->GetTextExtent (wxT ("64"), &w, &label_height, &label_descent, &label_leading);
103         extra[1] = label_width - w;
104         gc->GetTextExtent (wxT ("512"), &w, &label_height, &label_descent, &label_leading);
105         extra[2] = label_width - w;
106
107         int label_gaps = 2;
108         while (height / label_gaps > 64) {
109                 label_gaps *= 2;
110         }
111
112         for (int i = 0; i < label_gaps + 1; ++i) {
113                 wxGraphicsPath p = gc->CreatePath ();
114                 int const y = _vertical_margin + height - (i * height / label_gaps) - 1;
115                 p.MoveToPoint (label_width + 8, y);
116                 p.AddLineToPoint (_x_axis_width - 4, y);
117                 gc->StrokePath (p);
118                 int x = 4;
119                 int const n = i * _pixel_values / label_gaps;
120                 if (n < 10) {
121                         x += extra[0];
122                 } else if (n < 100) {
123                         x += extra[1];
124                 } else if (n < 1000) {
125                         x += extra[2];
126                 }
127                 gc->DrawText (std_to_wx (locale_convert<string> (n)), x, y - (label_height / 2));
128         }
129
130         wxImage waveform (_waveform->size().width, height, _waveform->data()[0], true);
131         wxBitmap bitmap (waveform);
132         gc->DrawBitmap (bitmap, _x_axis_width, _vertical_margin, _waveform->size().width, height);
133
134         delete gc;
135 }
136
137 void
138 VideoWaveformPlot::create_waveform ()
139 {
140         _waveform.reset ();
141
142         if (!_image) {
143                 return;
144         }
145
146         dcp::Size const image_size = _image->size();
147         int const waveform_height = GetSize().GetHeight() - _vertical_margin * 2;
148         _waveform.reset (new Image (AV_PIX_FMT_RGB24, dcp::Size (image_size.width, waveform_height), true));
149
150         for (int x = 0; x < image_size.width; ++x) {
151
152                 /* Work out one vertical `slice' of waveform pixels.  Each value in
153                    strip is the number of samples in image with the corresponding group of
154                    values.
155                 */
156                 int strip[waveform_height];
157                 memset (strip, 0, waveform_height * sizeof(int));
158
159                 int* ip = _image->data (_component) + x;
160                 for (int y = 0; y < image_size.height; ++y) {
161                         strip[*ip * waveform_height / _pixel_values]++;
162                         ip += image_size.width;
163                 }
164
165                 /* Copy slice into the waveform */
166                 uint8_t* wp = _waveform->data()[0] + x * 3;
167                 for (int y = waveform_height - 1; y >= 0; --y) {
168                         wp[0] = wp[1] = wp[2] = min (255, (strip[y] * 255 / waveform_height) * _contrast);
169                         wp += _waveform->stride()[0];
170                 }
171         }
172
173         _waveform = _waveform->scale (
174                 dcp::Size (GetSize().GetWidth() - _x_axis_width, waveform_height),
175                 dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, false, false
176                 );
177 }
178
179 static void
180 note ()
181 {
182
183 }
184
185 void
186 VideoWaveformPlot::set_image (weak_ptr<PlayerVideo> image)
187 {
188         if (!_enabled) {
189                 return;
190         }
191
192         shared_ptr<PlayerVideo> pv = image.lock ();
193         _image = DCPVideo::convert_to_xyz (pv, boost::bind (&note));
194         _dirty = true;
195         Refresh ();
196 }
197
198 void
199 VideoWaveformPlot::sized (wxSizeEvent &)
200 {
201         _dirty = true;
202 }
203
204 void
205 VideoWaveformPlot::set_enabled (bool e)
206 {
207         _enabled = e;
208 }
209
210 void
211 VideoWaveformPlot::set_component (int c)
212 {
213         _component = c;
214         _dirty = true;
215         Refresh ();
216 }
217
218 /** Set `contrast', i.e. a fudge multiplication factor to make low-level signals easier to see,
219  *  between 0 and 256.
220  */
221 void
222 VideoWaveformPlot::set_contrast (int b)
223 {
224         _contrast = b;
225         _dirty = true;
226         Refresh ();
227 }
228
229 void
230 VideoWaveformPlot::mouse_moved (wxMouseEvent& ev)
231 {
232         if (!_image) {
233                 return;
234         }
235
236         if (_dirty) {
237                 create_waveform ();
238                 _dirty = false;
239         }
240
241         shared_ptr<const Film> film = _film.lock ();
242         if (!film) {
243                 return;
244         }
245
246         dcp::Size const full = film->frame_size ();
247
248         double const xs = static_cast<double> (full.width) / _waveform->size().width;
249         int const x1 = max (0, min (full.width - 1, int (floor (ev.GetPosition().x - _x_axis_width - 0.5) * xs)));
250         int const x2 = max (0, min (full.width - 1, int (floor (ev.GetPosition().x - _x_axis_width + 0.5) * xs)));
251
252         double const ys = static_cast<double> (_pixel_values) / _waveform->size().height;
253         int const fy = _waveform->size().height - (ev.GetPosition().y - _vertical_margin);
254         int const y1 = max (0, min (_pixel_values - 1, int (floor (fy - 0.5) * ys)));
255         int const y2 = max (0, min (_pixel_values - 1, int (floor (fy + 0.5) * ys)));
256
257         MouseMoved (x1, x2, y1, y2);
258 }