Account for J2K decoding at lower-than-maximum resolution when cropping
[dcpomatic.git] / src / lib / player_video.cc
index 8eb39efedca6484e47b50d7fe679cef8d317197e..a50b196a200152caf782a108c45eb1fdaddc5fbc 100644 (file)
@@ -34,6 +34,7 @@ extern "C" {
 
 using std::string;
 using std::cout;
+using std::pair;
 using boost::shared_ptr;
 using boost::weak_ptr;
 using boost::dynamic_pointer_cast;
@@ -112,7 +113,9 @@ PlayerVideo::set_subtitle (PositionImage image)
 shared_ptr<Image>
 PlayerVideo::image (dcp::NoteHandler note, function<AVPixelFormat (AVPixelFormat)> pixel_format, bool aligned, bool fast) const
 {
-       shared_ptr<Image> im = _in->image (optional<dcp::NoteHandler> (note), _inter_size);
+       pair<shared_ptr<Image>, int> prox = _in->image (optional<dcp::NoteHandler> (note), _inter_size);
+       shared_ptr<Image> im = prox.first;
+       int const reduce = prox.second;
 
        Crop total_crop = _crop;
        switch (_part) {
@@ -132,6 +135,15 @@ PlayerVideo::image (dcp::NoteHandler note, function<AVPixelFormat (AVPixelFormat
                break;
        }
 
+       if (reduce > 0) {
+               /* Scale the crop down to account for the scaling that has already happened in ImageProxy::image */
+               int const r = pow(2, reduce);
+               total_crop.left /= r;
+               total_crop.right /= r;
+               total_crop.top /= r;
+               total_crop.bottom /= r;
+       }
+
        dcp::YUVToRGB yuv_to_rgb = dcp::YUV_TO_RGB_REC601;
        if (_colour_conversion) {
                yuv_to_rgb = _colour_conversion.get().yuv_to_rgb();