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