Tidy up duplicated code.
authorCarl Hetherington <cth@carlh.net>
Fri, 23 Aug 2013 10:17:03 +0000 (11:17 +0100)
committerCarl Hetherington <cth@carlh.net>
Fri, 23 Aug 2013 10:17:03 +0000 (11:17 +0100)
src/lib/image.cc
src/lib/image.h
src/lib/player.cc
test/image_test.cc
test/make_black_test.cc

index bdf7fd173c9d860db6cb8f749700d9fbea641075..5c04f70e6ab40f0e4c5ee1afd32f0ff6a14b2a55 100644 (file)
@@ -79,7 +79,7 @@ Image::components () const
 }
 
 shared_ptr<Image>
-Image::scale (libdcp::Size out_size, Scaler const * scaler, bool result_aligned) const
+Image::scale (libdcp::Size out_size, Scaler const * scaler, AVPixelFormat result_format, bool result_aligned) const
 {
        assert (scaler);
        /* Empirical testing suggests that sws_scale() will crash if
@@ -87,11 +87,11 @@ Image::scale (libdcp::Size out_size, Scaler const * scaler, bool result_aligned)
        */
        assert (aligned ());
 
-       shared_ptr<Image> scaled (new Image (pixel_format(), out_size, result_aligned));
+       shared_ptr<Image> scaled (new Image (result_format, out_size, result_aligned));
 
        struct SwsContext* scale_context = sws_getContext (
                size().width, size().height, pixel_format(),
-               out_size.width, out_size.height, pixel_format(),
+               out_size.width, out_size.height, result_format,
                scaler->ffmpeg_id (), 0, 0, 0
                );
 
@@ -107,40 +107,6 @@ Image::scale (libdcp::Size out_size, Scaler const * scaler, bool result_aligned)
        return scaled;
 }
 
-/** Scale this image to a given size and convert it to RGB.
- *  @param out_size Output image size in pixels.
- *  @param scaler Scaler to use.
- */
-shared_ptr<Image>
-Image::scale_and_convert_to_rgb (libdcp::Size out_size, Scaler const * scaler, bool result_aligned) const
-{
-       assert (scaler);
-       /* Empirical testing suggests that sws_scale() will crash if
-          the input image is not aligned.
-       */
-       assert (aligned ());
-
-       shared_ptr<Image> rgb (new Image (PIX_FMT_RGB24, out_size, result_aligned));
-
-       struct SwsContext* scale_context = sws_getContext (
-               size().width, size().height, pixel_format(),
-               out_size.width, out_size.height, PIX_FMT_RGB24,
-               scaler->ffmpeg_id (), 0, 0, 0
-               );
-
-       /* Scale and convert to RGB from whatever its currently in (which may be RGB) */
-       sws_scale (
-               scale_context,
-               data(), stride(),
-               0, size().height,
-               rgb->data(), rgb->stride()
-               );
-
-       sws_freeContext (scale_context);
-
-       return rgb;
-}
-
 /** Run a FFmpeg post-process on this image and return the processed version.
  *  @param pp Flags for the required set of post processes.
  *  @return Post-processed image.
index cc0baf9219782e818375801a1c57773d3dc7d6cc..6af74a8c5ee16e641c817f27361b4757118e1295 100644 (file)
@@ -57,8 +57,7 @@ public:
        int line_factor (int) const;
        int lines (int) const;
 
-       boost::shared_ptr<Image> scale_and_convert_to_rgb (libdcp::Size, Scaler const *, bool) const;
-       boost::shared_ptr<Image> scale (libdcp::Size, Scaler const *, bool aligned) const;
+       boost::shared_ptr<Image> scale (libdcp::Size, Scaler const *, AVPixelFormat, bool aligned) const;
        boost::shared_ptr<Image> post_process (std::string, bool aligned) const;
        void alpha_blend (boost::shared_ptr<const Image> image, Position<int> pos);
        void copy (boost::shared_ptr<const Image> image, Position<int> pos);
index bae00d348684bfb7d92012e0d0d726f266f212d2..84589ce39b44c495369367f4597ee4abd3197258 100644 (file)
@@ -255,7 +255,7 @@ Player::process_video (weak_ptr<Piece> weak_piece, shared_ptr<const Image> image
 
        libdcp::Size const image_size = content->ratio()->size (_video_container_size);
        
-       work_image = work_image->scale_and_convert_to_rgb (image_size, _film->scaler(), true);
+       work_image = work_image->scale (image_size, _film->scaler(), PIX_FMT_RGB24, true);
 
        Time time = content->position() + relative_time - content->trim_start ();
            
@@ -640,7 +640,12 @@ Player::update_subtitle ()
        _out_subtitle.position.x = rint (_video_container_size.width * (in_rect.x + (in_rect.width * (1 - sc->subtitle_scale ()) / 2)));
        _out_subtitle.position.y = rint (_video_container_size.height * (in_rect.y + (in_rect.height * (1 - sc->subtitle_scale ()) / 2)));
        
-       _out_subtitle.image = _in_subtitle.image->scale (libdcp::Size (scaled_size.width, scaled_size.height), Scaler::from_id ("bicubic"), true);
+       _out_subtitle.image = _in_subtitle.image->scale (
+               scaled_size,
+               Scaler::from_id ("bicubic"),
+               _in_subtitle.image->pixel_format (),
+               true
+               );
        _out_subtitle.from = _in_subtitle.from + piece->content->position ();
        _out_subtitle.to = _in_subtitle.to + piece->content->position ();
 }
index 4205e5f48e00ddfe942b887ee9623c4015ffd4f6..82ebe0b1ff5e6825c0db29bbd92f8fc2495092f8 100644 (file)
@@ -147,7 +147,7 @@ BOOST_AUTO_TEST_CASE (crop_image_test2)
        image = image->crop (crop, true);
 
        /* Convert it back to RGB to make comparison to black easier */
-       image = image->scale_and_convert_to_rgb (image->size(), Scaler::from_id ("bicubic"), true);
+       image = image->scale (image->size(), Scaler::from_id ("bicubic"), PIX_FMT_RGB24, true);
 
        /* Check that its still black after the crop */
        uint8_t* p = image->data()[0];
index 78c7fbbcba02f1222f4500c618fc5f67233d098b..17c78d23110013c8202d002c30588781256569e0 100644 (file)
@@ -53,7 +53,7 @@ BOOST_AUTO_TEST_CASE (make_black_test)
        for (list<AVPixelFormat>::const_iterator i = pix_fmts.begin(); i != pix_fmts.end(); ++i) {
                boost::shared_ptr<Image> foo (new Image (*i, in_size, true));
                foo->make_black ();
-               boost::shared_ptr<Image> bar = foo->scale_and_convert_to_rgb (out_size, Scaler::from_id ("bicubic"), true);
+               boost::shared_ptr<Image> bar = foo->scale (out_size, Scaler::from_id ("bicubic"), PIX_FMT_RGB24, true);
                
                uint8_t* p = bar->data()[0];
                for (int y = 0; y < bar->size().height; ++y) {