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