1 // $Id: gdcmElValSet.cxx,v 1.18 2003/03/12 23:38:58 frog Exp $
4 #include "gdcmElValSet.h"
6 TagElValueHT & gdcmElValSet::GetTagHt(void) {
10 void gdcmElValSet::Add(gdcmElValue * newElValue) {
11 tagHt [newElValue->GetKey()] = newElValue;
12 NameHt[newElValue->GetName()] = newElValue;
15 void gdcmElValSet::Print(ostream & os) {
16 for (TagElValueHT::iterator tag = tagHt.begin();
19 os << tag->first << ": ";
20 os << "[" << tag->second->GetValue() << "]";
21 os << "[" << tag->second->GetName() << "]";
22 os << "[" << tag->second->GetVR() << "]" << endl;
26 void gdcmElValSet::PrintByName(ostream & os) {
27 for (TagElValueNameHT::iterator tag = NameHt.begin();
30 os << tag->first << ": ";
31 os << "[" << tag->second->GetValue() << "]";
32 os << "[" << tag->second->GetKey() << "]";
33 os << "[" << tag->second->GetVR() << "]" << endl;
37 gdcmElValue* gdcmElValSet::GetElementByNumber(guint32 group, guint32 element) {
38 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
39 if ( ! tagHt.count(key))
40 return (gdcmElValue*)0;
41 if (tagHt.count(key) > 1)
42 dbg.Verbose(0, "gdcmElValSet::GetElementByNumber",
43 "multiple entries for this key (FIXME) !");
44 return tagHt.find(key)->second;
47 gdcmElValue* gdcmElValSet::GetElementByName(string TagName) {
48 if ( ! NameHt.count(TagName))
49 return (gdcmElValue*)0;
50 if (NameHt.count(TagName) > 1)
51 dbg.Verbose(0, "gdcmElValSet::GetElement",
52 "multipe entries for this key (FIXME) !");
53 return NameHt.find(TagName)->second;
56 string gdcmElValSet::GetElValueByNumber(guint32 group, guint32 element) {
57 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
58 if ( ! tagHt.count(key))
59 return "gdcm::Unfound";
60 if (tagHt.count(key) > 1)
61 dbg.Verbose(0, "gdcmElValSet::GetElValueByNumber",
62 "multiple entries for this key (FIXME) !");
63 return tagHt.find(key)->second->GetValue();
66 string gdcmElValSet::GetElValueByName(string TagName) {
67 if ( ! NameHt.count(TagName))
68 return "gdcm::Unfound";
69 if (NameHt.count(TagName) > 1)
70 dbg.Verbose(0, "gdcmElValSet::GetElValue",
71 "multipe entries for this key (FIXME) !");
72 return NameHt.find(TagName)->second->GetValue();
75 int gdcmElValSet::SetElValueByNumber(string content,
76 guint32 group, guint32 element) {
77 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
78 if ( ! tagHt.count(key))
80 if (tagHt.count(key) > 1) {
81 dbg.Verbose(0, "gdcmElValSet::SetElValueByNumber",
82 "multiple entries for this key (FIXME) !");
85 tagHt[key]->SetValue(content);
87 // Question : m à j LgrElem ?
88 tagHt[key]->SetLength(strlen(content.c_str()));
90 // FIXME should we really update the element length ?
91 tagHt[key]->SetLength(content.length());
96 int gdcmElValSet::SetElValueByName(string content, string TagName) {
97 if ( ! NameHt.count(TagName))
99 if (NameHt.count(TagName) > 1) {
100 dbg.Verbose(0, "gdcmElValSet::SetElValueByName",
101 "multipe entries for this key (FIXME) !");
104 NameHt.find(TagName)->second->SetValue(content);
105 NameHt.find(TagName)->second->SetLength(strlen(content.c_str()));
110 int gdcmElValSet::SetElValueLengthByNumber(guint32 l,
111 guint32 group, guint32 element) {
112 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
113 if ( ! tagHt.count(key))
115 if (tagHt.count(key) > 1) {
116 dbg.Verbose(0, "gdcmElValSet::SetElValueLengthByNumber",
117 "multiple entries for this key (FIXME) !");
120 // FIXME JPR: comments in English please !
122 tagHt[key]->SetLength(l);
127 int gdcmElValSet::SetElValueLengthByName(guint32 l, string TagName) {
128 if ( ! NameHt.count(TagName))
130 if (NameHt.count(TagName) > 1) {
131 dbg.Verbose(0, "gdcmElValSet::SetElValueByName",
132 "multipe entries for this key (FIXME) !");
135 NameHt.find(TagName)->second->SetLength(l);
140 int gdcmElValSet::Write(FILE * _fp) {
142 // ATTENTION : fonction non terminée (commitée a titre de precaution)
153 vector<string> tokens;
156 char str_lgrCalcGroupe[10];
158 string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
160 // Utilisées pour le calcul Group Length
162 guint32 lgrCalcGroupe=0;
163 gdcmElValue *elem, *elemZ, *elemZPrec;
164 guint16 grCourant = 0;
167 // Comment pourrait-on tester si on est TrueDicom ou non ,
168 // (FileType est un champ de gdcmHeader ...)
171 // On parcourt la table pour recalculer la longueur des 'elements 0x0000'
172 // au cas ou un tag ai été ajouté par rapport à ce qui a été lu
173 // dans l'image native
175 // cf : code IdDcmWriteFile dans libido/src/dcmwrite.c
177 if (0) // Risque de pb dans le calcul des lgr de chaque groupe. On le saute pour le moment!
179 for (TagElValueHT::iterator tag = tagHt.begin();
184 printf("gr %04x el %04x lgr %d\n",elem->GetGroup(), elem->GetElement(), elem->GetLength());
186 if ( (elem->GetGroup() != grCourant) &&
187 (elem->GetGroup() != 0xfffe) ) { // On arrive sur un nv Groupe
189 printf("Nouv Groupegr %04x el %04x \n",elem->GetGroup(), elem->GetElement());
193 if(elemZ->GetElement() != 0x0000) { // pas d'element 'Lgr groupe'
195 gdcmDictEntry * tagZ = new gdcmDictEntry(grCourant, 0x0000, "UL");
196 elemZ = new gdcmElValue(tagZ); // on le cree
198 Add(elemZ); // On l'accroche à sa place
202 //sprintf(str_lgrCalcGroupe,"%d",lgrCalcGroupe);
203 elemZPrec->SetValue(str_lgrCalcGroupe);
208 lgrCalcGroupe = 12; //2 + 2 + 4 + 4; // Gr + Num + Lgr + LgrGroupe
209 printf ("lgrCalcGroupe %d\n",lgrCalcGroupe);
212 grCourant = elem->GetGroup();
214 } else { // On n'EST PAS sur un nv Groupe
216 printf ("lgrCalcGroupe avant : %d LgrElem %d\n",lgrCalcGroupe,elem->GetLength());
218 lgrCalcGroupe += 2 + 2 + 4 + elem->GetLength(); // Gr + Num + Lgr + LgrElem
220 printf ("lgrCalcGroupe apres %d\n",lgrCalcGroupe);
224 // Si on fait de l'implicit VR little Endian
225 // (pour moins se fairche sur processeur INTEL)
226 // penser a forcer le TRANSFERT SYNTAX UID
228 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
229 SetElValueLengthByNumber(18, 0x0002, 0x0010); // Le 0 de fin de chaine doit etre stocké, dans ce cas
231 // restent à tester les echecs en écriture (apres chaque fwrite)
233 for (TagElValueHT::iterator tag2 = tagHt.begin();
237 gr = tag2->second->GetGroup();
238 el = tag2->second->GetElement();
239 lgr = tag2->second->GetLength();
240 val = tag2->second->GetValue().c_str();
241 vr = tag2->second->GetVR();
243 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
244 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
246 //fwrite ( vr,(size_t)2 ,(size_t)1 ,_fp); //VR
248 // si on n'est pas en IMPLICIT VR voir pb (lgr + VR)
250 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp); //lgr
252 tokens.erase(tokens.begin(),tokens.end());
253 Tokenize (tag2->second->GetValue(), tokens, "\\");
255 //printf ("%04x %04x [%s] : [%s]\n",gr, el, vr.c_str(), val);
256 //if (tokens.size() > 1) { printf ("size : %d\n",tokens.size());}
259 if (vr == "US" || vr == "SS") {
261 val_int16 = atoi(val);
263 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
267 for (unsigned int i=0; i<tokens.size();i++) {
268 val_uint16 = atoi(tokens[i].c_str());
270 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
275 if (vr == "UL" || vr == "SL") {
277 val_int32 = atoi(val);
279 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
284 for (unsigned int i=0; i<tokens.size();i++) {
285 val_uint32 = atoi(tokens[i].c_str());
287 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
293 // Les pixels ne sont pas chargés dans l'element !
294 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
296 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
305 int gdcmElValSet::WriteAcr(FILE * _fp) {
308 // ATTENTION : fonction non terminée (commitée a titre de precaution)
309 // ATTENTION : fusioner le code avec celui de lValSet::Write
321 vector<string> tokens;
324 char str_lgrCalcGroupe[10];
326 //string implicitVRTransfertSyntax = "1.2.840.10008.1.2"; // supprime par rapport à Write
328 // Utilisées pour le calcul Group Length
330 guint32 lgrCalcGroupe=0;
331 gdcmElValue *elem, *elemZ, *elemZPrec;
332 guint16 grCourant = 0;
335 // Comment pourrait-on tester si on est TrueDicom ou non ,
336 // (FileType est un champ de gdcmHeader ...)
339 // On parcourt la table pour recalculer la longueur des 'elements 0x0000'
340 // au cas ou un tag ai été ajouté par rapport à ce qui a été lu
341 // dans l'image native
343 // cf : code IdDcmWriteFile dans libido/src/dcmwrite.c
345 if (1) // Risque de pb dans le calcul des lgr de chaque groupe. On le saute pour le moment!
347 for (TagElValueHT::iterator tag = tagHt.begin();
352 //printf("gr %04x el %04x lgr %d\n",elem->GetGroup(), elem->GetElement(), elem->GetLength());
354 if ( (elem->GetGroup() != grCourant) &&
355 (elem->GetGroup() != 0xfffe) ) { // On arrive sur un nv Groupe
357 //printf("Nouv Groupegr %04x el %04x \n",elem->GetGroup(), elem->GetElement());
361 if(elemZ->GetElement() != 0x0000) { // pas d'element 'Lgr groupe'
363 gdcmDictEntry * tagZ = new gdcmDictEntry(grCourant, 0x0000, "UL");
364 elemZ = new gdcmElValue(tagZ); // on le cree
366 Add(elemZ); // On l'accroche à sa place
370 //sprintf(str_lgrCalcGroupe,"%d",lgrCalcGroupe);
371 elemZPrec->SetValue(str_lgrCalcGroupe);
376 lgrCalcGroupe = 12; //2 + 2 + 4 + 4; // Gr + Num + Lgr + LgrGroupe
377 //printf ("lgrCalcGroupe %d\n",lgrCalcGroupe);
380 grCourant = elem->GetGroup();
382 } else { // On n'EST PAS sur un nv Groupe
384 //printf ("lgrCalcGroupe avant : %d LgrElem %d\n",lgrCalcGroupe,elem->GetLength());
386 lgrCalcGroupe += 2 + 2 + 4 + elem->GetLength(); // Gr + Num + Lgr + LgrElem
388 //printf ("lgrCalcGroupe apres %d\n",lgrCalcGroupe);
392 // Si on fait de l'implicit VR little Endian
393 // (pour moins se fairche sur processeur INTEL)
394 // penser a forcer le TRANSFERT SYNTAX UID
396 // supprime par rapport à Write
397 //SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
398 //SetElValueLengthByNumber(18, 0x0002, 0x0010); // Le 0 de fin de chaine doit etre stocké, dans ce cas
400 // restent à tester les echecs en écriture (apres chaque fwrite)
402 for (TagElValueHT::iterator tag2 = tagHt.begin();
406 gr = tag2->second->GetGroup();
407 // saut des groupes speciaux DICOM V3
408 if (gr < 0x0008) continue; // ajouté par rapport à Write
409 // saut des groupes impairs
410 if (gr %2) continue; // ajouté par rapport à Write
412 el = tag2->second->GetElement();
413 lgr = tag2->second->GetLength();
414 val = tag2->second->GetValue().c_str();
415 vr = tag2->second->GetVR();
417 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
418 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
420 // si on n'est pas en IMPLICIT VR voir pb (lgr + VR)
422 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp); //lgr
424 tokens.erase(tokens.begin(),tokens.end());
425 Tokenize (tag2->second->GetValue(), tokens, "\\");
427 if (vr == "US" || vr == "SS") {
429 for (unsigned int i=0; i<tokens.size();i++) {
430 val_uint16 = atoi(tokens[i].c_str());
432 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
437 if (vr == "UL" || vr == "SL") {
438 for (unsigned int i=0; i<tokens.size();i++) {
439 val_uint32 = atoi(tokens[i].c_str());
441 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
447 // Les pixels ne sont pas chargés dans l'element !
448 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
450 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem