3 #include "gdcmCommon.h"
6 #include "gdcmDictEntry.h"
7 #include "gdcmDictSet.h"
8 #include "gdcmDicomDir.h"
9 #include "gdcmDicomDirElement.h"
10 #include "gdcmDicomDirImage.h"
11 #include "gdcmDicomDirMeta.h"
12 #include "gdcmDicomDirObject.h"
13 #include "gdcmDicomDirPatient.h"
14 #include "gdcmDicomDirStudy.h"
15 #include "gdcmDicomDirSerie.h"
16 #include "gdcmDocEntrySet.h"
17 #include "gdcmDocument.h"
18 #include "gdcmElementSet.h"
20 #include "gdcmGlobal.h"
21 #include "gdcmHeader.h"
22 #include "gdcmSerieHeader.h"
23 #include "gdcmRLEFramesInfo.h"
24 #include "gdcmJPEGFragmentsInfo.h"
25 #include "gdcmSQItem.h"
27 #include "gdcmValEntry.h"
29 ////////////////////////////////////////////////////////////////////////////
30 /// Refer (below) to the definition of multi-argument typemap
31 /// %typemap(python, in)
32 /// ( gdcm::DicomDir::Method*, void*, gdcm::DicomDir::Method*)
33 /// for detail on gdcmPythonVoidFunc() and gdcmPythonVoidFuncArgDelete().
34 void gdcmPythonVoidFunc(void *arg)
36 PyObject *arglist, *result;
37 PyObject *func = (PyObject *)arg;
39 arglist = Py_BuildValue("()");
41 result = PyEval_CallObject(func, arglist);
50 if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
52 std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
59 void gdcmPythonVoidFuncArgDelete(void *arg)
61 PyObject *func = (PyObject *)arg;
68 /// This is required in order to avoid %including all the gdcm include files.
73 /////////////////////// typemap section ////////////////////////////////////
75 ////////////////////////////////////////////////
76 // Convert an STL list<> to a python native list
77 %typemap(out) std::list<std::string> *
79 PyObject* NewItem = (PyObject*)0;
80 PyObject* NewList = PyList_New(0); // The result of this typemap
82 for (std::list<std::string>::iterator NewString = ($1)->begin();
83 NewString != ($1)->end();
86 NewItem = PyString_FromString(NewString->c_str());
87 PyList_Append( NewList, NewItem);
92 //////////////////////////////////////////////////////////////////
93 // Convert an STL map<> (hash table) to a python native dictionary
94 %typemap(out) std::map<std::string, std::list<std::string> > *
96 PyObject* NewDict = PyDict_New(); // The result of this typemap
97 PyObject* NewKey = (PyObject*)0;
98 PyObject* NewVal = (PyObject*)0;
100 for (std::map<std::string,
101 std::list<std::string> >::iterator tag = ($1)->begin();
102 tag != ($1)->end(); ++tag)
104 std::string first = tag->first;
105 // Do not publish entries whose keys is made of spaces
106 if (first.length() == 0)
108 NewKey = PyString_FromString(first.c_str());
109 PyObject* NewList = PyList_New(0);
110 for (std::list<std::string>::iterator Item = tag->second.begin();
111 Item != tag->second.end();
114 NewVal = PyString_FromString(Item->c_str());
115 PyList_Append( NewList, NewVal);
117 PyDict_SetItem( NewDict, NewKey, NewList);
122 /////////////////////////////////////////////////////////
123 // Convert a c++ hash table in a python native dictionary
124 %typemap(out) gdcm::TagDocEntryHT &
126 PyObject* NewDict = PyDict_New(); // The result of this typemap
127 std::string RawName; // Element name as gotten from gdcm
128 PyObject* NewKey = (PyObject*)0; // Associated name as python object
129 std::string RawValue; // Element value as gotten from gdcm
130 PyObject* NewVal = (PyObject*)0; // Associated value as python object
132 for (gdcm::TagDocEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag)
134 // The element name shall be the key:
135 RawName = tag->second->GetName();
136 // gdcm unrecognized (including not loaded because their size exceeds
137 // the user specified treshold) elements are exported with their
139 if (RawName == "Unknown")
140 RawName = tag->second->GetKey();
141 NewKey = PyString_FromString(RawName.c_str());
143 // Element values are striped from leading/trailing spaces
144 gdcm::ValEntry* ValEntryPtr = dynamic_cast< gdcm::ValEntry* >(tag->second);
147 RawValue = ValEntryPtr->GetValue();
151 NewVal = PyString_FromString(RawValue.c_str());
152 PyDict_SetItem( NewDict, NewKey, NewVal);
157 /////////////////////////////////////
158 %typemap(out) ListDicomDirPatient &
160 PyObject* NewItem = (PyObject*)0;
161 $result = PyList_New(0); // The result of this typemap
163 for (std::list<gdcm::DicomDirPatient *>::iterator New = ($1)->begin();
164 New != ($1)->end(); ++New)
166 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirPatient,1);
167 PyList_Append($result, NewItem);
171 %typemap(out) ListDicomDirStudy &
173 PyObject* NewItem = (PyObject*)0;
174 $result = PyList_New(0); // The result of this typemap
176 for (std::list<gdcm::DicomDirStudy *>::iterator New = ($1)->begin();
177 New != ($1)->end(); ++New)
179 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirStudy,1);
180 PyList_Append($result, NewItem);
184 %typemap(out) ListDicomDirSerie &
186 PyObject* NewItem = (PyObject*)0;
187 $result = PyList_New(0); // The result of this typemap
189 for (std::list<gdcm::DicomDirSerie *>::iterator New = ($1)->begin();
190 New != ($1)->end(); ++New)
192 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirSerie,1);
193 PyList_Append($result, NewItem);
197 %typemap(out) ListDicomDirImage &
199 PyObject* NewItem = (PyObject*)0;
200 $result = PyList_New(0); // The result of this typemap
202 for (std::list<gdcm::DicomDirImage *>::iterator New = ($1)->begin();
203 New != ($1)->end(); ++New)
205 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirImage,1);
206 PyList_Append($result, NewItem);
210 ////////////////////////////////////////////////////////////////////////////
211 // Multi-argument typemap designed for wrapping the progress related methods
212 // in order to control from an external application the computation of
213 // a DicomDir object (see DicomDir::SetStartMethod*,
214 // DicomDir::SetProgressMethod* and DicomDir::SetEndMethod*).
215 // Motivation: since DicomDir parsing can be quite long, a GUI application
216 // needs to display the avancement and potentially offer a
217 // cancel method to the user (when this one feels things are
218 // longer than expected).
219 // Example of usage: refer to demo/DicomDirProgressMethod.py
220 // Note: Uses gdcmPythonVoidFunc and gdcmPythonVoidFuncArgDelete defined
221 // in the Swig verbatim section of this gdcm.i i.e. in the above section
222 // enclosed within the %{ ... %} scope operator ).
223 %typemap(python, in) ( gdcm::DicomDir::Method *,
225 gdcm::DicomDir::Method * = NULL )
230 $1=gdcmPythonVoidFunc;
232 $3=gdcmPythonVoidFuncArgDelete;
242 //////////////////// STL string versus Python str ////////////////////////
243 // Convertion returning a C++ string.
244 %typemap(out) string, std::string
246 $result = PyString_FromString(($1).c_str());
249 // Convertion of incoming Python str to STL string
250 %typemap(python, in) const std::string, std::string
252 $1 = PyString_AsString($input);
255 // Same convertion as above but references (since swig converts C++
256 // refererences to pointers)
257 %typemap(python, in) std::string const &
259 $1 = new std::string( PyString_AsString( $input ) );
262 ////////////////////////////////////////////////////////////////////////////
263 // Because overloading and %rename don't work together (see below Note 1)
264 // we need to ignore some methods (e.g. the overloaded default constructor).
265 // The gdcm::Header class doesn't have any SetFilename method anyhow, and
266 // this constructor is only used internaly (not from the API) so this is
268 %ignore gdcm::binary_write(std::ostream &,uint32_t const &);
269 %ignore gdcm::binary_write(std::ostream &,uint16_t const &);
271 %ignore gdcm::Header::Header();
272 %ignore gdcm::DicomDir::DicomDir();
274 ////////////////////////////////////////////////////////////////////////////
275 // Warning: Order matters !
276 %include "gdcmCommon.h"
277 %include "gdcmBase.h"
278 %include "gdcmDictEntry.h"
279 %include "gdcmDict.h"
280 %include "gdcmDocEntrySet.h"
281 %include "gdcmElementSet.h"
282 %include "gdcmDictSet.h"
283 %include "gdcmSQItem.h"
284 %include "gdcmDicomDirElement.h"
285 %include "gdcmDicomDirObject.h"
286 %include "gdcmDicomDirImage.h"
287 %include "gdcmDicomDirSerie.h"
288 %include "gdcmDicomDirStudy.h"
289 %include "gdcmDicomDirPatient.h"
290 %include "gdcmDicomDirMeta.h"
291 %include "gdcmDocument.h"
292 %include "gdcmHeader.h"
293 %include "gdcmSerieHeader.h"
294 %include "gdcmFile.h"
295 %include "gdcmUtil.h"
296 %include "gdcmGlobal.h"
297 %include "gdcmDicomDir.h"
299 ////////////////////////////////////////////////////////////////////////////
300 // Notes on swig and this file gdcm.i:
302 /////////////////////////////////////
303 // Note 1: swig collision of method overloading and %typemap
304 // Consider the following junk.i file:
308 // #include <iostream>
309 // void Junk(std::string const & bozo) { std::cout << bozo << std::endl; }
310 // void Junk() { std::cout << "Renamed Junk()" << std::endl; }
313 // %typemap(python, in) std::string const &
315 // $1 = new std::string( PyString_AsString( $input ) );
318 // void Junk(std::string const & bozo);
320 // that we compile on linux with:
321 // swig -c++ -python junk.i
322 // g++ -g -I/usr/include/python2.3/ -o junk_wrap.o -c junk_wrap.cxx
323 // g++ junk_wrap.o -shared -g -o _junk.so -L/usr/lib/python2.3/config \
326 // python -c 'from junk import *; Junk("aaa") '
327 // then we get the following unexpected (for novice) python TypeError:
328 // TypeError: No matching function for overloaded 'Junk'
330 // This happens because the swig generated code (at least for python) does
331 // the following two stage process:
332 // 1/ first do a dynamic dispatch ON THE NUMBER OF ARGUMENTS of the overloaded
333 // Junk function (the same happens with method of course). [Note that the
334 // dispatch is NOT done on the type of the arguments].
335 // 2/ second apply the typemap.
336 // When the first dynamic dispatch is executed, the swig generated code
337 // has no knowledge of the typemap, and thus expects a pointer to a std::string
338 // type i.e. an argument to Junk of the form _p_std__int<address>. But this
339 // is not what python handles to Junk ! An invocation of the form 'Junk("aaa")'
340 // will make Python pass a PyString to swig (and this is precisely why we
341 // wrote the typemap). And this will fail....
342 /////////////////////////////////////