Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hpi.cpp
Go to the documentation of this file.
1 
17 /* This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public
19  * License as published by the Free Software Foundation; either
20  * version 2 of the License, or (at your option) any later version.
21  *
22  * This software is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25  * General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public
28  * License along with this software. If not, see
29  * <http://www.gnu.org/licenses/>.
30  *
31  * KFJ 2011-01-18
32  *
33  */
34 
35 // uncomment this if you want diagnostics:
36 // #define HPI_VERBOSE
37 
38 // first pull in the Python interface classes
40 
41 // pull in the declaration of callhpi() to ensure it's consistent
42 // with the definition below
44 
45 // callhpi() is a variadic function, so we need stdarg.h
46 // This is old-fashioned and may become deprecated, but for
47 // now it's very convenient.
48 #include <stdarg.h>
49 
50 namespace hpi
51 {
54 
55 int callhpi ( const char* plugin_name ,
56  int argc ,
57  ... )
58 {
59  va_list arglist ;
60  va_start ( arglist , argc ) ;
61 
62  // activate the python interface if it's not on already
63  if ( ! hpi_instance.activate() )
64  {
65  // trouble
66  fprintf ( stderr , "HPI ERROR: failed to activate Python interface\n" ) ;
67  va_end ( arglist ) ;
68  return -20 ;
69  }
70 
71  // we want to build a python argument list with argc + 1 arguments,
72  // + 1 because the first argument is the plugin name:
73  python_arglist pyarglist ( argc + 1 ) ;
74  pyarglist.add ( plugin_name ) ;
75 
76  for ( int argno = 0 ; argno < argc ; argno++ )
77  {
78  // extract the type name
79  const char* type_name = va_arg ( arglist , const char* ) ;
80  // extract the pointer to argument object
81  void* pvalue = va_arg ( arglist , void* ) ;
82  // make a python object from this information
83  PyObject* pyarg = pyarglist.make_hsi_object ( type_name , pvalue ) ;
84  // try add it to the arglist
85  if ( ! pyarglist.add ( pyarg ) )
86  {
87  // oops... this went wrong
88  va_end ( arglist ) ;
89  return -21 ;
90  }
91  }
92  va_end ( arglist ) ; // we're done with that
93 
94  // seems we're okay. get the argument tuple out
95  PyObject* arg_tuple = pyarglist.yield() ;
96  if ( ! arg_tuple )
97  {
98  // oops... this went wrong
99  return -22 ;
100  }
101 
102  // now let's call the thing. This is done by passing the argument
103  // tuple to hpi_dispatch, a Python function defined in hpi.py
104  // this function interprets the first argument as a plugin name
105  // and calls that plugin with the remaining parameters.
106  return hpi_instance.call_hpi ( "hpi_dispatch" , arg_tuple );
107 }
108 
109 } // namespace hsi
110 
111 // When I didn't yet have an entry point from hugin to call hpi,
112 // I used the next bit as my main() code. For now this is left
113 // inside, it may be handy later on.
114 
115 #ifdef standalone
116 
117 #include <fstream>
118 
119 using namespace std;
120 using namespace HuginBase;
121 using namespace hugin_utils;
122 using namespace AppBase;
123 
124 // we use this function to make a panorama from a pto file
125 // so we have something to pass to the hsi module. Later on,
126 // this function will disappear and the calls will generate
127 // from hugin
128 
129 HuginBase::Panorama* pano_open ( const char* infile )
130 {
131  string input ( infile ) ;
132 
133  ifstream prjfile(input.c_str());
134 
135  if (!prjfile.good())
136  {
137  fprintf ( stderr ,
138  "could not open script %s\n" ,
139  input.c_str() ) ;
140  return NULL;
141  }
142 
144 
145  pano->setFilePrefix ( getPathPrefix ( input ) );
146  AppBase::DocumentData::ReadWriteError err = pano->readData(prjfile);
147 
148  if (err != DocumentData::SUCCESSFUL)
149  {
150  fprintf ( stderr ,
151  "%s %s\n%s %d\n" ,
152  "error while parsing panos tool script:" ,
153  input.c_str() ,
154  "DocumentData::ReadWriteError code:" ,
155  err ) ;
156  delete pano ;
157  return NULL ;
158  }
159  fprintf ( stderr , "opened pano: %p\n" , pano ) ;
160  return pano ;
161 }
162 
163 // demo-main that opens the pto named in argv[1] and passes
164 // the Panorama pointer to a function named by argv[2]
165 // in the hsi module
166 
167 int main ( int argc , char* argv[] )
168 {
169  HuginBase::Panorama* pano = pano_open ( argv[1] ) ;
170  if ( ! pano )
171  {
172  fprintf(stderr, "Failed to open pano\n");
173  return 1;
174  }
175 
176  int success = hsi::callhpi ( argv[2] , 1 , "HuginBase::Panorama*" , pano ) ;
177 
178  return success ;
179 }
180 #endif // standalone
core classes of the hpi interface, not for user code
void setFilePrefix(std::string prefix)
sets the path prefix of the images reffered with relative paths
Definition: Panorama.h:664
PyObject * make_hsi_object(const char *hsi_type, void *hugin_value)
general function to make a Python object from a hugin object.
Model for a panorama.
Definition: Panorama.h:152
C++ call interface to hpi.
std::string getPathPrefix(const std::string &filename)
Get the path to a filename.
Definition: utils.cpp:184
bool activate()
: loads the necessary modules hsi and hpi.
Definition: hpi_classes.cpp:67
int callhpi(const char *plugin_name, int argc,...)
simplified call interface to the Python Plugin facility.
Definition: hpi.cpp:55
a class which encapsulates the python interface.
Definition: hpi_classes.h:50
static python_interface hpi_instance
this is where we keep the single instance of class python_interface
Definition: hpi.cpp:53
PyObject * yield()
returns the generated PyObject Note that this will only succeed if the argument count is correct...
bool add(PyObject *python_arg)
add a python object to the argument list.
helper class to generated PyObject from C/C++/hugin classes
Definition: hpi_classes.h:88
int call_hpi(const char *hpi_func, PyObject *pArgs)
call a routine in the hsi module with a bunch of parameters.
Definition: hpi_classes.cpp:99
int main(int argc, char *argv[])
Definition: Main.cpp:167