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