- //========================================================================
- 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<std::string>& 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<std::string>&) {}
-
- 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<std::string,std::string>& 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 "<<GetName()
- //<<" ::CanRead("<<filename<<")"
- // <<std::endl;
- return (mVTKReader->CanReadFile(filename.c_str())!=0);
- }
- //=====================================================================
-
- //=====================================================================
- vtkImageData* ReadImage(const std::string& filename)
- {
- // std::cout << "## Reader "<<GetName()
- //<<" ::Read("<<filename<<")"
- // <<std::endl;
- vtkImageData* im = 0;
- try
- {
- mVTKReader->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<std::string>& v)
- {
- std::string ext = mExtensions;
- if (ext.size()==0) ext = mVTKReader->GetFileExtensions ();
-
- IRSplitString(ext," ",v);
- }
- //=====================================================================
-
- //=====================================================================
- void ReadAttributes(const std::string& filename,
- std::map<std::string,std::string>& attr)
- {
- // std::cout << "SpecificVtkReader::ReadDicomInfo '"<<filename<<"'"<<std::endl;
- GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
- // Get image dimensions
- // How to get the image info without loading it in vtk ?
- mVTKReader->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]);
-