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 "gdcmContentEntry.h"
31 #include "gdcmValEntry.h"
32 #include "gdcmBinEntry.h"
33 #include "gdcmSeqEntry.h"
35 ////////////////////////////////////////////////////////////////////////////
36 /// Refer (below) to the definition of multi-argument typemap
37 /// %typemap(python, in)
38 /// ( gdcm::DicomDir::Method*, void*, gdcm::DicomDir::Method*)
39 /// for detail on gdcmPythonVoidFunc() and gdcmPythonVoidFuncArgDelete().
40 void gdcmPythonVoidFunc(void *arg)
42 PyObject *arglist, *result;
43 PyObject *func = (PyObject *)arg;
45 arglist = Py_BuildValue("()");
47 result = PyEval_CallObject(func, arglist);
56 if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
58 std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
65 void gdcmPythonVoidFuncArgDelete(void *arg)
67 PyObject *func = (PyObject *)arg;
74 /// This is required in order to avoid %including all the gdcm include files.
79 /////////////////////// typemap section ////////////////////////////////////
81 ////////////////////////////////////////////////
82 // Redefine all types used
84 typedef unsigned char uint8_t;
85 typedef short int16_t;
86 typedef unsigned short uint16_t;
88 typedef unsigned int uint32_t;
89 typedef long long int64_t;
90 typedef unsigned long long uint64_t;
92 ////////////////////////////////////////////////
93 // Convert a DocEntry * to the real derived class
94 %typemap(out) gdcm::DocEntry *
100 if(dynamic_cast<SeqEntry *>($1)) // SeqEntry *
101 newEntry = SWIG_NewPointerObj($1,SWIGTYPE_p_gdcm__SeqEntry,0);
102 else if(dynamic_cast<BinEntry *>($1)) // BinEntry *
103 newEntry = SWIG_NewPointerObj($1,SWIGTYPE_p_gdcm__BinEntry,0);
105 newEntry = SWIG_NewPointerObj($1,SWIGTYPE_p_gdcm__ValEntry,0);
109 newEntry = Py_BuildValue("");
114 ////////////////////////////////////////////////
115 // Convert an STL list<> to a python native list
116 //%typemap(out) std::list<std::string> *
118 // PyObject *newItem = (PyObject *)0;
119 // PyObject *newList = PyList_New(0); // The result of this typemap
121 // for (std::list<std::string>::iterator strIt = ($1)->begin();
122 // strIt != ($1)->end();
125 // newItem = PyString_FromString(strIt->c_str());
126 // PyList_Append( newList, newItem);
128 // $result = newList;
131 //////////////////////////////////////////////////////////////////
132 // Convert an STL map<> (hash table) to a python native dictionary
133 //%typemap(out) std::map<std::string, std::list<std::string> > *
135 // PyObject *newDict = PyDict_New(); // The result of this typemap
136 // PyObject *newKey = (PyObject *)0;
137 // PyObject *newVal = (PyObject *)0;
139 // for (std::map<std::string,
140 // std::list<std::string> >::iterator tag = ($1)->begin();
141 // tag != ($1)->end(); ++tag)
143 // std::string first = tag->first;
144 // // Do not publish entries whose keys is made of spaces
145 // if (first.length() == 0)
147 // newKey = PyString_FromString(first.c_str());
149 // PyObject *newList = PyList_New(0);
150 // for (std::list<std::string>::iterator itemIt = tag->second.begin();
151 // itemIt != tag->second.end();
154 // newVal = PyString_FromString(itemIt->c_str());
155 // PyList_Append( newList, newVal);
157 // PyDict_SetItem( newDict, newKey, newList);
159 // $result = newDict;
162 /////////////////////////////////////////////////////////
163 // Convert a c++ hash table in a python native dictionary
164 //%typemap(out) gdcm::TagDocEntryHT &
166 // PyObject *newDict = PyDict_New(); // The result of this typemap
167 // std::string rawName; // Element name as gotten from gdcm
168 // PyObject *newKey = (PyObject *)0; // Associated name as python object
169 // std::string rawValue; // Element value as gotten from gdcm
170 // PyObject *newVal = (PyObject *)0; // Associated value as python object
172 // for (gdcm::TagDocEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag)
174 // // The element name shall be the key:
175 // rawName = tag->second->GetName();
176 // // gdcm unrecognized (including not loaded because their size exceeds
177 // // the user specified treshold) elements are exported with their
179 // if (rawName == "Unknown")
180 // rawName = tag->second->GetKey();
181 // newKey = PyString_FromString(rawName.c_str());
183 // // Element values are striped from leading/trailing spaces
184 // gdcm::ValEntry *valEntryPtr = dynamic_cast< gdcm::ValEntry* >(tag->second);
185 // if ( valEntryPtr )
187 // rawValue = valEntryPtr->GetValue();
191 // newVal = PyString_FromString(rawValue.c_str());
192 // PyDict_SetItem( newDict, newKey, newVal);
194 // $result = newDict;
197 /////////////////////////////////////
198 //%typemap(out) ListDicomDirPatient &
200 // PyObject *newItem = (PyObject *)0;
201 // $result = PyList_New(0); // The result of this typemap
203 // for (std::list<gdcm::DicomDirPatient *>::iterator newIt = ($1)->begin();
204 // newIt != ($1)->end(); ++newIt)
206 // newItem = SWIG_NewPointerObj(*newIt,SWIGTYPE_p_DicomDirPatient,0);
207 // PyList_Append($result, newItem);
211 //%typemap(out) ListDicomDirStudy &
213 // PyObject *newItem = (PyObject *)0;
214 // $result = PyList_New(0); // The result of this typemap
216 // for (std::list<gdcm::DicomDirStudy *>::iterator newIt = ($1)->begin();
217 // newIt != ($1)->end(); ++newIt)
219 // newItem = SWIG_NewPointerObj(*newIt,SWIGTYPE_p_DicomDirStudy,0);
220 // PyList_Append($result, newItem);
224 //%typemap(out) ListDicomDirSerie &
226 // PyObject* newItem = (PyObject*)0;
227 // $result = PyList_New(0); // The result of this typemap
229 // for (std::list<gdcm::DicomDirSerie *>::iterator newIt = ($1)->begin();
230 // newIt != ($1)->end(); ++newIt)
232 // newItem = SWIG_NewPointerObj(*newIt,SWIGTYPE_p_DicomDirSerie,0);
233 // PyList_Append($result, newItem);
237 //%typemap(out) ListDicomDirImage &
239 // PyObject* newItem = (PyObject*)0;
240 // $result = PyList_New(0); // The result of this typemap
242 // for (std::list<gdcm::DicomDirImage *>::iterator newIt = ($1)->begin();
243 // newIt != ($1)->end(); ++newIt)
245 // newItem = SWIG_NewPointerObj(*newIt,SWIGTYPE_p_DicomDirImage,0);
246 // PyList_Append($result, newItem);
250 ////////////////////////////////////////////////////////////////////////////
251 // Multi-argument typemap designed for wrapping the progress related methods
252 // in order to control from an external application the computation of
253 // a DicomDir object (see DicomDir::SetStartMethod*,
254 // DicomDir::SetProgressMethod* and DicomDir::SetEndMethod*).
255 // Motivation: since DicomDir parsing can be quite long, a GUI application
256 // needs to display the avancement and potentially offer a
257 // cancel method to the user (when this one feels things are
258 // longer than expected).
259 // Example of usage: refer to demo/DicomDirProgressMethod.py
260 // Note: Uses gdcmPythonVoidFunc and gdcmPythonVoidFuncArgDelete defined
261 // in the Swig verbatim section of this gdcm.i i.e. in the above section
262 // enclosed within the %{ ... %} scope operator ).
263 %typemap(python, in) ( gdcm::DicomDir::Method *,
265 gdcm::DicomDir::Method * = NULL )
270 $1=gdcmPythonVoidFunc;
272 $3=gdcmPythonVoidFuncArgDelete;
282 //////////////////// STL string versus Python str ////////////////////////
283 // Convertion returning a C++ string.
284 %typemap(out) string, std::string
286 $result = PyString_FromString(($1).c_str());
289 // Convertion of incoming Python str to STL string
290 %typemap(python, in) const std::string, std::string
292 $1 = PyString_AsString($input);
295 // Same convertion as above but references (since swig converts C++
296 // refererences to pointers)
297 %typemap(python, in) std::string const &
299 $1 = new std::string( PyString_AsString( $input ) );
302 //////////////////// gdcm.TagName versus Python str //////////////////////
303 %typemap(out) gdcm::TagName, const gdcm::TagName &
305 $result = PyString_FromString(($1)->c_str());
308 // Convertion of incoming Python str to STL string
309 %typemap(python, in) const gdcm::TagName, gdcm::TagName
311 $1 = PyString_AsString($input);
314 // Same convertion as above but references (since swig converts C++
315 // refererences to pointers)
316 %typemap(python, in) gdcm::TagName const &
318 $1 = new std::string( PyString_AsString( $input ) );
322 ////////////////////////////////////////////////////////////////////////////
323 // Because overloading and %rename don't work together (see below Note 1)
324 // we need to ignore some methods (e.g. the overloaded default constructor).
325 // The gdcm::File class doesn't have any SetFilename method anyhow, and
326 // this constructor is only used internaly (not from the API) so this is
328 %ignore gdcm::binary_write(std::ostream &,uint32_t const &);
329 %ignore gdcm::binary_write(std::ostream &,uint16_t const &);
331 %ignore gdcm::File::File();
332 %ignore gdcm::DicomDir::DicomDir();
334 // Ignore all placed in gdcmCommon.h
335 %ignore GDCM_UNKNOWN;
336 %ignore GDCM_UNFOUND;
337 %ignore GDCM_BINLOADED;
338 %ignore GDCM_NOTLOADED;
341 ////////////////////////////////////////////////////////////////////////////
342 // Warning: Order matters !
343 %include "gdcmCommon.h"
344 %include "gdcmBase.h"
345 %include "gdcmDictEntry.h"
346 %include "gdcmDict.h"
347 %include "gdcmDictSet.h"
348 %include "gdcmDocEntrySet.h"
349 %include "gdcmElementSet.h"
350 %include "gdcmSQItem.h"
351 %include "gdcmDicomDirElement.h"
352 %include "gdcmDicomDirObject.h"
353 %include "gdcmDicomDirImage.h"
354 %include "gdcmDicomDirSerie.h"
355 %include "gdcmDicomDirStudy.h"
356 %include "gdcmDicomDirPatient.h"
357 %include "gdcmDicomDirMeta.h"
358 %include "gdcmDocument.h"
359 %include "gdcmFile.h"
360 %include "gdcmSerieHeader.h"
361 %include "gdcmFile.h"
362 %include "gdcmUtil.h"
363 %include "gdcmGlobal.h"
364 %include "gdcmDicomDir.h"
365 %include "gdcmDocEntry.h"
366 %include "gdcmContentEntry.h"
367 %include "gdcmValEntry.h"
368 %include "gdcmBinEntry.h"
369 %include "gdcmSeqEntry.h"
371 ////////////////////////////////////////////////////////////////////////////
372 // Notes on swig and this file gdcm.i:
374 /////////////////////////////////////
375 // Note 1: swig collision of method overloading and %typemap
376 // Consider the following junk.i file:
380 // #include <iostream>
381 // void Junk(std::string const & bozo) { std::cout << bozo << std::endl; }
382 // void Junk() { std::cout << "Renamed Junk()" << std::endl; }
385 // %typemap(python, in) std::string const &
387 // $1 = new std::string( PyString_AsString( $input ) );
390 // void Junk(std::string const & bozo);
392 // that we compile on linux with:
393 // swig -c++ -python junk.i
394 // g++ -g -I/usr/include/python2.3/ -o junk_wrap.o -c junk_wrap.cxx
395 // g++ junk_wrap.o -shared -g -o _junk.so -L/usr/lib/python2.3/config \
398 // python -c 'from junk import *; Junk("aaa") '
399 // then we get the following unexpected (for novice) python TypeError:
400 // TypeError: No matching function for overloaded 'Junk'
402 // This happens because the swig generated code (at least for python) does
403 // the following two stage process:
404 // 1/ first do a dynamic dispatch ON THE NUMBER OF ARGUMENTS of the overloaded
405 // Junk function (the same happens with method of course). [Note that the
406 // dispatch is NOT done on the type of the arguments].
407 // 2/ second apply the typemap.
408 // When the first dynamic dispatch is executed, the swig generated code
409 // has no knowledge of the typemap, and thus expects a pointer to a std::string
410 // type i.e. an argument to Junk of the form _p_std__int<address>. But this
411 // is not what python handles to Junk ! An invocation of the form 'Junk("aaa")'
412 // will make Python pass a PyString to swig (and this is precisely why we
413 // wrote the typemap). And this will fail....
414 /////////////////////////////////////