#include #include #include #include #include #include #include #include #include #include //#include #include #include #include "boost/filesystem/path.hpp" namespace creaImageIO { //======================================================================== std::string irclean(const std::string& str) { if (str == "GDCM::Unfound") { return ""; } if (str[str.size()-1]==' ') { return str.substr(0,str.size()-1); } if (str[str.size()-1]==0) { return str.substr(0,str.size()-1); } return str; } //======================================================================== void IRSplitString ( const std::string& str, const std::string& delimiters, std::vector& tokens) { // Skip delimiters at beginning. std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); // Find first delimiter. std::string::size_type pos = str.find_first_of(delimiters, lastPos); while (std::string::npos != pos || std::string::npos != lastPos) { // Found a token, add it to the vector. // SPECIFIC : REMOVE INITIAL DOT (lastPos + 1) tokens.push_back(str.substr(lastPos+1, pos - lastPos)); // Skip delimiters. Note the "not_of" lastPos = str.find_first_not_of(delimiters, pos); // Find next delimiter pos = str.find_first_of(delimiters, lastPos); } } //===================================================================== class SpecificImageReader { public: SpecificImageReader() {} virtual ~SpecificImageReader() {} void SetName(const std::string& s) { mName = s; } const std::string& GetName() const { return mName; } virtual void PushBackExtensions(std::vector&) {} virtual bool CanRead(const std::string& filename) { return false; } virtual vtkImageData* ReadImage(const std::string& filename) { return 0; } virtual void ReadAttributes(const std::string& filename, std::map& attr) {} private: std::string mName; }; //===================================================================== //===================================================================== class SpecificVtkReader : public SpecificImageReader { public: //===================================================================== SpecificVtkReader(vtkImageReader2* r, const std::string& name = "", const std::string& extensions = "") : mVTKReader(r), mExtensions(extensions) { if (name.size() == 0) { SetName ( mVTKReader->GetDescriptiveName() ); } else { SetName ( name ); } }; //===================================================================== ~SpecificVtkReader() { mVTKReader->Delete(); } //===================================================================== bool CanRead(const std::string& filename) { // std::cout << "## Reader "<CanReadFile(filename.c_str())!=0); } //===================================================================== //===================================================================== vtkImageData* ReadImage(const std::string& filename) { // std::cout << "## Reader "<SetFileName(filename.c_str()); mVTKReader->Update(); im = vtkImageData::New(); im->ShallowCopy(mVTKReader->GetOutput()); } catch (...) { if (im!=0) im->Delete(); im = 0; } return im; } //===================================================================== //===================================================================== void PushBackExtensions(std::vector& v) { std::string ext = mExtensions; if (ext.size()==0) ext = mVTKReader->GetFileExtensions (); IRSplitString(ext," ",v); } //===================================================================== //===================================================================== void ReadAttributes(const std::string& filename, std::map& attr) { // std::cout << "SpecificVtkReader::ReadDicomInfo '"<SetFileName(filename.c_str()); mVTKReader->Update(); //OpenFile(); int ext[6]; mVTKReader->GetDataExtent(ext); // Columns char cols[128]; sprintf(cols,"%i",ext[1]-ext[0]); // Rows char rows[128]; sprintf(rows,"%i",ext[3]-ext[2]); // Planes char planes[128]; sprintf(planes,"%i",ext[5]-ext[4]); // std::map::iterator i; if ( (i = attr.find("FullFileName")) != attr.end()) { // boost::filesystem::path full_path(filename); // std::string f = full_path.leaf(); i->second = filename; } if ( (i = attr.find("D0004_1500")) != attr.end()) { boost::filesystem::path full_path(filename); std::string f = full_path.leaf(); i->second = f; } if ( (i = attr.find("D0028_0010")) != attr.end()) { i->second = rows; } if ( (i = attr.find("D0028_0011")) != attr.end()) { i->second = cols; } if ( (i = attr.find("D0028_0012")) != attr.end()) { i->second = planes; } GimmickMessage(2,"Attributes map:"<GetTypeDescription().GetFieldDescriptionMap(); DicomNodeTypeDescription::FieldDescriptionMapType::const_iterator i; DicomNode::FieldValueMapType& vm = node->GetFieldValueMap(); for (i=dm.begin(); i!=dm.end(); ++i) { if ( (i->second.flags==0) && (i->second.group!=0) && (i->second.element!=0) ) { uint16_t gr = i->second.group; uint16_t el = i->second.element; std::string val = gdcmFile->GetEntryString(gr,el); vm[i->first] = irclean(val); } else { vm[i->first] = ""; } } } */ //===================================================================== //===================================================================== class DicomReader : public SpecificImageReader { public: //===================================================================== DicomReader() { mReader = vtkGdcmReader::New(); SetName ( "Dicom" ); }; //===================================================================== ~DicomReader() { mReader->Delete(); } //===================================================================== bool CanRead(const std::string& filename) { // std::cout << "## Reader "<SetLoadMode( GDCM_NAME_SPACE::LD_ALL); file->SetFileName(filename.c_str()); file->Load(); bool ok = file->IsReadable(); file->Delete(); return ok; } //===================================================================== vtkImageData* ReadImage(const std::string& filename) { // std::cout << "## Reader "<SetFileName(filename.c_str()); mReader->Update(); im = vtkImageData::New(); im->ShallowCopy(mReader->GetOutput()); } catch (...) { if (im!=0) im->Delete(); im = 0; } return im; } //===================================================================== void PushBackExtensions(std::vector& v) { v.push_back("dcm"); v.push_back(""); } //===================================================================== //===================================================================== //===================================================================== void ReadAttributes(const std::string& filename, std::map& attr) { // std::cout << "DicomReader::ReadDicomInfo '"<SetLoadMode( GDCM_NAME_SPACE::LD_ALL); file->SetFileName(filename.c_str()); file->Load(); if (file->IsReadable()) { std::map::iterator i; for (i=attr.begin();i!=attr.end();++i) { if ( i->first == "D0004_1500" ) { boost::filesystem::path full_path(filename); std::string f = full_path.leaf(); i->second = f; } else if ( i->first == "FullFileName" ) { i->second = filename; } else { uint16_t gr; uint16_t el; tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el); // GimmickMessage(2,"Key '"<first<<"' : "<GetEntryString(gr,el); i->second = irclean(val); // GimmickMessage(2,"Key '"<first<<"' : "<::const_iterator i; for (i=GetKnownExtensions().begin(); i!=GetKnownExtensions().end(); i++) { std::cout << "'"<<(*i)<<"'"<SetDimensions ( dim ); mUnreadableImage->SetScalarTypeToUnsignedChar(); mUnreadableImage->AllocateScalars(); for (int i=0;iSetScalarComponentFromFloat(i,j,0,0,0); for (int i=0;iSetScalarComponentFromFloat(i,i,0,0,255); mUnreadableImage->SetScalarComponentFromFloat(dim[0]-1-i,i,0,0,255); } } //===================================================================== //===================================================================== ImageReader::~ImageReader() { // std::cout << "#### ImageReader::~ImageReader()"<::iterator i; for (i=mReader.begin(); i!=mReader.end(); i++) { // std::cout << "#### ImageReader::UnRegister(" // << (*i)->GetName()<<")"<Delete(); mUnreadableImage = 0; } } //===================================================================== //===================================================================== void ImageReader::Register(SpecificImageReader* r) { // std::cout << "#### ImageReader::Register("<GetName()<<")"<PushBackExtensions(mKnownExtensions); } //===================================================================== //===================================================================== // Returns true iff the file is readable bool ImageReader::CanRead( const std::string& filename, const std::string& exclude ) { // std::cout << "## ImageReader::CanRead("<::iterator i; for (i=mReader.begin(); i!=mReader.end(); i++) { if ((*i)->GetName()==exclude) continue; ok = (*i)->CanRead(filename); if (ok) { mLastFilename = filename; mLastReader = *i; break; } } return ok; } //===================================================================== //===================================================================== // Reads the file (CanRead must be called before : no test here) vtkImageData* ImageReader::ReadImage( const std::string& filename, const std::string& exclude ) { // std::cout << "## ImageReader::Read("<ShallowCopy(mUnreadableImage); return im; } } vtkImageData* i = mLastReader->ReadImage(mLastFilename); // std::cout << "i="<ShallowCopy(mUnreadableImage); } // std::cout << "i="<& attr) { // std::cout << "ImageReader::ReadDicomInfo '"<ReadAttributes(mLastFilename,attr); } //===================================================================== } // namespace creaImageIO