From 805ec93a6b36ad6acf63458414c545a737322354 Mon Sep 17 00:00:00 2001 From: guigues Date: Mon, 23 Feb 2009 10:01:08 +0000 Subject: [PATCH] *** empty log message *** --- src2/CMakeLists.txt | 3 + src2/creaImageIODicomImageReader.cpp | 153 ++++++++++++ src2/creaImageIODicomImageReader.h | 43 ++++ src2/creaImageIOImageReader.cpp | 335 +------------------------- src2/creaImageIOSpecificImageReader.h | 48 ++++ src2/creaImageIOVtkImageReader.cpp | 159 ++++++++++++ src2/creaImageIOVtkImageReader.h | 47 ++++ 7 files changed, 464 insertions(+), 324 deletions(-) create mode 100644 src2/creaImageIODicomImageReader.cpp create mode 100644 src2/creaImageIODicomImageReader.h create mode 100644 src2/creaImageIOSpecificImageReader.h create mode 100644 src2/creaImageIOVtkImageReader.cpp create mode 100644 src2/creaImageIOVtkImageReader.h diff --git a/src2/CMakeLists.txt b/src2/CMakeLists.txt index afac46a..8a40709 100644 --- a/src2/CMakeLists.txt +++ b/src2/CMakeLists.txt @@ -17,6 +17,9 @@ SET( SRCS creaImageIOTreeComparators # Image readers + creaImageIOSpecificImageReader + creaImageIOVtkImageReader + creaImageIODicomImageReader creaImageIOImageReader creaImageIOMultiThreadImageReader diff --git a/src2/creaImageIODicomImageReader.cpp b/src2/creaImageIODicomImageReader.cpp new file mode 100644 index 0000000..95619e5 --- /dev/null +++ b/src2/creaImageIODicomImageReader.cpp @@ -0,0 +1,153 @@ +#include +#include +#include +#include +#include "boost/filesystem/path.hpp" + +#include + +namespace creaImageIO +{ + + //===================================================================== + DicomImageReader::DicomImageReader() + { + mReader = vtkGdcmReader::New(); + SetName ( "Dicom" ); + }; + //===================================================================== + + //===================================================================== + DicomImageReader::~DicomImageReader() + { + mReader->Delete(); + } + //===================================================================== + + //===================================================================== + bool DicomImageReader::CanRead(const std::string& filename) + { + // std::cout << "## Reader "<SetLoadMode( GDCM_NAME_SPACE::LD_ALL); + file->SetFileName(filename.c_str()); + file->Load(); + bool ok = file->IsReadable(); + file->Delete(); + return ok; + } + //===================================================================== + + //===================================================================== + vtkImageData* DicomImageReader::ReadImage(const std::string& filename) + { + // std::cout << "## Reader "<SetFileName(filename.c_str()); + mReader->Update(); + im = vtkImageData::New(); + im->ShallowCopy(mReader->GetOutput()); + } + catch (...) + { + if (im!=0) im->Delete(); + im = 0; + } + return im; + } + + //===================================================================== + void DicomImageReader::PushBackExtensions(std::vector& v) + { + v.push_back("dcm"); + v.push_back(""); + } + //===================================================================== + + //======================================================================== + std::string irclean(const std::string& str) + { + if (str == "GDCM::Unfound") + { + return ""; + } + if (str[str.size()-1]==' ') + { + return str.substr(0,str.size()-1); + } + if (str[str.size()-1]==0) + { + return str.substr(0,str.size()-1); + } + + return str; + } + //======================================================================== + + //===================================================================== + void DicomImageReader::ReadAttributes(const std::string& filename, + std::map& attr) + { + // std::cout << "DicomImageReader::ReadDicomInfo '"<SetLoadMode( GDCM_NAME_SPACE::LD_ALL); + file->SetFileName(filename.c_str()); + file->Load(); + if (file->IsReadable()) + { + + std::map::iterator i; + for (i=attr.begin();i!=attr.end();++i) + { + if ( i->first == "D0004_1500" ) + { + boost::filesystem::path full_path(filename); + std::string f = full_path.leaf(); + i->second = f; + } + else if ( i->first == "FullFileName" ) + { + i->second = filename; + } + else + { + uint16_t gr; + uint16_t el; + tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el); + // GimmickMessage(2,"Key '"<first<<"' : "<GetEntryString(gr,el); + i->second = irclean(val); + // GimmickMessage(2,"Key '"<first<<"' : "<& tokens) - { - // Skip delimiters at beginning. - std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); - // Find first delimiter. - std::string::size_type pos = str.find_first_of(delimiters, lastPos); - - while (std::string::npos != pos || std::string::npos != lastPos) - { - // Found a token, add it to the vector. - // SPECIFIC : REMOVE INITIAL DOT (lastPos + 1) - tokens.push_back(str.substr(lastPos+1, pos - lastPos)); - // Skip delimiters. Note the "not_of" - lastPos = str.find_first_not_of(delimiters, pos); - // Find next delimiter - pos = str.find_first_of(delimiters, lastPos); - } - - } - - - //===================================================================== - class SpecificImageReader - { - public: - SpecificImageReader() {} - virtual ~SpecificImageReader() {} - - void SetName(const std::string& s) { mName = s; } - const std::string& GetName() const { return mName; } - - virtual void PushBackExtensions(std::vector&) {} - - virtual bool CanRead(const std::string& filename) { return false; } - virtual vtkImageData* ReadImage(const std::string& filename) { return 0; } - virtual void ReadAttributes(const std::string& filename, - std::map& attr) {} - - private: - std::string mName; - }; - //===================================================================== - - //===================================================================== - class SpecificVtkReader : public SpecificImageReader - { - public: - //===================================================================== - SpecificVtkReader(vtkImageReader2* r, - const std::string& name = "", - const std::string& extensions = "") - : mVTKReader(r), mExtensions(extensions) - { - if (name.size() == 0) - { - SetName ( mVTKReader->GetDescriptiveName() ); - } - else - { - SetName ( name ); - } - }; - //===================================================================== - ~SpecificVtkReader() - { - mVTKReader->Delete(); - } - //===================================================================== - bool CanRead(const std::string& filename) - { - // std::cout << "## Reader "<CanReadFile(filename.c_str())!=0); - } - //===================================================================== - - //===================================================================== - vtkImageData* ReadImage(const std::string& filename) - { - // std::cout << "## Reader "<SetFileName(filename.c_str()); - mVTKReader->Update(); - im = vtkImageData::New(); - im->ShallowCopy(mVTKReader->GetOutput()); - } - catch (...) - { - if (im!=0) im->Delete(); - im = 0; - } - return im; - } - //===================================================================== - - //===================================================================== - void PushBackExtensions(std::vector& v) - { - std::string ext = mExtensions; - if (ext.size()==0) ext = mVTKReader->GetFileExtensions (); - - IRSplitString(ext," ",v); - } - //===================================================================== - - //===================================================================== - void ReadAttributes(const std::string& filename, - std::map& attr) - { - // std::cout << "SpecificVtkReader::ReadDicomInfo '"<SetFileName(filename.c_str()); - mVTKReader->Update(); //OpenFile(); - int ext[6]; - mVTKReader->GetDataExtent(ext); - // Columns - char cols[128]; - sprintf(cols,"%i",ext[1]-ext[0]); - // Rows - char rows[128]; - sprintf(rows,"%i",ext[3]-ext[2]); - // Planes - char planes[128]; - sprintf(planes,"%i",ext[5]-ext[4]); - - // - std::map::iterator i; - if ( (i = attr.find("FullFileName")) != attr.end()) - { - // boost::filesystem::path full_path(filename); - // std::string f = full_path.leaf(); - i->second = filename; - } - if ( (i = attr.find("D0004_1500")) != attr.end()) - { - boost::filesystem::path full_path(filename); - std::string f = full_path.leaf(); - i->second = f; - } - if ( (i = attr.find("D0028_0010")) != attr.end()) - { - i->second = rows; - } - if ( (i = attr.find("D0028_0011")) != attr.end()) - { - i->second = cols; - } - - if ( (i = attr.find("D0028_0012")) != attr.end()) - { - i->second = planes; - } - GimmickMessage(2,"Attributes map:"<Delete(); - } - //===================================================================== - bool CanRead(const std::string& filename) - { - // std::cout << "## Reader "<SetLoadMode( GDCM_NAME_SPACE::LD_ALL); - file->SetFileName(filename.c_str()); - file->Load(); - bool ok = file->IsReadable(); - file->Delete(); - return ok; - } - //===================================================================== - vtkImageData* ReadImage(const std::string& filename) - { - // std::cout << "## Reader "<SetFileName(filename.c_str()); - mReader->Update(); - im = vtkImageData::New(); - im->ShallowCopy(mReader->GetOutput()); - } - catch (...) - { - if (im!=0) im->Delete(); - im = 0; - } - return im; - } - //===================================================================== - void PushBackExtensions(std::vector& v) - { - v.push_back("dcm"); - v.push_back(""); - } - //===================================================================== - - //===================================================================== - //===================================================================== - void ReadAttributes(const std::string& filename, - std::map& attr) - { - // std::cout << "DicomReader::ReadDicomInfo '"<SetLoadMode( GDCM_NAME_SPACE::LD_ALL); - file->SetFileName(filename.c_str()); - file->Load(); - if (file->IsReadable()) - { - - std::map::iterator i; - for (i=attr.begin();i!=attr.end();++i) - { - if ( i->first == "D0004_1500" ) - { - boost::filesystem::path full_path(filename); - std::string f = full_path.leaf(); - i->second = f; - } - else if ( i->first == "FullFileName" ) - { - i->second = filename; - } - else - { - uint16_t gr; - uint16_t el; - tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el); - // GimmickMessage(2,"Key '"<first<<"' : "<GetEntryString(gr,el); - i->second = irclean(val); - // GimmickMessage(2,"Key '"<first<<"' : "< +#include +#include +#include +#include + +namespace creaImageIO +{ + + + /** + * \ingroup ReadWrite + */ + + //===================================================================== + /// Abstract specific image reader + class SpecificImageReader + { + public: + SpecificImageReader() {} + virtual ~SpecificImageReader() {} + + const std::string& GetName() const { return mName; } + + virtual void PushBackExtensions(std::vector&) {} + virtual bool CanRead(const std::string& filename) { return false; } + virtual vtkImageData* ReadImage(const std::string& filename) { return 0; } + virtual void ReadAttributes(const std::string& filename, + tree::AttributeMapType& attr) {} + + protected: + void SetName(const std::string& s) { mName = s; } + private: + std::string mName; + }; + //===================================================================== + + + +} // namespace creaImageIO + + + +#endif // #ifndef __creaImageIOSpecificImageReader_h_INCLUDED__ diff --git a/src2/creaImageIOVtkImageReader.cpp b/src2/creaImageIOVtkImageReader.cpp new file mode 100644 index 0000000..90a83eb --- /dev/null +++ b/src2/creaImageIOVtkImageReader.cpp @@ -0,0 +1,159 @@ +#include +#include +#include +#include "boost/filesystem/path.hpp" + +namespace creaImageIO +{ + + //===================================================================== + VtkImageReader::VtkImageReader(vtkImageReader2* r, + const std::string& name, + const std::string& extensions) + : mReader(r), mExtensions(extensions) + { + if (name.size() == 0) + { + SetName ( mReader->GetDescriptiveName() ); + } + else + { + SetName ( name ); + } + + + } + //===================================================================== + + //===================================================================== + VtkImageReader::~VtkImageReader() + { + mReader->Delete(); + } + //===================================================================== + + //===================================================================== + bool VtkImageReader::CanRead(const std::string& filename) + { + // std::cout << "## Reader "<CanReadFile(filename.c_str())!=0); + } + //===================================================================== + + //===================================================================== + vtkImageData* VtkImageReader::ReadImage(const std::string& filename) + { + // std::cout << "## Reader "<SetFileName(filename.c_str()); + mReader->Update(); + im = vtkImageData::New(); + im->ShallowCopy(mReader->GetOutput()); + } + catch (...) + { + if (im!=0) im->Delete(); + im = 0; + } + return im; + } + //===================================================================== + + //===================================================================== + void SplitExtensionsString ( const std::string& str, + const std::string& delimiters, + std::vector& tokens) + { + // Skip delimiters at beginning. + std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); + // Find first delimiter. + std::string::size_type pos = str.find_first_of(delimiters, lastPos); + + while (std::string::npos != pos || std::string::npos != lastPos) + { + // Found a token, add it to the vector. + // SPECIFIC : REMOVE INITIAL DOT (lastPos + 1) + tokens.push_back(str.substr(lastPos+1, pos - lastPos)); + // Skip delimiters. Note the "not_of" + lastPos = str.find_first_not_of(delimiters, pos); + // Find next delimiter + pos = str.find_first_of(delimiters, lastPos); + } + + } + //===================================================================== + + //===================================================================== + void VtkImageReader::PushBackExtensions(std::vector& v) + { + std::string ext = mExtensions; + if (ext.size()==0) ext = mReader->GetFileExtensions (); + + SplitExtensionsString(ext," ",v); + } + //===================================================================== + + + + //===================================================================== + void VtkImageReader::ReadAttributes(const std::string& filename, + std::map& attr) + { + // std::cout << "VtkImageReader::ReadDicomInfo '"<SetFileName(filename.c_str()); + mReader->Update(); //OpenFile(); + int ext[6]; + mReader->GetDataExtent(ext); + // Columns + char cols[128]; + sprintf(cols,"%i",ext[1]-ext[0]); + // Rows + char rows[128]; + sprintf(rows,"%i",ext[3]-ext[2]); + // Planes + char planes[128]; + sprintf(planes,"%i",ext[5]-ext[4]); + + + // + std::map::iterator i; + if ( (i = attr.find("FullFileName")) != attr.end()) + { + // boost::filesystem::path full_path(filename); + // std::string f = full_path.leaf(); + i->second = filename; + } + if ( (i = attr.find("D0004_1500")) != attr.end()) + { + boost::filesystem::path full_path(filename); + std::string f = full_path.leaf(); + i->second = f; + } + if ( (i = attr.find("D0028_0010")) != attr.end()) + { + i->second = rows; + } + if ( (i = attr.find("D0028_0011")) != attr.end()) + { + i->second = cols; + } + + if ( (i = attr.find("D0028_0012")) != attr.end()) + { + i->second = planes; + } + + GimmickMessage(2,"Attributes map:"< + +// forward decl +class vtkImageReader2; + +namespace creaImageIO +{ + + + /** + * \ingroup ReadWrite + */ + + //===================================================================== + /// Specific image reader based on a vtkImageReader2 + class VtkImageReader : virtual public SpecificImageReader + { + public: + VtkImageReader(vtkImageReader2* reader, + const std::string& name = "", + const std::string& extensions = ""); + + virtual ~VtkImageReader(); + + virtual void PushBackExtensions(std::vector&); + virtual bool CanRead(const std::string& filename); + virtual vtkImageData* ReadImage(const std::string& filename); + virtual void ReadAttributes(const std::string& filename, + tree::AttributeMapType& attr); + + private: + vtkImageReader2* mReader; + std::string mExtensions; + }; + //===================================================================== + + + +} // namespace creaImageIO + + + +#endif // #ifndef __creaImageIOVtkImageReader_h_INCLUDED__ -- 2.45.1