Clean up after previous commit.
[dcpomatic.git] / src / lib / types.cc
1 /*
2     Copyright (C) 2013 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 "types.h"
22 #include "compose.hpp"
23 #include "dcpomatic_assert.h"
24 #include <dcp/raw_convert.h>
25 #include <libxml++/libxml++.h>
26 #include <libcxml/cxml.h>
27
28 using std::max;
29 using std::min;
30 using std::string;
31 using boost::shared_ptr;
32 using dcp::raw_convert;
33
34 bool operator== (Crop const & a, Crop const & b)
35 {
36         return (a.left == b.left && a.right == b.right && a.top == b.top && a.bottom == b.bottom);
37 }
38
39 bool operator!= (Crop const & a, Crop const & b)
40 {
41         return !(a == b);
42 }
43
44 /** @param r Resolution.
45  *  @return Untranslated string representation.
46  */
47 string
48 resolution_to_string (Resolution r)
49 {
50         switch (r) {
51         case RESOLUTION_2K:
52                 return "2K";
53         case RESOLUTION_4K:
54                 return "4K";
55         }
56
57         DCPOMATIC_ASSERT (false);
58         return "";
59 }
60
61
62 Resolution
63 string_to_resolution (string s)
64 {
65         if (s == "2K") {
66                 return RESOLUTION_2K;
67         }
68
69         if (s == "4K") {
70                 return RESOLUTION_4K;
71         }
72
73         DCPOMATIC_ASSERT (false);
74         return RESOLUTION_2K;
75 }
76
77 Crop::Crop (shared_ptr<cxml::Node> node)
78 {
79         left = node->number_child<int> ("LeftCrop");
80         right = node->number_child<int> ("RightCrop");
81         top = node->number_child<int> ("TopCrop");
82         bottom = node->number_child<int> ("BottomCrop");
83 }
84
85 void
86 Crop::as_xml (xmlpp::Node* node) const
87 {
88         node->add_child("LeftCrop")->add_child_text (raw_convert<string> (left));
89         node->add_child("RightCrop")->add_child_text (raw_convert<string> (right));
90         node->add_child("TopCrop")->add_child_text (raw_convert<string> (top));
91         node->add_child("BottomCrop")->add_child_text (raw_convert<string> (bottom));
92 }
93
94 CaptionType
95 string_to_caption_type (string s)
96 {
97         if (s == "open") {
98                 return CAPTION_OPEN;
99         } else if (s == "closed") {
100                 return CAPTION_CLOSED;
101         } else {
102                 throw MetadataError (String::compose ("Unknown caption type %1", s));
103         }
104 }
105
106 string
107 caption_type_to_string (CaptionType t)
108 {
109         switch (t) {
110         case CAPTION_OPEN:
111                 return "open";
112         case CAPTION_CLOSED:
113                 return "closed";
114         default:
115                 DCPOMATIC_ASSERT (false);
116         }
117 }
118
119 string
120 video_frame_type_to_string (VideoFrameType t)
121 {
122         switch (t) {
123         case VIDEO_FRAME_TYPE_2D:
124                 return "2d";
125         case VIDEO_FRAME_TYPE_3D:
126                 return "3d";
127         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
128                 return "3d-left-right";
129         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
130                 return "3d-top-bottom";
131         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
132                 return "3d-alternate";
133         case VIDEO_FRAME_TYPE_3D_LEFT:
134                 return "3d-left";
135         case VIDEO_FRAME_TYPE_3D_RIGHT:
136                 return "3d-right";
137         default:
138                 DCPOMATIC_ASSERT (false);
139         }
140
141         DCPOMATIC_ASSERT (false);
142 }
143
144 VideoFrameType
145 string_to_video_frame_type (string s)
146 {
147         if (s == "2d") {
148                 return VIDEO_FRAME_TYPE_2D;
149         } else if (s == "3d") {
150                 return VIDEO_FRAME_TYPE_3D;
151         } else if (s == "3d-left-right") {
152                 return VIDEO_FRAME_TYPE_3D_LEFT_RIGHT;
153         } else if (s == "3d-top-bottom") {
154                 return VIDEO_FRAME_TYPE_3D_TOP_BOTTOM;
155         } else if (s == "3d-alternate") {
156                 return VIDEO_FRAME_TYPE_3D_ALTERNATE;
157         } else if (s == "3d-left") {
158                 return VIDEO_FRAME_TYPE_3D_LEFT;
159         } else if (s == "3d-right") {
160                 return VIDEO_FRAME_TYPE_3D_RIGHT;
161         }
162
163         DCPOMATIC_ASSERT (false);
164 }