Round image line sizes up to 32. Seems to help with LHS image corruption with both...
[dcpomatic.git] / src / wx / film_viewer.cc
index 6a992f0db9e305c7ab11840636f875c42509afc8..bf082adc2e534824eb6d769cc32fa5c43cb2be85 100644 (file)
@@ -30,6 +30,7 @@
 #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,29 +43,36 @@ public:
        ThumbPanel (wxPanel* parent, Film* film)
                : wxPanel (parent)
                , _film (film)
-       {
-       }
+               , _frame_rebuild_needed (false)
+               , _composition_needed (false)
+       {}
 
        /** Handle a paint event */
        void paint_event (wxPaintEvent& ev)
        {
-               if (_current_index != _pending_index) {
-                       _image.reset (new wxImage (std_to_wx (_film->thumb_file (_pending_index))));
-                       _current_index = _pending_index;
+               if (!_film || _film->num_thumbs() == 0) {
+                       wxPaintDC dc (this);
+                       return;
+               }
 
-                       _subtitles.clear ();
+               if (_frame_rebuild_needed) {
+                       _image.reset (new wxImage (std_to_wx (_film->thumb_file (_index))));
 
-                       list<pair<Position, string> > s = _film->thumb_subtitles (_pending_index);
+                       _subtitles.clear ();
+                       list<pair<Position, string> > s = _film->thumb_subtitles (_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 ();
+                       _frame_rebuild_needed = false;
+
+                       compose ();
+                       _composition_needed = false;
                }
 
-               if (_current_crop != _pending_crop) {
-                       _current_crop = _pending_crop;
-                       setup ();
+               if (_composition_needed) {
+                       compose ();
+                       _composition_needed = false;
                }
 
                wxPaintDC dc (this);
@@ -74,7 +82,7 @@ public:
 
                if (_film->with_subtitles ()) {
                        for (list<SubtitleView>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
-                               dc.DrawBitmap (*i->bitmap, i->position.x, i->position.y, true);
+                               dc.DrawBitmap (*i->bitmap, i->transformed_area.x, i->transformed_area.y, true);
                        }
                }
        }
@@ -86,20 +94,14 @@ public:
                        return;
                }
 
-               setup ();
-               Refresh ();
+               recompose ();
        }
 
        /** @param n Thumbnail index */
        void set (int n)
        {
-               _pending_index = n;
-               Refresh ();
-       }
-
-       void set_crop (Crop c)
-       {
-               _pending_crop = c;
+               _index = n;
+               _frame_rebuild_needed = true;
                Refresh ();
        }
 
@@ -108,9 +110,10 @@ public:
                _film = f;
                if (!_film) {
                        clear ();
+                       _frame_rebuild_needed = true;
                        Refresh ();
                } else {
-                       setup ();
+                       _frame_rebuild_needed = true;
                        Refresh ();
                }
        }
@@ -123,9 +126,9 @@ public:
                _subtitles.clear ();
        }
 
-       void refresh ()
+       void recompose ()
        {
-               setup ();
+               _composition_needed = true;
                Refresh ();
        }
 
@@ -133,7 +136,7 @@ public:
 
 private:
 
-       void setup ()
+       void compose ()
        {
                if (!_film || !_image) {
                        return;
@@ -144,79 +147,73 @@ private:
                GetSize (&vw, &vh);
 
                /* 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)
+               Rectangle cropped_area (
+                       _film->crop().left,
+                       _film->crop().top,
+                       _image->GetWidth() - (_film->crop().left + _film->crop().right),
+                       _image->GetHeight() - (_film->crop().top + _film->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));
+               _transformed_image = _image->GetSubImage (wxRect (cropped_area.x, cropped_area.y, cropped_area.w, cropped_area.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 ();  
+                       _transformed_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
+                       x_scale = vh * target / cropped_area.w;
+                       y_scale = float (vh) / cropped_area.h;
                } 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 ();
+                       _transformed_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
+                       x_scale = float (vw) / cropped_area.w;
+                       y_scale = (vw / target) / cropped_area.h;
                }
 
-               _bitmap.reset (new wxBitmap (_cropped_image));
+               _bitmap.reset (new wxBitmap (_transformed_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);
-
-                       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
+                       i->transformed_area = transformed_subtitle_area (
+                               x_scale, y_scale, i->base_area, _film->subtitle_offset(), _film->subtitle_scale()
                                );
 
-                       i->bitmap.reset (new wxBitmap (i->cropped_image));
+                       i->transformed_image = i->base_image;
+                       i->transformed_image.Rescale (i->transformed_area.w, i->transformed_area.h, wxIMAGE_QUALITY_HIGH);
+                       i->transformed_area.x -= _film->crop().left;
+                       i->transformed_area.y -= _film->crop().top;
+                       i->bitmap.reset (new wxBitmap (i->transformed_image));
                }
        }
 
        Film* _film;
        shared_ptr<wxImage> _image;
-       wxImage _cropped_image;
+       wxImage _transformed_image;
        /** currently-displayed thumbnail index */
-       int _current_index;
-       int _pending_index;
+       int _index;
        shared_ptr<wxBitmap> _bitmap;
-       Crop _current_crop;
-       Crop _pending_crop;
+       bool _frame_rebuild_needed;
+       bool _composition_needed;
 
        struct SubtitleView
        {
                SubtitleView (Position p, wxString const & i)
-                       : position (p)
-                       , image (i)
-               {}
-                             
-               Position position;
-               wxImage image;
-               wxImage cropped_image;
+                       : base_image (i)
+               {
+                       base_area.x = p.x;
+                       base_area.y = p.y;
+                       base_area.w = base_image.GetWidth ();
+                       base_area.h = base_image.GetHeight ();
+               }
+
+               Rectangle base_area;
+               Rectangle transformed_area;
+               wxImage base_image;
+               wxImage transformed_image;
                shared_ptr<wxBitmap> bitmap;
        };
 
@@ -268,9 +265,6 @@ void
 FilmViewer::film_changed (Film::Property p)
 {
        switch (p) {
-       case Film::CROP:
-               _thumb_panel->set_crop (_film->crop ());
-               break;
        case Film::THUMBS:
                if (_film && _film->num_thumbs() > 1) {
                        _slider->SetRange (0, _film->num_thumbs () - 1);
@@ -282,16 +276,17 @@ FilmViewer::film_changed (Film::Property p)
                _slider->SetValue (0);
                set_thumbnail (0);
                break;
-       case Film::FORMAT:
-               _thumb_panel->refresh ();
-               break;
        case Film::CONTENT:
                setup_visibility ();
                _film->examine_content ();
                update_thumbs ();
                break;
+       case Film::CROP:
+       case Film::FORMAT:
        case Film::WITH_SUBTITLES:
-               _thumb_panel->Refresh ();
+       case Film::SUBTITLE_OFFSET:
+       case Film::SUBTITLE_SCALE:
+               _thumb_panel->recompose ();
                break;
        default:
                break;
@@ -315,7 +310,6 @@ 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 ();
 }
 
@@ -334,6 +328,7 @@ FilmViewer::update_thumbs ()
        o->apply_crop = false;
        o->decode_audio = false;
        o->decode_video_frequency = 128;
+       o->decode_subtitles = true;
        
        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));