]> Creatis software - gdcm.git/blob - src/gdcmElValSet.cxx
Calcul correct (merci, Eric!) de la longueur de chaque Dicom Group
[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 int gdcmElValSet::SetElValueLengthByNumber(guint32 l,
135                                            guint32 group, guint32 element) {
136         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
137         if ( ! tagHt.count(key))
138                 return 0;
139         if (tagHt.count(key) > 1) {
140                 dbg.Verbose(0, "gdcmElValSet::SetElValueLengthByNumber",
141                             "multiple entries for this key (FIXME) !");
142                 return (0); 
143         }                                      
144         tagHt[key]->SetLength(l);        
145         return(1);              
146 }
147
148
149 int gdcmElValSet::SetElValueLengthByName(guint32 l, string TagName) {
150         if ( ! NameHt.count(TagName))
151                 return 0;
152         if (NameHt.count(TagName) > 1) {
153                 dbg.Verbose(0, "gdcmElValSet::SetElValueByName",
154                             "multipe entries for this key (FIXME) !");
155                 return 0;
156         }
157         NameHt.find(TagName)->second->SetLength(l);      
158         return(1);              
159 }
160
161 // Sorry for the DEBUG's, but tomorow is gonna be hoter than today
162 #define DEBUG 0
163
164 int gdcmElValSet::Write(FILE * _fp) {
165
166 // ATTENTION : fonction non terminée (commitée a titre de precaution)
167
168         guint16 gr, el;
169         guint32 lgr;
170         const char * val;
171         string vr;
172         guint32 val_uint32;
173         gint32  val_int32;
174         guint16 val_uint16;
175         gint16  val_int16;;
176         
177         vector<string> tokens;
178         
179         void *ptr;
180         char str_lgrCalcGroupe[10];
181
182         
183         string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
184         
185         // Utilisées pour le calcul Group Length
186         int deja = 0, prem=0;
187         guint32 lgrCalcGroupe=0;
188         gdcmElValue *elem, *elemZ, *elemZPrec;
189         guint16 grCourant = 0;
190         
191         // Question :
192         // Comment pourrait-on tester si on est TrueDicom ou non ,
193         // (FileType est un champ de gdcmHeader ...)
194         //
195
196         // On parcourt la table pour recalculer la longueur des 'elements 0x0000'
197         // au cas ou un tag ai été ajouté par rapport à ce qui a été lu
198         // dans l'image native
199         //
200         // cf : code IdDcmWriteFile dans libido/src/dcmwrite.c
201                 
202 if (1) {  // Risque de pb dans le calcul des lgr de chaque groupe. On le saute pour le moment!
203                 
204         // On fait de l'implicit VR little Endian 
205         // (pour moins se fairche sur processeur INTEL)
206         // On force le TRANSFERT SYNTAX UID
207                                 
208         SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);  
209         SetElValueLengthByNumber(18, 0x0002, 0x0010);  // Le 0 de fin de chaine doit etre stocké, dans ce cas   
210                         
211         TagElValueHT::iterator tag = tagHt.begin();
212         
213         elem = tag->second;
214         gr   = elem->GetGroup();
215         el   = elem->GetElement();
216                         
217         if (el != 0x0000) {
218                 if(DEBUG)printf("ajout elem OOOO premiere fois\n");
219                 gdcmDictEntry * tagZ = new gdcmDictEntry(gr, 0x0000, "UL");
220                 elemZPrec = new gdcmElValue(tagZ);      // on le cree
221                 elemZPrec->SetLength(4);
222                 Add(elemZPrec);                         // On l'accroche à sa place
223         } else {
224                 elemZPrec = elem;
225                 if(DEBUG)printf("Pas d'ajout elem OOOO premiere fois\n");
226         }
227         lgrCalcGroupe = 0;
228         if(DEBUG)printf("init-1 lgr (%d) pour gr %04x\n",lgrCalcGroupe, gr);
229         grCourant = gr;
230         
231         for (tag = ++tagHt.begin();
232                   tag != tagHt.end();
233                   ++tag){
234                   
235                 elem = tag->second;
236                 gr = elem->GetGroup();
237                 el = elem->GetElement();
238
239                 if ( (gr != grCourant) /*&&     // On arrive sur un nv Groupe     
240                      (el != 0xfffe) */  ) {
241                             
242                         if (el != 0x0000) {
243                                 gdcmDictEntry * tagZ = new gdcmDictEntry(gr, 0x0000, "UL");
244                                 elemZ = new gdcmElValue(tagZ); // on le cree
245                                 elemZ->SetLength(4);
246                                 Add(elemZ);                     // On l'accroche à sa place 
247                                 if(DEBUG)printf("ajout elem OOOO pour gr %04x\n",gr);
248                         } else { 
249                                 elemZ=elem;
250                                 if(DEBUG)printf("maj elmeZ\n");
251                         }
252                         
253                         ostringstream fock;
254                         fock << lgrCalcGroupe; 
255                         //sprintf(str_lgrCalcGroupe,"%d",lgrCalcGroupe);
256                         elemZPrec->SetValue(fock.str());
257                         if(DEBUG)printf("ecriture lgr (%d, %s) pour gr %04x\n",lgrCalcGroupe, fock.str().c_str(), grCourant);
258                         if(DEBUG)printf ("%04x %04x [%s]\n",elemZPrec->GetGroup(), elemZPrec->GetElement(),elemZPrec->GetValue().c_str());
259                         if(DEBUG)cout << "Addresse elemZPrec " << elemZPrec<< endl;
260                         elemZPrec=elemZ;
261                         lgrCalcGroupe = 0;
262                         grCourant     = gr;     
263                         if(DEBUG)printf("init-2 lgr (%d) pour gr %04x\n",lgrCalcGroupe, gr);                    
264                 } else {                        // On n'EST PAS sur un nv Groupe
265                         lgrCalcGroupe += 2 + 2 + 4 + elem->GetLength();  // Gr + Num + Lgr + LgrElem
266                         if(DEBUG)printf("increment (%d) el %04x-->lgr (%d) pour gr %04x\n",elem->GetLength(), el, lgrCalcGroupe, gr);
267                 }               
268         }
269         
270 } // fin if (1)
271
272         
273         // restent à tester les echecs en écriture (apres chaque fwrite)
274         
275         for (TagElValueHT::iterator tag2 = tagHt.begin();
276                   tag2 != tagHt.end();
277                   ++tag2){
278                 
279                 gr =  tag2->second->GetGroup();
280                 el =  tag2->second->GetElement();
281                 lgr = tag2->second->GetLength();
282                 val = tag2->second->GetValue().c_str();
283                 vr =  tag2->second->GetVR();
284                 if(DEBUG)printf ("%04x %04x [%s] : [%s]\n",gr, el, vr.c_str(), val);
285                         
286                 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp);        //group
287                 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp);        //element
288                 
289                 //fwrite ( vr,(size_t)2 ,(size_t)1 ,_fp);       //VR
290                 
291                 // si on n'est pas en IMPLICIT VR voir pb (lgr  + VR)
292                 
293                 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);       //lgr
294                 
295                 tokens.erase(tokens.begin(),tokens.end());
296                 Tokenize (tag2->second->GetValue(), tokens, "\\");
297                 
298                 //if (tokens.size() > 1) { printf ("size : %d\n",tokens.size());}
299                 
300                 if (vr == "US" || vr == "SS") {
301                         for (unsigned int i=0; i<tokens.size();i++) {
302                                 val_uint16 = atoi(tokens[i].c_str());           
303                                 ptr = &val_uint16;
304                                 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
305                         }
306                         continue;       
307                 }
308                 if (vr == "UL" || vr == "SL") { 
309                         for (unsigned int i=0; i<tokens.size();i++) {
310                                 val_uint32 = atoi(tokens[i].c_str());           
311                                 ptr = &val_uint32;
312                                 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
313                         }
314                         continue;       
315                 }       
316                 
317                 // Les pixels ne sont pas chargés dans l'element !
318                 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
319
320                 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
321         }
322                 
323         return(1);
324 }
325
326
327
328
329 int gdcmElValSet::WriteAcr(FILE * _fp) {
330
331
332 // ATTENTION : fonction non terminée (commitée a titre de precaution)
333 // ATTENTION : fusioner le code avec celui de lValSet::Write
334
335
336         guint16 gr, el;
337         guint32 lgr;
338         const char * val;
339         string vr;
340         guint32 val_uint32;
341         gint32  val_int32;
342         guint16 val_uint16;
343         gint16  val_int16;
344         
345         vector<string> tokens;
346         
347         void *ptr;
348         char str_lgrCalcGroupe[10];
349         
350         //string implicitVRTransfertSyntax = "1.2.840.10008.1.2"; // supprime par rapport à Write
351         
352         // Utilisées pour le calcul Group Length
353         int deja = 0;
354         guint32 lgrCalcGroupe=0;
355         gdcmElValue *elem, *elemZ, *elemZPrec;
356         guint16 grCourant = 0;
357         
358         // Question :
359         // Comment pourrait-on tester si on est TrueDicom ou non ,
360         // (FileType est un champ de gdcmHeader ...)
361         //
362
363         // On parcourt la table pour recalculer la longueur des 'elements 0x0000'
364         // au cas ou un tag ai été ajouté par rapport à ce qui a été lu
365         // dans l'image native
366         //
367         // cf : code IdDcmWriteFile dans libido/src/dcmwrite.c
368                 
369
370                         
371         TagElValueHT::iterator tag = tagHt.begin();
372         
373         elem = tag->second;
374         gr   = elem->GetGroup();
375         el   = elem->GetElement();
376                         
377         if (el != 0x0000) {
378                 if(DEBUG)printf("ajout elem OOOO premiere fois\n");
379                 gdcmDictEntry * tagZ = new gdcmDictEntry(gr, 0x0000, "UL");
380                 elemZPrec = new gdcmElValue(tagZ);      // on le cree
381                 elemZPrec->SetLength(4);
382                 Add(elemZPrec);                         // On l'accroche à sa place
383         } else {
384                 elemZPrec = elem;
385                 if(DEBUG)printf("Pas d'ajout elem OOOO premiere fois\n");
386         }
387         lgrCalcGroupe = 0;
388         if(DEBUG)printf("init-1 lgr (%d) pour gr %04x\n",lgrCalcGroupe, gr);
389         grCourant = gr;
390         
391         for (tag = ++tagHt.begin();
392                   tag != tagHt.end();
393                   ++tag){
394                   
395                 elem = tag->second;
396                 gr = elem->GetGroup();
397                 el = elem->GetElement();
398
399                 if ( (gr != grCourant) /*&&     // On arrive sur un nv Groupe     
400                      (el != 0xfffe) */  ) {
401                             
402                         if (el != 0x0000) {
403                                 gdcmDictEntry * tagZ = new gdcmDictEntry(gr, 0x0000, "UL");
404                                 elemZ = new gdcmElValue(tagZ); // on le cree
405                                 elemZ->SetLength(4);
406                                 Add(elemZ);                     // On l'accroche à sa place 
407                                 if(DEBUG)printf("ajout elem OOOO pour gr %04x\n",gr);
408                         } else { 
409                                 elemZ=elem;
410                                 if(DEBUG)printf("maj elmeZ\n");
411                         }
412                         
413                         ostringstream fock;
414                         fock << lgrCalcGroupe; 
415                         //sprintf(str_lgrCalcGroupe,"%d",lgrCalcGroupe);
416                         elemZPrec->SetValue(fock.str());
417                         if(DEBUG)printf("ecriture lgr (%d, %s) pour gr %04x\n",lgrCalcGroupe, fock.str().c_str(), grCourant);
418                         if(DEBUG)printf ("%04x %04x [%s]\n",elemZPrec->GetGroup(), elemZPrec->GetElement(),elemZPrec->GetValue().c_str());
419                         if(DEBUG)cout << "Addresse elemZPrec " << elemZPrec<< endl;
420                         elemZPrec=elemZ;
421                         lgrCalcGroupe = 0;
422                         grCourant     = gr;     
423                         if(DEBUG)printf("init-2 lgr (%d) pour gr %04x\n",lgrCalcGroupe, gr);                    
424                 } else {                        // On n'EST PAS sur un nv Groupe
425                         lgrCalcGroupe += 2 + 2 + 4 + elem->GetLength();  // Gr + Num + Lgr + LgrElem
426                         if(DEBUG)printf("increment (%d) el %04x-->lgr (%d) pour gr %04x\n",elem->GetLength(), el, lgrCalcGroupe, gr);
427                 }               
428         }
429         
430         // Si on fait de l'implicit VR little Endian 
431         // (pour moins se fairche sur processeur INTEL)
432         // penser a forcer le TRANSFERT SYNTAX UID
433         
434         // supprime par rapport à Write         
435         //SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);        
436         //SetElValueLengthByNumber(18, 0x0002, 0x0010);  // Le 0 de fin de chaine doit etre stocké, dans ce cas 
437                 
438         // restent à tester les echecs en écriture (apres chaque fwrite)
439         
440         for (TagElValueHT::iterator tag2 = tagHt.begin();
441                   tag2 != tagHt.end();
442                   ++tag2){
443
444                 gr =  tag2->second->GetGroup();
445                                                 // saut des groupes speciaux DICOM V3
446                 if (gr < 0x0008) continue;      // ajouté par rapport à Write
447                                                 // saut des groupes impairs
448                 if (gr %2)       continue;      // ajouté par rapport à Write
449                 
450                 el =  tag2->second->GetElement();
451                 lgr = tag2->second->GetLength();
452                 val = tag2->second->GetValue().c_str();
453                 vr =  tag2->second->GetVR();
454                         
455                 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp);        //group
456                 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp);        //element
457                                 
458                 // si on n'est pas en IMPLICIT VR voir pb (lgr  + VR)
459                 
460                 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);               //lgr
461                 
462                 tokens.erase(tokens.begin(),tokens.end());
463                 Tokenize (tag2->second->GetValue(), tokens, "\\");
464                 
465                 if (vr == "US" || vr == "SS") {
466
467                         for (unsigned int i=0; i<tokens.size();i++) {
468                                 val_uint16 = atoi(tokens[i].c_str());           
469                                 ptr = &val_uint16;
470                                 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
471                         }
472                         continue;
473                         
474                 }
475                 if (vr == "UL" || vr == "SL") {
476                         for (unsigned int i=0; i<tokens.size();i++) {
477                                 val_uint32 = atoi(tokens[i].c_str());           
478                                 ptr = &val_uint32;
479                                 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
480                         }
481                         continue;                               
482                         
483                 }       
484                 
485                 // Les pixels ne sont pas chargés dans l'element !
486                 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
487
488                 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
489         }
490                 
491         return(1);
492 }