3 #include "gdcmCommon.h"
5 #include "gdcmDictEntry.h"
6 #include "gdcmDictSet.h"
7 #include "gdcmDicomDir.h"
8 #include "gdcmDicomDirElement.h"
9 #include "gdcmDicomDirImage.h"
10 #include "gdcmDicomDirMeta.h"
11 #include "gdcmDicomDirObject.h"
12 #include "gdcmDicomDirPatient.h"
13 #include "gdcmDicomDirStudy.h"
14 #include "gdcmDicomDirSerie.h"
15 #include "gdcmDocEntrySet.h"
16 #include "gdcmDocument.h"
17 #include "gdcmElementSet.h"
19 #include "gdcmGlobal.h"
20 #include "gdcmHeader.h"
21 #include "gdcmHeaderHelper.h"
22 #include "gdcmRLEFramesInfo.h"
23 #include "gdcmJPEGFragmentsInfo.h"
24 #include "gdcmSQItem.h"
26 #include "gdcmValEntry.h"
28 ////////////////////////////////////////////////////////////////////////////
29 // Utility functions on strings for removing leading and trailing spaces
30 void EatLeadingAndTrailingSpaces(std::string & s) {
31 while ( s.length() && (s[0] == ' ') )
33 while ( s.length() && (s[s.length()-1] == ' ') )
34 s.erase(s.length()-1, 1);
37 void gdcmPythonVoidFunc(void *arg)
39 PyObject *arglist, *result;
40 PyObject *func = (PyObject *)arg;
42 arglist = Py_BuildValue("()");
44 result = PyEval_CallObject(func, arglist);
53 if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
55 std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
62 void gdcmPythonVoidFuncArgDelete(void *arg)
64 PyObject *func = (PyObject *)arg;
72 typedef unsigned short guint16;
73 typedef unsigned int guint32;
75 ////////////////////////////////////////////////////////////////////////////
76 %typemap(out) std::list<std::string> * {
77 PyObject* NewItem = (PyObject*)0;
78 PyObject* NewList = PyList_New(0); // The result of this typemap
80 for (std::list<std::string>::iterator NewString = ($1)->begin();
81 NewString != ($1)->end();
84 NewItem = PyString_FromString(NewString->c_str());
85 PyList_Append( NewList, NewItem);
90 ////////////////////////////////////////////////////////////////////////////
91 // Convert a c++ hash table in a python native dictionary
92 %typemap(out) std::map<std::string, std::list<std::string> > * {
93 PyObject* NewDict = PyDict_New(); // The result of this typemap
94 PyObject* NewKey = (PyObject*)0;
95 PyObject* NewVal = (PyObject*)0;
97 for (std::map<std::string,
98 std::list<std::string> >::iterator tag = ($1)->begin();
99 tag != ($1)->end(); ++tag)
101 std::string first = tag->first;
102 // Do not publish entries whose keys is made of spaces
103 if (first.length() == 0)
105 NewKey = PyString_FromString(first.c_str());
106 PyObject* NewList = PyList_New(0);
107 for (std::list<std::string>::iterator Item = tag->second.begin();
108 Item != tag->second.end();
111 NewVal = PyString_FromString(Item->c_str());
112 PyList_Append( NewList, NewVal);
114 PyDict_SetItem( NewDict, NewKey, NewList);
119 ////////////////////////////////////////////////////////////////////////////
120 // Convert a c++ hash table in a python native dictionary
121 %typemap(out) gdcm::TagDocEntryHT & {
122 PyObject* NewDict = PyDict_New(); // The result of this typemap
123 std::string RawName; // Element name as gotten from gdcm
124 PyObject* NewKey = (PyObject*)0; // Associated name as python object
125 std::string RawValue; // Element value as gotten from gdcm
126 PyObject* NewVal = (PyObject*)0; // Associated value as python object
128 for (gdcm::TagDocEntryHT::iterator tag = $1->begin(); tag != $1->end(); ++tag)
130 // The element name shall be the key:
131 RawName = tag->second->GetName();
132 // gdcm unrecognized (including not loaded because their size exceeds
133 // the user specified treshold) elements are exported with their
135 if (RawName == "Unknown")
136 RawName = tag->second->GetKey();
137 NewKey = PyString_FromString(RawName.c_str());
139 // Element values are striped from leading/trailing spaces
140 if (gdcm::ValEntry* ValEntryPtr =
141 dynamic_cast< gdcm::ValEntry* >(tag->second) )
143 RawValue = ValEntryPtr->GetValue();
147 EatLeadingAndTrailingSpaces(RawValue);
148 NewVal = PyString_FromString(RawValue.c_str());
149 PyDict_SetItem( NewDict, NewKey, NewVal);
154 ////////////////////////////////////////////////////////////////////////////
155 %typemap(out) ListDicomDirPatient & {
156 PyObject* NewItem = (PyObject*)0;
157 $result = PyList_New(0); // The result of this typemap
159 for (std::list<gdcm::DicomDirPatient *>::iterator New = ($1)->begin();
160 New != ($1)->end(); ++New) {
161 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirPatient,1);
162 PyList_Append($result, NewItem);
166 %typemap(out) ListDicomDirStudy & {
167 PyObject* NewItem = (PyObject*)0;
168 $result = PyList_New(0); // The result of this typemap
170 for (std::list<gdcm::DicomDirStudy *>::iterator New = ($1)->begin();
171 New != ($1)->end(); ++New) {
172 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirStudy,1);
173 PyList_Append($result, NewItem);
177 %typemap(out) ListDicomDirSerie & {
178 PyObject* NewItem = (PyObject*)0;
179 $result = PyList_New(0); // The result of this typemap
181 for (std::list<gdcm::DicomDirSerie *>::iterator New = ($1)->begin();
182 New != ($1)->end(); ++New) {
183 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirSerie,1);
184 PyList_Append($result, NewItem);
188 %typemap(out) ListDicomDirImage & {
189 PyObject* NewItem = (PyObject*)0;
190 $result = PyList_New(0); // The result of this typemap
192 for (std::list<gdcm::DicomDirImage *>::iterator New = ($1)->begin();
193 New != ($1)->end(); ++New) {
194 NewItem = SWIG_NewPointerObj(*New,SWIGTYPE_p_DicomDirImage,1);
195 PyList_Append($result, NewItem);
199 ////////////////////////////////////////////////////////////////////////////
200 // Deals with function returning a C++ string.
201 %typemap(python, in) (gdcm::Method *,void * =NULL,gdcm::Method * =NULL) {
205 $1=gdcmPythonVoidFunc;
207 $3=gdcmPythonVoidFuncArgDelete;
218 ////////////////////////////////////////////////////////////////////////////
219 // Deals with function returning a C++ string.
220 %typemap(out) string, std::string {
221 $result = PyString_FromString(($1).c_str());
224 %typemap(python, in) const std::string, std::string
226 $1 = PyString_AsString($input);
229 ////////////////////////////////////////////////////////////////////////////
230 // Warning: Order matters !
231 %include "gdcmCommon.h"
232 %include "gdcmRLEFramesInfo.h"
233 %include "gdcmJPEGFragmentsInfo.h"
234 %include "gdcmDictEntry.h"
235 %include "gdcmDict.h"
236 %include "gdcmDocEntry.h"
237 %include "gdcmDocEntrySet.h"
238 %include "gdcmElementSet.h"
239 %include "gdcmDictSet.h"
242 %include "gdcmSQItem.h"
243 %include "gdcmDicomDirElement.h"
244 %include "gdcmDicomDirObject.h"
245 %include "gdcmDicomDirImage.h"
246 %include "gdcmDicomDirSerie.h"
247 %include "gdcmDicomDirStudy.h"
248 %include "gdcmDicomDirPatient.h"
249 %include "gdcmDicomDirMeta.h"
250 %include "gdcmDocument.h"
251 %include "gdcmHeader.h"
252 %include "gdcmHeaderHelper.h"
253 %include "gdcmFile.h"
254 %include "gdcmUtil.h"
255 %include "gdcmGlobal.h"
256 %include "gdcmDicomDir.h"