2 Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "video_waveform_plot.h"
21 #include "film_viewer.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>
35 using boost::weak_ptr;
36 using boost::shared_ptr;
38 int const VideoWaveformPlot::_vertical_margin = 8;
40 VideoWaveformPlot::VideoWaveformPlot (wxWindow* parent, FilmViewer* viewer)
41 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
48 SetDoubleBuffered (true);
51 _viewer_connection = viewer->ImageChanged.connect (boost::bind (&VideoWaveformPlot::set_image, this, _1));
53 Bind (wxEVT_PAINT, boost::bind (&VideoWaveformPlot::paint, this));
54 Bind (wxEVT_SIZE, boost::bind (&VideoWaveformPlot::sized, this, _1));
56 SetMinSize (wxSize (640, 512));
57 SetBackgroundColour (wxColour (0, 0, 0));
61 VideoWaveformPlot::paint ()
74 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
79 int const axis_x = 48;
80 int const height = _waveform->size().height;
82 gc->SetPen (wxPen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
84 gc->SetFont (gc->CreateFont (*wxSMALL_FONT, wxColour (255, 255, 255)));
89 gc->GetTextExtent (wxT ("1024"), &label_width, &label_height, &label_descent, &label_leading);
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;
101 while (height / label_gaps > 64) {
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);
112 int const n = i * 4096 / label_gaps;
115 } else if (n < 100) {
117 } else if (n < 1000) {
120 gc->DrawText (std_to_wx (raw_convert<string> (n)), x, y - (label_height / 2));
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);
131 VideoWaveformPlot::create_waveform ()
139 dcp::Size const size = _image->size();
140 _waveform.reset (new Image (AV_PIX_FMT_RGB24, dcp::Size (size.width, size.height), true));
142 for (int x = 0; x < size.width; ++x) {
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
148 int strip[size.height];
149 for (int i = 0; i < size.height; ++i) {
153 int* ip = _image->data (_component) + x;
154 for (int y = 0; y < size.height; ++y) {
155 strip[*ip * size.height / 4096]++;
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];
167 _waveform = _waveform->scale (
168 dcp::Size (GetSize().GetWidth() - 32, GetSize().GetHeight() - _vertical_margin * 2),
169 dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, false
180 VideoWaveformPlot::set_image (weak_ptr<PlayerVideo> image)
186 shared_ptr<PlayerVideo> pv = image.lock ();
187 _image = DCPVideo::convert_to_xyz (pv, boost::bind (¬e));
193 VideoWaveformPlot::sized (wxSizeEvent &)
199 VideoWaveformPlot::set_enabled (bool e)
205 VideoWaveformPlot::set_component (int c)
212 /** Set `contrast', i.e. a fudge multiplication factor to make low-level signals easier to see,
216 VideoWaveformPlot::set_contrast (int b)