]> Creatis software - gdcm.git/blob - gdcmPython/gdcm.i
* src/gdcmDicomDirElement.h, gdcmDict.h : rename AddNewXxx methods to AddXxx.
[gdcm.git] / gdcmPython / gdcm.i
1 %module gdcm
2 %{
3 #include <iostream>
4
5 #include "gdcmCommon.h"
6 #include "gdcmBase.h"
7 #include "gdcmDict.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"
23 #include "gdcmFile.h"
24 #include "gdcmSerieHeader.h"
25 #include "gdcmRLEFramesInfo.h"
26 #include "gdcmJPEGFragmentsInfo.h"
27 #include "gdcmSQItem.h"
28 #include "gdcmUtil.h"
29 #include "gdcmDocEntry.h"
30 #include "gdcmValEntry.h"
31 #include "gdcmBinEntry.h"
32 #include "gdcmSeqEntry.h"
33
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)
40 {
41    PyObject *arglist, *result;
42    PyObject *func = (PyObject *)arg;
43
44    arglist = Py_BuildValue("()");
45
46    result = PyEval_CallObject(func, arglist);
47    Py_DECREF(arglist);
48
49    if (result)
50    {
51       Py_XDECREF(result);
52    }
53    else
54    {
55       if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
56       {
57          std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
58          Py_Exit(1);
59       }
60       PyErr_Print();
61    }
62 }
63
64 void gdcmPythonVoidFuncArgDelete(void *arg)
65 {
66    PyObject *func = (PyObject *)arg;
67    if (func)
68    {
69       Py_DECREF(func);
70    }
71 }
72
73 /// This is required in order to avoid %including all the gdcm include files.
74 using namespace gdcm;
75 %}
76
77
78 ///////////////////////  typemap section  ////////////////////////////////////
79
80 ////////////////////////////////////////////////
81 // Redefine all types used
82 typedef char int8_t;
83 typedef unsigned char uint8_t;
84 typedef short int16_t;
85 typedef unsigned short uint16_t;
86 typedef int int32_t;
87 typedef unsigned int uint32_t;
88 typedef long long int64_t;
89 typedef unsigned long long uint64_t;
90
91 ////////////////////////////////////////////////
92 // Convert a DocEntry * to the real derived class
93 %typemap(out) gdcm::DocEntry * 
94 {
95    PyObject *newEntry;
96
97    if($1)
98    {
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);
103       else // ValEntry *
104          newEntry = SWIG_NewPointerObj($1,SWIGTYPE_p_gdcm__ValEntry,0);
105    }
106    else
107    {
108       newEntry = Py_BuildValue("");
109    }
110    $result = newEntry;
111 }
112
113 ////////////////////////////////////////////////
114 // Convert an STL list<> to a python native list
115 //%typemap(out) std::list<std::string> * 
116 //{
117 //   PyObject *newItem = (PyObject *)0;
118 //   PyObject *newList = PyList_New(0); // The result of this typemap
119 //
120 //   for (std::list<std::string>::iterator strIt = ($1)->begin();
121 //        strIt != ($1)->end();
122 //        ++strIt)
123 //   {
124 //      newItem = PyString_FromString(strIt->c_str());
125 //      PyList_Append( newList, newItem);
126 //   }
127 //   $result = newList;
128 //}
129
130 //////////////////////////////////////////////////////////////////
131 // Convert an STL map<> (hash table) to a python native dictionary
132 //%typemap(out) std::map<std::string, std::list<std::string> > * 
133 //{
134 //   PyObject *newDict = PyDict_New(); // The result of this typemap
135 //   PyObject *newKey = (PyObject *)0;
136 //   PyObject *newVal = (PyObject *)0;
137 //
138 //   for (std::map<std::string,
139 //        std::list<std::string> >::iterator tag = ($1)->begin();
140 //        tag != ($1)->end(); ++tag)
141 //   {
142 //      std::string first = tag->first;
143 //      // Do not publish entries whose keys is made of spaces
144 //      if (first.length() == 0)
145 //         continue;
146 //      newKey = PyString_FromString(first.c_str());
147 //
148 //      PyObject *newList = PyList_New(0);
149 //      for (std::list<std::string>::iterator itemIt = tag->second.begin();
150 //           itemIt != tag->second.end();
151 //           ++itemIt)
152 //      {
153 //         newVal = PyString_FromString(itemIt->c_str());
154 //         PyList_Append( newList, newVal);
155 //      }
156 //      PyDict_SetItem( newDict, newKey, newList);
157 //   }
158 //   $result = newDict;
159 //}
160
161 /////////////////////////////////////////////////////////
162 // Convert a c++ hash table in a python native dictionary
163 //%typemap(out) gdcm::TagDocEntryHT & 
164 //{
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
170 //
171 //   for (gdcm::TagDocEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag)
172 //   {
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
177 //      // TagKey as key.
178 //      if (rawName == "Unknown")
179 //         rawName = tag->second->GetKey();
180 //      newKey = PyString_FromString(rawName.c_str());
181 //
182 //      // Element values are striped from leading/trailing spaces
183 //      gdcm::ValEntry *valEntryPtr = dynamic_cast< gdcm::ValEntry* >(tag->second);
184 //      if ( valEntryPtr )
185 //      {
186 //         rawValue = valEntryPtr->GetValue();
187 //      }
188 //      else
189 //        continue; 
190 //      newVal = PyString_FromString(rawValue.c_str());
191 //      PyDict_SetItem( newDict, newKey, newVal);
192 //   }
193 //   $result = newDict;
194 //}
195
196 /////////////////////////////////////
197 //%typemap(out) ListDicomDirPatient & 
198 //{
199 //      PyObject *newItem = (PyObject *)0;
200 //      $result = PyList_New(0); // The result of this typemap
201 //
202 //      for (std::list<gdcm::DicomDirPatient *>::iterator newIt = ($1)->begin();
203 //          newIt != ($1)->end(); ++newIt)
204 //   {
205 //              newItem = SWIG_NewPointerObj(*newIt,SWIGTYPE_p_DicomDirPatient,0);
206 //              PyList_Append($result, newItem);
207 //      }
208 //}
209
210 //%typemap(out) ListDicomDirStudy & 
211 //{
212 //      PyObject *newItem = (PyObject *)0;
213 //      $result = PyList_New(0); // The result of this typemap
214 //
215 //      for (std::list<gdcm::DicomDirStudy *>::iterator newIt = ($1)->begin();
216 //          newIt != ($1)->end(); ++newIt)
217 //   {
218 //              newItem = SWIG_NewPointerObj(*newIt,SWIGTYPE_p_DicomDirStudy,0);
219 //              PyList_Append($result, newItem);
220 //      }
221 //}
222
223 //%typemap(out) ListDicomDirSerie & 
224 //{
225 //      PyObject* newItem = (PyObject*)0;
226 //      $result = PyList_New(0); // The result of this typemap
227 //
228 //      for (std::list<gdcm::DicomDirSerie *>::iterator newIt = ($1)->begin();
229 //          newIt != ($1)->end(); ++newIt)
230 //   {
231 //              newItem = SWIG_NewPointerObj(*newIt,SWIGTYPE_p_DicomDirSerie,0);
232 //              PyList_Append($result, newItem);
233 //      }
234 //}
235
236 //%typemap(out) ListDicomDirImage & 
237 //{
238 //      PyObject* newItem = (PyObject*)0;
239 //      $result = PyList_New(0); // The result of this typemap
240 //
241 //      for (std::list<gdcm::DicomDirImage *>::iterator newIt = ($1)->begin();
242 //          newIt != ($1)->end(); ++newIt) 
243 //   {
244 //              newItem = SWIG_NewPointerObj(*newIt,SWIGTYPE_p_DicomDirImage,0);
245 //              PyList_Append($result, newItem);
246 //      }
247 //}
248
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 *, 
263                        void * = NULL, 
264                        gdcm::DicomDir::Method * = NULL )
265 {
266         if($input!=Py_None)
267         {
268                 Py_INCREF($input);
269                 $1=gdcmPythonVoidFunc;
270                 $2=$input;
271                 $3=gdcmPythonVoidFuncArgDelete;
272         }
273         else
274         {
275                 $1=NULL;
276                 $2=NULL;
277                 $3=NULL;
278         }
279 }
280
281 ////////////////////  STL string versus Python str  ////////////////////////
282 // Convertion returning a C++ string.
283 %typemap(out) string, std::string 
284 {
285     $result = PyString_FromString(($1).c_str());
286 }
287
288 // Convertion of incoming Python str to STL string
289 %typemap(python, in) const std::string, std::string
290 {
291   $1 = PyString_AsString($input);
292 }
293
294 // Same convertion as above but references (since swig converts C++
295 // refererences to pointers)
296 %typemap(python, in) std::string const &
297 {
298    $1 = new std::string( PyString_AsString( $input ) );
299 }
300
301 ////////////////////  gdcm.TagName versus Python str  //////////////////////
302 %typemap(out) gdcm::TagName, const gdcm::TagName &
303 {
304     $result = PyString_FromString(($1)->c_str());
305 }
306
307 // Convertion of incoming Python str to STL string
308 %typemap(python, in) const gdcm::TagName, gdcm::TagName
309 {
310   $1 = PyString_AsString($input);
311 }
312
313 // Same convertion as above but references (since swig converts C++
314 // refererences to pointers)
315 %typemap(python, in) gdcm::TagName const &
316 {
317    $1 = new std::string( PyString_AsString( $input ) );
318 }
319
320
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
326 // not a big loss.
327 %ignore gdcm::binary_write(std::ostream &,uint32_t const &);
328 %ignore gdcm::binary_write(std::ostream &,uint16_t const &);
329
330 %ignore gdcm::File::File();
331 %ignore gdcm::DicomDir::DicomDir();
332
333 // Ignore all placed in gdcmCommon.h
334 %ignore GDCM_UNKNOWN;
335 %ignore GDCM_UNFOUND;
336 %ignore GDCM_BINLOADED;
337 %ignore GDCM_NOTLOADED;
338 %ignore GDCM_UNREAD;
339
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"
368
369 ////////////////////////////////////////////////////////////////////////////
370 // Notes on swig and this file gdcm.i:
371 //
372 /////////////////////////////////////
373 // Note 1: swig collision of method overloading and %typemap
374 // Consider the following junk.i file:
375 //     %module junk
376 //     %{
377 //     #include <string>
378 //     #include <iostream>
379 //     void Junk(std::string const & bozo) { std::cout << bozo << std::endl; }
380 //     void Junk() { std::cout << "Renamed Junk()" << std::endl; }
381 //     %}
382 //   
383 //     %typemap(python, in) std::string const &
384 //     {
385 //     $1 = new std::string( PyString_AsString( $input ) );
386 //     }
387 //     void Junk();
388 //     void Junk(std::string const & bozo);
389 //
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 \
394 //        -lpython2.3
395 // and invoque with:
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'
399 //
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 /////////////////////////////////////