2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp 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 libdcp 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 libdcp. If not, see <http://www.gnu.org/licenses/>.
19 In addition, as a special exception, the copyright holders give
20 permission to link the code of portions of this program with the
21 OpenSSL library under certain conditions as described in each
22 individual source file, and distribute linked combinations
25 You must obey the GNU General Public License in all respects
26 for all of the code used other than OpenSSL. If you modify
27 file(s) with this exception, you may extend this exception to your
28 version of the file(s), but you are not obligated to do so. If you
29 do not wish to do so, delete this exception statement from your
30 version. If you delete this exception statement from all source
31 files in the program, then also delete it here.
34 #include "raw_convert.h"
36 #include "exceptions.h"
37 #include "compose.hpp"
38 #include "dcp_assert.h"
39 #include <boost/algorithm/string.hpp>
46 using namespace boost;
48 bool dcp::operator== (dcp::Size const & a, dcp::Size const & b)
50 return (a.width == b.width && a.height == b.height);
53 bool dcp::operator!= (dcp::Size const & a, dcp::Size const & b)
58 ostream& dcp::operator<< (ostream& s, dcp::Size const & a)
60 s << a.width << "x" << a.height;
64 /** Construct a Fraction from a string of the form <numerator> <denominator>
67 Fraction::Fraction (string s)
70 split (b, s, is_any_of (" "));
72 boost::throw_exception (XMLError ("malformed fraction " + s + " in XML node"));
74 numerator = raw_convert<int> (b[0]);
75 denominator = raw_convert<int> (b[1]);
79 Fraction::as_string () const
81 return String::compose ("%1 %2", numerator, denominator);
85 dcp::operator== (Fraction const & a, Fraction const & b)
87 return (a.numerator == b.numerator && a.denominator == b.denominator);
91 dcp::operator!= (Fraction const & a, Fraction const & b)
93 return (a.numerator != b.numerator || a.denominator != b.denominator);
97 dcp::operator<< (ostream& s, Fraction const & f)
99 s << f.numerator << "/" << f.denominator;
103 /** Construct a Colour, initialising it to black. */
112 /** Construct a Colour from R, G and B. The values run between
115 Colour::Colour (int r_, int g_, int b_)
123 /** Construct a Colour from an ARGB hex string; the alpha value is ignored.
124 * @param argb_hex A string of the form AARRGGBB, where e.g. RR is a two-character
127 Colour::Colour (string argb_hex)
130 if (sscanf (argb_hex.c_str(), "%2x%2x%2x%2x", &alpha, &r, &g, &b) != 4) {
131 boost::throw_exception (XMLError ("could not parse colour string"));
135 /** @return An ARGB string of the form AARRGGBB, where e.g. RR is a two-character
136 * hex value. The alpha value will always be FF (ie 255; maximum alpha).
139 Colour::to_argb_string () const
142 snprintf (buffer, sizeof(buffer), "FF%02X%02X%02X", r, g, b);
146 /** @return An RGB string of the form RRGGBB, where e.g. RR is a two-character
150 Colour::to_rgb_string () const
153 snprintf (buffer, sizeof(buffer), "%02X%02X%02X", r, g, b);
157 /** operator== for Colours.
158 * @param a First colour to compare.
159 * @param b Second colour to compare.
162 dcp::operator== (Colour const & a, Colour const & b)
164 return (a.r == b.r && a.g == b.g && a.b == b.b);
167 /** operator!= for Colours.
168 * @param a First colour to compare.
169 * @param b Second colour to compare.
172 dcp::operator!= (Colour const & a, Colour const & b)
178 dcp::operator<< (ostream& s, Colour const & c)
180 s << "(" << c.r << ", " << c.g << ", " << c.b << ")";
185 dcp::effect_to_string (Effect e)
196 boost::throw_exception (MiscError ("unknown effect type"));
200 dcp::string_to_effect (string s)
204 } else if (s == "border") {
206 } else if (s == "shadow") {
210 boost::throw_exception (DCPReadError ("unknown subtitle effect type"));
214 dcp::halign_to_string (HAlign h)
225 boost::throw_exception (MiscError ("unknown subtitle halign type"));
229 dcp::string_to_halign (string s)
233 } else if (s == "center") {
234 return HALIGN_CENTER;
235 } else if (s == "right") {
239 boost::throw_exception (DCPReadError ("unknown subtitle halign type"));
243 dcp::valign_to_string (VAlign v)
254 boost::throw_exception (MiscError ("unknown subtitle valign type"));
258 dcp::string_to_valign (string s)
262 } else if (s == "center") {
263 return VALIGN_CENTER;
264 } else if (s == "bottom") {
265 return VALIGN_BOTTOM;
268 boost::throw_exception (DCPReadError ("unknown subtitle valign type"));
272 dcp::direction_to_string (Direction v)
285 boost::throw_exception (MiscError ("unknown subtitle direction type"));
289 dcp::string_to_direction (string s)
291 if (s == "ltr" || s == "horizontal") {
292 return DIRECTION_LTR;
293 } else if (s == "rtl") {
294 return DIRECTION_RTL;
295 } else if (s == "ttb" || s == "vertical") {
296 return DIRECTION_TTB;
297 } else if (s == "btt") {
298 return DIRECTION_BTT;
301 boost::throw_exception (DCPReadError ("unknown subtitle direction type"));
304 /** Convert a content kind to a string which can be used in a
305 * <ContentKind> node.
306 * @param kind ContentKind.
310 dcp::content_kind_to_string (ContentKind kind)
322 return "transitional";
329 case PUBLIC_SERVICE_ANNOUNCEMENT:
332 return "advertisement";
338 /** Convert a string from a <ContentKind> node to a libdcp ContentKind.
339 * Reasonably tolerant about varying case.
340 * @param kind Content kind string.
341 * @return libdcp ContentKind.
344 dcp::content_kind_from_string (string kind)
346 transform (kind.begin(), kind.end(), kind.begin(), ::tolower);
348 if (kind == "feature") {
350 } else if (kind == "short") {
352 } else if (kind == "trailer") {
354 } else if (kind == "test") {
356 } else if (kind == "transitional") {
358 } else if (kind == "rating") {
360 } else if (kind == "teaser") {
362 } else if (kind == "policy") {
364 } else if (kind == "psa") {
365 return PUBLIC_SERVICE_ANNOUNCEMENT;
366 } else if (kind == "advertisement") {
367 return ADVERTISEMENT;
370 throw BadContentKindError (kind);
374 dcp::marker_to_string (dcp::Marker m)
403 dcp::marker_from_string (string s)
407 } else if (s == "LFOC") {
409 } else if (s == "FFTC") {
411 } else if (s == "LFTC") {
413 } else if (s == "FFOI") {
415 } else if (s == "LFOI") {
417 } else if (s == "FFEC") {
419 } else if (s == "LFEC") {
421 } else if (s == "FFMC") {
423 } else if (s == "LFMC") {