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