]> Creatis software - clitk.git/blob - segmentation/clitkAnatomicalFeatureDatabase.txx
Merge branch 'master' of git.creatis.insa-lyon.fr:clitk
[clitk.git] / segmentation / clitkAnatomicalFeatureDatabase.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ===========================================================================**/
18
19
20 //--------------------------------------------------------------------
21 template<class ImageType>
22 typename ImageType::Pointer AnatomicalFeatureDatabase::
23 GetImage(std::string tag, bool reload)
24 {
25   if (m_MapOfTag.find(tag) == m_MapOfTag.end()) {
26     clitkExceptionMacro("Could not find the tag <" << tag << "> of type 'Image' in the DB ('"
27                         << GetFilename() << "')");
28   }
29   else {
30     typename ImageType::Pointer image;
31     if ((!reload) && (m_MapOfImage[tag])) {
32       image = static_cast<ImageType *>(m_MapOfImage[tag]);
33     }
34     else {
35       std::string s = m_MapOfTag[tag];
36       // Read the file
37       image = readImage<ImageType>(GetPath()+"/"+s);
38       // I add a reference count because the cache is not a smartpointer
39       image->SetReferenceCount(image->GetReferenceCount()+1);
40       // Insert into the cache
41       m_MapOfImage.erase(tag); 
42       m_MapOfImage[tag] = &(*image); // pointer
43     }
44     return image;
45   }
46 }
47 //--------------------------------------------------------------------
48
49
50 //--------------------------------------------------------------------
51 template<class ImageType>
52 bool AnatomicalFeatureDatabase::
53 CheckImage(std::string tag)
54 {
55   if (m_MapOfTag.find(tag) == m_MapOfTag.end()) {
56     return false;
57   }
58   else {
59     typename ImageType::Pointer image;
60     if (m_MapOfImage[tag]) {
61       image = static_cast<ImageType *>(m_MapOfImage[tag]);
62     }
63     else {
64       std::string s = m_MapOfTag[tag];
65       // Read the file
66       std::string n = GetPath()+"/"+s;
67       //      image = readImage<ImageType>();
68       typedef itk::ImageFileReader<ImageType> ReaderType;
69       typename ReaderType::Pointer reader = ReaderType::New();
70       reader->SetFileName(n.c_str());
71       try {
72         reader->Update();
73       } catch(itk::ExceptionObject & err) {
74         return false;
75       }
76     }
77   }
78   return true;
79 }
80 //--------------------------------------------------------------------
81
82
83 //--------------------------------------------------------------------
84 template<class ImageType>
85 void AnatomicalFeatureDatabase::
86 SetImage(TagType tag, std::string f, typename ImageType::Pointer image, bool write)
87 {
88   SetImageFilename(tag, f);
89   m_MapOfImage[tag] = &(*image);
90   // I add a reference count because the cache is not a smartpointer
91   image->SetReferenceCount(image->GetReferenceCount()+1);
92   if (write) {
93     writeImage<ImageType>(image, f);
94   }
95 }
96 //--------------------------------------------------------------------
97
98
99 //--------------------------------------------------------------------
100 template<class ImageType>
101 void AnatomicalFeatureDatabase::
102 ReleaseImage(std::string tag)
103 {
104   if (m_MapOfTag.find(tag) == m_MapOfTag.end()) {
105     clitkExceptionMacro("Could not find the tag <" << tag << "> of type Image Filename in the DB");
106   }
107   else {
108     typename ImageType::Pointer image = GetImage<ImageType>(tag);
109     image->SetReferenceCount(image->GetReferenceCount()-1);
110     m_MapOfImage.erase(tag);
111   }
112 }
113 //--------------------------------------------------------------------