Various markup and tweaks.
[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 / 100.0) << 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 (new FixedFormat (119, libdcp::Size (1285, 1080), N_("119"), _("1.19"), N_("F")));
75         _formats.push_back (new FixedFormat (133, libdcp::Size (1436, 1080), N_("133"), _("1.33"), N_("F")));
76         _formats.push_back (new FixedFormat (138, libdcp::Size (1485, 1080), N_("138"), _("1.375"), N_("F")));
77         _formats.push_back (new FixedFormat (133, libdcp::Size (1998, 1080), N_("133-in-flat"), _("4:3 within Flat"), N_("F")));
78         _formats.push_back (new FixedFormat (137, libdcp::Size (1480, 1080), N_("137"), _("Academy"), N_("F")));
79         _formats.push_back (new FixedFormat (166, libdcp::Size (1793, 1080), N_("166"), _("1.66"), N_("F")));
80         _formats.push_back (new FixedFormat (166, libdcp::Size (1998, 1080), N_("166-in-flat"), _("1.66 within Flat"), N_("F")));
81         _formats.push_back (new FixedFormat (178, libdcp::Size (1998, 1080), N_("178-in-flat"), _("16:9 within Flat"), N_("F")));
82         _formats.push_back (new FixedFormat (178, libdcp::Size (1920, 1080), N_("178"), _("16:9"), N_("F")));
83         _formats.push_back (new FixedFormat (185, libdcp::Size (1998, 1080), N_("185"), _("Flat"), N_("F")));
84         _formats.push_back (new FixedFormat (239, libdcp::Size (2048, 858), N_("239"), _("Scope"), N_("S")));
85         _formats.push_back (new VariableFormat (libdcp::Size (1998, 1080), N_("var-185"), _("Flat"), N_("F")));
86         _formats.push_back (new VariableFormat (libdcp::Size (2048, 858), N_("var-239"), _("Scope"), N_("S")));
87 }
88
89 /** @param n Nickname.
90  *  @return Matching Format, or 0.
91  */
92 Format const *
93 Format::from_nickname (string n)
94 {
95         vector<Format const *>::iterator i = _formats.begin ();
96         while (i != _formats.end() && (*i)->nickname() != n) {
97                 ++i;
98         }
99
100         if (i == _formats.end ()) {
101                 return 0;
102         }
103
104         return *i;
105 }
106
107 /** @param i Id.
108  *  @return Matching Format, or 0.
109  */
110 Format const *
111 Format::from_id (string i)
112 {
113         vector<Format const *>::iterator j = _formats.begin ();
114         while (j != _formats.end() && (*j)->id() != i) {
115                 ++j;
116         }
117
118         if (j == _formats.end ()) {
119                 return 0;
120         }
121
122         return *j;
123 }
124
125
126 /** @param m Metadata, as returned from as_metadata().
127  *  @return Matching Format, or 0.
128  */
129 Format const *
130 Format::from_metadata (string m)
131 {
132         return from_id (m);
133 }
134
135 /** @return All available formats */
136 vector<Format const *>
137 Format::all ()
138 {
139         return _formats;
140 }
141
142 /** @param r Ratio multiplied by 100 (e.g. 185)
143  *  @param dcp Size (in pixels) of the images that we should put in a DCP.
144  *  @param id ID (e.g. 185)
145  *  @param n Nick name (e.g. Flat)
146  */
147 FixedFormat::FixedFormat (int r, libdcp::Size dcp, string id, string n, string d)
148         : Format (dcp, id, n, d)
149         , _ratio (r)
150 {
151
152 }
153
154 /** @return Number of pixels (int the DCP image) to pad either side of the film
155  *  (so there are dcp_padding() pixels on the left and dcp_padding() on the right)
156  */
157 int
158 Format::dcp_padding (shared_ptr<const Film> f) const
159 {
160         int p = rint ((_dcp_size.width - (_dcp_size.height * ratio_as_integer(f) / 100.0)) / 2.0);
161
162         /* This comes out -ve for Scope; bodge it */
163         if (p < 0) {
164                 p = 0;
165         }
166         
167         return p;
168 }
169
170 float
171 Format::container_ratio_as_float () const
172 {
173         return static_cast<float> (_dcp_size.width) / _dcp_size.height;
174 }
175
176 VariableFormat::VariableFormat (libdcp::Size dcp, string id, string n, string d)
177         : Format (dcp, id, n, d)
178 {
179
180 }
181
182 int
183 VariableFormat::ratio_as_integer (shared_ptr<const Film> f) const
184 {
185         return rint (ratio_as_float (f) * 100);
186 }
187
188 float
189 VariableFormat::ratio_as_float (shared_ptr<const Film> f) const
190 {
191         return float (f->size().width) / f->size().height;
192 }
193
194 /** @return A name to be presented to the user */
195 string
196 VariableFormat::name () const
197 {
198         return _nickname;
199 }