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