2 Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <boost/optional.hpp>
21 #include <libxml++/libxml++.h>
22 #include <libcxml/cxml.h>
23 #include "video_content_scale.h"
25 #include "safe_stringstream.h"
32 using boost::shared_ptr;
33 using boost::optional;
35 vector<VideoContentScale> VideoContentScale::_scales;
37 VideoContentScale::VideoContentScale (Ratio const * r)
44 VideoContentScale::VideoContentScale ()
51 VideoContentScale::VideoContentScale (bool scale)
58 VideoContentScale::VideoContentScale (shared_ptr<cxml::Node> node)
62 optional<string> r = node->optional_string_child ("Ratio");
64 _ratio = Ratio::from_id (r.get ());
66 _scale = node->bool_child ("Scale");
71 VideoContentScale::as_xml (xmlpp::Node* node) const
74 node->add_child("Ratio")->add_child_text (_ratio->id ());
76 node->add_child("Scale")->add_child_text (_scale ? "1" : "0");
81 VideoContentScale::id () const
88 s << (_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, int round) const
130 return fit_ratio_within (_ratio->ratio (), display_container, round);
133 dcp::Size const ac = c->video_size_after_crop ();
135 /* Force scale if the film_container is smaller than the content's image */
136 if (_scale || film_container.width < ac.width || film_container.height < ac.height) {
137 return fit_ratio_within (ac.ratio (), display_container, round);
140 /* Scale the image so that it will be in the right place in film_container, even if display_container is a
144 c->video_size().width * float(display_container.width) / film_container.width,
145 c->video_size().height * float(display_container.height) / film_container.height
150 VideoContentScale::setup_scales ()
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));
157 _scales.push_back (VideoContentScale (true));
158 _scales.push_back (VideoContentScale (false));
162 operator== (VideoContentScale const & a, VideoContentScale const & b)
164 return (a.ratio() == b.ratio() && a.scale() == b.scale());
168 operator!= (VideoContentScale const & a, VideoContentScale const & b)
170 return (a.ratio() != b.ratio() || a.scale() != b.scale());