]> Creatis software - gdcm.git/blob - src/gdcmElValSet.cxx
gdcmElValSet::SetElValueByNumber and gdcmElValSet::SetElValueByName
[gdcm.git] / src / gdcmElValSet.cxx
1 // gdcmElValSet.cxx
2
3 #include <sstream>
4 #include "gdcmUtil.h"
5 #include "gdcmElValSet.h"
6
7 gdcmElValSet::~gdcmElValSet() {
8    for (TagElValueHT::iterator tag = tagHt.begin(); tag != tagHt.end(); ++tag) {
9       gdcmElValue* EntryToDelete = tag->second;
10       if ( EntryToDelete )
11          delete EntryToDelete;
12    }
13    tagHt.clear();
14    // Since Add() adds symetrical in both tagHt and NameHt we can
15    // assume all the pointed gdcmElValues are allready cleaned-up when
16    // we cleaned tagHt.
17    NameHt.clear();
18 }
19
20 TagElValueHT & gdcmElValSet::GetTagHt(void) {
21         return tagHt;
22 }
23
24 void gdcmElValSet::Add(gdcmElValue * newElValue) {
25         tagHt [newElValue->GetKey()]  = newElValue;
26         NameHt[newElValue->GetName()] = newElValue;
27 }
28
29 void gdcmElValSet::Print(ostream & os) {
30         for (TagElValueHT::iterator tag = tagHt.begin();
31                   tag != tagHt.end();
32                   ++tag){
33                 os << tag->first << ": ";
34                 os << "[" << tag->second->GetValue() << "]";
35                 os << "[" << tag->second->GetName()  << "]";
36                 os << "[" << tag->second->GetVR()    << "]"; 
37                 os << " lgr : " << tag->second->GetLength();
38                 os << endl;
39         }
40
41
42 void gdcmElValSet::PrintByName(ostream & os) {
43         for (TagElValueNameHT::iterator tag = NameHt.begin();
44                   tag != NameHt.end();
45                   ++tag){
46                 os << tag->first << ": ";
47                 os << "[" << tag->second->GetValue() << "]";
48                 os << "[" << tag->second->GetKey()   << "]";
49                 os << "[" << tag->second->GetVR()    << "]" << endl;
50         }
51 }
52
53 gdcmElValue* gdcmElValSet::GetElementByNumber(guint16 group, guint16 element) {
54         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
55         if ( ! tagHt.count(key))
56                 return (gdcmElValue*)0;
57         return tagHt.find(key)->second;
58 }
59
60 gdcmElValue* gdcmElValSet::GetElementByName(string TagName) {
61    if ( ! NameHt.count(TagName))
62       return (gdcmElValue*)0;
63    return NameHt.find(TagName)->second;
64 }
65
66 string gdcmElValSet::GetElValueByNumber(guint16 group, guint16 element) {
67         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
68         if ( ! tagHt.count(key))
69                 return "gdcm::Unfound";
70         return tagHt.find(key)->second->GetValue();
71 }
72
73 string gdcmElValSet::GetElValueByName(string TagName) {
74         if ( ! NameHt.count(TagName))
75                 return "gdcm::Unfound";
76         return NameHt.find(TagName)->second->GetValue();
77 }
78
79
80 int gdcmElValSet::SetElValueByNumber(string content,
81                                      guint16 group, guint16 element) {
82         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
83         if ( ! tagHt.count(key))
84                 return 0;
85         tagHt[key]->SetValue(content);  
86         string vr = tagHt[key]->GetVR();
87         guint32 lgr;
88
89         if( (vr == "US") || (vr == "SS") ) 
90            lgr = 2;
91         else if( (vr == "UL") || (vr == "SL") )
92            lgr = 4;
93         else 
94            lgr = content.length();
95            
96         tagHt[key]->SetLength(lgr); 
97         return 1;
98 }
99
100 int gdcmElValSet::SetElValueByName(string content, string TagName) {
101         if ( ! NameHt.count(TagName))
102                 return 0;
103         NameHt[TagName]->SetValue(content);
104         string vr = NameHt[TagName]->GetVR();
105         guint32 lgr;
106
107         if( (vr == "US") || (vr == "SS") ) 
108            lgr = 2;
109         else if( (vr == "UL") || (vr == "SL") )
110            lgr = 4;
111         else 
112            lgr = content.length();
113            
114         NameHt[TagName]->SetLength(lgr);
115         return 1;               
116 }
117
118 /**
119  * \ingroup gdcmElValSet
120  * \brief   Generate a free TagKey i.e. a TagKey that is not present
121  *          in the TagHt dictionary.
122  * @param   group The generated tag must belong to this group.  
123  * @return  The element of tag with given group which is fee.
124  */
125 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
126    for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
127       TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
128       if (tagHt.count(key) == 0)
129          return elem;
130    }
131    return UINT32_MAX;
132 }
133
134 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
135                                            guint16 group, guint16 element) {
136         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
137         if ( ! tagHt.count(key))
138                 return 0;
139         tagHt[key]->SetLength(length);   
140         return 1 ;              
141 }
142
143
144 int gdcmElValSet::SetElValueLengthByName(guint32 length, string TagName) {
145         if ( ! NameHt.count(TagName))
146                 return 0;
147         NameHt.find(TagName)->second->SetLength(length);         
148         return 1 ;              
149 }
150
151
152
153 void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
154    guint16 gr, el;
155    string vr;
156    
157    gdcmElValue *elem;
158    char trash[10];
159    string str_trash;
160    GroupKey key;
161    GroupHT groupHt;
162    TagKey tk;
163    gdcmElValue *elemZ;
164    
165    for (TagElValueHT::iterator tag2 = tagHt.begin();
166         tag2 != tagHt.end();
167         ++tag2){
168
169       elem  = tag2->second;
170       gr = elem->GetGroup();
171       el = elem->GetElement();
172       vr = elem->GetVR(); 
173                  
174       sprintf(trash, "%04x", gr);
175       key = trash;
176                   
177       if (SkipSequence && vr == "SQ") continue;
178          // pas SEQUENCE en ACR-NEMA
179          // WARNING : pb CERTAIN
180          //           si on est descendu 'a l'interieur' des SQ 
181          //
182          // --> la descente a l'interieur' des SQ 
183          // devra etre faite avec une liste chainee, pas avec une HTable...
184              
185       if ( groupHt.count(key) == 0) { 
186          if (el ==0x0000) {
187             groupHt[key] = 0;
188          } else {
189             groupHt[key] =2 + 2 + 4 + elem->GetLength();
190          } 
191       } else {       
192          if (type = ExplicitVR) {
193             if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
194                groupHt[key] +=  4;
195             }
196          }
197          groupHt[key] += 2 + 2 + 4 + elem->GetLength(); 
198       } 
199    }
200   
201      if(0)
202      for (GroupHT::iterator g = groupHt.begin();
203         g != groupHt.end();
204         ++g){        
205         printf("groupKey %s : %d\n",g->first.c_str(),g->second);
206      }
207        
208   
209    unsigned short int gr_bid;
210   
211    for (GroupHT::iterator g = groupHt.begin();
212         g != groupHt.end();
213         ++g){ 
214   
215       sscanf(g->first.c_str(),"%x",&gr_bid);
216       tk = g->first + "|0000";
217                      
218       if ( tagHt.count(tk) == 0) { 
219          gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");       
220          elemZ = new gdcmElValue(tagZ);
221          elemZ->SetLength(4);
222          Add(elemZ);
223       } else {
224          elemZ=GetElementByNumber(gr_bid, 0x0000);
225       }     
226       sprintf(trash ,"%d",g->second);
227       str_trash=trash;
228       elemZ->SetValue(str_trash);
229    }   
230 }
231
232 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
233    guint16 gr, el;
234    guint32 lgr;
235    const char * val;
236    string vr;
237    guint32 val_uint32;
238    guint16 val_uint16;
239    
240    vector<string> tokens;
241
242    void *ptr;
243
244    // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian 
245    // restent à tester les echecs en écriture (apres chaque fwrite)
246
247    for (TagElValueHT::iterator tag2=tagHt.begin();
248         tag2 != tagHt.end();
249         ++tag2){
250
251       gr =  tag2->second->GetGroup();
252       el =  tag2->second->GetElement();
253       lgr = tag2->second->GetLength();
254       val = tag2->second->GetValue().c_str();
255       vr =  tag2->second->GetVR();
256
257       if ( type == ACR ) { 
258          if (gr < 0x0008) continue;
259          if (gr %2)   continue;
260          if (vr == "SQ" ) continue;
261       } 
262
263       fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp);  //group
264       fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp);  //element
265
266       if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
267          // On est en EXPLICIT VR
268          guint16 z=0, shortLgr;
269          fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
270
271          if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
272             fwrite ( &z,  (size_t)2 ,(size_t)1 ,_fp);
273             fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
274
275          } else {
276             shortLgr=lgr;
277             fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
278          }
279       } else {
280          fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
281       }
282
283       tokens.erase(tokens.begin(),tokens.end());
284       Tokenize (tag2->second->GetValue(), tokens, "\\");
285
286       if (vr == "US" || vr == "SS") {
287          for (unsigned int i=0; i<tokens.size();i++) {
288             val_uint16 = atoi(tokens[i].c_str());
289             ptr = &val_uint16;
290             fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
291          }
292          continue;
293       }
294       if (vr == "UL" || vr == "SL") {
295          for (unsigned int i=0; i<tokens.size();i++) {
296             val_uint32 = atoi(tokens[i].c_str());
297             ptr = &val_uint32;
298             fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
299          }
300          continue;
301       }
302       tokens.clear();
303
304       // Les pixels ne sont pas chargés dans l'element !
305       if ((gr == 0x7fe0) && (el == 0x0010) ) break;
306
307       fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
308    }
309 }
310
311 int gdcmElValSet::Write(FILE * _fp, FileType type) {
312
313    if (type == ImplicitVR) {
314       string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
315       SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
316       
317       //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
318       //      values with a VR of UI shall be padded with a single trailing null
319       //      Dans le cas suivant on doit pader manuellement avec un 0
320       
321       SetElValueLengthByNumber(18, 0x0002, 0x0010);
322    }
323    
324         // Question :
325         // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non ,
326         // (FileType est un champ de gdcmHeader ...)
327         // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
328         // no way
329         
330         
331    if (type == ExplicitVR) {
332       string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
333       SetElValueByNumber(explicitVRTransfertSyntax, 0x0002, 0x0010);
334       // See above comment 
335       SetElValueLengthByNumber(20, 0x0002, 0x0010);
336    }
337
338    if ( (type == ImplicitVR) || (type == ExplicitVR) )
339       UpdateGroupLength(false,type);
340    if ( type == ACR)
341       UpdateGroupLength(true,ACR);
342
343    WriteElements(type, _fp);
344
345    return(1);
346 }