3 #include "gdcmCommon.h"
4 #include "gdcmDictEntry.h"
6 #include "gdcmDictSet.h"
7 #include "gdcmDocEntrySet.h"
8 #include "gdcmSQItem.h"
9 #include "gdcmDocument.h"
10 #include "gdcmElementSet.h"
11 #include "gdcmHeader.h"
12 #include "gdcmHeaderHelper.h"
15 #include "gdcmGlobal.h"
16 #include "gdcmObject.h"
17 #include "gdcmDicomDir.h"
18 #include "gdcmDicomDirElement.h"
19 #include "gdcmDicomDirMeta.h"
20 #include "gdcmDicomDirPatient.h"
21 #include "gdcmDicomDirStudy.h"
22 #include "gdcmDicomDirSerie.h"
23 #include "gdcmDicomDirImage.h"
24 #include "gdcmValEntry.h"
26 ////////////////////////////////////////////////////////////////////////////
27 // Utility functions on strings for removing leading and trailing spaces
28 void EatLeadingAndTrailingSpaces(std::string & s) {
29 while ( s.length() && (s[0] == ' ') )
31 while ( s.length() && (s[s.length()-1] == ' ') )
32 s.erase(s.length()-1, 1);
35 void gdcmPythonVoidFunc(void *arg)
37 PyObject *arglist, *result;
38 PyObject *func = (PyObject *)arg;
40 arglist = Py_BuildValue("()");
42 result = PyEval_CallObject(func, arglist);
51 if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
53 std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
60 void gdcmPythonVoidFuncArgDelete(void *arg)
62 PyObject *func = (PyObject *)arg;
70 typedef unsigned short guint16;
71 typedef unsigned int guint32;
73 ////////////////////////////////////////////////////////////////////////////
74 %typemap(out) std::list<std::string> * {
75 PyObject* NewItem = (PyObject*)0;
76 PyObject* NewList = PyList_New(0); // The result of this typemap
77 for (std::list<std::string>::iterator NewString = ($1)->begin();
78 NewString != ($1)->end(); ++NewString) {
79 NewItem = PyString_FromString(NewString->c_str());
80 PyList_Append( NewList, NewItem);
85 ////////////////////////////////////////////////////////////////////////////
86 // Convert a c++ hash table in a python native dictionary
87 %typemap(out) std::map<std::string, std::list<std::string> > * {
88 PyObject* NewDict = PyDict_New(); // The result of this typemap
89 PyObject* NewKey = (PyObject*)0;
90 PyObject* NewVal = (PyObject*)0;
92 for (std::map<std::string, std::list<std::string> >::iterator tag = ($1)->begin();
93 tag != ($1)->end(); ++tag) {
94 std::string first = tag->first;
95 // Do not publish entries whose keys is made of spaces
96 if (first.length() == 0)
98 NewKey = PyString_FromString(first.c_str());
99 PyObject* NewList = PyList_New(0);
100 for (std::list<std::string>::iterator Item = tag->second.begin();
101 Item != tag->second.end(); ++Item) {
102 NewVal = PyString_FromString(Item->c_str());
103 PyList_Append( NewList, NewVal);
105 PyDict_SetItem( NewDict, NewKey, NewList);
110 ////////////////////////////////////////////////////////////////////////////
111 // Convert a c++ hash table in a python native dictionary
112 %typemap(out) TagDocEntryHT & {
113 PyObject* NewDict = PyDict_New(); // The result of this typemap
114 std::string RawName; // Element name as gotten from gdcm
115 PyObject* NewKey = (PyObject*)0; // Associated name as python object
116 std::string RawValue; // Element value as gotten from gdcm
117 PyObject* NewVal = (PyObject*)0; // Associated value as python object
119 for (TagDocEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag) {
121 // The element name shall be the key:
122 RawName = tag->second->GetName();
123 // gdcm unrecognized (including not loaded because their size exceeds
124 // the user specified treshold) elements are exported with their
126 if (RawName == "Unknown")
127 RawName = tag->second->GetKey();
128 NewKey = PyString_FromString(RawName.c_str());
130 // Element values are striped from leading/trailing spaces
131 // Element values are striped from leading/trailing spaces
132 if (gdcmValEntry* ValEntryPtr =
133 dynamic_cast< gdcmValEntry* >(tag->second) )
135 RawValue = ValEntryPtr->GetValue();
139 EatLeadingAndTrailingSpaces(RawValue);
140 NewVal = PyString_FromString(RawValue.c_str());
142 PyDict_SetItem( NewDict, NewKey, NewVal);
148 CLEAN ME FIXME CLEANME TODO
149 %typemap(out) TagDocEntryHT {
150 PyObject* NewDict = PyDict_New(); // The result of this typemap
151 std::string RawName; // Element name as gotten from gdcm
152 PyObject* NewKey = (PyObject*)0; // Associated name as python object
153 std::string RawValue; // Element value as gotten from gdcm
154 PyObject* NewVal = (PyObject*)0; // Associated value as python object
156 for (TagDocEntryHT::iterator tag = $1.begin(); tag != $1.end(); ++tag) {
158 // The element name shall be the key:
159 RawName = tag->second->GetName();
160 // gdcm unrecognized (including not loaded because their size exceeds
161 // the user specified treshold) elements are exported with their
163 if (RawName == "Unknown")
164 RawName = tag->second->GetKey();
165 NewKey = PyString_FromString(RawName.c_str());
167 // Element values are striped from leading/trailing spaces
168 if (gdcmValEntry* ValEntryPtr =
169 dynamic_cast< gdcmValEntry* >(tag->second) )
171 RawValue = ValEntryPtr->GetValue();
175 EatLeadingAndTrailingSpaces(RawValue);
176 NewVal = PyString_FromString(RawValue.c_str());
178 PyDict_SetItem( NewDict, NewKey, NewVal);
184 ////////////////////////////////////////////////////////////////////////////
185 %typemap(out) ListDicomDirPatient & {
186 PyObject* NewItem = (PyObject*)0;
187 $result = PyList_New(0); // The result of this typemap
189 for (std::list<gdcmDicomDirPatient *>::iterator New = ($1)->begin();
190 New != ($1)->end(); ++New) {
191 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirPatient,1);
192 PyList_Append($result, NewItem);
196 %typemap(out) ListDicomDirStudy & {
197 PyObject* NewItem = (PyObject*)0;
198 $result = PyList_New(0); // The result of this typemap
200 for (std::list<gdcmDicomDirStudy *>::iterator New = ($1)->begin();
201 New != ($1)->end(); ++New) {
202 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirStudy,1);
203 PyList_Append($result, NewItem);
207 %typemap(out) ListDicomDirSerie & {
208 PyObject* NewItem = (PyObject*)0;
209 $result = PyList_New(0); // The result of this typemap
211 for (std::list<gdcmDicomDirSerie *>::iterator New = ($1)->begin();
212 New != ($1)->end(); ++New) {
213 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirSerie,1);
214 PyList_Append($result, NewItem);
218 %typemap(out) ListDicomDirImage & {
219 PyObject* NewItem = (PyObject*)0;
220 $result = PyList_New(0); // The result of this typemap
222 for (std::list<gdcmDicomDirImage *>::iterator New = ($1)->begin();
223 New != ($1)->end(); ++New) {
224 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_gdcmDicomDirImage,1);
225 PyList_Append($result, NewItem);
229 ////////////////////////////////////////////////////////////////////////////
230 // Deals with function returning a C++ string.
231 %typemap(python, in) (gdcmMethod *,void * =NULL,gdcmMethod * =NULL) {
235 $1=gdcmPythonVoidFunc;
237 $3=gdcmPythonVoidFuncArgDelete;
248 ////////////////////////////////////////////////////////////////////////////
249 // Deals with function returning a C++ string.
250 %typemap(out) string, std::string {
251 $result = PyString_FromString(($1).c_str());
254 %typemap(python, in) const std::string, std::string
256 $1 = PyString_AsString($input);
259 ////////////////////////////////////////////////////////////////////////////
260 // Warning: Order matters !
261 %include "gdcmCommon.h"
262 %include "gdcmDictEntry.h"
263 %include "gdcmDict.h"
264 %include "gdcmDictSet.h"
265 %include "gdcmDocEntrySet.h"
266 %include "gdcmElementSet.h"
267 %include "gdcmDocument.h"
268 %include "gdcmSQItem.h"
269 %include "gdcmHeader.h"
270 %include "gdcmHeaderHelper.h"
271 %include "gdcmFile.h"
272 %include "gdcmUtil.h"
273 %include "gdcmGlobal.h"
274 %include "gdcmObject.h"
275 %include "gdcmDicomDir.h"
276 %include "gdcmDicomDirElement.h"
277 %include "gdcmDicomDirMeta.h"
278 %include "gdcmDicomDirPatient.h"
279 %include "gdcmDicomDirStudy.h"
280 %include "gdcmDicomDirSerie.h"
281 %include "gdcmDicomDirImage.h"