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