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"
16 #include "gdcmDicomDirMeta.h"
17 #include "gdcmDicomDirPatient.h"
18 #include "gdcmDicomDirStudy.h"
19 #include "gdcmDicomDirSerie.h"
20 #include "gdcmDicomDirImage.h"
24 ////////////////////////////////////////////////////////////////////////////
25 // Utility functions on strings for removing leading and trailing spaces
26 void EatLeadingAndTrailingSpaces(string & s) {
27 while ( s.length() && (s[0] == ' ') )
29 while ( s.length() && (s[s.length()-1] == ' ') )
30 s.erase(s.length()-1, 1);
33 void gdcmPythonVoidFunc(void *arg)
35 PyObject *arglist, *result;
36 PyObject *func = (PyObject *)arg;
38 arglist = Py_BuildValue("()");
40 result = PyEval_CallObject(func, arglist);
49 if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
51 cerr << "Caught a Ctrl-C within python, exiting program.\n";
58 void gdcmPythonVoidFuncArgDelete(void *arg)
60 PyObject *func = (PyObject *)arg;
68 typedef unsigned short guint16;
69 typedef unsigned int guint32;
71 ////////////////////////////////////////////////////////////////////////////
72 // Global variables get exported to cvar in Python
74 extern gdcmGlobal gdcmGlob;
77 ////////////////////////////////////////////////////////////////////////////
78 %typemap(out) std::list<std::string> * {
79 PyObject* NewItem = (PyObject*)0;
80 PyObject* NewList = PyList_New(0); // The result of this typemap
81 for (list<string>::iterator NewString = ($1)->begin();
82 NewString != ($1)->end(); ++NewString) {
83 NewItem = PyString_FromString(NewString->c_str());
84 PyList_Append( NewList, NewItem);
89 ////////////////////////////////////////////////////////////////////////////
90 // Convert a c++ hash table in a python native dictionary
91 %typemap(out) std::map<std::string, std::list<std::string> > * {
92 PyObject* NewDict = PyDict_New(); // The result of this typemap
93 PyObject* NewKey = (PyObject*)0;
94 PyObject* NewVal = (PyObject*)0;
96 for (map<string, list<string> >::iterator tag = ($1)->begin();
97 tag != ($1)->end(); ++tag) {
98 string first = tag->first;
99 // Do not publish entries whose keys is made of spaces
100 if (first.length() == 0)
102 NewKey = PyString_FromString(first.c_str());
103 PyObject* NewList = PyList_New(0);
104 for (list<string>::iterator Item = tag->second.begin();
105 Item != tag->second.end(); ++Item) {
106 NewVal = PyString_FromString(Item->c_str());
107 PyList_Append( NewList, NewVal);
109 PyDict_SetItem( NewDict, NewKey, NewList);
114 ////////////////////////////////////////////////////////////////////////////
115 // Convert a c++ hash table in a python native dictionary
116 %typemap(out) TagHeaderEntryHT & {
117 PyObject* NewDict = PyDict_New(); // The result of this typemap
118 string RawName; // Element name as gotten from gdcm
119 PyObject* NewKey = (PyObject*)0; // Associated name as python object
120 string RawValue; // Element value as gotten from gdcm
121 PyObject* NewVal = (PyObject*)0; // Associated value as python object
123 for (TagHeaderEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag) {
125 // The element name shall be the key:
126 RawName = tag->second->GetName();
127 // gdcm unrecognized (including not loaded because their size exceeds
128 // the user specified treshold) elements are exported with their
130 if (RawName == "Unknown")
131 RawName = tag->second->GetKey();
132 NewKey = PyString_FromString(RawName.c_str());
134 // Element values are striped from leading/trailing spaces
135 RawValue = tag->second->GetValue();
136 EatLeadingAndTrailingSpaces(RawValue);
137 NewVal = PyString_FromString(RawValue.c_str());
139 PyDict_SetItem( NewDict, NewKey, NewVal);
144 %typemap(out) TagHeaderEntryHT {
145 PyObject* NewDict = PyDict_New(); // The result of this typemap
146 string RawName; // Element name as gotten from gdcm
147 PyObject* NewKey = (PyObject*)0; // Associated name as python object
148 string RawValue; // Element value as gotten from gdcm
149 PyObject* NewVal = (PyObject*)0; // Associated value as python object
151 for (TagHeaderEntryHT::iterator tag = $1.begin(); tag != $1.end(); ++tag) {
153 // The element name shall be the key:
154 RawName = tag->second->GetName();
155 // gdcm unrecognized (including not loaded because their size exceeds
156 // the user specified treshold) elements are exported with their
158 if (RawName == "Unknown")
159 RawName = tag->second->GetKey();
160 NewKey = PyString_FromString(RawName.c_str());
162 // Element values are striped from leading/trailing spaces
163 RawValue = tag->second->GetValue();
164 EatLeadingAndTrailingSpaces(RawValue);
165 NewVal = PyString_FromString(RawValue.c_str());
167 PyDict_SetItem( NewDict, NewKey, NewVal);
172 ////////////////////////////////////////////////////////////////////////////
173 %typemap(out) ListDicomDirPatient & {
174 PyObject* NewItem = (PyObject*)0;
175 $result = PyList_New(0); // The result of this typemap
177 for (list<gdcmDicomDirPatient *>::iterator New = ($1)->begin();
178 New != ($1)->end(); ++New) {
179 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirPatient,1);
180 PyList_Append($result, NewItem);
184 %typemap(out) ListDicomDirStudy & {
185 PyObject* NewItem = (PyObject*)0;
186 $result = PyList_New(0); // The result of this typemap
188 for (list<gdcmDicomDirStudy *>::iterator New = ($1)->begin();
189 New != ($1)->end(); ++New) {
190 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirStudy,1);
191 PyList_Append($result, NewItem);
195 %typemap(out) ListDicomDirSerie & {
196 PyObject* NewItem = (PyObject*)0;
197 $result = PyList_New(0); // The result of this typemap
199 for (list<gdcmDicomDirSerie *>::iterator New = ($1)->begin();
200 New != ($1)->end(); ++New) {
201 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirSerie,1);
202 PyList_Append($result, NewItem);
206 %typemap(out) ListDicomDirImage & {
207 PyObject* NewItem = (PyObject*)0;
208 $result = PyList_New(0); // The result of this typemap
210 for (list<gdcmDicomDirImage *>::iterator New = ($1)->begin();
211 New != ($1)->end(); ++New) {
212 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirImage,1);
213 PyList_Append($result, NewItem);
217 ////////////////////////////////////////////////////////////////////////////
218 // Deals with function returning a C++ string.
219 %typemap(python, in) (gdcmMethod *,void * =NULL,gdcmMethod * =NULL) {
223 $1=gdcmPythonVoidFunc;
225 $3=gdcmPythonVoidFuncArgDelete;
236 ////////////////////////////////////////////////////////////////////////////
237 // Deals with function returning a C++ string.
238 %typemap(out) string, std::string {
239 $result = PyString_FromString(($1).c_str());
242 %typemap(python, in) const std::string, std::string
244 $1 = PyString_AsString($input);
247 ////////////////////////////////////////////////////////////////////////////
248 %include "gdcmCommon.h"
249 %include "gdcmDictEntry.h"
250 %include "gdcmDict.h"
251 %include "gdcmDictSet.h"
252 %include "gdcmParser.h"
253 %include "gdcmHeaderEntry.h"
254 %include "gdcmHeader.h"
255 %include "gdcmHeaderHelper.h"
256 %include "gdcmFile.h"
257 %include "gdcmUtil.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"