creaImageIOTreeComparators
# Image readers
+ creaImageIOSpecificImageReader
+ creaImageIOVtkImageReader
+ creaImageIODicomImageReader
creaImageIOImageReader
creaImageIOMultiThreadImageReader
--- /dev/null
+#include <creaImageIODicomImageReader.h>
+#include <gdcmFile.h>
+#include <vtkGdcmReader.h>
+#include <creaImageIOSystem.h>
+#include "boost/filesystem/path.hpp"
+
+#include <creaImageIOTreeAttributeDescriptor.h>
+
+namespace creaImageIO
+{
+
+ //=====================================================================
+ DicomImageReader::DicomImageReader()
+ {
+ mReader = vtkGdcmReader::New();
+ SetName ( "Dicom" );
+ };
+ //=====================================================================
+
+ //=====================================================================
+ DicomImageReader::~DicomImageReader()
+ {
+ mReader->Delete();
+ }
+ //=====================================================================
+
+ //=====================================================================
+ bool DicomImageReader::CanRead(const std::string& filename)
+ {
+ // std::cout << "## Reader "<<GetName()
+ //<<" ::CanRead("<<filename<<")"
+ // <<std::endl;
+ // return true;
+
+
+ // GDCM_NAME_SPACE
+ // std::cout << "GDCM_NAME_SPACE = '" << STRINGIFY_SYMBOL(GDCM_NAME_SPACE)
+ // << "'"
+ // <<std::endl;
+
+ GDCM_NAME_SPACE::File* file = GDCM_NAME_SPACE::File::New();
+ file->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
+ file->SetFileName(filename.c_str());
+ file->Load();
+ bool ok = file->IsReadable();
+ file->Delete();
+ return ok;
+ }
+ //=====================================================================
+
+ //=====================================================================
+ vtkImageData* DicomImageReader::ReadImage(const std::string& filename)
+ {
+ // std::cout << "## Reader "<<GetName()
+ //<<" ::Read("<<filename<<")"
+ // <<std::endl;
+
+ vtkImageData* im = 0;
+ try
+ {
+ mReader->SetFileName(filename.c_str());
+ mReader->Update();
+ im = vtkImageData::New();
+ im->ShallowCopy(mReader->GetOutput());
+ }
+ catch (...)
+ {
+ if (im!=0) im->Delete();
+ im = 0;
+ }
+ return im;
+ }
+
+ //=====================================================================
+ void DicomImageReader::PushBackExtensions(std::vector<std::string>& v)
+ {
+ v.push_back("dcm");
+ v.push_back("");
+ }
+ //=====================================================================
+
+ //========================================================================
+ 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 DicomImageReader::ReadAttributes(const std::string& filename,
+ std::map<std::string,std::string>& attr)
+ {
+ // std::cout << "DicomImageReader::ReadDicomInfo '"<<filename<<"'"<<std::endl;
+ GimmickMessage(2,"Reading attributes from DICOM file '"
+ <<filename<<"'"<<std::endl);
+
+ GDCM_NAME_SPACE::File* file = GDCM_NAME_SPACE::File::New();
+ file->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
+ file->SetFileName(filename.c_str());
+ file->Load();
+ if (file->IsReadable())
+ {
+
+ std::map<std::string,std::string>::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 '"<<i->first<<"' : "<<gr<<"|"<<el
+ // <<std::endl);
+ if ( ( gr!=0 ) && ( el!=0 ) )
+ {
+ std::string val = file->GetEntryString(gr,el);
+ i->second = irclean(val);
+ // GimmickMessage(2,"Key '"<<i->first<<"' : "<<gr<<"|"<<el
+ // <<"="<<i->second<<std::endl);
+ }
+ }
+ }
+ }
+ file->Delete();
+ }
+
+ //=====================================================================
+
+} // namespace creaImageIO
+
--- /dev/null
+#ifndef __creaImageIODicomImageReader_h_INCLUDED__
+#define __creaImageIODicomImageReader_h_INCLUDED__
+
+
+#include <creaImageIOSpecificImageReader.h>
+
+// forward decl
+class vtkGdcmReader;
+
+namespace creaImageIO
+{
+
+
+ /**
+ * \ingroup ReadWrite
+ */
+
+ //=====================================================================
+ /// Specific image reader for DICOM images
+ class DicomImageReader : virtual public SpecificImageReader
+ {
+ public:
+ DicomImageReader();
+ virtual ~DicomImageReader();
+
+ virtual void PushBackExtensions(std::vector<std::string>&);
+ virtual bool CanRead(const std::string& filename);
+ virtual vtkImageData* ReadImage(const std::string& filename);
+ virtual void ReadAttributes(const std::string& filename,
+ tree::AttributeMapType& attr);
+
+ private:
+ vtkGdcmReader* mReader;
+ };
+ //=====================================================================
+
+
+
+} // namespace creaImageIO
+
+
+
+#endif // #ifndef __creaImageIODicomImageReader_h_INCLUDED__
#include <creaImageIOTreeAttributeDescriptor.h>
#include <creaImageIOSystem.h>
-#include <vtkImageReader2.h>
+#include <creaImageIOVtkImageReader.h>
+#include <creaImageIODicomImageReader.h>
+
#include <vtkPNGReader.h>
#include <vtkTIFFReader.h>
#include <vtkJPEGReader.h>
#include <vtkMetaImageReader.h>
//#include <vtkGESignalReader.h>
-#include <gdcmFile.h>
-#include <vtkGdcmReader.h>
#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<std::string>& 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<std::string>&) {}
-
- 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<std::string,std::string>& 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 "<<GetName()
- //<<" ::CanRead("<<filename<<")"
- // <<std::endl;
- return (mVTKReader->CanReadFile(filename.c_str())!=0);
- }
- //=====================================================================
-
- //=====================================================================
- vtkImageData* ReadImage(const std::string& filename)
- {
- // std::cout << "## Reader "<<GetName()
- //<<" ::Read("<<filename<<")"
- // <<std::endl;
- vtkImageData* im = 0;
- try
- {
- mVTKReader->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<std::string>& v)
- {
- std::string ext = mExtensions;
- if (ext.size()==0) ext = mVTKReader->GetFileExtensions ();
-
- IRSplitString(ext," ",v);
- }
- //=====================================================================
-
- //=====================================================================
- void ReadAttributes(const std::string& filename,
- std::map<std::string,std::string>& attr)
- {
- // std::cout << "SpecificVtkReader::ReadDicomInfo '"<<filename<<"'"<<std::endl;
- GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
- // Get image dimensions
- // How to get the image info without loading it in vtk ?
- mVTKReader->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<std::string,std::string>::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:"<<std::endl<<attr<<std::endl);
- }
- //=====================================================================
- private:
- vtkImageReader2* mVTKReader;
- std::string mExtensions;
- };
- //=====================================================================
-
//=====================================================================
/*
void IRFillFields(DicomNode* node,
*/
//=====================================================================
- //=====================================================================
- class DicomReader : public SpecificImageReader
- {
- public:
- //=====================================================================
- DicomReader()
- {
- mReader = vtkGdcmReader::New();
- SetName ( "Dicom" );
- };
- //=====================================================================
- ~DicomReader()
- {
- mReader->Delete();
- }
- //=====================================================================
- bool CanRead(const std::string& filename)
- {
- // std::cout << "## Reader "<<GetName()
- //<<" ::CanRead("<<filename<<")"
- // <<std::endl;
- // return true;
-
-
- // GDCM_NAME_SPACE
- // std::cout << "GDCM_NAME_SPACE = '" << STRINGIFY_SYMBOL(GDCM_NAME_SPACE)
- // << "'"
- // <<std::endl;
-
- GDCM_NAME_SPACE::File* file = GDCM_NAME_SPACE::File::New();
- file->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 "<<GetName()
- //<<" ::Read("<<filename<<")"
- // <<std::endl;
-
- vtkImageData* im = 0;
- try
- {
- mReader->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<std::string>& v)
- {
- v.push_back("dcm");
- v.push_back("");
- }
- //=====================================================================
-
- //=====================================================================
- //=====================================================================
- void ReadAttributes(const std::string& filename,
- std::map<std::string,std::string>& attr)
- {
- // std::cout << "DicomReader::ReadDicomInfo '"<<filename<<"'"<<std::endl;
- GimmickMessage(2,"Reading attributes from DICOM file '"
- <<filename<<"'"<<std::endl);
-
- GDCM_NAME_SPACE::File* file = GDCM_NAME_SPACE::File::New();
- file->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
- file->SetFileName(filename.c_str());
- file->Load();
- if (file->IsReadable())
- {
-
- std::map<std::string,std::string>::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 '"<<i->first<<"' : "<<gr<<"|"<<el
- // <<std::endl);
- if ( ( gr!=0 ) && ( el!=0 ) )
- {
- std::string val = file->GetEntryString(gr,el);
- i->second = irclean(val);
- // GimmickMessage(2,"Key '"<<i->first<<"' : "<<gr<<"|"<<el
- // <<"="<<i->second<<std::endl);
- }
- }
- }
- }
- file->Delete();
- }
-
-
- private:
- vtkGdcmReader* mReader;
- };
- //=====================================================================
-
-
// std::cout << "#### ImageReader::ImageReader()"<<std::endl;
if (mUnreadableImage!=0) return;
- Register(new SpecificVtkReader(vtkPNGReader::New()));
- Register(new SpecificVtkReader(vtkTIFFReader::New()));
- Register(new SpecificVtkReader(vtkJPEGReader::New()));
- Register(new SpecificVtkReader(vtkBMPReader::New()));
- Register(new SpecificVtkReader(vtkSLCReader::New()));
- Register(new SpecificVtkReader(vtkMetaImageReader::New(),"MHD",".mhd"));
- // Register(new SpecificVtkReader(vtkGESignalReader::New()));
- Register(new DicomReader);
+ Register(new VtkImageReader(vtkPNGReader::New()));
+ Register(new VtkImageReader(vtkTIFFReader::New()));
+ Register(new VtkImageReader(vtkJPEGReader::New()));
+ Register(new VtkImageReader(vtkBMPReader::New()));
+ Register(new VtkImageReader(vtkSLCReader::New()));
+ Register(new VtkImageReader(vtkMetaImageReader::New(),"MHD",".mhd"));
+ // Register(new VtkImageReader(vtkGESignalReader::New()));
+ Register(new DicomImageReader);
/*
std::cout << "## Registered file extensions : "<<std::endl;
--- /dev/null
+#ifndef __creaImageIOSpecificImageReader_h_INCLUDED__
+#define __creaImageIOSpecificImageReader_h_INCLUDED__
+
+
+#include <vtkImageData.h>
+#include <string>
+#include <map>
+#include <vector>
+#include <creaImageIOTreeAttributeMapType.h>
+
+namespace creaImageIO
+{
+
+
+ /**
+ * \ingroup ReadWrite
+ */
+
+ //=====================================================================
+ /// Abstract specific image reader
+ class SpecificImageReader
+ {
+ public:
+ SpecificImageReader() {}
+ virtual ~SpecificImageReader() {}
+
+ const std::string& GetName() const { return mName; }
+
+ virtual void PushBackExtensions(std::vector<std::string>&) {}
+ 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,
+ tree::AttributeMapType& attr) {}
+
+ protected:
+ void SetName(const std::string& s) { mName = s; }
+ private:
+ std::string mName;
+ };
+ //=====================================================================
+
+
+
+} // namespace creaImageIO
+
+
+
+#endif // #ifndef __creaImageIOSpecificImageReader_h_INCLUDED__
--- /dev/null
+#include <creaImageIOVtkImageReader.h>
+#include <vtkImageReader2.h>
+#include <creaImageIOSystem.h>
+#include "boost/filesystem/path.hpp"
+
+namespace creaImageIO
+{
+
+ //=====================================================================
+ VtkImageReader::VtkImageReader(vtkImageReader2* r,
+ const std::string& name,
+ const std::string& extensions)
+ : mReader(r), mExtensions(extensions)
+ {
+ if (name.size() == 0)
+ {
+ SetName ( mReader->GetDescriptiveName() );
+ }
+ else
+ {
+ SetName ( name );
+ }
+
+
+ }
+ //=====================================================================
+
+ //=====================================================================
+ VtkImageReader::~VtkImageReader()
+ {
+ mReader->Delete();
+ }
+ //=====================================================================
+
+ //=====================================================================
+ bool VtkImageReader::CanRead(const std::string& filename)
+ {
+ // std::cout << "## Reader "<<GetName()
+ //<<" ::CanRead("<<filename<<")"
+ // <<std::endl;
+ return (mReader->CanReadFile(filename.c_str())!=0);
+ }
+ //=====================================================================
+
+ //=====================================================================
+ vtkImageData* VtkImageReader::ReadImage(const std::string& filename)
+ {
+ // std::cout << "## Reader "<<GetName()
+ //<<" ::Read("<<filename<<")"
+ // <<std::endl;
+ vtkImageData* im = 0;
+ try
+ {
+ mReader->SetFileName(filename.c_str());
+ mReader->Update();
+ im = vtkImageData::New();
+ im->ShallowCopy(mReader->GetOutput());
+ }
+ catch (...)
+ {
+ if (im!=0) im->Delete();
+ im = 0;
+ }
+ return im;
+ }
+ //=====================================================================
+
+ //=====================================================================
+ void SplitExtensionsString ( const std::string& str,
+ const std::string& delimiters,
+ std::vector<std::string>& 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);
+ }
+
+ }
+ //=====================================================================
+
+ //=====================================================================
+ void VtkImageReader::PushBackExtensions(std::vector<std::string>& v)
+ {
+ std::string ext = mExtensions;
+ if (ext.size()==0) ext = mReader->GetFileExtensions ();
+
+ SplitExtensionsString(ext," ",v);
+ }
+ //=====================================================================
+
+
+
+ //=====================================================================
+ void VtkImageReader::ReadAttributes(const std::string& filename,
+ std::map<std::string,std::string>& attr)
+ {
+ // std::cout << "VtkImageReader::ReadDicomInfo '"<<filename<<"'"<<std::endl;
+ GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
+ // Get image dimensions
+ // How to get the image info without loading it in vtk ?
+ mReader->SetFileName(filename.c_str());
+ mReader->Update(); //OpenFile();
+ int ext[6];
+ mReader->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<std::string,std::string>::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:"<<std::endl<<attr<<std::endl);
+ }
+ //=====================================================================
+
+} // namespace creaImageIO
--- /dev/null
+#ifndef __creaImageIOVtkImageReader_h_INCLUDED__
+#define __creaImageIOVtkImageReader_h_INCLUDED__
+
+
+#include <creaImageIOSpecificImageReader.h>
+
+// forward decl
+class vtkImageReader2;
+
+namespace creaImageIO
+{
+
+
+ /**
+ * \ingroup ReadWrite
+ */
+
+ //=====================================================================
+ /// Specific image reader based on a vtkImageReader2
+ class VtkImageReader : virtual public SpecificImageReader
+ {
+ public:
+ VtkImageReader(vtkImageReader2* reader,
+ const std::string& name = "",
+ const std::string& extensions = "");
+
+ virtual ~VtkImageReader();
+
+ virtual void PushBackExtensions(std::vector<std::string>&);
+ virtual bool CanRead(const std::string& filename);
+ virtual vtkImageData* ReadImage(const std::string& filename);
+ virtual void ReadAttributes(const std::string& filename,
+ tree::AttributeMapType& attr);
+
+ private:
+ vtkImageReader2* mReader;
+ std::string mExtensions;
+ };
+ //=====================================================================
+
+
+
+} // namespace creaImageIO
+
+
+
+#endif // #ifndef __creaImageIOVtkImageReader_h_INCLUDED__