]> Creatis software - gdcm.git/blob - gdcmPython/gdcm.i
ee606d27ea46695ac531f34a777da2d36623e55f
[gdcm.git] / gdcmPython / gdcm.i
1 %module gdcm
2 %{
3 #include "gdcmCommon.h"
4 #include "gdcmBase.h"
5 #include "gdcmDict.h"
6 #include "gdcmDictEntry.h"
7 #include "gdcmDictSet.h"
8 #include "gdcmDicomDir.h"
9 #include "gdcmDicomDirElement.h"
10 #include "gdcmDicomDirImage.h"
11 #include "gdcmDicomDirMeta.h"
12 #include "gdcmDicomDirObject.h"
13 #include "gdcmDicomDirPatient.h"
14 #include "gdcmDicomDirStudy.h"
15 #include "gdcmDicomDirSerie.h"
16 #include "gdcmDocEntrySet.h"
17 #include "gdcmDocument.h"
18 #include "gdcmElementSet.h"
19 #include "gdcmFile.h"
20 #include "gdcmGlobal.h"
21 #include "gdcmHeader.h"
22 #include "gdcmSerieHeader.h"
23 #include "gdcmRLEFramesInfo.h"
24 #include "gdcmJPEGFragmentsInfo.h"
25 #include "gdcmSQItem.h"
26 #include "gdcmUtil.h"
27 #include "gdcmValEntry.h"
28
29 ////////////////////////////////////////////////////////////////////////////
30 /// Refer (below) to the definition of multi-argument typemap
31 ///   %typemap(python, in)
32 ///      ( gdcm::DicomDir::Method*, void*, gdcm::DicomDir::Method*)
33 /// for detail on gdcmPythonVoidFunc() and gdcmPythonVoidFuncArgDelete().
34 void gdcmPythonVoidFunc(void *arg)
35 {
36    PyObject *arglist, *result;
37    PyObject *func = (PyObject *)arg;
38
39    arglist = Py_BuildValue("()");
40
41    result = PyEval_CallObject(func, arglist);
42    Py_DECREF(arglist);
43
44    if (result)
45    {
46       Py_XDECREF(result);
47    }
48    else
49    {
50       if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
51       {
52          std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
53          Py_Exit(1);
54       }
55       PyErr_Print();
56    }
57 }
58
59 void gdcmPythonVoidFuncArgDelete(void *arg)
60 {
61    PyObject *func = (PyObject *)arg;
62    if (func)
63    {
64       Py_DECREF(func);
65    }
66 }
67
68 /// This is required in order to avoid %including all the gdcm include files.
69 using namespace gdcm;
70 %}
71
72
73 ///////////////////////  typemap section  ////////////////////////////////////
74
75 ////////////////////////////////////////////////
76 // Convert an STL list<> to a python native list
77 %typemap(out) std::list<std::string> * 
78 {
79    PyObject* NewItem = (PyObject*)0;
80    PyObject* NewList = PyList_New(0); // The result of this typemap
81
82    for (std::list<std::string>::iterator NewString = ($1)->begin();
83         NewString != ($1)->end();
84         ++NewString)
85    {
86       NewItem = PyString_FromString(NewString->c_str());
87       PyList_Append( NewList, NewItem);
88    }
89    $result = NewList;
90 }
91
92 //////////////////////////////////////////////////////////////////
93 // Convert an STL map<> (hash table) to a python native dictionary
94 %typemap(out) std::map<std::string, std::list<std::string> > * 
95 {
96    PyObject* NewDict = PyDict_New(); // The result of this typemap
97    PyObject* NewKey = (PyObject*)0;
98    PyObject* NewVal = (PyObject*)0;
99
100    for (std::map<std::string,
101         std::list<std::string> >::iterator tag = ($1)->begin();
102         tag != ($1)->end(); ++tag)
103    {
104       std::string first = tag->first;
105       // Do not publish entries whose keys is made of spaces
106       if (first.length() == 0)
107          continue;
108       NewKey = PyString_FromString(first.c_str());
109       PyObject* NewList = PyList_New(0);
110       for (std::list<std::string>::iterator Item = tag->second.begin();
111            Item != tag->second.end();
112            ++Item)
113       {
114          NewVal = PyString_FromString(Item->c_str());
115          PyList_Append( NewList, NewVal);
116       }
117       PyDict_SetItem( NewDict, NewKey, NewList);
118    }
119    $result = NewDict;
120 }
121
122 /////////////////////////////////////////////////////////
123 // Convert a c++ hash table in a python native dictionary
124 %typemap(out) gdcm::TagDocEntryHT & 
125 {
126    PyObject* NewDict = PyDict_New(); // The result of this typemap
127    std::string RawName;              // Element name as gotten from gdcm
128    PyObject* NewKey = (PyObject*)0;  // Associated name as python object
129    std::string RawValue;             // Element value as gotten from gdcm
130    PyObject* NewVal = (PyObject*)0;  // Associated value as python object
131
132    for (gdcm::TagDocEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag)
133    {
134       // The element name shall be the key:
135       RawName = tag->second->GetName();
136       // gdcm unrecognized (including not loaded because their size exceeds
137       // the user specified treshold) elements are exported with their
138       // TagKey as key.
139       if (RawName == "Unknown")
140          RawName = tag->second->GetKey();
141       NewKey = PyString_FromString(RawName.c_str());
142
143       // Element values are striped from leading/trailing spaces
144           gdcm::ValEntry* ValEntryPtr = dynamic_cast< gdcm::ValEntry* >(tag->second);
145       if ( ValEntryPtr )
146       {
147          RawValue = ValEntryPtr->GetValue();
148       }
149       else
150         continue; 
151       NewVal = PyString_FromString(RawValue.c_str());
152       PyDict_SetItem( NewDict, NewKey, NewVal);
153    }
154    $result = NewDict;
155 }
156
157 /////////////////////////////////////
158 %typemap(out) ListDicomDirPatient & 
159 {
160         PyObject* NewItem = (PyObject*)0;
161         $result = PyList_New(0); // The result of this typemap
162
163         for (std::list<gdcm::DicomDirPatient *>::iterator New = ($1)->begin();
164             New != ($1)->end(); ++New)
165    {
166                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirPatient,1);
167                 PyList_Append($result, NewItem);
168         }
169 }
170
171 %typemap(out) ListDicomDirStudy & 
172 {
173         PyObject* NewItem = (PyObject*)0;
174         $result = PyList_New(0); // The result of this typemap
175
176         for (std::list<gdcm::DicomDirStudy *>::iterator New = ($1)->begin();
177             New != ($1)->end(); ++New)
178    {
179                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirStudy,1);
180                 PyList_Append($result, NewItem);
181         }
182 }
183
184 %typemap(out) ListDicomDirSerie & 
185 {
186         PyObject* NewItem = (PyObject*)0;
187         $result = PyList_New(0); // The result of this typemap
188
189         for (std::list<gdcm::DicomDirSerie *>::iterator New = ($1)->begin();
190             New != ($1)->end(); ++New)
191    {
192                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirSerie,1);
193                 PyList_Append($result, NewItem);
194         }
195 }
196
197 %typemap(out) ListDicomDirImage & 
198 {
199         PyObject* NewItem = (PyObject*)0;
200         $result = PyList_New(0); // The result of this typemap
201
202         for (std::list<gdcm::DicomDirImage *>::iterator New = ($1)->begin();
203             New != ($1)->end(); ++New) 
204    {
205                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirImage,1);
206                 PyList_Append($result, NewItem);
207         }
208 }
209
210 ////////////////////////////////////////////////////////////////////////////
211 // Multi-argument typemap designed for wrapping the progress related methods
212 // in order to control from an external application the computation of
213 // a DicomDir object (see DicomDir::SetStartMethod*,
214 // DicomDir::SetProgressMethod* and DicomDir::SetEndMethod*).
215 // Motivation: since DicomDir parsing can be quite long, a GUI application
216 //             needs to display the avancement and potentially offer a
217 //             cancel method to the user (when this one feels things are
218 //             longer than expected).
219 // Example of usage: refer to demo/DicomDirProgressMethod.py
220 // Note: Uses gdcmPythonVoidFunc and gdcmPythonVoidFuncArgDelete defined
221 //       in the Swig verbatim section of this gdcm.i i.e. in the above section
222 //       enclosed within the %{ ... %} scope operator ).
223 %typemap(python, in) ( gdcm::DicomDir::Method *, 
224                        void * = NULL, 
225                        gdcm::DicomDir::Method * = NULL )
226 {
227         if($input!=Py_None)
228         {
229                 Py_INCREF($input);
230                 $1=gdcmPythonVoidFunc;
231                 $2=$input;
232                 $3=gdcmPythonVoidFuncArgDelete;
233         }
234         else
235         {
236                 $1=NULL;
237                 $2=NULL;
238                 $3=NULL;
239         }
240 }
241
242 ////////////////////  STL string versus Python str  ////////////////////////
243 // Convertion returning a C++ string.
244 %typemap(out) string, std::string 
245 {
246     $result = PyString_FromString(($1).c_str());
247 }
248
249 // Convertion of incoming Python str to STL string
250 %typemap(python, in) const std::string, std::string
251 {
252   $1 = PyString_AsString($input);
253 }
254
255 // Same convertion as above but references (since swig converts C++
256 // refererences to pointers)
257 %typemap(python, in) std::string const &
258 {
259    $1 = new std::string( PyString_AsString( $input ) );
260 }
261
262 ////////////////////////////////////////////////////////////////////////////
263 // Because overloading and %rename don't work together (see below Note 1)
264 // we need to ignore some methods (e.g. the overloaded default constructor).
265 // The gdcm::Header class doesn't have any SetFilename method anyhow, and
266 // this constructor is only used internaly (not from the API) so this is
267 // not a big loss.
268 %ignore gdcm::binary_write(std::ostream &,uint32_t const &);
269 %ignore gdcm::binary_write(std::ostream &,uint16_t const &);
270
271 %ignore gdcm::Header::Header();
272 %ignore gdcm::DicomDir::DicomDir();
273
274 // Ignore all placed in gdcmCommon.h
275 %ignore GDCM_UNKNOWN;
276 %ignore GDCM_UNFOUND;
277 %ignore GDCM_BINLOADED;
278 %ignore GDCM_NOTLOADED;
279 %ignore GDCM_UNREAD;
280
281 ////////////////////////////////////////////////////////////////////////////
282 // Warning: Order matters !
283 %include "gdcmCommon.h"
284 %include "gdcmBase.h"
285 %include "gdcmDictEntry.h"
286 %include "gdcmDict.h"
287 %include "gdcmDocEntrySet.h"
288 %include "gdcmElementSet.h"
289 %include "gdcmDictSet.h"
290 %include "gdcmSQItem.h"
291 %include "gdcmDicomDirElement.h"
292 %include "gdcmDicomDirObject.h"
293 %include "gdcmDicomDirImage.h"
294 %include "gdcmDicomDirSerie.h"
295 %include "gdcmDicomDirStudy.h"
296 %include "gdcmDicomDirPatient.h"
297 %include "gdcmDicomDirMeta.h"
298 %include "gdcmDocument.h"
299 %include "gdcmHeader.h"
300 %include "gdcmSerieHeader.h"
301 %include "gdcmFile.h"
302 %include "gdcmUtil.h"
303 %include "gdcmGlobal.h"
304 %include "gdcmDicomDir.h"
305
306 ////////////////////////////////////////////////////////////////////////////
307 // Notes on swig and this file gdcm.i:
308 //
309 /////////////////////////////////////
310 // Note 1: swig collision of method overloading and %typemap
311 // Consider the following junk.i file:
312 //     %module junk
313 //     %{
314 //     #include <string>
315 //     #include <iostream>
316 //     void Junk(std::string const & bozo) { std::cout << bozo << std::endl; }
317 //     void Junk() { std::cout << "Renamed Junk()" << std::endl; }
318 //     %}
319 //   
320 //     %typemap(python, in) std::string const &
321 //     {
322 //     $1 = new std::string( PyString_AsString( $input ) );
323 //     }
324 //     void Junk();
325 //     void Junk(std::string const & bozo);
326 //
327 // that we compile on linux with:
328 //    swig -c++ -python junk.i
329 //    g++ -g -I/usr/include/python2.3/ -o junk_wrap.o -c junk_wrap.cxx
330 //    g++ junk_wrap.o -shared -g -o _junk.so -L/usr/lib/python2.3/config \
331 //        -lpython2.3
332 // and invoque with:
333 //    python -c 'from junk import *; Junk("aaa") '
334 // then we get the following unexpected (for novice) python TypeError:
335 //    TypeError: No matching function for overloaded 'Junk'
336 //
337 // This happens because the swig generated code (at least for python) does
338 // the following two stage process:
339 //   1/ first do a dynamic dispatch ON THE NUMBER OF ARGUMENTS of the overloaded
340 //      Junk function (the same happens with method of course). [Note that the
341 //      dispatch is NOT done on the type of the arguments].
342 //   2/ second apply the typemap.
343 // When the first dynamic dispatch is executed, the swig generated code
344 // has no knowledge of the typemap, and thus expects a pointer to a std::string
345 // type i.e. an argument to Junk of the form _p_std__int<address>. But this
346 // is not what python handles to Junk ! An invocation of the form 'Junk("aaa")'
347 // will make Python pass a PyString to swig (and this is precisely why we
348 // wrote the typemap). And this will fail....
349 /////////////////////////////////////