]> Creatis software - gdcm.git/blob - gdcmPython/gdcm.i
BUG: Hopefully deals with cygwin/apple/vc++ library naming convention... sad that...
[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 // 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 (std::list<std::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 (std::map<std::string, std::list<std::string> >::iterator tag = ($1)->begin();
96              tag != ($1)->end(); ++tag) {
97       std::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 (std::list<std::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         std::string RawName;                   // Element name as gotten from gdcm
118         PyObject* NewKey = (PyObject*)0;  // Associated name as python object
119         std::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         std::string RawName;                   // Element name as gotten from gdcm
146         PyObject* NewKey = (PyObject*)0;  // Associated name as python object
147         std::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) ListDicomDirPatient & {
173         PyObject* NewItem = (PyObject*)0;
174         $result = PyList_New(0); // The result of this typemap
175
176         for (std::list<gdcmDicomDirPatient *>::iterator New = ($1)->begin();
177             New != ($1)->end(); ++New) {
178                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirPatient,1);
179                 PyList_Append($result, NewItem);
180         }
181 }
182
183 %typemap(out) ListDicomDirStudy & {
184         PyObject* NewItem = (PyObject*)0;
185         $result = PyList_New(0); // The result of this typemap
186
187         for (std::list<gdcmDicomDirStudy *>::iterator New = ($1)->begin();
188             New != ($1)->end(); ++New) {
189                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirStudy,1);
190                 PyList_Append($result, NewItem);
191         }
192 }
193
194 %typemap(out) ListDicomDirSerie & {
195         PyObject* NewItem = (PyObject*)0;
196         $result = PyList_New(0); // The result of this typemap
197
198         for (std::list<gdcmDicomDirSerie *>::iterator New = ($1)->begin();
199             New != ($1)->end(); ++New) {
200                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirSerie,1);
201                 PyList_Append($result, NewItem);
202         }
203 }
204
205 %typemap(out) ListDicomDirImage & {
206         PyObject* NewItem = (PyObject*)0;
207         $result = PyList_New(0); // The result of this typemap
208
209         for (std::list<gdcmDicomDirImage *>::iterator New = ($1)->begin();
210             New != ($1)->end(); ++New) {
211                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirImage,1);
212                 PyList_Append($result, NewItem);
213         }
214 }
215
216 ////////////////////////////////////////////////////////////////////////////
217 // Deals with function returning a C++ string.
218 %typemap(python, in) (gdcmMethod *,void * =NULL,gdcmMethod * =NULL) {
219         if($input!=Py_None)
220         {
221                 Py_INCREF($input);
222                 $1=gdcmPythonVoidFunc;
223                 $2=$input;
224                 $3=gdcmPythonVoidFuncArgDelete;
225         }
226         else
227         {
228                 $1=NULL;
229                 $2=NULL;
230                 $3=NULL;
231         }
232 }
233
234
235 ////////////////////////////////////////////////////////////////////////////
236 // Deals with function returning a C++ string.
237 %typemap(out) string, std::string  {
238     $result = PyString_FromString(($1).c_str());
239 }
240
241 %typemap(python, in) const std::string, std::string
242 {
243   $1 = PyString_AsString($input);
244 }
245
246 ////////////////////////////////////////////////////////////////////////////
247 %include "gdcmCommon.h"
248 %include "gdcmDictEntry.h"
249 %include "gdcmDict.h"
250 %include "gdcmDictSet.h"
251 %include "gdcmParser.h"
252 %include "gdcmHeaderEntry.h"
253 %include "gdcmHeader.h"
254 %include "gdcmHeaderHelper.h"
255 %include "gdcmFile.h"
256 %include "gdcmUtil.h"
257 %include "gdcmGlobal.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"