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;
38 * \ingroup gdcmElValSet
43 void gdcmElValSet::Print(ostream & os) {
50 gdcmTS * ts = gdcmGlobal::GetTS();
52 for (TagElValueHT::iterator tag = tagHt.begin();
55 g = tag->second->GetGroup();
56 e = tag->second->GetElement();
57 v = tag->second->GetValue();
58 o = tag->second->GetOffset();
59 d = _CreateCleanString(v); // TODO : trouver qq chose moins goret
62 os << tag->first << ": ";
63 //os << "[" << v << "]";
64 os << "[" << d2 << "]";
65 os << "[" << tag->second->GetName() << "]";
66 os << "[" << tag->second->GetVR() << "]";
68 if ( (g == 0x0002) && (e == 0x0010) ) {
69 os << " [" << ts->GetValue(v) << "]";
74 os << " lgr : " << tag->second->GetLength();
75 os << ", Offset : " << o;
76 os << " x(" << hex << o << dec << ") ";
82 * \ingroup gdcmElValSet
87 void gdcmElValSet::PrintByName(ostream & os) {
88 for (TagElValueNameHT::iterator tag = NameHt.begin();
91 os << tag->first << ": ";
92 os << "[" << tag->second->GetValue() << "]";
93 os << "[" << tag->second->GetKey() << "]";
94 os << "[" << tag->second->GetVR() << "]" << endl;
99 * \ingroup gdcmElValSet
104 gdcmElValue* gdcmElValSet::GetElementByNumber(guint16 group, guint16 element) {
105 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
106 if ( ! tagHt.count(key))
107 return (gdcmElValue*)0;
108 return tagHt.find(key)->second;
112 * \ingroup gdcmElValSet
117 gdcmElValue* gdcmElValSet::GetElementByName(string TagName) {
118 if ( ! NameHt.count(TagName))
119 return (gdcmElValue*)0;
120 return NameHt.find(TagName)->second;
124 * \ingroup gdcmElValSet
129 string gdcmElValSet::GetElValueByNumber(guint16 group, guint16 element) {
130 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
131 if ( ! tagHt.count(key))
132 return "gdcm::Unfound";
133 return tagHt.find(key)->second->GetValue();
137 * \ingroup gdcmElValSet
142 string gdcmElValSet::GetElValueByName(string TagName) {
143 if ( ! NameHt.count(TagName))
144 return "gdcm::Unfound";
145 return NameHt.find(TagName)->second->GetValue();
149 * \ingroup gdcmElValSet
154 int gdcmElValSet::SetElValueByNumber(string content,
155 guint16 group, guint16 element) {
156 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
157 if ( ! tagHt.count(key))
159 tagHt[key]->SetValue(content);
160 string vr = tagHt[key]->GetVR();
163 if( (vr == "US") || (vr == "SS") )
165 else if( (vr == "UL") || (vr == "SL") )
168 lgr = content.length();
169 tagHt[key]->SetLength(lgr);
174 * \ingroup gdcmElValSet
179 int gdcmElValSet::SetElValueByName(string content, string TagName) {
180 if ( ! NameHt.count(TagName))
182 NameHt[TagName]->SetValue(content);
183 string vr = NameHt[TagName]->GetVR();
186 if( (vr == "US") || (vr == "SS") )
188 else if( (vr == "UL") || (vr == "SL") )
191 lgr = content.length();
193 // TODO : WARNING: le cas de l'element des pixels (7fe0,0010) n'est pas traite
194 // par SetElValueByName
195 // il faudra utiliser SetElValueByNumber
197 NameHt[TagName]->SetLength(lgr);
202 * \ingroup gdcmElValSet
203 * \brief Generate a free TagKey i.e. a TagKey that is not present
204 * in the TagHt dictionary.
205 * @param group The generated tag must belong to this group.
206 * @return The element of tag with given group which is fee.
208 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
209 for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
210 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
211 if (tagHt.count(key) == 0)
218 * \ingroup gdcmElValSet
223 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
224 guint16 group, guint16 element) {
225 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
226 if ( ! tagHt.count(key))
228 tagHt[key]->SetLength(length);
234 * \ingroup gdcmElValSet
239 int gdcmElValSet::SetElValueLengthByName(guint32 length, string TagName) {
240 if ( ! NameHt.count(TagName))
242 NameHt.find(TagName)->second->SetLength(length);
247 // re-computes the length of a ACR-NEMA/Dicom group
251 * \ingroup gdcmElValSet
256 void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
265 GroupHT groupHt; // to hold the length of each group
268 // typedef std::map<GroupKey, int> GroupHT;
272 // for each Tag in the DCM Header
274 for (TagElValueHT::iterator tag2 = tagHt.begin();
279 gr = elem->GetGroup();
280 el = elem->GetElement();
283 sprintf(trash, "%04x", gr);
284 key = trash; // generate 'group tag'
286 // if the caller decided not to take SEQUENCEs into account
287 // e.g : he wants to write an ACR-NEMA File
289 if (SkipSequence && vr == "SQ") continue;
291 // pas SEQUENCE en ACR-NEMA
292 // WARNING : pb CERTAIN
293 // si on est descendu 'a l'interieur' des SQ
295 // --> la descente a l'interieur' des SQ
296 // devra etre faite avec une liste chainee, pas avec une HTable...
298 if ( groupHt.count(key) == 0) { // we just read the first elem of a given group
299 if (el == 0x0000) { // the first elem is 0x0000
300 groupHt[key] = 0; // initialize group length
302 groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
304 } else { // any elem but the first
305 if (type == ExplicitVR) {
306 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
307 groupHt[key] += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
310 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
314 if(1) // unnormalized way to see what happened
315 for (GroupHT::iterator g = groupHt.begin();
318 printf("groupKey %s : %d\n",g->first.c_str(),g->second);
321 unsigned short int gr_bid;
323 for (GroupHT::iterator g = groupHt.begin(); // for each group we found
326 // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
327 // warning: unsigned int format, different type arg
328 sscanf(g->first.c_str(),"%x",&gr_bid);
329 tk = g->first + "|0000"; // generate the element full tag
331 if ( tagHt.count(tk) == 0) { // if element 0x0000 not found
332 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
333 elemZ = new gdcmElValue(tagZ);
335 Add(elemZ); // create it
337 elemZ=GetElementByNumber(gr_bid, 0x0000);
339 sprintf(trash ,"%d",g->second);
341 elemZ->SetValue(str_trash);
346 * \ingroup gdcmElValSet
351 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
359 vector<string> tokens;
363 // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian
364 // restent à tester les echecs en écriture (apres chaque fwrite)
366 for (TagElValueHT::iterator tag2=tagHt.begin();
370 gr = tag2->second->GetGroup();
371 el = tag2->second->GetElement();
372 lgr = tag2->second->GetLength();
373 val = tag2->second->GetValue().c_str();
374 vr = tag2->second->GetVR();
377 if (gr < 0x0008) continue;
378 // if (gr %2) continue; // pour voir
379 if (vr == "SQ" ) continue;
382 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
383 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
385 if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
386 // On est en EXPLICIT VR
387 guint16 z=0, shortLgr;
388 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
390 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
391 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
392 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
396 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
399 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
402 tokens.erase(tokens.begin(),tokens.end());
403 Tokenize (tag2->second->GetValue(), tokens, "\\");
405 if (vr == "US" || vr == "SS") {
406 for (unsigned int i=0; i<tokens.size();i++) {
407 val_uint16 = atoi(tokens[i].c_str());
409 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
413 if (vr == "UL" || vr == "SL") {
414 for (unsigned int i=0; i<tokens.size();i++) {
415 val_uint32 = atoi(tokens[i].c_str());
417 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
423 // Les pixels ne sont pas chargés dans l'element !
424 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
426 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
431 * \ingroup gdcmElValSet
436 int gdcmElValSet::Write(FILE * _fp, FileType type) {
438 if (type == ImplicitVR) {
439 string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
440 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
442 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
443 // values with a VR of UI shall be padded with a single trailing null
444 // Dans le cas suivant on doit pader manuellement avec un 0
446 SetElValueLengthByNumber(18, 0x0002, 0x0010);
449 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non ,
450 // (FileType est un champ de gdcmHeader ...)
451 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
454 if (type == ExplicitVR) {
455 string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
456 SetElValueByNumber(explicitVRTransfertSyntax, 0x0002, 0x0010);
458 SetElValueLengthByNumber(20, 0x0002, 0x0010);
461 if ( (type == ImplicitVR) || (type == ExplicitVR) )
462 UpdateGroupLength(false,type);
464 UpdateGroupLength(true,ACR);
466 WriteElements(type, _fp);