Thumbs sort of have subs.
[dcpomatic.git] / src / wx / film_viewer.cc
index 56f20449b1ec3abfe686a177aac4423b99b7098d..8c298ea57d3087915204efaa2b5f826ff5c0a150 100644 (file)
@@ -42,17 +42,24 @@ public:
        ThumbPanel (wxPanel* parent, Film* film)
                : wxPanel (parent)
                , _film (film)
-               , _image (0)
-               , _bitmap (0)
        {
        }
 
+       /** Handle a paint event */
        void paint_event (wxPaintEvent& ev)
        {
-               if (_current_image != _pending_image) {
-                       delete _image;
-                       _image = new wxImage (std_to_wx (_pending_image));
-                       _current_image = _pending_image;
+               if (_current_index != _pending_index) {
+                       _image.reset (new wxImage (std_to_wx (_film->thumb_file (_pending_index))));
+                       _current_index = _pending_index;
+
+                       _subtitles.clear ();
+
+                       cout << "=== SUBS for " << _film->thumb_frame (_pending_index) << "\n";
+                       list<pair<Position, string> > s = _film->thumb_subtitles (_pending_index);
+                       for (list<pair<Position, string> >::iterator i = s.begin(); i != s.end(); ++i) {
+                               _subtitles.push_back (SubtitleView (i->first, std_to_wx (i->second)));
+                       }
+
                        setup ();
                }
 
@@ -63,10 +70,17 @@ public:
 
                wxPaintDC dc (this);
                if (_bitmap) {
+                       cout << "frame bm " << _bitmap->GetWidth() << " " << _bitmap->GetHeight() << "\n";
                        dc.DrawBitmap (*_bitmap, 0, 0, false);
                }
+
+               for (list<SubtitleView>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
+                       dc.DrawBitmap (*i->bitmap, i->position.x, i->position.y, true);
+                       cout << "\tsub bm at " << i->position.x << " " << i->position.y << " size " << i->bitmap->GetWidth() << " by " << i->bitmap->GetHeight() << "\n";
+               }
        }
 
+       /** Handle a size event */
        void size_event (wxSizeEvent &)
        {
                if (!_image) {
@@ -77,9 +91,10 @@ public:
                Refresh ();
        }
 
-       void set (string f)
+       /** @param n Thumbnail index */
+       void set (int n)
        {
-               _pending_image = f;
+               _pending_index = n;
                Refresh ();
        }
 
@@ -101,12 +116,12 @@ public:
                }
        }
 
+       /** Clear our thumbnail image */
        void clear ()
        {
-               delete _bitmap;
-               _bitmap = 0;
-               delete _image;
-               _image = 0;
+               _bitmap.reset ();
+               _image.reset ();
+               _subtitles.clear ();
        }
 
        void refresh ()
@@ -124,41 +139,95 @@ private:
                if (!_film || !_image) {
                        return;
                }
-               
+
+               /* Size of the view */
                int vw, vh;
                GetSize (&vw, &vh);
 
