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 "gdcmSerieHeader.h"
25 #include "gdcmRLEFramesInfo.h"
26 #include "gdcmJPEGFragmentsInfo.h"
27 #include "gdcmSQItem.h"
29 #include "gdcmDocEntry.h"
30 #include "gdcmValEntry.h"
31 #include "gdcmBinEntry.h"
32 #include "gdcmSeqEntry.h"
34 ////////////////////////////////////////////////////////////////////////////
35 /// Refer (below) to the definition of multi-argument typemap
36 /// %typemap(python, in)
37 /// ( gdcm::DicomDir::Method*, void*, gdcm::DicomDir::Method*)
38 /// for detail on gdcmPythonVoidFunc() and gdcmPythonVoidFuncArgDelete().
39 void gdcmPythonVoidFunc(void *arg)
41 PyObject *arglist, *result;
42 PyObject *func = (PyObject *)arg;
44 arglist = Py_BuildValue("()");
46 result = PyEval_CallObject(func, arglist);
55 if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
57 std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
64 void gdcmPythonVoidFuncArgDelete(void *arg)
66 PyObject *func = (PyObject *)arg;
73 /// This is required in order to avoid %including all the gdcm include files.
78 /////////////////////// typemap section ////////////////////////////////////
80 ////////////////////////////////////////////////
81 // Redefine all types used
83 typedef unsigned char uint8_t;
84 typedef short int16_t;
85 typedef unsigned short uint16_t;
87 typedef unsigned int uint32_t;
88 typedef long long int64_t;
89 typedef unsigned long long uint64_t;
91 ////////////////////////////////////////////////
92 // Convert a DocEntry * to the real derived class
93 %typemap(out) gdcm::DocEntry *
99 if(dynamic_cast<SeqEntry *>($1)) // SeqEntry *
100 newEntry = SWIG_NewPointerObj($1,SWIGTYPE_p_gdcm__SeqEntry,0);
101 else if(dynamic_cast<BinEntry *>($1)) // BinEntry *
102 newEntry = SWIG_NewPointerObj($1,SWIGTYPE_p_gdcm__BinEntry,0);
104 newEntry = SWIG_NewPointerObj($1,SWIGTYPE_p_gdcm__ValEntry,0);
108 newEntry = Py_BuildValue("");
113 ////////////////////////////////////////////////
114 // Convert an STL list<> to a python native list
115 //%typemap(out) std::list<std::string> *
117 // PyObject *newItem = (PyObject *)0;
118 // PyObject *newList = PyList_New(0); // The result of this typemap
120 // for (std::list<std::string>::iterator strIt = ($1)->begin();
121 // strIt != ($1)->end();
124 // newItem = PyString_FromString(strIt->c_str());
125 // PyList_Append( newList, newItem);
127 // $result = newList;
130 //////////////////////////////////////////////////////////////////
131 // Convert an STL map<> (hash table) to a python native dictionary
132 //%typemap(out) std::map<std::string, std::list<std::string> > *
134 // PyObject *newDict = PyDict_New(); // The result of this typemap
135 // PyObject *newKey = (PyObject *)0;
136 // PyObject *newVal = (PyObject *)0;
138 // for (std::map<std::string,
139 // std::list<std::string> >::iterator tag = ($1)->begin();
140 // tag != ($1)->end(); ++tag)
142 // std::string first = tag->first;
143 // // Do not publish entries whose keys is made of spaces
144 // if (first.length() == 0)
146 // newKey = PyString_FromString(first.c_str());
148 // PyObject *newList = PyList_New(0);
149 // for (std::list<std::string>::iterator itemIt = tag->second.begin();
150 // itemIt != tag->second.end();
153 // newVal = PyString_FromString(itemIt->c_str());
154 // PyList_Append( newList, newVal);
156 // PyDict_SetItem( newDict, newKey, newList);
158 // $result = newDict;
161 /////////////////////////////////////////////////////////
162 // Convert a c++ hash table in a python native dictionary
163 //%typemap(out) gdcm::TagDocEntryHT &
165 // PyObject *newDict = PyDict_New(); // The result of this typemap
166 // std::string rawName; // Element name as gotten from gdcm
167 // PyObject *newKey = (PyObject *)0; // Associated name as python object
168 // std::string rawValue; // Element value as gotten from gdcm
169 // PyObject *newVal = (PyObject *)0; // Associated value as python object
171 // for (gdcm::TagDocEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag)
173 // // The element name shall be the key:
174 // rawName = tag->second->GetName();
175 // // gdcm unrecognized (including not loaded because their size exceeds
176 // // the user specified treshold) elements are exported with their
178 // if (rawName == "Unknown")
179 // rawName = tag->second->GetKey();
180 // newKey = PyString_FromString(rawName.c_str());
182 // // Element values are striped from leading/trailing spaces
183 // gdcm::ValEntry *valEntryPtr = dynamic_cast< gdcm::ValEntry* >(tag->second);
184 // if ( valEntryPtr )
186 // rawValue = valEntryPtr->GetValue();
190 // newVal = PyString_FromString(rawValue.c_str());
191 // PyDict_SetItem( newDict, newKey, newVal);
193 // $result = newDict;
196 /////////////////////////////////////
197 //%typemap(out) ListDicomDirPatient &
199 // PyObject *newItem = (PyObject *)0;
200 // $result = PyList_New(0); // The result of this typemap
202 // for (std::list<gdcm::DicomDirPatient *>::iterator newIt = ($1)->begin();
203 // newIt != ($1)->end(); ++newIt)
205 // newItem = SWIG_NewPointerObj(*newIt,SWIGTYPE_p_DicomDirPatient,0);
206 // PyList_Append($result, newItem);
210 //%typemap(out) ListDicomDirStudy &
212 // PyObject *newItem = (PyObject *)0;
213 // $result = PyList_New(0); // The result of this typemap
215 // for (std::list<gdcm::DicomDirStudy *>::iterator newIt = ($1)->begin();
216 // newIt != ($1)->end(); ++newIt)
218 // newItem = SWIG_NewPointerObj(*newIt,SWIGTYPE_p_DicomDirStudy,0);
219 // PyList_Append($result, newItem);
223 //%typemap(out) ListDicomDirSerie &
225 // PyObject* newItem = (PyObject*)0;
226 // $result = PyList_New(0); // The result of this typemap
228 // for (std::list<gdcm::DicomDirSerie *>::iterator newIt = ($1)->begin();
229 // newIt != ($1)->end(); ++newIt)
231 // newItem = SWIG_NewPointerObj(*newIt,SWIGTYPE_p_DicomDirSerie,0);
232 // PyList_Append($result, newItem);
236 //%typemap(out) ListDicomDirImage &
238 // PyObject* newItem = (PyObject*)0;
239 // $result = PyList_New(0); // The result of this typemap
241 // for (std::list<gdcm::DicomDirImage *>::iterator newIt = ($1)->begin();
242 // newIt != ($1)->end(); ++newIt)
244 // newItem = SWIG_NewPointerObj(*newIt,SWIGTYPE_p_DicomDirImage,0);
245 // PyList_Append($result, newItem);
249 ////////////////////////////////////////////////////////////////////////////
250 // Multi-argument typemap designed for wrapping the progress related methods
251 // in order to control from an external application the computation of
252 // a DicomDir object (see DicomDir::SetStartMethod*,
253 // DicomDir::SetProgressMethod* and DicomDir::SetEndMethod*).
254 // Motivation: since DicomDir parsing can be quite long, a GUI application
255 // needs to display the avancement and potentially offer a
256 // cancel method to the user (when this one feels things are
257 // longer than expected).
258 // Example of usage: refer to demo/DicomDirProgressMethod.py
259 // Note: Uses gdcmPythonVoidFunc and gdcmPythonVoidFuncArgDelete defined
260 // in the Swig verbatim section of this gdcm.i i.e. in the above section
261 // enclosed within the %{ ... %} scope operator ).
262 %typemap(python, in) ( gdcm::DicomDir::Method *,
264 gdcm::DicomDir::Method * = NULL )
269 $1=gdcmPythonVoidFunc;
271 $3=gdcmPythonVoidFuncArgDelete;
281 //////////////////// STL string versus Python str ////////////////////////
282 // Convertion returning a C++ string.
283 %typemap(out) string, std::string
285 $result = PyString_FromString(($1).c_str());
288 // Convertion of incoming Python str to STL string
289 %typemap(python, in) const std::string, std::string
291 $1 = PyString_AsString($input);
294 // Same convertion as above but references (since swig converts C++
295 // refererences to pointers)
296 %typemap(python, in) std::string const &
298 $1 = new std::string( PyString_AsString( $input ) );
301 //////////////////// gdcm.TagName versus Python str //////////////////////
302 %typemap(out) gdcm::TagName, const gdcm::TagName &
304 $result = PyString_FromString(($1)->c_str());
307 // Convertion of incoming Python str to STL string
308 %typemap(python, in) const gdcm::TagName, gdcm::TagName
310 $1 = PyString_AsString($input);
313 // Same convertion as above but references (since swig converts C++
314 // refererences to pointers)
315 %typemap(python, in) gdcm::TagName const &
317 $1 = new std::string( PyString_AsString( $input ) );
321 ////////////////////////////////////////////////////////////////////////////
322 // Because overloading and %rename don't work together (see below Note 1)
323 // we need to ignore some methods (e.g. the overloaded default constructor).
324 // The gdcm::File class doesn't have any SetFilename method anyhow, and
325 // this constructor is only used internaly (not from the API) so this is
327 %ignore gdcm::binary_write(std::ostream &,uint32_t const &);
328 %ignore gdcm::binary_write(std::ostream &,uint16_t const &);
330 %ignore gdcm::File::File();
331 %ignore gdcm::DicomDir::DicomDir();
333 // Ignore all placed in gdcmCommon.h
334 %ignore GDCM_UNKNOWN;
335 %ignore GDCM_UNFOUND;
336 %ignore GDCM_BINLOADED;
337 %ignore GDCM_NOTLOADED;
340 ////////////////////////////////////////////////////////////////////////////
341 // Warning: Order matters !
342 %include "gdcmCommon.h"
343 %include "gdcmBase.h"
344 %include "gdcmDictEntry.h"
345 %include "gdcmDict.h"
346 %include "gdcmDictSet.h"
347 %include "gdcmDocEntrySet.h"
348 %include "gdcmElementSet.h"
349 %include "gdcmSQItem.h"
350 %include "gdcmDicomDirElement.h"
351 %include "gdcmDicomDirObject.h"
352 %include "gdcmDicomDirImage.h"
353 %include "gdcmDicomDirSerie.h"
354 %include "gdcmDicomDirStudy.h"
355 %include "gdcmDicomDirPatient.h"
356 %include "gdcmDicomDirMeta.h"
357 %include "gdcmDocument.h"
358 %include "gdcmFile.h"
359 %include "gdcmSerieHeader.h"
360 %include "gdcmFile.h"
361 %include "gdcmUtil.h"
362 %include "gdcmGlobal.h"
363 %include "gdcmDicomDir.h"
364 %include "gdcmDocEntry.h"
365 %include "gdcmValEntry.h"
366 %include "gdcmBinEntry.h"
367 %include "gdcmSeqEntry.h"
369 ////////////////////////////////////////////////////////////////////////////
370 // Notes on swig and this file gdcm.i:
372 /////////////////////////////////////
373 // Note 1: swig collision of method overloading and %typemap
374 // Consider the following junk.i file:
378 // #include <iostream>
379 // void Junk(std::string const & bozo) { std::cout << bozo << std::endl; }
380 // void Junk() { std::cout << "Renamed Junk()" << std::endl; }
383 // %typemap(python, in) std::string const &
385 // $1 = new std::string( PyString_AsString( $input ) );
388 // void Junk(std::string const & bozo);
390 // that we compile on linux with:
391 // swig -c++ -python junk.i
392 // g++ -g -I/usr/include/python2.3/ -o junk_wrap.o -c junk_wrap.cxx
393 // g++ junk_wrap.o -shared -g -o _junk.so -L/usr/lib/python2.3/config \
396 // python -c 'from junk import *; Junk("aaa") '
397 // then we get the following unexpected (for novice) python TypeError:
398 // TypeError: No matching function for overloaded 'Junk'
400 // This happens because the swig generated code (at least for python) does
401 // the following two stage process:
402 // 1/ first do a dynamic dispatch ON THE NUMBER OF ARGUMENTS of the overloaded
403 // Junk function (the same happens with method of course). [Note that the
404 // dispatch is NOT done on the type of the arguments].
405 // 2/ second apply the typemap.
406 // When the first dynamic dispatch is executed, the swig generated code
407 // has no knowledge of the typemap, and thus expects a pointer to a std::string
408 // type i.e. an argument to Junk of the form _p_std__int<address>. But this
409 // is not what python handles to Junk ! An invocation of the form 'Junk("aaa")'
410 // will make Python pass a PyString to swig (and this is precisely why we
411 // wrote the typemap). And this will fail....
412 /////////////////////////////////////