From 3c1886a2789de650bebcbcbc60e454b248f83e38 Mon Sep 17 00:00:00 2001 From: dsarrut Date: Tue, 15 Feb 2011 11:30:53 +0000 Subject: [PATCH] add cache for read image --- .../clitkAnatomicalFeatureDatabase.cxx | 25 ++++++-- segmentation/clitkAnatomicalFeatureDatabase.h | 10 +++ .../clitkAnatomicalFeatureDatabase.txx | 63 +++++++++++++++++-- 3 files changed, 88 insertions(+), 10 deletions(-) diff --git a/segmentation/clitkAnatomicalFeatureDatabase.cxx b/segmentation/clitkAnatomicalFeatureDatabase.cxx index 539e170..8bf17fb 100644 --- a/segmentation/clitkAnatomicalFeatureDatabase.cxx +++ b/segmentation/clitkAnatomicalFeatureDatabase.cxx @@ -28,7 +28,7 @@ //-------------------------------------------------------------------- clitk::AnatomicalFeatureDatabase::AnatomicalFeatureDatabase() { - SetFilename("noname.afdb"); + SetFilename("default.afdb"); } //-------------------------------------------------------------------- @@ -71,6 +71,7 @@ static inline std::string &trim(std::string &s) { //-------------------------------------------------------------------- void clitk::AnatomicalFeatureDatabase::Load() { + m_MapOfTag.clear(); // open file std::ifstream is; openFileForReading(is, GetFilename()); @@ -78,11 +79,14 @@ void clitk::AnatomicalFeatureDatabase::Load() while (!is.fail()) { std::string tag; is >> tag; - std::string value; - std::getline(is,value,'\n'); - ltrim(value); // remove leading space - m_MapOfTag[tag] = value; + if (tag != "") { + std::string value; + std::getline(is,value,'\n'); + ltrim(value); // remove leading space + m_MapOfTag[tag] = value; + } } + is.close(); } //-------------------------------------------------------------------- @@ -106,6 +110,7 @@ double clitk::AnatomicalFeatureDatabase::GetPoint3D(std::string tag, int dim) } //-------------------------------------------------------------------- + //-------------------------------------------------------------------- void clitk::AnatomicalFeatureDatabase::GetPoint3D(std::string tag, PointType3D & p) { @@ -126,7 +131,13 @@ void clitk::AnatomicalFeatureDatabase::GetPoint3D(std::string tag, PointType3D & // parse the string into 3 doubles for(int i=0; i<3; i++) { - p[i] = atof(results[i].c_str()); + + if (!clitk::fromString(p[i], results[i].c_str())) { + clitkExceptionMacro("Error while reading Point3D, could not convert '" + << results[i].c_str() << "' into double."); + } + + // p[i] = atof(results[i].c_str()); } /* @@ -155,3 +166,5 @@ void clitk::AnatomicalFeatureDatabase::SetImageFilename(std::string tag, std::st } //-------------------------------------------------------------------- + + diff --git a/segmentation/clitkAnatomicalFeatureDatabase.h b/segmentation/clitkAnatomicalFeatureDatabase.h index f1324cb..e2850e1 100644 --- a/segmentation/clitkAnatomicalFeatureDatabase.h +++ b/segmentation/clitkAnatomicalFeatureDatabase.h @@ -56,6 +56,13 @@ namespace clitk { void SetImageFilename(TagType tag, std::string f); template typename ImageType::Pointer GetImage(TagType tag); + template + void SetImage(TagType tag, + std::string f, + typename ImageType::Pointer image, + bool write=false); + template + void ReleaseImage(TagType tag); // Set Get Double void SetDouble(TagType tag, double d); @@ -63,8 +70,11 @@ namespace clitk { protected: std::string m_Filename; + typedef itk::ImageBase<3> ImageBaseType; typedef std::map MapTagType; + typedef std::map MapTagImageType; MapTagType m_MapOfTag; + MapTagImageType m_MapOfImage; }; // end class //-------------------------------------------------------------------- diff --git a/segmentation/clitkAnatomicalFeatureDatabase.txx b/segmentation/clitkAnatomicalFeatureDatabase.txx index ae4567b..f1ee25e 100644 --- a/segmentation/clitkAnatomicalFeatureDatabase.txx +++ b/segmentation/clitkAnatomicalFeatureDatabase.txx @@ -23,13 +23,68 @@ typename ImageType::Pointer AnatomicalFeatureDatabase:: GetImage(std::string tag) { if (m_MapOfTag.find(tag) == m_MapOfTag.end()) { - clitkExceptionMacro("Could not find the tag <" << tag << "> of type Image Filename in the DB"); + clitkExceptionMacro("Could not find the tag <" << tag << "> of type 'Image' in the DB ('" + << GetFilename() << "')"); } else { - std::string s = m_MapOfTag[tag]; - // Read the file - typename ImageType::Pointer image = readImage(s); + typename ImageType::Pointer image; + if (m_MapOfImage[tag]) { + image = static_cast(m_MapOfImage[tag]); + } + else { + std::string s = m_MapOfTag[tag]; + // Read the file + image = readImage(s); + // I add a reference count because the cache is not a smartpointer + image->SetReferenceCount(image->GetReferenceCount()+1); + // Insert into the cache + m_MapOfImage[tag] = &(*image); // pointer + } return image; } } //-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void AnatomicalFeatureDatabase:: +SetImage(TagType tag, std::string f, typename ImageType::Pointer image, bool write) +{ + SetImageFilename(tag, f); + m_MapOfImage[tag] = &(*image); + if (write) { + writeImage(image, f); + } +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void AnatomicalFeatureDatabase:: +ReleaseImage(std::string tag) +{ + if (m_MapOfTag.find(tag) == m_MapOfTag.end()) { + 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(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) + } + } +} +//-------------------------------------------------------------------- -- 2.47.1