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() );
26 //=====================================================================
28 //=====================================================================
29 VtkImageReader::~VtkImageReader()
33 //=====================================================================
35 //=====================================================================
36 bool VtkImageReader::CanRead(const std::string& filename)
38 // std::cout << "## Reader "<<GetName()
39 //<<" ::CanRead("<<filename<<")"
41 return (mReader->CanReadFile(filename.c_str())!=0);
43 //=====================================================================
45 //=====================================================================
46 vtkImageData* VtkImageReader::ReadImage(const std::string& filename)
48 // std::cout << "## Reader "<<GetName()
49 //<<" ::Read("<<filename<<")"
54 mReader->SetFileName(filename.c_str());
56 im = vtkImageData::New();
57 im->ShallowCopy(mReader->GetOutput());
61 if (im!=0) im->Delete();
66 //=====================================================================
68 //=====================================================================
69 void SplitExtensionsString ( const std::string& str,
70 const std::string& delimiters,
71 std::vector<std::string>& tokens)
73 // Skip delimiters at beginning.
74 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
75 // Find first delimiter.
76 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
78 while (std::string::npos != pos || std::string::npos != lastPos)
80 // Found a token, add it to the vector.
81 // SPECIFIC : REMOVE INITIAL DOT (lastPos + 1)
82 tokens.push_back(str.substr(lastPos+1, pos - lastPos));
83 // Skip delimiters. Note the "not_of"
84 lastPos = str.find_first_not_of(delimiters, pos);
85 // Find next delimiter
86 pos = str.find_first_of(delimiters, lastPos);
90 //=====================================================================
92 //=====================================================================
93 void VtkImageReader::PushBackExtensions(std::vector<std::string>& v)
95 std::string ext = mExtensions;
96 if (ext.size()==0) ext = mReader->GetFileExtensions ();
98 SplitExtensionsString(ext," ",v);
100 //=====================================================================
104 //=====================================================================
105 void VtkImageReader::ReadAttributes(const std::string& filename,
106 std::map<std::string,std::string>& attr)
108 // std::cout << "VtkImageReader::ReadDicomInfo '"<<filename<<"'"<<std::endl;
109 GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
110 // Get image dimensions
111 // How to get the image info without loading it in vtk ?
112 mReader->SetFileName(filename.c_str());
113 mReader->Update(); //OpenFile();
115 mReader->GetDataExtent(ext);
118 sprintf(cols,"%i",ext[1]-ext[0]);
121 sprintf(rows,"%i",ext[3]-ext[2]);
124 sprintf(planes,"%i",ext[5]-ext[4]);
128 std::map<std::string,std::string>::iterator i;
129 if ( (i = attr.find("FullFileName")) != attr.end())
131 // boost::filesystem::path full_path(filename);
132 // std::string f = full_path.leaf();
133 i->second = filename;
135 if ( (i = attr.find("D0004_1500")) != attr.end())
137 boost::filesystem::path full_path(filename);
138 std::string f = full_path.leaf();
141 if ( (i = attr.find("D0028_0010")) != attr.end())
145 if ( (i = attr.find("D0028_0011")) != attr.end())
150 if ( (i = attr.find("D0028_0012")) != attr.end())
155 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
157 //=====================================================================
159 } // namespace creaImageIO