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 // std::cout << "## Reader "<<GetName()
42 //<<" ::CanRead("<<filename<<")"
44 return (mReader->CanReadFile(filename.c_str())!=0);
46 //=====================================================================
48 //=====================================================================
49 vtkImageData* VtkImageReader::ReadImage(const std::string& filename)
51 // std::cout << "## Reader "<<GetName()
52 //<<" ::Read("<<filename<<")"
57 mReader->SetFileName(filename.c_str());
59 im = vtkImageData::New();
60 im->ShallowCopy(mReader->GetOutput());
64 if (im!=0) im->Delete();
69 //=====================================================================
71 //=====================================================================
72 void SplitExtensionsString ( const std::string& str,
73 const std::string& delimiters,
74 std::vector<std::string>& tokens)
76 // Skip delimiters at beginning.
77 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
78 // Find first delimiter.
79 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
81 while (std::string::npos != pos || std::string::npos != lastPos)
83 // Found a token, add it to the vector.
84 // SPECIFIC : REMOVE INITIAL DOT (lastPos + 1)
85 tokens.push_back(str.substr(lastPos+1, pos - lastPos));
86 // Skip delimiters. Note the "not_of"
87 lastPos = str.find_first_not_of(delimiters, pos);
88 // Find next delimiter
89 pos = str.find_first_of(delimiters, lastPos);
93 //=====================================================================
95 //=====================================================================
96 void VtkImageReader::PushBackExtensions(std::vector<std::string>& v)
98 std::string ext = mExtensions;
99 if (ext.size()==0) ext = mReader->GetFileExtensions ();
101 SplitExtensionsString(ext," ",v);
103 //=====================================================================
107 //=====================================================================
108 void VtkImageReader::ReadAttributes(const std::string& filename,
109 std::map<std::string,std::string>& attr)
111 // std::cout << "VtkImageReader::ReadDicomInfo '"<<filename<<"'"<<std::endl;
112 GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
113 // Get image dimensions
114 // How to get the image info without loading it in vtk ?
115 mReader->SetFileName(filename.c_str());
116 mReader->Update(); //OpenFile();
118 mReader->GetDataExtent(ext);
121 sprintf(cols,"%i",ext[1]-ext[0]);
124 sprintf(rows,"%i",ext[3]-ext[2]);
127 sprintf(planes,"%i",ext[5]-ext[4]);
131 std::map<std::string,std::string>::iterator i;
132 if ( (i = attr.find("FullFileName")) != attr.end())
134 // boost::filesystem::path full_path(filename);
135 // std::string f = full_path.leaf();
136 i->second = filename;
138 if ( (i = attr.find("D0004_1500")) != attr.end())
140 boost::filesystem::path full_path(filename);
141 std::string f = full_path.leaf();
144 if ( (i = attr.find("D0028_0010")) != attr.end())
148 if ( (i = attr.find("D0028_0011")) != attr.end())
153 if ( (i = attr.find("D0028_0012")) != attr.end())
158 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
160 //=====================================================================
162 } // namespace creaImageIO