]> Creatis software - gdcm.git/blob - gdcmPython/gdcm.i
* Fix compilation error for python due to my commit on DataEntry changes
[gdcm.git] / gdcmPython / gdcm.i
1 %module gdcm
2 #pragma SWIG nowarn=504
3 %{
4 #include <iostream>
5
6 #include "gdcmCommon.h"
7 #include "gdcmBase.h"
8 #include "gdcmDict.h"
9 #include "gdcmDictEntry.h"
10 #include "gdcmDictSet.h"
11 #include "gdcmDicomDir.h"
12 #include "gdcmDicomDirElement.h"
13 #include "gdcmDicomDirImage.h"
14 #include "gdcmDicomDirMeta.h"
15 #include "gdcmDicomDirObject.h"
16 #include "gdcmDicomDirPatient.h"
17 #include "gdcmDicomDirStudy.h"
18 #include "gdcmDicomDirSerie.h"
19 #include "gdcmDocEntrySet.h"
20 #include "gdcmDocument.h"
21 #include "gdcmElementSet.h"
22 #include "gdcmFileHelper.h"
23 #include "gdcmGlobal.h"
24 #include "gdcmFile.h"
25 #include "gdcmSerieHelper.h"
26 #include "gdcmRLEFramesInfo.h"
27 #include "gdcmJPEGFragmentsInfo.h"
28 #include "gdcmSQItem.h"
29 #include "gdcmUtil.h"
30 #include "gdcmDocEntry.h"
31 #include "gdcmDataEntry.h"
32 #include "gdcmSeqEntry.h"
33 #include "gdcmVR.h"
34 #include "gdcmTS.h"
35 #include "gdcmDictGroupName.h"
36
37 ////////////////////////////////////////////////////////////////////////////
38 /// Refer (below) to the definition of multi-argument typemap
39 ///   %typemap(python, in)
40 ///      ( gdcm::DicomDir::Method*, void*, gdcm::DicomDir::Method*)
41 /// for detail on gdcmPythonVoidFunc() and gdcmPythonVoidFuncArgDelete().
42 void gdcmPythonVoidFunc(void *arg)
43 {
44    PyObject *arglist, *result;
45    PyObject *func = (PyObject *)arg;
46
47    arglist = Py_BuildValue("()");
48
49    result = PyEval_CallObject(func, arglist);
50    Py_DECREF(arglist);
51
52    if (result)
53    {
54       Py_XDECREF(result);
55    }
56    else
57    {
58       if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
59       {
60          std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
61          Py_Exit(1);
62       }
63       PyErr_Print();
64    }
65 }
66
67 void gdcmPythonVoidFuncArgDelete(void *arg)
68 {
69    PyObject *func = (PyObject *)arg;
70    if (func)
71    {
72       Py_DECREF(func);
73    }
74 }
75
76 /// This is required in order to avoid %including all the gdcm include files.
77 using namespace gdcm;
78 %}
79
80
81 ///////////////////////  typemap section  ////////////////////////////////////
82
83 ////////////////////////////////////////////////
84 // Redefine all types used
85 typedef char int8_t;
86 typedef unsigned char uint8_t;
87 typedef short int16_t;
88 typedef unsigned short uint16_t;
89 typedef int int32_t;
90 typedef unsigned int uint32_t;
91 typedef long long int64_t;
92 typedef unsigned long long uint64_t;
93
94 ////////////////////////////////////////////////
95 // Convert a DocEntry * to the real derived class
96 %typemap(out) gdcm::DocEntry * 
97 {
98    PyObject *newEntry;
99
100    if($1)
101    {
102       if(dynamic_cast<SeqEntry *>($1)) // SeqEntry *
103          newEntry = SWIG_NewPointerObj($1,SWIGTYPE_p_gdcm__SeqEntry,0);
104       else if(dynamic_cast<BinEntry *>($1)) // BinEntry *
105          newEntry = SWIG_NewPointerObj($1,SWIGTYPE_p_gdcm__BinEntry,0);
106       else // ValEntry *
107          newEntry = SWIG_NewPointerObj($1,SWIGTYPE_p_gdcm__ValEntry,0);
108    }
109    else
110    {
111       newEntry = Py_BuildValue("");
112    }
113    $result = newEntry;
114 }
115
116 ////////////////////////////////////////////////////////////////////////////
117 // Multi-argument typemap designed for wrapping the progress related methods
118 // in order to control from an external application the computation of
119 // a DicomDir object (see DicomDir::SetStartMethod*,
120 // DicomDir::SetProgressMethod* and DicomDir::SetEndMethod*).
121 // Motivation: since DicomDir parsing can be quite long, a GUI application
122 //             needs to display the avancement and potentially offer a
123 //             cancel method to the user (when this one feels things are
124 //             longer than expected).
125 // Example of usage: refer to demo/DicomDirProgressMethod.py
126 // Note: Uses gdcmPythonVoidFunc and gdcmPythonVoidFuncArgDelete defined
127 //       in the Swig verbatim section of this gdcm.i i.e. in the above section
128 //       enclosed within the %{ ... %} scope operator ).
129 %typemap(python, in) (void(*method)(void *),void *arg,void(*argDelete)(void *))
130 {
131         if($input!=Py_None)
132         {
133                 Py_INCREF($input);
134                 $1=gdcmPythonVoidFunc;
135                 $2=$input;
136                 $3=gdcmPythonVoidFuncArgDelete;
137         }
138         else
139         {
140                 $1=NULL;
141                 $2=NULL;
142                 $3=NULL;
143         }
144 }
145
146 ////////////////////  STL string versus Python str  ////////////////////////
147 // Convertion returning a C++ string.
148 %typemap(out) std::string
149 {
150     $result = PyString_FromString(($1).c_str());
151 }
152
153 %typemap(out) string
154 {
155     $result = PyString_FromString(($1).c_str());
156 }
157
158 %typemap(out) std::string const &
159 {
160     $result = PyString_FromString(($1)->c_str());
161 }
162
163 // Convertion of incoming Python str to STL string
164 %typemap(python, in) const std::string, std::string
165 {
166   $1 = PyString_AsString($input);
167 }
168
169 // Same convertion as above but references (since swig converts C++
170 // refererences to pointers)
171 %typemap(python, in) std::string const &
172 {
173    $1 = new std::string( PyString_AsString( $input ) );
174 }
175
176 ////////////////////  gdcm.TagName versus Python str  //////////////////////
177 %typemap(out) gdcm::TagName, const gdcm::TagName &
178 {
179     $result = PyString_FromString(($1)->c_str());
180 }
181
182 // Convertion of incoming Python str to STL string
183 %typemap(python, in) const gdcm::TagName, gdcm::TagName
184 {
185   $1 = PyString_AsString($input);
186 }
187
188 // Same convertion as above but references (since swig converts C++
189 // refererences to pointers)
190 %typemap(python, in) gdcm::TagName const &
191 {
192    $1 = new std::string( PyString_AsString( $input ) );
193 }
194
195 ////////////////////////////////////////////////////////////////////////////
196 // Because overloading and %rename don't work together (see below Note 1)
197 // we need to ignore some methods (e.g. the overloaded default constructor).
198 // The gdcm::File class doesn't have any SetFilename method anyhow, and
199 // this constructor is only used internaly (not from the API) so this is
200 // not a big loss.
201 %ignore gdcm::binary_write(std::ostream &,uint32_t const &);
202 %ignore gdcm::binary_write(std::ostream &,uint16_t const &);
203
204 %ignore gdcm::DicomDir::SetStartMethod(DicomDir::Method *method,void *arg = NULL);
205 %ignore gdcm::DicomDir::SetProgressMethod(DicomDir::Method *method,void *arg = NULL);
206 %ignore gdcm::DicomDir::SetEndMethod(DicomDir::Method *method,void *arg = NULL);
207
208 // Ignore all placed in gdcmCommon.h
209 %ignore GDCM_UNKNOWN;
210 %ignore GDCM_UNFOUND;
211 %ignore GDCM_BINLOADED;
212 %ignore GDCM_NOTLOADED;
213 %ignore GDCM_UNREAD;
214 %ignore GDCM_NOTASCII;
215 %ignore GDCM_PIXELDATA;
216 %ignore GDCM_LEGACY;
217 %ignore GDCM_VRUNKNOWN;
218
219 %constant const char *UNKNOWN        = "gdcm::Unknown";
220 %constant const char *UNFOUND        = "gdcm::Unfound";
221 %constant const char *BINLOADED      = "gdcm::Binary data loaded";
222 %constant const char *NOTLOADED      = "gdcm::NotLoaded";
223 %constant const char *UNREAD         = "gdcm::UnRead";
224 %constant const char *GDCM_NOTASCII  = "gdcm::NotAscii";
225 %constant const char *GDCM_PIXELDATA = "gdcm::Pixel Data to be loaded";
226 %constant const char *VRUNKNOWN      = "  ";
227
228 ////////////////////////////////////////////////////////////////////////////
229 // Warning: Order matters !
230 %include "gdcmCommon.h"
231 %include "gdcmBase.h"
232 %include "gdcmDictEntry.h"
233 %include "gdcmDict.h"
234 %include "gdcmDictSet.h"
235 %include "gdcmDocEntrySet.h"
236 %include "gdcmElementSet.h"
237 %include "gdcmSQItem.h"
238 %include "gdcmDicomDirElement.h"
239 %include "gdcmDicomDirObject.h"
240 %include "gdcmDicomDirImage.h"
241 %include "gdcmDicomDirSerie.h"
242 %include "gdcmDicomDirStudy.h"
243 %include "gdcmDicomDirPatient.h"
244 %include "gdcmDicomDirMeta.h"
245 %include "gdcmDocument.h"
246 %include "gdcmFile.h"
247 %include "gdcmSerieHelper.h"
248 %include "gdcmFileHelper.h"
249 %include "gdcmUtil.h"
250 %include "gdcmGlobal.h"
251 %include "gdcmDicomDir.h"
252 %include "gdcmDocEntry.h"
253 %include "gdcmDataEntry.h"
254 %include "gdcmSeqEntry.h"
255 %include "gdcmVR.h"
256 %include "gdcmTS.h"
257 %include "gdcmDictGroupName.h"
258
259 ////////////////////////////////////////////////////////////////////////////
260 // Notes on swig and this file gdcm.i:
261 //
262 /////////////////////////////////////
263 // Note 1: swig collision of method overloading and %typemap
264 // Consider the following junk.i file:
265 //     %module junk
266 //     %{
267 //     #include <string>
268 //     #include <iostream>
269 //     void Junk(std::string const & bozo) { std::cout << bozo << std::endl; }
270 //     void Junk() { std::cout << "Renamed Junk()" << std::endl; }
271 //     %}
272 //   
273 //     %typemap(python, in) std::string const &
274 //     {
275 //     $1 = new std::string( PyString_AsString( $input ) );
276 //     }
277 //     void Junk();
278 //     void Junk(std::string const & bozo);
279 //
280 // that we compile on linux with:
281 //    swig -c++ -python junk.i
282 //    g++ -g -I/usr/include/python2.3/ -o junk_wrap.o -c junk_wrap.cxx
283 //    g++ junk_wrap.o -shared -g -o _junk.so -L/usr/lib/python2.3/config \
284 //        -lpython2.3
285 // and invoque with:
286 //    python -c 'from junk import *; Junk("aaa") '
287 // then we get the following unexpected (for novice) python TypeError:
288 //    TypeError: No matching function for overloaded 'Junk'
289 //
290 // This happens because the swig generated code (at least for python) does
291 // the following two stage process:
292 //   1/ first do a dynamic dispatch ON THE NUMBER OF ARGUMENTS of the overloaded
293 //      Junk function (the same happens with method of course). [Note that the
294 //      dispatch is NOT done on the type of the arguments].
295 //   2/ second apply the typemap.
296 // When the first dynamic dispatch is executed, the swig generated code
297 // has no knowledge of the typemap, and thus expects a pointer to a std::string
298 // type i.e. an argument to Junk of the form _p_std__int<address>. But this
299 // is not what python handles to Junk ! An invocation of the form 'Junk("aaa")'
300 // will make Python pass a PyString to swig (and this is precisely why we
301 // wrote the typemap). And this will fail....
302 /////////////////////////////////////