]> Creatis software - gdcm.git/blob - gdcmPython/gdcm.i
41d5ee2080708e256e2da6137d9f51081a64ed03
[gdcm.git] / gdcmPython / gdcm.i
1 %module gdcm
2 %{
3 #include "gdcmCommon.h"
4 #include "gdcmDictEntry.h"
5 #include "gdcmDict.h"
6 #include "gdcmDictSet.h"
7 #include "gdcmElValue.h"
8 #include "gdcmElValSet.h"
9 #include "gdcmHeader.h"
10 #include "gdcmFile.h"
11
12 // Utility functions on strings for removing leading and trailing spaces
13 void EatLeadingAndTrailingSpaces(string & s) {
14         while ( s.length() && (s[0] == ' ') )
15                 s.erase(0,1);
16         while ( s.length() && (s[s.length()-1] == ' ') )
17                 s.erase(s.length()-1, 1);
18 }
19 %}
20 typedef  unsigned short guint16;
21 typedef  unsigned int guint32;
22
23 ////////////////////////////////////////////////////////////////////////////
24 %typemap(out) list<string> * {
25         PyObject* NewItem = (PyObject*)0;
26         PyObject* NewList = PyList_New(0); // The result of this typemap
27         for (list<string>::iterator NewString = ($1)->begin();
28              NewString != ($1)->end(); ++NewString) {
29                 NewItem = PyString_FromString(NewString->c_str());
30                 PyList_Append( NewList, NewItem);
31         }
32         $result = NewList;
33 }
34
35 ////////////////////////////////////////////////////////////////////////////
36 // Convert a c++ hash table in a python native dictionary
37 %typemap(out) map<string, list<string> > * {
38         PyObject* NewDict = PyDict_New(); // The result of this typemap
39         PyObject* NewKey = (PyObject*)0;
40         PyObject* NewVal = (PyObject*)0;
41
42         for (map<string, list<string> >::iterator tag = ($1)->begin();
43              tag != ($1)->end(); ++tag) {
44       string first = tag->first;
45       // Do not publish entries whose keys is made of spaces
46       if (first.length() == 0)
47          continue;
48                 NewKey = PyString_FromString(first.c_str());
49                 PyObject* NewList = PyList_New(0);
50                 for (list<string>::iterator Item = tag->second.begin();
51                      Item != tag->second.end(); ++Item) {
52                         NewVal = PyString_FromString(Item->c_str());
53                         PyList_Append( NewList, NewVal);
54                 }
55                 PyDict_SetItem( NewDict, NewKey, NewList);
56         }
57         $result = NewDict;
58 }
59
60 ////////////////////////////////////////////////////////////////////////////
61 // Convert a c++ hash table in a python native dictionary
62 %typemap(out) TagElValueHT & {
63         PyObject* NewDict = PyDict_New(); // The result of this typemap
64         string RawName;                   // Element name as gotten from gdcm
65         PyObject* NewKey = (PyObject*)0;  // Associated name as python object
66         string RawValue;                  // Element value as gotten from gdcm
67         PyObject* NewVal = (PyObject*)0;  // Associated value as python object
68
69         for (TagElValueHT::iterator tag = $1->begin(); tag != $1->end(); ++tag) {
70
71                 // The element name shall be the key:
72                 RawName = tag->second->GetName();
73                 // gdcm unrecognized (including not loaded because their size exceeds
74                 // the user specified treshold) elements are exported with their
75                 // TagKey as key.
76                 if (RawName == "Unknown")
77                         RawName = tag->second->GetKey();
78                 NewKey = PyString_FromString(RawName.c_str());
79
80                 // Element values are striped from leading/trailing spaces
81                 RawValue = tag->second->GetValue();
82                 EatLeadingAndTrailingSpaces(RawValue);
83                 NewVal = PyString_FromString(RawValue.c_str());
84
85                 PyDict_SetItem( NewDict, NewKey, NewVal);
86     }
87         $result = NewDict;
88 }
89
90 ////////////////////////////////////////////////////////////////////////////
91 // Deals with function returning a C++ string.
92 %typemap(out) string  {
93     $result = PyString_FromString(($1).c_str());
94 }
95
96 ////////////////////////////////////////////////////////////////////////////
97 %include "gdcmCommon.h"
98 %include "gdcmDictEntry.h"
99 %include "gdcmDict.h"
100 %include "gdcmDictSet.h"
101 %include "gdcmElValue.h"
102 %include "gdcmElValSet.h"
103 %include "gdcmHeader.h"
104 %include "gdcmFile.h"
105