]> Creatis software - creaImageIO.git/blob - src2/creaImageIOTreeAttributeDescriptor.cpp
3d6a0e3dd9a00b9e3be04944de361651bc5c1a96
[creaImageIO.git] / src2 / creaImageIOTreeAttributeDescriptor.cpp
1 #include <creaImageIOTreeAttributeDescriptor.h>
2 #include <creaImageIOSystem.h>
3
4 #include <gdcmGlobal.h>
5 #include <gdcmDictSet.h>
6
7 #include <boost/algorithm/string/replace.hpp>
8
9 namespace creaImageIO
10 {
11
12   namespace tree
13   {
14
15     //========================================================================
16     void AttributeDescriptor::CleanName(std::string& str) const
17     {
18       // quote must be doubled for SQL
19       //    crea::Utils::Replace( str, "'", "''" );
20       boost::algorithm::replace_all(str,"'","''");
21       // Found strange strings which contained NULL char INSIDE string 
22       int i,size=str.size();
23       for (i=0;i<size;++i) 
24         {
25           if (str[i]==0) 
26             {
27               str = str.substr(0,i);
28               break;
29             }
30         }
31     }
32     //========================================================================
33
34     //=====================================================================
35     // Ctor with key, name and flags
36      AttributeDescriptor::AttributeDescriptor(const std::string& key,
37                                              const std::string& name,
38                                              unsigned int flags)
39       : mKey(key), mName(name), mGroup(0), mElement(0), mFlags(flags)
40     {
41
42       CleanName(mName);
43       GimmickDebugMessage(3,"AttributeDescriptor : '"<<key
44                           <<"' ["<<flags<<"]"<<std::endl);
45       GimmickDebugMessage(3,"='"<<mName<<"'"<<std::endl);
46     }
47
48     //=====================================================================
49
50      //=====================================================================
51    // Ctor with dicom group, elem and flags
52     // The key is built as 'Dgroup_elem'
53     // The user name is retreived from dicom dictionnary
54     AttributeDescriptor::AttributeDescriptor(unsigned short group,
55                                              unsigned short element,
56                                              unsigned int flags)
57       : mGroup(group), mElement(element), mFlags(flags)
58     {
59       
60       //GDCM_NAME_SPACE::TagKey tag(group,element);
61       char ctag[12];
62       sprintf(ctag,"D%04x_%04x",group,element);
63       mKey = ctag;
64
65       GimmickDebugMessage(3,"AttributeDescriptor : '"<<mKey
66                           <<"' ["<<flags<<"]"<<std::endl);
67
68       // Retrieve the name from gdcm dict
69       GDCM_NAME_SPACE::DictEntry* entry =
70         GDCM_NAME_SPACE::Global::GetDicts()
71         ->GetDefaultPubDict()->GetEntry(mGroup,mElement);
72           
73       if (entry)
74         {
75           mName = entry->GetName();
76           CleanName(mName);
77           GimmickDebugMessage(3,"='"<<mName<<"'"<<std::endl);
78         }
79       else
80         {
81           GimmickMessage(1,"!! WARNING : tag '"<<mKey
82                          <<"' is not in DICOM dictionnary ! "
83                          <<"Considering it as a user attribute"
84                          << std::endl);
85           mName = "UNKNOWN";
86           mGroup = mElement = 0;
87         }
88       
89     }
90     //=====================================================================
91
92
93     //=====================================================================
94     /// Extracts group and element from a key of the form "Dgroup_elem" 
95     void AttributeDescriptor::GetDicomGroupElementFromKey(const std::string& key,
96                                                           unsigned short& group,
97                                                           unsigned short& elem)
98     {
99       group = elem = 0;
100       if ( (key.size()==10) &&
101            (key[0] == 'D') &&
102            (key[5] == '_') )
103           {
104          sscanf(key.c_str(),"D%04hx_%04hx ",&group,&elem);  
105           GimmickDebugMessage(3,"GetDicomGroupElementFromKey '"<<key<<"' : "                     <<group<<"|"<<elem<<std::endl);
106         }
107       else 
108         { 
109           GimmickMessage(5,"GetDicomGroupElementFromKey '"<<key<<"' : "
110                          <<" not a DICOM key format"<<std::endl);
111         }
112       return;
113     }
114
115         //=====================================================================
116         /// test if the type is a date
117         bool AttributeDescriptor::isDateEntry() const
118         {
119                  
120                 bool btest = false;
121                 // Retrieve the name from gdcm dict
122                 GDCM_NAME_SPACE::DictEntry* entry =     GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
123                 if(     entry != 0)
124                 {
125                         if( entry->GetVR().str() == "DA" )
126                         {
127                                 btest = true;
128                         }
129                 }
130                 return btest;
131         }
132
133         //=====================================================================
134         /// test if the type is a time
135         bool AttributeDescriptor::isTimeEntry() const
136         {
137                  
138                 bool btest = false;
139                 // Retrieve the name from gdcm dict
140                 GDCM_NAME_SPACE::DictEntry* entry =     GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
141                 if(     entry != 0)
142                 {
143                         if( entry->GetVR().str() == "TM" )
144                         {
145                                 btest = true;
146                         }
147                 }
148                 return btest;
149         }
150
151
152     //=====================================================================
153         /// Decodes the type of the attribute
154      void AttributeDescriptor::DecodeType(unsigned int& typ) const
155           {
156                   
157                 
158                   // Retrieve the name from gdcm dict
159                 GDCM_NAME_SPACE::DictEntry* entry =
160                 GDCM_NAME_SPACE::Global::GetDicts()
161                 ->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
162
163                 if (entry==0) 
164                 {
165                         typ = 2;
166                         return;
167                 }
168                 std::string type = entry->GetVR().str();
169                 GimmickDebugMessage(3,"VR Value is "<<type<<"!"<<std::endl);
170                 if(type=="AS" ||
171                 type=="DA" ||
172                 type=="FL" ||
173                 type=="FD" ||
174                 type=="IS" ||
175                 type=="SL" ||
176                 type=="SS" ||
177                 type=="UI" ||
178                 type=="US" ||
179                 type=="SH")
180                 {
181                         // Numerical 
182                         typ = 1;
183                 }
184                 else
185                 {
186                         // String
187                         typ = 2;
188                 }
189                 
190           }
191           //=====================================================================
192
193   } // EO namespace tree
194
195 } // EO namespace creaImageIO