Present fixed formats with video content and variable formats with stills.
[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 namespace std;
34
35 vector<Format const *> Format::_formats;
36
37 /** @return A name to be presented to the user */
38 string
39 FixedFormat::name () const
40 {
41         stringstream s;
42         if (!_nickname.empty ()) {
43                 s << _nickname << " (";
44         }
45
46         s << setprecision(3) << (_ratio / 100.0) << ":1";
47
48         if (!_nickname.empty ()) {
49                 s << ")";
50         }
51
52         return s.str ();
53 }
54
55 /** @return Identifier for this format as metadata for a Film's metadata file */
56 string
57 Format::as_metadata () const
58 {
59         return _id;
60 }
61
62 /** Fill our _formats vector with all available formats */
63 void
64 Format::setup_formats ()
65 {
66         _formats.push_back (new FixedFormat (119, Size (1285, 1080), "119", "1.19"));
67         _formats.push_back (new FixedFormat (133, Size (1436, 1080), "133", "1.33"));
68         _formats.push_back (new FixedFormat (138, Size (1485, 1080), "138", "1.375"));
69         _formats.push_back (new FixedFormat (133, Size (1998, 1080), "133-in-flat", "4:3 within Flat"));
70         _formats.push_back (new FixedFormat (137, Size (1480, 1080), "137", "Academy"));
71         _formats.push_back (new FixedFormat (166, Size (1793, 1080), "166", "1.66"));
72         _formats.push_back (new FixedFormat (166, Size (1998, 1080), "166-in-flat", "1.66 within Flat"));
73         _formats.push_back (new FixedFormat (178, Size (1998, 1080), "178-in-flat", "16:9 within Flat"));
74         _formats.push_back (new FixedFormat (185, Size (1998, 1080), "185", "Flat"));
75         _formats.push_back (new FixedFormat (239, Size (2048, 858), "239", "Scope"));
76         _formats.push_back (new VariableFormat (Size (1998, 1080), "var-185", "Flat"));
77         _formats.push_back (new VariableFormat (Size (2048, 858), "var-239", "Scope"));
78 }
79
80 /** @param n Nickname.
81  *  @return Matching Format, or 0.
82  */
83 Format const *
84 Format::from_nickname (string n)
85 {
86         vector<Format const *>::iterator i = _formats.begin ();
87         while (i != _formats.end() && (*i)->nickname() != n) {
88                 ++i;
89         }
90
91         if (i == _formats.end ()) {
92                 return 0;
93         }
94
95         return *i;
96 }
97
98 /** @param i Id.
99  *  @return Matching Format, or 0.
100  */
101 Format const *
102 Format::from_id (string i)
103 {
104         vector<Format const *>::iterator j = _formats.begin ();
105         while (j != _formats.end() && (*j)->id() != i) {
106                 ++j;
107         }
108
109         if (j == _formats.end ()) {
110                 return 0;
111         }
112
113         return *j;
114 }
115
116
117 /** @param m Metadata, as returned from as_metadata().
118  *  @return Matching Format, or 0.
119  */
120 Format const *
121 Format::from_metadata (string m)
122 {
123         return from_id (m);
124 }
125
126 /** @return All available formats */
127 vector<Format const *>
128 Format::all ()
129 {
130         return _formats;
131 }
132
133 /** @param r Ratio multiplied by 100 (e.g. 185)
134  *  @param dcp Size (in pixels) of the images that we should put in a DCP.
135  *  @param id ID (e.g. 185)
136  *  @param n Nick name (e.g. Flat)
137  */
138 FixedFormat::FixedFormat (int r, Size dcp, string id, string n)
139         : Format (dcp, id, n)
140         , _ratio (r)
141 {
142
143 }
144
145 int
146 Format::dcp_padding (Film const * f) const
147 {
148         int p = rint ((_dcp_size.width - (_dcp_size.height * ratio_as_integer(f) / 100.0)) / 2.0);
149
150         /* This comes out -ve for Scope; bodge it */
151         if (p < 0) {
152                 p = 0;
153         }
154         
155         return p;
156 }
157
158 VariableFormat::VariableFormat (Size dcp, string id, string n)
159         : Format (dcp, id, n)
160 {
161
162 }
163
164 int
165 VariableFormat::ratio_as_integer (Film const * f) const
166 {
167         return rint (ratio_as_float (f) * 100);
168 }
169
170 float
171 VariableFormat::ratio_as_float (Film const * f) const
172 {
173         return float (f->size().width) / f->size().height;
174 }
175
176 /** @return A name to be presented to the user */
177 string
178 VariableFormat::name () const
179 {
180         return _nickname;
181 }