]> Creatis software - gdcm.git/blob - gdcmPython/gdcm.i
* src/gdcmValEntry.h: member voidArea type changed from char* to void*.
[gdcm.git] / gdcmPython / gdcm.i
1 %module gdcm
2 %{
3 #include "gdcmCommon.h"
4 #include "gdcmDictEntry.h"
5 #include "gdcmDict.h"
6 #include "gdcmDictSet.h"
7 #include "gdcmDocEntrySet.h"
8 #include "gdcmSQItem.h"
9 #include "gdcmDocument.h"
10 #include "gdcmElementSet.h"
11 #include "gdcmHeader.h"
12 #include "gdcmHeaderHelper.h"
13 #include "gdcmFile.h"
14 #include "gdcmUtil.h"
15 #include "gdcmGlobal.h"
16 #include "gdcmObject.h"
17 #include "gdcmDicomDir.h"
18 #include "gdcmDicomDirElement.h"
19 #include "gdcmDicomDirMeta.h"
20 #include "gdcmDicomDirPatient.h"
21 #include "gdcmDicomDirStudy.h"
22 #include "gdcmDicomDirSerie.h"
23 #include "gdcmDicomDirImage.h"
24 #include "gdcmValEntry.h"
25
26 ////////////////////////////////////////////////////////////////////////////
27 // Utility functions on strings for removing leading and trailing spaces
28 void EatLeadingAndTrailingSpaces(std::string & s) {
29         while ( s.length() && (s[0] == ' ') )
30                 s.erase(0,1);
31         while ( s.length() && (s[s.length()-1] == ' ') )
32                 s.erase(s.length()-1, 1);
33 }
34
35 void gdcmPythonVoidFunc(void *arg)
36 {
37   PyObject *arglist, *result;
38   PyObject *func = (PyObject *)arg;
39
40   arglist = Py_BuildValue("()");
41
42   result = PyEval_CallObject(func, arglist);
43   Py_DECREF(arglist);
44
45   if (result)
46     {
47     Py_XDECREF(result);
48     }
49   else
50     {
51     if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
52       {
53       std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
54       Py_Exit(1);
55       }
56     PyErr_Print();
57     }
58 }
59
60 void gdcmPythonVoidFuncArgDelete(void *arg)
61 {
62   PyObject *func = (PyObject *)arg;
63   if (func)
64     {
65     Py_DECREF(func);
66     }
67 }
68
69 %}
70 typedef  unsigned short guint16;
71 typedef  unsigned int guint32;
72
73 ////////////////////////////////////////////////////////////////////////////
74 %typemap(out) std::list<std::string> * {
75         PyObject* NewItem = (PyObject*)0;
76         PyObject* NewList = PyList_New(0); // The result of this typemap
77         for (std::list<std::string>::iterator NewString = ($1)->begin();
78              NewString != ($1)->end(); ++NewString) {
79                 NewItem = PyString_FromString(NewString->c_str());
80                 PyList_Append( NewList, NewItem);
81         }
82         $result = NewList;
83 }
84
85 ////////////////////////////////////////////////////////////////////////////
86 // Convert a c++ hash table in a python native dictionary
87 %typemap(out) std::map<std::string, std::list<std::string> > * {
88         PyObject* NewDict = PyDict_New(); // The result of this typemap
89         PyObject* NewKey = (PyObject*)0;
90         PyObject* NewVal = (PyObject*)0;
91
92         for (std::map<std::string, std::list<std::string> >::iterator tag = ($1)->begin();
93              tag != ($1)->end(); ++tag) {
94       std::string first = tag->first;
95       // Do not publish entries whose keys is made of spaces
96       if (first.length() == 0)
97          continue;
98                 NewKey = PyString_FromString(first.c_str());
99                 PyObject* NewList = PyList_New(0);
100                 for (std::list<std::string>::iterator Item = tag->second.begin();
101                      Item != tag->second.end(); ++Item) {
102                         NewVal = PyString_FromString(Item->c_str());
103                         PyList_Append( NewList, NewVal);
104                 }
105                 PyDict_SetItem( NewDict, NewKey, NewList);
106         }
107         $result = NewDict;
108 }
109
110 ////////////////////////////////////////////////////////////////////////////
111 // Convert a c++ hash table in a python native dictionary
112 %typemap(out) TagDocEntryHT & {
113         PyObject* NewDict = PyDict_New(); // The result of this typemap
114         std::string RawName;                   // Element name as gotten from gdcm
115         PyObject* NewKey = (PyObject*)0;  // Associated name as python object
116         std::string RawValue;                  // Element value as gotten from gdcm
117         PyObject* NewVal = (PyObject*)0;  // Associated value as python object
118
119         for (TagDocEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag) {
120
121                 // The element name shall be the key:
122                 RawName = tag->second->GetName();
123                 // gdcm unrecognized (including not loaded because their size exceeds
124                 // the user specified treshold) elements are exported with their
125                 // TagKey as key.
126                 if (RawName == "Unknown")
127                         RawName = tag->second->GetKey();
128                 NewKey = PyString_FromString(RawName.c_str());
129
130       // Element values are striped from leading/trailing spaces
131       // Element values are striped from leading/trailing spaces
132       if (gdcmValEntry* ValEntryPtr =
133                 dynamic_cast< gdcmValEntry* >(tag->second) )
134       {
135          RawValue = ValEntryPtr->GetValue();
136       }
137       else
138         continue; 
139       EatLeadingAndTrailingSpaces(RawValue);
140       NewVal = PyString_FromString(RawValue.c_str());
141
142       PyDict_SetItem( NewDict, NewKey, NewVal);
143    }
144    $result = NewDict;
145 }
146
147 /*
148 CLEAN ME FIXME CLEANME TODO
149 %typemap(out) TagDocEntryHT {
150         PyObject* NewDict = PyDict_New(); // The result of this typemap
151         std::string RawName;              // Element name as gotten from gdcm
152         PyObject* NewKey = (PyObject*)0;  // Associated name as python object
153         std::string RawValue;             // Element value as gotten from gdcm
154         PyObject* NewVal = (PyObject*)0;  // Associated value as python object
155
156         for (TagDocEntryHT::iterator tag = $1.begin(); tag != $1.end(); ++tag) {
157
158                 // The element name shall be the key:
159                 RawName = tag->second->GetName();
160                 // gdcm unrecognized (including not loaded because their size exceeds
161                 // the user specified treshold) elements are exported with their
162                 // TagKey as key.
163                 if (RawName == "Unknown")
164                         RawName = tag->second->GetKey();
165                 NewKey = PyString_FromString(RawName.c_str());
166
167       // Element values are striped from leading/trailing spaces
168       if (gdcmValEntry* ValEntryPtr =
169                 dynamic_cast< gdcmValEntry* >(tag->second) )
170       {
171          RawValue = ValEntryPtr->GetValue();
172       } 
173       else
174         continue;
175                 EatLeadingAndTrailingSpaces(RawValue);
176                 NewVal = PyString_FromString(RawValue.c_str());
177
178                 PyDict_SetItem( NewDict, NewKey, NewVal);
179     }
180         $result = NewDict;
181 }
182 */
183
184 ////////////////////////////////////////////////////////////////////////////
185 %typemap(out) ListDicomDirPatient & {
186         PyObject* NewItem = (PyObject*)0;
187         $result = PyList_New(0); // The result of this typemap
188
189         for (std::list<gdcmDicomDirPatient *>::iterator New = ($1)->begin();
190             New != ($1)->end(); ++New) {
191                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirPatient,1);
192                 PyList_Append($result, NewItem);
193         }
194 }
195
196 %typemap(out) ListDicomDirStudy & {
197         PyObject* NewItem = (PyObject*)0;
198         $result = PyList_New(0); // The result of this typemap
199
200         for (std::list<gdcmDicomDirStudy *>::iterator New = ($1)->begin();
201             New != ($1)->end(); ++New) {
202                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirStudy,1);
203                 PyList_Append($result, NewItem);
204         }
205 }
206
207 %typemap(out) ListDicomDirSerie & {
208         PyObject* NewItem = (PyObject*)0;
209         $result = PyList_New(0); // The result of this typemap
210
211         for (std::list<gdcmDicomDirSerie *>::iterator New = ($1)->begin();
212             New != ($1)->end(); ++New) {
213                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirSerie,1);
214                 PyList_Append($result, NewItem);
215         }
216 }
217
218 %typemap(out) ListDicomDirImage & {
219         PyObject* NewItem = (PyObject*)0;
220         $result = PyList_New(0); // The result of this typemap
221
222         for (std::list<gdcmDicomDirImage *>::iterator New = ($1)->begin();
223             New != ($1)->end(); ++New) {
224                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirImage,1);
225                 PyList_Append($result, NewItem);
226         }
227 }
228
229 ////////////////////////////////////////////////////////////////////////////
230 // Deals with function returning a C++ string.
231 %typemap(python, in) (gdcmMethod *,void * =NULL,gdcmMethod * =NULL) {
232         if($input!=Py_None)
233         {
234                 Py_INCREF($input);
235                 $1=gdcmPythonVoidFunc;
236                 $2=$input;
237                 $3=gdcmPythonVoidFuncArgDelete;
238         }
239         else
240         {
241                 $1=NULL;
242                 $2=NULL;
243                 $3=NULL;
244         }
245 }
246
247
248 ////////////////////////////////////////////////////////////////////////////
249 // Deals with function returning a C++ string.
250 %typemap(out) string, std::string  {
251     $result = PyString_FromString(($1).c_str());
252 }
253
254 %typemap(python, in) const std::string, std::string
255 {
256   $1 = PyString_AsString($input);
257 }
258
259 ////////////////////////////////////////////////////////////////////////////
260 // Warning: Order matters !
261 %include "gdcmCommon.h"
262 %include "gdcmDictEntry.h"
263 %include "gdcmDict.h"
264 %include "gdcmDictSet.h"
265 %include "gdcmDocEntrySet.h"
266 %include "gdcmElementSet.h"
267 %include "gdcmDocument.h"
268 %include "gdcmSQItem.h"
269 %include "gdcmHeader.h"
270 %include "gdcmHeaderHelper.h"
271 %include "gdcmFile.h"
272 %include "gdcmUtil.h"
273 %include "gdcmGlobal.h"
274 %include "gdcmObject.h"
275 %include "gdcmDicomDir.h"
276 %include "gdcmDicomDirElement.h"
277 %include "gdcmDicomDirMeta.h"
278 %include "gdcmDicomDirPatient.h"
279 %include "gdcmDicomDirStudy.h"
280 %include "gdcmDicomDirSerie.h"
281 %include "gdcmDicomDirImage.h"