creaImageIOTreeAttributeDescriptor.cpp

Go to the documentation of this file.
00001 #include <creaImageIOTreeAttributeDescriptor.h>
00002 #include <creaImageIOSystem.h>
00003 
00004 
00005 #if defined(USE_GDCM)
00006 #include <gdcmGlobal.h>
00007 #include <gdcmDictSet.h>
00008 #endif
00009 
00010 #if defined(USE_GDCM2)
00011 #include <gdcmGlobal.h>
00012 #include <gdcmDicts.h>
00013 #include <gdcmDict.h>
00014 #endif
00015 
00016 #include <boost/algorithm/string/replace.hpp>
00017 
00018 namespace creaImageIO
00019 {
00020 
00021   namespace tree
00022   {
00023 
00024     //========================================================================
00025     void AttributeDescriptor::CleanName(std::string& str) const
00026     {
00027       // quote must be doubled for SQL
00028       //    crea::Utils::Replace( str, "'", "''" );
00029       boost::algorithm::replace_all(str,"'","''");
00030       // Found strange strings which contained NULL char INSIDE string 
00031       int i,size=str.size();
00032       for (i=0;i<size;++i) 
00033         {
00034           if (str[i]==0) 
00035             {
00036               str = str.substr(0,i);
00037               break;
00038             }
00039         }
00040     }
00041     //========================================================================
00042 
00043     //=====================================================================
00044     // Ctor with key, name and flags
00045      AttributeDescriptor::AttributeDescriptor(const std::string& key,
00046                                              const std::string& name,
00047                                              unsigned int flags)
00048       : mKey(key), mName(name), mGroup(0), mElement(0), mFlags(flags)
00049     {
00050 
00051       CleanName(mName);
00052       GimmickDebugMessage(3,"AttributeDescriptor : '"<<key
00053                           <<"' ["<<flags<<"]"<<std::endl);
00054       GimmickDebugMessage(3,"='"<<mName<<"'"<<std::endl);
00055     }
00056 
00057     //=====================================================================
00058 
00059      //=====================================================================
00060    // Ctor with dicom group, elem and flags
00061     // The key is built as 'Dgroup_elem'
00062     // The user name is retreived from dicom dictionnary
00063     AttributeDescriptor::AttributeDescriptor(unsigned short group,
00064                                              unsigned short element,
00065                                              unsigned int flags)
00066       : mGroup(group), mElement(element), mFlags(flags)
00067     {
00068       
00069       //GDCM_NAME_SPACE::TagKey tag(group,element);
00070       char ctag[12];
00071       sprintf(ctag,"D%04x_%04x",group,element);
00072       mKey = ctag;
00073 
00074       GimmickDebugMessage(3,"AttributeDescriptor : '"<<mKey
00075                           <<"' ["<<flags<<"]"<<std::endl);
00076 
00077 #if defined(USE_GDCM)
00078       // Retrieve the name from gdcm dict
00079       GDCM_NAME_SPACE::DictEntry* entry =
00080         GDCM_NAME_SPACE::Global::GetDicts()
00081         ->GetDefaultPubDict()->GetEntry(mGroup,mElement);
00082           
00083       if (entry)
00084         {
00085           mName = entry->GetName();
00086           CleanName(mName);
00087           GimmickDebugMessage(3,"='"<<mName<<"'"<<std::endl);
00088         }
00089       else
00090         {
00091           GimmickMessage(1,"!! WARNING : tag '"<<mKey
00092                          <<"' is not in DICOM dictionnary ! "
00093                          <<"Considering it as a user attribute"
00094                          << std::endl);
00095           mName = "UNKNOWN";
00096           mGroup = mElement = 0;
00097         }
00098 #endif
00099 
00100 
00101           
00102           
00103 #if defined(USE_GDCM2)
00104       // Retrieve the name from gdcm dict
00105         const gdcm::Global& g = gdcm::Global::GetInstance(); 
00106          const gdcm::Dicts &dicts = g.GetDicts();
00107   const gdcm::Dict &dict = dicts.GetPublicDict();
00108           gdcm::DictEntry dictentry =  dict.GetDictEntry(gdcm::Tag(mGroup, mElement));
00109           
00110       mName = dictentry.GetName();
00111           if(!mName.empty())
00112           {
00113                   CleanName(mName);
00114                   GimmickDebugMessage(3,"='"<<mName<<"'"<<std::endl);
00115           }
00116       else
00117           {
00118                 GimmickMessage(1,"!! WARNING : tag '"<<mKey
00119                          <<"' is not in DICOM dictionnary ! "
00120                          <<"Considering it as a user attribute"
00121                          << std::endl);
00122                 mName = "UNKNOWN";
00123                 mGroup = mElement = 0;
00124                 }
00125 #endif
00126 
00127     }
00128     //=====================================================================
00129 
00130 
00131     //=====================================================================
00133     void AttributeDescriptor::GetDicomGroupElementFromKey(const std::string& key,
00134                                                           unsigned short& group,
00135                                                           unsigned short& elem)
00136     {
00137       group = elem = 0;
00138       if ( (key.size()==10) &&
00139            (key[0] == 'D') &&
00140            (key[5] == '_') )
00141           {
00142          sscanf(key.c_str(),"D%04hx_%04hx ",&group,&elem);  
00143           GimmickDebugMessage(3,"GetDicomGroupElementFromKey '"<<key<<"' : "                     <<group<<"|"<<elem<<std::endl);
00144         }
00145       else 
00146         { 
00147           GimmickMessage(5,"GetDicomGroupElementFromKey '"<<key<<"' : "
00148                          <<" not a DICOM key format"<<std::endl);
00149         }
00150       return;
00151     }
00152 
00153         //=====================================================================
00155         bool AttributeDescriptor::isDateEntry() const
00156         {
00157                  
00158                 bool btest = false;
00159                 // Retrieve the name from gdcm dict
00160 #if defined(USE_GDCM)
00161                 GDCM_NAME_SPACE::DictEntry* entry =     GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
00162                 if(     entry != 0)
00163                 {
00164                         if( entry->GetVR().str() == "DA" )
00165                         {
00166                                 btest = true;
00167                         }
00168                 }
00169 #endif
00170 #if defined(USE_GDCM2)
00171          const gdcm::Global& g = gdcm::Global::GetInstance(); 
00172          const gdcm::Dicts &dicts = g.GetDicts();
00173   const gdcm::Dict &dict = dicts.GetPublicDict();
00174           if(mGroup != 0 && mElement != 0)
00175           {
00176                   gdcm::DictEntry dictentry =  dict.GetDictEntry(gdcm::Tag(GetGroup(), GetElement()));
00177                 if( gdcm::VR::GetVRString(dictentry.GetVR()) == "DA")
00178                 {
00179                                 btest = true;
00180                 }
00181           }
00182 #endif
00183                 return btest;
00184         }
00185 
00186         //=====================================================================
00188         bool AttributeDescriptor::isTimeEntry() const
00189         {
00190                  
00191                 bool btest = false;
00192 #if defined(USE_GDCM)
00193                 // Retrieve the name from gdcm dict
00194                 GDCM_NAME_SPACE::DictEntry* entry =     GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
00195                 if(     entry != 0)
00196                 {
00197                         if( entry->GetVR().str() == "TM" )
00198                         {
00199                                 btest = true;
00200                         }
00201                 }
00202 #endif
00203 
00204 #if defined(USE_GDCM2)
00205          const gdcm::Global& g = gdcm::Global::GetInstance(); // sum of all knowledge !
00206          const gdcm::Dicts &dicts = g.GetDicts();
00207   const gdcm::Dict &dict = dicts.GetPublicDict(); // Part 6
00208           if(mGroup != 0 && mElement != 0)
00209           {
00210                 gdcm::DictEntry dictentry =  dict.GetDictEntry(gdcm::Tag(mGroup, mElement));
00211                 if(gdcm::VR::GetVRString(dictentry.GetVR()) == "TM")
00212                 {
00213                                 btest = true;
00214                 }
00215           }
00216 #endif
00217 
00218                 return btest;
00219         }
00220 
00221 
00222     //=====================================================================
00224      void AttributeDescriptor::DecodeType(unsigned int& typ) const
00225           {
00226                  std::string type=""; 
00227 #if defined(USE_GDCM)   
00228                   // Retrieve the name from gdcm dict
00229                 GDCM_NAME_SPACE::DictEntry* entry =
00230                 GDCM_NAME_SPACE::Global::GetDicts()
00231                 ->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
00232 
00233                 if (entry==0) 
00234                 {
00235                         typ = 2;
00236                         return;
00237                 }
00238                  type = entry->GetVR().str();
00239 #endif
00240 #if defined(USE_GDCM2)
00241          const gdcm::Global& g = gdcm::Global::GetInstance(); // sum of all knowledge !
00242          const gdcm::Dicts &dicts = g.GetDicts();
00243   const gdcm::Dict &dict = dicts.GetPublicDict(); // Part 6
00244           gdcm::DictEntry dictentry =  dict.GetDictEntry(gdcm::Tag(mGroup, mElement));
00245           type = gdcm::VR::GetVRString(dictentry.GetVR());
00246 #endif
00247 
00248                 GimmickDebugMessage(3,"VR Value is "<<type<<"!"<<std::endl);
00249                 if(type=="AS" ||
00250                 type=="DA" ||
00251                 type=="FL" ||
00252                 type=="FD" ||
00253                 type=="IS" ||
00254                 type=="SL" ||
00255                 type=="SS" ||
00256                 type=="UI" ||
00257                 type=="US" ||
00258                 type=="SH")
00259                 {
00260                         // Numerical 
00261                         typ = 1;
00262                 }
00263                 else
00264                 {
00265                         // String
00266                         typ = 2;
00267                 }
00268                 
00269           }
00270           //=====================================================================
00271 
00272   } // EO namespace tree
00273 
00274 } // EO namespace creaImageIO