Hugin trunk 0.1
Loading...
Searching...
No Matches
hpi_classes.cpp
Go to the documentation of this file.
1
15/* This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public
17 * License as published by the Free Software Foundation; either
18 * version 2 of the License, or (at your option) any later version.
19 *
20 * This software is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public
26 * License along with this software. If not, see
27 * <http://www.gnu.org/licenses/>.
28 *
29 * KFJ 2011-01-18
30 *
31 */
32
33#include "hpi_classes.h"
34
35namespace hpi
36{
37
39{
40#ifdef HPI_VERBOSE
41 fprintf ( stdout , "HPI: loading module %s\n" , name ) ;
42#endif
44 if ( ! pModule )
45 {
46 PyErr_Print () ;
47 fprintf ( stderr , "HPI ERROR: Failed to load module %s\n" , name );
48 return NULL ;
49 }
50 return pModule;
51}
52
54{
55 if ( activated ) // the interface was used, do cleanup
56 {
57#ifdef HPI_VERBOSE
58 fprintf ( stdout , "HPI: finalizing Python\n" ) ;
59#endif
60 // cleaning up
64 }
65}
66
68{
69 if ( activated )
70 {
71 return true ;
72 }
73#ifdef HPI_VERBOSE
74 fprintf ( stdout , "HPI: initializing Python\n" ) ;
75#endif
77 // load hsi module
78 hsi_module = load_module ( "hsi" ) ;
79 if ( hsi_module )
80 {
81 // load hpi module
82 hpi_module = load_module ("hpi");
83 if ( hpi_module )
84 {
85 activated = true ;
86 }
87 else
88 {
90 }
91 }
92 if ( ! activated )
93 {
94 Py_Finalize() ;
95 }
96 return activated ;
97}
98
100 PyObject* pArgs )
101{
102 int result = -1 ;
103#ifdef HPI_VERBOSE
104 fprintf ( stdout , "HPI: calling %s\n" , hpi_func ) ;
105#endif
106 // look up the desired function in the hpi module's namespace
108 // if it's found and can be called, go ahead call it
109 if ( pFunc && PyCallable_Check ( pFunc ) )
110 {
112 if ( pValue != NULL )
113 {
114 // the function should have returned an integer
115 result = PyLong_AsLong ( pValue ) ;
117 }
118 }
119 Py_XDECREF ( pFunc ) ;
120
121#ifdef HPI_VERBOSE
122 fprintf ( stdout , "HPI: call returns: %d\n" , result );
123#endif
124 return result ;
125};
126
128 void* hugin_value )
129{
131#ifdef HPI_VERBOSE
132 fprintf ( stdout ,
133 "HPI: making a %s from %p\n" ,
134 hsi_type ,
135 hugin_value );
136#endif
137 if ( ! swigtype )
138 {
139 fprintf ( stderr,
140 "HPI ERROR: can't find SWIG type for %s\n" ,
141 hsi_type );
142 return NULL;
143 }
144
145 // we have managed to gain the SWIG type information,
146 // se we can generate the Python object
147 return SWIG_NewPointerObj ( hugin_value , swigtype , 0 );
148};
149
150python_arglist::python_arglist ( int _argc ) : argc ( _argc ) , have ( 0 )
151{
152 pArgs = PyTuple_New ( argc );
153}
154
159
161{
162 if ( ( ! python_arg ) || ( have >= argc ) )
163 {
164 return false ;
165 }
167 return true ;
168}
169
170bool python_arglist::add ( const char* str )
171{
172#ifdef HPI_VERBOSE
173 fprintf ( stdout , "HPI: making a PyString from '%s'\n" , str ) ;
174#endif
175
176#if PY_MAJOR_VERSION>=3
177 return add ( PyUnicode_FromString(str));
178#else
179 return add ( PyString_FromString(str));
180#endif
181}
182
184{
185 if ( argc != have )
186 {
187 return NULL ;
188 }
189 return pArgs ;
190}
191
192} //namespace
python_arglist(int _argc)
the constructor is called with the number of arguments the argument list is meant to contain.
bool add(PyObject *python_arg)
add a python object to the argument list.
PyObject * make_hsi_object(const char *hsi_type, void *hugin_value)
general function to make a Python object from a hugin object.
PyObject * yield()
returns the generated PyObject Note that this will only succeed if the argument count is correct.
~python_arglist()
destructor, does cleanup
PyObject * hsi_module
pointer to loaded hsi module
Definition hpi_classes.h:56
bool activate()
: loads the necessary modules hsi and hpi.
int call_hpi(const char *hpi_func, PyObject *pArgs)
call a routine in the hsi module with a bunch of parameters.
PyObject * load_module(const char *name)
general module-loading function
bool activated
flag, true if activated
Definition hpi_classes.h:54
PyObject * hpi_module
pointer to loaded hpi module
Definition hpi_classes.h:58
core classes of the hpi interface, not for user code
Definition hpi.cpp:51
std::vector< deghosting::BImagePtr > threshold(const std::vector< deghosting::FImagePtr > &inputImages, const double threshold, const uint16_t flags)
Threshold function used for creating alpha masks for images.
Definition threshold.h:41