]> Creatis software - gdcm.git/blob - gdcmPython/gdcm.i
* src/gdcmDocument.cxx ftell() return properly stored in a long (i.e.
[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 using namespace gdcm;
72 %}
73 typedef  unsigned short guint16;
74 typedef  unsigned int guint32;
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
81    for (std::list<std::string>::iterator NewString = ($1)->begin();
82         NewString != ($1)->end();
83         ++NewString)
84    {
85       NewItem = PyString_FromString(NewString->c_str());
86       PyList_Append( NewList, NewItem);
87    }
88    $result = NewList;
89 }
90
91 ////////////////////////////////////////////////////////////////////////////
92 // Convert a c++ hash table in a python native dictionary
93 %typemap(out) std::map<std::string, std::list<std::string> > * {
94    PyObject* NewDict = PyDict_New(); // The result of this typemap
95    PyObject* NewKey = (PyObject*)0;
96    PyObject* NewVal = (PyObject*)0;
97
98    for (std::map<std::string,
99         std::list<std::string> >::iterator tag = ($1)->begin();
100         tag != ($1)->end(); ++tag)
101    {
102       std::string first = tag->first;
103       // Do not publish entries whose keys is made of spaces
104       if (first.length() == 0)
105          continue;
106       NewKey = PyString_FromString(first.c_str());
107       PyObject* NewList = PyList_New(0);
108       for (std::list<std::string>::iterator Item = tag->second.begin();
109            Item != tag->second.end();
110            ++Item)
111       {
112          NewVal = PyString_FromString(Item->c_str());
113          PyList_Append( NewList, NewVal);
114       }
115       PyDict_SetItem( NewDict, NewKey, NewList);
116    }
117    $result = NewDict;
118 }
119
120 ////////////////////////////////////////////////////////////////////////////
121 // Convert a c++ hash table in a python native dictionary
122 %typemap(out) gdcm::TagDocEntryHT & {
123    PyObject* NewDict = PyDict_New(); // The result of this typemap
124    std::string RawName;              // Element name as gotten from gdcm
125    PyObject* NewKey = (PyObject*)0;  // Associated name as python object
126    std::string RawValue;             // Element value as gotten from gdcm
127    PyObject* NewVal = (PyObject*)0;  // Associated value as python object
128
129    for (gdcm::TagDocEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag)
130    {
131       // The element name shall be the key:
132       RawName = tag->second->GetName();
133       // gdcm unrecognized (including not loaded because their size exceeds
134       // the user specified treshold) elements are exported with their
135       // TagKey as key.
136       if (RawName == "Unknown")
137          RawName = tag->second->GetKey();
138       NewKey = PyString_FromString(RawName.c_str());
139
140       // Element values are striped from leading/trailing spaces
141       if (gdcm::ValEntry* ValEntryPtr =
142                 dynamic_cast< gdcm::ValEntry* >(tag->second) )
143       {
144          RawValue = ValEntryPtr->GetValue();
145       }
146       else
147         continue; 
148       EatLeadingAndTrailingSpaces(RawValue);
149       NewVal = PyString_FromString(RawValue.c_str());
150       PyDict_SetItem( NewDict, NewKey, NewVal);
151    }
152    $result = NewDict;
153 }
154
155 ////////////////////////////////////////////////////////////////////////////
156 %typemap(out) ListDicomDirPatient & {
157         PyObject* NewItem = (PyObject*)0;
158         $result = PyList_New(0); // The result of this typemap
159
160         for (std::list<gdcm::DicomDirPatient *>::iterator New = ($1)->begin();
161             New != ($1)->end(); ++New) {
162                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirPatient,1);
163                 PyList_Append($result, NewItem);
164         }
165 }
166
167 %typemap(out) ListDicomDirStudy & {
168         PyObject* NewItem = (PyObject*)0;
169         $result = PyList_New(0); // The result of this typemap
170
171         for (std::list<gdcm::DicomDirStudy *>::iterator New = ($1)->begin();
172             New != ($1)->end(); ++New) {
173                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirStudy,1);
174                 PyList_Append($result, NewItem);
175         }
176 }
177
178 %typemap(out) ListDicomDirSerie & {
179         PyObject* NewItem = (PyObject*)0;
180         $result = PyList_New(0); // The result of this typemap
181
182         for (std::list<gdcm::DicomDirSerie *>::iterator New = ($1)->begin();
183             New != ($1)->end(); ++New) {
184                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirSerie,1);
185                 PyList_Append($result, NewItem);
186         }
187 }
188
189 %typemap(out) ListDicomDirImage & {
190         PyObject* NewItem = (PyObject*)0;
191         $result = PyList_New(0); // The result of this typemap
192
193         for (std::list<gdcm::DicomDirImage *>::iterator New = ($1)->begin();
194             New != ($1)->end(); ++New) {
195                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirImage,1);
196                 PyList_Append($result, NewItem);
197         }
198 }
199
200 ////////////////////////////////////////////////////////////////////////////
201 // Deals with function returning a C++ string.
202 %typemap(python, in) (gdcm::Method *,void * =NULL,gdcm::Method * =NULL) {
203         if($input!=Py_None)
204         {
205                 Py_INCREF($input);
206                 $1=gdcmPythonVoidFunc;
207                 $2=$input;
208                 $3=gdcmPythonVoidFuncArgDelete;
209         }
210         else
211         {
212                 $1=NULL;
213                 $2=NULL;
214                 $3=NULL;
215         }
216 }
217
218
219 ////////////////////////////////////////////////////////////////////////////
220 // Deals with function returning a C++ string.
221 %typemap(out) string, std::string  {
222     $result = PyString_FromString(($1).c_str());
223 }
224
225 %typemap(python, in) const std::string, std::string
226 {
227   $1 = PyString_AsString($input);
228 }
229
230 ////////////////////////////////////////////////////////////////////////////
231 // Warning: Order matters !
232 %include "gdcmCommon.h"
233 %include "gdcmRLEFramesInfo.h"
234 %include "gdcmJPEGFragmentsInfo.h"
235 %include "gdcmDictEntry.h"
236 %include "gdcmDict.h"
237 %include "gdcmDocEntry.h"
238 %include "gdcmDocEntrySet.h"
239 %include "gdcmElementSet.h"
240 %include "gdcmDictSet.h"
241 %include "gdcmTS.h"
242 %include "gdcmVR.h"
243 %include "gdcmSQItem.h"
244 %include "gdcmDicomDirElement.h"
245 %include "gdcmDicomDirObject.h"
246 %include "gdcmDicomDirImage.h"
247 %include "gdcmDicomDirSerie.h"
248 %include "gdcmDicomDirStudy.h"
249 %include "gdcmDicomDirPatient.h"
250 %include "gdcmDicomDirMeta.h"
251 %include "gdcmDocument.h"
252 %include "gdcmHeader.h"
253 %include "gdcmHeaderHelper.h"
254 %include "gdcmFile.h"
255 %include "gdcmUtil.h"
256 %include "gdcmGlobal.h"
257 %include "gdcmDicomDir.h"