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 "gdcmHeaderHelper.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> * {
77 PyObject* NewItem = (PyObject*)0;
78 PyObject* NewList = PyList_New(0); // The result of this typemap
80 for (std::list<std::string>::iterator NewString = ($1)->begin();
81 NewString != ($1)->end();
84 NewItem = PyString_FromString(NewString->c_str());
85 PyList_Append( NewList, NewItem);
90 //////////////////////////////////////////////////////////////////
91 // Convert an STL map<> (hash table) to a python native dictionary
92 %typemap(out) std::map<std::string, std::list<std::string> > * {
93 PyObject* NewDict = PyDict_New(); // The result of this typemap
94 PyObject* NewKey = (PyObject*)0;
95 PyObject* NewVal = (PyObject*)0;
97 for (std::map<std::string,
98 std::list<std::string> >::iterator tag = ($1)->begin();
99 tag != ($1)->end(); ++tag)
101 std::string first = tag->first;
102 // Do not publish entries whose keys is made of spaces
103 if (first.length() == 0)
105 NewKey = PyString_FromString(first.c_str());
106 PyObject* NewList = PyList_New(0);
107 for (std::list<std::string>::iterator Item = tag->second.begin();
108 Item != tag->second.end();
111 NewVal = PyString_FromString(Item->c_str());
112 PyList_Append( NewList, NewVal);
114 PyDict_SetItem( NewDict, NewKey, NewList);
119 /////////////////////////////////////////////////////////
120 // Convert a c++ hash table in a python native dictionary
121 %typemap(out) gdcm::TagDocEntryHT & {
122 PyObject* NewDict = PyDict_New(); // The result of this typemap
123 std::string RawName; // Element name as gotten from gdcm
124 PyObject* NewKey = (PyObject*)0; // Associated name as python object
125 std::string RawValue; // Element value as gotten from gdcm
126 PyObject* NewVal = (PyObject*)0; // Associated value as python object
128 for (gdcm::TagDocEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag)
130 // The element name shall be the key:
131 RawName = tag->second->GetName();
132 // gdcm unrecognized (including not loaded because their size exceeds
133 // the user specified treshold) elements are exported with their
135 if (RawName == "Unknown")
136 RawName = tag->second->GetKey();
137 NewKey = PyString_FromString(RawName.c_str());
139 // Element values are striped from leading/trailing spaces
140 if (gdcm::ValEntry* ValEntryPtr =
141 dynamic_cast< gdcm::ValEntry* >(tag->second) )
143 RawValue = ValEntryPtr->GetValue();
147 NewVal = PyString_FromString(RawValue.c_str());
148 PyDict_SetItem( NewDict, NewKey, NewVal);
153 /////////////////////////////////////
154 %typemap(out) ListDicomDirPatient & {
155 PyObject* NewItem = (PyObject*)0;
156 $result = PyList_New(0); // The result of this typemap
158 for (std::list<gdcm::DicomDirPatient *>::iterator New = ($1)->begin();
159 New != ($1)->end(); ++New) {
160 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirPatient,1);
161 PyList_Append($result, NewItem);
165 %typemap(out) ListDicomDirStudy & {
166 PyObject* NewItem = (PyObject*)0;
167 $result = PyList_New(0); // The result of this typemap
169 for (std::list<gdcm::DicomDirStudy *>::iterator New = ($1)->begin();
170 New != ($1)->end(); ++New) {
171 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirStudy,1);
172 PyList_Append($result, NewItem);
176 %typemap(out) ListDicomDirSerie & {
177 PyObject* NewItem = (PyObject*)0;
178 $result = PyList_New(0); // The result of this typemap
180 for (std::list<gdcm::DicomDirSerie *>::iterator New = ($1)->begin();
181 New != ($1)->end(); ++New) {
182 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirSerie,1);
183 PyList_Append($result, NewItem);
187 %typemap(out) ListDicomDirImage & {
188 PyObject* NewItem = (PyObject*)0;
189 $result = PyList_New(0); // The result of this typemap
191 for (std::list<gdcm::DicomDirImage *>::iterator New = ($1)->begin();
192 New != ($1)->end(); ++New) {
193 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirImage,1);
194 PyList_Append($result, NewItem);
198 ////////////////////////////////////////////////////////////////////////////
199 // Multi-argument typemap designed for wrapping the progress related methods
200 // in order to control from an external application the computation of
201 // a DicomDir object (see DicomDir::SetStartMethod*,
202 // DicomDir::SetProgressMethod* and DicomDir::SetEndMethod*).
203 // Motivation: since DicomDir parsing can be quite long, a GUI application
204 // needs to display the avancement and potentially offer a
205 // cancel method to the user (when this one feels things are
206 // longer than expected).
207 // Example of usage: refer to demo/DicomDirProgressMethod.py
208 // Note: Uses gdcmPythonVoidFunc and gdcmPythonVoidFuncArgDelete defined
209 // in the Swig verbatim section of this gdcm.i i.e. in the above section
210 // enclosed within the %{ ... %} scope operator ).
211 %typemap(python, in) ( gdcm::DicomDir::Method *,
213 gdcm::DicomDir::Method * = NULL )
218 $1=gdcmPythonVoidFunc;
220 $3=gdcmPythonVoidFuncArgDelete;
230 //////////////////// STL string versus Python str ////////////////////////
231 // Convertion returning a C++ string.
232 %typemap(out) string, std::string {
233 $result = PyString_FromString(($1).c_str());
236 // Convertion of incoming Python str to STL string
237 %typemap(python, in) const std::string, std::string
239 $1 = PyString_AsString($input);
242 // Same convertion as above but references (since swig converts C++
243 // refererences to pointers)
244 %typemap(python, in) std::string const &
246 $1 = new std::string( PyString_AsString( $input ) );
249 ////////////////////////////////////////////////////////////////////////////
250 // Because overloading and %rename don't work together (see below Note 1)
251 // we need to ignore some methods (e.g. the overloaded default constructor).
252 // The gdcm::Header class doesn't have any SetFilename method anyhow, and
253 // this constructor is only used internaly (not from the API) so this is
255 %ignore gdcm::Header::Header();
256 %ignore gdcm::DicomDir::DicomDir();
258 ////////////////////////////////////////////////////////////////////////////
259 // Warning: Order matters !
260 %include "gdcmCommon.h"
261 //CLEANME %include "gdcmRLEFramesInfo.h"
262 //CLEANME %include "gdcmJPEGFragmentsInfo.h"
263 //CLEANME %include "gdcmDictEntry.h"
264 //CLEANME %include "gdcmDict.h"
265 //CLEANME %include "gdcmDocEntry.h"
266 %include "gdcmDocEntrySet.h"
267 //CLEANME %include "gdcmElementSet.h"
268 //CLEANME %include "gdcmDictSet.h"
269 //CLEANME %include "gdcmTS.h"
270 //CLEANME %include "gdcmVR.h"
271 //CLEANME %include "gdcmSQItem.h"
272 %include "gdcmDicomDirElement.h"
273 %include "gdcmDicomDirObject.h"
274 %include "gdcmDicomDirImage.h"
275 %include "gdcmDicomDirSerie.h"
276 %include "gdcmDicomDirStudy.h"
277 %include "gdcmDicomDirPatient.h"
278 %include "gdcmDicomDirMeta.h"
279 %include "gdcmDocument.h"
280 %include "gdcmHeader.h"
281 %include "gdcmHeaderHelper.h"
282 %include "gdcmFile.h"
283 %include "gdcmUtil.h"
284 %include "gdcmGlobal.h"
285 %include "gdcmDicomDir.h"
287 ////////////////////////////////////////////////////////////////////////////
288 // Notes on swig and this file gdcm.i:
290 /////////////////////////////////////
291 // Note 1: swig collision of method overloading and %typemap
292 // Consider the following junk.i file:
296 // #include <iostream>
297 // void Junk(std::string const & bozo) { std::cout << bozo << std::endl; }
298 // void Junk() { std::cout << "Renamed Junk()" << std::endl; }
301 // %typemap(python, in) std::string const &
303 // $1 = new std::string( PyString_AsString( $input ) );
306 // void Junk(std::string const & bozo);
308 // that we compile on linux with:
309 // swig -c++ -python junk.i
310 // g++ -g -I/usr/include/python2.3/ -o junk_wrap.o -c junk_wrap.cxx
311 // g++ junk_wrap.o -shared -g -o _junk.so -L/usr/lib/python2.3/config \
314 // python -c 'from junk import *; Junk("aaa") '
315 // then we get the following unexpected (for novice) python TypeError:
316 // TypeError: No matching function for overloaded 'Junk'
318 // This happens because the swig generated code (at least for python) does
319 // the following two stage process:
320 // 1/ first do a dynamic dispatch ON THE NUMBER OF ARGUMENTS of the overloaded
321 // Junk function (the same happens with method of course). [Note that the
322 // dispatch is NOT done on the type of the arguments].
323 // 2/ second apply the typemap.
324 // When the first dynamic dispatch is executed, the swig generated code
325 // has no knowledge of the typemap, and thus expects a pointer to a std::string
326 // type i.e. an argument to Junk of the form _p_std__int<address>. But this
327 // is not what python handles to Junk ! An invocation of the form 'Junk("aaa")'
328 // will make Python pass a PyString to swig (and this is precisely why we
329 // wrote the typemap). And this will fail....
330 /////////////////////////////////////