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"
17 gdcmElValSet::~gdcmElValSet() {
18 for (TagElValueHT::iterator tag = tagHt.begin(); tag != tagHt.end(); ++tag) {
19 gdcmElValue* EntryToDelete = tag->second;
24 // Since Add() adds symetrical in both tagHt and NameHt we can
25 // assume all the pointed gdcmElValues are already cleaned-up when
30 TagElValueHT & gdcmElValSet::GetTagHt(void) {
35 * \ingroup gdcmElValSet
40 void gdcmElValSet::Add(gdcmElValue * newElValue) {
41 tagHt [newElValue->GetKey()] = newElValue;
42 NameHt[newElValue->GetName()] = newElValue;
46 * \ingroup gdcmElValSet
47 * \brief Checks if a given Dicom element exists
53 int gdcmElValSet::CheckIfExistByNumber(guint16 Group, guint16 Elem ) {
54 std::string key = TranslateToKey(Group, Elem );
55 return (tagHt.count(key));
59 * \ingroup gdcmElValSet
62 void gdcmElValSet::Print(ostream & os) {
68 gdcmTS * ts = gdcmGlobal::GetTS();
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(" << hex << o << 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) << "]";
102 * \ingroup gdcmElValSet
105 void gdcmElValSet::PrintByName(ostream & os) {
106 for (TagElValueNameHT::iterator tag = NameHt.begin();
109 os << tag->first << ": ";
110 os << "[" << tag->second->GetValue() << "]";
111 os << "[" << tag->second->GetKey() << "]";
112 os << "[" << tag->second->GetVR() << "]" << endl;
117 * \ingroup gdcmElValSet
123 gdcmElValue* gdcmElValSet::GetElementByNumber(guint16 group, guint16 element) {
124 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
125 if ( ! tagHt.count(key))
126 return (gdcmElValue*)0;
127 return tagHt.find(key)->second;
131 * \ingroup gdcmElValSet
135 gdcmElValue* gdcmElValSet::GetElementByName(std::string TagName) {
136 if ( ! NameHt.count(TagName))
137 return (gdcmElValue*)0;
138 return NameHt.find(TagName)->second;
142 * \ingroup gdcmElValSet
148 std::string gdcmElValSet::GetElValueByNumber(guint16 group, guint16 element) {
149 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
150 if ( ! tagHt.count(key))
152 return tagHt.find(key)->second->GetValue();
156 * \ingroup gdcmElValSet
160 std::string gdcmElValSet::GetElValueByName(std::string TagName) {
161 if ( ! NameHt.count(TagName))
163 return NameHt.find(TagName)->second->GetValue();
167 * \ingroup gdcmElValSet
174 int gdcmElValSet::SetElValueByNumber(std::string content,
175 guint16 group, guint16 element) {
176 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
177 if ( ! tagHt.count(key))
179 tagHt[key]->SetValue(content);
180 std::string vr = tagHt[key]->GetVR();
183 if( (vr == "US") || (vr == "SS") )
185 else if( (vr == "UL") || (vr == "SL") )
188 lgr = content.length();
189 tagHt[key]->SetLength(lgr);
194 * \ingroup gdcmElValSet
200 int gdcmElValSet::SetElValueByName(std::string content, std::string TagName) {
201 if ( ! NameHt.count(TagName))
203 NameHt[TagName]->SetValue(content);
204 std::string vr = NameHt[TagName]->GetVR();
207 if( (vr == "US") || (vr == "SS") )
209 else if( (vr == "UL") || (vr == "SL") )
212 lgr = content.length();
214 // TODO : WARNING: le cas de l'element des pixels (7fe0,0010) n'est pas traite
215 // par SetElValueByName
216 // il faudra utiliser SetElValueByNumber
218 NameHt[TagName]->SetLength(lgr);
223 * \ingroup gdcmElValSet
224 * \brief Generate a free TagKey i.e. a TagKey that is not present
225 * in the TagHt dictionary.
226 * @param group The generated tag must belong to this group.
227 * @return The element of tag with given group which is fee.
229 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
230 for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
231 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
232 if (tagHt.count(key) == 0)
239 * \ingroup gdcmElValSet
246 int gdcmElValSet::SetVoidAreaByNumber(void * area,
247 guint16 group, guint16 element) {
248 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
249 if ( ! tagHt.count(key))
251 tagHt[key]->SetVoidArea(area);
256 * \ingroup gdcmElValSet
261 * @return int acts as a boolean
263 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
264 guint16 group, guint16 element) {
265 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
266 if ( ! tagHt.count(key))
268 tagHt[key]->SetLength(length);
272 * \ingroup gdcmElValSet
278 int gdcmElValSet::SetElValueLengthByName(guint32 length, std::string TagName) {
279 if ( ! NameHt.count(TagName))
281 NameHt.find(TagName)->second->SetLength(length);
286 * \ingroup gdcmElValSet
287 * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
288 * @param SkipSequence TRUE if we don't want to write Sequences (ACR-NEMA Files)
291 void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
297 std::string str_trash;
300 GroupHT groupHt; // to hold the length of each group
303 // typedef std::map<GroupKey, int> GroupHT;
307 // for each Tag in the DCM Header
309 for (TagElValueHT::iterator tag2 = tagHt.begin();
314 gr = elem->GetGroup();
315 el = elem->GetElement();
318 sprintf(trash, "%04x", gr);
319 key = trash; // generate 'group tag'
321 // if the caller decided not to take SEQUENCEs into account
322 // e.g : he wants to write an ACR-NEMA File
324 if (SkipSequence && vr == "SQ") continue;
326 // Still unsolved problem :
327 // we cannot find the 'Sequence Delimitation Item'
328 // since it's at the end of the Hash Table
331 // pas SEQUENCE en ACR-NEMA
333 // --> la descente a l'interieur' des SQ
334 // devrait etre faite avec une liste chainee, pas avec une HTable...
336 if ( groupHt.count(key) == 0) { // we just read the first elem of a given group
337 if (el == 0x0000) { // the first elem is 0x0000
338 groupHt[key] = 0; // initialize group length
340 groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
342 } else { // any elem but the first
343 if (type == ExplicitVR) {
344 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
345 groupHt[key] += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
348 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
352 unsigned short int gr_bid;
354 for (GroupHT::iterator g = groupHt.begin(); // for each group we found
357 // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
358 // warning: unsigned int format, different type arg
359 sscanf(g->first.c_str(),"%x",&gr_bid);
360 tk = g->first + "|0000"; // generate the element full tag
362 if ( tagHt.count(tk) == 0) { // if element 0x0000 not found
363 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
364 elemZ = new gdcmElValue(tagZ);
366 Add(elemZ); // create it
368 elemZ=GetElementByNumber(gr_bid, 0x0000);
370 sprintf(trash ,"%d",g->second);
372 elemZ->SetValue(str_trash);
377 * \ingroup gdcmElValSet
383 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
391 vector<std::string> tokens;
395 // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian
396 // restent à tester les echecs en écriture (apres chaque fwrite)
398 for (TagElValueHT::iterator tag2=tagHt.begin();
402 gr = tag2->second->GetGroup();
403 el = tag2->second->GetElement();
404 lgr = tag2->second->GetLength();
405 val = tag2->second->GetValue().c_str();
406 vr = tag2->second->GetVR();
408 // cout << "Tag "<< hex << gr << " " << el << "\n";
411 if (gr < 0x0008) continue; // ignore pure DICOM V3 groups
412 if (gr %2) continue; // ignore shadow groups
413 if (vr == "SQ" ) continue; // ignore Sequences
414 if (gr == 0xfffe ) continue; // ignore delimiters
417 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
418 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
420 if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
422 guint16 z=0, shortLgr;
423 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
425 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
426 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
427 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
431 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
433 } else { // IMPLICIT VR
434 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
437 if (vr == "US" || vr == "SS") {
438 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
439 Tokenize (tag2->second->GetValue(), tokens, "\\");
440 for (unsigned int i=0; i<tokens.size();i++) {
441 val_uint16 = atoi(tokens[i].c_str());
443 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
448 if (vr == "UL" || vr == "SL") {
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_uint32 = atoi(tokens[i].c_str());
454 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
459 // Pixels are never loaded in the element !
460 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
462 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
467 * \ingroup gdcmElValSet
473 int gdcmElValSet::Write(FILE * _fp, FileType type) {
475 if (type == ImplicitVR) {
476 std::string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
477 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
479 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
480 // values with a VR of UI shall be padded with a single trailing null
481 // Dans le cas suivant on doit pader manuellement avec un 0
483 SetElValueLengthByNumber(18, 0x0002, 0x0010);
486 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non ,
487 // (FileType est un champ de gdcmHeader ...)
488 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
491 if (type == ExplicitVR) {
492 std::string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
493 SetElValueByNumber(explicitVRTransfertSyntax, 0x0002, 0x0010);
495 SetElValueLengthByNumber(20, 0x0002, 0x0010);
498 if ( (type == ImplicitVR) || (type == ExplicitVR) )
499 UpdateGroupLength(false,type);
501 UpdateGroupLength(true,ACR);
503 WriteElements(type, _fp);