]> Creatis software - creaImageIO.git/blob - src/creaImageIOTreeAttributeDescriptor.cpp
#3185 creaImageIO Feature New Normal - Clean code
[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
31
32 #if defined(_WIN32)
33 #pragma warning(disable: 4996)
34 #endif
35
36 #if defined(USE_GDCM)
37 #include <gdcmGlobal.h>
38 #include <gdcmDictSet.h>
39 #endif
40
41 #if defined(USE_GDCM2)
42 #include <gdcmGlobal.h>
43 #include <gdcmDicts.h>
44 #include <gdcmDict.h>
45 #endif
46
47 #include <boost/algorithm/string/replace.hpp>
48
49 namespace creaImageIO
50 {
51
52   namespace tree
53   {
54
55     //========================================================================
56     void AttributeDescriptor::CleanName(std::string& str) const
57     {
58       // quote must be doubled for SQL
59       //    crea::Utils::Replace( str, "'", "''" );
60       boost::algorithm::replace_all(str,"'","''");
61       // Found strange strings which contained NULL char INSIDE string 
62       int i,size=(int)str.size();
63       for (i=0;i<size;++i) 
64         {
65           if (str[i]==0) 
66             {
67               str = str.substr(0,i);
68               break;
69             }
70         }
71     }
72     //========================================================================
73
74     //=====================================================================
75     // Ctor with key, name and flags
76      AttributeDescriptor::AttributeDescriptor(const std::string& key,
77                                              const std::string& name,
78                                              unsigned int flags)
79       : mKey(key), mName(name), mGroup(0), mElement(0), mFlags(flags)
80     {
81
82       CleanName(mName);
83       GimmickDebugMessage(3,"AttributeDescriptor : '"<<key
84                           <<"' ["<<flags<<"]"<<std::endl);
85       GimmickDebugMessage(3,"='"<<mName<<"'"<<std::endl);
86     }
87
88     //=====================================================================
89
90      //=====================================================================
91    // Ctor with dicom group, elem and flags
92     // The key is built as 'Dgroup_elem'
93     // The user name is retreived from dicom dictionnary
94     AttributeDescriptor::AttributeDescriptor(unsigned short group,
95                                              unsigned short element,
96                                              unsigned int flags)
97       : mGroup(group), mElement(element), mFlags(flags)
98     {
99       
100       //GDCM_NAME_SPACE::TagKey tag(group,element);
101       char ctag[12];
102       sprintf(ctag,"D%04x_%04x",group,element);
103       mKey = ctag;
104
105       GimmickDebugMessage(3,"AttributeDescriptor : '"<<mKey
106                           <<"' ["<<flags<<"]"<<std::endl);
107
108 #if defined(USE_GDCM)
109       // Retrieve the name from gdcm dict
110       GDCM_NAME_SPACE::DictEntry* entry =
111         GDCM_NAME_SPACE::Global::GetDicts()
112         ->GetDefaultPubDict()->GetEntry(mGroup,mElement);
113           
114       if (entry)
115         {
116           mName = entry->GetName();
117           CleanName(mName);
118           GimmickDebugMessage(3,"='"<<mName<<"'"<<std::endl);
119         }
120       else
121         {
122           GimmickMessage(1,"!! WARNING : tag '"<<mKey
123                          <<"' is not in DICOM dictionnary ! "
124                          <<"Considering it as a user attribute"
125                          << std::endl);
126           mName = "UNKNOWN";
127           mGroup = mElement = 0;
128         }
129 #endif
130
131
132           
133           
134 #if defined(USE_GDCM2)
135       // Retrieve the name from gdcm dict
136         const gdcm::Global& g = gdcm::Global::GetInstance(); 
137          const gdcm::Dicts &dicts = g.GetDicts();
138   const gdcm::Dict &dict = dicts.GetPublicDict();
139           gdcm::DictEntry dictentry =  dict.GetDictEntry(gdcm::Tag(mGroup, mElement));
140           
141       mName = dictentry.GetName();
142           if(!mName.empty())
143           {
144                   CleanName(mName);
145                   GimmickDebugMessage(3,"='"<<mName<<"'"<<std::endl);
146           }
147       else
148           {
149                 GimmickMessage(1,"!! WARNING : tag '"<<mKey
150                          <<"' is not in DICOM dictionnary ! "
151                          <<"Considering it as a user attribute"
152                          << std::endl);
153                 mName = "UNKNOWN";
154                 mGroup = mElement = 0;
155                 }
156 #endif
157
158     }
159     //=====================================================================
160
161
162     //=====================================================================
163     /// Extracts group and element from a key of the form "Dgroup_elem" 
164     void AttributeDescriptor::GetDicomGroupElementFromKey(const std::string& key,
165                                                           unsigned short& group,
166                                                           unsigned short& elem)
167     {
168       group = elem = 0;
169       if ( (key.size()==10) &&
170            (key[0] == 'D') &&
171            (key[5] == '_') )
172           {
173          sscanf(key.c_str(),"D%04hx_%04hx ",&group,&elem);  
174           GimmickDebugMessage(3,"GetDicomGroupElementFromKey '"<<key<<"' : "                     <<group<<"|"<<elem<<std::endl);
175         }
176       else 
177         { 
178           GimmickMessage(5,"GetDicomGroupElementFromKey '"<<key<<"' : "
179                          <<" not a DICOM key format"<<std::endl);
180         }
181       return;
182     }
183
184         //=====================================================================
185         /// test if the type is a date
186         bool AttributeDescriptor::isDateEntry() const
187         {
188                  
189                 bool btest = false;
190                 // Retrieve the name from gdcm dict
191 #if defined(USE_GDCM)
192                 GDCM_NAME_SPACE::DictEntry* entry =     GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
193                 if(     entry != 0)
194                 {
195                         if( entry->GetVR().str() == "DA" )
196                         {
197                                 btest = true;
198                         }
199                 }
200 #endif
201 #if defined(USE_GDCM2)
202          const gdcm::Global& g = gdcm::Global::GetInstance(); 
203          const gdcm::Dicts &dicts = g.GetDicts();
204   const gdcm::Dict &dict = dicts.GetPublicDict();
205           if(mGroup != 0 && mElement != 0)
206           {
207                   gdcm::DictEntry dictentry =  dict.GetDictEntry(gdcm::Tag(GetGroup(), GetElement()));
208                 if( gdcm::VR::GetVRString(dictentry.GetVR()) == "DA")
209                 {
210                                 btest = true;
211                 }
212           }
213 #endif
214                 return btest;
215         }
216
217         //=====================================================================
218         /// test if the type is a time
219         bool AttributeDescriptor::isTimeEntry() const
220         {
221                  
222                 bool btest = false;
223 #if defined(USE_GDCM)
224                 // Retrieve the name from gdcm dict
225                 GDCM_NAME_SPACE::DictEntry* entry =     GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
226                 if(     entry != 0)
227                 {
228                         if( entry->GetVR().str() == "TM" )
229                         {
230                                 btest = true;
231                         }
232                 }
233 #endif
234
235 #if defined(USE_GDCM2)
236          const gdcm::Global& g = gdcm::Global::GetInstance(); // sum of all knowledge !
237          const gdcm::Dicts &dicts = g.GetDicts();
238   const gdcm::Dict &dict = dicts.GetPublicDict(); // Part 6
239           if(mGroup != 0 && mElement != 0)
240           {
241                 gdcm::DictEntry dictentry =  dict.GetDictEntry(gdcm::Tag(mGroup, mElement));
242                 if(gdcm::VR::GetVRString(dictentry.GetVR()) == "TM")
243                 {
244                                 btest = true;
245                 }
246           }
247 #endif
248
249                 return btest;
250         }
251
252
253     //=====================================================================
254         /// Decodes the type of the attribute
255      void AttributeDescriptor::DecodeType(unsigned int& typ) const
256           {
257                  std::string type=""; 
258 #if defined(USE_GDCM)   
259                   // Retrieve the name from gdcm dict
260                 GDCM_NAME_SPACE::DictEntry* entry =
261                 GDCM_NAME_SPACE::Global::GetDicts()
262                 ->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
263
264                 if (entry==0) 
265                 {
266                         typ = 2;
267                         return;
268                 }
269                  type = entry->GetVR().str();
270 #endif
271 #if defined(USE_GDCM2)
272          const gdcm::Global& g = gdcm::Global::GetInstance(); // sum of all knowledge !
273          const gdcm::Dicts &dicts = g.GetDicts();
274   const gdcm::Dict &dict = dicts.GetPublicDict(); // Part 6
275           gdcm::DictEntry dictentry =  dict.GetDictEntry(gdcm::Tag(mGroup, mElement));
276           type = gdcm::VR::GetVRString(dictentry.GetVR());
277 #endif
278
279                 GimmickDebugMessage(3,"VR Value is "<<type<<"!"<<std::endl);
280                 if(type=="AS" ||
281                 type=="DA" ||
282                 type=="FL" ||
283                 type=="FD" ||
284                 type=="IS" ||
285                 type=="SL" ||
286                 type=="SS" ||
287                 type=="UI" ||
288                 type=="US" ||
289                 type=="SH")
290                 {
291                         // Numerical 
292                         typ = 1;
293                 }
294                 else
295                 {
296                         // String
297                         typ = 2;
298                 }
299                 
300           }
301           //=====================================================================
302
303   } // EO namespace tree
304
305 } // EO namespace creaImageIO