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