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