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