Fix crash on using delay; fix x-thread GUI access caused by FilmState default copy...
[dcpomatic.git] / src / wx / film_viewer.cc
index 6ffe4e66a847f2f92aa4ccd7a9638df2c2d797a0..5262338332e7073884b37bbc398c9d007eb057ca 100644 (file)
 #include "lib/film.h"
 #include "lib/format.h"
 #include "lib/util.h"
-#include "lib/thumbs_job.h"
 #include "lib/job_manager.h"
 #include "lib/film_state.h"
 #include "lib/options.h"
+#include "lib/subtitle.h"
 #include "film_viewer.h"
 #include "wx_util.h"
 
@@ -42,30 +42,43 @@ public:
        ThumbPanel (wxPanel* parent, Film* film)
                : wxPanel (parent)
                , _film (film)
-               , _image (0)
-               , _bitmap (0)
-       {
-       }
+               , _frame_rebuild_needed (false)
+               , _composition_needed (false)
+       {}
 
        /** 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;
-                       setup ();
+               if (!_film || _film->thumbs().size() == 0) {
+                       wxPaintDC dc (this);
+                       return;
                }
 
-               if (_current_crop != _pending_crop) {
-                       _current_crop = _pending_crop;
-                       setup ();
+               if (_frame_rebuild_needed) {
+                       _image.reset (new wxImage (std_to_wx (_film->thumb_file (_index))));
+
+                       _subtitle.reset ();
+                       pair<Position, string> s = _film->thumb_subtitle (_index);
+                       if (!s.second.empty ()) {
+                               _subtitle.reset (new SubtitleView (s.first, std_to_wx (s.second)));
+                       }
+
+                       _frame_rebuild_needed = false;
+                       compose ();
+               }
+
+               if (_composition_needed) {
+                       compose ();
                }
 
                wxPaintDC dc (this);
                if (_bitmap) {
                        dc.DrawBitmap (*_bitmap, 0, 0, false);
                }
+
+               if (_film->with_subtitles() && _subtitle) {
+                       dc.DrawBitmap (*_subtitle->bitmap, _subtitle->transformed_area.x, _subtitle->transformed_area.y, true);
+               }
        }
 
        /** Handle a size event */
@@ -75,19 +88,14 @@ public:
                        return;
                }
 
-               setup ();
-               Refresh ();
+               recompose ();
        }
 
-       void set (string f)
+       /** @param n Thumbnail index */
+       void set (int n)
        {
-               _pending_image = f;
-               Refresh ();
-       }
-
-       void set_crop (Crop c)
-       {
-               _pending_crop = c;
+               _index = n;
+               _frame_rebuild_needed = true;
                Refresh ();
        }
 
@@ -96,9 +104,10 @@ public:
                _film = f;
                if (!_film) {
                        clear ();
+                       _frame_rebuild_needed = true;
                        Refresh ();
                } else {
-                       setup ();
+                       _frame_rebuild_needed = true;
                        Refresh ();
                }
        }
@@ -106,15 +115,14 @@ public:
        /** Clear our thumbnail image */
        void clear ()
        {
-               delete _bitmap;
-               _bitmap = 0;
-               delete _image;
-               _image = 0;
+               _bitmap.reset ();
+               _image.reset ();
+               _subtitle.reset ();
        }
 
-       void refresh ()
+       void recompose ()
        {
-               setup ();
+               _composition_needed = true;
                Refresh ();
        }
 
@@ -122,46 +130,92 @@ public:
 
 private:
 
-       void setup ()
+       void compose ()
        {
+               _composition_needed = false;
+               
                if (!_film || !_image) {
                        return;
                }
-               
+
+               /* Size of the view */
                int vw, vh;
                GetSize (&vw, &vh);
 
-               float const target = _film->format() ? _film->format()->ratio_as_float (_film) : 1.78;
+               Crop const fc = _film->crop ();
 
-               _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 */
+               Rect cropped_area (
+                       fc.left,
+                       fc.top,
+                       _image->GetWidth() - (fc.left + fc.right),
+                       _image->GetHeight() - (fc.top + fc.bottom)
                        );
 
+               /* Target ratio */
+               float const target = _film->format() ? _film->format()->ratio_as_float (_film) : 1.78;
+
+               _transformed_image = _image->GetSubImage (wxRect (cropped_area.x, cropped_area.y, cropped_area.width, cropped_area.height));
+
+               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);
+                       _transformed_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
+                       x_scale = vh * target / cropped_area.width;
+                       y_scale = float (vh) / cropped_area.height;
                } else {
                        /* view is shorter (horizontally) than the ratio; fit width */
-                       _cropped_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
+                       _transformed_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
+                       x_scale = float (vw) / cropped_area.width;
+                       y_scale = (vw / target) / cropped_area.height;
                }
 
