1 #include <creaImageIOVtkImageReader.h>
2 #include <vtkImageReader2.h>
3 #include <creaImageIOSystem.h>
4 #include "boost/filesystem/path.hpp"
7 //=====================================================================
8 VtkImageReader::VtkImageReader(vtkImageReader2* r,
9 const std::string& name,
10 const std::string& extensions)
11 : mReader(r), mExtensions(extensions)
15 const char *test =mReader->GetDescriptiveName();
18 SetName ( "toto");// mReader->GetDescriptiveName());
26 GimmickDebugMessage(5,"Constructing vtkImageReader : "<<GetName()
30 //=====================================================================
32 //=====================================================================
33 VtkImageReader::~VtkImageReader()
35 GimmickDebugMessage(5,"Destructing vtkImageReader : "<<GetName()
39 //=====================================================================
41 //=====================================================================
42 bool VtkImageReader::CanRead(const std::string& filename)
45 return (mReader->CanReadFile(filename.c_str())!=0);
48 return (mReader->CanReadFile(filename.c_str())!=0);
55 //=====================================================================
57 //=====================================================================
58 vtkImageData* VtkImageReader::ReadImage(const std::string& filename)
63 mReader->SetFileName(filename.c_str());
65 im = vtkImageData::New();
66 im->ShallowCopy(mReader->GetOutput());
70 if (im!=0) im->Delete();
75 //=====================================================================
77 //=====================================================================
78 void SplitExtensionsString ( const std::string& str,
79 const std::string& delimiters,
80 std::vector<std::string>& tokens)
82 // Skip delimiters at beginning.
83 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
84 // Find first delimiter.
85 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
87 while (std::string::npos != pos || std::string::npos != lastPos)
89 // Found a token, add it to the vector.
90 // SPECIFIC : REMOVE INITIAL DOT (lastPos + 1)
91 tokens.push_back(str.substr(lastPos+1, pos - lastPos));
92 // Skip delimiters. Note the "not_of"
93 lastPos = str.find_first_not_of(delimiters, pos);
94 // Find next delimiter
95 pos = str.find_first_of(delimiters, lastPos);
99 //=====================================================================
101 //=====================================================================
102 void VtkImageReader::PushBackExtensions(std::vector<std::string>& v)
104 std::string ext = mExtensions;
105 if (ext.size()==0) ext = mReader->GetFileExtensions ();
107 SplitExtensionsString(ext," ",v);
109 //=====================================================================
113 //=====================================================================
114 void VtkImageReader::ReadAttributes(const std::string& filename,
115 std::map<std::string,std::string>& attr)
117 GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
118 // Get image dimensions
119 // How to get the image info without loading it in vtk ?
120 mReader->SetFileName(filename.c_str());
121 mReader->Update(); //OpenFile();
123 mReader->GetDataExtent(ext);
126 sprintf(cols,"%i",ext[1]-ext[0]);
129 sprintf(rows,"%i",ext[3]-ext[2]);
132 sprintf(planes,"%i",ext[5]-ext[4]);
134 std::map<std::string,std::string>::iterator i;
135 if ( (i = attr.find("FullFileName")) != attr.end())
137 i->second = filename;
139 if ( (i = attr.find("D0004_1500")) != attr.end())
141 boost::filesystem::path full_path(filename);
142 std::string f = full_path.leaf();
145 if ( (i = attr.find("D0028_0010")) != attr.end())
149 if ( (i = attr.find("D0028_0011")) != attr.end())
154 if ( (i = attr.find("D0028_0012")) != attr.end())
158 if ( (i = attr.find("FullFileDirectory")) != attr.end())
160 std::string::size_type last_pos = filename.find_last_of("//");
161 i->second = filename.substr(0, last_pos);
164 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
166 //=====================================================================
168 } // namespace creaImageIO