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 "gdcmObject.h"
14 #include "gdcmDicomDir.h"
15 #include "gdcmDicomDirElement.h"
17 #include "gdcmPatient.h"
18 #include "gdcmStudy.h"
19 #include "gdcmSerie.h"
20 #include "gdcmImage.h"
24 // Utility functions on strings for removing leading and trailing spaces
25 void EatLeadingAndTrailingSpaces(string & s) {
26 while ( s.length() && (s[0] == ' ') )
28 while ( s.length() && (s[s.length()-1] == ' ') )
29 s.erase(s.length()-1, 1);
32 void vtkPythonVoidFunc(void *arg)
34 PyObject *arglist, *result;
35 PyObject *func = (PyObject *)arg;
37 arglist = Py_BuildValue("()");
39 result = PyEval_CallObject(func, arglist);
48 if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
50 cerr << "Caught a Ctrl-C within python, exiting program.\n";
57 void vtkPythonVoidFuncArgDelete(void *arg)
59 PyObject *func = (PyObject *)arg;
67 typedef unsigned short guint16;
68 typedef unsigned int guint32;
70 ////////////////////////////////////////////////////////////////////////////
71 // Global variables get exported to cvar in Python
73 extern gdcmGlobal gdcmGlob;
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 for (list<string>::iterator NewString = ($1)->begin();
81 NewString != ($1)->end(); ++NewString) {
82 NewItem = PyString_FromString(NewString->c_str());
83 PyList_Append( NewList, NewItem);
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;
95 for (map<string, list<string> >::iterator tag = ($1)->begin();
96 tag != ($1)->end(); ++tag) {
97 string first = tag->first;
98 // Do not publish entries whose keys is made of spaces
99 if (first.length() == 0)
101 NewKey = PyString_FromString(first.c_str());
102 PyObject* NewList = PyList_New(0);
103 for (list<string>::iterator Item = tag->second.begin();
104 Item != tag->second.end(); ++Item) {
105 NewVal = PyString_FromString(Item->c_str());
106 PyList_Append( NewList, NewVal);
108 PyDict_SetItem( NewDict, NewKey, NewList);
113 ////////////////////////////////////////////////////////////////////////////
114 // Convert a c++ hash table in a python native dictionary
115 %typemap(out) TagHeaderEntryHT & {
116 PyObject* NewDict = PyDict_New(); // The result of this typemap
117 string RawName; // Element name as gotten from gdcm
118 PyObject* NewKey = (PyObject*)0; // Associated name as python object
119 string RawValue; // Element value as gotten from gdcm
120 PyObject* NewVal = (PyObject*)0; // Associated value as python object
122 for (TagHeaderEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag) {
124 // The element name shall be the key:
125 RawName = tag->second->GetName();
126 // gdcm unrecognized (including not loaded because their size exceeds
127 // the user specified treshold) elements are exported with their
129 if (RawName == "Unknown")
130 RawName = tag->second->GetKey();
131 NewKey = PyString_FromString(RawName.c_str());
133 // Element values are striped from leading/trailing spaces
134 RawValue = tag->second->GetValue();
135 EatLeadingAndTrailingSpaces(RawValue);
136 NewVal = PyString_FromString(RawValue.c_str());
138 PyDict_SetItem( NewDict, NewKey, NewVal);
143 %typemap(out) TagHeaderEntryHT {
144 PyObject* NewDict = PyDict_New(); // The result of this typemap
145 string RawName; // Element name as gotten from gdcm
146 PyObject* NewKey = (PyObject*)0; // Associated name as python object
147 string RawValue; // Element value as gotten from gdcm
148 PyObject* NewVal = (PyObject*)0; // Associated value as python object
150 for (TagHeaderEntryHT::iterator tag = $1.begin(); tag != $1.end(); ++tag) {
152 // The element name shall be the key:
153 RawName = tag->second->GetName();
154 // gdcm unrecognized (including not loaded because their size exceeds
155 // the user specified treshold) elements are exported with their
157 if (RawName == "Unknown")
158 RawName = tag->second->GetKey();
159 NewKey = PyString_FromString(RawName.c_str());
161 // Element values are striped from leading/trailing spaces
162 RawValue = tag->second->GetValue();
163 EatLeadingAndTrailingSpaces(RawValue);
164 NewVal = PyString_FromString(RawValue.c_str());
166 PyDict_SetItem( NewDict, NewKey, NewVal);
171 ////////////////////////////////////////////////////////////////////////////
172 %typemap(out) ListPatient & {
173 PyObject* NewItem = (PyObject*)0;
174 $result = PyList_New(0); // The result of this typemap
176 for (list<gdcmPatient *>::iterator New = ($1)->begin();
177 New != ($1)->end(); ++New) {
178 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmPatient,1);
179 PyList_Append($result, NewItem);
183 %typemap(out) ListStudy & {
184 PyObject* NewItem = (PyObject*)0;
185 $result = PyList_New(0); // The result of this typemap
187 for (list<gdcmStudy *>::iterator New = ($1)->begin();
188 New != ($1)->end(); ++New) {
189 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmStudy,1);
190 PyList_Append($result, NewItem);
194 %typemap(out) ListSerie & {
195 PyObject* NewItem = (PyObject*)0;
196 $result = PyList_New(0); // The result of this typemap
198 for (list<gdcmSerie *>::iterator New = ($1)->begin();
199 New != ($1)->end(); ++New) {
200 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmSerie,1);
201 PyList_Append($result, NewItem);
205 %typemap(out) ListImage & {
206 PyObject* NewItem = (PyObject*)0;
207 $result = PyList_New(0); // The result of this typemap
209 for (list<gdcmImage *>::iterator New = ($1)->begin();
210 New != ($1)->end(); ++New) {
211 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmImage,1);
212 PyList_Append($result, NewItem);
216 ////////////////////////////////////////////////////////////////////////////
217 // Deals with function returning a C++ string.
218 %typemap(python, in) (gdcmMethod *method,void *arg) {
220 $1=vtkPythonVoidFunc;
225 ////////////////////////////////////////////////////////////////////////////
226 // Deals with function returning a C++ string.
227 %typemap(out) string, std::string {
228 $result = PyString_FromString(($1).c_str());
231 %typemap(python, in) const std::string, std::string
233 $1 = PyString_AsString($input);
236 ////////////////////////////////////////////////////////////////////////////
237 %include "gdcmCommon.h"
238 %include "gdcmDictEntry.h"
239 %include "gdcmDict.h"
240 %include "gdcmDictSet.h"
241 %include "gdcmParser.h"
242 %include "gdcmHeaderEntry.h"
243 %include "gdcmHeader.h"
244 %include "gdcmHeaderHelper.h"
245 %include "gdcmFile.h"
246 %include "gdcmUtil.h"
247 %include "gdcmObject.h"
248 %include "gdcmDicomDir.h"
249 %include "gdcmDicomDirElement.h"
250 %include "gdcmMeta.h"
251 %include "gdcmPatient.h"
252 %include "gdcmStudy.h"
253 %include "gdcmSerie.h"
254 %include "gdcmImage.h"