a1673d3deafc58ae84950eec58314783eaf14ac4
[dcpomatic.git] / src / lib / video_content_scale.cc
1 /*
2     Copyright (C) 2013-2015 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 "safe_stringstream.h"
25 #include "util.h"
26 #include <libcxml/cxml.h>
27 #include <libxml++/libxml++.h>
28 #include <boost/optional.hpp>
29 #include <iostream>
30
31 #include "i18n.h"
32
33 using std::vector;
34 using std::string;
35 using std::min;
36 using std::cout;
37 using boost::shared_ptr;
38 using boost::optional;
39
40 vector<VideoContentScale> VideoContentScale::_scales;
41
42 VideoContentScale::VideoContentScale (Ratio const * r)
43         : _ratio (r)
44         , _scale (true)
45 {
46
47 }
48
49 VideoContentScale::VideoContentScale ()
50         : _ratio (0)
51         , _scale (false)
52 {
53
54 }
55
56 VideoContentScale::VideoContentScale (bool scale)
57         : _ratio (0)
58         , _scale (scale)
59 {
60
61 }
62
63 VideoContentScale::VideoContentScale (shared_ptr<cxml::Node> node)
64         : _ratio (0)
65         , _scale (true)
66 {
67         optional<string> r = node->optional_string_child ("Ratio");
68         if (r) {
69                 _ratio = Ratio::from_id (r.get ());
70         } else {
71                 _scale = node->bool_child ("Scale");
72         }
73 }
74
75 void
76 VideoContentScale::as_xml (xmlpp::Node* node) const
77 {
78         if (_ratio) {
79                 node->add_child("Ratio")->add_child_text (_ratio->id ());
80         } else {
81                 node->add_child("Scale")->add_child_text (_scale ? "1" : "0");
82         }
83 }
84
85 string
86 VideoContentScale::id () const
87 {
88         SafeStringStream s;
89
90         if (_ratio) {
91                 s << _ratio->id ();
92         } else {
93                 s << (_scale ? "S1" : "S0");
94         }
95
96         return s.str ();
97 }
98
99 string
100 VideoContentScale::name () const
101 {
102         if (_ratio) {
103                 return _ratio->nickname ();
104         }
105
106         if (_scale) {
107                 return _("No stretch");
108         }
109
110         return _("No scale");
111 }
112
113 VideoContentScale
114 VideoContentScale::from_id (string id)
115 {
116         Ratio const * r = Ratio::from_id (id);
117         if (r) {
118                 return VideoContentScale (r);
119         }
120
121         if (id == "S0") {
122                 return VideoContentScale (false);
123         }
124
125         return VideoContentScale (true);
126 }
127
128 /** @param display_container Size of the container that we are displaying this content in.
129  *  @param film_container The size of the film's image.
130  */
131 dcp::Size
132 VideoContentScale::size (shared_ptr<const VideoContent> c, dcp::Size display_container, dcp::Size film_container) const
133 {
134         /* Work out the size of the content if it were put inside film_container */
135
136         dcp::Size const video_size_after_crop = c->size_after_crop ();
137
138         dcp::Size size;
139
140         if (_ratio) {
141                 /* Stretch to fit the requested ratio */
142                 size = fit_ratio_within (_ratio->ratio (), film_container);
143         } else if (_scale || video_size_after_crop.width > film_container.width || video_size_after_crop.height > film_container.height) {
144                 /* Scale, preserving aspect ratio; this is either if we have been asked to scale with no stretch
145                    or if the unscaled content is too big for film_container.
146                 */
147                 size = fit_ratio_within (video_size_after_crop.ratio(), film_container);
148         } else {
149                 /* No stretch nor scale */
150                 size = video_size_after_crop;
151         }
152
153         /* Now scale it down if the display container is smaller than the film container */
154         if (display_container != film_container) {
155                 float const scale = min (
156                         float (display_container.width) / film_container.width,
157                         float (display_container.height) / film_container.height
158                         );
159
160                 size.width = lrintf (size.width * scale);
161                 size.height = lrintf (size.height * scale);
162         }
163
164         return size;
165 }
166
167 void
168 VideoContentScale::setup_scales ()
169 {
170         vector<Ratio const *> ratios = Ratio::all ();
171         for (vector<Ratio const *>::const_iterator i = ratios.begin(); i != ratios.end(); ++i) {
172                 _scales.push_back (VideoContentScale (*i));
173         }
174
175         _scales.push_back (VideoContentScale (true));
176         _scales.push_back (VideoContentScale (false));
177 }
178
179 bool
180 operator== (VideoContentScale const & a, VideoContentScale const & b)
181 {
182         return (a.ratio() == b.ratio() && a.scale() == b.scale());
183 }
184
185 bool
186 operator!= (VideoContentScale const & a, VideoContentScale const & b)
187 {
188         return (a.ratio() != b.ratio() || a.scale() != b.scale());
189 }