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