]> Creatis software - clitk.git/commitdiff
Added support for ESRF reconstructions
authorSimon Rit <simon.rit@creatis.insa-lyon.fr>
Tue, 13 Nov 2012 17:42:59 +0000 (18:42 +0100)
committerSimon Rit <simon.rit@creatis.insa-lyon.fr>
Tue, 13 Nov 2012 17:42:59 +0000 (18:42 +0100)
common/CMakeLists.txt
common/clitkEsrfHstImageIO.cxx [new file with mode: 0644]
common/clitkEsrfHstImageIO.h [new file with mode: 0644]
common/clitkEsrfHstImageIOFactory.cxx [new file with mode: 0644]
common/clitkEsrfHstImageIOFactory.h [new file with mode: 0644]
common/clitkEsrfHstXMLFileReader.cxx [new file with mode: 0644]
common/clitkEsrfHstXMLFileReader.h [new file with mode: 0644]
common/clitkIO.cxx

index 8ad60a0618eef6b3248fde74dcc751d4e03d9ede..efe81edce046f25702940723f46f97c94c47ac5f 100644 (file)
@@ -24,6 +24,9 @@ SET(clitkCommon_SRC
   rtkHndImageIOFactory.cxx
   rtkEdfImageIO.cxx
   rtkEdfImageIOFactory.cxx
+  clitkEsrfHstImageIO.cxx  
+  clitkEsrfHstImageIOFactory.cxx
+  clitkEsrfHstXMLFileReader.cxx
   clitkDicomRTDoseIO.cxx
   clitkDicomRTDoseIOFactory.cxx
   clitkOrientation.cxx
diff --git a/common/clitkEsrfHstImageIO.cxx b/common/clitkEsrfHstImageIO.cxx
new file mode 100644 (file)
index 0000000..8023ddd
--- /dev/null
@@ -0,0 +1,142 @@
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to:
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+===========================================================================**/
+
+#include "clitkEsrfHstImageIO.h"
+#include "clitkEsrfHstXMLFileReader.h"
+#include "clitkDD.h"
+
+// itk include (for itkReadRawBytesAfterSwappingMacro)
+#include <itkRawImageIO.h>
+#include <itksys/SystemTools.hxx>
+#include <itkMetaDataObject.h>
+
+//--------------------------------------------------------------------
+// Read Image Information
+void clitk::EsrfHstImageIO::ReadImageInformation()
+{
+  clitk::EsrfHstXMLFileReader::Pointer hstXmlReader;
+  hstXmlReader = clitk::EsrfHstXMLFileReader::New();
+  hstXmlReader->SetFilename(m_XmlFileName);
+  hstXmlReader->GenerateOutputInformation();
+  itk::MetaDataDictionary &dic = *(hstXmlReader->GetOutputObject() );
+
+  typedef itk::MetaDataObject< double > MetaDataDoubleType;
+  typedef itk::MetaDataObject< std::string > MetaDataStringType;
+  typedef itk::MetaDataObject< int > MetaDataIntType;
+  SetComponentType(itk::ImageIOBase::FLOAT);
+  SetNumberOfDimensions(3);
+  SetDimensions(0, dynamic_cast<MetaDataIntType *>(dic["SIZEX"].GetPointer() )->GetMetaDataObjectValue());
+  SetDimensions(1, dynamic_cast<MetaDataIntType *>(dic["SIZEY"].GetPointer() )->GetMetaDataObjectValue());
+  SetDimensions(2, dynamic_cast<MetaDataIntType *>(dic["SIZEZ"].GetPointer() )->GetMetaDataObjectValue());
+  SetSpacing(0, dynamic_cast<MetaDataDoubleType *>(dic["voxelsize"].GetPointer() )->GetMetaDataObjectValue());
+  SetSpacing(1, GetSpacing(0));
+  SetSpacing(2, GetSpacing(0));
+  SetOrigin(0, GetSpacing(0)*(dynamic_cast<MetaDataIntType *>(dic["ORIGINX"].GetPointer() )->GetMetaDataObjectValue()-1));
+  SetOrigin(1, GetSpacing(0)*(dynamic_cast<MetaDataIntType *>(dic["ORIGINY"].GetPointer() )->GetMetaDataObjectValue()-1));
+  SetOrigin(2, GetSpacing(0)*(dynamic_cast<MetaDataIntType *>(dic["ORIGINZ"].GetPointer() )->GetMetaDataObjectValue()-1));
+  if(std::string("LOWBYTEFIRST") == dynamic_cast<MetaDataStringType*>(dic["SUBVOLUME_NAME"].GetPointer() )->GetMetaDataObjectValue())
+    this->SetByteOrder(BigEndian);
+  else
+    this->SetByteOrder(LittleEndian);
+
+  // Prepare raw file name
+  m_RawFileName = itksys::SystemTools::GetFilenamePath(m_XmlFileName);
+  if(m_RawFileName != "")
+    m_RawFileName += std::string("/");
+  m_RawFileName += dynamic_cast<MetaDataStringType*>(dic["SUBVOLUME_NAME"].GetPointer() )->GetMetaDataObjectValue();
+} ////
+
+//--------------------------------------------------------------------
+// Read Image Information
+bool clitk::EsrfHstImageIO::CanReadFile(const char* FileNameToRead)
+{
+  std::string ext = itksys::SystemTools::GetFilenameLastExtension(FileNameToRead);
+  if( ext!=std::string(".xml") && ext!=std::string(".vol") )
+    return false;
+
+  m_XmlFileName = std::string(FileNameToRead);
+  if( ext==std::string(".vol") )
+    m_XmlFileName += std::string(".xml");
+
+  std::ifstream is(m_XmlFileName.c_str());
+  if(! is.is_open() )
+    return false;
+
+  std::string firstLine;
+  std::getline(is, firstLine);
+  if(firstLine != std::string("<!-- PyHST VOLUME XML FILE -->") )
+    return false;
+
+  return true;
+} ////
+
+//--------------------------------------------------------------------
+// Read Image Content
+void clitk::EsrfHstImageIO::Read(void * buffer)
+{
+  // Adapted from itkRawImageIO
+  std::ifstream is(m_RawFileName.c_str(), std::ios::binary);
+  if(! is.is_open() )
+    itkExceptionMacro(<<"Could not open file " << m_RawFileName);
+
+  unsigned long numberOfBytesToBeRead = GetComponentSize();
+  for(unsigned int i=0; i<GetNumberOfDimensions(); i++) numberOfBytesToBeRead *= GetDimensions(i);
+
+  if(!this->ReadBufferAsBinary(is, buffer, numberOfBytesToBeRead)) {
+    itkExceptionMacro(<<"Read failed: Wanted "
+                      << numberOfBytesToBeRead
+                      << " bytes, but read "
+                      << is.gcount() << " bytes.");
+  }
+  itkDebugMacro(<< "Reading Done");
+
+  // Adapted from itkRawImageIO
+  {
+    using namespace itk;
+    // Swap bytes if necessary
+    if itkReadRawBytesAfterSwappingMacro( unsigned short, USHORT )
+      else if itkReadRawBytesAfterSwappingMacro( short, SHORT )
+        else if itkReadRawBytesAfterSwappingMacro( char, CHAR )
+          else if itkReadRawBytesAfterSwappingMacro( unsigned char, UCHAR )
+            else if itkReadRawBytesAfterSwappingMacro( unsigned int, UINT )
+              else if itkReadRawBytesAfterSwappingMacro( int, INT )
+                else if itkReadRawBytesAfterSwappingMacro( unsigned int, ULONG )
+                  else if itkReadRawBytesAfterSwappingMacro( int, LONG )
+                    else if itkReadRawBytesAfterSwappingMacro( float, FLOAT )
+                      else if itkReadRawBytesAfterSwappingMacro( double, DOUBLE );
+  }
+}
+
+//--------------------------------------------------------------------
+// Write Image Information
+void clitk::EsrfHstImageIO::WriteImageInformation(bool keepOfStream)
+{
+}
+
+//--------------------------------------------------------------------
+// Write Image Information
+bool clitk::EsrfHstImageIO::CanWriteFile(const char* FileNameToWrite)
+{
+  return false;
+}
+
+//--------------------------------------------------------------------
+// Write Image
+void clitk::EsrfHstImageIO::Write(const void * buffer)
+{
+} ////
diff --git a/common/clitkEsrfHstImageIO.h b/common/clitkEsrfHstImageIO.h
new file mode 100644 (file)
index 0000000..9a732be
--- /dev/null
@@ -0,0 +1,66 @@
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to:
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+===========================================================================**/
+
+#ifndef __clitkEsrfHstImageIO_h
+#define __clitkEsrfHstImageIO_h
+
+#include <itkImageIOBase.h>
+#include <fstream>
+#include <string.h>
+
+namespace clitk
+{
+
+//====================================================================
+// Class for reading Esrf Hst Image file format
+class EsrfHstImageIO: public itk::ImageIOBase
+{
+public:
+  /** Standard class typedefs. */
+  typedef EsrfHstImageIO          Self;
+  typedef itk::ImageIOBase        Superclass;
+  typedef itk::SmartPointer<Self> Pointer;
+
+  EsrfHstImageIO():Superclass() { }
+
+  /** Method for creation through the object factory. */
+  itkNewMacro(Self);
+
+  /** Run-time type information (and related methods). */
+  itkTypeMacro(EsrfHstImageIO, ImageIOBase);
+
+  /*-------- This part of the interface deals with reading data. ------ */
+  virtual void ReadImageInformation();
+  virtual bool CanReadFile( const char* FileNameToRead );
+  virtual void Read(void * buffer);
+
+  /*-------- This part of the interfaces deals with writing data. ----- */
+  virtual void WriteImageInformation(bool keepOfStream);
+  virtual void WriteImageInformation() { WriteImageInformation(false); }
+  virtual bool CanWriteFile(const char* filename);
+  virtual void Write(const void* buffer);
+
+protected:
+  std::string m_XmlFileName;
+  std::string m_RawFileName;
+};
+
+} // end namespace
+
+#endif
+
diff --git a/common/clitkEsrfHstImageIOFactory.cxx b/common/clitkEsrfHstImageIOFactory.cxx
new file mode 100644 (file)
index 0000000..3ceb983
--- /dev/null
@@ -0,0 +1,30 @@
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to:
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+===========================================================================**/
+
+#include "clitkEsrfHstImageIOFactory.h"
+
+//====================================================================
+clitk::EsrfHstImageIOFactory::EsrfHstImageIOFactory()
+{
+  this->RegisterOverride("itkImageIOBase",
+                         "EsrfHstImageIO",
+                         "Esrf Hst Image IO",
+                         1,
+                         itk::CreateObjectFunction<EsrfHstImageIO>::New());
+}
+
diff --git a/common/clitkEsrfHstImageIOFactory.h b/common/clitkEsrfHstImageIOFactory.h
new file mode 100644 (file)
index 0000000..0f26584
--- /dev/null
@@ -0,0 +1,75 @@
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to:
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+===========================================================================**/
+
+#ifndef __clitkEsrfHstImageIOFactory_h
+#define __clitkEsrfHstImageIOFactory_h
+
+#include "clitkEsrfHstImageIO.h"
+#include <itkImageIOBase.h>
+#include <itkObjectFactoryBase.h>
+#include <itkVersion.h>
+
+namespace clitk
+{
+
+//====================================================================
+// Factory for reading Esrf Hst Image file format
+class EsrfHstImageIOFactory: public itk::ObjectFactoryBase
+{
+public:
+  /** Standard class typedefs. */
+  typedef EsrfHstImageIOFactory          Self;
+  typedef itk::ObjectFactoryBase         Superclass;
+  typedef itk::SmartPointer<Self>        Pointer;
+  typedef itk::SmartPointer<const Self>  ConstPointer;
+
+  /** Class methods used to interface with the registered factories. */
+  const char* GetITKSourceVersion(void) const {
+    return ITK_SOURCE_VERSION;
+  }
+
+  const char* GetDescription(void) const {
+    return "Esrf Hst ImageIO Factory, allows the loading of Esrf Hst images into insight";
+  }
+
+  /** Method for class instantiation. */
+  itkFactorylessNewMacro(Self);
+
+  /** Run-time type information (and related methods). */
+  itkTypeMacro(EsrfHstImageIOFactory, ObjectFactoryBase);
+
+  /** Register one factory of this type  */
+  static void RegisterOneFactory(void) {
+    ObjectFactoryBase::RegisterFactory( Self::New() );
+  }
+
+protected:
+  EsrfHstImageIOFactory();
+  ~EsrfHstImageIOFactory() {};
+  typedef EsrfHstImageIOFactory myProductType;
+  const myProductType* m_MyProduct;
+
+private:
+  EsrfHstImageIOFactory(const Self&); //purposely not implemented
+  void operator=(const Self&);        //purposely not implemented
+};
+
+} // end namespace
+
+#endif
+
diff --git a/common/clitkEsrfHstXMLFileReader.cxx b/common/clitkEsrfHstXMLFileReader.cxx
new file mode 100644 (file)
index 0000000..30473f0
--- /dev/null
@@ -0,0 +1,88 @@
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to:
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+===========================================================================**/
+
+#include "clitkEsrfHstXMLFileReader.h"
+#include "itkMacro.h"
+
+#include <itksys/SystemTools.hxx>
+#include <itkMetaDataObject.h>
+
+namespace clitk
+{
+
+int
+EsrfHstXMLFileReader::
+CanReadFile(const char *name)
+{
+  if(!itksys::SystemTools::FileExists(name) ||
+      itksys::SystemTools::FileIsDirectory(name) ||
+      itksys::SystemTools::FileLength(name) == 0)
+    return 0;
+  return 1;
+}
+
+void
+EsrfHstXMLFileReader::
+StartElement(const char * itkNotUsed(name),const char ** itkNotUsed(atts))
+{
+  m_CurCharacterData = "";
+}
+
+void
+EsrfHstXMLFileReader::
+EndElement(const char *name)
+{
+#define ENCAPLULATE_META_DATA_DOUBLE(metaName) \
+  if(itksys::SystemTools::Strucmp(name, metaName) == 0) { \
+    double d = atof(m_CurCharacterData.c_str() ); \
+    itk::EncapsulateMetaData<double>(m_Dictionary, metaName, d); \
+    }
+
+#define ENCAPLULATE_META_DATA_INT(metaName) \
+  if(itksys::SystemTools::Strucmp(name, metaName) == 0) { \
+    double d = atof(m_CurCharacterData.c_str() ); \
+    itk::EncapsulateMetaData<int>(m_Dictionary, metaName, d); \
+    }
+
+#define ENCAPLULATE_META_DATA_STRING(metaName) \
+  if(itksys::SystemTools::Strucmp(name, metaName) == 0) { \
+    itk::EncapsulateMetaData<std::string>(m_Dictionary, metaName, m_CurCharacterData); \
+    }
+
+  ENCAPLULATE_META_DATA_INT("idAc");
+  ENCAPLULATE_META_DATA_STRING("SUBVOLUME_NAME");
+  ENCAPLULATE_META_DATA_INT("SIZEX");
+  ENCAPLULATE_META_DATA_INT("SIZEY");
+  ENCAPLULATE_META_DATA_INT("SIZEZ");
+  ENCAPLULATE_META_DATA_INT("ORIGINX");
+  ENCAPLULATE_META_DATA_INT("ORIGINY");
+  ENCAPLULATE_META_DATA_INT("ORIGINZ");
+  ENCAPLULATE_META_DATA_INT("DIM_REC");
+  ENCAPLULATE_META_DATA_DOUBLE("voxelsize");
+  ENCAPLULATE_META_DATA_STRING("BYTE_ORDER");
+}
+
+void
+EsrfHstXMLFileReader::
+CharacterDataHandler(const char *inData, int inLength)
+{
+  for(int i = 0; i < inLength; i++)
+    m_CurCharacterData = m_CurCharacterData + inData[i];
+}
+
+}
diff --git a/common/clitkEsrfHstXMLFileReader.h b/common/clitkEsrfHstXMLFileReader.h
new file mode 100644 (file)
index 0000000..1b24a0b
--- /dev/null
@@ -0,0 +1,75 @@
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to:
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+===========================================================================**/
+
+#ifndef __clitkEsrfHstXMLFileReader_h
+#define __clitkEsrfHstXMLFileReader_h
+
+#ifdef _MSC_VER
+#pragma warning ( disable : 4786 )
+#endif
+
+#include <itkXMLFile.h>
+#include <itkMetaDataDictionary.h>
+
+#include <map>
+
+namespace clitk
+{
+
+/** \class EsrfHstXMLFileReader
+ *
+ * Reads the XML-format file written by the software High Speed Tomography
+ * at the European Synchrotron Radiation Facility
+ */
+class EsrfHstXMLFileReader : public itk::XMLReader<itk::MetaDataDictionary>
+{
+public:
+  /** Standard typedefs */
+  typedef EsrfHstXMLFileReader                    Self;
+  typedef itk::XMLReader<itk::MetaDataDictionary> Superclass;
+  typedef itk::SmartPointer<Self>                 Pointer;
+
+  /** Run-time type information (and related methods). */
+  itkTypeMacro(EsrfHstXMLFileReader, itk::XMLReader);
+
+  /** Method for creation through the object factory. */
+  itkNewMacro(Self);
+
+  /** Determine if a file can be read */
+  int CanReadFile(const char* name);
+
+protected:
+  EsrfHstXMLFileReader() {m_OutputObject = &m_Dictionary;};
+  virtual ~EsrfHstXMLFileReader() {};
+
+  virtual void StartElement(const char * name,const char **atts);
+
+  virtual void EndElement(const char *name);
+
+  void CharacterDataHandler(const char *inData, int inLength);
+
+private:
+  EsrfHstXMLFileReader(const Self&); //purposely not implemented
+  void operator=(const Self&);       //purposely not implemented
+
+  itk::MetaDataDictionary m_Dictionary;
+  std::string             m_CurCharacterData;
+};
+
+}
+#endif
index 271d5a01d8f8e4b14a4882a290d5487da265c578..74284ea94449396aad0cf731b0c3a7825253028d 100644 (file)
@@ -33,6 +33,7 @@
 #include "rtkHisImageIOFactory.h"
 #include "rtkHndImageIOFactory.h"
 #include "rtkEdfImageIOFactory.h"
+#include "clitkEsrfHstImageIOFactory.h"
 #include "clitkGateAsciiImageIOFactory.h"
 #include "clitkConfiguration.h"
 #if CLITK_PRIVATE_FEATURES
@@ -61,5 +62,6 @@ void clitk::RegisterClitkFactories()
   rtk::HisImageIOFactory::RegisterOneFactory();
   rtk::HndImageIOFactory::RegisterOneFactory();
   rtk::EdfImageIOFactory::RegisterOneFactory();
+  clitk::EsrfHstImageIOFactory::RegisterOneFactory();
 } ////