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