1 // $Id: gdcmElValSet.cxx,v 1.21 2003/03/14 14:26:01 jpr Exp $
4 #include "gdcmElValSet.h"
7 TagElValueHT & gdcmElValSet::GetTagHt(void) {
11 void gdcmElValSet::Add(gdcmElValue * newElValue) {
12 tagHt [newElValue->GetKey()] = newElValue;
13 NameHt[newElValue->GetName()] = newElValue;
16 void gdcmElValSet::Print(ostream & os) {
17 for (TagElValueHT::iterator tag = tagHt.begin();
20 os << tag->first << ": ";
21 os << "[" << tag->second->GetValue() << "]";
22 os << "[" << tag->second->GetName() << "]";
23 os << "[" << tag->second->GetVR() << "]" << endl;
27 void gdcmElValSet::PrintByName(ostream & os) {
28 for (TagElValueNameHT::iterator tag = NameHt.begin();
31 os << tag->first << ": ";
32 os << "[" << tag->second->GetValue() << "]";
33 os << "[" << tag->second->GetKey() << "]";
34 os << "[" << tag->second->GetVR() << "]" << endl;
38 gdcmElValue* gdcmElValSet::GetElementByNumber(guint32 group, guint32 element) {
39 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
40 if ( ! tagHt.count(key))
41 return (gdcmElValue*)0;
42 if (tagHt.count(key) > 1)
43 dbg.Verbose(0, "gdcmElValSet::GetElementByNumber",
44 "multiple entries for this key (FIXME) !");
45 return tagHt.find(key)->second;
48 gdcmElValue* gdcmElValSet::GetElementByName(string TagName) {
49 if ( ! NameHt.count(TagName))
50 return (gdcmElValue*)0;
51 if (NameHt.count(TagName) > 1)
52 dbg.Verbose(0, "gdcmElValSet::GetElement",
53 "multipe entries for this key (FIXME) !");
54 return NameHt.find(TagName)->second;
57 string gdcmElValSet::GetElValueByNumber(guint32 group, guint32 element) {
58 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
59 if ( ! tagHt.count(key))
60 return "gdcm::Unfound";
61 if (tagHt.count(key) > 1)
62 dbg.Verbose(0, "gdcmElValSet::GetElValueByNumber",
63 "multiple entries for this key (FIXME) !");
64 return tagHt.find(key)->second->GetValue();
67 string gdcmElValSet::GetElValueByName(string TagName) {
68 if ( ! NameHt.count(TagName))
69 return "gdcm::Unfound";
70 if (NameHt.count(TagName) > 1)
71 dbg.Verbose(0, "gdcmElValSet::GetElValue",
72 "multipe entries for this key (FIXME) !");
73 return NameHt.find(TagName)->second->GetValue();
76 int gdcmElValSet::SetElValueByNumber(string content,
77 guint32 group, guint32 element) {
78 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
79 if ( ! tagHt.count(key))
81 if (tagHt.count(key) > 1) {
82 dbg.Verbose(0, "gdcmElValSet::SetElValueByNumber",
83 "multiple entries for this key (FIXME) !");
86 tagHt[key]->SetValue(content);
88 // Question : m à j LgrElem ?
89 tagHt[key]->SetLength(strlen(content.c_str()));
91 // FIXME should we really update the element length ?
92 tagHt[key]->SetLength(content.length());
97 int gdcmElValSet::SetElValueByName(string content, string TagName) {
98 if ( ! NameHt.count(TagName))
100 if (NameHt.count(TagName) > 1) {
101 dbg.Verbose(0, "gdcmElValSet::SetElValueByName",
102 "multipe entries for this key (FIXME) !");
105 NameHt.find(TagName)->second->SetValue(content);
106 NameHt.find(TagName)->second->SetLength(strlen(content.c_str()));
111 int gdcmElValSet::SetElValueLengthByNumber(guint32 l,
112 guint32 group, guint32 element) {
113 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
114 if ( ! tagHt.count(key))
116 if (tagHt.count(key) > 1) {
117 dbg.Verbose(0, "gdcmElValSet::SetElValueLengthByNumber",
118 "multiple entries for this key (FIXME) !");
121 // FIXME JPR: comments in English please !
123 tagHt[key]->SetLength(l);
128 int gdcmElValSet::SetElValueLengthByName(guint32 l, string TagName) {
129 if ( ! NameHt.count(TagName))
131 if (NameHt.count(TagName) > 1) {
132 dbg.Verbose(0, "gdcmElValSet::SetElValueByName",
133 "multipe entries for this key (FIXME) !");
136 NameHt.find(TagName)->second->SetLength(l);
141 int gdcmElValSet::Write(FILE * _fp) {
143 // ATTENTION : fonction non terminée (commitée a titre de precaution)
154 vector<string> tokens;
157 char str_lgrCalcGroupe[10];
159 string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
161 // Utilisées pour le calcul Group Length
163 guint32 lgrCalcGroupe=0;
164 gdcmElValue *elem, *elemZ, *elemZPrec;
165 guint16 grCourant = 0;
168 // Comment pourrait-on tester si on est TrueDicom ou non ,
169 // (FileType est un champ de gdcmHeader ...)
172 // On parcourt la table pour recalculer la longueur des 'elements 0x0000'
173 // au cas ou un tag ai été ajouté par rapport à ce qui a été lu
174 // dans l'image native
176 // cf : code IdDcmWriteFile dans libido/src/dcmwrite.c
178 if (0) // Risque de pb dans le calcul des lgr de chaque groupe. On le saute pour le moment!
180 for (TagElValueHT::iterator tag = tagHt.begin();
185 printf("gr %04x el %04x lgr %d\n",elem->GetGroup(), elem->GetElement(), elem->GetLength());
187 if ( (elem->GetGroup() != grCourant) &&
188 (elem->GetGroup() != 0xfffe) ) { // On arrive sur un nv Groupe
190 printf("Nouv Groupegr %04x el %04x \n",elem->GetGroup(), elem->GetElement());
194 if(elemZ->GetElement() != 0x0000) { // pas d'element 'Lgr groupe'
196 gdcmDictEntry * tagZ = new gdcmDictEntry(grCourant, 0x0000, "UL");
197 elemZ = new gdcmElValue(tagZ); // on le cree
199 Add(elemZ); // On l'accroche à sa place
203 //sprintf(str_lgrCalcGroupe,"%d",lgrCalcGroupe);
204 elemZPrec->SetValue(str_lgrCalcGroupe);
209 lgrCalcGroupe = 12; //2 + 2 + 4 + 4; // Gr + Num + Lgr + LgrGroupe
210 printf ("lgrCalcGroupe %d\n",lgrCalcGroupe);
213 grCourant = elem->GetGroup();
215 } else { // On n'EST PAS sur un nv Groupe
217 printf ("lgrCalcGroupe avant : %d LgrElem %d\n",lgrCalcGroupe,elem->GetLength());
219 lgrCalcGroupe += 2 + 2 + 4 + elem->GetLength(); // Gr + Num + Lgr + LgrElem
221 printf ("lgrCalcGroupe apres %d\n",lgrCalcGroupe);
225 // Si on fait de l'implicit VR little Endian
226 // (pour moins se fairche sur processeur INTEL)
227 // penser a forcer le TRANSFERT SYNTAX UID
229 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
230 SetElValueLengthByNumber(18, 0x0002, 0x0010); // Le 0 de fin de chaine doit etre stocké, dans ce cas
232 // restent à tester les echecs en écriture (apres chaque fwrite)
234 for (TagElValueHT::iterator tag2 = tagHt.begin();
238 gr = tag2->second->GetGroup();
239 el = tag2->second->GetElement();
240 lgr = tag2->second->GetLength();
241 val = tag2->second->GetValue().c_str();
242 vr = tag2->second->GetVR();
244 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
245 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
247 //fwrite ( vr,(size_t)2 ,(size_t)1 ,_fp); //VR
249 // si on n'est pas en IMPLICIT VR voir pb (lgr + VR)
251 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp); //lgr
253 tokens.erase(tokens.begin(),tokens.end());
254 Tokenize (tag2->second->GetValue(), tokens, "\\");
256 //printf ("%04x %04x [%s] : [%s]\n",gr, el, vr.c_str(), val);
257 //if (tokens.size() > 1) { printf ("size : %d\n",tokens.size());}
260 if (vr == "US" || vr == "SS") {
262 val_int16 = atoi(val);
264 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
268 for (unsigned int i=0; i<tokens.size();i++) {
269 val_uint16 = atoi(tokens[i].c_str());
271 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
276 if (vr == "UL" || vr == "SL") {
278 val_int32 = atoi(val);
280 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
285 for (unsigned int i=0; i<tokens.size();i++) {
286 val_uint32 = atoi(tokens[i].c_str());
288 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
294 // Les pixels ne sont pas chargés dans l'element !
295 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
297 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
306 int gdcmElValSet::WriteAcr(FILE * _fp) {
309 // ATTENTION : fonction non terminée (commitée a titre de precaution)
310 // ATTENTION : fusioner le code avec celui de lValSet::Write
322 vector<string> tokens;
325 char str_lgrCalcGroupe[10];
327 //string implicitVRTransfertSyntax = "1.2.840.10008.1.2"; // supprime par rapport à Write
329 // Utilisées pour le calcul Group Length
331 guint32 lgrCalcGroupe=0;
332 gdcmElValue *elem, *elemZ, *elemZPrec;
333 guint16 grCourant = 0;
336 // Comment pourrait-on tester si on est TrueDicom ou non ,
337 // (FileType est un champ de gdcmHeader ...)
340 // On parcourt la table pour recalculer la longueur des 'elements 0x0000'
341 // au cas ou un tag ai été ajouté par rapport à ce qui a été lu
342 // dans l'image native
344 // cf : code IdDcmWriteFile dans libido/src/dcmwrite.c
346 if (0) // Risque de pb dans le calcul des lgr de chaque groupe. On le saute pour le moment!
348 for (TagElValueHT::iterator tag = tagHt.begin();
353 //printf("gr %04x el %04x lgr %d\n",elem->GetGroup(), elem->GetElement(), elem->GetLength());
355 if ( (elem->GetGroup() != grCourant) &&
356 (elem->GetGroup() != 0xfffe) ) { // On arrive sur un nv Groupe
358 //printf("Nouv Groupegr %04x el %04x \n",elem->GetGroup(), elem->GetElement());
362 if(elemZ->GetElement() != 0x0000) { // pas d'element 'Lgr groupe'
364 gdcmDictEntry * tagZ = new gdcmDictEntry(grCourant, 0x0000, "UL");
365 elemZ = new gdcmElValue(tagZ); // on le cree
367 Add(elemZ); // On l'accroche à sa place
371 //sprintf(str_lgrCalcGroupe,"%d",lgrCalcGroupe);
372 elemZPrec->SetValue(str_lgrCalcGroupe);
377 lgrCalcGroupe = 12; //2 + 2 + 4 + 4; // Gr + Num + Lgr + LgrGroupe
378 //printf ("lgrCalcGroupe %d\n",lgrCalcGroupe);
381 grCourant = elem->GetGroup();
383 } else { // On n'EST PAS sur un nv Groupe
385 //printf ("lgrCalcGroupe avant : %d LgrElem %d\n",lgrCalcGroupe,elem->GetLength());
387 lgrCalcGroupe += 2 + 2 + 4 + elem->GetLength(); // Gr + Num + Lgr + LgrElem
389 //printf ("lgrCalcGroupe apres %d\n",lgrCalcGroupe);
393 // Si on fait de l'implicit VR little Endian
394 // (pour moins se fairche sur processeur INTEL)
395 // penser a forcer le TRANSFERT SYNTAX UID
397 // supprime par rapport à Write
398 //SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
399 //SetElValueLengthByNumber(18, 0x0002, 0x0010); // Le 0 de fin de chaine doit etre stocké, dans ce cas
401 // restent à tester les echecs en écriture (apres chaque fwrite)
403 for (TagElValueHT::iterator tag2 = tagHt.begin();
407 gr = tag2->second->GetGroup();
408 // saut des groupes speciaux DICOM V3
409 if (gr < 0x0008) continue; // ajouté par rapport à Write
410 // saut des groupes impairs
411 if (gr %2) continue; // ajouté par rapport à Write
413 el = tag2->second->GetElement();
414 lgr = tag2->second->GetLength();
415 val = tag2->second->GetValue().c_str();
416 vr = tag2->second->GetVR();
418 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
419 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
421 // si on n'est pas en IMPLICIT VR voir pb (lgr + VR)
423 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp); //lgr
425 tokens.erase(tokens.begin(),tokens.end());
426 Tokenize (tag2->second->GetValue(), tokens, "\\");
428 if (vr == "US" || vr == "SS") {
430 for (unsigned int i=0; i<tokens.size();i++) {
431 val_uint16 = atoi(tokens[i].c_str());
433 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
438 if (vr == "UL" || vr == "SL") {
439 for (unsigned int i=0; i<tokens.size();i++) {
440 val_uint32 = atoi(tokens[i].c_str());
442 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
448 // Les pixels ne sont pas chargés dans l'element !
449 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
451 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem