2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "video_content_scale.h"
22 #include "video_content.h"
25 #include <libcxml/cxml.h>
26 #include <libxml++/libxml++.h>
27 #include <boost/optional.hpp>
36 using boost::shared_ptr;
37 using boost::optional;
39 vector<VideoContentScale> VideoContentScale::_scales;
41 VideoContentScale::VideoContentScale (Ratio const * r)
48 VideoContentScale::VideoContentScale ()
55 VideoContentScale::VideoContentScale (bool scale)
62 VideoContentScale::VideoContentScale (shared_ptr<cxml::Node> node)
66 optional<string> r = node->optional_string_child ("Ratio");
68 _ratio = Ratio::from_id (r.get ());
70 _scale = node->bool_child ("Scale");
75 VideoContentScale::as_xml (xmlpp::Node* node) const
78 node->add_child("Ratio")->add_child_text (_ratio->id ());
80 node->add_child("Scale")->add_child_text (_scale ? "1" : "0");
85 VideoContentScale::id () const
91 return (_scale ? "S1" : "S0");
95 VideoContentScale::name () const
98 return _ratio->nickname ();
102 return _("No stretch");
105 return _("No scale");
109 VideoContentScale::from_id (string id)
111 Ratio const * r = Ratio::from_id (id);
113 return VideoContentScale (r);
117 return VideoContentScale (false);
120 return VideoContentScale (true);
123 /** @param display_container Size of the container that we are displaying this content in.
124 * @param film_container The size of the film's image.
127 VideoContentScale::size (shared_ptr<const VideoContent> c, dcp::Size display_container, dcp::Size film_container) const
129 /* Work out the size of the content if it were put inside film_container */
131 dcp::Size const video_size_after_crop = c->size_after_crop ();
136 /* Stretch to fit the requested ratio */
137 size = fit_ratio_within (_ratio->ratio (), film_container);
138 } else if (_scale || video_size_after_crop.width > film_container.width || video_size_after_crop.height > film_container.height) {
139 /* Scale, preserving aspect ratio; this is either if we have been asked to scale with no stretch
140 or if the unscaled content is too big for film_container.
142 size = fit_ratio_within (video_size_after_crop.ratio(), film_container);
144 /* No stretch nor scale */
145 size = video_size_after_crop;
148 /* Now scale it down if the display container is smaller than the film container */
149 if (display_container != film_container) {
150 float const scale = min (
151 float (display_container.width) / film_container.width,
152 float (display_container.height) / film_container.height
155 size.width = lrintf (size.width * scale);
156 size.height = lrintf (size.height * scale);
163 VideoContentScale::setup_scales ()
165 vector<Ratio const *> ratios = Ratio::all ();
166 for (vector<Ratio const *>::const_iterator i = ratios.begin(); i != ratios.end(); ++i) {
167 _scales.push_back (VideoContentScale (*i));
170 _scales.push_back (VideoContentScale (true));
171 _scales.push_back (VideoContentScale (false));
175 operator== (VideoContentScale const & a, VideoContentScale const & b)
177 return (a.ratio() == b.ratio() && a.scale() == b.scale());
181 operator!= (VideoContentScale const & a, VideoContentScale const & b)
183 return (a.ratio() != b.ratio() || a.scale() != b.scale());