]> Creatis software - gdcm.git/blob - src/gdcmElValSet.cxx
* gdcm/Doc many doxygen changes:
[gdcm.git] / src / gdcmElValSet.cxx
1 // gdcmElValSet.cxx
2
3 #include <sstream>
4 #include "gdcmUtil.h"
5 #include "gdcmElValSet.h"
6
7
8 TagElValueHT & gdcmElValSet::GetTagHt(void) {
9         return tagHt;
10 }
11
12 void gdcmElValSet::Add(gdcmElValue * newElValue) {
13         tagHt [newElValue->GetKey()]  = newElValue;
14         NameHt[newElValue->GetName()] = newElValue;
15 }
16
17 // TODO : faire un gdcmElValSet::ReplaceOrCreate qui remplace si ça existe, qui cree sinon
18
19 void gdcmElValSet::ReplaceOrCreate(gdcmElValue * newElValue) {
20
21         TagKey key = newElValue->GetKey();
22
23         if (tagHt.count(key) > 1)
24                 dbg.Verbose(0, "gdcmElValSet::GetElValueByNumber",
25                             "multiple entries for this key (FIXME) !");
26                             
27         if (tagHt.count(key)) {
28                 tagHt.erase(key);
29                 tagHt.erase(newElValue->GetName());
30         }
31
32         tagHt [key]                   = newElValue;
33         NameHt[newElValue->GetName()] = newElValue;
34 }
35
36
37 void gdcmElValSet::Print(ostream & os) {
38         for (TagElValueHT::iterator tag = tagHt.begin();
39                   tag != tagHt.end();
40                   ++tag){
41                 os << tag->first << ": ";
42                 os << "[" << tag->second->GetValue() << "]";
43                 os << "[" << tag->second->GetName()  << "]";
44                 os << "[" << tag->second->GetVR()    << "]"; 
45                 os << " lgr : " << tag->second->GetLength();
46                                                 os << endl;
47         }
48
49
50 void gdcmElValSet::PrintByName(ostream & os) {
51         for (TagElValueNameHT::iterator tag = NameHt.begin();
52                   tag != NameHt.end();
53                   ++tag){
54                 os << tag->first << ": ";
55                 os << "[" << tag->second->GetValue() << "]";
56                 os << "[" << tag->second->GetKey()   << "]";
57                 os << "[" << tag->second->GetVR()    << "]" << endl;
58         }
59 }
60
61 gdcmElValue* gdcmElValSet::GetElementByNumber(guint32 group, guint32 element) {
62         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
63         if ( ! tagHt.count(key))
64                 return (gdcmElValue*)0;
65         if (tagHt.count(key) > 1)
66                 dbg.Verbose(0, "gdcmElValSet::GetElementByNumber",
67                             "multiple entries for this key (FIXME) !");
68         return tagHt.find(key)->second;
69 }
70
71 gdcmElValue* gdcmElValSet::GetElementByName(string TagName) {
72    if ( ! NameHt.count(TagName))
73       return (gdcmElValue*)0;
74    if (NameHt.count(TagName) > 1)
75       dbg.Verbose(0, "gdcmElValSet::GetElement",
76                   "multipe entries for this key (FIXME) !");
77    return NameHt.find(TagName)->second;
78 }
79
80 string gdcmElValSet::GetElValueByNumber(guint32 group, guint32 element) {
81         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
82         if ( ! tagHt.count(key))
83                 return "gdcm::Unfound";
84         if (tagHt.count(key) > 1)
85                 dbg.Verbose(0, "gdcmElValSet::GetElValueByNumber",
86                             "multiple entries for this key (FIXME) !");
87         return tagHt.find(key)->second->GetValue();
88 }
89
90 string gdcmElValSet::GetElValueByName(string TagName) {
91         if ( ! NameHt.count(TagName))
92                 return "gdcm::Unfound";
93         if (NameHt.count(TagName) > 1)
94                 dbg.Verbose(0, "gdcmElValSet::GetElValue",
95                             "multipe entries for this key (FIXME) !");
96         return NameHt.find(TagName)->second->GetValue();
97 }
98
99 int gdcmElValSet::SetElValueByNumber(string content,
100                                      guint32 group, guint32 element) {
101         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
102         if ( ! tagHt.count(key))
103                 return 0;
104         if (tagHt.count(key) > 1) {
105                 dbg.Verbose(0, "gdcmElValSet::SetElValueByNumber",
106                             "multiple entries for this key (FIXME) !");
107                 return (0); 
108         }                                      
109         tagHt[key]->SetValue(content);
110
111         // Question : m à j LgrElem ?
112         tagHt[key]->SetLength(strlen(content.c_str()));  
113
114         // FIXME should we really update the element length ?
115         tagHt[key]->SetLength(content.length());         
116
117         return(1);              
118 }
119
120 int gdcmElValSet::SetElValueByName(string content, string TagName) {
121         if ( ! NameHt.count(TagName))
122                 return 0;
123         if (NameHt.count(TagName) > 1) {
124                 dbg.Verbose(0, "gdcmElValSet::SetElValueByName",
125                             "multipe entries for this key (FIXME) !");
126                 return 0;
127         }
128         NameHt.find(TagName)->second->SetValue(content);
129         NameHt.find(TagName)->second->SetLength(strlen(content.c_str()));        
130         return(1);              
131 }
132
133 /**
134  * \ingroup  gdcmElValSet
135  * \brief    Generate a free TagKey i.e. a TagKey that is not present
136  *           in the TagHt dictionary. One of the potential usage is
137  *           to add  gdcm generated additional informartion to the ElValSet
138  *           (see gdcmHeader::AddAndDefaultElements).
139  * @param group The generated tag must belong to this group.  
140  * @return   The element of tag with given group which is fee.
141  */
142 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint32 group) {
143    for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
144       TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
145       if (tagHt.count(key) == 0)
146          return elem;
147    }
148    return UINT32_MAX;
149 }
150
151 int gdcmElValSet::SetElValueLengthByNumber(guint32 l,
152                                            guint32 group, guint32 element) {
153         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
154         if ( ! tagHt.count(key))
155                 return 0;
156         tagHt[key]->SetLength(l);        
157         return 1 ;              
158 }
159
160
161 int gdcmElValSet::SetElValueLengthByName(guint32 l, string TagName) {
162         if ( ! NameHt.count(TagName))
163                 return 0;
164         NameHt.find(TagName)->second->SetLength(l);      
165         return 1 ;              
166 }
167
168 // Sorry for the DEBUG's, but tomorow is gonna be hoter than today
169 #define DEBUG 0
170
171 int gdcmElValSet::Write(FILE * _fp) {
172
173 // ATTENTION : fonction non terminée (commitée a titre de precaution)
174
175         guint16 gr, el;
176         guint32 lgr;
177         const char * val;
178         string vr;
179         guint32 val_uint32;
180         gint32  val_int32;
181         guint16 val_uint16;
182         gint16  val_int16;;
183         
184         vector<string> tokens;
185         
186         void *ptr;
187         char str_lgrCalcGroupe[10];
188
189         
190         string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
191         
192         // Utilisées pour le calcul Group Length
193         int deja = 0, prem=0;
194         guint32 lgrCalcGroupe=0;
195         gdcmElValue *elem, *elemZ, *elemZPrec;
196         guint16 grCourant = 0;
197         
198         // Question :
199         // Comment pourrait-on tester si on est TrueDicom ou non ,
200         // (FileType est un champ de gdcmHeader ...)
201         //
202
203         // On parcourt la table pour recalculer la longueur des 'elements 0x0000'
204         // au cas ou un tag ai été ajouté par rapport à ce qui a été lu
205         // dans l'image native
206         //
207         // cf : code IdDcmWriteFile dans libido/src/dcmwrite.c
208                 
209 if (1) {  // Risque de pb dans le calcul des lgr de chaque groupe. On le saute pour le moment!
210                 
211         // On fait de l'implicit VR little Endian 
212         // (pour moins se fairche sur processeur INTEL)
213         // On force le TRANSFERT SYNTAX UID
214                                 
215         SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);  
216         SetElValueLengthByNumber(18, 0x0002, 0x0010);  // Le 0 de fin de chaine doit etre stocké, dans ce cas   
217                         
218         TagElValueHT::iterator tag = tagHt.begin();
219         
220         elem = tag->second;
221         gr   = elem->GetGroup();
222         el   = elem->GetElement();
223                         
224         if (el != 0x0000) {
225                 if(DEBUG)printf("ajout elem OOOO premiere fois\n");
226                 gdcmDictEntry * tagZ = new gdcmDictEntry(gr, 0x0000, "UL");
227                 elemZPrec = new gdcmElValue(tagZ);      // on le cree
228                 elemZPrec->SetLength(4);
229                 Add(elemZPrec);                         // On l'accroche à sa place
230         } else {
231                 elemZPrec = elem;
232                 if(DEBUG)printf("Pas d'ajout elem OOOO premiere fois\n");
233         }
234         lgrCalcGroupe = 0;
235         if(DEBUG)printf("init-1 lgr (%d) pour gr %04x\n",lgrCalcGroupe, gr);
236         grCourant = gr;
237         
238         for (tag = ++tagHt.begin();
239                   tag != tagHt.end();
240                   ++tag){
241                   
242                 elem = tag->second;
243                 gr = elem->GetGroup();
244                 el = elem->GetElement();
245
246                 if ( (gr != grCourant) /*&&     // On arrive sur un nv Groupe     
247                      (el != 0xfffe) */  ) {
248                             
249                         if (el != 0x0000) {
250                                 gdcmDictEntry * tagZ = new gdcmDictEntry(gr, 0x0000, "UL");
251                                 elemZ = new gdcmElValue(tagZ); // on le cree
252                                 elemZ->SetLength(4);
253                                 Add(elemZ);                     // On l'accroche à sa place 
254                                 if(DEBUG)printf("ajout elem OOOO pour gr %04x\n",gr);
255                         } else { 
256                                 elemZ=elem;
257                                 if(DEBUG)printf("maj elmeZ\n");
258                         }
259                         
260                         ostringstream fock;
261                         fock << lgrCalcGroupe; 
262                         //sprintf(str_lgrCalcGroupe,"%d",lgrCalcGroupe);
263                         elemZPrec->SetValue(fock.str());
264                         if(DEBUG)printf("ecriture lgr (%d, %s) pour gr %04x\n",lgrCalcGroupe, fock.str().c_str(), grCourant);
265                         if(DEBUG)printf ("%04x %04x [%s]\n",elemZPrec->GetGroup(), elemZPrec->GetElement(),elemZPrec->GetValue().c_str());
266                         if(DEBUG)cout << "Addresse elemZPrec " << elemZPrec<< endl;
267                         elemZPrec=elemZ;
268                         lgrCalcGroupe = 0;
269                         grCourant     = gr;     
270                         if(DEBUG)printf("init-2 lgr (%d) pour gr %04x\n",lgrCalcGroupe, gr);                    
271                 } else {                        // On n'EST PAS sur un nv Groupe
272                         lgrCalcGroupe += 2 + 2 + 4 + elem->GetLength();  // Gr + Num + Lgr + LgrElem
273                         if(DEBUG)printf("increment (%d) el %04x-->lgr (%d) pour gr %04x\n",elem->GetLength(), el, lgrCalcGroupe, gr);
274                 }               
275         }
276         
277 } // fin if (1)
278
279         
280         // restent à tester les echecs en écriture (apres chaque fwrite)
281         
282         for (TagElValueHT::iterator tag2 = tagHt.begin();
283                   tag2 != tagHt.end();
284                   ++tag2){
285                 
286                 gr =  tag2->second->GetGroup();
287                 el =  tag2->second->GetElement();
288                 lgr = tag2->second->GetLength();
289                 val = tag2->second->GetValue().c_str();
290                 vr =  tag2->second->GetVR();
291                 if(DEBUG)printf ("%04x %04x [%s] : [%s]\n",gr, el, vr.c_str(), val);
292                         
293                 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp);        //group
294                 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp);        //element
295                 
296                 //fwrite ( vr,(size_t)2 ,(size_t)1 ,_fp);       //VR
297                 
298                 // si on n'est pas en IMPLICIT VR voir pb (lgr  + VR)
299                 
300                 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);       //lgr
301                 
302                 tokens.erase(tokens.begin(),tokens.end());
303                 Tokenize (tag2->second->GetValue(), tokens, "\\");
304                 
305                 //if (tokens.size() > 1) { printf ("size : %d\n",tokens.size());}
306                 
307                 if (vr == "US" || vr == "SS") {
308                         for (unsigned int i=0; i<tokens.size();i++) {
309                                 val_uint16 = atoi(tokens[i].c_str());           
310                                 ptr = &val_uint16;
311                                 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
312                         }
313                         continue;       
314                 }
315                 if (vr == "UL" || vr == "SL") { 
316                         for (unsigned int i=0; i<tokens.size();i++) {
317                                 val_uint32 = atoi(tokens[i].c_str());           
318                                 ptr = &val_uint32;
319                                 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
320                         }
321                         continue;       
322                 }       
323                 
324                 // Les pixels ne sont pas chargés dans l'element !
325                 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
326
327                 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
328         }
329                 
330         return(1);
331 }
332
333
334
335
336 int gdcmElValSet::WriteAcr(FILE * _fp) {
337
338
339 // ATTENTION : fonction non terminée (commitée a titre de precaution)
340 // ATTENTION : fusioner le code avec celui de lValSet::Write
341
342
343         guint16 gr, el;
344         guint32 lgr;
345         const char * val;
346         string vr;
347         guint32 val_uint32;
348         gint32  val_int32;
349         guint16 val_uint16;
350         gint16  val_int16;
351         
352         vector<string> tokens;
353         
354         void *ptr;
355         char str_lgrCalcGroupe[10];
356         
357         //string implicitVRTransfertSyntax = "1.2.840.10008.1.2"; // supprime par rapport à Write
358         
359         // Utilisées pour le calcul Group Length
360         int deja = 0;
361         guint32 lgrCalcGroupe=0;
362         gdcmElValue *elem, *elemZ, *elemZPrec;
363         guint16 grCourant = 0;
364         
365         // Question :
366         // Comment pourrait-on tester si on est TrueDicom ou non ,
367         // (FileType est un champ de gdcmHeader ...)
368         //
369
370         // On parcourt la table pour recalculer la longueur des 'elements 0x0000'
371         // au cas ou un tag ai été ajouté par rapport à ce qui a été lu
372         // dans l'image native
373         //
374         // cf : code IdDcmWriteFile dans libido/src/dcmwrite.c
375                 
376
377                         
378         TagElValueHT::iterator tag = tagHt.begin();
379         
380         elem = tag->second;
381         gr   = elem->GetGroup();
382         el   = elem->GetElement();
383                         
384         if (el != 0x0000) {
385                 if(DEBUG)printf("ajout elem OOOO premiere fois\n");
386                 gdcmDictEntry * tagZ = new gdcmDictEntry(gr, 0x0000, "UL");
387                 elemZPrec = new gdcmElValue(tagZ);      // on le cree
388                 elemZPrec->SetLength(4);
389                 Add(elemZPrec);                         // On l'accroche à sa place
390         } else {
391                 elemZPrec = elem;
392                 if(DEBUG)printf("Pas d'ajout elem OOOO premiere fois\n");
393         }
394         lgrCalcGroupe = 0;
395         if(DEBUG)printf("init-1 lgr (%d) pour gr %04x\n",lgrCalcGroupe, gr);
396         grCourant = gr;
397         
398         for (tag = ++tagHt.begin();
399                   tag != tagHt.end();
400                   ++tag){
401                   
402                 elem = tag->second;
403                 gr = elem->GetGroup();
404                 el = elem->GetElement();
405
406                 if ( (gr != grCourant) /*&&     // On arrive sur un nv Groupe     
407                      (el != 0xfffe) */  ) {
408                             
409                         if (el != 0x0000) {
410                                 gdcmDictEntry * tagZ = new gdcmDictEntry(gr, 0x0000, "UL");
411                                 elemZ = new gdcmElValue(tagZ); // on le cree
412                                 elemZ->SetLength(4);
413                                 Add(elemZ);                     // On l'accroche à sa place 
414                                 if(DEBUG)printf("ajout elem OOOO pour gr %04x\n",gr);
415                         } else { 
416                                 elemZ=elem;
417                                 if(DEBUG)printf("maj elmeZ\n");
418                         }
419                         
420                         ostringstream fock;
421                         fock << lgrCalcGroupe; 
422                         //sprintf(str_lgrCalcGroupe,"%d",lgrCalcGroupe);
423                         elemZPrec->SetValue(fock.str());
424                         if(DEBUG)printf("ecriture lgr (%d, %s) pour gr %04x\n",lgrCalcGroupe, fock.str().c_str(), grCourant);
425                         if(DEBUG)printf ("%04x %04x [%s]\n",elemZPrec->GetGroup(), elemZPrec->GetElement(),elemZPrec->GetValue().c_str());
426                         if(DEBUG)cout << "Addresse elemZPrec " << elemZPrec<< endl;
427                         elemZPrec=elemZ;
428                         lgrCalcGroupe = 0;
429                         grCourant     = gr;     
430                         if(DEBUG)printf("init-2 lgr (%d) pour gr %04x\n",lgrCalcGroupe, gr);                    
431                 } else {                        // On n'EST PAS sur un nv Groupe
432                         lgrCalcGroupe += 2 + 2 + 4 + elem->GetLength();  // Gr + Num + Lgr + LgrElem
433                         if(DEBUG)printf("increment (%d) el %04x-->lgr (%d) pour gr %04x\n",elem->GetLength(), el, lgrCalcGroupe, gr);
434                 }               
435         }
436         
437         // Si on fait de l'implicit VR little Endian 
438         // (pour moins se fairche sur processeur INTEL)
439         // penser a forcer le TRANSFERT SYNTAX UID
440         
441         // supprime par rapport à Write         
442         //SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);        
443         //SetElValueLengthByNumber(18, 0x0002, 0x0010);  // Le 0 de fin de chaine doit etre stocké, dans ce cas 
444                 
445         // restent à tester les echecs en écriture (apres chaque fwrite)
446         
447         for (TagElValueHT::iterator tag2 = tagHt.begin();
448                   tag2 != tagHt.end();
449                   ++tag2){
450
451                 gr =  tag2->second->GetGroup();
452                                                 // saut des groupes speciaux DICOM V3
453                 if (gr < 0x0008) continue;      // ajouté par rapport à Write
454                                                 // saut des groupes impairs
455                 if (gr %2)       continue;      // ajouté par rapport à Write
456                 
457                 el =  tag2->second->GetElement();
458                 lgr = tag2->second->GetLength();
459                 val = tag2->second->GetValue().c_str();
460                 vr =  tag2->second->GetVR();
461                         
462                 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp);        //group
463                 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp);        //element
464                                 
465                 // si on n'est pas en IMPLICIT VR voir pb (lgr  + VR)
466                 
467                 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);               //lgr
468                 
469                 tokens.erase(tokens.begin(),tokens.end());
470                 Tokenize (tag2->second->GetValue(), tokens, "\\");
471                 
472                 if (vr == "US" || vr == "SS") {
473
474                         for (unsigned int i=0; i<tokens.size();i++) {
475                                 val_uint16 = atoi(tokens[i].c_str());           
476                                 ptr = &val_uint16;
477                                 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
478                         }
479                         continue;
480                         
481                 }
482                 if (vr == "UL" || vr == "SL") {
483                         for (unsigned int i=0; i<tokens.size();i++) {
484                                 val_uint32 = atoi(tokens[i].c_str());           
485                                 ptr = &val_uint32;
486                                 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
487                         }
488                         continue;                               
489                         
490                 }       
491                 
492                 // Les pixels ne sont pas chargés dans l'element !
493                 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
494
495                 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
496         }
497                 
498         return(1);
499 }