Try to fix no-stretch / no-scale for non-square pixels (#1636).
[dcpomatic.git] / src / lib / video_content_scale.cc
1 /*
2     Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "video_content_scale.h"
22 #include "video_content.h"
23 #include "ratio.h"
24 #include "util.h"
25 #include <libcxml/cxml.h>
26 #include <libxml++/libxml++.h>
27 #include <boost/optional.hpp>
28 #include <iostream>
29
30 #include "i18n.h"
31
32 using std::vector;
33 using std::string;
34 using std::min;
35 using std::cout;
36 using boost::shared_ptr;
37 using boost::optional;
38
39 vector<VideoContentScale> VideoContentScale::_scales;
40
41 VideoContentScale::VideoContentScale (Ratio const * r)
42         : _ratio (r)
43         , _scale (true)
44 {
45
46 }
47
48 VideoContentScale::VideoContentScale ()
49         : _ratio (0)
50         , _scale (false)
51 {
52
53 }
54
55 VideoContentScale::VideoContentScale (bool scale)
56         : _ratio (0)
57         , _scale (scale)
58 {
59
60 }
61
62 VideoContentScale::VideoContentScale (shared_ptr<cxml::Node> node)
63         : _ratio (0)
64         , _scale (true)
65 {
66         optional<string> r = node->optional_string_child ("Ratio");
67         if (r) {
68                 _ratio = Ratio::from_id (r.get ());
69         } else {
70                 _scale = node->bool_child ("Scale");
71         }
72 }
73
74 void
75 VideoContentScale::as_xml (xmlpp::Node* node) const
76 {
77         if (_ratio) {
78                 node->add_child("Ratio")->add_child_text (_ratio->id ());
79         } else {
80                 node->add_child("Scale")->add_child_text (_scale ? "1" : "0");
81         }
82 }
83
84 string
85 VideoContentScale::id () const
86 {
87         if (_ratio) {
88                 return _ratio->id ();
89         }
90
91         return (_scale ? "S1" : "S0");
92 }
93
94 string
95 VideoContentScale::name () const
96 {
97         if (_ratio) {
98                 return _ratio->image_nickname ();
99         }
100
101         if (_scale) {
102                 return _("No stretch");
103         }
104
105         return _("No scale");
106 }
107
108 /** @param display_container Size of the container that we are displaying this content in.
109  *  @param film_container The size of the film's image.
110  *  @return Size, in pixels that the VideoContent's image should be scaled to (taking into account its pixel aspect ratio)
111  */
112 dcp::Size
113 VideoContentScale::size (shared_ptr<const VideoContent> c, dcp::Size display_container, dcp::Size film_container) const
114 {
115         /* Work out the size of the content if it were put inside film_container */
116
117         dcp::Size video_size_after_crop = c->size_after_crop();
118         video_size_after_crop.width *= c->sample_aspect_ratio().get_value_or(1);
119
120         dcp::Size size;
121
122         if (_ratio) {
123                 /* Stretch to fit the requested ratio */
124                 size = fit_ratio_within (_ratio->ratio (), film_container);
125         } else if (_scale || video_size_after_crop.width > film_container.width || video_size_after_crop.height > film_container.height) {
126                 /* Scale, preserving aspect ratio; this is either if we have been asked to scale with no stretch
127                    or if the unscaled content is too big for film_container.
128                 */
129                 size = fit_ratio_within (video_size_after_crop.ratio(), film_container);
130         } else {
131                 /* No stretch nor scale */
132                 size = video_size_after_crop;
133         }
134
135         /* Now scale it down if the display container is smaller than the film container */
136         if (display_container != film_container) {
137                 float const scale = min (
138                         float (display_container.width) / film_container.width,
139                         float (display_container.height) / film_container.height
140                         );
141
142                 size.width = lrintf (size.width * scale);
143                 size.height = lrintf (size.height * scale);
144         }
145
146         return size;
147 }
148
149 void
150 VideoContentScale::setup_scales ()
151 {
152         vector<Ratio const *> ratios = Ratio::all ();
153         for (vector<Ratio const *>::const_iterator i = ratios.begin(); i != ratios.end(); ++i) {
154                 _scales.push_back (VideoContentScale (*i));
155         }
156
157         _scales.push_back (VideoContentScale (true));
158         _scales.push_back (VideoContentScale (false));
159 }
160
161 bool
162 operator== (VideoContentScale const & a, VideoContentScale const & b)
163 {
164         return (a.ratio() == b.ratio() && a.scale() == b.scale());
165 }
166
167 bool
168 operator!= (VideoContentScale const & a, VideoContentScale const & b)
169 {
170         return (a.ratio() != b.ratio() || a.scale() != b.scale());
171 }