]> Creatis software - gdcm.git/blob - gdcmPython/gdcm.i
* src/gdcm.h splitted in gdcmCommon.h, gdcmDict.h, gdcmDictEntry.h,
[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 %typemap(out) list<string> * {
24         PyObject* NewItem = (PyObject*)0;
25         PyObject* NewList = PyList_New(0); // The result of this typemap
26         for (list<string>::iterator NewString = ($1)->begin();
27              NewString != ($1)->end(); ++NewString) {
28                 NewItem = PyString_FromString(NewString->c_str());
29                 PyList_Append( NewList, NewItem);
30         }
31         $result = NewList;
32 }
33
34 // Convert a c++ hash table in a python native dictionary
35 %typemap(out) map<string, list<string> > * {
36         PyObject* NewDict = PyDict_New(); // The result of this typemap
37         PyObject* NewKey = (PyObject*)0;
38         PyObject* NewVal = (PyObject*)0;
39
40         for (map<string, list<string> >::iterator tag = ($1)->begin();
41              tag != ($1)->end(); ++tag) {
42       string first = tag->first;
43       // Do not publish entries whose keys is made of spaces
44       if (first.length() == 0)
45          continue;
46                 NewKey = PyString_FromString(first.c_str());
47                 PyObject* NewList = PyList_New(0);
48                 for (list<string>::iterator Item = tag->second.begin();
49                      Item != tag->second.end(); ++Item) {
50                         NewVal = PyString_FromString(Item->c_str());
51                         PyList_Append( NewList, NewVal);
52                 }
53                 PyDict_SetItem( NewDict, NewKey, NewList);
54         }
55         $result = NewDict;
56 }
57
58 // Convert a c++ hash table in a python native dictionary
59 %typemap(out) TagElValueHT & {
60         PyObject* NewDict = PyDict_New(); // The result of this typemap
61         string RawName;                   // Element name as gotten from gdcm
62         PyObject* NewKey = (PyObject*)0;  // Associated name as python object
63         string RawValue;                  // Element value as gotten from gdcm
64         PyObject* NewVal = (PyObject*)0;  // Associated value as python object
65
66         for (TagElValueHT::iterator tag = $1->begin(); tag != $1->end(); ++tag) {
67
68                 // The element name shall be the key:
69                 RawName = tag->second->GetName();
70                 // gdcm unrecognized (including not loaded because their size exceeds
71                 // the user specified treshold) elements are exported with their
72                 // TagKey as key.
73                 if (RawName == "Unknown")
74                         RawName = tag->second->GetKey();
75                 NewKey = PyString_FromString(RawName.c_str());
76
77                 // Element values are striped from leading/trailing spaces
78                 RawValue = tag->second->GetValue();
79                 EatLeadingAndTrailingSpaces(RawValue);
80                 NewVal = PyString_FromString(RawValue.c_str());
81
82                 PyDict_SetItem( NewDict, NewKey, NewVal);
83     }
84         $result = NewDict;
85 }
86
87 %include "gdcmCommon.h"
88 %include "gdcmDictEntry.h"
89 %include "gdcmDict.h"
90 %include "gdcmDictSet.h"
91 %include "gdcmElValue.h"
92 %include "gdcmElValSet.h"
93 %include "gdcmHeader.h"
94 %include "gdcmFile.h"
95