]> Creatis software - creaImageIO.git/blob - src/creaImageIOTreeAttributeDescriptor.cpp
Feature #1764
[creaImageIO.git] / src / creaImageIOTreeAttributeDescriptor.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27
28
29 #include <creaImageIOTreeAttributeDescriptor.h>
30 #include <creaImageIOSystem.h>
31
32 #if defined(USE_GDCM)
33 #include <gdcmGlobal.h>
34 #include <gdcmDictSet.h>
35 #endif
36
37 #if defined(USE_GDCM2)
38 #include <gdcmGlobal.h>
39 #include <gdcmDicts.h>
40 #include <gdcmDict.h>
41 #endif
42
43 #include <boost/algorithm/string/replace.hpp>
44
45 namespace creaImageIO
46 {
47
48   namespace tree
49   {
50
51     //========================================================================
52     void AttributeDescriptor::CleanName(std::string& str) const
53     {
54       // quote must be doubled for SQL
55       //    crea::Utils::Replace( str, "'", "''" );
56       boost::algorithm::replace_all(str,"'","''");
57       // Found strange strings which contained NULL char INSIDE string 
58       int i,size=str.size();
59       for (i=0;i<size;++i) 
60         {
61           if (str[i]==0) 
62             {
63               str = str.substr(0,i);
64               break;
65             }
66         }
67     }
68     //========================================================================
69
70     //=====================================================================
71     // Ctor with key, name and flags
72      AttributeDescriptor::AttributeDescriptor(const std::string& key,
73                                              const std::string& name,
74                                              unsigned int flags)
75       : mKey(key), mName(name), mGroup(0), mElement(0), mFlags(flags)
76     {
77
78       CleanName(mName);
79       GimmickDebugMessage(3,"AttributeDescriptor : '"<<key
80                           <<"' ["<<flags<<"]"<<std::endl);
81       GimmickDebugMessage(3,"='"<<mName<<"'"<<std::endl);
82     }
83
84     //=====================================================================
85
86      //=====================================================================
87    // Ctor with dicom group, elem and flags
88     // The key is built as 'Dgroup_elem'
89     // The user name is retreived from dicom dictionnary
90     AttributeDescriptor::AttributeDescriptor(unsigned short group,
91                                              unsigned short element,
92                                              unsigned int flags)
93       : mGroup(group), mElement(element), mFlags(flags)
94     {
95       
96       //GDCM_NAME_SPACE::TagKey tag(group,element);
97       char ctag[12];
98       sprintf(ctag,"D%04x_%04x",group,element);
99       mKey = ctag;
100
101       GimmickDebugMessage(3,"AttributeDescriptor : '"<<mKey
102                           <<"' ["<<flags<<"]"<<std::endl);
103
104 #if defined(USE_GDCM)
105       // Retrieve the name from gdcm dict
106       GDCM_NAME_SPACE::DictEntry* entry =
107         GDCM_NAME_SPACE::Global::GetDicts()
108         ->GetDefaultPubDict()->GetEntry(mGroup,mElement);
109           
110       if (entry)
111         {
112           mName = entry->GetName();
113           CleanName(mName);
114           GimmickDebugMessage(3,"='"<<mName<<"'"<<std::endl);
115         }
116       else
117         {
118           GimmickMessage(1,"!! WARNING : tag '"<<mKey
119                          <<"' is not in DICOM dictionnary ! "
120                          <<"Considering it as a user attribute"
121                          << std::endl);
122           mName = "UNKNOWN";
123           mGroup = mElement = 0;
124         }
125 #endif
126
127
128           
129           
130 #if defined(USE_GDCM2)
131       // Retrieve the name from gdcm dict
132         const gdcm::Global& g = gdcm::Global::GetInstance(); 
133          const gdcm::Dicts &dicts = g.GetDicts();
134   const gdcm::Dict &dict = dicts.GetPublicDict();
135           gdcm::DictEntry dictentry =  dict.GetDictEntry(gdcm::Tag(mGroup, mElement));
136           
137       mName = dictentry.GetName();
138           if(!mName.empty())
139           {
140                   CleanName(mName);
141                   GimmickDebugMessage(3,"='"<<mName<<"'"<<std::endl);
142           }
143       else
144           {
145                 GimmickMessage(1,"!! WARNING : tag '"<<mKey
146                          <<"' is not in DICOM dictionnary ! "
147                          <<"Considering it as a user attribute"
148                          << std::endl);
149                 mName = "UNKNOWN";
150                 mGroup = mElement = 0;
151                 }
152 #endif
153
154     }
155     //=====================================================================
156
157
158     //=====================================================================
159     /// Extracts group and element from a key of the form "Dgroup_elem" 
160     void AttributeDescriptor::GetDicomGroupElementFromKey(const std::string& key,
161                                                           unsigned short& group,
162                                                           unsigned short& elem)
163     {
164       group = elem = 0;
165       if ( (key.size()==10) &&
166            (key[0] == 'D') &&
167            (key[5] == '_') )
168           {
169          sscanf(key.c_str(),"D%04hx_%04hx ",&group,&elem);  
170           GimmickDebugMessage(3,"GetDicomGroupElementFromKey '"<<key<<"' : "                     <<group<<"|"<<elem<<std::endl);
171         }
172       else 
173         { 
174           GimmickMessage(5,"GetDicomGroupElementFromKey '"<<key<<"' : "
175                          <<" not a DICOM key format"<<std::endl);
176         }
177       return;
178     }
179
180         //=====================================================================
181         /// test if the type is a date
182         bool AttributeDescriptor::isDateEntry() const
183         {
184                  
185                 bool btest = false;
186                 // Retrieve the name from gdcm dict
187 #if defined(USE_GDCM)
188                 GDCM_NAME_SPACE::DictEntry* entry =     GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
189                 if(     entry != 0)
190                 {
191                         if( entry->GetVR().str() == "DA" )
192                         {
193                                 btest = true;
194                         }
195                 }
196 #endif
197 #if defined(USE_GDCM2)
198          const gdcm::Global& g = gdcm::Global::GetInstance(); 
199          const gdcm::Dicts &dicts = g.GetDicts();
200   const gdcm::Dict &dict = dicts.GetPublicDict();
201           if(mGroup != 0 && mElement != 0)
202           {
203                   gdcm::DictEntry dictentry =  dict.GetDictEntry(gdcm::Tag(GetGroup(), GetElement()));
204                 if( gdcm::VR::GetVRString(dictentry.GetVR()) == "DA")
205                 {
206                                 btest = true;
207                 }
208           }
209 #endif
210                 return btest;
211         }
212
213         //=====================================================================
214         /// test if the type is a time
215         bool AttributeDescriptor::isTimeEntry() const
216         {
217                  
218                 bool btest = false;
219 #if defined(USE_GDCM)
220                 // Retrieve the name from gdcm dict
221                 GDCM_NAME_SPACE::DictEntry* entry =     GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
222                 if(     entry != 0)
223                 {
224                         if( entry->GetVR().str() == "TM" )
225                         {
226                                 btest = true;
227                         }
228                 }
229 #endif
230
231 #if defined(USE_GDCM2)
232          const gdcm::Global& g = gdcm::Global::GetInstance(); // sum of all knowledge !
233          const gdcm::Dicts &dicts = g.GetDicts();
234   const gdcm::Dict &dict = dicts.GetPublicDict(); // Part 6
235           if(mGroup != 0 && mElement != 0)
236           {
237                 gdcm::DictEntry dictentry =  dict.GetDictEntry(gdcm::Tag(mGroup, mElement));
238                 if(gdcm::VR::GetVRString(dictentry.GetVR()) == "TM")
239                 {
240                                 btest = true;
241                 }
242           }
243 #endif
244
245                 return btest;
246         }
247
248
249     //=====================================================================
250         /// Decodes the type of the attribute
251      void AttributeDescriptor::DecodeType(unsigned int& typ) const
252           {
253                  std::string type=""; 
254 #if defined(USE_GDCM)   
255                   // Retrieve the name from gdcm dict
256                 GDCM_NAME_SPACE::DictEntry* entry =
257                 GDCM_NAME_SPACE::Global::GetDicts()
258                 ->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
259
260                 if (entry==0) 
261                 {
262                         typ = 2;
263                         return;
264                 }
265                  type = entry->GetVR().str();
266 #endif
267 #if defined(USE_GDCM2)
268          const gdcm::Global& g = gdcm::Global::GetInstance(); // sum of all knowledge !
269          const gdcm::Dicts &dicts = g.GetDicts();
270   const gdcm::Dict &dict = dicts.GetPublicDict(); // Part 6
271           gdcm::DictEntry dictentry =  dict.GetDictEntry(gdcm::Tag(mGroup, mElement));
272           type = gdcm::VR::GetVRString(dictentry.GetVR());
273 #endif
274
275                 GimmickDebugMessage(3,"VR Value is "<<type<<"!"<<std::endl);
276                 if(type=="AS" ||
277                 type=="DA" ||
278                 type=="FL" ||
279                 type=="FD" ||
280                 type=="IS" ||
281                 type=="SL" ||
282                 type=="SS" ||
283                 type=="UI" ||
284                 type=="US" ||
285                 type=="SH")
286                 {
287                         // Numerical 
288                         typ = 1;
289                 }
290                 else
291                 {
292                         // String
293                         typ = 2;
294                 }
295                 
296           }
297           //=====================================================================
298
299   } // EO namespace tree
300
301 } // EO namespace creaImageIO