Go to the documentation of this file.00001 #include <creaImageIOVtkImageReader.h>
00002 #include <vtkImageReader2.h>
00003 #include <creaImageIOSystem.h>
00004 #include "boost/filesystem/path.hpp"
00005
00006 namespace creaImageIO{
00007
00008 VtkImageReader::VtkImageReader(vtkImageReader2* r,
00009 const std::string& name,
00010 const std::string& extensions)
00011 : mReader(r), mExtensions(extensions)
00012 {
00013 if (name.size() == 0)
00014 {
00015 const char *test =mReader->GetDescriptiveName();
00016 if(test != "")
00017 {
00018 SetName ( "toto");
00019 }
00020 }
00021 else
00022 {
00023 SetName ( name );
00024 }
00025 GimmickDebugMessage(5,"Constructing vtkImageReader : "<<GetName()
00026 <<std::endl);
00027
00028 }
00029
00030
00031
00032 VtkImageReader::~VtkImageReader()
00033 {
00034
00035 mReader->Delete();
00036 }
00037
00038
00039
00040 bool VtkImageReader::CanRead(const std::string& filename)
00041 {
00042
00043 return (mReader->CanReadFile(filename.c_str())!=0);
00044
00045
00046
00047
00048
00049
00050
00051
00052 }
00053
00054 void VtkImageReader::getAttributes(const std::string filename,
00055 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
00056 {
00057 }
00058
00059 vtkImageData* VtkImageReader::ReadImage(const std::string& filename)
00060 {
00061 vtkImageData* im = 0;
00062 try
00063 {
00064 mReader->SetFileName(filename.c_str());
00065 mReader->Update();
00066 im = vtkImageData::New();
00067 im->ShallowCopy(mReader->GetOutput());
00068 }
00069 catch (...)
00070 {
00071 if (im!=0) im->Delete();
00072 im = 0;
00073 }
00074 return im;
00075 }
00076
00077
00078
00079 void SplitExtensionsString ( const std::string& str,
00080 const std::string& delimiters,
00081 std::vector<std::string>& tokens)
00082 {
00083
00084 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00085
00086 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
00087
00088 while (std::string::npos != pos || std::string::npos != lastPos)
00089 {
00090
00091
00092 tokens.push_back(str.substr(lastPos+1, pos - lastPos));
00093
00094 lastPos = str.find_first_not_of(delimiters, pos);
00095
00096 pos = str.find_first_of(delimiters, lastPos);
00097 }
00098
00099 }
00100
00101
00102
00103 void VtkImageReader::PushBackExtensions(std::vector<std::string>& v)
00104 {
00105 std::string ext = mExtensions;
00106 if (ext.size()==0) ext = mReader->GetFileExtensions ();
00107
00108 SplitExtensionsString(ext," ",v);
00109 }
00110
00111
00112
00113
00114
00115 void VtkImageReader::ReadAttributes(const std::string& filename,
00116 std::map<std::string,std::string>& attr)
00117 {
00118 GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
00119
00120
00121 mReader->SetFileName(filename.c_str());
00122 mReader->Update();
00123 int ext[6];
00124 mReader->GetDataExtent(ext);
00125
00126 char cols[128];
00127 sprintf(cols,"%i",ext[1]-ext[0]);
00128
00129 char rows[128];
00130 sprintf(rows,"%i",ext[3]-ext[2]);
00131
00132 char planes[128];
00133 sprintf(planes,"%i",ext[5]-ext[4]);
00134
00135 std::map<std::string,std::string>::iterator i;
00136 if ( (i = attr.find("FullFileName")) != attr.end())
00137 {
00138 i->second = filename;
00139 }
00140 if ( (i = attr.find("D0004_1500")) != attr.end())
00141 {
00142 boost::filesystem::path full_path(filename);
00143 std::string f = full_path.leaf();
00144 i->second = f;
00145 }
00146 if ( (i = attr.find("D0028_0010")) != attr.end())
00147 {
00148 i->second = rows;
00149 }
00150 if ( (i = attr.find("D0028_0011")) != attr.end())
00151 {
00152 i->second = cols;
00153 }
00154
00155 if ( (i = attr.find("D0028_0012")) != attr.end())
00156 {
00157 i->second = planes;
00158 }
00159 if ( (i = attr.find("FullFileDirectory")) != attr.end())
00160 {
00161 std::string::size_type last_pos = filename.find_last_of("//");
00162 i->second = filename.substr(0, last_pos);
00163 }
00164
00165 GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
00166 }
00167
00168
00169 }