2 Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "video_waveform_plot.h"
22 #include "film_viewer.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>
37 using boost::weak_ptr;
38 using boost::shared_ptr;
39 using dcp::locale_convert;
41 int const VideoWaveformPlot::_vertical_margin = 8;
42 int const VideoWaveformPlot::_pixel_values = 4096;
43 int const VideoWaveformPlot::_x_axis_width = 52;
45 VideoWaveformPlot::VideoWaveformPlot (wxWindow* parent, weak_ptr<const Film> film, FilmViewer* viewer)
46 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
54 SetDoubleBuffered (true);
57 _viewer_connection = viewer->ImageChanged.connect (boost::bind (&VideoWaveformPlot::set_image, this, _1));
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));
63 SetMinSize (wxSize (640, 512));
64 SetBackgroundColour (wxColour (0, 0, 0));
68 VideoWaveformPlot::paint ()
81 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
86 int const height = _waveform->size().height;
88 gc->SetPen (wxPen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
90 gc->SetFont (gc->CreateFont (*wxSMALL_FONT, wxColour (255, 255, 255)));
95 gc->GetTextExtent (wxT ("1024"), &label_width, &label_height, &label_descent, &label_leading);
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;
107 while (height / label_gaps > 64) {
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);
118 int const n = i * _pixel_values / label_gaps;
121 } else if (n < 100) {
123 } else if (n < 1000) {
126 gc->DrawText (std_to_wx (locale_convert<string> (n)), x, y - (label_height / 2));
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);
137 VideoWaveformPlot::create_waveform ()
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));
149 for (int x = 0; x < image_size.width; ++x) {
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
155 int strip[waveform_height];
156 memset (strip, 0, waveform_height * sizeof(int));
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;
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];
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
185 VideoWaveformPlot::set_image (weak_ptr<PlayerVideo> image)
191 shared_ptr<PlayerVideo> pv = image.lock ();
192 _image = DCPVideo::convert_to_xyz (pv, boost::bind (¬e));
198 VideoWaveformPlot::sized (wxSizeEvent &)
204 VideoWaveformPlot::set_enabled (bool e)
210 VideoWaveformPlot::set_component (int c)
217 /** Set `contrast', i.e. a fudge multiplication factor to make low-level signals easier to see,
221 VideoWaveformPlot::set_contrast (int b)
229 VideoWaveformPlot::mouse_moved (wxMouseEvent& ev)
240 shared_ptr<const Film> film = _film.lock ();
245 dcp::Size const full = film->frame_size ();
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)));
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)));
256 MouseMoved (x1, x2, y1, y2);