5 #include "gdcmElValSet.h"
10 TagElValueHT & gdcmElValSet::GetTagHt(void) {
14 void gdcmElValSet::Add(gdcmElValue * newElValue) {
15 tagHt [newElValue->GetKey()] = newElValue;
16 NameHt[newElValue->GetName()] = newElValue;
19 void gdcmElValSet::Print(ostream & os) {
20 for (TagElValueHT::iterator tag = tagHt.begin();
23 os << tag->first << ": ";
24 os << "[" << tag->second->GetValue() << "]";
25 os << "[" << tag->second->GetName() << "]";
26 os << "[" << tag->second->GetVR() << "]";
27 os << " lgr : " << tag->second->GetLength();
32 void gdcmElValSet::PrintByName(ostream & os) {
33 for (TagElValueNameHT::iterator tag = NameHt.begin();
36 os << tag->first << ": ";
37 os << "[" << tag->second->GetValue() << "]";
38 os << "[" << tag->second->GetKey() << "]";
39 os << "[" << tag->second->GetVR() << "]" << endl;
43 gdcmElValue* gdcmElValSet::GetElementByNumber(guint16 group, guint16 element) {
44 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
45 if ( ! tagHt.count(key))
46 return (gdcmElValue*)0;
47 return tagHt.find(key)->second;
50 gdcmElValue* gdcmElValSet::GetElementByName(string TagName) {
51 if ( ! NameHt.count(TagName))
52 return (gdcmElValue*)0;
53 return NameHt.find(TagName)->second;
56 string gdcmElValSet::GetElValueByNumber(guint16 group, guint16 element) {
57 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
58 if ( ! tagHt.count(key))
59 return "gdcm::Unfound";
60 return tagHt.find(key)->second->GetValue();
63 string gdcmElValSet::GetElValueByName(string TagName) {
64 if ( ! NameHt.count(TagName))
65 return "gdcm::Unfound";
66 return NameHt.find(TagName)->second->GetValue();
70 int gdcmElValSet::SetElValueByNumber(string content,
71 guint16 group, guint16 element) {
72 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
73 if ( ! tagHt.count(key))
75 tagHt[key]->SetValue(content);
76 tagHt[key]->SetLength(content.length());
80 int gdcmElValSet::SetElValueByName(string content, string TagName) {
81 if ( ! NameHt.count(TagName))
83 NameHt[TagName]->SetValue(content);
84 NameHt[TagName]->SetLength(content.length());
89 * \ingroup gdcmElValSet
90 * \brief Generate a free TagKey i.e. a TagKey that is not present
91 * in the TagHt dictionary.
92 * @param group The generated tag must belong to this group.
93 * @return The element of tag with given group which is fee.
95 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
96 for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
97 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
98 if (tagHt.count(key) == 0)
104 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
105 guint16 group, guint16 element) {
106 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
107 if ( ! tagHt.count(key))
109 tagHt[key]->SetLength(length);
114 int gdcmElValSet::SetElValueLengthByName(guint32 length, string TagName) {
115 if ( ! NameHt.count(TagName))
117 NameHt.find(TagName)->second->SetLength(length);
121 void gdcmElValSet::UpdateGroupLength(bool SkipSequence) {
122 // On parcourt la table pour recalculer la longueur des 'elements 0x0000'
123 // au cas ou un tag ai été ajouté par rapport à ce qui a été lu
124 // dans l'image native
126 // cf : code IdDcmWriteFile dans libido/src/dcmwrite.c
128 // On fait de l'implicit VR little Endian
129 // (pour moins se fairche sur processeur INTEL)
130 // On force le TRANSFERT SYNTAX UID
133 guint32 lgrCalcGroupe=0;
134 gdcmElValue *elem, *elemZ, *elemZPrec;
135 guint16 grCourant = 0;
137 TagElValueHT::iterator tag = tagHt.begin();
140 gr = elem->GetGroup();
141 el = elem->GetElement();
144 if(DEBUG)printf("ajout elem OOOO premiere fois\n");
145 gdcmDictEntry * tagZ = new gdcmDictEntry(gr, 0x0000, "UL");
146 elemZPrec = new gdcmElValue(tagZ); // on le cree
147 elemZPrec->SetLength(4);
148 Add(elemZPrec); // On l'accroche à sa place
151 if(DEBUG)printf("Pas d'ajout elem OOOO premiere fois\n");
154 if(DEBUG)printf("init-1 lgr (%d) pour gr %04x\n",lgrCalcGroupe, gr);
157 for (tag = ++tagHt.begin();
162 gr = elem->GetGroup();
163 el = elem->GetElement();
166 if (SkipSequence && vr == "SQ")
167 // pas SEQUENCE en ACR-NEMA
168 // WARNING : risque de pb
169 // si on est descendu 'a l'interieur' des SQ
173 if ( (gr != grCourant) /*&& // On arrive sur un nv Groupe
174 (el != 0xfffe) */ ) {
177 gdcmDictEntry * tagZ = new gdcmDictEntry(gr, 0x0000, "UL");
178 elemZ = new gdcmElValue(tagZ); // on le cree
180 Add(elemZ); // On l'accroche à sa place
181 if(DEBUG)printf("ajout elem OOOO pour gr %04x\n",gr);
184 if(DEBUG)printf("maj elemZ\n");
189 //sprintf(str_lgrCalcGroupe,"%d",lgrCalcGroupe);
190 elemZPrec->SetValue(f.str());
191 if(DEBUG)printf("ecriture lgr (%d, %s) pour gr %04x\n",lgrCalcGroupe, f.str().c_str(), grCourant);
192 if(DEBUG)printf ("%04x %04x [%s]\n",elemZPrec->GetGroup(), elemZPrec->GetElement(),
193 elemZPrec->GetValue().c_str());
194 if(DEBUG)cout << "Addresse elemZPrec " << elemZPrec<< endl;
198 if(DEBUG)printf("init-2 lgr (%d) pour gr %04x\n",lgrCalcGroupe, gr);
199 } else { // On n'EST PAS sur un nv Groupe
200 lgrCalcGroupe += 2 + 2 + 4 + elem->GetLength(); // Gr + Num + Lgr + LgrElem
201 if(DEBUG)printf("increment (%d) el %04x-->lgr (%d) pour gr %04x\n",
202 elem->GetLength(), el, lgrCalcGroupe, gr);
207 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
215 vector<string> tokens;
219 // restent à tester les echecs en écriture (apres chaque fwrite)
220 for (TagElValueHT::iterator tag2 = tagHt.begin();
224 gr = tag2->second->GetGroup();
225 el = tag2->second->GetElement();
226 lgr = tag2->second->GetLength();
227 val = tag2->second->GetValue().c_str();
228 vr = tag2->second->GetVR();
229 if(DEBUG)printf ("%04x %04x [%s] : [%s]\n",gr, el, vr.c_str(), val);
232 if (gr < 0x0008) continue;
234 if (vr == "SQ" ) continue;
238 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
239 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
241 if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
242 // On est en EXPLICIT VR
243 guint16 z=0, shortLgr;
244 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
246 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
247 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
248 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
252 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
255 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
258 tokens.erase(tokens.begin(),tokens.end());
259 Tokenize (tag2->second->GetValue(), tokens, "\\");
261 if (vr == "US" || vr == "SS") {
262 for (unsigned int i=0; i<tokens.size();i++) {
263 val_uint16 = atoi(tokens[i].c_str());
265 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
269 if (vr == "UL" || vr == "SL") {
270 for (unsigned int i=0; i<tokens.size();i++) {
271 val_uint32 = atoi(tokens[i].c_str());
273 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
278 // Les pixels ne sont pas chargés dans l'element !
279 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
281 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
285 int gdcmElValSet::Write(FILE * _fp, FileType type) {
287 if (type == ImplicitVR) {
288 string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
289 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
290 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
291 // values with a VR of UI shall be padded with a single trailing null
292 // Dans le cas suivant on doit pader manuellement avec un 0.
293 SetElValueLengthByNumber(18, 0x0002, 0x0010);
295 if (type == ExplicitVR) {
296 string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
297 SetElValueByNumber(explicitVRTransfertSyntax, 0x0002, 0x0010);
299 SetElValueLengthByNumber(20, 0x0002, 0x0010);
302 if ( (type == ImplicitVR) || (type == ExplicitVR) )
305 UpdateGroupLength(true);
307 WriteElements(type, _fp);