]> Creatis software - gdcm.git/blob - src/gdcmElValSet.cxx
Add protected method : gdcmElValue * GetElValueByNumber(guint16 group, guint16...
[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         tagHt[key]->SetLength(lgr); 
96         return 1;
97 }
98
99 int gdcmElValSet::SetElValueByName(string content, string TagName) {
100         if ( ! NameHt.count(TagName))
101                 return 0;
102         NameHt[TagName]->SetValue(content);
103         string vr = NameHt[TagName]->GetVR();
104         guint32 lgr;
105
106         if( (vr == "US") || (vr == "SS") ) 
107            lgr = 2;
108         else if( (vr == "UL") || (vr == "SL") )
109            lgr = 4;        
110         else 
111            lgr = content.length();
112            
113 // TODO : WARNING: le cas de l'element des pixels (7fe0,0010) n'est pas traite
114 // par SetElValueByName
115 // il faudra utiliser SetElValueByNumber
116            
117         NameHt[TagName]->SetLength(lgr);
118         return 1;               
119 }
120
121 /**
122  * \ingroup gdcmElValSet
123  * \brief   Generate a free TagKey i.e. a TagKey that is not present
124  *          in the TagHt dictionary.
125  * @param   group The generated tag must belong to this group.  
126  * @return  The element of tag with given group which is fee.
127  */
128 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
129    for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
130       TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
131       if (tagHt.count(key) == 0)
132          return elem;
133    }
134    return UINT32_MAX;
135 }
136
137 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
138                                            guint16 group, guint16 element) {
139         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
140         if ( ! tagHt.count(key))
141                 return 0;
142         tagHt[key]->SetLength(length);   
143         return 1 ;              
144 }
145
146
147 int gdcmElValSet::SetElValueLengthByName(guint32 length, string TagName) {
148         if ( ! NameHt.count(TagName))
149                 return 0;
150         NameHt.find(TagName)->second->SetLength(length);         
151         return 1 ;              
152 }
153
154
155
156 void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
157    guint16 gr, el;
158    string vr;
159    
160    gdcmElValue *elem;
161    char trash[10];
162    string str_trash;
163    GroupKey key;
164    GroupHT groupHt;
165    TagKey tk;
166    gdcmElValue *elemZ;
167    
168    for (TagElValueHT::iterator tag2 = tagHt.begin();
169         tag2 != tagHt.end();
170         ++tag2){
171
172       elem  = tag2->second;
173       gr = elem->GetGroup();
174       el = elem->GetElement();
175       vr = elem->GetVR(); 
176                  
177       sprintf(trash, "%04x", gr);
178       key = trash;
179                   
180       if (SkipSequence && vr == "SQ") continue;
181          // pas SEQUENCE en ACR-NEMA
182          // WARNING : pb CERTAIN
183          //           si on est descendu 'a l'interieur' des SQ 
184          //
185          // --> la descente a l'interieur' des SQ 
186          // devra etre faite avec une liste chainee, pas avec une HTable...
187              
188       if ( groupHt.count(key) == 0) { 
189          if (el ==0x0000) {
190             groupHt[key] = 0;
191          } else {
192             groupHt[key] =2 + 2 + 4 + elem->GetLength();
193          } 
194       } else {       
195          if (type = ExplicitVR) {
196             if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
197                groupHt[key] +=  4;
198             }
199          }
200          groupHt[key] += 2 + 2 + 4 + elem->GetLength(); 
201       } 
202    }
203   
204      if(0)
205      for (GroupHT::iterator g = groupHt.begin();
206         g != groupHt.end();
207         ++g){        
208         printf("groupKey %s : %d\n",g->first.c_str(),g->second);
209      }
210        
211   
212    unsigned short int gr_bid;
213   
214    for (GroupHT::iterator g = groupHt.begin();
215         g != groupHt.end();
216         ++g){ 
217   
218       sscanf(g->first.c_str(),"%x",&gr_bid);
219       tk = g->first + "|0000";
220                      
221       if ( tagHt.count(tk) == 0) { 
222          gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");       
223          elemZ = new gdcmElValue(tagZ);
224          elemZ->SetLength(4);
225          Add(elemZ);
226       } else {
227          elemZ=GetElementByNumber(gr_bid, 0x0000);
228       }     
229       sprintf(trash ,"%d",g->second);
230       str_trash=trash;
231       elemZ->SetValue(str_trash);
232    }   
233 }
234
235 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
236    guint16 gr, el;
237    guint32 lgr;
238    const char * val;
239    string vr;
240    guint32 val_uint32;
241    guint16 val_uint16;
242    
243    vector<string> tokens;
244
245    void *ptr;
246
247    // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian 
248    // restent à tester les echecs en écriture (apres chaque fwrite)
249
250    for (TagElValueHT::iterator tag2=tagHt.begin();
251         tag2 != tagHt.end();
252         ++tag2){
253
254       gr =  tag2->second->GetGroup();
255       el =  tag2->second->GetElement();
256       lgr = tag2->second->GetLength();
257       val = tag2->second->GetValue().c_str();
258       vr =  tag2->second->GetVR();
259
260       if ( type == ACR ) { 
261          if (gr < 0x0008) continue;
262          if (gr %2)   continue;
263          if (vr == "SQ" ) continue;
264       } 
265
266       fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp);  //group
267       fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp);  //element
268
269       if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
270          // On est en EXPLICIT VR
271          guint16 z=0, shortLgr;
272          fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
273
274          if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
275             fwrite ( &z,  (size_t)2 ,(size_t)1 ,_fp);
276             fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
277
278          } else {
279             shortLgr=lgr;
280             fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
281          }
282       } else {
283          fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
284       }
285
286       tokens.erase(tokens.begin(),tokens.end());
287       Tokenize (tag2->second->GetValue(), tokens, "\\");
288
289       if (vr == "US" || vr == "SS") {
290          for (unsigned int i=0; i<tokens.size();i++) {
291             val_uint16 = atoi(tokens[i].c_str());
292             ptr = &val_uint16;
293             fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
294          }
295          continue;
296       }
297       if (vr == "UL" || vr == "SL") {
298          for (unsigned int i=0; i<tokens.size();i++) {
299             val_uint32 = atoi(tokens[i].c_str());
300             ptr = &val_uint32;
301             fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
302          }
303          continue;
304       }
305       tokens.clear();
306
307       // Les pixels ne sont pas chargés dans l'element !
308       if ((gr == 0x7fe0) && (el == 0x0010) ) break;
309
310       fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
311    }
312 }
313
314 int gdcmElValSet::Write(FILE * _fp, FileType type) {
315
316    if (type == ImplicitVR) {
317       string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
318       SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
319       
320       //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
321       //      values with a VR of UI shall be padded with a single trailing null
322       //      Dans le cas suivant on doit pader manuellement avec un 0
323       
324       SetElValueLengthByNumber(18, 0x0002, 0x0010);
325    }
326    
327         // Question :
328         // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non ,
329         // (FileType est un champ de gdcmHeader ...)
330         // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
331         // no way
332         
333         
334    if (type == ExplicitVR) {
335       string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
336       SetElValueByNumber(explicitVRTransfertSyntax, 0x0002, 0x0010);
337       // See above comment 
338       SetElValueLengthByNumber(20, 0x0002, 0x0010);
339    }
340
341    if ( (type == ImplicitVR) || (type == ExplicitVR) )
342       UpdateGroupLength(false,type);
343    if ( type == ACR)
344       UpdateGroupLength(true,ACR);
345
346    WriteElements(type, _fp);
347
348    return(1);
349 }