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 for (TagElValueHT::iterator tag = tagHt.begin();
71 g = tag->second->GetGroup();
72 e = tag->second->GetElement();
73 v = tag->second->GetValue();
74 o = tag->second->GetOffset();
75 d2 = _CreateCleanString(v); // replace non printable characters by '.'
77 os << tag->first << ": ";
78 os << " lgr : " << tag->second->GetLength();
79 os << ", Offset : " << o;
80 os << " x(" << std::hex << o << std::dec << ") ";
81 os << "\t[" << tag->second->GetVR() << "]";
82 os << "\t[" << tag->second->GetName() << "]";
83 os << "\t[" << d2 << "]";
85 // Display the UID value (instead of displaying the rough code)
86 if (g == 0x0002) { // Some more to be displayed ?
87 if ( (e == 0x0010) || (e == 0x0002) )
88 os << " ==>\t[" << ts->GetValue(v) << "]";
91 if ( (e == 0x0016) || (e == 0x1150) )
92 os << " ==>\t[" << ts->GetValue(v) << "]";
99 char greltag[10]; //group element tag
101 for (ListTag::iterator i = listElem.begin();
104 g = (*i)->GetGroup();
105 e = (*i)->GetElement();
106 v = (*i)->GetValue();
107 o = (*i)->GetOffset();
108 sprintf(greltag,"%04x|%04x",g,e);
109 d2 = _CreateCleanString(v); // replace non printable characters by '.'
110 os << greltag << ": lgth : ";
111 lgth = (*i)->GetReadLength();
112 if ( lgth == 0xffffffff)
113 os << std::hex << lgth << std::dec ;
116 os << ", Offset : " << o;
117 os << " x(" << std::hex << o << std::dec << ") ";
118 os << "\t[" << (*i)->GetVR() << "]";
119 os << "\t[" << (*i)->GetName() << "]";
120 os << "\t[" << d2 << "]";
122 // Display the UID value (instead of displaying the rough code)
123 if (g == 0x0002) { // Any more to be displayed ?
124 if ( (e == 0x0010) || (e == 0x0002) )
125 os << " ==>\t[" << ts->GetValue(v) << "]";
128 if ( (e == 0x0016) || (e == 0x1150) )
129 os << " ==>\t[" << ts->GetValue(v) << "]";
137 * \ingroup gdcmElValSet
140 void gdcmElValSet::PrintByName(std::ostream & os) {
141 for (TagElValueNameHT::iterator tag = NameHt.begin();
144 os << tag->first << ": ";
145 os << "[" << tag->second->GetValue() << "]";
146 os << "[" << tag->second->GetKey() << "]";
147 os << "[" << tag->second->GetVR() << "]" << std::endl;
152 * \ingroup gdcmElValSet
158 gdcmElValue* gdcmElValSet::GetElementByNumber(guint16 group, guint16 element) {
159 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
160 if ( ! tagHt.count(key))
161 return (gdcmElValue*)0;
162 return tagHt.find(key)->second;
166 * \ingroup gdcmElValSet
170 gdcmElValue* gdcmElValSet::GetElementByName(std::string TagName) {
171 if ( ! NameHt.count(TagName))
172 return (gdcmElValue*)0;
173 return NameHt.find(TagName)->second;
177 * \ingroup gdcmElValSet
183 std::string gdcmElValSet::GetElValueByNumber(guint16 group, guint16 element) {
184 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
185 if ( ! tagHt.count(key))
187 return tagHt.find(key)->second->GetValue();
191 * \ingroup gdcmElValSet
195 std::string gdcmElValSet::GetElValueByName(std::string TagName) {
196 if ( ! NameHt.count(TagName))
198 return NameHt.find(TagName)->second->GetValue();
202 * \ingroup gdcmElValSet
209 int gdcmElValSet::SetElValueByNumber(std::string content,
210 guint16 group, guint16 element) {
211 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
212 if ( ! tagHt.count(key))
214 int l = content.length();
215 if(l%2) { // Odd length are padded with a space (020H).
217 content = content + '\0';
219 tagHt[key]->SetValue(content);
221 std::string vr = tagHt[key]->GetVR();
224 if( (vr == "US") || (vr == "SS") )
226 else if( (vr == "UL") || (vr == "SL") )
230 tagHt[key]->SetLength(lgr);
235 * \ingroup gdcmElValSet
241 int gdcmElValSet::SetElValueByName(std::string content, std::string TagName) {
242 if ( ! NameHt.count(TagName))
244 int l = content.length();
245 if(l%2) { // Odd length are padded with a space (020H).
247 // Well. I know that '/0' is NOT a space
248 // but it doesn't work with a space.
249 // Use hexedit and see 0002|0010 value (Transfer Syntax UID)
250 content = content + '\0';
252 NameHt[TagName]->SetValue(content);
254 std::string vr = NameHt[TagName]->GetVR();
257 if( (vr == "US") || (vr == "SS") )
259 else if( (vr == "UL") || (vr == "SL") )
262 lgr = content.length();
264 // TODO : WARNING: le cas de l'element des pixels (7fe0,0010) n'est pas traite
265 // par SetElValueByName
266 // il faudra utiliser SetElValueByNumber
268 NameHt[TagName]->SetLength(lgr);
273 * \ingroup gdcmElValSet
274 * \brief Generate a free TagKey i.e. a TagKey that is not present
275 * in the TagHt dictionary.
276 * @param group The generated tag must belong to this group.
277 * @return The element of tag with given group which is fee.
279 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
280 for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
281 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
282 if (tagHt.count(key) == 0)
289 * \ingroup gdcmElValSet
296 int gdcmElValSet::SetVoidAreaByNumber(void * area,
297 guint16 group, guint16 element) {
298 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
299 if ( ! tagHt.count(key))
301 tagHt[key]->SetVoidArea(area);
306 * \ingroup gdcmElValSet
311 * @return int acts as a boolean
313 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
314 guint16 group, guint16 element) {
315 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
316 if ( ! tagHt.count(key))
318 if (length%2) length++; // length must be even
319 tagHt[key]->SetLength(length);
323 * \ingroup gdcmElValSet
329 int gdcmElValSet::SetElValueLengthByName(guint32 length, std::string TagName) {
330 if ( ! NameHt.count(TagName))
332 if (length%2) length++; // length must be even
333 NameHt.find(TagName)->second->SetLength(length);
338 * \ingroup gdcmElValSet
339 * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
340 * @param SkipSequence TRUE if we don't want to write Sequences (ACR-NEMA Files)
343 void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
349 std::string str_trash;
352 GroupHT groupHt; // to hold the length of each group
355 // typedef std::map<GroupKey, int> GroupHT;
359 // for each Tag in the DCM Header
361 for (TagElValueHT::iterator tag2 = tagHt.begin();
366 gr = elem->GetGroup();
367 el = elem->GetElement();
370 sprintf(trash, "%04x", gr);
371 key = trash; // generate 'group tag'
373 // if the caller decided not to take SEQUENCEs into account
374 // e.g : he wants to write an ACR-NEMA File
376 if (SkipSequence && vr == "SQ") continue;
378 // Still unsolved problem :
379 // we cannot find the 'Sequence Delimitation Item'
380 // since it's at the end of the Hash Table
383 // pas SEQUENCE en ACR-NEMA
385 // --> la descente a l'interieur' des SQ
386 // devrait etre faite avec une liste chainee, pas avec une HTable...
388 if ( groupHt.count(key) == 0) { // we just read the first elem of a given group
389 if (el == 0x0000) { // the first elem is 0x0000
390 groupHt[key] = 0; // initialize group length
392 groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
394 } else { // any elem but the first
395 if (type == ExplicitVR) {
396 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
397 groupHt[key] += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
400 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
404 unsigned short int gr_bid;
406 for (GroupHT::iterator g = groupHt.begin(); // for each group we found
409 // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
410 // warning: unsigned int format, different type arg
411 sscanf(g->first.c_str(),"%x",&gr_bid);
412 tk = g->first + "|0000"; // generate the element full tag
414 if ( tagHt.count(tk) == 0) { // if element 0x0000 not found
415 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
416 elemZ = new gdcmElValue(tagZ);
418 Add(elemZ); // create it
420 elemZ=GetElementByNumber(gr_bid, 0x0000);
422 sprintf(trash ,"%d",g->second);
424 elemZ->SetValue(str_trash);
429 * \ingroup gdcmElValSet
430 * \brief writes on disc according to the requested format
431 * \ (ACR-NEMA, DICOM, RAW) the image
432 * \ warning does NOT add the missing elements in the header :
433 * \ it's up to the user doing it !
434 * \ (function CheckHeaderCoherence to be written)
435 * @param type type of the File to be written
436 * (ACR-NEMA, DICOM, RAW)
437 * @param _fp already open file pointer
440 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
448 std::vector<std::string> tokens;
450 // TODO : use listElem to iterate, not TagHt!
451 // pb : gdcmElValSet.Add does NOT update listElem
452 // find a trick in STL to do it, at low cost !
456 // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian
457 // restent a tester les echecs en ecriture (apres chaque fwrite)
459 for (TagElValueHT::iterator tag2=tagHt.begin();
463 gr = tag2->second->GetGroup();
464 el = tag2->second->GetElement();
465 lgr = tag2->second->GetLength();
466 val = tag2->second->GetValue().c_str();
467 vr = tag2->second->GetVR();
470 if (gr < 0x0008) continue; // ignore pure DICOM V3 groups
471 if (gr %2) continue; // ignore shadow groups
472 if (vr == "SQ" ) continue; // ignore Sequences
473 if (gr == 0xfffe ) continue; // ignore delimiters
476 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
477 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
479 if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
481 guint16 z=0, shortLgr;
482 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
484 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
485 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
486 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
490 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
492 } else { // IMPLICIT VR
493 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
496 if (vr == "US" || vr == "SS") {
497 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
498 Tokenize (tag2->second->GetValue(), tokens, "\\");
499 for (unsigned int i=0; i<tokens.size();i++) {
500 val_uint16 = atoi(tokens[i].c_str());
502 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
507 if (vr == "UL" || vr == "SL") {
508 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
509 Tokenize (tag2->second->GetValue(), tokens, "\\");
510 for (unsigned int i=0; i<tokens.size();i++) {
511 val_uint32 = atoi(tokens[i].c_str());
513 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
518 // Pixels are never loaded in the element !
519 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
521 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
526 * \ingroup gdcmElValSet
532 int gdcmElValSet::Write(FILE * _fp, FileType type) {
535 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non
536 // (FileType est un champ de gdcmHeader ...)
537 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
539 // a moins de se livrer a un tres complique ajout des champs manquants.
540 // faire un CheckAndCorrectHeader (?)
542 if ( (type == ImplicitVR) || (type == ExplicitVR) )
543 UpdateGroupLength(false,type);
545 UpdateGroupLength(true,ACR);
547 WriteElements(type, _fp);