]> Creatis software - gdcm.git/blob - src/gdcmElValSet.cxx
Ajout fonction
[gdcm.git] / src / gdcmElValSet.cxx
1 // gdcmElValSet.cxx
2
3 #include "gdcm.h"
4 #include "gdcmUtil.h"
5
6 TagElValueHT & ElValSet::GetTagHt(void) {
7         return tagHt;
8 }
9
10 void ElValSet::Add(ElValue * newElValue) {
11         tagHt [newElValue->GetKey()]  = newElValue;
12         NameHt[newElValue->GetName()] = newElValue;
13 }
14
15 void ElValSet::Print(ostream & os) {
16         for (TagElValueHT::iterator tag = tagHt.begin();
17                   tag != tagHt.end();
18                   ++tag){
19                 os << tag->first << ": ";
20                 os << "[" << tag->second->GetValue() << "]";
21                 os << "[" << tag->second->GetName()  << "]";
22                 os << "[" << tag->second->GetVR()    << "]" << endl;
23         }
24 }
25  
26
27 int ElValSet::Write(FILE * _fp) {
28
29 // ATTENTION : fonction non terminée (commitée a titre de precaution)
30
31         guint16 gr, el;
32         guint32 lgr;
33         
34         const char * val;
35         string vr;
36         guint32 val_int32;
37         guint16 val_int16;
38         void *ptr;
39         
40         
41         // A FAIRE :
42         // parcourir la table pour recalculer la longueur des 'elements 0x0000'
43         // au cas ou un tag ai été ajouté par rapport à ce qui a été lu
44         // dans l'image native
45         //
46         // cf : code IdDcmWriteFile
47         
48 /*      
49         for (TagElValueHT::iterator tag = tagHt.begin();
50                   tag != tagHt.end();
51                   ++tag){
52                   
53                 / ...
54
55         }
56 */
57         // resteront à tester les echecs en écriture
58         
59         for (TagElValueHT::iterator tag = tagHt.begin();
60                   tag != tagHt.end();
61                   ++tag){
62
63                 // Question :
64                 // peut-on se passer des affectations?
65                 // - passer l'adresse du resultat d'une fonction (???)
66                 // - acceder au champ sans passer par un accesseur ?
67                 
68                 gr =  tag->second->GetGroup();
69                 el =  tag->second->GetElement();
70                 lgr = tag->second->GetLength();
71                 val = tag->second->GetValue().c_str();
72                 vr =  tag->second->GetVR();
73                         
74                 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp);        //group
75                 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp);        //element
76                 
77                 //fwrite ( vr,(size_t)2 ,(size_t)1 ,_fp);       //VR
78                 // voir pb lgr  + VR
79                 // On fait de l'implicit VR (penser a forcer le SYNTAX TRANSFERT UID)
80                 
81                 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);                       //lgr
82                 
83                 if (vr == "US" || vr == "SS") {
84                         val_int16 = atoi(val);
85                         ptr = &val_int16;
86                         fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);        
87                         continue;
88                 }
89                 if (vr == "UL" || vr == "SL") {
90                         val_int32 = atoi(val);
91                         ptr = &val_int32;
92                         fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);        
93                         continue;
94                 }       
95                 
96                 // Les pixels ne sont pas chargés dans l'element !
97                 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
98
99                 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
100         }
101                 
102         return(1);
103 }
104
105
106
107 void ElValSet::PrintByName(ostream & os) {
108         for (TagElValueNameHT::iterator tag = NameHt.begin();
109                   tag != NameHt.end();
110                   ++tag){
111                 os << tag->first << ": ";
112                 os << "[" << tag->second->GetValue() << "]";
113                 os << "[" << tag->second->GetKey()   << "]";
114                 os << "[" << tag->second->GetVR()    << "]" << endl;
115         }
116 }
117
118 ElValue* ElValSet::GetElementByNumber(guint32 group, guint32 element) {
119         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
120         if ( ! tagHt.count(key))
121                 return (ElValue*)0;
122         if (tagHt.count(key) > 1)
123                 dbg.Verbose(0, "ElValSet::GetElementByNumber",
124                             "multiple entries for this key (FIXME) !");
125         return tagHt.find(key)->second;
126 }
127
128 ElValue* ElValSet::GetElementByName(string TagName) {
129    if ( ! NameHt.count(TagName))
130       return (ElValue*)0;
131    if (NameHt.count(TagName) > 1)
132       dbg.Verbose(0, "ElValSet::GetElement",
133                   "multipe entries for this key (FIXME) !");
134    return NameHt.find(TagName)->second;
135 }
136
137 string ElValSet::GetElValueByNumber(guint32 group, guint32 element) {
138         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
139         if ( ! tagHt.count(key))
140                 return "gdcm::Unfound";
141         if (tagHt.count(key) > 1)
142                 dbg.Verbose(0, "ElValSet::GetElValueByNumber",
143                             "multiple entries for this key (FIXME) !");
144         return tagHt.find(key)->second->GetValue();
145 }
146
147 int ElValSet::SetElValueByNumber(string content, guint32 group, guint32 element) {
148         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
149         if ( ! tagHt.count(key))
150                 return 0;
151         if (tagHt.count(key) > 1) {
152                 dbg.Verbose(0, "ElValSet::SetElValueByNumber",
153                             "multiple entries for this key (FIXME) !");
154                 return (0); 
155         }
156                                        
157         tagHt[key]->SetValue(content);
158         return(1);              
159 }
160
161 string ElValSet::GetElValueByName(string TagName) {
162         if ( ! NameHt.count(TagName))
163                 return "gdcm::Unfound";
164         if (NameHt.count(TagName) > 1)
165                 dbg.Verbose(0, "ElValSet::GetElValue",
166                             "multipe entries for this key (FIXME) !");
167         return NameHt.find(TagName)->second->GetValue();
168 }
169
170 int ElValSet::SetElValueByName(string content, string TagName) {
171         if ( ! NameHt.count(TagName))
172                 return 0;
173         if (NameHt.count(TagName) > 1) {
174                 dbg.Verbose(0, "ElValSet::SetElValue",
175                             "multipe entries for this key (FIXME) !");
176                 return 0;
177         }
178         NameHt.find(TagName)->second->SetValue(content);
179 }
180
181