4 #include "gdcmElValSet.h"
6 #ifdef GDCM_NO_ANSI_STRING_STREAM
8 # define ostringstream ostrstream
14 gdcmElValSet::~gdcmElValSet() {
15 for (TagElValueHT::iterator tag = tagHt.begin(); tag != tagHt.end(); ++tag) {
16 gdcmElValue* EntryToDelete = tag->second;
21 // Since Add() adds symetrical in both tagHt and NameHt we can
22 // assume all the pointed gdcmElValues are already cleaned-up when
28 * \ingroup gdcmElValSet
33 void gdcmElValSet::Add(gdcmElValue * newElValue) {
34 tagHt [newElValue->GetKey()] = newElValue;
35 NameHt[newElValue->GetName()] = newElValue;
37 // WARNING : push_bash in listElem ONLY during ParseHeader
38 // TODO : something to allow further Elements addition
39 // position to be taken care of !
40 listElem.push_back(newElValue);
44 * \ingroup gdcmElValSet
45 * \brief Checks if a given Dicom element exists
51 int gdcmElValSet::CheckIfExistByNumber(guint16 Group, guint16 Elem ) {
52 std::string key = TranslateToKey(Group, Elem );
53 return (tagHt.count(key));
57 * \ingroup gdcmElValSet
60 void gdcmElValSet::Print(std::ostream & os) {
63 unsigned short int g, e;
66 gdcmTS * ts = gdcmGlobal::GetTS();
68 std::cout << "------------- using tagHt ---------------------" << std::endl;
70 for (TagElValueHT::iterator tag = tagHt.begin();
73 g = tag->second->GetGroup();
74 e = tag->second->GetElement();
75 v = tag->second->GetValue();
76 o = tag->second->GetOffset();
77 d2 = _CreateCleanString(v); // replace non printable characters by '.'
79 os << tag->first << ": ";
80 os << " lgr : " << tag->second->GetLength();
81 os << ", Offset : " << o;
82 os << " x(" << std::hex << o << std::dec << ") ";
83 os << "\t[" << tag->second->GetVR() << "]";
84 os << "\t[" << tag->second->GetName() << "]";
85 os << "\t[" << d2 << "]";
87 // Display the UID value (instead of displaying the rough code)
88 if (g == 0x0002) { // Some more to be displayed ?
89 if ( (e == 0x0010) || (e == 0x0002) )
90 os << " ==>\t[" << ts->GetValue(v) << "]";
93 if ( (e == 0x0016) || (e == 0x1150) )
94 os << " ==>\t[" << ts->GetValue(v) << "]";
100 std::cout << "------------ using listElem -------------------" << std::endl;
103 char greltag[10]; //group element tag
105 for (ListTag::iterator i = listElem.begin();
108 g = (*i)->GetGroup();
109 e = (*i)->GetElement();
110 v = (*i)->GetValue();
111 o = (*i)->GetOffset();
112 sprintf(greltag,"%04x|%04x",g,e);
113 d2 = _CreateCleanString(v); // replace non printable characters by '.'
114 os << greltag << ": lgth : ";
115 lgth = (*i)->GetReadLength();
116 if ( lgth == 0xffffffff)
117 os << std::hex << lgth << std::dec ;
120 os << ", Offset : " << o;
121 os << " x(" << std::hex << o << std::dec << ") ";
122 os << "\t[" << (*i)->GetVR() << "]";
123 os << "\t[" << (*i)->GetName() << "]";
124 os << "\t[" << d2 << "]";
126 // Display the UID value (instead of displaying the rough code)
127 if (g == 0x0002) { // Any more to be displayed ?
128 if ( (e == 0x0010) || (e == 0x0002) )
129 os << " ==>\t[" << ts->GetValue(v) << "]";
132 if ( (e == 0x0016) || (e == 0x1150) )
133 os << " ==>\t[" << ts->GetValue(v) << "]";
141 * \ingroup gdcmElValSet
144 void gdcmElValSet::PrintByName(std::ostream & os) {
145 for (TagElValueNameHT::iterator tag = NameHt.begin();
148 os << tag->first << ": ";
149 os << "[" << tag->second->GetValue() << "]";
150 os << "[" << tag->second->GetKey() << "]";
151 os << "[" << tag->second->GetVR() << "]" << std::endl;
156 * \ingroup gdcmElValSet
162 gdcmElValue* gdcmElValSet::GetElementByNumber(guint16 group, guint16 element) {
163 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
164 if ( ! tagHt.count(key))
165 return (gdcmElValue*)0;
166 return tagHt.find(key)->second;
170 * \ingroup gdcmElValSet
174 gdcmElValue* gdcmElValSet::GetElementByName(std::string TagName) {
175 if ( ! NameHt.count(TagName))
176 return (gdcmElValue*)0;
177 return NameHt.find(TagName)->second;
181 * \ingroup gdcmElValSet
187 std::string gdcmElValSet::GetElValueByNumber(guint16 group, guint16 element) {
188 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
189 if ( ! tagHt.count(key))
191 return tagHt.find(key)->second->GetValue();
195 * \ingroup gdcmElValSet
199 std::string gdcmElValSet::GetElValueByName(std::string TagName) {
200 if ( ! NameHt.count(TagName))
202 return NameHt.find(TagName)->second->GetValue();
206 * \ingroup gdcmElValSet
213 int gdcmElValSet::SetElValueByNumber(std::string content,
214 guint16 group, guint16 element) {
215 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
216 if ( ! tagHt.count(key))
218 int l = content.length();
219 if(l%2) { // Odd length are padded with a space (020H).
221 content = content + '\0';
223 tagHt[key]->SetValue(content);
225 std::string vr = tagHt[key]->GetVR();
228 if( (vr == "US") || (vr == "SS") )
230 else if( (vr == "UL") || (vr == "SL") )
234 tagHt[key]->SetLength(lgr);
239 * \ingroup gdcmElValSet
245 int gdcmElValSet::SetElValueByName(std::string content, std::string TagName) {
246 if ( ! NameHt.count(TagName))
248 int l = content.length();
249 if(l%2) { // Odd length are padded with a space (020H).
251 // Well. I know that '/0' is NOT a space
252 // but it doesn't work with a space.
253 // Use hexedit and see 0002|0010 value (Transfer Syntax UID)
254 content = content + '\0';
256 NameHt[TagName]->SetValue(content);
258 std::string vr = NameHt[TagName]->GetVR();
261 if( (vr == "US") || (vr == "SS") )
263 else if( (vr == "UL") || (vr == "SL") )
266 lgr = content.length();
268 // TODO : WARNING: le cas de l'element des pixels (7fe0,0010) n'est pas traite
269 // par SetElValueByName
270 // il faudra utiliser SetElValueByNumber
272 NameHt[TagName]->SetLength(lgr);
277 * \ingroup gdcmElValSet
278 * \brief Generate a free TagKey i.e. a TagKey that is not present
279 * in the TagHt dictionary.
280 * @param group The generated tag must belong to this group.
281 * @return The element of tag with given group which is fee.
283 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
284 for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
285 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
286 if (tagHt.count(key) == 0)
293 * \ingroup gdcmElValSet
300 int gdcmElValSet::SetVoidAreaByNumber(void * area,
301 guint16 group, guint16 element) {
302 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
303 if ( ! tagHt.count(key))
305 tagHt[key]->SetVoidArea(area);
310 * \ingroup gdcmElValSet
315 * @return int acts as a boolean
317 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
318 guint16 group, guint16 element) {
319 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
320 if ( ! tagHt.count(key))
322 if (length%2) length++; // length must be even
323 tagHt[key]->SetLength(length);
327 * \ingroup gdcmElValSet
333 int gdcmElValSet::SetElValueLengthByName(guint32 length, std::string TagName) {
334 if ( ! NameHt.count(TagName))
336 if (length%2) length++; // length must be even
337 NameHt.find(TagName)->second->SetLength(length);
342 * \ingroup gdcmElValSet
343 * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
344 * @param SkipSequence TRUE if we don't want to write Sequences (ACR-NEMA Files)
347 void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
353 std::string str_trash;
356 GroupHT groupHt; // to hold the length of each group
359 // typedef std::map<GroupKey, int> GroupHT;
363 // for each Tag in the DCM Header
365 for (TagElValueHT::iterator tag2 = tagHt.begin();
370 gr = elem->GetGroup();
371 el = elem->GetElement();
374 sprintf(trash, "%04x", gr);
375 key = trash; // generate 'group tag'
377 // if the caller decided not to take SEQUENCEs into account
378 // e.g : he wants to write an ACR-NEMA File
380 if (SkipSequence && vr == "SQ") continue;
382 // Still unsolved problem :
383 // we cannot find the 'Sequence Delimitation Item'
384 // since it's at the end of the Hash Table
387 // pas SEQUENCE en ACR-NEMA
389 // --> la descente a l'interieur' des SQ
390 // devrait etre faite avec une liste chainee, pas avec une HTable...
392 if ( groupHt.count(key) == 0) { // we just read the first elem of a given group
393 if (el == 0x0000) { // the first elem is 0x0000
394 groupHt[key] = 0; // initialize group length
396 groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
398 } else { // any elem but the first
399 if (type == ExplicitVR) {
400 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
401 groupHt[key] += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
404 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
408 unsigned short int gr_bid;
410 for (GroupHT::iterator g = groupHt.begin(); // for each group we found
413 // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
414 // warning: unsigned int format, different type arg
415 sscanf(g->first.c_str(),"%x",&gr_bid);
416 tk = g->first + "|0000"; // generate the element full tag
418 if ( tagHt.count(tk) == 0) { // if element 0x0000 not found
419 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
420 elemZ = new gdcmElValue(tagZ);
422 Add(elemZ); // create it
424 elemZ=GetElementByNumber(gr_bid, 0x0000);
426 sprintf(trash ,"%d",g->second);
428 elemZ->SetValue(str_trash);
433 * \ingroup gdcmElValSet
434 * \brief writes on disc according to the requested format
435 * \ (ACR-NEMA, DICOM, RAW) the image
436 * \ warning does NOT add the missing elements in the header :
437 * \ it's up to the user doing it !
438 * \ (function CheckHeaderCoherence to be written)
439 * @param type type of the File to be written
440 * (ACR-NEMA, DICOM, RAW)
441 * @param _fp already open file pointer
444 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
452 std::vector<std::string> tokens;
454 // TODO : use listElem to iterate, not TagHt!
455 // pb : gdcmElValSet.Add does NOT update listElem
456 // find a trick in STL to do it, at low cost !
460 // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian
461 // restent a tester les echecs en ecriture (apres chaque fwrite)
463 for (TagElValueHT::iterator tag2=tagHt.begin();
467 gr = tag2->second->GetGroup();
468 el = tag2->second->GetElement();
469 lgr = tag2->second->GetLength();
470 val = tag2->second->GetValue().c_str();
471 vr = tag2->second->GetVR();
473 // std::cout << "Tag "<< std::hex << gr << " " << el << std::endl;
476 if (gr < 0x0008) continue; // ignore pure DICOM V3 groups
477 if (gr %2) continue; // ignore shadow groups
478 if (vr == "SQ" ) continue; // ignore Sequences
479 if (gr == 0xfffe ) continue; // ignore delimiters
482 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
483 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
485 if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
487 guint16 z=0, shortLgr;
488 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
490 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
491 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
492 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
496 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
498 } else { // IMPLICIT VR
499 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
502 if (vr == "US" || vr == "SS") {
503 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
504 Tokenize (tag2->second->GetValue(), tokens, "\\");
505 for (unsigned int i=0; i<tokens.size();i++) {
506 val_uint16 = atoi(tokens[i].c_str());
508 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
513 if (vr == "UL" || vr == "SL") {
514 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
515 Tokenize (tag2->second->GetValue(), tokens, "\\");
516 for (unsigned int i=0; i<tokens.size();i++) {
517 val_uint32 = atoi(tokens[i].c_str());
519 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
524 // Pixels are never loaded in the element !
525 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
527 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
532 * \ingroup gdcmElValSet
538 int gdcmElValSet::Write(FILE * _fp, FileType type) {
541 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non
542 // (FileType est un champ de gdcmHeader ...)
543 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
545 // a moins de se livrer a un tres complique ajout des champs manquants.
546 // faire un CheckAndCorrectHeader (?)
548 if ( (type == ImplicitVR) || (type == ExplicitVR) )
549 UpdateGroupLength(false,type);
551 UpdateGroupLength(true,ACR);
553 WriteElements(type, _fp);