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