]> Creatis software - gdcm.git/blob - gdcmPython/gdcm.i
* FIX : bug fix in the gdcmDirList for the recursivity in directories
[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 "gdcmMeta.h"
17 #include "gdcmPatient.h"
18 #include "gdcmStudy.h"
19 #include "gdcmSerie.h"
20 #include "gdcmImage.h"
21
22 using namespace std;
23
24 // Utility functions on strings for removing leading and trailing spaces
25 void EatLeadingAndTrailingSpaces(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 typedef  unsigned short guint16;
33 typedef  unsigned int guint32;
34
35 ////////////////////////////////////////////////////////////////////////////
36 // Global variables get exported to cvar in Python
37 %immutable;
38 extern gdcmGlobal gdcmGlob;
39 %mutable;
40
41 ////////////////////////////////////////////////////////////////////////////
42 %typemap(out) std::list<std::string> * {
43         PyObject* NewItem = (PyObject*)0;
44         PyObject* NewList = PyList_New(0); // The result of this typemap
45         for (list<string>::iterator NewString = ($1)->begin();
46              NewString != ($1)->end(); ++NewString) {
47                 NewItem = PyString_FromString(NewString->c_str());
48                 PyList_Append( NewList, NewItem);
49         }
50         $result = NewList;
51 }
52
53 ////////////////////////////////////////////////////////////////////////////
54 // Convert a c++ hash table in a python native dictionary
55 %typemap(out) std::map<std::string, std::list<std::string> > * {
56         PyObject* NewDict = PyDict_New(); // The result of this typemap
57         PyObject* NewKey = (PyObject*)0;
58         PyObject* NewVal = (PyObject*)0;
59
60         for (map<string, list<string> >::iterator tag = ($1)->begin();
61              tag != ($1)->end(); ++tag) {
62       string first = tag->first;
63       // Do not publish entries whose keys is made of spaces
64       if (first.length() == 0)
65          continue;
66                 NewKey = PyString_FromString(first.c_str());
67                 PyObject* NewList = PyList_New(0);
68                 for (list<string>::iterator Item = tag->second.begin();
69                      Item != tag->second.end(); ++Item) {
70                         NewVal = PyString_FromString(Item->c_str());
71                         PyList_Append( NewList, NewVal);
72                 }
73                 PyDict_SetItem( NewDict, NewKey, NewList);
74         }
75         $result = NewDict;
76 }
77
78 ////////////////////////////////////////////////////////////////////////////
79 // Convert a c++ hash table in a python native dictionary
80 %typemap(out) TagHeaderEntryHT & {
81         PyObject* NewDict = PyDict_New(); // The result of this typemap
82         string RawName;                   // Element name as gotten from gdcm
83         PyObject* NewKey = (PyObject*)0;  // Associated name as python object
84         string RawValue;                  // Element value as gotten from gdcm
85         PyObject* NewVal = (PyObject*)0;  // Associated value as python object
86
87         for (TagHeaderEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag) {
88
89                 // The element name shall be the key:
90                 RawName = tag->second->GetName();
91                 // gdcm unrecognized (including not loaded because their size exceeds
92                 // the user specified treshold) elements are exported with their
93                 // TagKey as key.
94                 if (RawName == "Unknown")
95                         RawName = tag->second->GetKey();
96                 NewKey = PyString_FromString(RawName.c_str());
97
98                 // Element values are striped from leading/trailing spaces
99                 RawValue = tag->second->GetValue();
100                 EatLeadingAndTrailingSpaces(RawValue);
101                 NewVal = PyString_FromString(RawValue.c_str());
102
103                 PyDict_SetItem( NewDict, NewKey, NewVal);
104     }
105         $result = NewDict;
106 }
107
108 %typemap(out) TagHeaderEntryHT {
109         PyObject* NewDict = PyDict_New(); // The result of this typemap
110         string RawName;                   // Element name as gotten from gdcm
111         PyObject* NewKey = (PyObject*)0;  // Associated name as python object
112         string RawValue;                  // Element value as gotten from gdcm
113         PyObject* NewVal = (PyObject*)0;  // Associated value as python object
114
115         for (TagHeaderEntryHT::iterator tag = $1.begin(); tag != $1.end(); ++tag) {
116
117                 // The element name shall be the key:
118                 RawName = tag->second->GetName();
119                 // gdcm unrecognized (including not loaded because their size exceeds
120                 // the user specified treshold) elements are exported with their
121                 // TagKey as key.
122                 if (RawName == "Unknown")
123                         RawName = tag->second->GetKey();
124                 NewKey = PyString_FromString(RawName.c_str());
125
126                 // Element values are striped from leading/trailing spaces
127                 RawValue = tag->second->GetValue();
128                 EatLeadingAndTrailingSpaces(RawValue);
129                 NewVal = PyString_FromString(RawValue.c_str());
130
131                 PyDict_SetItem( NewDict, NewKey, NewVal);
132     }
133         $result = NewDict;
134 }
135
136 ////////////////////////////////////////////////////////////////////////////
137 %typemap(out) ListPatient & {
138         PyObject* NewItem = (PyObject*)0;
139         $result = PyList_New(0); // The result of this typemap
140
141         for (list<gdcmPatient *>::iterator New = ($1)->begin();
142             New != ($1)->end(); ++New) {
143                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmPatient,1);
144                 PyList_Append($result, NewItem);
145         }
146 }
147
148 %typemap(out) ListStudy & {
149         PyObject* NewItem = (PyObject*)0;
150         $result = PyList_New(0); // The result of this typemap
151
152         for (list<gdcmStudy *>::iterator New = ($1)->begin();
153             New != ($1)->end(); ++New) {
154                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmStudy,1);
155                 PyList_Append($result, NewItem);
156         }
157 }
158
159 %typemap(out) ListSerie & {
160         PyObject* NewItem = (PyObject*)0;
161         $result = PyList_New(0); // The result of this typemap
162
163         for (list<gdcmSerie *>::iterator New = ($1)->begin();
164             New != ($1)->end(); ++New) {
165                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmSerie,1);
166                 PyList_Append($result, NewItem);
167         }
168 }
169
170 %typemap(out) ListImage & {
171         PyObject* NewItem = (PyObject*)0;
172         $result = PyList_New(0); // The result of this typemap
173
174         for (list<gdcmImage *>::iterator New = ($1)->begin();
175             New != ($1)->end(); ++New) {
176                 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmImage,1);
177                 PyList_Append($result, NewItem);
178         }
179 }
180
181 ////////////////////////////////////////////////////////////////////////////
182
183 ////////////////////////////////////////////////////////////////////////////
184 // Deals with function returning a C++ string.
185 %typemap(out) string, std::string  {
186     $result = PyString_FromString(($1).c_str());
187 }
188
189 %typemap(python, in) const std::string, std::string
190 {
191   $1 = PyString_AsString($input);
192 }
193
194 ////////////////////////////////////////////////////////////////////////////
195 %include "gdcmCommon.h"
196 %include "gdcmDictEntry.h"
197 %include "gdcmDict.h"
198 %include "gdcmDictSet.h"
199 %include "gdcmParser.h"
200 %include "gdcmHeaderEntry.h"
201 %include "gdcmHeader.h"
202 %include "gdcmHeaderHelper.h"
203 %include "gdcmFile.h"
204 %include "gdcmUtil.h"
205 %include "gdcmObject.h"
206 %include "gdcmDicomDir.h"
207 %include "gdcmDicomDirElement.h"
208 %include "gdcmMeta.h"
209 %include "gdcmPatient.h"
210 %include "gdcmStudy.h"
211 %include "gdcmSerie.h"
212 %include "gdcmImage.h"