4 #include "gdcmElValSet.h"
6 TagElValueHT & ElValSet::GetTagHt(void) {
10 void ElValSet::Add(ElValue * newElValue) {
11 tagHt [newElValue->GetKey()] = newElValue;
12 NameHt[newElValue->GetName()] = newElValue;
15 void ElValSet::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 ElValSet::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 ElValue* ElValSet::GetElementByNumber(guint32 group, guint32 element) {
38 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
39 if ( ! tagHt.count(key))
41 if (tagHt.count(key) > 1)
42 dbg.Verbose(0, "ElValSet::GetElementByNumber",
43 "multiple entries for this key (FIXME) !");
44 return tagHt.find(key)->second;
47 ElValue* ElValSet::GetElementByName(string TagName) {
48 if ( ! NameHt.count(TagName))
50 if (NameHt.count(TagName) > 1)
51 dbg.Verbose(0, "ElValSet::GetElement",
52 "multipe entries for this key (FIXME) !");
53 return NameHt.find(TagName)->second;
56 string ElValSet::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, "ElValSet::GetElValueByNumber",
62 "multiple entries for this key (FIXME) !");
63 return tagHt.find(key)->second->GetValue();
66 string ElValSet::GetElValueByName(string TagName) {
67 if ( ! NameHt.count(TagName))
68 return "gdcm::Unfound";
69 if (NameHt.count(TagName) > 1)
70 dbg.Verbose(0, "ElValSet::GetElValue",
71 "multipe entries for this key (FIXME) !");
72 return NameHt.find(TagName)->second->GetValue();
75 int ElValSet::SetElValueByNumber(string content, guint32 group, guint32 element) {
76 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
77 if ( ! tagHt.count(key))
79 if (tagHt.count(key) > 1) {
80 dbg.Verbose(0, "ElValSet::SetElValueByNumber",
81 "multiple entries for this key (FIXME) !");
84 tagHt[key]->SetValue(content);
86 // Question : m à j LgrElem ?
87 tagHt[key]->SetLength(strlen(content.c_str()));
89 // FIXME should we really update the element length ?
90 tagHt[key]->SetLength(content.length());
95 int ElValSet::SetElValueByName(string content, string TagName) {
96 if ( ! NameHt.count(TagName))
98 if (NameHt.count(TagName) > 1) {
99 dbg.Verbose(0, "ElValSet::SetElValueByName",
100 "multipe entries for this key (FIXME) !");
103 NameHt.find(TagName)->second->SetValue(content);
104 NameHt.find(TagName)->second->SetLength(strlen(content.c_str()));
109 int ElValSet::SetElValueLengthByNumber(guint32 l, guint32 group, guint32 element) {
110 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
111 if ( ! tagHt.count(key))
113 if (tagHt.count(key) > 1) {
114 dbg.Verbose(0, "ElValSet::SetElValueLengthByNumber",
115 "multiple entries for this key (FIXME) !");
119 tagHt[key]->SetLength(l);
124 int ElValSet::SetElValueLengthByName(guint32 l, string TagName) {
125 if ( ! NameHt.count(TagName))
127 if (NameHt.count(TagName) > 1) {
128 dbg.Verbose(0, "ElValSet::SetElValueByName",
129 "multipe entries for this key (FIXME) !");
132 NameHt.find(TagName)->second->SetLength(l);
137 int ElValSet::Write(FILE * _fp) {
139 // ATTENTION : fonction non terminée (commitée a titre de precaution)
148 char str_lgrCalcGroupe[10];
150 string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
152 // Utilisées pour le calcul Group Length
154 guint32 lgrCalcGroupe=0;
155 ElValue *elem, *elemZ, *elemZPrec;
156 guint16 grCourant = 0;
159 // Comment pourrait-on tester si on est TrueDicom ou non ,
160 // (FileType est un champ de gdcmHeader ...)
163 // On parcourt la table pour recalculer la longueur des 'elements 0x0000'
164 // au cas ou un tag ai été ajouté par rapport à ce qui a été lu
165 // dans l'image native
167 // cf : code IdDcmWriteFile dans libido/src/dcmwrite.c
169 for (TagElValueHT::iterator tag = tagHt.begin();
174 printf("gr %04x el %04x lgr %d\n",elem->GetGroup(), elem->GetElement(), elem->GetLength());
176 if ( (elem->GetGroup() != grCourant) &&
177 (elem->GetGroup() != 0xfffe) ) { // On arrive sur un nv Groupe
179 printf("Nouv Groupegr %04x el %04x \n",elem->GetGroup(), elem->GetElement());
183 if(elemZ->GetElement() != 0x0000) { // pas d'element 'Lgr groupe'
185 gdcmDictEntry * tagZ = new gdcmDictEntry(grCourant, 0x0000, "UL");
186 elemZ = new ElValue(tagZ); // on le cree
188 Add(elemZ); // On l'accroche à sa place
192 sprintf(str_lgrCalcGroupe,"%d",lgrCalcGroupe);
193 elemZPrec->SetValue(str_lgrCalcGroupe);
198 lgrCalcGroupe = 12; //2 + 2 + 4 + 4; // Gr + Num + Lgr + LgrGroupe
199 printf ("lgrCalcGroupe %d\n",lgrCalcGroupe);
202 grCourant = elem->GetGroup();
204 } else { // On n'EST PAS sur un nv Groupe
206 printf ("lgrCalcGroupe avant : %d LgrElem %d\n",lgrCalcGroupe,elem->GetLength());
208 lgrCalcGroupe += 2 + 2 + 4 + elem->GetLength(); // Gr + Num + Lgr + LgrElem
210 printf ("lgrCalcGroupe apres %d\n",lgrCalcGroupe);
215 // Si on fait de l'implicit VR little Endian
216 // (pour moins se fairche sur processeur INTEL)
217 // penser a forcer le TRANSFERT SYNTAX UID
219 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
220 SetElValueLengthByNumber(18, 0x0002, 0x0010); // Le 0 de fin de chaine doit etre stocké, dans ce cas
222 // restent à tester les echecs en écriture (apres chaque fwrite)
224 for (TagElValueHT::iterator tag2 = tagHt.begin();
229 // peut-on se passer des affectations?
230 // - passer l'adresse du resultat d'une fonction (???)
231 // - acceder au champ sans passer par un accesseur ?
233 gr = tag2->second->GetGroup();
234 el = tag2->second->GetElement();
235 lgr = tag2->second->GetLength();
236 val = tag2->second->GetValue().c_str();
237 vr = tag2->second->GetVR();
239 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
240 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
242 //fwrite ( vr,(size_t)2 ,(size_t)1 ,_fp); //VR
244 // si on n'est pas en IMPLICIT VR voir pb (lgr + VR)
246 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp); //lgr
248 if (vr == "US" || vr == "SS") {
249 val_int16 = atoi(val);
251 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
254 if (vr == "UL" || vr == "SL") {
255 val_int32 = atoi(val);
257 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
261 // Les pixels ne sont pas chargés dans l'element !
262 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
264 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem