]> Creatis software - gdcm.git/blob - gdcmPython/gdcm.i
* Bugs fix for the Windows build with VC6
[gdcm.git] / gdcmPython / gdcm.i
1 %module gdcm
2 %{
3 #include "gdcmCommon.h"
4 #include "gdcmDict.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"
18 #include "gdcmFile.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"
25 #include "gdcmUtil.h"
26 #include "gdcmValEntry.h"
27
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)
34 {
35   PyObject *arglist, *result;
36   PyObject *func = (PyObject *)arg;
37
38   arglist = Py_BuildValue("()");
39
40   result = PyEval_CallObject(func, arglist);
41   Py_DECREF(arglist);
42
43   if (result)
44     {
45     Py_XDECREF(result);
46     }
47   else
48     {
49     if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
50       {
51       std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
52       Py_Exit(1);
53       }
54     PyErr_Print();
55     }
56 }
57
58 void gdcmPythonVoidFuncArgDelete(void *arg)
59 {
60   PyObject *func = (PyObject *)arg;
61   if (func)
62     {
63     Py_DECREF(func);
64     }
65 }
66
67 /// This is required in order to avoid %including all the gdcm include files.
68 using namespace gdcm;
69 %}
70
71
72 ///////////////////////  typemap section  ////////////////////////////////////
73
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
79
80    for (std::list<std::string>::iterator NewString = ($1)->begin();
81         NewString != ($1)->end();
82         ++NewString)
83    {
84       NewItem = PyString_FromString(NewString->c_str());
85       PyList_Append( NewList, NewItem);
86    }
87    $result = NewList;
88 }
89
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;
96
97    for (std::map<std::string,
98         std::list<std::string> >::iterator tag = ($1)->begin();
99         tag != ($1)->end(); ++tag)
100    {
101       std::string first = tag->first;
102       // Do not publish entries whose keys is made of spaces
103       if (first.length() == 0)
104          continue;
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();
109            ++Item)
110       {
111          NewVal = PyString_FromString(Item->c_str());
112          PyList_Append( NewList, NewVal);
113       }
114       PyDict_SetItem( NewDict, NewKey, NewList);
115    }
116    $result = NewDict;
117 }
118
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
127
128    for (gdcm::TagDocEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag)
129    {
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
134       // TagKey as key.
135       if (RawName == "Unknown")
136          RawName = tag->second->GetKey();
137       NewKey = PyString_FromString(RawName.c_str());
138
139       // Element values are striped from leading/trailing spaces
140       if (gdcm::ValEntry* ValEntryPtr =
141                 dynamic_cast< gdcm::ValEntry* >(tag->second) )
142       {
143          RawValue = ValEntryPtr->GetValue();
144       }
145       else
146         continue; 
147       NewVal = PyString_FromString(RawValue.c_str());
148       PyDict_SetItem( NewDict, NewKey, NewVal);
149    }
150    $result = NewDict;
151 }
152
153 /////////////////////////////////////
154 %typemap(out) ListDicomDirPatient & {
155         PyObject* NewItem = (PyObject*)0;
156         $result = PyList_New(0); // The result of this typemap
157
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);
162         }
163 }
164
165 %typemap(out) ListDicomDirStudy & {
166         PyObject* NewItem = (PyObject*)0;
167         $result = PyList_New(0); // The result of this typemap
168
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);
173         }
174 }
175
176 %typemap(out) ListDicomDirSerie & {
177         PyObject* NewItem = (PyObject*)0;
178         $result = PyList_New(0); // The result of this typemap
179
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);
184         }
185 }
186
187 %typemap(out) ListDicomDirImage & {
188         PyObject* NewItem = (PyObject*)0;
189         $result = PyList_New(0); // The result of this typemap
190
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);
195         }
196 }
197
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 *, 
212                        void * = NULL, 
213                        gdcm::DicomDir::Method * = NULL )
214 {
215         if($input!=Py_None)
216         {
217                 Py_INCREF($input);
218                 $1=gdcmPythonVoidFunc;
219                 $2=$input;
220                 $3=gdcmPythonVoidFuncArgDelete;
221         }
222         else
223         {
224                 $1=NULL;
225                 $2=NULL;
226                 $3=NULL;
227         }
228 }
229
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());
234 }
235
236 // Convertion of incoming Python str to STL string
237 %typemap(python, in) const std::string, std::string
238 {
239   $1 = PyString_AsString($input);
240 }
241
242 // Same convertion as above but references (since swig converts C++
243 // refererences to pointers)
244 %typemap(python, in) std::string const &
245 {
246    $1 = new std::string( PyString_AsString( $input ) );
247 }
248
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
254 // not a big loss.
255 %ignore gdcm::Header::Header();
256 %ignore gdcm::DicomDir::DicomDir();
257
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"
286
287 ////////////////////////////////////////////////////////////////////////////
288 // Notes on swig and this file gdcm.i:
289 //
290 /////////////////////////////////////
291 // Note 1: swig collision of method overloading and %typemap
292 // Consider the following junk.i file:
293 //     %module junk
294 //     %{
295 //     #include <string>
296 //     #include <iostream>
297 //     void Junk(std::string const & bozo) { std::cout << bozo << std::endl; }
298 //     void Junk() { std::cout << "Renamed Junk()" << std::endl; }
299 //     %}
300 //   
301 //     %typemap(python, in) std::string const &
302 //     {
303 //     $1 = new std::string( PyString_AsString( $input ) );
304 //     }
305 //     void Junk();
306 //     void Junk(std::string const & bozo);
307 //
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 \
312 //        -lpython2.3
313 // and invoque with:
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'
317 //
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 /////////////////////////////////////