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"
23 ////////////////////////////////////////////////////////////////////////////
24 // Utility functions on strings for removing leading and trailing spaces
25 void EatLeadingAndTrailingSpaces(std::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 gdcmPythonVoidFunc(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 std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
57 void gdcmPythonVoidFuncArgDelete(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 (std::list<std::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 (std::map<std::string, std::list<std::string> >::iterator tag = ($1)->begin();
96 tag != ($1)->end(); ++tag) {
97 std::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 (std::list<std::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 std::string RawName; // Element name as gotten from gdcm
118 PyObject* NewKey = (PyObject*)0; // Associated name as python object
119 std::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 std::string RawName; // Element name as gotten from gdcm
146 PyObject* NewKey = (PyObject*)0; // Associated name as python object
147 std::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) ListDicomDirPatient & {
173 PyObject* NewItem = (PyObject*)0;
174 $result = PyList_New(0); // The result of this typemap
176 for (std::list<gdcmDicomDirPatient *>::iterator New = ($1)->begin();
177 New != ($1)->end(); ++New) {
178 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirPatient,1);
179 PyList_Append($result, NewItem);
183 %typemap(out) ListDicomDirStudy & {
184 PyObject* NewItem = (PyObject*)0;
185 $result = PyList_New(0); // The result of this typemap
187 for (std::list<gdcmDicomDirStudy *>::iterator New = ($1)->begin();
188 New != ($1)->end(); ++New) {
189 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirStudy,1);
190 PyList_Append($result, NewItem);
194 %typemap(out) ListDicomDirSerie & {
195 PyObject* NewItem = (PyObject*)0;
196 $result = PyList_New(0); // The result of this typemap
198 for (std::list<gdcmDicomDirSerie *>::iterator New = ($1)->begin();
199 New != ($1)->end(); ++New) {
200 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirSerie,1);
201 PyList_Append($result, NewItem);
205 %typemap(out) ListDicomDirImage & {
206 PyObject* NewItem = (PyObject*)0;
207 $result = PyList_New(0); // The result of this typemap
209 for (std::list<gdcmDicomDirImage *>::iterator New = ($1)->begin();
210 New != ($1)->end(); ++New) {
211 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirImage,1);
212 PyList_Append($result, NewItem);
216 ////////////////////////////////////////////////////////////////////////////
217 // Deals with function returning a C++ string.
218 %typemap(python, in) (gdcmMethod *,void * =NULL,gdcmMethod * =NULL) {
222 $1=gdcmPythonVoidFunc;
224 $3=gdcmPythonVoidFuncArgDelete;
235 ////////////////////////////////////////////////////////////////////////////
236 // Deals with function returning a C++ string.
237 %typemap(out) string, std::string {
238 $result = PyString_FromString(($1).c_str());
241 %typemap(python, in) const std::string, std::string
243 $1 = PyString_AsString($input);
246 ////////////////////////////////////////////////////////////////////////////
247 %include "gdcmCommon.h"
248 %include "gdcmDictEntry.h"
249 %include "gdcmDict.h"
250 %include "gdcmDictSet.h"
251 %include "gdcmParser.h"
252 %include "gdcmHeaderEntry.h"
253 %include "gdcmHeader.h"
254 %include "gdcmHeaderHelper.h"
255 %include "gdcmFile.h"
256 %include "gdcmUtil.h"
257 %include "gdcmGlobal.h"
258 %include "gdcmObject.h"
259 %include "gdcmDicomDir.h"
260 %include "gdcmDicomDirElement.h"
261 %include "gdcmDicomDirMeta.h"
262 %include "gdcmDicomDirPatient.h"
263 %include "gdcmDicomDirStudy.h"
264 %include "gdcmDicomDirSerie.h"
265 %include "gdcmDicomDirImage.h"