]> Creatis software - clitk.git/blobdiff - segmentation/clitkAnatomicalFeatureDatabase.txx
Debug RTStruct conversion with empty struc
[clitk.git] / segmentation / clitkAnatomicalFeatureDatabase.txx
index f1ee25ebc8b863d855e4ecb052aaa77e02a1ef27..dc7c39a6652d9672e44543e317ec22860e1afd3d 100644 (file)
@@ -3,7 +3,7 @@
 
   Authors belong to: 
   - University of LYON              http://www.universite-lyon.fr/
-  - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
 
   This software is distributed WITHOUT ANY WARRANTY; without even
 
   - BSD        See included LICENSE.txt file
   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
-  ======================================================================-====*/
+  ===========================================================================**/
 
 
 //--------------------------------------------------------------------
 template<class ImageType>
 typename ImageType::Pointer AnatomicalFeatureDatabase::
-GetImage(std::string tag)
+GetImage(std::string tag, bool reload)
 {
   if (m_MapOfTag.find(tag) == m_MapOfTag.end()) {
     clitkExceptionMacro("Could not find the tag <" << tag << "> of type 'Image' in the DB ('"
@@ -28,16 +28,20 @@ GetImage(std::string tag)
   }
   else {
     typename ImageType::Pointer image;
-    if (m_MapOfImage[tag]) {
+    if ((!reload) && (m_MapOfImage[tag])) {
       image = static_cast<ImageType *>(m_MapOfImage[tag]);
     }
     else {
       std::string s = m_MapOfTag[tag];
       // Read the file
-      image = readImage<ImageType>(s);
+      if (s[0] != '/')
+        image = readImage<ImageType>(GetPath() + "/" + s);
+      else
+        image = readImage<ImageType>(s);
       // I add a reference count because the cache is not a smartpointer
       image->SetReferenceCount(image->GetReferenceCount()+1);
-      // Insert into the cache 
+      // Insert into the cache
+      m_MapOfImage.erase(tag); 
       m_MapOfImage[tag] = &(*image); // pointer
     }
     return image;
@@ -46,6 +50,39 @@ GetImage(std::string tag)
 //--------------------------------------------------------------------
 
 
+//--------------------------------------------------------------------
+template<class ImageType>
+bool AnatomicalFeatureDatabase::
+CheckImage(std::string tag)
+{
+  if (m_MapOfTag.find(tag) == m_MapOfTag.end()) {
+    return false;
+  }
+  else {
+    typename ImageType::Pointer image;
+    if (m_MapOfImage[tag]) {
+      image = static_cast<ImageType *>(m_MapOfImage[tag]);
+    }
+    else {
+      std::string s = m_MapOfTag[tag];
+      // Read the file
+      std::string n = GetPath()+"/"+s;
+      //      image = readImage<ImageType>();
+      typedef itk::ImageFileReader<ImageType> ReaderType;
+      typename ReaderType::Pointer reader = ReaderType::New();
+      reader->SetFileName(n.c_str());
+      try {
+        reader->Update();
+      } catch(itk::ExceptionObject & err) {
+        return false;
+      }
+    }
+  }
+  return true;
+}
+//--------------------------------------------------------------------
+
+
 //--------------------------------------------------------------------
 template<class ImageType>
 void AnatomicalFeatureDatabase::
@@ -53,6 +90,8 @@ SetImage(TagType tag, std::string f, typename ImageType::Pointer image, bool wri
 {
   SetImageFilename(tag, f);
   m_MapOfImage[tag] = &(*image);
+  // I add a reference count because the cache is not a smartpointer
+  image->SetReferenceCount(image->GetReferenceCount()+1);
   if (write) {
     writeImage<ImageType>(image, f);
   }
@@ -69,22 +108,9 @@ ReleaseImage(std::string tag)
     clitkExceptionMacro("Could not find the tag <" << tag << "> of type Image Filename in the DB");
   }
   else {
-    DD("TODO");
-    exit(0);
-     if (m_MapOfImage[tag]) {
-      DD(m_MapOfImage[tag]->GetReferenceCount());
-      ImageType * image = static_cast<ImageType*>(m_MapOfImage[tag]);
-      image->SetReferenceCount(image->GetReferenceCount()-1);
-      m_MapOfImage.erase(tag);
-      /*
-      DD(image->GetReferenceCount());
-      image->Delete();
-      */
-      //      DD(image->GetReferenceCount());
-    }
-    else {
-      // Do nothing in this case (image not loaded)
-    }
+    typename ImageType::Pointer image = GetImage<ImageType>(tag);
+    image->SetReferenceCount(image->GetReferenceCount()-1);
+    m_MapOfImage.erase(tag);
   }
 }
 //--------------------------------------------------------------------