Merge branch 'master' of /home/carl/git/dvdomatic
[dcpomatic.git] / src / wx / film_viewer.cc
1 /*
2     Copyright (C) 2012 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 /** @file  src/film_viewer.cc
21  *  @brief A wx widget to view `thumbnails' of a Film.
22  */
23
24 #include <iostream>
25 #include <iomanip>
26 #include "lib/film.h"
27 #include "lib/format.h"
28 #include "lib/util.h"
29 #include "lib/thumbs_job.h"
30 #include "lib/job_manager.h"
31 #include "lib/film_state.h"
32 #include "lib/options.h"
33 #include "lib/subtitle.h"
34 #include "film_viewer.h"
35 #include "wx_util.h"
36
37 using namespace std;
38 using namespace boost;
39
40 class ThumbPanel : public wxPanel
41 {
42 public:
43         ThumbPanel (wxPanel* parent, Film* film)
44                 : wxPanel (parent)
45                 , _film (film)
46                 , _frame_rebuild_needed (false)
47                 , _composition_needed (false)
48         {}
49
50         /** Handle a paint event */
51         void paint_event (wxPaintEvent& ev)
52         {
53                 if (!_film || _film->num_thumbs() == 0) {
54                         wxPaintDC dc (this);
55                         return;
56                 }
57
58                 if (_frame_rebuild_needed) {
59                         _image.reset (new wxImage (std_to_wx (_film->thumb_file (_index))));
60
61                         _subtitles.clear ();
62                         list<pair<Position, string> > s = _film->thumb_subtitles (_index);
63                         for (list<pair<Position, string> >::iterator i = s.begin(); i != s.end(); ++i) {
64                                 _subtitles.push_back (SubtitleView (i->first, std_to_wx (i->second)));
65                         }
66
67                         _frame_rebuild_needed = false;
68
69                         compose ();
70                         _composition_needed = false;
71                 }
72
73                 if (_composition_needed) {
74                         compose ();
75                         _composition_needed = false;
76                 }
77
78                 wxPaintDC dc (this);
79                 if (_bitmap) {
80                         dc.DrawBitmap (*_bitmap, 0, 0, false);
81                 }
82
83                 if (_film->with_subtitles ()) {
84                         for (list<SubtitleView>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
85                                 dc.DrawBitmap (*i->bitmap, i->transformed_area.x, i->transformed_area.y, true);
86                         }
87                 }
88         }
89
90         /** Handle a size event */
91         void size_event (wxSizeEvent &)
92         {
93                 if (!_image) {
94                         return;
95                 }
96
97                 recompose ();
98         }
99
100         /** @param n Thumbnail index */
101         void set (int n)
102         {
103                 _index = n;
104                 _frame_rebuild_needed = true;
105                 Refresh ();
106         }
107
108         void set_film (Film* f)
109         {
110                 _film = f;
111                 if (!_film) {
112                         clear ();
113                         _frame_rebuild_needed = true;
114                         Refresh ();
115                 } else {
116                         _frame_rebuild_needed = true;
117                         Refresh ();
118                 }
119         }
120
121         /** Clear our thumbnail image */
122         void clear ()
123         {
124                 _bitmap.reset ();
125                 _image.reset ();
126                 _subtitles.clear ();
127         }
128
129         void recompose ()
130         {
131                 _composition_needed = true;
132                 Refresh ();
133         }
134
135         DECLARE_EVENT_TABLE ();
136
137 private:
138
139         void compose ()
140         {
141                 if (!_film || !_image) {
142                         return;
143                 }
144
145                 /* Size of the view */
146                 int vw, vh;
147                 GetSize (&vw, &vh);
148
149                 /* Cropped rectangle */
150                 Rectangle cropped_area (
151                         _film->crop().left,
152                         _film->crop().top,
153                         _image->GetWidth() - (_film->crop().left + _film->crop().right),
154                         _image->GetHeight() - (_film->crop().top + _film->crop().bottom)
155                         );
156
157                 /* Target ratio */
158                 float const target = _film->format() ? _film->format()->ratio_as_float (_film) : 1.78;
159
160                 _transformed_image = _image->GetSubImage (wxRect (cropped_area.x, cropped_area.y, cropped_area.w, cropped_area.h));
161
162                 float x_scale = 1;
163                 float y_scale = 1;
164
165                 if ((float (vw) / vh) > target) {
166                         /* view is longer (horizontally) than the ratio; fit height */
167                         _transformed_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
168                         x_scale = vh * target / cropped_area.w;
169                         y_scale = float (vh) / cropped_area.h;
170                 } else {
171                         /* view is shorter (horizontally) than the ratio; fit width */
172                         _transformed_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
173                         x_scale = float (vw) / cropped_area.w;
174                         y_scale = (vw / target) / cropped_area.h;
175                 }
176
177                 _bitmap.reset (new wxBitmap (_transformed_image));
178
179                 for (list<SubtitleView>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
180
181                         i->transformed_area = transformed_subtitle_area (
182                                 x_scale, y_scale, i->base_area, _film->subtitle_offset(), _film->subtitle_scale()
183                                 );
184
185                         i->transformed_image = i->base_image;
186                         i->transformed_image.Rescale (i->transformed_area.w, i->transformed_area.h, wxIMAGE_QUALITY_HIGH);
187                         i->transformed_area.x -= _film->crop().left;
188                         i->transformed_area.y -= _film->crop().top;
189                         i->bitmap.reset (new wxBitmap (i->transformed_image));
190                 }
191         }
192
193         Film* _film;
194         shared_ptr<wxImage> _image;
195         wxImage _transformed_image;
196         /** currently-displayed thumbnail index */
197         int _index;
198         shared_ptr<wxBitmap> _bitmap;
199         bool _frame_rebuild_needed;
200         bool _composition_needed;
201
202         struct SubtitleView
203         {
204                 SubtitleView (Position p, wxString const & i)
205                         : base_image (i)
206                 {
207                         base_area.x = p.x;
208                         base_area.y = p.y;
209                         base_area.w = base_image.GetWidth ();
210                         base_area.h = base_image.GetHeight ();
211                 }
212
213                 Rectangle base_area;
214                 Rectangle transformed_area;
215                 wxImage base_image;
216                 wxImage transformed_image;
217                 shared_ptr<wxBitmap> bitmap;
218         };
219
220         list<SubtitleView> _subtitles;
221 };
222
223 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
224 EVT_PAINT (ThumbPanel::paint_event)
225 EVT_SIZE (ThumbPanel::size_event)
226 END_EVENT_TABLE ()
227
228 FilmViewer::FilmViewer (Film* f, wxWindow* p)
229         : wxPanel (p)
230         , _film (0)
231 {
232         _sizer = new wxBoxSizer (wxVERTICAL);
233         SetSizer (_sizer);
234         
235         _thumb_panel = new ThumbPanel (this, f);
236         _sizer->Add (_thumb_panel, 1, wxEXPAND);
237
238         int const max = f ? f->num_thumbs() - 1 : 0;
239         _slider = new wxSlider (this, wxID_ANY, 0, 0, max);
240         _sizer->Add (_slider, 0, wxEXPAND | wxLEFT | wxRIGHT);
241         set_thumbnail (0);
242
243         _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
244
245         set_film (_film);
246 }
247
248 void
249 FilmViewer::set_thumbnail (int n)
250 {
251         if (_film == 0 || _film->num_thumbs() <= n) {
252                 return;
253         }
254
255         _thumb_panel->set (n);
256 }
257
258 void
259 FilmViewer::slider_changed (wxCommandEvent &)
260 {
261         set_thumbnail (_slider->GetValue ());
262 }
263
264 void
265 FilmViewer::film_changed (Film::Property p)
266 {
267         switch (p) {
268         case Film::THUMBS:
269                 if (_film && _film->num_thumbs() > 1) {
270                         _slider->SetRange (0, _film->num_thumbs () - 1);
271                 } else {
272                         _thumb_panel->clear ();
273                         _slider->SetRange (0, 1);
274                 }
275                 
276                 _slider->SetValue (0);
277                 set_thumbnail (0);
278                 break;
279         case Film::CONTENT:
280                 setup_visibility ();
281                 _film->examine_content ();
282                 update_thumbs ();
283                 break;
284         case Film::CROP:
285         case Film::FORMAT:
286         case Film::WITH_SUBTITLES:
287         case Film::SUBTITLE_OFFSET:
288         case Film::SUBTITLE_SCALE:
289                 _thumb_panel->recompose ();
290                 break;
291         default:
292                 break;
293         }
294 }
295
296 void
297 FilmViewer::set_film (Film* f)
298 {
299         if (_film == f) {
300                 return;
301         }
302         
303         _film = f;
304         _thumb_panel->set_film (_film);
305
306         if (!_film) {
307                 return;
308         }
309
310         _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
311         film_changed (Film::CROP);
312         film_changed (Film::THUMBS);
313         setup_visibility ();
314 }
315
316 void
317 FilmViewer::update_thumbs ()
318 {
319         if (!_film) {
320                 return;
321         }
322
323         _film->update_thumbs_pre_gui ();
324
325         shared_ptr<const FilmState> s = _film->state_copy ();
326         shared_ptr<Options> o (new Options (s->dir ("thumbs"), ".png", ""));
327         o->out_size = _film->size ();
328         o->apply_crop = false;
329         o->decode_audio = false;
330         o->decode_video_frequency = 128;
331         o->decode_subtitles = true;
332         
333         shared_ptr<Job> j (new ThumbsJob (s, o, _film->log(), shared_ptr<Job> ()));
334         j->Finished.connect (sigc::mem_fun (_film, &Film::update_thumbs_post_gui));
335         JobManager::instance()->add (j);
336 }
337
338 void
339 FilmViewer::setup_visibility ()
340 {
341         if (!_film) {
342                 return;
343         }
344
345         ContentType const c = _film->content_type ();
346         _slider->Show (c == VIDEO);
347 }