Add test for incorrect ISDCF name with full-frame content, and fix it (#1118).
[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 using boost::optional;
31
32 vector<Ratio const *> Ratio::_ratios;
33
34 void
35 Ratio::setup_ratios ()
36 {
37         _ratios.push_back (new Ratio (float(1290) / 1080, "119", _("1.19"),              optional<string>(),      "119"));
38         _ratios.push_back (new Ratio (float(1440) / 1080, "133", _("1.33 (4:3)"),        optional<string>(),      "133"));
39         _ratios.push_back (new Ratio (float(1485) / 1080, "138", _("1.38 (Academy)"),    optional<string>(),      "137"));
40         _ratios.push_back (new Ratio (float(1544) / 1080, "143", _("1.43 (IMAX)"),       optional<string>(),      "143"));
41         _ratios.push_back (new Ratio (float(1800) / 1080, "166", _("1.66"),              optional<string>(),      "166"));
42         _ratios.push_back (new Ratio (float(1920) / 1080, "178", _("1.78 (16:9 or HD)"), optional<string>(),      "178"));
43         _ratios.push_back (new Ratio (float(1998) / 1080, "185", _("1.85 (Flat)"),       string(_("DCI Flat")),   "F"));
44         _ratios.push_back (new Ratio (float(2048) /  872, "235", _("2.35 (35mm Scope)"), optional<string>(),      "S"));
45         _ratios.push_back (new Ratio (float(2048) /  858, "239", _("2.39 (Scope)"),      string(_("DCI Scope")),  "S"));
46         _ratios.push_back (new Ratio (float(2048) / 1080, "190", _("1.90 (Full frame)"), string(_("Full frame")), "C"));
47 }
48
49 Ratio const *
50 Ratio::from_id (string i)
51 {
52         /* We removed the ratio with id 137; replace it with 138 */
53         if (i == "137") {
54                 i = "138";
55         }
56
57         vector<Ratio const *>::iterator j = _ratios.begin ();
58         while (j != _ratios.end() && (*j)->id() != i) {
59                 ++j;
60         }
61
62         if (j == _ratios.end ()) {
63                 return 0;
64         }
65
66         return *j;
67 }
68
69 /** @return Ratio corresponding to a given fractional ratio (+/- 0.01), or 0 */
70 Ratio const *
71 Ratio::from_ratio (float r)
72 {
73         vector<Ratio const *>::iterator j = _ratios.begin ();
74         while (j != _ratios.end() && fabs ((*j)->ratio() - r) > 0.01) {
75                 ++j;
76         }
77
78         if (j == _ratios.end ()) {
79                 return 0;
80         }
81
82         return *j;
83 }
84
85 Ratio const *
86 Ratio::nearest_from_ratio (float r)
87 {
88         Ratio const * nearest = 0;
89         float distance = FLT_MAX;
90
91         for (vector<Ratio const *>::iterator i = _ratios.begin (); i != _ratios.end(); ++i) {
92                 float const d = fabs ((*i)->ratio() - r);
93                 if (d < distance) {
94                         distance = d;
95                         nearest = *i;
96                 }
97         }
98
99         return nearest;
100 }
101
102 vector<Ratio const *>
103 Ratio::containers ()
104 {
105         vector<Ratio const *> r;
106         r.push_back (Ratio::from_id ("185"));
107         r.push_back (Ratio::from_id ("239"));
108         r.push_back (Ratio::from_id ("190"));
109         return r;
110 }
111
112 string
113 Ratio::container_nickname () const
114 {
115         DCPOMATIC_ASSERT (_container_nickname);
116         return *_container_nickname;
117 }