1 #include <creaImageIOVtkImageReader.h>
2 #include <vtkImageReader2.h>
3 #include <creaImageIOSystem.h>
4 #include "boost/filesystem/path.hpp"
9 //=====================================================================
10 VtkImageReader::VtkImageReader(vtkImageReader2* r,
11 const std::string& name,
12 const std::string& extensions)
13 : mReader(r), mExtensions(extensions)
17 SetName ( mReader->GetDescriptiveName() );
23 GimmickDebugMessage(5,"Constructing vtkImageReader : "<<GetName()
27 //=====================================================================
29 //=====================================================================
30 VtkImageReader::~VtkImageReader()
32 GimmickDebugMessage(5,"Destructing vtkImageReader : "<<GetName()
36 //=====================================================================
38 //=====================================================================
39 bool VtkImageReader::CanRead(const std::string& filename)
41 return (mReader->CanReadFile(filename.c_str())!=0);
43 //=====================================================================
45 //=====================================================================
46 vtkImageData* VtkImageReader::ReadImage(const std::string& filename)
51 mReader->SetFileName(filename.c_str());
53 im = vtkImageData::New();
54 im->ShallowCopy(mReader->GetOutput());
58 if (im!=0) im->Delete();
63 //=====================================================================
65 //=====================================================================
66 void SplitExtensionsString ( const std::string& str,
67 const std::string& delimiters,
68 std::vector<std::string>& tokens)
70 // Skip delimiters at beginning.
71 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
72 // Find first delimiter.
73 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
75 while (std::string::npos != pos || std::string::npos != lastPos)
77 // Found a token, add it to the vector.
78 // SPECIFIC : REMOVE INITIAL DOT (lastPos + 1)
79 tokens.push_back(str.substr(lastPos+1, pos - lastPos));
80 // Skip delimiters. Note the "not_of"
81 lastPos = str.find_first_not_of(delimiters, pos);
82 // Find next delimiter
83 pos = str.find_first_of(delimiters, lastPos);
87 //=====================================================================
89 //=====================================================================
90 void VtkImageReader::PushBackExtensions(std::vector<std::string>& v)
92 std::string ext = mExtensions;
93 if (ext.size()==0) ext = mReader->GetFileExtensions ();
95 SplitExtensionsString(ext," ",v);
97 //=====================================================================
101 //=====================================================================
102 void VtkImageReader::ReadAttributes(const std::string& filename,
103 std::map<std::string,std::string>& attr)
105 GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
106 // Get image dimensions
107 // How to get the image info without loading it in vtk ?
108 mReader->SetFileName(filename.c_str());
109 mReader->Update(); //OpenFile();
111 mReader->GetDataExtent(ext);
114 sprintf(cols,"%i",ext[1]-ext[0]);
117 sprintf(rows,"%i",ext[3]-ext[2]);
120 sprintf(planes,"%i",ext[5]-ext[4]);
122 std::map<std::string,std::string>::iterator i;
123 if ( (i = attr.find("FullFileName")) != attr.end())
125 i->second = filename;
127 if ( (i = attr.find("D0004_1500")) != attr.end())
129 boost::filesystem::path full_path(filename);
130 std::string f = full_path.leaf();
133 if ( (i = attr.find("D0028_0010")) != attr.end())
137 if ( (i = attr.find("D0028_0011")) != attr.end())
142 if ( (i = attr.find("D0028_0012")) != attr.end())
146 if ( (i = attr.find("FullFileDirectory")) != attr.end())
148 std::string::size_type last_pos = filename.find_last_of("//");
149 i->second = filename.substr(0, last_pos);
152 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
154 //=====================================================================
156 } // namespace creaImageIO