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