4 // 'identifier' : class 'type' needs to have dll-interface to be used by
5 // clients of class 'type2'
6 #pragma warning ( disable : 4251 )
7 // 'identifier' : identifier was truncated to 'number' characters in the
9 #pragma warning ( disable : 4786 )
14 #include "gdcmElValSet.h"
18 gdcmElValSet::~gdcmElValSet() {
19 for (TagElValueHT::iterator tag = tagHt.begin(); tag != tagHt.end(); ++tag) {
20 gdcmElValue* EntryToDelete = tag->second;
25 // Since Add() adds symetrical in both tagHt and NameHt we can
26 // assume all the pointed gdcmElValues are already cleaned-up when
31 TagElValueHT & gdcmElValSet::GetTagHt(void) {
36 * \ingroup gdcmElValSet
41 void gdcmElValSet::Add(gdcmElValue * newElValue) {
42 tagHt [newElValue->GetKey()] = newElValue;
43 NameHt[newElValue->GetName()] = newElValue;
47 * \ingroup gdcmElValSet
48 * \brief Checks if a given Dicom element exists
54 int gdcmElValSet::CheckIfExistByNumber(guint16 Group, guint16 Elem ) {
55 string key = TranslateToKey(Group, Elem );
56 return (tagHt.count(key));
60 * \ingroup gdcmElValSet
63 void gdcmElValSet::Print(ostream & os) {
69 gdcmTS * ts = gdcmGlobal::GetTS();
71 for (TagElValueHT::iterator tag = tagHt.begin();
74 g = tag->second->GetGroup();
75 e = tag->second->GetElement();
76 v = tag->second->GetValue();
77 o = tag->second->GetOffset();
78 d2 = _CreateCleanString(v); // replace non printable characters by '.'
80 os << tag->first << ": ";
81 os << " lgr : " << tag->second->GetLength();
82 os << ", Offset : " << o;
83 os << " x(" << hex << o << dec << ") ";
84 os << "\t[" << tag->second->GetVR() << "]";
85 os << "\t[" << tag->second->GetName() << "]";
86 os << "\t[" << d2 << "]";
88 // Display the UID value (instead of displaying the rough code)
89 if (g == 0x0002) { // Some more to be displayed ?
90 if ( (e == 0x0010) || (e == 0x0002) )
91 os << " ==>\t[" << ts->GetValue(v) << "]";
94 if ( (e == 0x0016) || (e == 0x1150) )
95 os << " ==>\t[" << ts->GetValue(v) << "]";
103 * \ingroup gdcmElValSet
106 void gdcmElValSet::PrintByName(ostream & os) {
107 for (TagElValueNameHT::iterator tag = NameHt.begin();
110 os << tag->first << ": ";
111 os << "[" << tag->second->GetValue() << "]";
112 os << "[" << tag->second->GetKey() << "]";
113 os << "[" << tag->second->GetVR() << "]" << endl;
118 * \ingroup gdcmElValSet
124 gdcmElValue* gdcmElValSet::GetElementByNumber(guint16 group, guint16 element) {
125 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
126 if ( ! tagHt.count(key))
127 return (gdcmElValue*)0;
128 return tagHt.find(key)->second;
132 * \ingroup gdcmElValSet
136 gdcmElValue* gdcmElValSet::GetElementByName(string TagName) {
137 if ( ! NameHt.count(TagName))
138 return (gdcmElValue*)0;
139 return NameHt.find(TagName)->second;
143 * \ingroup gdcmElValSet
149 string gdcmElValSet::GetElValueByNumber(guint16 group, guint16 element) {
150 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
151 if ( ! tagHt.count(key))
153 return tagHt.find(key)->second->GetValue();
157 * \ingroup gdcmElValSet
161 string gdcmElValSet::GetElValueByName(string TagName) {
162 if ( ! NameHt.count(TagName))
164 return NameHt.find(TagName)->second->GetValue();
168 * \ingroup gdcmElValSet
175 int gdcmElValSet::SetElValueByNumber(string content,
176 guint16 group, guint16 element) {
177 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
178 if ( ! tagHt.count(key))
180 tagHt[key]->SetValue(content);
181 string vr = tagHt[key]->GetVR();
184 if( (vr == "US") || (vr == "SS") )
186 else if( (vr == "UL") || (vr == "SL") )
189 lgr = content.length();
190 tagHt[key]->SetLength(lgr);
195 * \ingroup gdcmElValSet
201 int gdcmElValSet::SetElValueByName(string content, string TagName) {
202 if ( ! NameHt.count(TagName))
204 NameHt[TagName]->SetValue(content);
205 string vr = NameHt[TagName]->GetVR();
208 if( (vr == "US") || (vr == "SS") )
210 else if( (vr == "UL") || (vr == "SL") )
213 lgr = content.length();
215 // TODO : WARNING: le cas de l'element des pixels (7fe0,0010) n'est pas traite
216 // par SetElValueByName
217 // il faudra utiliser SetElValueByNumber
219 NameHt[TagName]->SetLength(lgr);
224 * \ingroup gdcmElValSet
225 * \brief Generate a free TagKey i.e. a TagKey that is not present
226 * in the TagHt dictionary.
227 * @param group The generated tag must belong to this group.
228 * @return The element of tag with given group which is fee.
230 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
231 for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
232 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
233 if (tagHt.count(key) == 0)
240 * \ingroup gdcmElValSet
247 int gdcmElValSet::SetVoidAreaByNumber(void * area,
248 guint16 group, guint16 element) {
249 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
250 if ( ! tagHt.count(key))
252 tagHt[key]->SetVoidArea(area);
257 * \ingroup gdcmElValSet
262 * @return int acts as a boolean
264 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
265 guint16 group, guint16 element) {
266 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
267 if ( ! tagHt.count(key))
269 tagHt[key]->SetLength(length);
273 * \ingroup gdcmElValSet
279 int gdcmElValSet::SetElValueLengthByName(guint32 length, string TagName) {
280 if ( ! NameHt.count(TagName))
282 NameHt.find(TagName)->second->SetLength(length);
287 * \ingroup gdcmElValSet
288 * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
289 * @param SkipSequence TRUE if we don't want to write Sequences (ACR-NEMA Files)
292 void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
301 GroupHT groupHt; // to hold the length of each group
304 // typedef std::map<GroupKey, int> GroupHT;
308 // for each Tag in the DCM Header
310 for (TagElValueHT::iterator tag2 = tagHt.begin();
315 gr = elem->GetGroup();
316 el = elem->GetElement();
319 sprintf(trash, "%04x", gr);
320 key = trash; // generate 'group tag'
322 // if the caller decided not to take SEQUENCEs into account
323 // e.g : he wants to write an ACR-NEMA File
325 if (SkipSequence && vr == "SQ") continue;
327 // Still unsolved problem :
328 // we cannot find the 'Sequence Delimitation Item'
329 // since it's at the end of the Hash Table
332 // pas SEQUENCE en ACR-NEMA
334 // --> la descente a l'interieur' des SQ
335 // devrait etre faite avec une liste chainee, pas avec une HTable...
337 if ( groupHt.count(key) == 0) { // we just read the first elem of a given group
338 if (el == 0x0000) { // the first elem is 0x0000
339 groupHt[key] = 0; // initialize group length
341 groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
343 } else { // any elem but the first
344 if (type == ExplicitVR) {
345 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
346 groupHt[key] += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
349 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
353 unsigned short int gr_bid;
355 for (GroupHT::iterator g = groupHt.begin(); // for each group we found
358 // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
359 // warning: unsigned int format, different type arg
360 sscanf(g->first.c_str(),"%x",&gr_bid);
361 tk = g->first + "|0000"; // generate the element full tag
363 if ( tagHt.count(tk) == 0) { // if element 0x0000 not found
364 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
365 elemZ = new gdcmElValue(tagZ);
367 Add(elemZ); // create it
369 elemZ=GetElementByNumber(gr_bid, 0x0000);
371 sprintf(trash ,"%d",g->second);
373 elemZ->SetValue(str_trash);
378 * \ingroup gdcmElValSet
384 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
392 vector<string> tokens;
396 // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian
397 // restent à tester les echecs en écriture (apres chaque fwrite)
399 for (TagElValueHT::iterator tag2=tagHt.begin();
403 gr = tag2->second->GetGroup();
404 el = tag2->second->GetElement();
405 lgr = tag2->second->GetLength();
406 val = tag2->second->GetValue().c_str();
407 vr = tag2->second->GetVR();
409 // cout << "Tag "<< hex << gr << " " << el << "\n";
412 if (gr < 0x0008) continue; // ignore pure DICOM V3 groups
413 if (gr %2) continue; // ignore shadow groups
414 if (vr == "SQ" ) continue; // ignore Sequences
415 if (gr == 0xfffe ) continue; // ignore delimiters
418 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
419 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
421 if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
423 guint16 z=0, shortLgr;
424 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
426 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
427 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
428 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
432 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
434 } else { // IMPLICIT VR
435 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
438 if (vr == "US" || vr == "SS") {
439 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
440 Tokenize (tag2->second->GetValue(), tokens, "\\");
441 for (unsigned int i=0; i<tokens.size();i++) {
442 val_uint16 = atoi(tokens[i].c_str());
444 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
449 if (vr == "UL" || vr == "SL") {
450 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
451 Tokenize (tag2->second->GetValue(), tokens, "\\");
452 for (unsigned int i=0; i<tokens.size();i++) {
453 val_uint32 = atoi(tokens[i].c_str());
455 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
460 // Pixels are never loaded in the element !
461 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
463 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
468 * \ingroup gdcmElValSet
474 int gdcmElValSet::Write(FILE * _fp, FileType type) {
476 if (type == ImplicitVR) {
477 string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
478 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
480 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
481 // values with a VR of UI shall be padded with a single trailing null
482 // Dans le cas suivant on doit pader manuellement avec un 0
484 SetElValueLengthByNumber(18, 0x0002, 0x0010);
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
492 if (type == ExplicitVR) {
493 string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
494 SetElValueByNumber(explicitVRTransfertSyntax, 0x0002, 0x0010);
496 SetElValueLengthByNumber(20, 0x0002, 0x0010);
499 if ( (type == ImplicitVR) || (type == ExplicitVR) )
500 UpdateGroupLength(false,type);
502 UpdateGroupLength(true,ACR);
504 WriteElements(type, _fp);