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