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