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 already 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;
38 * \ingroup gdcmElValSet
39 * \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
54 void gdcmElValSet::Print(ostream & os) {
61 gdcmTS * ts = gdcmGlobal::GetTS();
63 for (TagElValueHT::iterator tag = tagHt.begin();
66 g = tag->second->GetGroup();
67 e = tag->second->GetElement();
68 v = tag->second->GetValue();
69 o = tag->second->GetOffset();
70 d = _CreateCleanString(v); // replace non printable characters by '.'
73 os << tag->first << ": ";
74 os << " lgr : " << tag->second->GetLength();
75 os << ", Offset : " << o;
76 os << " x(" << hex << o << dec << ") ";
77 os << "\t[" << tag->second->GetVR() << "]";
78 os << "\t[" << tag->second->GetName() << "]";
79 os << "\t[" << d2 << "]";
81 // Display the UID value (instead of displaying the rough code)
82 if (g == 0x0002) { // Some more to be displayed ?
83 if ( (e == 0x0010) || (e == 0x0002) )
84 os << " ==>\t[" << ts->GetValue(v) << "]";
87 if ( (e == 0x0016) || (e == 0x1150) )
88 os << " ==>\t[" << ts->GetValue(v) << "]";
97 * \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
118 gdcmElValue* gdcmElValSet::GetElementByNumber(guint16 group, guint16 element) {
119 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
120 if ( ! tagHt.count(key))
121 return (gdcmElValue*)0;
122 return tagHt.find(key)->second;
126 * \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
143 string gdcmElValSet::GetElValueByNumber(guint16 group, guint16 element) {
144 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
145 if ( ! tagHt.count(key))
146 return "gdcm::Unfound";
147 return tagHt.find(key)->second->GetValue();
151 * \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
169 int gdcmElValSet::SetElValueByNumber(string content,
170 guint16 group, guint16 element) {
171 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
172 if ( ! tagHt.count(key))
174 tagHt[key]->SetValue(content);
175 string vr = tagHt[key]->GetVR();
178 if( (vr == "US") || (vr == "SS") )
180 else if( (vr == "UL") || (vr == "SL") )
183 lgr = content.length();
184 tagHt[key]->SetLength(lgr);
189 * \ingroup gdcmElValSet
195 int gdcmElValSet::SetElValueByName(string content, string TagName) {
196 if ( ! NameHt.count(TagName))
198 NameHt[TagName]->SetValue(content);
199 string vr = NameHt[TagName]->GetVR();
202 if( (vr == "US") || (vr == "SS") )
204 else if( (vr == "UL") || (vr == "SL") )
207 lgr = content.length();
209 // TODO : WARNING: le cas de l'element des pixels (7fe0,0010) n'est pas traite
210 // par SetElValueByName
211 // il faudra utiliser SetElValueByNumber
213 NameHt[TagName]->SetLength(lgr);
218 * \ingroup gdcmElValSet
219 * \brief Generate a free TagKey i.e. a TagKey that is not present
220 * in the TagHt dictionary.
221 * @param group The generated tag must belong to this group.
222 * @return The element of tag with given group which is fee.
224 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
225 for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
226 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
227 if (tagHt.count(key) == 0)
234 * \ingroup gdcmElValSet
241 int gdcmElValSet::SetVoidAreaByNumber(void * area,
242 guint16 group, guint16 element) {
243 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
244 if ( ! tagHt.count(key))
246 tagHt[key]->SetVoidArea(area);
251 * \ingroup gdcmElValSet
256 * @return int acts as a boolean
258 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
259 guint16 group, guint16 element) {
260 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
261 if ( ! tagHt.count(key))
263 tagHt[key]->SetLength(length);
267 * \ingroup gdcmElValSet
273 int gdcmElValSet::SetElValueLengthByName(guint32 length, string TagName) {
274 if ( ! NameHt.count(TagName))
276 NameHt.find(TagName)->second->SetLength(length);
281 * \ingroup gdcmElValSet
282 * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
283 * @param SkipSequence TRUE if we don't want to write Sequences (ACR-NEMA Files)
286 void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
295 GroupHT groupHt; // to hold the length of each group
298 // typedef std::map<GroupKey, int> GroupHT;
302 // for each Tag in the DCM Header
304 for (TagElValueHT::iterator tag2 = tagHt.begin();
309 gr = elem->GetGroup();
310 el = elem->GetElement();
313 sprintf(trash, "%04x", gr);
314 key = trash; // generate 'group tag'
316 // if the caller decided not to take SEQUENCEs into account
317 // e.g : he wants to write an ACR-NEMA File
319 if (SkipSequence && vr == "SQ") continue;
321 // Still unsolved problem :
322 // we cannot find the 'Sequence Delimitation Item'
323 // since it's at the end of the Hash Table
326 // pas SEQUENCE en ACR-NEMA
328 // --> la descente a l'interieur' des SQ
329 // devrait etre faite avec une liste chainee, pas avec une HTable...
331 if ( groupHt.count(key) == 0) { // we just read the first elem of a given group
332 if (el == 0x0000) { // the first elem is 0x0000
333 groupHt[key] = 0; // initialize group length
335 groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
337 } else { // any elem but the first
338 if (type == ExplicitVR) {
339 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
340 groupHt[key] += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
343 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
347 unsigned short int gr_bid;
349 for (GroupHT::iterator g = groupHt.begin(); // for each group we found
352 // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
353 // warning: unsigned int format, different type arg
354 sscanf(g->first.c_str(),"%x",&gr_bid);
355 tk = g->first + "|0000"; // generate the element full tag
357 if ( tagHt.count(tk) == 0) { // if element 0x0000 not found
358 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
359 elemZ = new gdcmElValue(tagZ);
361 Add(elemZ); // create it
363 elemZ=GetElementByNumber(gr_bid, 0x0000);
365 sprintf(trash ,"%d",g->second);
367 elemZ->SetValue(str_trash);
372 * \ingroup gdcmElValSet
378 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
386 vector<string> tokens;
390 // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian
391 // restent à tester les echecs en écriture (apres chaque fwrite)
393 for (TagElValueHT::iterator tag2=tagHt.begin();
397 gr = tag2->second->GetGroup();
398 el = tag2->second->GetElement();
399 lgr = tag2->second->GetLength();
400 val = tag2->second->GetValue().c_str();
401 vr = tag2->second->GetVR();
403 // cout << "Tag "<< hex << gr << " " << el << "\n";
406 if (gr < 0x0008) continue; // ignore pure DICOM V3 groups
407 if (gr %2) continue; // ignore shadow groups
408 if (vr == "SQ" ) continue; // ignore Sequences
409 if (gr == 0xfffe ) continue; // ignore delimiters
412 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
413 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
415 if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
417 guint16 z=0, shortLgr;
418 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
420 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
421 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
422 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
426 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
428 } else { // IMPLICIT VR
429 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
432 if (vr == "US" || vr == "SS") {
433 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
434 Tokenize (tag2->second->GetValue(), tokens, "\\");
435 for (unsigned int i=0; i<tokens.size();i++) {
436 val_uint16 = atoi(tokens[i].c_str());
438 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
443 if (vr == "UL" || vr == "SL") {
444 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
445 Tokenize (tag2->second->GetValue(), tokens, "\\");
446 for (unsigned int i=0; i<tokens.size();i++) {
447 val_uint32 = atoi(tokens[i].c_str());
449 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
454 // Pixels are never loaded in the element !
455 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
457 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
462 * \ingroup gdcmElValSet
468 int gdcmElValSet::Write(FILE * _fp, FileType type) {
470 if (type == ImplicitVR) {
471 string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
472 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
474 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
475 // values with a VR of UI shall be padded with a single trailing null
476 // Dans le cas suivant on doit pader manuellement avec un 0
478 SetElValueLengthByNumber(18, 0x0002, 0x0010);
481 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non ,
482 // (FileType est un champ de gdcmHeader ...)
483 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
486 if (type == ExplicitVR) {
487 string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
488 SetElValueByNumber(explicitVRTransfertSyntax, 0x0002, 0x0010);
490 SetElValueLengthByNumber(20, 0x0002, 0x0010);
493 if ( (type == ImplicitVR) || (type == ExplicitVR) )
494 UpdateGroupLength(false,type);
496 UpdateGroupLength(true,ACR);
498 WriteElements(type, _fp);