1 // $Id: gdcmElValSet.cxx,v 1.19 2003/03/13 15:56:26 jpr Exp $
4 #include "gdcmElValSet.h"
6 <<<<<<< gdcmElValSet.cxx
9 static void Tokenize (const string& str, vector<string>& tokens, const string& delimiters);
13 TagElValueHT & gdcmElValSet::GetTagHt(void) {
17 void gdcmElValSet::Add(gdcmElValue * newElValue) {
18 tagHt [newElValue->GetKey()] = newElValue;
19 NameHt[newElValue->GetName()] = newElValue;
22 void gdcmElValSet::Print(ostream & os) {
23 for (TagElValueHT::iterator tag = tagHt.begin();
26 os << tag->first << ": ";
27 os << "[" << tag->second->GetValue() << "]";
28 os << "[" << tag->second->GetName() << "]";
29 os << "[" << tag->second->GetVR() << "]" << endl;
33 void gdcmElValSet::PrintByName(ostream & os) {
34 for (TagElValueNameHT::iterator tag = NameHt.begin();
37 os << tag->first << ": ";
38 os << "[" << tag->second->GetValue() << "]";
39 os << "[" << tag->second->GetKey() << "]";
40 os << "[" << tag->second->GetVR() << "]" << endl;
44 gdcmElValue* gdcmElValSet::GetElementByNumber(guint32 group, guint32 element) {
45 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
46 if ( ! tagHt.count(key))
47 return (gdcmElValue*)0;
48 if (tagHt.count(key) > 1)
49 dbg.Verbose(0, "gdcmElValSet::GetElementByNumber",
50 "multiple entries for this key (FIXME) !");
51 return tagHt.find(key)->second;
54 gdcmElValue* gdcmElValSet::GetElementByName(string TagName) {
55 if ( ! NameHt.count(TagName))
56 return (gdcmElValue*)0;
57 if (NameHt.count(TagName) > 1)
58 dbg.Verbose(0, "gdcmElValSet::GetElement",
59 "multipe entries for this key (FIXME) !");
60 return NameHt.find(TagName)->second;
63 string gdcmElValSet::GetElValueByNumber(guint32 group, guint32 element) {
64 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
65 if ( ! tagHt.count(key))
66 return "gdcm::Unfound";
67 if (tagHt.count(key) > 1)
68 dbg.Verbose(0, "gdcmElValSet::GetElValueByNumber",
69 "multiple entries for this key (FIXME) !");
70 return tagHt.find(key)->second->GetValue();
73 string gdcmElValSet::GetElValueByName(string TagName) {
74 if ( ! NameHt.count(TagName))
75 return "gdcm::Unfound";
76 if (NameHt.count(TagName) > 1)
77 dbg.Verbose(0, "gdcmElValSet::GetElValue",
78 "multipe entries for this key (FIXME) !");
79 return NameHt.find(TagName)->second->GetValue();
82 int gdcmElValSet::SetElValueByNumber(string content,
83 guint32 group, guint32 element) {
84 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
85 if ( ! tagHt.count(key))
87 if (tagHt.count(key) > 1) {
88 dbg.Verbose(0, "gdcmElValSet::SetElValueByNumber",
89 "multiple entries for this key (FIXME) !");
92 tagHt[key]->SetValue(content);
94 // Question : m à j LgrElem ?
95 tagHt[key]->SetLength(strlen(content.c_str()));
97 // FIXME should we really update the element length ?
98 tagHt[key]->SetLength(content.length());
103 int gdcmElValSet::SetElValueByName(string content, string TagName) {
104 if ( ! NameHt.count(TagName))
106 if (NameHt.count(TagName) > 1) {
107 dbg.Verbose(0, "gdcmElValSet::SetElValueByName",
108 "multipe entries for this key (FIXME) !");
111 NameHt.find(TagName)->second->SetValue(content);
112 NameHt.find(TagName)->second->SetLength(strlen(content.c_str()));
117 int gdcmElValSet::SetElValueLengthByNumber(guint32 l,
118 guint32 group, guint32 element) {
119 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
120 if ( ! tagHt.count(key))
122 if (tagHt.count(key) > 1) {
123 dbg.Verbose(0, "gdcmElValSet::SetElValueLengthByNumber",
124 "multiple entries for this key (FIXME) !");
127 // FIXME JPR: comments in English please !
129 tagHt[key]->SetLength(l);
134 int gdcmElValSet::SetElValueLengthByName(guint32 l, string TagName) {
135 if ( ! NameHt.count(TagName))
137 if (NameHt.count(TagName) > 1) {
138 dbg.Verbose(0, "gdcmElValSet::SetElValueByName",
139 "multipe entries for this key (FIXME) !");
142 NameHt.find(TagName)->second->SetLength(l);
147 int gdcmElValSet::Write(FILE * _fp) {
149 // ATTENTION : fonction non terminée (commitée a titre de precaution)
160 vector<string> tokens;
163 char str_lgrCalcGroupe[10];
165 string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
167 // Utilisées pour le calcul Group Length
169 guint32 lgrCalcGroupe=0;
170 gdcmElValue *elem, *elemZ, *elemZPrec;
171 guint16 grCourant = 0;
174 // Comment pourrait-on tester si on est TrueDicom ou non ,
175 // (FileType est un champ de gdcmHeader ...)
178 // On parcourt la table pour recalculer la longueur des 'elements 0x0000'
179 // au cas ou un tag ai été ajouté par rapport à ce qui a été lu
180 // dans l'image native
182 // cf : code IdDcmWriteFile dans libido/src/dcmwrite.c
184 if (0) // Risque de pb dans le calcul des lgr de chaque groupe. On le saute pour le moment!
186 for (TagElValueHT::iterator tag = tagHt.begin();
191 printf("gr %04x el %04x lgr %d\n",elem->GetGroup(), elem->GetElement(), elem->GetLength());
193 if ( (elem->GetGroup() != grCourant) &&
194 (elem->GetGroup() != 0xfffe) ) { // On arrive sur un nv Groupe
196 printf("Nouv Groupegr %04x el %04x \n",elem->GetGroup(), elem->GetElement());
200 if(elemZ->GetElement() != 0x0000) { // pas d'element 'Lgr groupe'
202 gdcmDictEntry * tagZ = new gdcmDictEntry(grCourant, 0x0000, "UL");
203 elemZ = new gdcmElValue(tagZ); // on le cree
205 Add(elemZ); // On l'accroche à sa place
209 //sprintf(str_lgrCalcGroupe,"%d",lgrCalcGroupe);
210 elemZPrec->SetValue(str_lgrCalcGroupe);
215 lgrCalcGroupe = 12; //2 + 2 + 4 + 4; // Gr + Num + Lgr + LgrGroupe
216 printf ("lgrCalcGroupe %d\n",lgrCalcGroupe);
219 grCourant = elem->GetGroup();
221 } else { // On n'EST PAS sur un nv Groupe
223 printf ("lgrCalcGroupe avant : %d LgrElem %d\n",lgrCalcGroupe,elem->GetLength());
225 lgrCalcGroupe += 2 + 2 + 4 + elem->GetLength(); // Gr + Num + Lgr + LgrElem
227 printf ("lgrCalcGroupe apres %d\n",lgrCalcGroupe);
231 // Si on fait de l'implicit VR little Endian
232 // (pour moins se fairche sur processeur INTEL)
233 // penser a forcer le TRANSFERT SYNTAX UID
235 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
236 SetElValueLengthByNumber(18, 0x0002, 0x0010); // Le 0 de fin de chaine doit etre stocké, dans ce cas
238 // restent à tester les echecs en écriture (apres chaque fwrite)
240 for (TagElValueHT::iterator tag2 = tagHt.begin();
244 gr = tag2->second->GetGroup();
245 el = tag2->second->GetElement();
246 lgr = tag2->second->GetLength();
247 val = tag2->second->GetValue().c_str();
248 vr = tag2->second->GetVR();
250 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
251 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
253 //fwrite ( vr,(size_t)2 ,(size_t)1 ,_fp); //VR
255 // si on n'est pas en IMPLICIT VR voir pb (lgr + VR)
257 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp); //lgr
259 tokens.erase(tokens.begin(),tokens.end());
260 Tokenize (tag2->second->GetValue(), tokens, "\\");
262 //printf ("%04x %04x [%s] : [%s]\n",gr, el, vr.c_str(), val);
263 //if (tokens.size() > 1) { printf ("size : %d\n",tokens.size());}
266 if (vr == "US" || vr == "SS") {
268 val_int16 = atoi(val);
270 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
274 for (unsigned int i=0; i<tokens.size();i++) {
275 val_uint16 = atoi(tokens[i].c_str());
277 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
282 if (vr == "UL" || vr == "SL") {
284 val_int32 = atoi(val);
286 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
291 for (unsigned int i=0; i<tokens.size();i++) {
292 val_uint32 = atoi(tokens[i].c_str());
294 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
300 // Les pixels ne sont pas chargés dans l'element !
301 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
303 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
312 int gdcmElValSet::WriteAcr(FILE * _fp) {
315 // ATTENTION : fonction non terminée (commitée a titre de precaution)
316 // ATTENTION : fusioner le code avec celui de lValSet::Write
328 vector<string> tokens;
331 char str_lgrCalcGroupe[10];
333 //string implicitVRTransfertSyntax = "1.2.840.10008.1.2"; // supprime par rapport à Write
335 // Utilisées pour le calcul Group Length
337 guint32 lgrCalcGroupe=0;
338 gdcmElValue *elem, *elemZ, *elemZPrec;
339 guint16 grCourant = 0;
342 // Comment pourrait-on tester si on est TrueDicom ou non ,
343 // (FileType est un champ de gdcmHeader ...)
346 // On parcourt la table pour recalculer la longueur des 'elements 0x0000'
347 // au cas ou un tag ai été ajouté par rapport à ce qui a été lu
348 // dans l'image native
350 // cf : code IdDcmWriteFile dans libido/src/dcmwrite.c
352 if (1) // Risque de pb dans le calcul des lgr de chaque groupe. On le saute pour le moment!
354 for (TagElValueHT::iterator tag = tagHt.begin();
359 //printf("gr %04x el %04x lgr %d\n",elem->GetGroup(), elem->GetElement(), elem->GetLength());
361 if ( (elem->GetGroup() != grCourant) &&
362 (elem->GetGroup() != 0xfffe) ) { // On arrive sur un nv Groupe
364 //printf("Nouv Groupegr %04x el %04x \n",elem->GetGroup(), elem->GetElement());
368 if(elemZ->GetElement() != 0x0000) { // pas d'element 'Lgr groupe'
370 gdcmDictEntry * tagZ = new gdcmDictEntry(grCourant, 0x0000, "UL");
371 elemZ = new gdcmElValue(tagZ); // on le cree
373 Add(elemZ); // On l'accroche à sa place
377 //sprintf(str_lgrCalcGroupe,"%d",lgrCalcGroupe);
378 elemZPrec->SetValue(str_lgrCalcGroupe);
383 lgrCalcGroupe = 12; //2 + 2 + 4 + 4; // Gr + Num + Lgr + LgrGroupe
384 //printf ("lgrCalcGroupe %d\n",lgrCalcGroupe);
387 grCourant = elem->GetGroup();
389 } else { // On n'EST PAS sur un nv Groupe
391 //printf ("lgrCalcGroupe avant : %d LgrElem %d\n",lgrCalcGroupe,elem->GetLength());
393 lgrCalcGroupe += 2 + 2 + 4 + elem->GetLength(); // Gr + Num + Lgr + LgrElem
395 //printf ("lgrCalcGroupe apres %d\n",lgrCalcGroupe);
399 // Si on fait de l'implicit VR little Endian
400 // (pour moins se fairche sur processeur INTEL)
401 // penser a forcer le TRANSFERT SYNTAX UID
403 // supprime par rapport à Write
404 //SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
405 //SetElValueLengthByNumber(18, 0x0002, 0x0010); // Le 0 de fin de chaine doit etre stocké, dans ce cas
407 // restent à tester les echecs en écriture (apres chaque fwrite)
409 for (TagElValueHT::iterator tag2 = tagHt.begin();
413 gr = tag2->second->GetGroup();
414 // saut des groupes speciaux DICOM V3
415 if (gr < 0x0008) continue; // ajouté par rapport à Write
416 // saut des groupes impairs
417 if (gr %2) continue; // ajouté par rapport à Write
419 el = tag2->second->GetElement();
420 lgr = tag2->second->GetLength();
421 val = tag2->second->GetValue().c_str();
422 vr = tag2->second->GetVR();
424 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
425 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
427 // si on n'est pas en IMPLICIT VR voir pb (lgr + VR)
429 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp); //lgr
431 tokens.erase(tokens.begin(),tokens.end());
432 Tokenize (tag2->second->GetValue(), tokens, "\\");
434 if (vr == "US" || vr == "SS") {
436 for (unsigned int i=0; i<tokens.size();i++) {
437 val_uint16 = atoi(tokens[i].c_str());
439 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
444 if (vr == "UL" || vr == "SL") {
445 for (unsigned int i=0; i<tokens.size();i++) {
446 val_uint32 = atoi(tokens[i].c_str());
448 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
454 // Les pixels ne sont pas chargés dans l'element !
455 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
457 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem