From c7a6bc15ce7ef75426e8f1db97453635fa430370 Mon Sep 17 00:00:00 2001 From: Simon Rit Date: Tue, 13 Nov 2012 18:42:59 +0100 Subject: [PATCH] Added support for ESRF reconstructions --- common/CMakeLists.txt | 3 + common/clitkEsrfHstImageIO.cxx | 142 ++++++++++++++++++++++++++ common/clitkEsrfHstImageIO.h | 66 ++++++++++++ common/clitkEsrfHstImageIOFactory.cxx | 30 ++++++ common/clitkEsrfHstImageIOFactory.h | 75 ++++++++++++++ common/clitkEsrfHstXMLFileReader.cxx | 88 ++++++++++++++++ common/clitkEsrfHstXMLFileReader.h | 75 ++++++++++++++ common/clitkIO.cxx | 2 + 8 files changed, 481 insertions(+) create mode 100644 common/clitkEsrfHstImageIO.cxx create mode 100644 common/clitkEsrfHstImageIO.h create mode 100644 common/clitkEsrfHstImageIOFactory.cxx create mode 100644 common/clitkEsrfHstImageIOFactory.h create mode 100644 common/clitkEsrfHstXMLFileReader.cxx create mode 100644 common/clitkEsrfHstXMLFileReader.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 8ad60a0..efe81ed 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -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 index 0000000..8023ddd --- /dev/null +++ b/common/clitkEsrfHstImageIO.cxx @@ -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 +#include +#include + +//-------------------------------------------------------------------- +// 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(dic["SIZEX"].GetPointer() )->GetMetaDataObjectValue()); + SetDimensions(1, dynamic_cast(dic["SIZEY"].GetPointer() )->GetMetaDataObjectValue()); + SetDimensions(2, dynamic_cast(dic["SIZEZ"].GetPointer() )->GetMetaDataObjectValue()); + SetSpacing(0, dynamic_cast(dic["voxelsize"].GetPointer() )->GetMetaDataObjectValue()); + SetSpacing(1, GetSpacing(0)); + SetSpacing(2, GetSpacing(0)); + SetOrigin(0, GetSpacing(0)*(dynamic_cast(dic["ORIGINX"].GetPointer() )->GetMetaDataObjectValue()-1)); + SetOrigin(1, GetSpacing(0)*(dynamic_cast(dic["ORIGINY"].GetPointer() )->GetMetaDataObjectValue()-1)); + SetOrigin(2, GetSpacing(0)*(dynamic_cast(dic["ORIGINZ"].GetPointer() )->GetMetaDataObjectValue()-1)); + if(std::string("LOWBYTEFIRST") == dynamic_cast(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(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("") ) + 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; iReadBufferAsBinary(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 index 0000000..9a732be --- /dev/null +++ b/common/clitkEsrfHstImageIO.h @@ -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 +#include +#include + +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 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 index 0000000..3ceb983 --- /dev/null +++ b/common/clitkEsrfHstImageIOFactory.cxx @@ -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::New()); +} + diff --git a/common/clitkEsrfHstImageIOFactory.h b/common/clitkEsrfHstImageIOFactory.h new file mode 100644 index 0000000..0f26584 --- /dev/null +++ b/common/clitkEsrfHstImageIOFactory.h @@ -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 +#include +#include + +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 Pointer; + typedef itk::SmartPointer 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 index 0000000..30473f0 --- /dev/null +++ b/common/clitkEsrfHstXMLFileReader.cxx @@ -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 +#include + +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(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(m_Dictionary, metaName, d); \ + } + +#define ENCAPLULATE_META_DATA_STRING(metaName) \ + if(itksys::SystemTools::Strucmp(name, metaName) == 0) { \ + itk::EncapsulateMetaData(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 index 0000000..1b24a0b --- /dev/null +++ b/common/clitkEsrfHstXMLFileReader.h @@ -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 +#include + +#include + +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 +{ +public: + /** Standard typedefs */ + typedef EsrfHstXMLFileReader Self; + typedef itk::XMLReader Superclass; + typedef itk::SmartPointer 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 diff --git a/common/clitkIO.cxx b/common/clitkIO.cxx index 271d5a0..74284ea 100644 --- a/common/clitkIO.cxx +++ b/common/clitkIO.cxx @@ -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(); } //// -- 2.47.1