summaryrefslogtreecommitdiff
path: root/src/lib/format.cc
blob: eb42593feada4da576d645842bbd44cb13d60b42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/** @file src/format.cc
 *  @brief Class to describe a format (aspect ratio) that a Film should
 *  be shown in.
 */

#include <sstream>
#include <cstdlib>
#include <cassert>
#include <iomanip>
#include <iostream>
#include "format.h"
#include "film.h"

using std::string;
using std::setprecision;
using std::stringstream;
using std::vector;
using boost::shared_ptr;

vector<Format const *> Format::_formats;

/** @return A name to be presented to the user */
string
FixedFormat::name () const
{
	stringstream s;
	if (!_nickname.empty ()) {
		s << _nickname << " (";
	}

	s << setprecision(3) << (_ratio / 100.0) << ":1";

	if (!_nickname.empty ()) {
		s << ")";
	}

	return s.str ();
}

/** @return Identifier for this format as metadata for a Film's metadata file */
string
Format::as_metadata () const
{
	return _id;
}

/** Fill our _formats vector with all available formats */
void
Format::setup_formats ()
{
	_formats.push_back (new FixedFormat (119, Size (1285, 1080), "119", "1.19", "F"));
	_formats.push_back (new FixedFormat (133, Size (1436, 1080), "133", "1.33", "F"));
	_formats.push_back (new FixedFormat (138, Size (1485, 1080), "138", "1.375", "F"));
	_formats.push_back (new FixedFormat (133, Size (1998, 1080), "133-in-flat", "4:3 within Flat", "F"));
	_formats.push_back (new FixedFormat (137, Size (1480, 1080), "137", "Academy", "F"));
	_formats.push_back (new FixedFormat (166, Size (1793, 1080), "166", "1.66", "F"));
	_formats.push_back (new FixedFormat (166, Size (1998, 1080), "166-in-flat", "1.66 within Flat", "F"));
	_formats.push_back (new FixedFormat (178, Size (1998, 1080), "178-in-flat", "16:9 within Flat", "F"));
	_formats.push_back (new FixedFormat (185, Size (1998, 1080), "185", "Flat", "F"));
	_formats.push_back (new FixedFormat (239, Size (2048, 858), "239", "Scope", "S"));
	_formats.push_back (new VariableFormat (Size (1998, 1080), "var-185", "Flat", "F"));
	_formats.push_back (new VariableFormat (Size (2048, 858), "var-239", "Scope", "S"));
}

/** @param n Nickname.
 *  @return Matching Format, or 0.
 */
Format const *
Format::from_nickname (string n)
{
	vector<Format const *>::iterator i = _formats.begin ();
	while (i != _formats.end() && (*i)->nickname() != n) {
		++i;
	}

	if (i == _formats.end ()) {
		return 0;
	}

	return *i;
}

/** @param i Id.
 *  @return Matching Format, or 0.
 */
Format const *
Format::from_id (string i)
{
	vector<Format const *>::iterator j = _formats.begin ();
	while (j != _formats.end() && (*j)->id() != i) {
		++j;
	}

	if (j == _formats.end ()) {
		return 0;
	}

	return *j;
}


/** @param m Metadata, as returned from as_metadata().
 *  @return Matching Format, or 0.
 */
Format const *
Format::from_metadata (string m)
{
	return from_id (m);
}

/** @return All available formats */
vector<Format const *>
Format::all ()
{
	return _formats;
}

/** @param r Ratio multiplied by 100 (e.g. 185)
 *  @param dcp Size (in pixels) of the images that we should put in a DCP.
 *  @param id ID (e.g. 185)
 *  @param n Nick name (e.g. Flat)
 */
FixedFormat::FixedFormat (int r, Size dcp, string id, string n, string d)
	: Format (dcp, id, n, d)
	, _ratio (r)
{

}

int
Format::dcp_padding (shared_ptr<const Film> f) const
{
	int p = rint ((_dcp_size.width - (_dcp_size.height * ratio_as_integer(f) / 100.0)) / 2.0);

	/* This comes out -ve for Scope; bodge it */
	if (p < 0) {
		p = 0;
	}
	
	return p;
}

VariableFormat::VariableFormat (Size dcp, string id, string n, string d)
	: Format (dcp, id, n, d)
{

}

int
VariableFormat::ratio_as_integer (shared_ptr<const Film> f) const
{
	return rint (ratio_as_float (f) * 100);
}

float
VariableFormat::ratio_as_float (shared_ptr<const Film> f) const
{
	return float (f->size().width) / f->size().height;
}

/** @return A name to be presented to the user */
string
VariableFormat::name () const
{
	return _nickname;
}