]> Creatis software - creaImageIO.git/blob - src2/creaImageIOTreeAttributeDescriptor.cpp
3d3ffb1df97c5e17788e582b7f6c63db459e7f31
[creaImageIO.git] / src2 / 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           gdcm::Dicts dicts;
106           gdcm::DictEntry dictentry =  dicts.GetDictEntry(gdcm::Tag(mGroup, mElement));
107           
108       mName = dictentry.GetName();
109           if(!mName.empty())
110           {
111                   CleanName(mName);
112                   GimmickDebugMessage(3,"='"<<mName<<"'"<<std::endl);
113           }
114       else
115           {
116                 GimmickMessage(1,"!! WARNING : tag '"<<mKey
117                          <<"' is not in DICOM dictionnary ! "
118                          <<"Considering it as a user attribute"
119                          << std::endl);
120                 mName = "UNKNOWN";
121                 mGroup = mElement = 0;
122                 }
123 #endif
124
125     }
126     //=====================================================================
127
128
129     //=====================================================================
130     /// Extracts group and element from a key of the form "Dgroup_elem" 
131     void AttributeDescriptor::GetDicomGroupElementFromKey(const std::string& key,
132                                                           unsigned short& group,
133                                                           unsigned short& elem)
134     {
135       group = elem = 0;
136       if ( (key.size()==10) &&
137            (key[0] == 'D') &&
138            (key[5] == '_') )
139           {
140          sscanf(key.c_str(),"D%04hx_%04hx ",&group,&elem);  
141           GimmickDebugMessage(3,"GetDicomGroupElementFromKey '"<<key<<"' : "                     <<group<<"|"<<elem<<std::endl);
142         }
143       else 
144         { 
145           GimmickMessage(5,"GetDicomGroupElementFromKey '"<<key<<"' : "
146                          <<" not a DICOM key format"<<std::endl);
147         }
148       return;
149     }
150
151         //=====================================================================
152         /// test if the type is a date
153         bool AttributeDescriptor::isDateEntry() const
154         {
155                  
156                 bool btest = false;
157                 // Retrieve the name from gdcm dict
158 #if defined(USE_GDCM)
159                 GDCM_NAME_SPACE::DictEntry* entry =     GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
160                 if(     entry != 0)
161                 {
162                         if( entry->GetVR().str() == "DA" )
163                         {
164                                 btest = true;
165                         }
166                 }
167 #endif
168 #if defined(USE_GDCM2)
169          const gdcm::Global& g = gdcm::Global::GetInstance(); // sum of all knowledge !
170          const gdcm::Dicts &dicts = g.GetDicts();
171   const gdcm::Dict &dict = dicts.GetPublicDict(); // Part 6
172
173           if(mGroup != 0 && mElement != 0)
174           {
175                   gdcm::DictEntry dictentry =  dict.GetDictEntry(gdcm::Tag(GetGroup(), GetElement()));
176                 if( gdcm::VR::GetVRString(dictentry.GetVR()) == "DA")
177                 {
178                                 btest = true;
179                 }
180           }
181 #endif
182                 return btest;
183         }
184
185         //=====================================================================
186         /// test if the type is a time
187         bool AttributeDescriptor::isTimeEntry() const
188         {
189                  
190                 bool btest = false;
191 #if defined(USE_GDCM)
192                 // Retrieve the name from gdcm dict
193                 GDCM_NAME_SPACE::DictEntry* entry =     GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
194                 if(     entry != 0)
195                 {
196                         if( entry->GetVR().str() == "TM" )
197                         {
198                                 btest = true;
199                         }
200                 }
201 #endif
202
203 #if defined(USE_GDCM2)
204          const gdcm::Global& g = gdcm::Global::GetInstance(); // sum of all knowledge !
205          const gdcm::Dicts &dicts = g.GetDicts();
206   const gdcm::Dict &dict = dicts.GetPublicDict(); // Part 6
207           if(mGroup != 0 && mElement != 0)
208           {
209                 gdcm::DictEntry dictentry =  dict.GetDictEntry(gdcm::Tag(mGroup, mElement));
210                 if(gdcm::VR::GetVRString(dictentry.GetVR()) == "TM")
211                 {
212                                 btest = true;
213                 }
214           }
215 #endif
216
217                 return btest;
218         }
219
220
221     //=====================================================================
222         /// Decodes the type of the attribute
223      void AttributeDescriptor::DecodeType(unsigned int& typ) const
224           {
225                  std::string type=""; 
226 #if defined(USE_GDCM)   
227                   // Retrieve the name from gdcm dict
228                 GDCM_NAME_SPACE::DictEntry* entry =
229                 GDCM_NAME_SPACE::Global::GetDicts()
230                 ->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
231
232                 if (entry==0) 
233                 {
234                         typ = 2;
235                         return;
236                 }
237                  type = entry->GetVR().str();
238 #endif
239 #if defined(USE_GDCM2)
240          const gdcm::Global& g = gdcm::Global::GetInstance(); // sum of all knowledge !
241          const gdcm::Dicts &dicts = g.GetDicts();
242   const gdcm::Dict &dict = dicts.GetPublicDict(); // Part 6
243           gdcm::DictEntry dictentry =  dict.GetDictEntry(gdcm::Tag(mGroup, mElement));
244           type = gdcm::VR::GetVRString(dictentry.GetVR());
245 #endif
246
247                 GimmickDebugMessage(3,"VR Value is "<<type<<"!"<<std::endl);
248                 if(type=="AS" ||
249                 type=="DA" ||
250                 type=="FL" ||
251                 type=="FD" ||
252                 type=="IS" ||
253                 type=="SL" ||
254                 type=="SS" ||
255                 type=="UI" ||
256                 type=="US" ||
257                 type=="SH")
258                 {
259                         // Numerical 
260                         typ = 1;
261                 }
262                 else
263                 {
264                         // String
265                         typ = 2;
266                 }
267                 
268           }
269           //=====================================================================
270
271   } // EO namespace tree
272
273 } // EO namespace creaImageIO