Bump version
[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 #include "i18n.h"
34
35 using std::string;
36 using std::setprecision;
37 using std::stringstream;
38 using std::vector;
39 using boost::shared_ptr;
40 using libdcp::Size;
41
42 vector<Format const *> Format::_formats;
43
44 /** @return A name to be presented to the user */
45 string
46 FixedFormat::name () const
47 {
48         stringstream s;
49         if (!_nickname.empty ()) {
50                 s << _nickname << N_(" (");
51         }
52
53         s << setprecision(3) << _ratio << N_(":1");
54
55         if (!_nickname.empty ()) {
56                 s << N_(")");
57         }
58
59         return s.str ();
60 }
61
62 /** @return Identifier for this format as metadata for a Film's metadata file */
63 string
64 Format::as_metadata () const
65 {
66         return _id;
67 }
68
69 /** Fill our _formats vector with all available formats */
70 void
71 Format::setup_formats ()
72 {
73         /// TRANSLATORS: these are film picture aspect ratios; "Academy" means 1.37, "Flat" 1.85 and "Scope" 2.39.
74         _formats.push_back (
75                 new FixedFormat (1.19, libdcp::Size (1285, 1080), "119", _("1.19"), "F"
76                         ));
77         
78         _formats.push_back (
79                 new FixedFormat (4.0 / 3.0, libdcp::Size (1436, 1080), "133", _("4:3"), "F"
80                         ));
81         
82         _formats.push_back (
83                 new FixedFormat (1.38, libdcp::Size (1485, 1080), "138", _("1.375"), "F"
84                         ));
85         
86         _formats.push_back (
87                 new FixedFormat (4.0 / 3.0, libdcp::Size (1998, 1080), "133-in-flat", _("4:3 within Flat"), "F"
88                         ));
89         
90         _formats.push_back (
91                 new FixedFormat (1.37, libdcp::Size (1480, 1080), "137", _("Academy"), "F"
92                         ));
93         
94         _formats.push_back (
95                 new FixedFormat (1.66, libdcp::Size (1793, 1080), "166", _("1.66"), "F"
96                         ));
97         
98         _formats.push_back (
99                 new FixedFormat (1.66, libdcp::Size (1998, 1080), "166-in-flat", _("1.66 within Flat"), "F"
100                         ));
101         
102         _formats.push_back (
103                 new FixedFormat (1.78, libdcp::Size (1998, 1080), "178-in-flat", _("16:9 within Flat"), "F"
104                         ));
105         
106         _formats.push_back (
107                 new FixedFormat (1.78, libdcp::Size (1920, 1080), "178", _("16:9"), "F"
108                         ));
109         
110         _formats.push_back (
111                 new FixedFormat (1.85, libdcp::Size (1998, 1080), "185", _("Flat"), "F"
112                         ));
113         
114         _formats.push_back (
115                 new FixedFormat (1.78, libdcp::Size (2048, 858), "178-in-scope", _("16:9 within Scope"), "S"
116                         ));
117         
118         _formats.push_back (
119                 new FixedFormat (2.39, libdcp::Size (2048, 858), "239", _("Scope"), "S"
120                         ));
121
122         _formats.push_back (
123                 new FixedFormat (1.896, libdcp::Size (2048, 1080), "full-frame", _("Full frame"), "C"
124                         ));
125                 
126         _formats.push_back (
127                 new VariableFormat (libdcp::Size (1998, 1080), "var-185", _("Flat without stretch"), "F"
128                         ));
129         
130         _formats.push_back (
131                 new VariableFormat (libdcp::Size (2048, 858), "var-239", _("Scope without stretch"), "S"
132                         ));
133 }
134
135 /** @param n Nickname.
136  *  @return Matching Format, or 0.
137  */
138 Format const *
139 Format::from_nickname (string n)
140 {
141         vector<Format const *>::iterator i = _formats.begin ();
142         while (i != _formats.end() && (*i)->nickname() != n) {
143                 ++i;
144         }
145
146         if (i == _formats.end ()) {
147                 return 0;
148         }
149
150         return *i;
151 }
152
153 /** @param i Id.
154  *  @return Matching Format, or 0.
155  */
156 Format const *
157 Format::from_id (string i)
158 {
159         vector<Format const *>::iterator j = _formats.begin ();
160         while (j != _formats.end() && (*j)->id() != i) {
161                 ++j;
162         }
163
164         if (j == _formats.end ()) {
165                 return 0;
166         }
167
168         return *j;
169 }
170
171
172 /** @param m Metadata, as returned from as_metadata().
173  *  @return Matching Format, or 0.
174  */
175 Format const *
176 Format::from_metadata (string m)
177 {
178         return from_id (m);
179 }
180
181 /** @return All available formats */
182 vector<Format const *>
183 Format::all ()
184 {
185         return _formats;
186 }
187
188 /** @param r Ratio
189  *  @param dcp Size (in pixels) of the images that we should put in a DCP.
190  *  @param id ID (e.g. 185)
191  *  @param n Nick name (e.g. Flat)
192  */
193 FixedFormat::FixedFormat (float r, libdcp::Size dcp, string id, string n, string d)
194         : Format (dcp, id, n, d)
195         , _ratio (r)
196 {
197
198 }
199
200 /** @return Number of pixels (int the DCP image) to pad either side of the film
201  *  (so there are dcp_padding() pixels on the left and dcp_padding() on the right)
202  */
203 int
204 Format::dcp_padding (shared_ptr<const Film> f) const
205 {
206         int p = rint ((_dcp_size.width - (_dcp_size.height * ratio(f))) / 2.0);
207
208         /* This comes out -ve for Scope; bodge it */
209         if (p < 0) {
210                 p = 0;
211         }
212         
213         return p;
214 }
215
216 float
217 Format::container_ratio () const
218 {
219         return static_cast<float> (_dcp_size.width) / _dcp_size.height;
220 }
221
222 VariableFormat::VariableFormat (libdcp::Size dcp, string id, string n, string d)
223         : Format (dcp, id, n, d)
224 {
225
226 }
227
228 float
229 VariableFormat::ratio (shared_ptr<const Film> f) const
230 {
231         libdcp::Size const c = f->cropped_size (f->size ());
232         return float (c.width) / c.height;
233 }
234
235 /** @return A name to be presented to the user */
236 string
237 VariableFormat::name () const
238 {
239         return _nickname;
240 }