Rename SafeStringStream -> locked_stringstream. Bump deps for removal of stringstream.
[dcpomatic.git] / src / lib / ratio.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "ratio.h"
22 #include "util.h"
23 #include <dcp/types.h>
24 #include <cfloat>
25
26 #include "i18n.h"
27
28 using std::string;
29 using std::vector;
30
31 vector<Ratio const *> Ratio::_ratios;
32
33 void
34 Ratio::setup_ratios ()
35 {
36         _ratios.push_back (new Ratio (float(1290) / 1080, "119", _("1.19"), "119"));
37         _ratios.push_back (new Ratio (float(1440) / 1080, "133", _("4:3"), "133"));
38         _ratios.push_back (new Ratio (float(1485) / 1080, "138", _("Academy"), "137"));
39         _ratios.push_back (new Ratio (float(1800) / 1080, "166", _("1.66"), "166"));
40         _ratios.push_back (new Ratio (float(1920) / 1080, "178", _("16:9"), "178"));
41         _ratios.push_back (new Ratio (float(1998) / 1080, "185", _("Flat"), "F"));
42         _ratios.push_back (new Ratio (float(2048) /  872, "235", _("2.35"), "S"));
43         _ratios.push_back (new Ratio (float(2048) /  858, "239", _("Scope"), "S"));
44         _ratios.push_back (new Ratio (float(2048) / 1080, "full-frame", _("Full frame"), "C"));
45 }
46
47 Ratio const *
48 Ratio::from_id (string i)
49 {
50         /* We removed the ratio with id 137; replace it with 138 */
51         if (i == "137") {
52                 i = "138";
53         }
54
55         vector<Ratio const *>::iterator j = _ratios.begin ();
56         while (j != _ratios.end() && (*j)->id() != i) {
57                 ++j;
58         }
59
60         if (j == _ratios.end ()) {
61                 return 0;
62         }
63
64         return *j;
65 }
66
67 /** @return Ratio corresponding to a given fractional ratio (+/- 0.01), or 0 */
68 Ratio const *
69 Ratio::from_ratio (float r)
70 {
71         vector<Ratio const *>::iterator j = _ratios.begin ();
72         while (j != _ratios.end() && fabs ((*j)->ratio() - r) > 0.01) {
73                 ++j;
74         }
75
76         if (j == _ratios.end ()) {
77                 return 0;
78         }
79
80         return *j;
81 }
82
83 Ratio const *
84 Ratio::nearest_from_ratio (float r)
85 {
86         Ratio const * nearest = 0;
87         float distance = FLT_MAX;
88
89         for (vector<Ratio const *>::iterator i = _ratios.begin (); i != _ratios.end(); ++i) {
90                 float const d = fabs ((*i)->ratio() - r);
91                 if (d < distance) {
92                         distance = d;
93                         nearest = *i;
94                 }
95         }
96
97         return nearest;
98 }