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