-               float const target = _film->format() ? _film->format()->ratio_as_float () : 1.78;
-
-               _cropped_image = _image->GetSubImage (
-                       wxRect (
-                               _current_crop.left,
-                               _current_crop.top,
-                               _image->GetWidth() - (_current_crop.left + _current_crop.right),
-                               _image->GetHeight() - (_current_crop.top + _current_crop.bottom)
-                               )
+               /* Cropped rectangle */
+               Rectangle cropped (
+                       _current_crop.left,
+                       _current_crop.top,
+                       _image->GetWidth() - (_current_crop.left + _current_crop.right),
+                       _image->GetHeight() - (_current_crop.top + _current_crop.bottom)
                        );
 
+               /* Target ratio */
+               float const target = _film->format() ? _film->format()->ratio_as_float (_film) : 1.78;
+
+               _cropped_image = _image->GetSubImage (wxRect (cropped.x, cropped.y, cropped.w, cropped.h));
+
+               float x_scale = 1;
+               float y_scale = 1;
+
                if ((float (vw) / vh) > target) {
                        /* view is longer (horizontally) than the ratio; fit height */
                        _cropped_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
+                       x_scale = vh * target / _image->GetWidth ();
+                       y_scale = float (vh) / _image->GetHeight ();  
                } else {
                        /* view is shorter (horizontally) than the ratio; fit width */
                        _cropped_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
+                       x_scale = float (vw) / _image->GetWidth ();
+                       y_scale = (vw / target) / _image->GetHeight ();
                }
 
-               delete _bitmap;
-               _bitmap = new wxBitmap (_cropped_image);
+               _bitmap.reset (new wxBitmap (_cropped_image));
+
+               for (list<SubtitleView>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
+                       Rectangle sub_rect (i->position.x, i->position.y, i->image.GetWidth(), i->image.GetHeight());
+                       Rectangle cropped_sub_rect = sub_rect.intersection (cropped);
+
+                       cout << "sub " << sub_rect.x << " " << sub_rect.y << " " << sub_rect.w << " " << sub_rect.h << "\n";
+                       cout << "cropped " << cropped_sub_rect.x << " " << cropped_sub_rect.y << " " << cropped_sub_rect.w << " " << cropped_sub_rect.h << "\n";
+
+                       i->cropped_image = i->image.GetSubImage (
+                               wxRect (
+                                       cropped_sub_rect.x - sub_rect.x,
+                                       cropped_sub_rect.y - sub_rect.y,
+                                       cropped_sub_rect.w,
+                                       cropped_sub_rect.h
+                                       )
+                               );
+                       
+                       i->cropped_image.Rescale (cropped_sub_rect.w * x_scale, cropped_sub_rect.h * y_scale, wxIMAGE_QUALITY_HIGH);
+
+                       i->position = Position (
+                               cropped_sub_rect.x * x_scale,
+                               cropped_sub_rect.y * y_scale
+                               );
+
+                       cout << "scales are " << x_scale << " " << y_scale << "\n";
+                       cout << "scaled to " << (cropped_sub_rect.w * x_scale) << " " << (cropped_sub_rect.h * y_scale) << "\n";
+
+                       i->bitmap.reset (new wxBitmap (i->cropped_image));
+               }
        }
 
        Film* _film;
-       wxImage* _image;
-       std::string _current_image;
-       std::string _pending_image;
+       shared_ptr<wxImage> _image;
        wxImage _cropped_image;
-       wxBitmap* _bitmap;
+       /** currently-displayed thumbnail index */
+       int _current_index;
+       int _pending_index;
+       shared_ptr<wxBitmap> _bitmap;
        Crop _current_crop;
        Crop _pending_crop;
+
+       struct SubtitleView
+       {
+               SubtitleView (Position p, wxString const & i)
+                       : position (p)
+                       , image (i)
+               {}
+                             
+               Position position;
+               wxImage image;
+               wxImage cropped_image;
+               shared_ptr<wxBitmap> bitmap;
+       };
+
+       list<SubtitleView> _subtitles;
 };
 
 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
@@ -168,7 +237,7 @@ END_EVENT_TABLE ()
 
 FilmViewer::FilmViewer (Film* f, wxWindow* p)
        : wxPanel (p)
-       , _film (f)
+       , _film (0)
 {
        _sizer = new wxBoxSizer (wxVERTICAL);
        SetSizer (_sizer);
@@ -193,7 +262,7 @@ FilmViewer::set_thumbnail (int n)
                return;
        }
 
-       _thumb_panel->set (_film->thumb_file(n));
+       _thumb_panel->set (n);
 }
 
 void
@@ -229,6 +298,10 @@ FilmViewer::film_changed (Film::Property p)
 void
 FilmViewer::set_film (Film* f)
 {
+       if (_film == f) {
+               return;
+       }
+       
        _film = f;
        _thumb_panel->set_film (_film);
 
@@ -259,7 +332,7 @@ FilmViewer::update_thumbs ()
        o->decode_audio = false;
        o->decode_video_frequency = 128;
        
-       shared_ptr<Job> j (new ThumbsJob (s, o, _film->log ()));
+       shared_ptr<Job> j (new ThumbsJob (s, o, _film->log(), shared_ptr<Job> ()));
        j->Finished.connect (sigc::mem_fun (_film, &Film::update_thumbs_post_gui));
        JobManager::instance()->add (j);
 }