3 #include "gdcmCommon.h"
5 #include "gdcmDictEntry.h"
6 #include "gdcmDictSet.h"
7 #include "gdcmDicomDir.h"
8 #include "gdcmDicomDirElement.h"
9 #include "gdcmDicomDirImage.h"
10 #include "gdcmDicomDirMeta.h"
11 #include "gdcmDicomDirObject.h"
12 #include "gdcmDicomDirPatient.h"
13 #include "gdcmDicomDirStudy.h"
14 #include "gdcmDicomDirSerie.h"
15 #include "gdcmDocEntrySet.h"
16 #include "gdcmDocument.h"
17 #include "gdcmElementSet.h"
19 #include "gdcmGlobal.h"
20 #include "gdcmHeader.h"
21 #include "gdcmSerieHeader.h"
22 #include "gdcmRLEFramesInfo.h"
23 #include "gdcmJPEGFragmentsInfo.h"
24 #include "gdcmSQItem.h"
26 #include "gdcmValEntry.h"
28 ////////////////////////////////////////////////////////////////////////////
29 /// Refer (below) to the definition of multi-argument typemap
30 /// %typemap(python, in)
31 /// ( gdcm::DicomDir::Method*, void*, gdcm::DicomDir::Method*)
32 /// for detail on gdcmPythonVoidFunc() and gdcmPythonVoidFuncArgDelete().
33 void gdcmPythonVoidFunc(void *arg)
35 PyObject *arglist, *result;
36 PyObject *func = (PyObject *)arg;
38 arglist = Py_BuildValue("()");
40 result = PyEval_CallObject(func, arglist);
49 if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
51 std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
58 void gdcmPythonVoidFuncArgDelete(void *arg)
60 PyObject *func = (PyObject *)arg;
67 /// This is required in order to avoid %including all the gdcm include files.
72 /////////////////////// typemap section ////////////////////////////////////
74 ////////////////////////////////////////////////
75 // Convert an STL list<> to a python native list
76 %typemap(out) std::list<std::string> *
78 PyObject* NewItem = (PyObject*)0;
79 PyObject* NewList = PyList_New(0); // The result of this typemap
81 for (std::list<std::string>::iterator NewString = ($1)->begin();
82 NewString != ($1)->end();
85 NewItem = PyString_FromString(NewString->c_str());
86 PyList_Append( NewList, NewItem);
91 //////////////////////////////////////////////////////////////////
92 // Convert an STL map<> (hash table) to a python native dictionary
93 %typemap(out) std::map<std::string, std::list<std::string> > *
95 PyObject* NewDict = PyDict_New(); // The result of this typemap
96 PyObject* NewKey = (PyObject*)0;
97 PyObject* NewVal = (PyObject*)0;
99 for (std::map<std::string,
100 std::list<std::string> >::iterator tag = ($1)->begin();
101 tag != ($1)->end(); ++tag)
103 std::string first = tag->first;
104 // Do not publish entries whose keys is made of spaces
105 if (first.length() == 0)
107 NewKey = PyString_FromString(first.c_str());
108 PyObject* NewList = PyList_New(0);
109 for (std::list<std::string>::iterator Item = tag->second.begin();
110 Item != tag->second.end();
113 NewVal = PyString_FromString(Item->c_str());
114 PyList_Append( NewList, NewVal);
116 PyDict_SetItem( NewDict, NewKey, NewList);
121 /////////////////////////////////////////////////////////
122 // Convert a c++ hash table in a python native dictionary
123 %typemap(out) gdcm::TagDocEntryHT &
125 PyObject* NewDict = PyDict_New(); // The result of this typemap
126 std::string RawName; // Element name as gotten from gdcm
127 PyObject* NewKey = (PyObject*)0; // Associated name as python object
128 std::string RawValue; // Element value as gotten from gdcm
129 PyObject* NewVal = (PyObject*)0; // Associated value as python object
131 for (gdcm::TagDocEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag)
133 // The element name shall be the key:
134 RawName = tag->second->GetName();
135 // gdcm unrecognized (including not loaded because their size exceeds
136 // the user specified treshold) elements are exported with their
138 if (RawName == "Unknown")
139 RawName = tag->second->GetKey();
140 NewKey = PyString_FromString(RawName.c_str());
142 // Element values are striped from leading/trailing spaces
143 gdcm::ValEntry* ValEntryPtr = dynamic_cast< gdcm::ValEntry* >(tag->second);
146 RawValue = ValEntryPtr->GetValue();
150 NewVal = PyString_FromString(RawValue.c_str());
151 PyDict_SetItem( NewDict, NewKey, NewVal);
156 /////////////////////////////////////
157 %typemap(out) ListDicomDirPatient &
159 PyObject* NewItem = (PyObject*)0;
160 $result = PyList_New(0); // The result of this typemap
162 for (std::list<gdcm::DicomDirPatient *>::iterator New = ($1)->begin();
163 New != ($1)->end(); ++New)
165 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirPatient,1);
166 PyList_Append($result, NewItem);
170 %typemap(out) ListDicomDirStudy &
172 PyObject* NewItem = (PyObject*)0;
173 $result = PyList_New(0); // The result of this typemap
175 for (std::list<gdcm::DicomDirStudy *>::iterator New = ($1)->begin();
176 New != ($1)->end(); ++New)
178 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirStudy,1);
179 PyList_Append($result, NewItem);
183 %typemap(out) ListDicomDirSerie &
185 PyObject* NewItem = (PyObject*)0;
186 $result = PyList_New(0); // The result of this typemap
188 for (std::list<gdcm::DicomDirSerie *>::iterator New = ($1)->begin();
189 New != ($1)->end(); ++New)
191 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirSerie,1);
192 PyList_Append($result, NewItem);
196 %typemap(out) ListDicomDirImage &
198 PyObject* NewItem = (PyObject*)0;
199 $result = PyList_New(0); // The result of this typemap
201 for (std::list<gdcm::DicomDirImage *>::iterator New = ($1)->begin();
202 New != ($1)->end(); ++New)
204 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirImage,1);
205 PyList_Append($result, NewItem);
209 ////////////////////////////////////////////////////////////////////////////
210 // Multi-argument typemap designed for wrapping the progress related methods
211 // in order to control from an external application the computation of
212 // a DicomDir object (see DicomDir::SetStartMethod*,
213 // DicomDir::SetProgressMethod* and DicomDir::SetEndMethod*).
214 // Motivation: since DicomDir parsing can be quite long, a GUI application
215 // needs to display the avancement and potentially offer a
216 // cancel method to the user (when this one feels things are
217 // longer than expected).
218 // Example of usage: refer to demo/DicomDirProgressMethod.py
219 // Note: Uses gdcmPythonVoidFunc and gdcmPythonVoidFuncArgDelete defined
220 // in the Swig verbatim section of this gdcm.i i.e. in the above section
221 // enclosed within the %{ ... %} scope operator ).
222 %typemap(python, in) ( gdcm::DicomDir::Method *,
224 gdcm::DicomDir::Method * = NULL )
229 $1=gdcmPythonVoidFunc;
231 $3=gdcmPythonVoidFuncArgDelete;
241 //////////////////// STL string versus Python str ////////////////////////
242 // Convertion returning a C++ string.
243 %typemap(out) string, std::string
245 $result = PyString_FromString(($1).c_str());
248 // Convertion of incoming Python str to STL string
249 %typemap(python, in) const std::string, std::string
251 $1 = PyString_AsString($input);
254 // Same convertion as above but references (since swig converts C++
255 // refererences to pointers)
256 %typemap(python, in) std::string const &
258 $1 = new std::string( PyString_AsString( $input ) );
261 ////////////////////////////////////////////////////////////////////////////
262 // Because overloading and %rename don't work together (see below Note 1)
263 // we need to ignore some methods (e.g. the overloaded default constructor).
264 // The gdcm::Header class doesn't have any SetFilename method anyhow, and
265 // this constructor is only used internaly (not from the API) so this is
267 %ignore gdcm::binary_write(std::ostream &,uint32_t const &);
268 %ignore gdcm::binary_write(std::ostream &,uint16_t const &);
270 %ignore gdcm::Header::Header();
271 %ignore gdcm::DicomDir::DicomDir();
273 ////////////////////////////////////////////////////////////////////////////
274 // Warning: Order matters !
275 %include "gdcmCommon.h"
276 %include "gdcmDictEntry.h"
277 %include "gdcmDict.h"
278 %include "gdcmDocEntrySet.h"
279 %include "gdcmElementSet.h"
280 %include "gdcmDictSet.h"
281 %include "gdcmSQItem.h"
282 %include "gdcmDicomDirElement.h"
283 %include "gdcmDicomDirObject.h"
284 %include "gdcmDicomDirImage.h"
285 %include "gdcmDicomDirSerie.h"
286 %include "gdcmDicomDirStudy.h"
287 %include "gdcmDicomDirPatient.h"
288 %include "gdcmDicomDirMeta.h"
289 %include "gdcmDocument.h"
290 %include "gdcmHeader.h"
291 %include "gdcmSerieHeader.h"
292 %include "gdcmFile.h"
293 %include "gdcmUtil.h"
294 %include "gdcmGlobal.h"
295 %include "gdcmDicomDir.h"
297 ////////////////////////////////////////////////////////////////////////////
298 // Notes on swig and this file gdcm.i:
300 /////////////////////////////////////
301 // Note 1: swig collision of method overloading and %typemap
302 // Consider the following junk.i file:
306 // #include <iostream>
307 // void Junk(std::string const & bozo) { std::cout << bozo << std::endl; }
308 // void Junk() { std::cout << "Renamed Junk()" << std::endl; }
311 // %typemap(python, in) std::string const &
313 // $1 = new std::string( PyString_AsString( $input ) );
316 // void Junk(std::string const & bozo);
318 // that we compile on linux with:
319 // swig -c++ -python junk.i
320 // g++ -g -I/usr/include/python2.3/ -o junk_wrap.o -c junk_wrap.cxx
321 // g++ junk_wrap.o -shared -g -o _junk.so -L/usr/lib/python2.3/config \
324 // python -c 'from junk import *; Junk("aaa") '
325 // then we get the following unexpected (for novice) python TypeError:
326 // TypeError: No matching function for overloaded 'Junk'
328 // This happens because the swig generated code (at least for python) does
329 // the following two stage process:
330 // 1/ first do a dynamic dispatch ON THE NUMBER OF ARGUMENTS of the overloaded
331 // Junk function (the same happens with method of course). [Note that the
332 // dispatch is NOT done on the type of the arguments].
333 // 2/ second apply the typemap.
334 // When the first dynamic dispatch is executed, the swig generated code
335 // has no knowledge of the typemap, and thus expects a pointer to a std::string
336 // type i.e. an argument to Junk of the form _p_std__int<address>. But this
337 // is not what python handles to Junk ! An invocation of the form 'Junk("aaa")'
338 // will make Python pass a PyString to swig (and this is precisely why we
339 // wrote the typemap). And this will fail....
340 /////////////////////////////////////