4 #include "gdcmElValSet.h"
8 gdcmElValSet::~gdcmElValSet() {
9 for (TagElValueHT::iterator tag = tagHt.begin(); tag != tagHt.end(); ++tag) {
10 gdcmElValue* EntryToDelete = tag->second;
15 // Since Add() adds symetrical in both tagHt and NameHt we can
16 // assume all the pointed gdcmElValues are already cleaned-up when
21 TagElValueHT & gdcmElValSet::GetTagHt(void) {
26 * \ingroup gdcmElValSet
31 void gdcmElValSet::Add(gdcmElValue * newElValue) {
32 tagHt [newElValue->GetKey()] = newElValue;
33 NameHt[newElValue->GetName()] = newElValue;
37 * \ingroup gdcmElValSet
38 * \brief Checks if a given Dicom element exists
44 int gdcmElValSet::CheckIfExistByNumber(guint16 Group, guint16 Elem ) {
45 std::string key = TranslateToKey(Group, Elem );
46 return (tagHt.count(key));
50 * \ingroup gdcmElValSet
53 void gdcmElValSet::Print(std::ostream & os) {
59 gdcmTS * ts = gdcmGlobal::GetTS();
61 for (TagElValueHT::iterator tag = tagHt.begin();
64 g = tag->second->GetGroup();
65 e = tag->second->GetElement();
66 v = tag->second->GetValue();
67 o = tag->second->GetOffset();
68 d2 = _CreateCleanString(v); // replace non printable characters by '.'
70 os << tag->first << ": ";
71 os << " lgr : " << tag->second->GetLength();
72 os << ", Offset : " << o;
73 os << " x(" << std::hex << o << std::dec << ") ";
74 os << "\t[" << tag->second->GetVR() << "]";
75 os << "\t[" << tag->second->GetName() << "]";
76 os << "\t[" << d2 << "]";
78 // Display the UID value (instead of displaying the rough code)
79 if (g == 0x0002) { // Some more to be displayed ?
80 if ( (e == 0x0010) || (e == 0x0002) )
81 os << " ==>\t[" << ts->GetValue(v) << "]";
84 if ( (e == 0x0016) || (e == 0x1150) )
85 os << " ==>\t[" << ts->GetValue(v) << "]";
93 * \ingroup gdcmElValSet
96 void gdcmElValSet::PrintByName(std::ostream & os) {
97 for (TagElValueNameHT::iterator tag = NameHt.begin();
100 os << tag->first << ": ";
101 os << "[" << tag->second->GetValue() << "]";
102 os << "[" << tag->second->GetKey() << "]";
103 os << "[" << tag->second->GetVR() << "]" << std::endl;
108 * \ingroup gdcmElValSet
114 gdcmElValue* gdcmElValSet::GetElementByNumber(guint16 group, guint16 element) {
115 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
116 if ( ! tagHt.count(key))
117 return (gdcmElValue*)0;
118 return tagHt.find(key)->second;
122 * \ingroup gdcmElValSet
126 gdcmElValue* gdcmElValSet::GetElementByName(std::string TagName) {
127 if ( ! NameHt.count(TagName))
128 return (gdcmElValue*)0;
129 return NameHt.find(TagName)->second;
133 * \ingroup gdcmElValSet
139 std::string gdcmElValSet::GetElValueByNumber(guint16 group, guint16 element) {
140 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
141 if ( ! tagHt.count(key))
143 return tagHt.find(key)->second->GetValue();
147 * \ingroup gdcmElValSet
151 std::string gdcmElValSet::GetElValueByName(std::string TagName) {
152 if ( ! NameHt.count(TagName))
154 return NameHt.find(TagName)->second->GetValue();
158 * \ingroup gdcmElValSet
165 int gdcmElValSet::SetElValueByNumber(std::string content,
166 guint16 group, guint16 element) {
167 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
168 if ( ! tagHt.count(key))
170 int l = content.length();
171 if(l%2) { // Odd length are padded with a space (020H).
173 content = content + '\0';
175 tagHt[key]->SetValue(content);
177 std::string vr = tagHt[key]->GetVR();
180 if( (vr == "US") || (vr == "SS") )
182 else if( (vr == "UL") || (vr == "SL") )
186 tagHt[key]->SetLength(lgr);
194 * \ingroup gdcmElValSet
200 int gdcmElValSet::SetElValueByName(std::string content, std::string TagName) {
201 if ( ! NameHt.count(TagName))
203 int l = content.length();
204 if(l%2) { // Odd length are padded with a space (020H).
206 // Well. I know that '/0' is NOT a space
207 // but it doesn't work with a space.
208 // Use hexedit and see 0002|0010 value (Transfer Syntax UID)
209 content = content + '\0';
211 NameHt[TagName]->SetValue(content);
213 std::string vr = NameHt[TagName]->GetVR();
216 if( (vr == "US") || (vr == "SS") )
218 else if( (vr == "UL") || (vr == "SL") )
221 lgr = content.length();
223 // TODO : WARNING: le cas de l'element des pixels (7fe0,0010) n'est pas traite
224 // par SetElValueByName
225 // il faudra utiliser SetElValueByNumber
227 NameHt[TagName]->SetLength(lgr);
232 * \ingroup gdcmElValSet
233 * \brief Generate a free TagKey i.e. a TagKey that is not present
234 * in the TagHt dictionary.
235 * @param group The generated tag must belong to this group.
236 * @return The element of tag with given group which is fee.
238 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
239 for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
240 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
241 if (tagHt.count(key) == 0)
248 * \ingroup gdcmElValSet
255 int gdcmElValSet::SetVoidAreaByNumber(void * area,
256 guint16 group, guint16 element) {
257 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
258 if ( ! tagHt.count(key))
260 tagHt[key]->SetVoidArea(area);
265 * \ingroup gdcmElValSet
270 * @return int acts as a boolean
272 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
273 guint16 group, guint16 element) {
274 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
275 if ( ! tagHt.count(key))
277 if (length%2) length++; // length must be even
278 tagHt[key]->SetLength(length);
282 * \ingroup gdcmElValSet
288 int gdcmElValSet::SetElValueLengthByName(guint32 length, std::string TagName) {
289 if ( ! NameHt.count(TagName))
291 if (length%2) length++; // length must be even
292 NameHt.find(TagName)->second->SetLength(length);
297 * \ingroup gdcmElValSet
298 * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
299 * @param SkipSequence TRUE if we don't want to write Sequences (ACR-NEMA Files)
302 void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
308 std::string str_trash;
311 GroupHT groupHt; // to hold the length of each group
314 // typedef std::map<GroupKey, int> GroupHT;
318 // for each Tag in the DCM Header
320 for (TagElValueHT::iterator tag2 = tagHt.begin();
325 gr = elem->GetGroup();
326 el = elem->GetElement();
329 sprintf(trash, "%04x", gr);
330 key = trash; // generate 'group tag'
332 // if the caller decided not to take SEQUENCEs into account
333 // e.g : he wants to write an ACR-NEMA File
335 if (SkipSequence && vr == "SQ") continue;
337 // Still unsolved problem :
338 // we cannot find the 'Sequence Delimitation Item'
339 // since it's at the end of the Hash Table
342 // pas SEQUENCE en ACR-NEMA
344 // --> la descente a l'interieur' des SQ
345 // devrait etre faite avec une liste chainee, pas avec une HTable...
347 if ( groupHt.count(key) == 0) { // we just read the first elem of a given group
348 if (el == 0x0000) { // the first elem is 0x0000
349 groupHt[key] = 0; // initialize group length
351 groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
353 } else { // any elem but the first
354 if (type == ExplicitVR) {
355 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
356 groupHt[key] += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
359 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
363 unsigned short int gr_bid;
365 for (GroupHT::iterator g = groupHt.begin(); // for each group we found
368 // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
369 // warning: unsigned int format, different type arg
370 sscanf(g->first.c_str(),"%x",&gr_bid);
371 tk = g->first + "|0000"; // generate the element full tag
373 if ( tagHt.count(tk) == 0) { // if element 0x0000 not found
374 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
375 elemZ = new gdcmElValue(tagZ);
377 Add(elemZ); // create it
379 elemZ=GetElementByNumber(gr_bid, 0x0000);
381 sprintf(trash ,"%d",g->second);
383 elemZ->SetValue(str_trash);
388 * \ingroup gdcmElValSet
394 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
402 std::vector<std::string> tokens;
406 // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian
407 // restent a tester les echecs en ecriture (apres chaque fwrite)
409 for (TagElValueHT::iterator tag2=tagHt.begin();
413 gr = tag2->second->GetGroup();
414 el = tag2->second->GetElement();
415 lgr = tag2->second->GetLength();
416 val = tag2->second->GetValue().c_str();
417 vr = tag2->second->GetVR();
419 // std::cout << "Tag "<< std::hex << gr << " " << el << std::endl;
422 if (gr < 0x0008) continue; // ignore pure DICOM V3 groups
423 if (gr %2) continue; // ignore shadow groups
424 if (vr == "SQ" ) continue; // ignore Sequences
425 if (gr == 0xfffe ) continue; // ignore delimiters
428 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
429 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
431 if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
433 guint16 z=0, shortLgr;
434 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
436 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
437 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
438 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
442 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
444 } else { // IMPLICIT VR
445 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
448 if (vr == "US" || vr == "SS") {
449 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
450 Tokenize (tag2->second->GetValue(), tokens, "\\");
451 for (unsigned int i=0; i<tokens.size();i++) {
452 val_uint16 = atoi(tokens[i].c_str());
454 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
459 if (vr == "UL" || vr == "SL") {
460 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
461 Tokenize (tag2->second->GetValue(), tokens, "\\");
462 for (unsigned int i=0; i<tokens.size();i++) {
463 val_uint32 = atoi(tokens[i].c_str());
465 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
470 // Pixels are never loaded in the element !
471 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
473 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
478 * \ingroup gdcmElValSet
484 int gdcmElValSet::Write(FILE * _fp, FileType type) {
487 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non
488 // (FileType est un champ de gdcmHeader ...)
489 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
491 // a moins de se livrer a un tres complique ajout des champs manquants.
492 // faire un CheckAndCorrectHeader (?)
494 if ( (type == ImplicitVR) || (type == ExplicitVR) )
495 UpdateGroupLength(false,type);
497 UpdateGroupLength(true,ACR);
499 WriteElements(type, _fp);