]> Creatis software - gdcm.git/blob - gdcmPython/gdcm.i
* ENH : add callback and methods to get the progression of dicomDir
[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 "gdcmObject.h"
14 #include "gdcmDicomDir.h"
15 #include "gdcmDicomDirElement.h"
16 #include "gdcmMeta.h"
17 #include "gdcmPatient.h"
18 #include "gdcmStudy.h"
19 #include "gdcmSerie.h"
20 #include "gdcmImage.h"
21
22 using namespace std;
23
24 // Utility functions on strings for removing leading and trailing spaces
25 void EatLeadingAndTrailingSpaces(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 vtkPythonVoidFunc(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       cerr << "Caught a Ctrl-C within python, exiting program.\n";
51       Py_Exit(1);
52       }
53     PyErr_Print();
54     }
55 }
56
57 void vtkPythonVoidFuncArgDelete(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 // Global variables get exported to cvar in Python
72 %immutable;
73 extern gdcmGlobal gdcmGlob;
74 %mutable;
75
76 ////////////////////////////////////////////////////////////////////////////
77 %typemap(out) std::list<std::string> * {
78         PyObject* NewItem = (PyObject*)0;
79         PyObject* NewList = PyList_New(0); // The result of this typemap
80         for (list<string>::iterator NewString = ($1)->begin();
81              NewString != ($1)->end(); ++NewString) {
82                 NewItem = PyString_FromString(NewString->c_str());
83                 PyList_Append( NewList, NewItem);
84         }
85         $result = NewList;
86 }
87
88 ////////////////////////////////////////////////////////////////////////////
89 // Convert a c++ hash table in a python native dictionary
90 %typemap(out) std::map<std::string, std::list<std::string> > * {
91         PyObject* NewDict = PyDict_New(); // The result of this typemap
92         PyObject* NewKey = (PyObject*)0;
93         PyObject* NewVal = (PyObject*)0;
94
95         for (map<string, list<string> >::iterator tag = ($1)->begin();
96              tag != ($1)->end(); ++tag) {
97       string first = tag->first;
98       // Do not publish entries whose keys is made of spaces
99       if (first.length() == 0)
100          continue;
101                 NewKey = PyString_FromString(first.c_str());
102                 PyObject* NewList = PyList_New(0);
103                 for (list<string>::iterator Item = tag->second.begin();
104                      Item != tag->second.end(); ++Item) {
105                         NewVal = PyString_FromString(Item->c_str());
106                         PyList_Append( NewList, NewVal);
107                 }
108                 PyDict_SetItem( NewDict, NewKey, NewList);
109         }
110         $result = NewDict;
111 }
112
113 ////////////////////////////////////////////////////////////////////////////
114 // Convert a c++ hash table in a python native dictionary
115 %typemap(out) TagHeaderEntryHT & {
116         PyObject* NewDict = PyDict_New(); // The result of this typemap
117         string RawName;                   // Element name as gotten from gdcm
118         PyObject* NewKey = (PyObject*)0;  // Associated name as python object
119         string RawValue;                  // Element value as gotten from gdcm
120         PyObject* NewVal = (PyObject*)0;  // Associated value as python object
121
122         for (TagHeaderEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag) {
123
124                 // The element name shall be the key:
125                 RawName = tag->second->GetName();
126                 // gdcm unrecognized (including not loaded because their size exceeds
127                 // the user specified treshold) elements are exported with their
128                 // TagKey as key.
129                 if (RawName == "Unknown")
130                         RawName = tag->second->GetKey();
131                 NewKey = PyString_FromString(RawName.c_str());
132
133                 // Element values are striped from leading/trailing spaces
134                 RawValue = tag->second->GetValue();
135                 EatLeadingAndTrailingSpaces(RawValue);
136                 NewVal = PyString_FromString(RawValue.c_str());
137
138                 PyDict_SetItem( NewDict, NewKey, NewVal);
139     }
140         $result = NewDict;
141 }
142
143 %typemap(out) TagHeaderEntryHT {
144         PyObject* NewDict = PyDict_New(); // The result of this typemap
145         string RawName;                   // Element name as gotten from gdcm
146         PyObject* NewKey = (PyObject*)0;  // Associated name as python object
147         string RawValue;                  // Element value as gotten from gdcm
148         PyObject* NewVal = (PyObject*)0;  // Associated value as python object
149
150         for (TagHeaderEntryHT::iterator tag = $1.begin(); tag != $1.end(); ++tag) {
151
152                 // The element name shall be the key:
153                 RawName = tag->second->GetName();
154                 // gdcm unrecognized (including not loaded because their size exceeds
155                 // the user specified treshold) elements are exported with their
156                 // TagKey as key.
157                 if (RawName == "Unknown")
158                         RawName = tag->second->GetKey();
159                 NewKey = PyString_FromString(RawName.c_str());
160
161                 // Element values are striped from leading/trailing spaces
162                 RawValue = tag->second->GetValue();
163                 EatLeadingAndTrailingSpaces(RawValue);
164                 NewVal = PyString_FromString(RawValue.c_str());
165
166                 PyDict_SetItem( NewDict, NewKey, NewVal);
167     }
168         $result = NewDict;
169 }
170
171 ////////////////////////////////////////////////////////////////////////////
172 %typemap(out) ListPatient & {
173         PyObject* NewItem = (PyObject*)0;
174         $result = PyList_New(0); // The result of this typemap
175
176         for (list<gdcmPatient *>::iterator New = ($1)->begin();
177             New != ($1)->end(); ++New) {
178                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmPatient,1);
179                 PyList_Append($result, NewItem);
180         }
181 }
182
183 %typemap(out) ListStudy & {
184         PyObject* NewItem = (PyObject*)0;
185         $result = PyList_New(0); // The result of this typemap
186
187         for (list<gdcmStudy *>::iterator New = ($1)->begin();
188             New != ($1)->end(); ++New) {
189                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmStudy,1);
190                 PyList_Append($result, NewItem);
191         }
192 }
193
194 %typemap(out) ListSerie & {
195         PyObject* NewItem = (PyObject*)0;
196         $result = PyList_New(0); // The result of this typemap
197
198         for (list<gdcmSerie *>::iterator New = ($1)->begin();
199             New != ($1)->end(); ++New) {
200                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmSerie,1);
201                 PyList_Append($result, NewItem);
202         }
203 }
204
205 %typemap(out) ListImage & {
206         PyObject* NewItem = (PyObject*)0;
207         $result = PyList_New(0); // The result of this typemap
208
209         for (list<gdcmImage *>::iterator New = ($1)->begin();
210             New != ($1)->end(); ++New) {
211                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmImage,1);
212                 PyList_Append($result, NewItem);
213         }
214 }
215
216 ////////////////////////////////////////////////////////////////////////////
217 // Deals with function returning a C++ string.
218 %typemap(python, in) (gdcmMethod *method,void *arg) {
219     Py_INCREF($input);
220     $1=vtkPythonVoidFunc;
221         $2=$input;
222 }
223
224
225 ////////////////////////////////////////////////////////////////////////////
226 // Deals with function returning a C++ string.
227 %typemap(out) string, std::string  {
228     $result = PyString_FromString(($1).c_str());
229 }
230
231 %typemap(python, in) const std::string, std::string
232 {
233   $1 = PyString_AsString($input);
234 }
235
236 ////////////////////////////////////////////////////////////////////////////
237 %include "gdcmCommon.h"
238 %include "gdcmDictEntry.h"
239 %include "gdcmDict.h"
240 %include "gdcmDictSet.h"
241 %include "gdcmParser.h"
242 %include "gdcmHeaderEntry.h"
243 %include "gdcmHeader.h"
244 %include "gdcmHeaderHelper.h"
245 %include "gdcmFile.h"
246 %include "gdcmUtil.h"
247 %include "gdcmObject.h"
248 %include "gdcmDicomDir.h"
249 %include "gdcmDicomDirElement.h"
250 %include "gdcmMeta.h"
251 %include "gdcmPatient.h"
252 %include "gdcmStudy.h"
253 %include "gdcmSerie.h"
254 %include "gdcmImage.h"