]> Creatis software - gdcm.git/blob - gdcmPython/gdcm.i
* gdcmPython/ : amelioration to load Version with the library
[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 // Multi-argument typemap designed for wrapping the progress related methods
119 // in order to control from an external application the computation of
120 // a DicomDir object (see DicomDir::SetStartMethod*,
121 // DicomDir::SetProgressMethod* and DicomDir::SetEndMethod*).
122 // Motivation: since DicomDir parsing can be quite long, a GUI application
123 //             needs to display the avancement and potentially offer a
124 //             cancel method to the user (when this one feels things are
125 //             longer than expected).
126 // Example of usage: refer to demo/DicomDirProgressMethod.py
127 // Note: Uses gdcmPythonVoidFunc and gdcmPythonVoidFuncArgDelete defined
128 //       in the Swig verbatim section of this gdcm.i i.e. in the above section
129 //       enclosed within the %{ ... %} scope operator ).
130 %typemap(python, in) ( gdcm::DicomDir::Method *, 
131                        void * = NULL, 
132                        gdcm::DicomDir::Method * = NULL )
133 {
134         if($input!=Py_None)
135         {
136                 Py_INCREF($input);
137                 $1=gdcmPythonVoidFunc;
138                 $2=$input;
139                 $3=gdcmPythonVoidFuncArgDelete;
140         }
141         else
142         {
143                 $1=NULL;
144                 $2=NULL;
145                 $3=NULL;
146         }
147 }
148
149 ////////////////////  STL string versus Python str  ////////////////////////
150 // Convertion returning a C++ string.
151 %typemap(out) std::string
152 {
153     $result = PyString_FromString(($1).c_str());
154 }
155
156 %typemap(out) string
157 {
158     $result = PyString_FromString(($1).c_str());
159 }
160
161 %typemap(out) std::string const &
162 {
163     $result = PyString_FromString(($1)->c_str());
164 }
165
166 // Convertion of incoming Python str to STL string
167 %typemap(python, in) const std::string, std::string
168 {
169   $1 = PyString_AsString($input);
170 }
171
172 // Same convertion as above but references (since swig converts C++
173 // refererences to pointers)
174 %typemap(python, in) std::string const &
175 {
176    $1 = new std::string( PyString_AsString( $input ) );
177 }
178
179 ////////////////////  gdcm.TagName versus Python str  //////////////////////
180 %typemap(out) gdcm::TagName, const gdcm::TagName &
181 {
182     $result = PyString_FromString(($1)->c_str());
183 }
184
185 // Convertion of incoming Python str to STL string
186 %typemap(python, in) const gdcm::TagName, gdcm::TagName
187 {
188   $1 = PyString_AsString($input);
189 }
190
191 // Same convertion as above but references (since swig converts C++
192 // refererences to pointers)
193 %typemap(python, in) gdcm::TagName const &
194 {
195    $1 = new std::string( PyString_AsString( $input ) );
196 }
197
198 ////////////////////////////////////////////////////////////////////////////
199 // Because overloading and %rename don't work together (see below Note 1)
200 // we need to ignore some methods (e.g. the overloaded default constructor).
201 // The gdcm::File class doesn't have any SetFilename method anyhow, and
202 // this constructor is only used internaly (not from the API) so this is
203 // not a big loss.
204 %ignore gdcm::binary_write(std::ostream &,uint32_t const &);
205 %ignore gdcm::binary_write(std::ostream &,uint16_t const &);
206
207 %ignore gdcm::File::File();
208 %ignore gdcm::DicomDir::DicomDir();
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 // Warning: Order matters !
225 %include "gdcmCommon.h"
226 %include "gdcmBase.h"
227 %include "gdcmDictEntry.h"
228 %include "gdcmDict.h"
229 %include "gdcmDictSet.h"
230 %include "gdcmDocEntrySet.h"
231 %include "gdcmElementSet.h"
232 %include "gdcmSQItem.h"
233 %include "gdcmDicomDirElement.h"
234 %include "gdcmDicomDirObject.h"
235 %include "gdcmDicomDirImage.h"
236 %include "gdcmDicomDirSerie.h"
237 %include "gdcmDicomDirStudy.h"
238 %include "gdcmDicomDirPatient.h"
239 %include "gdcmDicomDirMeta.h"
240 %include "gdcmDocument.h"
241 %include "gdcmFile.h"
242 %include "gdcmSerieHelper.h"
243 %include "gdcmFile.h"
244 %include "gdcmUtil.h"
245 %include "gdcmGlobal.h"
246 %include "gdcmDicomDir.h"
247 %include "gdcmDocEntry.h"
248 %include "gdcmContentEntry.h"
249 %include "gdcmValEntry.h"
250 %include "gdcmBinEntry.h"
251 %include "gdcmSeqEntry.h"
252 %include "gdcmVR.h"
253 %include "gdcmTS.h"
254 %include "gdcmDictGroupName.h"
255
256 ////////////////////////////////////////////////////////////////////////////
257 // Notes on swig and this file gdcm.i:
258 //
259 /////////////////////////////////////
260 // Note 1: swig collision of method overloading and %typemap
261 // Consider the following junk.i file:
262 //     %module junk
263 //     %{
264 //     #include <string>
265 //     #include <iostream>
266 //     void Junk(std::string const & bozo) { std::cout << bozo << std::endl; }
267 //     void Junk() { std::cout << "Renamed Junk()" << std::endl; }
268 //     %}
269 //   
270 //     %typemap(python, in) std::string const &
271 //     {
272 //     $1 = new std::string( PyString_AsString( $input ) );
273 //     }
274 //     void Junk();
275 //     void Junk(std::string const & bozo);
276 //
277 // that we compile on linux with:
278 //    swig -c++ -python junk.i
279 //    g++ -g -I/usr/include/python2.3/ -o junk_wrap.o -c junk_wrap.cxx
280 //    g++ junk_wrap.o -shared -g -o _junk.so -L/usr/lib/python2.3/config \
281 //        -lpython2.3
282 // and invoque with:
283 //    python -c 'from junk import *; Junk("aaa") '
284 // then we get the following unexpected (for novice) python TypeError:
285 //    TypeError: No matching function for overloaded 'Junk'
286 //
287 // This happens because the swig generated code (at least for python) does
288 // the following two stage process:
289 //   1/ first do a dynamic dispatch ON THE NUMBER OF ARGUMENTS of the overloaded
290 //      Junk function (the same happens with method of course). [Note that the
291 //      dispatch is NOT done on the type of the arguments].
292 //   2/ second apply the typemap.
293 // When the first dynamic dispatch is executed, the swig generated code
294 // has no knowledge of the typemap, and thus expects a pointer to a std::string
295 // type i.e. an argument to Junk of the form _p_std__int<address>. But this
296 // is not what python handles to Junk ! An invocation of the form 'Junk("aaa")'
297 // will make Python pass a PyString to swig (and this is precisely why we
298 // wrote the typemap). And this will fail....
299 /////////////////////////////////////