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