X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=src2%2FcreaImageIOImageReader.cpp;h=b1f9279e47a7412c525c21ba847a8abc6698ba3e;hb=96ca5a0d7b7ccd6b4291160c19afd48e3207db2a;hp=2480a47d921ebe5a19747a0b3c68bcf3c04ccadb;hpb=cbf693fa62cd51f4ca5c881838bbb609edc447b0;p=creaImageIO.git diff --git a/src2/creaImageIOImageReader.cpp b/src2/creaImageIOImageReader.cpp index 2480a47..b1f9279 100644 --- a/src2/creaImageIOImageReader.cpp +++ b/src2/creaImageIOImageReader.cpp @@ -1,8 +1,11 @@ #include #include +#include +#include +#include +#include -#include #include #include #include @@ -11,8 +14,6 @@ #include //#include -#include -#include #include "boost/filesystem/path.hpp" @@ -20,331 +21,6 @@ namespace creaImageIO { - //======================================================================== - 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 IRSplitString ( 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); - } - - } - - - //===================================================================== - 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("Full File Name")) != attr.end()) - { - boost::filesystem::path full_path(filename); - std::string f = full_path.leaf(); - i->second = f; - } - if ( (i = attr.find("Columns")) != attr.end()) - { - i->second = cols; - } - if ( (i = attr.find("Rows")) != attr.end()) - { - i->second = rows; - } - if ( (i = attr.find("Planes")) != attr.end()) - { - i->second = planes; - } - } - //===================================================================== - private: - vtkImageReader2* mVTKReader; - std::string mExtensions; - }; - //===================================================================== - - //===================================================================== - /* - void IRFillFields(DicomNode* node, - GDCM_NAME_SPACE::File* gdcmFile) - { - const DicomNodeTypeDescription::FieldDescriptionMapType& dm - = node->GetTypeDescription().GetFieldDescriptionMap(); - DicomNodeTypeDescription::FieldDescriptionMapType::const_iterator i; - - - DicomNode::FieldValueMapType& vm = node->GetFieldValueMap(); - for (i=dm.begin(); i!=dm.end(); ++i) - { - if ( (i->second.flags==0) && - (i->second.group!=0) && - (i->second.element!=0) ) - { - uint16_t gr = i->second.group; - uint16_t el = i->second.element; - - std::string val = gdcmFile->GetEntryString(gr,el); - - vm[i->first] = irclean(val); - } - else - { - vm[i->first] = ""; - } - } - } - */ - //===================================================================== - - //===================================================================== - class DicomReader : public SpecificImageReader - { - public: - //===================================================================== - DicomReader() - { - mReader = vtkGdcmReader::New(); - SetName ( "Dicom" ); - }; - //===================================================================== - ~DicomReader() - { - mReader->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 == "Full File Name" ) - { - boost::filesystem::path full_path(filename); - std::string f = full_path.leaf(); - i->second = f; - } - else - { - tree::AttributeDescriptor a(i->first); - uint16_t gr = a.GetGroup(); - uint16_t el = a.GetElement(); - if ( ( gr!=0 ) && ( el!=0 ) ) - { - std::string val = file->GetEntryString(gr,el); - i->second = irclean(val); - } - } - } - } - file->Delete(); - } - - - private: - vtkGdcmReader* mReader; - }; - //===================================================================== - @@ -360,26 +36,17 @@ namespace creaImageIO // std::cout << "#### ImageReader::ImageReader()"<::const_iterator i; - for (i=GetKnownExtensions().begin(); - i!=GetKnownExtensions().end(); - i++) - { - std::cout << "'"<<(*i)<<"'"<::iterator i; + std::vector::iterator i; for (i=mReader.begin(); i!=mReader.end(); i++) { - // std::cout << "#### ImageReader::UnRegister(" - // << (*i)->GetName()<<")"<GetName()<<")"<PushBackExtensions(mKnownExtensions); + } //===================================================================== //===================================================================== // Returns true iff the file is readable - bool ImageReader::CanRead( const std::string& filename, - const std::string& exclude ) + bool ImageReader::CanRead( const std::string& filename ) { - // std::cout << "## ImageReader::CanRead("<::iterator i; + std::vector::iterator i; for (i=mReader.begin(); i!=mReader.end(); i++) { - if ((*i)->GetName()==exclude) continue; ok = (*i)->CanRead(filename); if (ok) { @@ -455,29 +115,23 @@ namespace creaImageIO //===================================================================== // Reads the file (CanRead must be called before : no test here) - vtkImageData* ImageReader::ReadImage( const std::string& filename, - const std::string& exclude ) + vtkImageData* ImageReader::ReadImage( const std::string& filename) { - // std::cout << "## ImageReader::Read("<ShallowCopy(mUnreadableImage); return im; } } vtkImageData* i = mLastReader->ReadImage(mLastFilename); - // std::cout << "i="<ShallowCopy(mUnreadableImage); } - // std::cout << "i="<& attr) { - // std::cout << "ImageReader::ReadDicomInfo '"<& v) + { + std::vector::iterator i; + for (i=mReader.begin(); i!=mReader.end(); i++) + { + (*i)->PushBackExtensions(v); + } + } + //===================================================================== + } // namespace creaImageIO