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