11b5bf6229b8713dffde89a777cd5181335136f6 from master; default colour conversions...
[dcpomatic.git] / src / lib / image.cc
index 3df498c874efa6ac02885a88f7a101c6bb1deb4e..bba5eeda1a0241d213dae5b30c804022f98de7ba 100644 (file)
@@ -23,7 +23,6 @@
 
 #include "image.h"
 #include "exceptions.h"
-#include "scaler.h"
 #include "timer.h"
 #include "rect.h"
 #include "util.h"
@@ -88,9 +87,10 @@ Image::components () const
 
 /** Crop this image, scale it to `inter_size' and then place it in a black frame of `out_size' */
 shared_ptr<Image>
-Image::crop_scale_window (Crop crop, dcp::Size inter_size, dcp::Size out_size, Scaler const * scaler, AVPixelFormat out_format, bool out_aligned) const
+Image::crop_scale_window (
+       Crop crop, dcp::Size inter_size, dcp::Size out_size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, bool out_aligned
+       ) const
 {
-       DCPOMATIC_ASSERT (scaler);
        /* Empirical testing suggests that sws_scale() will crash if
           the input image is not aligned.
        */
@@ -110,13 +110,26 @@ Image::crop_scale_window (Crop crop, dcp::Size inter_size, dcp::Size out_size, S
        struct SwsContext* scale_context = sws_getContext (
                        cropped_size.width, cropped_size.height, pixel_format(),
                        inter_size.width, inter_size.height, out_format,
-                       scaler->ffmpeg_id (), 0, 0, 0
+                       SWS_BICUBIC, 0, 0, 0
                );
 
        if (!scale_context) {
                throw StringError (N_("Could not allocate SwsContext"));
        }
 
+       DCPOMATIC_ASSERT (yuv_to_rgb < dcp::YUV_TO_RGB_COUNT);
+       int const lut[dcp::YUV_TO_RGB_COUNT] = {
+               SWS_CS_ITU601,
+               SWS_CS_ITU709
+       };
+
+       sws_setColorspaceDetails (
+               scale_context,
+               sws_getCoefficients (lut[yuv_to_rgb]), 0,
+               sws_getCoefficients (lut[yuv_to_rgb]), 0,
+               0, 1 << 16, 1 << 16
+               );
+       
        /* Prepare input data pointers with crop */
        uint8_t* scale_in_data[components()];
        for (int c = 0; c < components(); ++c) {
@@ -144,9 +157,8 @@ Image::crop_scale_window (Crop crop, dcp::Size inter_size, dcp::Size out_size, S
 }
 
 shared_ptr<Image>
-Image::scale (dcp::Size out_size, Scaler const * scaler, AVPixelFormat out_format, bool out_aligned) const
+Image::scale (dcp::Size out_size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, bool out_aligned) const
 {
-       DCPOMATIC_ASSERT (scaler);
        /* Empirical testing suggests that sws_scale() will crash if
           the input image is not aligned.
        */
@@ -157,9 +169,22 @@ Image::scale (dcp::Size out_size, Scaler const * scaler, AVPixelFormat out_forma
        struct SwsContext* scale_context = sws_getContext (
                size().width, size().height, pixel_format(),
                out_size.width, out_size.height, out_format,
-               scaler->ffmpeg_id (), 0, 0, 0
+               SWS_BICUBIC, 0, 0, 0
                );
 
+       DCPOMATIC_ASSERT (yuv_to_rgb < dcp::YUV_TO_RGB_COUNT);
+       int const lut[dcp::YUV_TO_RGB_COUNT] = {
+               SWS_CS_ITU601,
+               SWS_CS_ITU709
+       };
+
+       sws_setColorspaceDetails (
+               scale_context,
+               sws_getCoefficients (lut[yuv_to_rgb]), 0,
+               sws_getCoefficients (lut[yuv_to_rgb]), 0,
+               0, 1 << 16, 1 << 16
+               );
+       
        sws_scale (
                scale_context,
                data(), stride(),
@@ -525,7 +550,7 @@ Image::bytes_per_pixel (int c) const
  *  @param s Size in pixels.
  */
 Image::Image (AVPixelFormat p, dcp::Size s, bool aligned)
-       : dcp::Image (s)
+       : _size (s)
        , _pixel_format (p)
        , _aligned (aligned)
 {
@@ -567,8 +592,8 @@ Image::allocate ()
 }
 
 Image::Image (Image const & other)
-       : dcp::Image (other)
-       ,  _pixel_format (other._pixel_format)
+       : _size (other._size)
+       , _pixel_format (other._pixel_format)
        , _aligned (other._aligned)
 {
        allocate ();
@@ -585,7 +610,7 @@ Image::Image (Image const & other)
 }
 
 Image::Image (AVFrame* frame)
-       : dcp::Image (dcp::Size (frame->width, frame->height))
+       : _size (frame->width, frame->height)
        , _pixel_format (static_cast<AVPixelFormat> (frame->format))
        , _aligned (true)
 {
@@ -604,7 +629,7 @@ Image::Image (AVFrame* frame)
 }
 
 Image::Image (shared_ptr<const Image> other, bool aligned)
-       : dcp::Image (other)
+       : _size (other->_size)
        , _pixel_format (other->_pixel_format)
        , _aligned (aligned)
 {
@@ -637,8 +662,7 @@ Image::operator= (Image const & other)
 void
 Image::swap (Image & other)
 {
-       dcp::Image::swap (other);
-       
+       std::swap (_size, other._size);
        std::swap (_pixel_format, other._pixel_format);
 
        for (int i = 0; i < 4; ++i) {