1 #include <creaImageIOVtkImageReader.h>
2 #include <vtkImageReader2.h>
3 #include <creaImageIOSystem.h>
4 #include "boost/filesystem/path.hpp"
5 #include <creaImageIOGimmick.h>
12 //=====================================================================
13 VtkImageReader::VtkImageReader(vtkImageReader2* r,
14 const std::string& name,
15 const std::string& extensions)
16 : mReader(r), mExtensions(extensions)
20 const char *test =mReader->GetDescriptiveName();
23 SetName ( "toto");// mReader->GetDescriptiveName());
31 GimmickDebugMessage(5,"Constructing vtkImageReader : "<<GetName()
35 //=====================================================================
37 //=====================================================================
38 VtkImageReader::~VtkImageReader()
40 GimmickDebugMessage(5,"Destructing vtkImageReader : "<<GetName()
44 //=====================================================================
46 //=====================================================================
47 bool VtkImageReader::CanRead(const std::string& filename)
50 return (mReader->CanReadFile(filename.c_str())!=0);
53 return (mReader->CanReadFile(filename.c_str())!=0);
60 //=====================================================================
62 //=====================================================================
63 vtkImageData* VtkImageReader::ReadImage(const std::string& filename)
68 mReader->SetFileName(filename.c_str());
70 im = vtkImageData::New();
71 im->ShallowCopy(mReader->GetOutput());
75 if (im!=0) im->Delete();
80 //=====================================================================
82 //=====================================================================
83 void SplitExtensionsString ( const std::string& str,
84 const std::string& delimiters,
85 std::vector<std::string>& tokens)
87 // Skip delimiters at beginning.
88 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
89 // Find first delimiter.
90 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
92 while (std::string::npos != pos || std::string::npos != lastPos)
94 // Found a token, add it to the vector.
95 // SPECIFIC : REMOVE INITIAL DOT (lastPos + 1)
96 tokens.push_back(str.substr(lastPos+1, pos - lastPos));
97 // Skip delimiters. Note the "not_of"
98 lastPos = str.find_first_not_of(delimiters, pos);
99 // Find next delimiter
100 pos = str.find_first_of(delimiters, lastPos);
104 //=====================================================================
106 //=====================================================================
107 void VtkImageReader::PushBackExtensions(std::vector<std::string>& v)
109 std::string ext = mExtensions;
110 if (ext.size()==0) ext = mReader->GetFileExtensions ();
112 SplitExtensionsString(ext," ",v);
114 //=====================================================================
118 //=====================================================================
119 void VtkImageReader::ReadAttributes(const std::string& filename,
120 std::map<std::string,std::string>& attr)
122 GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
123 // Get image dimensions
124 // How to get the image info without loading it in vtk ?
125 mReader->SetFileName(filename.c_str());
126 mReader->Update(); //OpenFile();
128 mReader->GetDataExtent(ext);
131 sprintf(cols,"%i",ext[1]-ext[0]);
134 sprintf(rows,"%i",ext[3]-ext[2]);
137 sprintf(planes,"%i",ext[5]-ext[4]);
139 std::map<std::string,std::string>::iterator i;
140 if ( (i = attr.find("FullFileName")) != attr.end())
142 i->second = filename;
144 if ( (i = attr.find("D0004_1500")) != attr.end())
146 boost::filesystem::path full_path(filename);
147 std::string f = full_path.leaf();
150 if ( (i = attr.find("D0028_0010")) != attr.end())
154 if ( (i = attr.find("D0028_0011")) != attr.end())
159 if ( (i = attr.find("D0028_0012")) != attr.end())
163 if ( (i = attr.find("FullFileDirectory")) != attr.end())
165 std::string::size_type last_pos = filename.find_last_of("//");
166 i->second = filename.substr(0, last_pos);
169 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
171 //=====================================================================
173 } // namespace creaImageIO