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