5 #include "gdcmCommon.h"
8 #include "gdcmDictEntry.h"
9 #include "gdcmDictSet.h"
10 #include "gdcmDicomDir.h"
11 #include "gdcmDicomDirElement.h"
12 #include "gdcmDicomDirImage.h"
13 #include "gdcmDicomDirMeta.h"
14 #include "gdcmDicomDirObject.h"
15 #include "gdcmDicomDirPatient.h"
16 #include "gdcmDicomDirStudy.h"
17 #include "gdcmDicomDirSerie.h"
18 #include "gdcmDocEntrySet.h"
19 #include "gdcmDocument.h"
20 #include "gdcmElementSet.h"
21 #include "gdcmFileHelper.h"
22 #include "gdcmGlobal.h"
24 #include "gdcmSerieHelper.h"
25 #include "gdcmRLEFramesInfo.h"
26 #include "gdcmJPEGFragmentsInfo.h"
27 #include "gdcmSQItem.h"
29 #include "gdcmDocEntry.h"
30 #include "gdcmContentEntry.h"
31 #include "gdcmValEntry.h"
32 #include "gdcmBinEntry.h"
33 #include "gdcmSeqEntry.h"
36 #include "gdcmDictGroupName.h"
38 ////////////////////////////////////////////////////////////////////////////
39 /// Refer (below) to the definition of multi-argument typemap
40 /// %typemap(python, in)
41 /// ( gdcm::DicomDir::Method*, void*, gdcm::DicomDir::Method*)
42 /// for detail on gdcmPythonVoidFunc() and gdcmPythonVoidFuncArgDelete().
43 void gdcmPythonVoidFunc(void *arg)
45 PyObject *arglist, *result;
46 PyObject *func = (PyObject *)arg;
48 arglist = Py_BuildValue("()");
50 result = PyEval_CallObject(func, arglist);
59 if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
61 std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
68 void gdcmPythonVoidFuncArgDelete(void *arg)
70 PyObject *func = (PyObject *)arg;
77 /// This is required in order to avoid %including all the gdcm include files.
82 /////////////////////// typemap section ////////////////////////////////////
84 ////////////////////////////////////////////////
85 // Redefine all types used
87 typedef unsigned char uint8_t;
88 typedef short int16_t;
89 typedef unsigned short uint16_t;
91 typedef unsigned int uint32_t;
92 typedef long long int64_t;
93 typedef unsigned long long uint64_t;
95 ////////////////////////////////////////////////
96 // Convert a DocEntry * to the real derived class
97 %typemap(out) gdcm::DocEntry *
103 if(dynamic_cast<SeqEntry *>($1)) // SeqEntry *
104 newEntry = SWIG_NewPointerObj($1,SWIGTYPE_p_gdcm__SeqEntry,0);
105 else if(dynamic_cast<BinEntry *>($1)) // BinEntry *
106 newEntry = SWIG_NewPointerObj($1,SWIGTYPE_p_gdcm__BinEntry,0);
108 newEntry = SWIG_NewPointerObj($1,SWIGTYPE_p_gdcm__ValEntry,0);
112 newEntry = Py_BuildValue("");
117 ////////////////////////////////////////////////
118 // Convert an STL list<> to a python native list
119 //%typemap(out) std::list<std::string> *
121 // PyObject *newItem = (PyObject *)0;
122 // PyObject *newList = PyList_New(0); // The result of this typemap
124 // for (std::list<std::string>::iterator strIt = ($1)->begin();
125 // strIt != ($1)->end();
128 // newItem = PyString_FromString(strIt->c_str());
129 // PyList_Append( newList, newItem);
131 // $result = newList;
134 //////////////////////////////////////////////////////////////////
135 // Convert an STL map<> (hash table) to a python native dictionary
136 //%typemap(out) std::map<std::string, std::list<std::string> > *
138 // PyObject *newDict = PyDict_New(); // The result of this typemap
139 // PyObject *newKey = (PyObject *)0;
140 // PyObject *newVal = (PyObject *)0;
142 // for (std::map<std::string,
143 // std::list<std::string> >::iterator tag = ($1)->begin();
144 // tag != ($1)->end(); ++tag)
146 // std::string first = tag->first;
147 // // Do not publish entries whose keys is made of spaces
148 // if (first.length() == 0)
150 // newKey = PyString_FromString(first.c_str());
152 // PyObject *newList = PyList_New(0);
153 // for (std::list<std::string>::iterator itemIt = tag->second.begin();
154 // itemIt != tag->second.end();
157 // newVal = PyString_FromString(itemIt->c_str());
158 // PyList_Append( newList, newVal);
160 // PyDict_SetItem( newDict, newKey, newList);
162 // $result = newDict;
165 /////////////////////////////////////////////////////////
166 // Convert a c++ hash table in a python native dictionary
167 //%typemap(out) gdcm::TagDocEntryHT &
169 // PyObject *newDict = PyDict_New(); // The result of this typemap
170 // std::string rawName; // Element name as gotten from gdcm
171 // PyObject *newKey = (PyObject *)0; // Associated name as python object
172 // std::string rawValue; // Element value as gotten from gdcm
173 // PyObject *newVal = (PyObject *)0; // Associated value as python object
175 // for (gdcm::TagDocEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag)
177 // // The element name shall be the key:
178 // rawName = tag->second->GetName();
179 // // gdcm unrecognized (including not loaded because their size exceeds
180 // // the user specified treshold) elements are exported with their
182 // if (rawName == "Unknown")
183 // rawName = tag->second->GetKey();
184 // newKey = PyString_FromString(rawName.c_str());
186 // // Element values are striped from leading/trailing spaces
187 // gdcm::ValEntry *valEntryPtr = dynamic_cast< gdcm::ValEntry* >(tag->second);
188 // if ( valEntryPtr )
190 // rawValue = valEntryPtr->GetValue();
194 // newVal = PyString_FromString(rawValue.c_str());
195 // PyDict_SetItem( newDict, newKey, newVal);
197 // $result = newDict;
200 /////////////////////////////////////
201 //%typemap(out) ListDicomDirPatient &
203 // PyObject *newItem = (PyObject *)0;
204 // $result = PyList_New(0); // The result of this typemap
206 // for (std::list<gdcm::DicomDirPatient *>::iterator newIt = ($1)->begin();
207 // newIt != ($1)->end(); ++newIt)
209 // newItem = SWIG_NewPointerObj(*newIt,SWIGTYPE_p_DicomDirPatient,0);
210 // PyList_Append($result, newItem);
214 //%typemap(out) ListDicomDirStudy &
216 // PyObject *newItem = (PyObject *)0;
217 // $result = PyList_New(0); // The result of this typemap
219 // for (std::list<gdcm::DicomDirStudy *>::iterator newIt = ($1)->begin();
220 // newIt != ($1)->end(); ++newIt)
222 // newItem = SWIG_NewPointerObj(*newIt,SWIGTYPE_p_DicomDirStudy,0);
223 // PyList_Append($result, newItem);
227 //%typemap(out) ListDicomDirSerie &
229 // PyObject* newItem = (PyObject*)0;
230 // $result = PyList_New(0); // The result of this typemap
232 // for (std::list<gdcm::DicomDirSerie *>::iterator newIt = ($1)->begin();
233 // newIt != ($1)->end(); ++newIt)
235 // newItem = SWIG_NewPointerObj(*newIt,SWIGTYPE_p_DicomDirSerie,0);
236 // PyList_Append($result, newItem);
240 //%typemap(out) ListDicomDirImage &
242 // PyObject* newItem = (PyObject*)0;
243 // $result = PyList_New(0); // The result of this typemap
245 // for (std::list<gdcm::DicomDirImage *>::iterator newIt = ($1)->begin();
246 // newIt != ($1)->end(); ++newIt)
248 // newItem = SWIG_NewPointerObj(*newIt,SWIGTYPE_p_DicomDirImage,0);
249 // PyList_Append($result, newItem);
253 ////////////////////////////////////////////////////////////////////////////
254 // Multi-argument typemap designed for wrapping the progress related methods
255 // in order to control from an external application the computation of
256 // a DicomDir object (see DicomDir::SetStartMethod*,
257 // DicomDir::SetProgressMethod* and DicomDir::SetEndMethod*).
258 // Motivation: since DicomDir parsing can be quite long, a GUI application
259 // needs to display the avancement and potentially offer a
260 // cancel method to the user (when this one feels things are
261 // longer than expected).
262 // Example of usage: refer to demo/DicomDirProgressMethod.py
263 // Note: Uses gdcmPythonVoidFunc and gdcmPythonVoidFuncArgDelete defined
264 // in the Swig verbatim section of this gdcm.i i.e. in the above section
265 // enclosed within the %{ ... %} scope operator ).
266 %typemap(python, in) ( gdcm::DicomDir::Method *,
268 gdcm::DicomDir::Method * = NULL )
273 $1=gdcmPythonVoidFunc;
275 $3=gdcmPythonVoidFuncArgDelete;
285 //////////////////// STL string versus Python str ////////////////////////
286 // Convertion returning a C++ string.
287 %typemap(out) std::string
289 $result = PyString_FromString(($1).c_str());
294 $result = PyString_FromString(($1).c_str());
297 %typemap(out) std::string const &
299 $result = PyString_FromString(($1)->c_str());
302 // Convertion of incoming Python str to STL string
303 %typemap(python, in) const std::string, std::string
305 $1 = PyString_AsString($input);
308 // Same convertion as above but references (since swig converts C++
309 // refererences to pointers)
310 %typemap(python, in) std::string const &
312 $1 = new std::string( PyString_AsString( $input ) );
315 //////////////////// gdcm.TagName versus Python str //////////////////////
316 %typemap(out) gdcm::TagName, const gdcm::TagName &
318 $result = PyString_FromString(($1)->c_str());
321 // Convertion of incoming Python str to STL string
322 %typemap(python, in) const gdcm::TagName, gdcm::TagName
324 $1 = PyString_AsString($input);
327 // Same convertion as above but references (since swig converts C++
328 // refererences to pointers)
329 %typemap(python, in) gdcm::TagName const &
331 $1 = new std::string( PyString_AsString( $input ) );
335 ////////////////////////////////////////////////////////////////////////////
336 // Because overloading and %rename don't work together (see below Note 1)
337 // we need to ignore some methods (e.g. the overloaded default constructor).
338 // The gdcm::File class doesn't have any SetFilename method anyhow, and
339 // this constructor is only used internaly (not from the API) so this is
341 %ignore gdcm::binary_write(std::ostream &,uint32_t const &);
342 %ignore gdcm::binary_write(std::ostream &,uint16_t const &);
344 %ignore gdcm::File::File();
345 %ignore gdcm::DicomDir::DicomDir();
347 // Ignore all placed in gdcmCommon.h
348 %ignore GDCM_UNKNOWN;
349 %ignore GDCM_UNFOUND;
350 %ignore GDCM_BINLOADED;
351 %ignore GDCM_NOTLOADED;
354 ////////////////////////////////////////////////////////////////////////////
355 // Warning: Order matters !
356 %include "gdcmCommon.h"
357 %include "gdcmBase.h"
358 %include "gdcmDictEntry.h"
359 %include "gdcmDict.h"
360 %include "gdcmDictSet.h"
361 %include "gdcmDocEntrySet.h"
362 %include "gdcmElementSet.h"
363 %include "gdcmSQItem.h"
364 %include "gdcmDicomDirElement.h"
365 %include "gdcmDicomDirObject.h"
366 %include "gdcmDicomDirImage.h"
367 %include "gdcmDicomDirSerie.h"
368 %include "gdcmDicomDirStudy.h"
369 %include "gdcmDicomDirPatient.h"
370 %include "gdcmDicomDirMeta.h"
371 %include "gdcmDocument.h"
372 %include "gdcmFile.h"
373 %include "gdcmSerieHelper.h"
374 %include "gdcmFile.h"
375 %include "gdcmUtil.h"
376 %include "gdcmGlobal.h"
377 %include "gdcmDicomDir.h"
378 %include "gdcmDocEntry.h"
379 %include "gdcmContentEntry.h"
380 %include "gdcmValEntry.h"
381 %include "gdcmBinEntry.h"
382 %include "gdcmSeqEntry.h"
385 %include "gdcmDictGroupName.h"
387 ////////////////////////////////////////////////////////////////////////////
388 // Notes on swig and this file gdcm.i:
390 /////////////////////////////////////
391 // Note 1: swig collision of method overloading and %typemap
392 // Consider the following junk.i file:
396 // #include <iostream>
397 // void Junk(std::string const & bozo) { std::cout << bozo << std::endl; }
398 // void Junk() { std::cout << "Renamed Junk()" << std::endl; }
401 // %typemap(python, in) std::string const &
403 // $1 = new std::string( PyString_AsString( $input ) );
406 // void Junk(std::string const & bozo);
408 // that we compile on linux with:
409 // swig -c++ -python junk.i
410 // g++ -g -I/usr/include/python2.3/ -o junk_wrap.o -c junk_wrap.cxx
411 // g++ junk_wrap.o -shared -g -o _junk.so -L/usr/lib/python2.3/config \
414 // python -c 'from junk import *; Junk("aaa") '
415 // then we get the following unexpected (for novice) python TypeError:
416 // TypeError: No matching function for overloaded 'Junk'
418 // This happens because the swig generated code (at least for python) does
419 // the following two stage process:
420 // 1/ first do a dynamic dispatch ON THE NUMBER OF ARGUMENTS of the overloaded
421 // Junk function (the same happens with method of course). [Note that the
422 // dispatch is NOT done on the type of the arguments].
423 // 2/ second apply the typemap.
424 // When the first dynamic dispatch is executed, the swig generated code
425 // has no knowledge of the typemap, and thus expects a pointer to a std::string
426 // type i.e. an argument to Junk of the form _p_std__int<address>. But this
427 // is not what python handles to Junk ! An invocation of the form 'Junk("aaa")'
428 // will make Python pass a PyString to swig (and this is precisely why we
429 // wrote the typemap). And this will fail....
430 /////////////////////////////////////