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)
14 //EED 21 mars 2012 FLIP probleme ..PLOP..
15 mReader->FileLowerLeftOn();
19 const char *test =mReader->GetDescriptiveName();
20 //warning: comparison with string literal results in unspecified behaviour
22 std::string emptyString("");
23 if(test != emptyString)
25 SetName ( "toto");// mReader->GetDescriptiveName());
32 GimmickDebugMessage(5,"Constructing vtkImageReader : "<<GetName()
36 //=====================================================================
38 //=====================================================================
39 VtkImageReader::~VtkImageReader()
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 //=====================================================================
61 void VtkImageReader::getAttributes(const std::string filename,
62 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
65 //=====================================================================
66 vtkImageData* VtkImageReader::ReadImage(const std::string& filename)
71 mReader->SetFileName(filename.c_str());
73 im = vtkImageData::New();
74 im->ShallowCopy(mReader->GetOutput());
78 if (im!=0) im->Delete();
83 //=====================================================================
85 //=====================================================================
86 void SplitExtensionsString ( const std::string& str,
87 const std::string& delimiters,
88 std::vector<std::string>& tokens)
90 // Skip delimiters at beginning.
91 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
92 // Find first delimiter.
93 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
95 while (std::string::npos != pos || std::string::npos != lastPos)
97 // Found a token, add it to the vector.
98 // SPECIFIC : REMOVE INITIAL DOT (lastPos + 1)
99 tokens.push_back(str.substr(lastPos+1, pos - lastPos));
100 // Skip delimiters. Note the "not_of"
101 lastPos = str.find_first_not_of(delimiters, pos);
102 // Find next delimiter
103 pos = str.find_first_of(delimiters, lastPos);
107 //=====================================================================
109 //=====================================================================
110 void VtkImageReader::PushBackExtensions(std::vector<std::string>& v)
112 std::string ext = mExtensions;
113 if (ext.size()==0) ext = mReader->GetFileExtensions ();
115 SplitExtensionsString(ext," ",v);
117 //=====================================================================
121 //=====================================================================
122 void VtkImageReader::ReadAttributes(const std::string& filename,
123 std::map<std::string,std::string>& attr)
125 GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
126 // Get image dimensions
127 // How to get the image info without loading it in vtk ?
128 mReader->SetFileName(filename.c_str());
129 mReader->Update(); //OpenFile();
131 mReader->GetDataExtent(ext);
134 sprintf(cols,"%i",ext[1]-ext[0]);
137 sprintf(rows,"%i",ext[3]-ext[2]);
140 sprintf(planes,"%i",ext[5]-ext[4]);
142 std::map<std::string,std::string>::iterator i;
143 if ( (i = attr.find("FullFileName")) != attr.end())
145 i->second = filename;
147 if ( (i = attr.find("D0004_1500")) != attr.end())
149 boost::filesystem::path full_path(filename);
150 std::string f = full_path.leaf();
153 if ( (i = attr.find("D0028_0010")) != attr.end())
157 if ( (i = attr.find("D0028_0011")) != attr.end())
162 if ( (i = attr.find("D0028_0012")) != attr.end())
166 if ( (i = attr.find("FullFileDirectory")) != attr.end())
168 std::string::size_type last_pos = filename.find_last_of("//");
169 i->second = filename.substr(0, last_pos);
172 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
174 //=====================================================================
176 } // namespace creaImageIO