43c1ac81cb4213ec43895e7c0412ab0c802c16d9
[dcpomatic.git] / src / lib / scaler.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/scaler.cc
21  *  @brief A class to describe one of FFmpeg's software scalers.
22  */
23
24 #include "dcpomatic_assert.h"
25 #include "scaler.h"
26 extern "C" {
27 #include <libswscale/swscale.h>
28 }
29 #include <iostream>
30 #include <cassert>
31
32 #include "i18n.h"
33
34 using namespace std;
35
36 vector<Scaler const *> Scaler::_scalers;
37
38 /** @param f FFmpeg id.
39  *  @param i Our id.
40  *  @param n User-visible name.
41  */
42 Scaler::Scaler (int f, string i, string n)
43         : _ffmpeg_id (f)
44         , _id (i)
45         , _name (n)
46 {
47
48 }
49
50 /** @return All available scalers */
51 vector<Scaler const *>
52 Scaler::all ()
53 {
54         return _scalers;
55 }
56
57 /** Set up the static _scalers vector; must be called before from_*
58  *  methods are used.
59  */
60 void
61 Scaler::setup_scalers ()
62 {
63         _scalers.push_back (new Scaler (SWS_BICUBIC, N_("bicubic"), _("Bicubic")));
64         _scalers.push_back (new Scaler (SWS_X, N_("x"), _("X")));
65         _scalers.push_back (new Scaler (SWS_AREA, N_("area"), _("Area")));
66         _scalers.push_back (new Scaler (SWS_GAUSS, N_("gauss"), _("Gaussian")));
67         _scalers.push_back (new Scaler (SWS_LANCZOS, N_("lanczos"), _("Lanczos")));
68         _scalers.push_back (new Scaler (SWS_SINC, N_("sinc"), _("Sinc")));
69         _scalers.push_back (new Scaler (SWS_SPLINE, N_("spline"), _("Spline")));
70         _scalers.push_back (new Scaler (SWS_BILINEAR, N_("bilinear"), _("Bilinear")));
71         _scalers.push_back (new Scaler (SWS_FAST_BILINEAR, N_("fastbilinear"), _("Fast Bilinear")));
72 }
73
74 /** @param id One of our ids.
75  *  @return Corresponding scaler, or 0.
76  */
77 Scaler const *
78 Scaler::from_id (string id)
79 {
80         vector<Scaler const *>::iterator i = _scalers.begin ();
81         while (i != _scalers.end() && (*i)->id() != id) {
82                 ++i;
83         }
84
85         if (i == _scalers.end ()) {
86                 return 0;
87         }
88
89         return *i;
90 }
91
92 /** @param s A scaler from our static list.
93  *  @return Index of the scaler with the list, or -1.
94  */
95 int
96 Scaler::as_index (Scaler const * s)
97 {
98         vector<Scaler*>::size_type i = 0;
99         while (i < _scalers.size() && _scalers[i] != s) {
100                 ++i;
101         }
102
103         if (i == _scalers.size ()) {
104                 return -1;
105         }
106
107         return i;
108 }
109
110 /** @param i An index returned from as_index().
111  *  @return Corresponding scaler.
112  */
113 Scaler const *
114 Scaler::from_index (int i)
115 {
116         DCPOMATIC_ASSERT (i <= int(_scalers.size ()));
117         return _scalers[i];
118 }