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