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 allready 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;
39 * \ingroup gdcmElValSet
40 * \brief Checks if a given Dicom element exists
46 int gdcmElValSet::CheckIfExistByNumber(guint16 Group, guint16 Elem ) {
47 string key = TranslateToKey(Group, Elem );
48 return (tagHt.count(key));
52 * \ingroup gdcmElValSet
55 void gdcmElValSet::Print(ostream & os) {
62 gdcmTS * ts = gdcmGlobal::GetTS();
64 for (TagElValueHT::iterator tag = tagHt.begin();
67 g = tag->second->GetGroup();
68 e = tag->second->GetElement();
69 v = tag->second->GetValue();
70 o = tag->second->GetOffset();
71 d = _CreateCleanString(v); // TODO : trouver qq chose moins goret
74 os << tag->first << ": ";
75 //os << "[" << v << "]";
76 os << "[" << d2 << "]";
77 os << "[" << tag->second->GetName() << "]";
78 os << "[" << tag->second->GetVR() << "]";
80 if ( (g == 0x0002) && (e == 0x0010) ) {
81 os << " [" << ts->GetValue(v) << "]";
86 os << " lgr : " << tag->second->GetLength();
87 os << ", Offset : " << o;
88 os << " x(" << hex << o << dec << ") ";
94 * \ingroup gdcmElValSet
97 void gdcmElValSet::PrintByName(ostream & os) {
98 for (TagElValueNameHT::iterator tag = NameHt.begin();
101 os << tag->first << ": ";
102 os << "[" << tag->second->GetValue() << "]";
103 os << "[" << tag->second->GetKey() << "]";
104 os << "[" << tag->second->GetVR() << "]" << endl;
109 * \ingroup gdcmElValSet
115 gdcmElValue* gdcmElValSet::GetElementByNumber(guint16 group, guint16 element) {
116 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
117 if ( ! tagHt.count(key))
118 return (gdcmElValue*)0;
119 return tagHt.find(key)->second;
123 * \ingroup gdcmElValSet
127 gdcmElValue* gdcmElValSet::GetElementByName(string TagName) {
128 if ( ! NameHt.count(TagName))
129 return (gdcmElValue*)0;
130 return NameHt.find(TagName)->second;
134 * \ingroup gdcmElValSet
140 string gdcmElValSet::GetElValueByNumber(guint16 group, guint16 element) {
141 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
142 if ( ! tagHt.count(key))
143 return "gdcm::Unfound";
144 return tagHt.find(key)->second->GetValue();
148 * \ingroup gdcmElValSet
152 string gdcmElValSet::GetElValueByName(string TagName) {
153 if ( ! NameHt.count(TagName))
154 return "gdcm::Unfound";
155 return NameHt.find(TagName)->second->GetValue();
159 * \ingroup gdcmElValSet
166 int gdcmElValSet::SetElValueByNumber(string content,
167 guint16 group, guint16 element) {
168 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
169 if ( ! tagHt.count(key))
171 tagHt[key]->SetValue(content);
172 string vr = tagHt[key]->GetVR();
175 if( (vr == "US") || (vr == "SS") )
177 else if( (vr == "UL") || (vr == "SL") )
180 lgr = content.length();
181 tagHt[key]->SetLength(lgr);
186 * \ingroup gdcmElValSet
192 int gdcmElValSet::SetElValueByName(string content, string TagName) {
193 if ( ! NameHt.count(TagName))
195 NameHt[TagName]->SetValue(content);
196 string vr = NameHt[TagName]->GetVR();
199 if( (vr == "US") || (vr == "SS") )
201 else if( (vr == "UL") || (vr == "SL") )
204 lgr = content.length();
206 // TODO : WARNING: le cas de l'element des pixels (7fe0,0010) n'est pas traite
207 // par SetElValueByName
208 // il faudra utiliser SetElValueByNumber
210 NameHt[TagName]->SetLength(lgr);
215 * \ingroup gdcmElValSet
216 * \brief Generate a free TagKey i.e. a TagKey that is not present
217 * in the TagHt dictionary.
218 * @param group The generated tag must belong to this group.
219 * @return The element of tag with given group which is fee.
221 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
222 for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
223 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
224 if (tagHt.count(key) == 0)
231 * \ingroup gdcmElValSet
238 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
239 guint16 group, guint16 element) {
240 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
241 if ( ! tagHt.count(key))
243 tagHt[key]->SetLength(length);
249 * \ingroup gdcmElValSet
255 int gdcmElValSet::SetElValueLengthByName(guint32 length, string TagName) {
256 if ( ! NameHt.count(TagName))
258 NameHt.find(TagName)->second->SetLength(length);
263 * \ingroup gdcmElValSet
264 * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
265 * @param SkipSequence
268 void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
277 GroupHT groupHt; // to hold the length of each group
280 // typedef std::map<GroupKey, int> GroupHT;
284 // for each Tag in the DCM Header
286 for (TagElValueHT::iterator tag2 = tagHt.begin();
291 gr = elem->GetGroup();
292 el = elem->GetElement();
295 sprintf(trash, "%04x", gr);
296 key = trash; // generate 'group tag'
298 // if the caller decided not to take SEQUENCEs into account
299 // e.g : he wants to write an ACR-NEMA File
301 if (SkipSequence && vr == "SQ") continue;
303 // pas SEQUENCE en ACR-NEMA
304 // WARNING : pb CERTAIN
305 // si on est descendu 'a l'interieur' des SQ
307 // --> la descente a l'interieur' des SQ
308 // devra etre faite avec une liste chainee, pas avec une HTable...
310 if ( groupHt.count(key) == 0) { // we just read the first elem of a given group
311 if (el == 0x0000) { // the first elem is 0x0000
312 groupHt[key] = 0; // initialize group length
314 groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
316 } else { // any elem but the first
317 if (type == ExplicitVR) {
318 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
319 groupHt[key] += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
322 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
326 if(1) // unnormalized way to see what happened
327 for (GroupHT::iterator g = groupHt.begin();
330 printf("groupKey %s : %d\n",g->first.c_str(),g->second);
333 unsigned short int gr_bid;
335 for (GroupHT::iterator g = groupHt.begin(); // for each group we found
338 // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
339 // warning: unsigned int format, different type arg
340 sscanf(g->first.c_str(),"%x",&gr_bid);
341 tk = g->first + "|0000"; // generate the element full tag
343 if ( tagHt.count(tk) == 0) { // if element 0x0000 not found
344 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
345 elemZ = new gdcmElValue(tagZ);
347 Add(elemZ); // create it
349 elemZ=GetElementByNumber(gr_bid, 0x0000);
351 sprintf(trash ,"%d",g->second);
353 elemZ->SetValue(str_trash);
358 * \ingroup gdcmElValSet
364 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
372 vector<string> tokens;
376 // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian
377 // restent à tester les echecs en écriture (apres chaque fwrite)
379 for (TagElValueHT::iterator tag2=tagHt.begin();
383 gr = tag2->second->GetGroup();
384 el = tag2->second->GetElement();
385 lgr = tag2->second->GetLength();
386 val = tag2->second->GetValue().c_str();
387 vr = tag2->second->GetVR();
390 if (gr < 0x0008) continue;
391 // if (gr %2) continue; // pour voir
392 if (vr == "SQ" ) continue;
395 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
396 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
398 if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
399 // On est en EXPLICIT VR
400 guint16 z=0, shortLgr;
401 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
403 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
404 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
405 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
409 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
412 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
415 tokens.erase(tokens.begin(),tokens.end());
416 Tokenize (tag2->second->GetValue(), tokens, "\\");
418 if (vr == "US" || vr == "SS") {
419 for (unsigned int i=0; i<tokens.size();i++) {
420 val_uint16 = atoi(tokens[i].c_str());
422 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
426 if (vr == "UL" || vr == "SL") {
427 for (unsigned int i=0; i<tokens.size();i++) {
428 val_uint32 = atoi(tokens[i].c_str());
430 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
436 // Les pixels ne sont pas chargés dans l'element !
437 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
439 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
444 * \ingroup gdcmElValSet
450 int gdcmElValSet::Write(FILE * _fp, FileType type) {
452 if (type == ImplicitVR) {
453 string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
454 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
456 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
457 // values with a VR of UI shall be padded with a single trailing null
458 // Dans le cas suivant on doit pader manuellement avec un 0
460 SetElValueLengthByNumber(18, 0x0002, 0x0010);
463 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non ,
464 // (FileType est un champ de gdcmHeader ...)
465 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
468 if (type == ExplicitVR) {
469 string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
470 SetElValueByNumber(explicitVRTransfertSyntax, 0x0002, 0x0010);
472 SetElValueLengthByNumber(20, 0x0002, 0x0010);
475 if ( (type == ImplicitVR) || (type == ExplicitVR) )
476 UpdateGroupLength(false,type);
478 UpdateGroupLength(true,ACR);
480 WriteElements(type, _fp);