]> Creatis software - gdcm.git/blob - src/gdcmElValSet.cxx
aef6800ffec75fcb544a0543a8520bb44a4e1fb3
[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 void ElValSet::PrintByName(ostream & os) {
27         for (TagElValueNameHT::iterator tag = NameHt.begin();
28                   tag != NameHt.end();
29                   ++tag){
30                 os << tag->first << ": ";
31                 os << "[" << tag->second->GetValue() << "]";
32                 os << "[" << tag->second->GetKey()   << "]";
33                 os << "[" << tag->second->GetVR()    << "]" << endl;
34         }
35 }
36
37 ElValue* ElValSet::GetElementByNumber(guint32 group, guint32 element) {
38         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
39         if ( ! tagHt.count(key))
40                 return (ElValue*)0;
41         if (tagHt.count(key) > 1)
42                 dbg.Verbose(0, "ElValSet::GetElementByNumber",
43                             "multiple entries for this key (FIXME) !");
44         return tagHt.find(key)->second;
45 }
46
47 ElValue* ElValSet::GetElementByName(string TagName) {
48    if ( ! NameHt.count(TagName))
49       return (ElValue*)0;
50    if (NameHt.count(TagName) > 1)
51       dbg.Verbose(0, "ElValSet::GetElement",
52                   "multipe entries for this key (FIXME) !");
53    return NameHt.find(TagName)->second;
54 }
55
56 string ElValSet::GetElValueByNumber(guint32 group, guint32 element) {
57         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
58         if ( ! tagHt.count(key))
59                 return "gdcm::Unfound";
60         if (tagHt.count(key) > 1)
61                 dbg.Verbose(0, "ElValSet::GetElValueByNumber",
62                             "multiple entries for this key (FIXME) !");
63         return tagHt.find(key)->second->GetValue();
64 }
65
66 string ElValSet::GetElValueByName(string TagName) {
67         if ( ! NameHt.count(TagName))
68                 return "gdcm::Unfound";
69         if (NameHt.count(TagName) > 1)
70                 dbg.Verbose(0, "ElValSet::GetElValue",
71                             "multipe entries for this key (FIXME) !");
72         return NameHt.find(TagName)->second->GetValue();
73 }
74
75 int ElValSet::SetElValueByNumber(string content, guint32 group, guint32 element) {
76         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
77         if ( ! tagHt.count(key))
78                 return 0;
79         if (tagHt.count(key) > 1) {
80                 dbg.Verbose(0, "ElValSet::SetElValueByNumber",
81                             "multiple entries for this key (FIXME) !");
82                 return (0); 
83         }                                      
84         tagHt[key]->SetValue(content);
85         // FIXME should we really update the element length ?
86         tagHt[key]->SetLength(content.length());         
87         return(1);              
88 }
89
90
91 int ElValSet::SetElValueByName(string content, string TagName) {
92         if ( ! NameHt.count(TagName))
93                 return 0;
94         if (NameHt.count(TagName) > 1) {
95                 dbg.Verbose(0, "ElValSet::SetElValue",
96                             "multipe entries for this key (FIXME) !");
97                 return 0;
98         }
99         NameHt.find(TagName)->second->SetValue(content);
100         NameHt.find(TagName)->second->SetLength(strlen(content.c_str()));        
101         return(1);              
102 }
103
104
105
106 int ElValSet::Write(FILE * _fp) {
107
108 // ATTENTION : fonction non terminée (commitée a titre de precaution)
109
110         guint16 gr, el;
111         guint32 lgr;
112         const char * val;
113         string vr;
114         guint32 val_int32;
115         guint16 val_int16;
116         void *ptr;
117         
118         string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
119         
120 /*      // Utilisées pour le calcul Group Length
121         int deja = 0;
122         guint32 lgrCalcGroupe;
123         ElValue * elemZ, *elemZPrec;
124         guint16 grCourant = 0;
125         
126 */
127
128         // Question :
129         // Comment pourrait-on tester si on est TRueDicom ou non ,
130         // (FileType est un champ de gdcmHeader ...)
131         //
132
133         // On parcourt la table pour recalculer la longueur des 'elements 0x0000'
134         // au cas ou un tag ai été ajouté par rapport à ce qui a été lu
135         // dans l'image native
136         //
137         // cf : code IdDcmWriteFile
138
139 /*
140                 // Pas le temps de finir 
141                 // voir libido/src/dcmwrite.c
142                 //
143                 // mais avant ... voir si le 'group length', lorsqu'il est present
144                 // sert encore a qq chose
145                 // patcher une image DICOM, mettre une lgr erronnée
146                 // et voir si e-film la reconnait ...
147                 
148                 
149         for (TagElValueHT::iterator tag = tagHt.begin();
150                   tag != tagHt.end();
151                   ++tag){
152                   
153                 elemZ = tag->second;
154         
155                 if ( (elemZ->GetGroup() != grCourant) &&          
156                          (elemZ->GetGroup() != 0xfffe)  ) {     // On arrive sur un nv Groupe
157                          
158                         if(elemZ->GetNum != 0x0000) {   // pas d'element 'Lgr groupe'
159                                 // On le crée
160                                 gdcmDictEntry * tagZ = IsInDicts(tag->second->GetGroup(), 0);
161                                 elemZ = new (ElValue(tagZ)); // on le cree
162                                 elemZ.SetLength(4);
163                                 Add(elemZ);                                      // On l'accroche à sa place    
164                         }       
165                         
166                         if (deja) {
167                         
168                         // A FINIR
169                         }
170                         deja = 1;
171                         elemZPrec = elemZ;
172                         grCourant = elemZ->GetGroup();
173                         lgrCalcGroupe =  12; //2 + 2 + 4 + 4; // lgr (Gr + Num + LgrElem + LgrGroupe)
174                                                         
175                 } else {                // On n'EST PAS sur un nv Groupe
176                 
177                         lgrCalcGroupe += 2 + 2;  // lgr (Gr + Num )
178                 
179                         if (IsVrUnknowkn()) {
180                         
181                         // A FINIR
182                         
183                         }
184                         
185                         // A FINIR        
186         }
187
188 */
189         
190         // Si on fait de l'implicit VR littele Endian 
191         // (pour moins se fairche sur processeur INTEL)
192         // penser a forcer le SYNTAX TRANSFERT UID
193                                 
194         SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);  
195                 
196         // restent à tester les echecs en écriture (apres chaque fwrite)
197         
198         for (TagElValueHT::iterator tag = tagHt.begin();
199                   tag != tagHt.end();
200                   ++tag){
201
202                 // Question :
203                 // peut-on se passer des affectations?
204                 // - passer l'adresse du resultat d'une fonction (???)
205                 // - acceder au champ sans passer par un accesseur ?
206                 
207                 gr =  tag->second->GetGroup();
208                 el =  tag->second->GetElement();
209                 lgr = tag->second->GetLength();
210                 val = tag->second->GetValue().c_str();
211                 vr =  tag->second->GetVR();
212                         
213                 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp);        //group
214                 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp);        //element
215                 
216                 //fwrite ( vr,(size_t)2 ,(size_t)1 ,_fp);       //VR
217                 
218                 // si on n'est pas en IMPLICIT VR voir pb (lgr  + VR)
219                 
220                 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);                       //lgr
221                 
222                 if (vr == "US" || vr == "SS") {
223                         val_int16 = atoi(val);
224                         ptr = &val_int16;
225                         fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);        
226                         continue;
227                 }
228                 if (vr == "UL" || vr == "SL") {
229                         val_int32 = atoi(val);
230                         ptr = &val_int32;
231                         fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);        
232                         continue;
233                 }       
234                 
235                 // Les pixels ne sont pas chargés dans l'element !
236                 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
237
238                 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
239         }
240                 
241         return(1);
242 }