Lots of #include <iostream>s for Arch.
[dcpomatic.git] / src / wx / video_waveform_plot.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "video_waveform_plot.h"
21 #include "film_viewer.h"
22 #include "wx_util.h"
23 #include "lib/image.h"
24 #include "lib/raw_convert.h"
25 #include "lib/dcp_video.h"
26 #include <dcp/openjpeg_image.h>
27 #include <wx/rawbmp.h>
28 #include <wx/graphics.h>
29 #include <boost/bind.hpp>
30 #include <iostream>
31
32 using std::cout;
33 using std::min;
34 using std::string;
35 using boost::weak_ptr;
36 using boost::shared_ptr;
37
38 int const VideoWaveformPlot::_vertical_margin = 8;
39
40 VideoWaveformPlot::VideoWaveformPlot (wxWindow* parent, FilmViewer* viewer)
41         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
42         , _dirty (true)
43         , _enabled (false)
44         , _component (0)
45         , _contrast (0)
46 {
47 #ifndef __WXOSX__
48         SetDoubleBuffered (true);
49 #endif
50
51         _viewer_connection = viewer->ImageChanged.connect (boost::bind (&VideoWaveformPlot::set_image, this, _1));
52
53         Bind (wxEVT_PAINT, boost::bind (&VideoWaveformPlot::paint, this));
54         Bind (wxEVT_SIZE,  boost::bind (&VideoWaveformPlot::sized, this, _1));
55
56         SetMinSize (wxSize (640, 512));
57         SetBackgroundColour (wxColour (0, 0, 0));
58 }
59
60 void
61 VideoWaveformPlot::paint ()
62 {
63         wxPaintDC dc (this);
64
65         if (_dirty) {
66                 create_waveform ();
67                 _dirty = false;
68         }
69
70         if (!_waveform) {
71                 return;
72         }
73
74         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
75         if (!gc) {
76                 return;
77         }
78
79         int const axis_x = 48;
80         int const height = _waveform->size().height;
81
82         gc->SetPen (wxPen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
83
84         gc->SetFont (gc->CreateFont (*wxSMALL_FONT, wxColour (255, 255, 255)));
85         double label_width;
86         double label_height;
87         double label_descent;
88         double label_leading;
89         gc->GetTextExtent (wxT ("1024"), &label_width, &label_height, &label_descent, &label_leading);
90
91         double extra[3];
92         double w;
93         gc->GetTextExtent (wxT ("0"), &w, &label_height, &label_descent, &label_leading);
94         extra[0] = label_width - w;
95         gc->GetTextExtent (wxT ("64"), &w, &label_height, &label_descent, &label_leading);
96         extra[1] = label_width - w;
97         gc->GetTextExtent (wxT ("512"), &w, &label_height, &label_descent, &label_leading);
98         extra[2] = label_width - w;
99
100         int label_gaps = 2;
101         while (height / label_gaps > 64) {
102                 label_gaps *= 2;
103         }
104
105         for (int i = 0; i < label_gaps + 1; ++i) {
106                 wxGraphicsPath p = gc->CreatePath ();
107                 int const y = _vertical_margin + height - (i * height / label_gaps) - 1;
108                 p.MoveToPoint (label_width + 8, y);
109                 p.AddLineToPoint (axis_x, y);
110                 gc->StrokePath (p);
111                 int x = 4;
112                 int const n = i * 4096 / label_gaps;
113                 if (n < 10) {
114                         x += extra[0];
115                 } else if (n < 100) {
116                         x += extra[1];
117                 } else if (n < 1000) {
118                         x += extra[2];
119                 }
120                 gc->DrawText (std_to_wx (raw_convert<string> (n)), x, y - (label_height / 2));
121         }
122
123         wxImage waveform (_waveform->size().width, height, _waveform->data()[0], true);
124         wxBitmap bitmap (waveform);
125         gc->DrawBitmap (bitmap, axis_x + 4, _vertical_margin, _waveform->size().width, height);
126
127         delete gc;
128 }
129
130 void
131 VideoWaveformPlot::create_waveform ()
132 {
133         _waveform.reset ();
134
135         if (!_image) {
136                 return;
137         }
138
139         dcp::Size const size = _image->size();
140         _waveform.reset (new Image (PIX_FMT_RGB24, dcp::Size (size.width, size.height), true));
141
142         for (int x = 0; x < size.width; ++x) {
143
144                 /* Work out one vertical `slice' of waveform pixels.  Each value in
145                    strip is the number of samples in image with the corresponding group of
146                    values.
147                 */
148                 int strip[size.height];
149                 for (int i = 0; i < size.height; ++i) {
150                         strip[i] = 0;
151                 }
152
153                 int* ip = _image->data (_component) + x;
154                 for (int y = 0; y < size.height; ++y) {
155                         strip[*ip * size.height / 4096]++;
156                         ip += size.width;
157                 }
158
159                 /* Copy slice into the waveform */
160                 uint8_t* wp = _waveform->data()[0] + x * 3;
161                 for (int y = size.height - 1; y >= 0; --y) {
162                         wp[0] = wp[1] = wp[2] = min (255, (strip[y] * 255 / size.height) * _contrast);
163                         wp += _waveform->stride()[0];
164                 }
165         }
166
167         _waveform = _waveform->scale (
168                 dcp::Size (GetSize().GetWidth() - 32, GetSize().GetHeight() - _vertical_margin * 2),
169                 dcp::YUV_TO_RGB_REC709, PIX_FMT_RGB24, false
170                 );
171 }
172
173 static void
174 note ()
175 {
176
177 }
178
179 void
180 VideoWaveformPlot::set_image (weak_ptr<PlayerVideo> image)
181 {
182         if (!_enabled) {
183                 return;
184         }
185
186         shared_ptr<PlayerVideo> pv = image.lock ();
187         _image = DCPVideo::convert_to_xyz (pv, boost::bind (&note));
188         _dirty = true;
189         Refresh ();
190 }
191
192 void
193 VideoWaveformPlot::sized (wxSizeEvent &)
194 {
195         _dirty = true;
196 }
197
198 void
199 VideoWaveformPlot::set_enabled (bool e)
200 {
201         _enabled = e;
202 }
203
204 void
205 VideoWaveformPlot::set_component (int c)
206 {
207         _component = c;
208         _dirty = true;
209         Refresh ();
210 }
211
212 /** Set `contrast', i.e. a fudge multiplication factor to make low-level signals easier to see,
213  *  between 0 and 256.
214  */
215 void
216 VideoWaveformPlot::set_contrast (int b)
217 {
218         _contrast = b;
219         _dirty = true;
220         Refresh ();
221 }