5 #include "gdcmElValSet.h"
9 gdcmElValSet::~gdcmElValSet() {
10 for (TagElValueHT::iterator tag = tagHt.begin(); tag != tagHt.end(); ++tag) {
11 gdcmElValue* EntryToDelete = tag->second;
16 // Since Add() adds symetrical in both tagHt and NameHt we can
17 // assume all the pointed gdcmElValues are allready cleaned-up when
22 TagElValueHT & gdcmElValSet::GetTagHt(void) {
27 * \ingroup gdcmElValSet
32 void gdcmElValSet::Add(gdcmElValue * newElValue) {
33 tagHt [newElValue->GetKey()] = newElValue;
34 NameHt[newElValue->GetName()] = newElValue;
39 * \ingroup gdcmElValSet
40 * \brief Checks if a given Dicom element exists
45 int gdcmElValSet::CheckIfExistByNumber(guint16 Group, guint16 Elem ) {
46 string key = TranslateToKey(Group, Elem );
47 return (tagHt.count(key));
51 * \ingroup gdcmElValSet
56 void gdcmElValSet::Print(ostream & os) {
63 gdcmTS * ts = gdcmGlobal::GetTS();
65 for (TagElValueHT::iterator tag = tagHt.begin();
68 g = tag->second->GetGroup();
69 e = tag->second->GetElement();
70 v = tag->second->GetValue();
71 o = tag->second->GetOffset();
72 d = _CreateCleanString(v); // TODO : trouver qq chose moins goret
75 os << tag->first << ": ";
76 //os << "[" << v << "]";
77 os << "[" << d2 << "]";
78 os << "[" << tag->second->GetName() << "]";
79 os << "[" << tag->second->GetVR() << "]";
81 if ( (g == 0x0002) && (e == 0x0010) ) {
82 os << " [" << ts->GetValue(v) << "]";
87 os << " lgr : " << tag->second->GetLength();
88 os << ", Offset : " << o;
89 os << " x(" << hex << o << dec << ") ";
95 * \ingroup gdcmElValSet
100 void gdcmElValSet::PrintByName(ostream & os) {
101 for (TagElValueNameHT::iterator tag = NameHt.begin();
104 os << tag->first << ": ";
105 os << "[" << tag->second->GetValue() << "]";
106 os << "[" << tag->second->GetKey() << "]";
107 os << "[" << tag->second->GetVR() << "]" << endl;
112 * \ingroup gdcmElValSet
117 gdcmElValue* gdcmElValSet::GetElementByNumber(guint16 group, guint16 element) {
118 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
119 if ( ! tagHt.count(key))
120 return (gdcmElValue*)0;
121 return tagHt.find(key)->second;
125 * \ingroup gdcmElValSet
130 gdcmElValue* gdcmElValSet::GetElementByName(string TagName) {
131 if ( ! NameHt.count(TagName))
132 return (gdcmElValue*)0;
133 return NameHt.find(TagName)->second;
137 * \ingroup gdcmElValSet
142 string gdcmElValSet::GetElValueByNumber(guint16 group, guint16 element) {
143 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
144 if ( ! tagHt.count(key))
145 return "gdcm::Unfound";
146 return tagHt.find(key)->second->GetValue();
150 * \ingroup gdcmElValSet
155 string gdcmElValSet::GetElValueByName(string TagName) {
156 if ( ! NameHt.count(TagName))
157 return "gdcm::Unfound";
158 return NameHt.find(TagName)->second->GetValue();
162 * \ingroup gdcmElValSet
167 int gdcmElValSet::SetElValueByNumber(string content,
168 guint16 group, guint16 element) {
169 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
170 if ( ! tagHt.count(key))
172 tagHt[key]->SetValue(content);
173 string vr = tagHt[key]->GetVR();
176 if( (vr == "US") || (vr == "SS") )
178 else if( (vr == "UL") || (vr == "SL") )
181 lgr = content.length();
182 tagHt[key]->SetLength(lgr);
187 * \ingroup gdcmElValSet
192 int gdcmElValSet::SetElValueByName(string content, string TagName) {
193 if ( ! NameHt.count(TagName))
195 NameHt[TagName]->SetValue(content);
196 string vr = NameHt[TagName]->GetVR();
199 if( (vr == "US") || (vr == "SS") )
201 else if( (vr == "UL") || (vr == "SL") )
204 lgr = content.length();
206 // TODO : WARNING: le cas de l'element des pixels (7fe0,0010) n'est pas traite
207 // par SetElValueByName
208 // il faudra utiliser SetElValueByNumber
210 NameHt[TagName]->SetLength(lgr);
215 * \ingroup gdcmElValSet
216 * \brief Generate a free TagKey i.e. a TagKey that is not present
217 * in the TagHt dictionary.
218 * @param group The generated tag must belong to this group.
219 * @return The element of tag with given group which is fee.
221 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
222 for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
223 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
224 if (tagHt.count(key) == 0)
231 * \ingroup gdcmElValSet
236 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
237 guint16 group, guint16 element) {
238 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
239 if ( ! tagHt.count(key))
241 tagHt[key]->SetLength(length);
247 * \ingroup gdcmElValSet
252 int gdcmElValSet::SetElValueLengthByName(guint32 length, string TagName) {
253 if ( ! NameHt.count(TagName))
255 NameHt.find(TagName)->second->SetLength(length);
260 // re-computes the length of a ACR-NEMA/Dicom group
264 * \ingroup gdcmElValSet
269 void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
278 GroupHT groupHt; // to hold the length of each group
281 // typedef std::map<GroupKey, int> GroupHT;
285 // for each Tag in the DCM Header
287 for (TagElValueHT::iterator tag2 = tagHt.begin();
292 gr = elem->GetGroup();
293 el = elem->GetElement();
296 sprintf(trash, "%04x", gr);
297 key = trash; // generate 'group tag'
299 // if the caller decided not to take SEQUENCEs into account
300 // e.g : he wants to write an ACR-NEMA File
302 if (SkipSequence && vr == "SQ") continue;
304 // pas SEQUENCE en ACR-NEMA
305 // WARNING : pb CERTAIN
306 // si on est descendu 'a l'interieur' des SQ
308 // --> la descente a l'interieur' des SQ
309 // devra etre faite avec une liste chainee, pas avec une HTable...
311 if ( groupHt.count(key) == 0) { // we just read the first elem of a given group
312 if (el == 0x0000) { // the first elem is 0x0000
313 groupHt[key] = 0; // initialize group length
315 groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
317 } else { // any elem but the first
318 if (type == ExplicitVR) {
319 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
320 groupHt[key] += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
323 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
327 if(1) // unnormalized way to see what happened
328 for (GroupHT::iterator g = groupHt.begin();
331 printf("groupKey %s : %d\n",g->first.c_str(),g->second);
334 unsigned short int gr_bid;
336 for (GroupHT::iterator g = groupHt.begin(); // for each group we found
339 // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
340 // warning: unsigned int format, different type arg
341 sscanf(g->first.c_str(),"%x",&gr_bid);
342 tk = g->first + "|0000"; // generate the element full tag
344 if ( tagHt.count(tk) == 0) { // if element 0x0000 not found
345 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
346 elemZ = new gdcmElValue(tagZ);
348 Add(elemZ); // create it
350 elemZ=GetElementByNumber(gr_bid, 0x0000);
352 sprintf(trash ,"%d",g->second);
354 elemZ->SetValue(str_trash);
359 * \ingroup gdcmElValSet
364 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
372 vector<string> tokens;
376 // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian
377 // restent à tester les echecs en écriture (apres chaque fwrite)
379 for (TagElValueHT::iterator tag2=tagHt.begin();
383 gr = tag2->second->GetGroup();
384 el = tag2->second->GetElement();
385 lgr = tag2->second->GetLength();
386 val = tag2->second->GetValue().c_str();
387 vr = tag2->second->GetVR();
390 if (gr < 0x0008) continue;
391 // if (gr %2) continue; // pour voir
392 if (vr == "SQ" ) continue;
395 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
396 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
398 if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
399 // On est en EXPLICIT VR
400 guint16 z=0, shortLgr;
401 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
403 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
404 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
405 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
409 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
412 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
415 tokens.erase(tokens.begin(),tokens.end());
416 Tokenize (tag2->second->GetValue(), tokens, "\\");
418 if (vr == "US" || vr == "SS") {
419 for (unsigned int i=0; i<tokens.size();i++) {
420 val_uint16 = atoi(tokens[i].c_str());
422 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
426 if (vr == "UL" || vr == "SL") {
427 for (unsigned int i=0; i<tokens.size();i++) {
428 val_uint32 = atoi(tokens[i].c_str());
430 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
436 // Les pixels ne sont pas chargés dans l'element !
437 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
439 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
444 * \ingroup gdcmElValSet
449 int gdcmElValSet::Write(FILE * _fp, FileType type) {
451 if (type == ImplicitVR) {
452 string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
453 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
455 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
456 // values with a VR of UI shall be padded with a single trailing null
457 // Dans le cas suivant on doit pader manuellement avec un 0
459 SetElValueLengthByNumber(18, 0x0002, 0x0010);
462 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non ,
463 // (FileType est un champ de gdcmHeader ...)
464 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
467 if (type == ExplicitVR) {
468 string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
469 SetElValueByNumber(explicitVRTransfertSyntax, 0x0002, 0x0010);
471 SetElValueLengthByNumber(20, 0x0002, 0x0010);
474 if ( (type == ImplicitVR) || (type == ExplicitVR) )
475 UpdateGroupLength(false,type);
477 UpdateGroupLength(true,ACR);
479 WriteElements(type, _fp);