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();
16 //warning: comparison with string literal results in unspecified behaviour
18 std::string emptyString("");
19 if(test != emptyString)
21 SetName ( "toto");// mReader->GetDescriptiveName());
28 GimmickDebugMessage(5,"Constructing vtkImageReader : "<<GetName()
32 //=====================================================================
34 //=====================================================================
35 VtkImageReader::~VtkImageReader()
40 //=====================================================================
42 //=====================================================================
43 bool VtkImageReader::CanRead(const std::string& filename)
46 return (mReader->CanReadFile(filename.c_str())!=0);
49 return (mReader->CanReadFile(filename.c_str())!=0);
56 //=====================================================================
57 void VtkImageReader::getAttributes(const std::string filename,
58 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
61 //=====================================================================
62 vtkImageData* VtkImageReader::ReadImage(const std::string& filename)
67 mReader->SetFileName(filename.c_str());
69 im = vtkImageData::New();
70 im->ShallowCopy(mReader->GetOutput());
74 if (im!=0) im->Delete();
79 //=====================================================================
81 //=====================================================================
82 void SplitExtensionsString ( const std::string& str,
83 const std::string& delimiters,
84 std::vector<std::string>& tokens)
86 // Skip delimiters at beginning.
87 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
88 // Find first delimiter.
89 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
91 while (std::string::npos != pos || std::string::npos != lastPos)
93 // Found a token, add it to the vector.
94 // SPECIFIC : REMOVE INITIAL DOT (lastPos + 1)
95 tokens.push_back(str.substr(lastPos+1, pos - lastPos));
96 // Skip delimiters. Note the "not_of"
97 lastPos = str.find_first_not_of(delimiters, pos);
98 // Find next delimiter
99 pos = str.find_first_of(delimiters, lastPos);
103 //=====================================================================
105 //=====================================================================
106 void VtkImageReader::PushBackExtensions(std::vector<std::string>& v)
108 std::string ext = mExtensions;
109 if (ext.size()==0) ext = mReader->GetFileExtensions ();
111 SplitExtensionsString(ext," ",v);
113 //=====================================================================
117 //=====================================================================
118 void VtkImageReader::ReadAttributes(const std::string& filename,
119 std::map<std::string,std::string>& attr)
121 GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
122 // Get image dimensions
123 // How to get the image info without loading it in vtk ?
124 mReader->SetFileName(filename.c_str());
125 mReader->Update(); //OpenFile();
127 mReader->GetDataExtent(ext);
130 sprintf(cols,"%i",ext[1]-ext[0]);
133 sprintf(rows,"%i",ext[3]-ext[2]);
136 sprintf(planes,"%i",ext[5]-ext[4]);
138 std::map<std::string,std::string>::iterator i;
139 if ( (i = attr.find("FullFileName")) != attr.end())
141 i->second = filename;
143 if ( (i = attr.find("D0004_1500")) != attr.end())
145 boost::filesystem::path full_path(filename);
146 std::string f = full_path.leaf();
149 if ( (i = attr.find("D0028_0010")) != attr.end())
153 if ( (i = attr.find("D0028_0011")) != attr.end())
158 if ( (i = attr.find("D0028_0012")) != attr.end())
162 if ( (i = attr.find("FullFileDirectory")) != attr.end())
164 std::string::size_type last_pos = filename.find_last_of("//");
165 i->second = filename.substr(0, last_pos);
168 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
170 //=====================================================================
172 } // namespace creaImageIO