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 "gdcmDicomDirObject.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
78 for (std::list<std::string>::iterator NewString = ($1)->begin();
79 NewString != ($1)->end();
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,
96 std::list<std::string> >::iterator tag = ($1)->begin();
97 tag != ($1)->end(); ++tag)
99 std::string first = tag->first;
100 // Do not publish entries whose keys is made of spaces
101 if (first.length() == 0)
103 NewKey = PyString_FromString(first.c_str());
104 PyObject* NewList = PyList_New(0);
105 for (std::list<std::string>::iterator Item = tag->second.begin();
106 Item != tag->second.end();
109 NewVal = PyString_FromString(Item->c_str());
110 PyList_Append( NewList, NewVal);
112 PyDict_SetItem( NewDict, NewKey, NewList);
117 ////////////////////////////////////////////////////////////////////////////
118 // Convert a c++ hash table in a python native dictionary
119 %typemap(out) gdcm::TagDocEntryHT & {
120 PyObject* NewDict = PyDict_New(); // The result of this typemap
121 std::string RawName; // Element name as gotten from gdcm
122 PyObject* NewKey = (PyObject*)0; // Associated name as python object
123 std::string RawValue; // Element value as gotten from gdcm
124 PyObject* NewVal = (PyObject*)0; // Associated value as python object
126 for (gdcm::TagDocEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag)
128 // The element name shall be the key:
129 RawName = tag->second->GetName();
130 // gdcm unrecognized (including not loaded because their size exceeds
131 // the user specified treshold) elements are exported with their
133 if (RawName == "Unknown")
134 RawName = tag->second->GetKey();
135 NewKey = PyString_FromString(RawName.c_str());
137 // Element values are striped from leading/trailing spaces
138 if (gdcm::ValEntry* ValEntryPtr =
139 dynamic_cast< gdcm::ValEntry* >(tag->second) )
141 RawValue = ValEntryPtr->GetValue();
145 EatLeadingAndTrailingSpaces(RawValue);
146 NewVal = PyString_FromString(RawValue.c_str());
147 PyDict_SetItem( NewDict, NewKey, NewVal);
152 ////////////////////////////////////////////////////////////////////////////
153 %typemap(out) ListDicomDirPatient & {
154 PyObject* NewItem = (PyObject*)0;
155 $result = PyList_New(0); // The result of this typemap
157 for (std::list<gdcm::DicomDirPatient *>::iterator New = ($1)->begin();
158 New != ($1)->end(); ++New) {
159 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirPatient,1);
160 PyList_Append($result, NewItem);
164 %typemap(out) ListDicomDirStudy & {
165 PyObject* NewItem = (PyObject*)0;
166 $result = PyList_New(0); // The result of this typemap
168 for (std::list<gdcm::DicomDirStudy *>::iterator New = ($1)->begin();
169 New != ($1)->end(); ++New) {
170 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirStudy,1);
171 PyList_Append($result, NewItem);
175 %typemap(out) ListDicomDirSerie & {
176 PyObject* NewItem = (PyObject*)0;
177 $result = PyList_New(0); // The result of this typemap
179 for (std::list<gdcm::DicomDirSerie *>::iterator New = ($1)->begin();
180 New != ($1)->end(); ++New) {
181 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirSerie,1);
182 PyList_Append($result, NewItem);
186 %typemap(out) ListDicomDirImage & {
187 PyObject* NewItem = (PyObject*)0;
188 $result = PyList_New(0); // The result of this typemap
190 for (std::list<gdcm::DicomDirImage *>::iterator New = ($1)->begin();
191 New != ($1)->end(); ++New) {
192 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirImage,1);
193 PyList_Append($result, NewItem);
197 ////////////////////////////////////////////////////////////////////////////
198 // Deals with function returning a C++ string.
199 %typemap(python, in) (gdcm::Method *,void * =NULL,gdcm::Method * =NULL) {
203 $1=gdcmPythonVoidFunc;
205 $3=gdcmPythonVoidFuncArgDelete;
216 ////////////////////////////////////////////////////////////////////////////
217 // Deals with function returning a C++ string.
218 %typemap(out) string, std::string {
219 $result = PyString_FromString(($1).c_str());
222 %typemap(python, in) const std::string, std::string
224 $1 = PyString_AsString($input);
227 ////////////////////////////////////////////////////////////////////////////
228 // Warning: Order matters !
229 %include "gdcmCommon.h"
230 %include "gdcmDictEntry.h"
231 %include "gdcmDict.h"
232 %include "gdcmDocEntry.h"
233 %include "gdcmDocEntrySet.h"
234 %include "gdcmElementSet.h"
235 %include "gdcmDictSet.h"
238 %include "gdcmSQItem.h"
239 %include "gdcmDicomDirElement.h"
240 %include "gdcmDicomDirObject.h"
241 %include "gdcmDicomDirImage.h"
242 %include "gdcmDicomDirSerie.h"
243 %include "gdcmDicomDirStudy.h"
244 %include "gdcmDicomDirPatient.h"
245 %include "gdcmDicomDirMeta.h"
246 %include "gdcmDocument.h"
247 %include "gdcmHeader.h"
248 %include "gdcmHeaderHelper.h"
249 %include "gdcmFile.h"
250 %include "gdcmUtil.h"
251 %include "gdcmGlobal.h"
252 %include "gdcmDicomDir.h"