Various work on certificate handling for screens; need XML config here, now.
[dcpomatic.git] / src / lib / format.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file src/format.cc
21  *  @brief Class to describe a format (aspect ratio) that a Film should
22  *  be shown in.
23  */
24
25 #include <sstream>
26 #include <cstdlib>
27 #include <cassert>
28 #include <iomanip>
29 #include <iostream>
30 #include "format.h"
31 #include "film.h"
32
33 using std::string;
34 using std::setprecision;
35 using std::stringstream;
36 using std::vector;
37 using boost::shared_ptr;
38
39 vector<Format const *> Format::_formats;
40
41 /** @return A name to be presented to the user */
42 string
43 FixedFormat::name () const
44 {
45         stringstream s;
46         if (!_nickname.empty ()) {
47                 s << _nickname << " (";
48         }
49
50         s << setprecision(3) << (_ratio / 100.0) << ":1";
51
52         if (!_nickname.empty ()) {
53                 s << ")";
54         }
55
56         return s.str ();
57 }
58
59 /** @return Identifier for this format as metadata for a Film's metadata file */
60 string
61 Format::as_metadata () const
62 {
63         return _id;
64 }
65
66 /** Fill our _formats vector with all available formats */
67 void
68 Format::setup_formats ()
69 {
70         _formats.push_back (new FixedFormat (119, Size (1285, 1080), "119", "1.19", "F"));
71         _formats.push_back (new FixedFormat (133, Size (1436, 1080), "133", "1.33", "F"));
72         _formats.push_back (new FixedFormat (138, Size (1485, 1080), "138", "1.375", "F"));
73         _formats.push_back (new FixedFormat (133, Size (1998, 1080), "133-in-flat", "4:3 within Flat", "F"));
74         _formats.push_back (new FixedFormat (137, Size (1480, 1080), "137", "Academy", "F"));
75         _formats.push_back (new FixedFormat (166, Size (1793, 1080), "166", "1.66", "F"));
76         _formats.push_back (new FixedFormat (166, Size (1998, 1080), "166-in-flat", "1.66 within Flat", "F"));
77         _formats.push_back (new FixedFormat (178, Size (1998, 1080), "178-in-flat", "16:9 within Flat", "F"));
78         _formats.push_back (new FixedFormat (178, Size (1920, 1080), "178", "16:9", "F"));
79         _formats.push_back (new FixedFormat (185, Size (1998, 1080), "185", "Flat", "F"));
80         _formats.push_back (new FixedFormat (239, Size (2048, 858), "239", "Scope", "S"));
81         _formats.push_back (new VariableFormat (Size (1998, 1080), "var-185", "Flat", "F"));
82         _formats.push_back (new VariableFormat (Size (2048, 858), "var-239", "Scope", "S"));
83 }
84
85 /** @param n Nickname.
86  *  @return Matching Format, or 0.
87  */
88 Format const *
89 Format::from_nickname (string n)
90 {
91         vector<Format const *>::iterator i = _formats.begin ();
92         while (i != _formats.end() && (*i)->nickname() != n) {
93                 ++i;
94         }
95
96         if (i == _formats.end ()) {
97                 return 0;
98         }
99
100         return *i;
101 }
102
103 /** @param i Id.
104  *  @return Matching Format, or 0.
105  */
106 Format const *
107 Format::from_id (string i)
108 {
109         vector<Format const *>::iterator j = _formats.begin ();
110         while (j != _formats.end() && (*j)->id() != i) {
111                 ++j;
112         }
113
114         if (j == _formats.end ()) {
115                 return 0;
116         }
117
118         return *j;
119 }
120
121
122 /** @param m Metadata, as returned from as_metadata().
123  *  @return Matching Format, or 0.
124  */
125 Format const *
126 Format::from_metadata (string m)
127 {
128         return from_id (m);
129 }
130
131 /** @return All available formats */
132 vector<Format const *>
133 Format::all ()
134 {
135         return _formats;
136 }
137
138 /** @param r Ratio multiplied by 100 (e.g. 185)
139  *  @param dcp Size (in pixels) of the images that we should put in a DCP.
140  *  @param id ID (e.g. 185)
141  *  @param n Nick name (e.g. Flat)
142  */
143 FixedFormat::FixedFormat (int r, Size dcp, string id, string n, string d)
144         : Format (dcp, id, n, d)
145         , _ratio (r)
146 {
147
148 }
149
150 int
151 Format::dcp_padding (shared_ptr<const Film> f) const
152 {
153         int p = rint ((_dcp_size.width - (_dcp_size.height * ratio_as_integer(f) / 100.0)) / 2.0);
154
155         /* This comes out -ve for Scope; bodge it */
156         if (p < 0) {
157                 p = 0;
158         }
159         
160         return p;
161 }
162
163 VariableFormat::VariableFormat (Size dcp, string id, string n, string d)
164         : Format (dcp, id, n, d)
165 {
166
167 }
168
169 int
170 VariableFormat::ratio_as_integer (shared_ptr<const Film> f) const
171 {
172         return rint (ratio_as_float (f) * 100);
173 }
174
175 float
176 VariableFormat::ratio_as_float (shared_ptr<const Film> f) const
177 {
178         return float (f->size().width) / f->size().height;
179 }
180
181 /** @return A name to be presented to the user */
182 string
183 VariableFormat::name () const
184 {
185         return _nickname;
186 }