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 %typemap(out) std::list<std::string> * {
72 PyObject* NewItem = (PyObject*)0;
73 PyObject* NewList = PyList_New(0); // The result of this typemap
74 for (std::list<std::string>::iterator NewString = ($1)->begin();
75 NewString != ($1)->end(); ++NewString) {
76 NewItem = PyString_FromString(NewString->c_str());
77 PyList_Append( NewList, NewItem);
82 ////////////////////////////////////////////////////////////////////////////
83 // Convert a c++ hash table in a python native dictionary
84 %typemap(out) std::map<std::string, std::list<std::string> > * {
85 PyObject* NewDict = PyDict_New(); // The result of this typemap
86 PyObject* NewKey = (PyObject*)0;
87 PyObject* NewVal = (PyObject*)0;
89 for (std::map<std::string, std::list<std::string> >::iterator tag = ($1)->begin();
90 tag != ($1)->end(); ++tag) {
91 std::string first = tag->first;
92 // Do not publish entries whose keys is made of spaces
93 if (first.length() == 0)
95 NewKey = PyString_FromString(first.c_str());
96 PyObject* NewList = PyList_New(0);
97 for (std::list<std::string>::iterator Item = tag->second.begin();
98 Item != tag->second.end(); ++Item) {
99 NewVal = PyString_FromString(Item->c_str());
100 PyList_Append( NewList, NewVal);
102 PyDict_SetItem( NewDict, NewKey, NewList);
107 ////////////////////////////////////////////////////////////////////////////
108 // Convert a c++ hash table in a python native dictionary
109 %typemap(out) TagHeaderEntryHT & {
110 PyObject* NewDict = PyDict_New(); // The result of this typemap
111 std::string RawName; // Element name as gotten from gdcm
112 PyObject* NewKey = (PyObject*)0; // Associated name as python object
113 std::string RawValue; // Element value as gotten from gdcm
114 PyObject* NewVal = (PyObject*)0; // Associated value as python object
116 for (TagHeaderEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag) {
118 // The element name shall be the key:
119 RawName = tag->second->GetName();
120 // gdcm unrecognized (including not loaded because their size exceeds
121 // the user specified treshold) elements are exported with their
123 if (RawName == "Unknown")
124 RawName = tag->second->GetKey();
125 NewKey = PyString_FromString(RawName.c_str());
127 // Element values are striped from leading/trailing spaces
128 RawValue = tag->second->GetValue();
129 EatLeadingAndTrailingSpaces(RawValue);
130 NewVal = PyString_FromString(RawValue.c_str());
132 PyDict_SetItem( NewDict, NewKey, NewVal);
137 %typemap(out) TagHeaderEntryHT {
138 PyObject* NewDict = PyDict_New(); // The result of this typemap
139 std::string RawName; // Element name as gotten from gdcm
140 PyObject* NewKey = (PyObject*)0; // Associated name as python object
141 std::string RawValue; // Element value as gotten from gdcm
142 PyObject* NewVal = (PyObject*)0; // Associated value as python object
144 for (TagHeaderEntryHT::iterator tag = $1.begin(); tag != $1.end(); ++tag) {
146 // The element name shall be the key:
147 RawName = tag->second->GetName();
148 // gdcm unrecognized (including not loaded because their size exceeds
149 // the user specified treshold) elements are exported with their
151 if (RawName == "Unknown")
152 RawName = tag->second->GetKey();
153 NewKey = PyString_FromString(RawName.c_str());
155 // Element values are striped from leading/trailing spaces
156 RawValue = tag->second->GetValue();
157 EatLeadingAndTrailingSpaces(RawValue);
158 NewVal = PyString_FromString(RawValue.c_str());
160 PyDict_SetItem( NewDict, NewKey, NewVal);
165 ////////////////////////////////////////////////////////////////////////////
166 %typemap(out) ListDicomDirPatient & {
167 PyObject* NewItem = (PyObject*)0;
168 $result = PyList_New(0); // The result of this typemap
170 for (std::list<gdcmDicomDirPatient *>::iterator New = ($1)->begin();
171 New != ($1)->end(); ++New) {
172 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirPatient,1);
173 PyList_Append($result, NewItem);
177 %typemap(out) ListDicomDirStudy & {
178 PyObject* NewItem = (PyObject*)0;
179 $result = PyList_New(0); // The result of this typemap
181 for (std::list<gdcmDicomDirStudy *>::iterator New = ($1)->begin();
182 New != ($1)->end(); ++New) {
183 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirStudy,1);
184 PyList_Append($result, NewItem);
188 %typemap(out) ListDicomDirSerie & {
189 PyObject* NewItem = (PyObject*)0;
190 $result = PyList_New(0); // The result of this typemap
192 for (std::list<gdcmDicomDirSerie *>::iterator New = ($1)->begin();
193 New != ($1)->end(); ++New) {
194 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirSerie,1);
195 PyList_Append($result, NewItem);
199 %typemap(out) ListDicomDirImage & {
200 PyObject* NewItem = (PyObject*)0;
201 $result = PyList_New(0); // The result of this typemap
203 for (std::list<gdcmDicomDirImage *>::iterator New = ($1)->begin();
204 New != ($1)->end(); ++New) {
205 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirImage,1);
206 PyList_Append($result, NewItem);
210 ////////////////////////////////////////////////////////////////////////////
211 // Deals with function returning a C++ string.
212 %typemap(python, in) (gdcmMethod *,void * =NULL,gdcmMethod * =NULL) {
216 $1=gdcmPythonVoidFunc;
218 $3=gdcmPythonVoidFuncArgDelete;
229 ////////////////////////////////////////////////////////////////////////////
230 // Deals with function returning a C++ string.
231 %typemap(out) string, std::string {
232 $result = PyString_FromString(($1).c_str());
235 %typemap(python, in) const std::string, std::string
237 $1 = PyString_AsString($input);
240 ////////////////////////////////////////////////////////////////////////////
241 %include "gdcmCommon.h"
242 %include "gdcmDictEntry.h"
243 %include "gdcmDict.h"
244 %include "gdcmDictSet.h"
245 %include "gdcmParser.h"
246 %include "gdcmHeaderEntry.h"
247 %include "gdcmHeader.h"
248 %include "gdcmHeaderHelper.h"
249 %include "gdcmFile.h"
250 %include "gdcmUtil.h"
251 %include "gdcmGlobal.h"
252 %include "gdcmObject.h"
253 %include "gdcmDicomDir.h"
254 %include "gdcmDicomDirElement.h"
255 %include "gdcmDicomDirMeta.h"
256 %include "gdcmDicomDirPatient.h"
257 %include "gdcmDicomDirStudy.h"
258 %include "gdcmDicomDirSerie.h"
259 %include "gdcmDicomDirImage.h"