Fix a load of stuff that wasn't being freed on close.
[dcpomatic.git] / src / lib / ratio.cc
1 /*
2     Copyright (C) 2013-2021 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
22 #include "ratio.h"
23 #include "util.h"
24 #include "config.h"
25 #include <dcp/types.h>
26 #include <cfloat>
27
28 #include "i18n.h"
29
30
31 using std::string;
32 using std::vector;
33 using boost::optional;
34
35
36 vector<Ratio> Ratio::_ratios;
37
38
39 void
40 Ratio::setup_ratios ()
41 {
42         /* This must only be called once as we rely on the addresses of objects in _ratios staying the same */
43         DCPOMATIC_ASSERT (_ratios.empty());
44
45         _ratios.push_back (Ratio(float(1290) / 1080, "119", _("1.19"),              {},                      "119"));
46         _ratios.push_back (Ratio(float(1440) / 1080, "133", _("1.33 (4:3)"),        {},                      "133"));
47         _ratios.push_back (Ratio(float(1485) / 1080, "138", _("1.38 (Academy)"),    {},                      "137"));
48         _ratios.push_back (Ratio(float(1544) / 1080, "143", _("1.43 (IMAX)"),       {},                      "143"));
49         _ratios.push_back (Ratio(float(1800) / 1080, "166", _("1.66"),              {},                      "166"));
50         _ratios.push_back (Ratio(float(1920) / 1080, "178", _("1.78 (16:9 or HD)"), {},                      "178"));
51         _ratios.push_back (Ratio(float(1998) / 1080, "185", _("1.85 (Flat)"),       string(_("DCI Flat")),   "F"));
52         _ratios.push_back (Ratio(float(2048) /  872, "235", _("2.35 (35mm Scope)"), {},                      "S"));
53         _ratios.push_back (Ratio(float(2048) /  858, "239", _("2.39 (Scope)"),      string(_("DCI Scope")),  "S"));
54         _ratios.push_back (Ratio(float(2048) / 1080, "190", _("1.90 (Full frame)"), string(_("Full frame")), "C"));
55 }
56
57
58 vector<Ratio const *>
59 Ratio::all ()
60 {
61         vector<Ratio const *> pointers;
62         for (Ratio& ratio: _ratios) {
63                 pointers.push_back (&ratio);
64         }
65         return pointers;
66 }
67
68
69 Ratio const *
70 Ratio::from_id (string i)
71 {
72         /* We removed the ratio with id 137; replace it with 138 */
73         if (i == "137") {
74                 i = "138";
75         }
76
77         auto j = _ratios.begin ();
78         while (j != _ratios.end() && j->id() != i) {
79                 ++j;
80         }
81
82         if (j == _ratios.end()) {
83                 return nullptr;
84         }
85
86         return &(*j);
87 }
88
89
90 /** @return Ratio corresponding to a given fractional ratio (+/- 0.01), or 0 */
91 Ratio const *
92 Ratio::from_ratio (float r)
93 {
94         auto j = _ratios.begin ();
95         while (j != _ratios.end() && fabs(j->ratio() - r) > 0.01) {
96                 ++j;
97         }
98
99         if (j == _ratios.end ()) {
100                 return nullptr;
101         }
102
103         return &(*j);
104 }
105
106
107 Ratio const *
108 Ratio::nearest_from_ratio (float r)
109 {
110         vector<Ratio>::const_iterator nearest = _ratios.end();
111         float distance = FLT_MAX;
112
113         for (auto i = _ratios.begin(); i != _ratios.end(); ++i) {
114                 float const d = fabs(i->ratio() - r);
115                 if (d < distance) {
116                         distance = d;
117                         nearest = i;
118                 }
119         }
120
121         DCPOMATIC_ASSERT (nearest != _ratios.end());
122
123         return &(*nearest);
124 }
125
126 vector<Ratio const *>
127 Ratio::containers ()
128 {
129         if (Config::instance()->allow_any_container()) {
130                 return all();
131         }
132
133         vector<Ratio const *> r;
134         r.push_back (Ratio::from_id ("185"));
135         r.push_back (Ratio::from_id ("239"));
136         return r;
137 }
138
139
140 string
141 Ratio::container_nickname () const
142 {
143         if (!_container_nickname) {
144                 /* Fall back to the image nickname; this just for when non-standard container
145                    ratios are enabled.
146                 */
147                 return _image_nickname;
148         }
149
150         return *_container_nickname;
151 }