5 #include "gdcmElValSet.h"
8 TagElValueHT & gdcmElValSet::GetTagHt(void) {
12 void gdcmElValSet::Add(gdcmElValue * newElValue) {
13 tagHt [newElValue->GetKey()] = newElValue;
14 NameHt[newElValue->GetName()] = newElValue;
17 // TODO : faire un gdcmElValSet::ReplaceOrCreate qui remplace si ça existe, qui cree sinon
19 void gdcmElValSet::ReplaceOrCreate(gdcmElValue * newElValue) {
21 TagKey key = newElValue->GetKey();
23 if (tagHt.count(key) > 1)
24 dbg.Verbose(0, "gdcmElValSet::GetElValueByNumber",
25 "multiple entries for this key (FIXME) !");
27 if (tagHt.count(key)) {
29 tagHt.erase(newElValue->GetName());
32 tagHt [key] = newElValue;
33 NameHt[newElValue->GetName()] = newElValue;
37 void gdcmElValSet::Print(ostream & os) {
38 for (TagElValueHT::iterator tag = tagHt.begin();
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();
50 void gdcmElValSet::PrintByName(ostream & os) {
51 for (TagElValueNameHT::iterator tag = NameHt.begin();
54 os << tag->first << ": ";
55 os << "[" << tag->second->GetValue() << "]";
56 os << "[" << tag->second->GetKey() << "]";
57 os << "[" << tag->second->GetVR() << "]" << endl;
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;
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;
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();
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();
99 int gdcmElValSet::SetElValueByNumber(string content,
100 guint32 group, guint32 element) {
101 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
102 if ( ! tagHt.count(key))
104 if (tagHt.count(key) > 1) {
105 dbg.Verbose(0, "gdcmElValSet::SetElValueByNumber",
106 "multiple entries for this key (FIXME) !");
109 tagHt[key]->SetValue(content);
111 // Question : m à j LgrElem ?
112 tagHt[key]->SetLength(strlen(content.c_str()));
114 // FIXME should we really update the element length ?
115 tagHt[key]->SetLength(content.length());
120 int gdcmElValSet::SetElValueByName(string content, string TagName) {
121 if ( ! NameHt.count(TagName))
123 if (NameHt.count(TagName) > 1) {
124 dbg.Verbose(0, "gdcmElValSet::SetElValueByName",
125 "multipe entries for this key (FIXME) !");
128 NameHt.find(TagName)->second->SetValue(content);
129 NameHt.find(TagName)->second->SetLength(strlen(content.c_str()));
134 int gdcmElValSet::SetElValueLengthByNumber(guint32 l,
135 guint32 group, guint32 element) {
136 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
137 if ( ! tagHt.count(key))
139 if (tagHt.count(key) > 1) {
140 dbg.Verbose(0, "gdcmElValSet::SetElValueLengthByNumber",
141 "multiple entries for this key (FIXME) !");
144 tagHt[key]->SetLength(l);
149 int gdcmElValSet::SetElValueLengthByName(guint32 l, string TagName) {
150 if ( ! NameHt.count(TagName))
152 if (NameHt.count(TagName) > 1) {
153 dbg.Verbose(0, "gdcmElValSet::SetElValueByName",
154 "multipe entries for this key (FIXME) !");
157 NameHt.find(TagName)->second->SetLength(l);
161 // Sorry for the DEBUG's, but tomorow is gonna be hoter than today
164 int gdcmElValSet::Write(FILE * _fp) {
166 // ATTENTION : fonction non terminée (commitée a titre de precaution)
177 vector<string> tokens;
180 char str_lgrCalcGroupe[10];
183 string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
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;
192 // Comment pourrait-on tester si on est TrueDicom ou non ,
193 // (FileType est un champ de gdcmHeader ...)
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
200 // cf : code IdDcmWriteFile dans libido/src/dcmwrite.c
202 if (1) { // Risque de pb dans le calcul des lgr de chaque groupe. On le saute pour le moment!
204 // On fait de l'implicit VR little Endian
205 // (pour moins se fairche sur processeur INTEL)
206 // On force le TRANSFERT SYNTAX UID
208 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
209 SetElValueLengthByNumber(18, 0x0002, 0x0010); // Le 0 de fin de chaine doit etre stocké, dans ce cas
211 TagElValueHT::iterator tag = tagHt.begin();
214 gr = elem->GetGroup();
215 el = elem->GetElement();
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
225 if(DEBUG)printf("Pas d'ajout elem OOOO premiere fois\n");
228 if(DEBUG)printf("init-1 lgr (%d) pour gr %04x\n",lgrCalcGroupe, gr);
231 for (tag = ++tagHt.begin();
236 gr = elem->GetGroup();
237 el = elem->GetElement();
239 if ( (gr != grCourant) /*&& // On arrive sur un nv Groupe
240 (el != 0xfffe) */ ) {
243 gdcmDictEntry * tagZ = new gdcmDictEntry(gr, 0x0000, "UL");
244 elemZ = new gdcmElValue(tagZ); // on le cree
246 Add(elemZ); // On l'accroche à sa place
247 if(DEBUG)printf("ajout elem OOOO pour gr %04x\n",gr);
250 if(DEBUG)printf("maj elmeZ\n");
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;
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);
273 // restent à tester les echecs en écriture (apres chaque fwrite)
275 for (TagElValueHT::iterator tag2 = tagHt.begin();
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);
286 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
287 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
289 //fwrite ( vr,(size_t)2 ,(size_t)1 ,_fp); //VR
291 // si on n'est pas en IMPLICIT VR voir pb (lgr + VR)
293 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp); //lgr
295 tokens.erase(tokens.begin(),tokens.end());
296 Tokenize (tag2->second->GetValue(), tokens, "\\");
298 //if (tokens.size() > 1) { printf ("size : %d\n",tokens.size());}
300 if (vr == "US" || vr == "SS") {
301 for (unsigned int i=0; i<tokens.size();i++) {
302 val_uint16 = atoi(tokens[i].c_str());
304 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
308 if (vr == "UL" || vr == "SL") {
309 for (unsigned int i=0; i<tokens.size();i++) {
310 val_uint32 = atoi(tokens[i].c_str());
312 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
317 // Les pixels ne sont pas chargés dans l'element !
318 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
320 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
329 int gdcmElValSet::WriteAcr(FILE * _fp) {
332 // ATTENTION : fonction non terminée (commitée a titre de precaution)
333 // ATTENTION : fusioner le code avec celui de lValSet::Write
345 vector<string> tokens;
348 char str_lgrCalcGroupe[10];
350 //string implicitVRTransfertSyntax = "1.2.840.10008.1.2"; // supprime par rapport à Write
352 // Utilisées pour le calcul Group Length
354 guint32 lgrCalcGroupe=0;
355 gdcmElValue *elem, *elemZ, *elemZPrec;
356 guint16 grCourant = 0;
359 // Comment pourrait-on tester si on est TrueDicom ou non ,
360 // (FileType est un champ de gdcmHeader ...)
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
367 // cf : code IdDcmWriteFile dans libido/src/dcmwrite.c
371 TagElValueHT::iterator tag = tagHt.begin();
374 gr = elem->GetGroup();
375 el = elem->GetElement();
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
385 if(DEBUG)printf("Pas d'ajout elem OOOO premiere fois\n");
388 if(DEBUG)printf("init-1 lgr (%d) pour gr %04x\n",lgrCalcGroupe, gr);
391 for (tag = ++tagHt.begin();
396 gr = elem->GetGroup();
397 el = elem->GetElement();
399 if ( (gr != grCourant) /*&& // On arrive sur un nv Groupe
400 (el != 0xfffe) */ ) {
403 gdcmDictEntry * tagZ = new gdcmDictEntry(gr, 0x0000, "UL");
404 elemZ = new gdcmElValue(tagZ); // on le cree
406 Add(elemZ); // On l'accroche à sa place
407 if(DEBUG)printf("ajout elem OOOO pour gr %04x\n",gr);
410 if(DEBUG)printf("maj elmeZ\n");
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;
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);
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
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
438 // restent à tester les echecs en écriture (apres chaque fwrite)
440 for (TagElValueHT::iterator tag2 = tagHt.begin();
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
450 el = tag2->second->GetElement();
451 lgr = tag2->second->GetLength();
452 val = tag2->second->GetValue().c_str();
453 vr = tag2->second->GetVR();
455 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
456 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
458 // si on n'est pas en IMPLICIT VR voir pb (lgr + VR)
460 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp); //lgr
462 tokens.erase(tokens.begin(),tokens.end());
463 Tokenize (tag2->second->GetValue(), tokens, "\\");
465 if (vr == "US" || vr == "SS") {
467 for (unsigned int i=0; i<tokens.size();i++) {
468 val_uint16 = atoi(tokens[i].c_str());
470 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
475 if (vr == "UL" || vr == "SL") {
476 for (unsigned int i=0; i<tokens.size();i++) {
477 val_uint32 = atoi(tokens[i].c_str());
479 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
485 // Les pixels ne sont pas chargés dans l'element !
486 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
488 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem