]> Creatis software - clitk.git/blob - segmentation/clitkAnatomicalFeatureDatabase.txx
Remove transparency for screenshot
[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       if (s[0] != '/')
38         image = readImage<ImageType>(GetPath() + "/" + s);
39       else
40         image = readImage<ImageType>(s);
41       // I add a reference count because the cache is not a smartpointer
42       image->SetReferenceCount(image->GetReferenceCount()+1);
43       // Insert into the cache
44       m_MapOfImage.erase(tag); 
45       m_MapOfImage[tag] = &(*image); // pointer
46     }
47     return image;
48   }
49 }
50 //--------------------------------------------------------------------
51
52
53 //--------------------------------------------------------------------
54 template<class ImageType>
55 bool AnatomicalFeatureDatabase::
56 CheckImage(std::string tag)
57 {
58   if (m_MapOfTag.find(tag) == m_MapOfTag.end()) {
59     return false;
60   }
61   else {
62     typename ImageType::Pointer image;
63     if (m_MapOfImage[tag]) {
64       image = static_cast<ImageType *>(m_MapOfImage[tag]);
65     }
66     else {
67       std::string s = m_MapOfTag[tag];
68       // Read the file
69       std::string n = GetPath()+"/"+s;
70       //      image = readImage<ImageType>();
71       typedef itk::ImageFileReader<ImageType> ReaderType;
72       typename ReaderType::Pointer reader = ReaderType::New();
73       reader->SetFileName(n.c_str());
74       try {
75         reader->Update();
76       } catch(itk::ExceptionObject & err) {
77         return false;
78       }
79     }
80   }
81   return true;
82 }
83 //--------------------------------------------------------------------
84
85
86 //--------------------------------------------------------------------
87 template<class ImageType>
88 void AnatomicalFeatureDatabase::
89 SetImage(TagType tag, std::string f, typename ImageType::Pointer image, bool write)
90 {
91   SetImageFilename(tag, f);
92   m_MapOfImage[tag] = &(*image);
93   // I add a reference count because the cache is not a smartpointer
94   image->SetReferenceCount(image->GetReferenceCount()+1);
95   if (write) {
96     writeImage<ImageType>(image, f);
97   }
98 }
99 //--------------------------------------------------------------------
100
101
102 //--------------------------------------------------------------------
103 template<class ImageType>
104 void AnatomicalFeatureDatabase::
105 ReleaseImage(std::string tag)
106 {
107   if (m_MapOfTag.find(tag) == m_MapOfTag.end()) {
108     clitkExceptionMacro("Could not find the tag <" << tag << "> of type Image Filename in the DB");
109   }
110   else {
111     typename ImageType::Pointer image = GetImage<ImageType>(tag);
112     image->SetReferenceCount(image->GetReferenceCount()-1);
113     m_MapOfImage.erase(tag);
114   }
115 }
116 //--------------------------------------------------------------------