3 #include "gdcmCommon.h"
4 #include "gdcmDictEntry.h"
6 #include "gdcmDictSet.h"
7 #include "gdcmParser.h"
8 #include "gdcmHeaderEntry.h"
9 #include "gdcmHeader.h"
10 #include "gdcmHeaderHelper.h"
13 #include "gdcmGlobal.h"
14 #include "gdcmObject.h"
15 #include "gdcmDicomDir.h"
16 #include "gdcmDicomDirElement.h"
17 #include "gdcmDicomDirMeta.h"
18 #include "gdcmDicomDirPatient.h"
19 #include "gdcmDicomDirStudy.h"
20 #include "gdcmDicomDirSerie.h"
21 #include "gdcmDicomDirImage.h"
25 ////////////////////////////////////////////////////////////////////////////
26 // Utility functions on strings for removing leading and trailing spaces
27 void EatLeadingAndTrailingSpaces(string & s) {
28 while ( s.length() && (s[0] == ' ') )
30 while ( s.length() && (s[s.length()-1] == ' ') )
31 s.erase(s.length()-1, 1);
34 void gdcmPythonVoidFunc(void *arg)
36 PyObject *arglist, *result;
37 PyObject *func = (PyObject *)arg;
39 arglist = Py_BuildValue("()");
41 result = PyEval_CallObject(func, arglist);
50 if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
52 cerr << "Caught a Ctrl-C within python, exiting program.\n";
59 void gdcmPythonVoidFuncArgDelete(void *arg)
61 PyObject *func = (PyObject *)arg;
69 typedef unsigned short guint16;
70 typedef unsigned int guint32;
72 ////////////////////////////////////////////////////////////////////////////
73 // Global variables get exported to cvar in Python
75 extern gdcmGlobal gdcmGlob;
78 ////////////////////////////////////////////////////////////////////////////
79 %typemap(out) std::list<std::string> * {
80 PyObject* NewItem = (PyObject*)0;
81 PyObject* NewList = PyList_New(0); // The result of this typemap
82 for (list<string>::iterator NewString = ($1)->begin();
83 NewString != ($1)->end(); ++NewString) {
84 NewItem = PyString_FromString(NewString->c_str());
85 PyList_Append( NewList, NewItem);
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;
97 for (map<string, list<string> >::iterator tag = ($1)->begin();
98 tag != ($1)->end(); ++tag) {
99 string first = tag->first;
100 // Do not publish entries whose keys is made of spaces
101 if (first.length() == 0)
103 NewKey = PyString_FromString(first.c_str());
104 PyObject* NewList = PyList_New(0);
105 for (list<string>::iterator Item = tag->second.begin();
106 Item != tag->second.end(); ++Item) {
107 NewVal = PyString_FromString(Item->c_str());
108 PyList_Append( NewList, NewVal);
110 PyDict_SetItem( NewDict, NewKey, NewList);
115 ////////////////////////////////////////////////////////////////////////////
116 // Convert a c++ hash table in a python native dictionary
117 %typemap(out) TagHeaderEntryHT & {
118 PyObject* NewDict = PyDict_New(); // The result of this typemap
119 string RawName; // Element name as gotten from gdcm
120 PyObject* NewKey = (PyObject*)0; // Associated name as python object
121 string RawValue; // Element value as gotten from gdcm
122 PyObject* NewVal = (PyObject*)0; // Associated value as python object
124 for (TagHeaderEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag) {
126 // The element name shall be the key:
127 RawName = tag->second->GetName();
128 // gdcm unrecognized (including not loaded because their size exceeds
129 // the user specified treshold) elements are exported with their
131 if (RawName == "Unknown")
132 RawName = tag->second->GetKey();
133 NewKey = PyString_FromString(RawName.c_str());
135 // Element values are striped from leading/trailing spaces
136 RawValue = tag->second->GetValue();
137 EatLeadingAndTrailingSpaces(RawValue);
138 NewVal = PyString_FromString(RawValue.c_str());
140 PyDict_SetItem( NewDict, NewKey, NewVal);
145 %typemap(out) TagHeaderEntryHT {
146 PyObject* NewDict = PyDict_New(); // The result of this typemap
147 string RawName; // Element name as gotten from gdcm
148 PyObject* NewKey = (PyObject*)0; // Associated name as python object
149 string RawValue; // Element value as gotten from gdcm
150 PyObject* NewVal = (PyObject*)0; // Associated value as python object
152 for (TagHeaderEntryHT::iterator tag = $1.begin(); tag != $1.end(); ++tag) {
154 // The element name shall be the key:
155 RawName = tag->second->GetName();
156 // gdcm unrecognized (including not loaded because their size exceeds
157 // the user specified treshold) elements are exported with their
159 if (RawName == "Unknown")
160 RawName = tag->second->GetKey();
161 NewKey = PyString_FromString(RawName.c_str());
163 // Element values are striped from leading/trailing spaces
164 RawValue = tag->second->GetValue();
165 EatLeadingAndTrailingSpaces(RawValue);
166 NewVal = PyString_FromString(RawValue.c_str());
168 PyDict_SetItem( NewDict, NewKey, NewVal);
173 ////////////////////////////////////////////////////////////////////////////
174 %typemap(out) ListDicomDirPatient & {
175 PyObject* NewItem = (PyObject*)0;
176 $result = PyList_New(0); // The result of this typemap
178 for (list<gdcmDicomDirPatient *>::iterator New = ($1)->begin();
179 New != ($1)->end(); ++New) {
180 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirPatient,1);
181 PyList_Append($result, NewItem);
185 %typemap(out) ListDicomDirStudy & {
186 PyObject* NewItem = (PyObject*)0;
187 $result = PyList_New(0); // The result of this typemap
189 for (list<gdcmDicomDirStudy *>::iterator New = ($1)->begin();
190 New != ($1)->end(); ++New) {
191 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirStudy,1);
192 PyList_Append($result, NewItem);
196 %typemap(out) ListDicomDirSerie & {
197 PyObject* NewItem = (PyObject*)0;
198 $result = PyList_New(0); // The result of this typemap
200 for (list<gdcmDicomDirSerie *>::iterator New = ($1)->begin();
201 New != ($1)->end(); ++New) {
202 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirSerie,1);
203 PyList_Append($result, NewItem);
207 %typemap(out) ListDicomDirImage & {
208 PyObject* NewItem = (PyObject*)0;
209 $result = PyList_New(0); // The result of this typemap
211 for (list<gdcmDicomDirImage *>::iterator New = ($1)->begin();
212 New != ($1)->end(); ++New) {
213 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirImage,1);
214 PyList_Append($result, NewItem);
218 ////////////////////////////////////////////////////////////////////////////
219 // Deals with function returning a C++ string.
220 %typemap(python, in) (gdcmMethod *,void * =NULL,gdcmMethod * =NULL) {
224 $1=gdcmPythonVoidFunc;
226 $3=gdcmPythonVoidFuncArgDelete;
237 ////////////////////////////////////////////////////////////////////////////
238 // Deals with function returning a C++ string.
239 %typemap(out) string, std::string {
240 $result = PyString_FromString(($1).c_str());
243 %typemap(python, in) const std::string, std::string
245 $1 = PyString_AsString($input);
248 ////////////////////////////////////////////////////////////////////////////
249 %include "gdcmCommon.h"
250 %include "gdcmDictEntry.h"
251 %include "gdcmDict.h"
252 %include "gdcmDictSet.h"
253 %include "gdcmParser.h"
254 %include "gdcmHeaderEntry.h"
255 %include "gdcmHeader.h"
256 %include "gdcmHeaderHelper.h"
257 %include "gdcmFile.h"
258 %include "gdcmUtil.h"
259 %include "gdcmGlobal.h"
260 %include "gdcmObject.h"
261 %include "gdcmDicomDir.h"
262 %include "gdcmDicomDirElement.h"
263 %include "gdcmDicomDirMeta.h"
264 %include "gdcmDicomDirPatient.h"
265 %include "gdcmDicomDirStudy.h"
266 %include "gdcmDicomDirSerie.h"
267 %include "gdcmDicomDirImage.h"