-               delete _bitmap;
-               _bitmap = new wxBitmap (_cropped_image);
+               _bitmap.reset (new wxBitmap (_transformed_image));
+
+               if (_subtitle) {
+
+                       _subtitle->transformed_area = subtitle_transformed_area (
+                               x_scale, y_scale, _subtitle->base_area, _film->subtitle_offset(), _film->subtitle_scale()
+                               );
+
+                       _subtitle->transformed_image = _subtitle->base_image;
+                       _subtitle->transformed_image.Rescale (_subtitle->transformed_area.width, _subtitle->transformed_area.height, wxIMAGE_QUALITY_HIGH);
+                       _subtitle->transformed_area.x -= rint (_film->crop().left * x_scale);
+                       _subtitle->transformed_area.y -= rint (_film->crop().top * y_scale);
+                       _subtitle->bitmap.reset (new wxBitmap (_subtitle->transformed_image));
+               }
        }
 
        Film* _film;
-       wxImage* _image;
-       std::string _current_image;
-       std::string _pending_image;
-       wxImage _cropped_image;
-       wxBitmap* _bitmap;
-       Crop _current_crop;
-       Crop _pending_crop;
+       shared_ptr<wxImage> _image;
+       wxImage _transformed_image;
+       /** currently-displayed thumbnail index */
+       int _index;
+       shared_ptr<wxBitmap> _bitmap;
+       bool _frame_rebuild_needed;
+       bool _composition_needed;
+
+       struct SubtitleView
+       {
+               SubtitleView (Position p, wxString const & i)
+                       : base_image (i)
+               {
+                       base_area.x = p.x;
+                       base_area.y = p.y;
+                       base_area.width = base_image.GetWidth ();
+                       base_area.height = base_image.GetHeight ();
+               }
+
+               Rect base_area;
+               Rect transformed_area;
+               wxImage base_image;
+               wxImage transformed_image;
+               shared_ptr<wxBitmap> bitmap;
+       };
+
+       shared_ptr<SubtitleView> _subtitle;
 };
 
 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
@@ -179,8 +233,8 @@ FilmViewer::FilmViewer (Film* f, wxWindow* p)
        _thumb_panel = new ThumbPanel (this, f);
        _sizer->Add (_thumb_panel, 1, wxEXPAND);
 
-       int const max = f ? f->num_thumbs() - 1 : 0;
-       _slider = new wxSlider (this, wxID_ANY, 0, 0, max);
+       int const m = max ((size_t) 1, f ? f->thumbs().size() - 1 : 0);
+       _slider = new wxSlider (this, wxID_ANY, 0, 0, m);
        _sizer->Add (_slider, 0, wxEXPAND | wxLEFT | wxRIGHT);
        set_thumbnail (0);
 
@@ -192,11 +246,11 @@ FilmViewer::FilmViewer (Film* f, wxWindow* p)
 void
 FilmViewer::set_thumbnail (int n)
 {
-       if (_film == 0 || _film->num_thumbs() <= n) {
+       if (_film == 0 || int (_film->thumbs().size()) <= n) {
                return;
        }
 
-       _thumb_panel->set (_film->thumb_file(n));
+       _thumb_panel->set (n);
 }
 
 void
@@ -206,13 +260,14 @@ FilmViewer::slider_changed (wxCommandEvent &)
 }
 
 void
-FilmViewer::film_changed (Film::Property p)
+FilmViewer::film_changed (FilmState::Property p)
 {
-       if (p == Film::CROP) {
-               _thumb_panel->set_crop (_film->crop ());
-       } else if (p == Film::THUMBS) {
-               if (_film && _film->num_thumbs() > 1) {
-                       _slider->SetRange (0, _film->num_thumbs () - 1);
+       ensure_ui_thread ();
+       
+       switch (p) {
+       case FilmState::THUMBS:
+               if (_film && _film->thumbs().size() > 1) {
+                       _slider->SetRange (0, _film->thumbs().size() - 1);
                } else {
                        _thumb_panel->clear ();
                        _slider->SetRange (0, 1);
@@ -220,12 +275,19 @@ FilmViewer::film_changed (Film::Property p)
                
                _slider->SetValue (0);
                set_thumbnail (0);
-       } else if (p == Film::FORMAT) {
-               _thumb_panel->refresh ();
-       } else if (p == Film::CONTENT) {
+               break;
+       case FilmState::CONTENT:
                setup_visibility ();
-               _film->examine_content ();
-               update_thumbs ();
+               break;
+       case FilmState::CROP:
+       case FilmState::FORMAT:
+       case FilmState::WITH_SUBTITLES:
+       case FilmState::SUBTITLE_OFFSET:
+       case FilmState::SUBTITLE_SCALE:
+               _thumb_panel->recompose ();
+               break;
+       default:
+               break;
        }
 }
 
@@ -246,31 +308,9 @@ FilmViewer::set_film (Film* f)
        _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
        film_changed (Film::CROP);
        film_changed (Film::THUMBS);
-       _thumb_panel->refresh ();
        setup_visibility ();
 }
 
-void
-FilmViewer::update_thumbs ()
-{
-       if (!_film) {
-               return;
-       }
-
-       _film->update_thumbs_pre_gui ();
-
-       shared_ptr<const FilmState> s = _film->state_copy ();
-       shared_ptr<Options> o (new Options (s->dir ("thumbs"), ".tiff", ""));
-       o->out_size = _film->size ();
-       o->apply_crop = false;
-       o->decode_audio = false;
-       o->decode_video_frequency = 128;
-       
-       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);
-}
-
 void
 FilmViewer::setup_visibility ()
 {