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