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/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>
36 using boost::weak_ptr;
37 using boost::shared_ptr;
39 int const VideoWaveformPlot::_vertical_margin = 8;
41 VideoWaveformPlot::VideoWaveformPlot (wxWindow* parent, FilmViewer* viewer)
42 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
49 SetDoubleBuffered (true);
52 _viewer_connection = viewer->ImageChanged.connect (boost::bind (&VideoWaveformPlot::set_image, this, _1));
54 Bind (wxEVT_PAINT, boost::bind (&VideoWaveformPlot::paint, this));
55 Bind (wxEVT_SIZE, boost::bind (&VideoWaveformPlot::sized, this, _1));
57 SetMinSize (wxSize (640, 512));
58 SetBackgroundColour (wxColour (0, 0, 0));
62 VideoWaveformPlot::paint ()
75 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
80 int const axis_x = 48;
81 int const height = _waveform->size().height;
83 gc->SetPen (wxPen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
85 gc->SetFont (gc->CreateFont (*wxSMALL_FONT, wxColour (255, 255, 255)));
90 gc->GetTextExtent (wxT ("1024"), &label_width, &label_height, &label_descent, &label_leading);
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;
102 while (height / label_gaps > 64) {
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);
113 int const n = i * 4096 / label_gaps;
116 } else if (n < 100) {
118 } else if (n < 1000) {
121 gc->DrawText (std_to_wx (raw_convert<string> (n)), x, y - (label_height / 2));
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);
132 VideoWaveformPlot::create_waveform ()
140 dcp::Size const size = _image->size();
141 _waveform.reset (new Image (AV_PIX_FMT_RGB24, dcp::Size (size.width, size.height), true));
143 for (int x = 0; x < size.width; ++x) {
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
149 int strip[size.height];
150 for (int i = 0; i < size.height; ++i) {
154 int* ip = _image->data (_component) + x;
155 for (int y = 0; y < size.height; ++y) {
156 strip[*ip * size.height / 4096]++;
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];
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
181 VideoWaveformPlot::set_image (weak_ptr<PlayerVideo> image)
187 shared_ptr<PlayerVideo> pv = image.lock ();
188 _image = DCPVideo::convert_to_xyz (pv, boost::bind (¬e));
194 VideoWaveformPlot::sized (wxSizeEvent &)
200 VideoWaveformPlot::set_enabled (bool e)
206 VideoWaveformPlot::set_component (int c)
213 /** Set `contrast', i.e. a fudge multiplication factor to make low-level signals easier to see,
217 VideoWaveformPlot::set_contrast (int b)