From bf4b9657cd6577fe1276b9d3f5fc9f40dbb4d13c Mon Sep 17 00:00:00 2001 From: Simon Rit Date: Wed, 20 Mar 2013 16:30:40 +0100 Subject: [PATCH] Xrad image reader --- common/CMakeLists.txt | 2 + common/clitkIO.cxx | 2 + common/rtkXRadImageIO.cxx | 162 +++++++++++++++++++++++++++++++ common/rtkXRadImageIO.h | 77 +++++++++++++++ common/rtkXRadImageIOFactory.cxx | 29 ++++++ common/rtkXRadImageIOFactory.h | 77 +++++++++++++++ 6 files changed, 349 insertions(+) create mode 100644 common/rtkXRadImageIO.cxx create mode 100644 common/rtkXRadImageIO.h create mode 100644 common/rtkXRadImageIOFactory.cxx create mode 100644 common/rtkXRadImageIOFactory.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index ed51fd8..cd6af5b 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -24,6 +24,8 @@ SET(clitkCommon_SRC rtkHndImageIOFactory.cxx rtkEdfImageIO.cxx rtkEdfImageIOFactory.cxx + rtkXRadImageIO.cxx + rtkXRadImageIOFactory.cxx rtkImagXImageIO.cxx rtkImagXImageIOFactory.cxx rtkImagXXMLFileReader.cxx diff --git a/common/clitkIO.cxx b/common/clitkIO.cxx index 07af298..5c5977b 100644 --- a/common/clitkIO.cxx +++ b/common/clitkIO.cxx @@ -34,6 +34,7 @@ #include "rtkHndImageIOFactory.h" #include "rtkEdfImageIOFactory.h" #include "rtkImagXImageIOFactory.h" +#include "rtkXRadImageIOFactory.h" #include "clitkEsrfHstImageIOFactory.h" #include "clitkGateAsciiImageIOFactory.h" #include "clitkConfiguration.h" @@ -76,6 +77,7 @@ void clitk::RegisterClitkFactories() rtk::HndImageIOFactory::RegisterOneFactory(); rtk::EdfImageIOFactory::RegisterOneFactory(); rtk::ImagXImageIOFactory::RegisterOneFactory(); + rtk::XRadImageIOFactory::RegisterOneFactory(); clitk::EsrfHstImageIOFactory::RegisterOneFactory(); #if ITK_VERSION_MAJOR >= 4 itk::GDCMImageIOFactory::RegisterOneFactory(); diff --git a/common/rtkXRadImageIO.cxx b/common/rtkXRadImageIO.cxx new file mode 100644 index 0000000..f790356 --- /dev/null +++ b/common/rtkXRadImageIO.cxx @@ -0,0 +1,162 @@ +/*========================================================================= + * + * Copyright RTK Consortium + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ + +#include "rtkXRadImageIO.h" + +#include +#include + +//-------------------------------------------------------------------- +// Read Image Information +void rtk::XRadImageIO::ReadImageInformation() +{ + std::ifstream is; + is.open(m_FileName.c_str()); + + if(!is.is_open()) + itkExceptionMacro(<<"Could not open file " << m_FileName); + + SetNumberOfDimensions(3); + while(!is.eof()) + { + std::string line; + std::string section; + std::getline(is, line); + if(line.find('[')!=std::string::npos) + { + unsigned int pos1 = line.find('['); + unsigned int pos2 = line.find(']'); + section = line.substr(pos1+1, pos2-pos1-1); + } + if(line.find('=')!=std::string::npos) + { + unsigned int pos = line.find('='); + std::string paramName = line.substr(0,pos); + std::string paramValue = line.substr(pos+1, line.length()-pos-1); + + if(paramName == std::string("CBCT.DimensionalAttributes.IDim")) + SetDimensions(0, atoi(paramValue.c_str())); + else if(paramName == std::string("CBCT.DimensionalAttributes.JDim")) + SetDimensions(1, atoi(paramValue.c_str())); + else if(paramName == std::string("CBCT.DimensionalAttributes.KDim")) + SetDimensions(2, atoi(paramValue.c_str())); + else if(paramName == std::string("CBCT.DimensionalAttributes.DataSize")) + { + if(atoi(paramValue.c_str())!=3) + { + itkExceptionMacro(<<"Was expecting CBCT.DimensionalAttributes.DataSize==3"); + } + } + else if(paramName == std::string("CBCT.DimensionalAttributes.PixelDimension_I_cm")) + { + double spacing = 0.1*atof(paramValue.c_str()); + SetSpacing(0, (spacing==0.)?1.:spacing); + } + else if(paramName == std::string("CBCT.DimensionalAttributes.PixelDimension_J_cm")) + { + double spacing = 0.1*atof(paramValue.c_str()); + SetSpacing(1, (spacing==0.)?1.:spacing); + } + else if(paramName == std::string("CBCT.DimensionalAttributes.PixelDimension_K_cm")) + { + double spacing = 0.1*atof(paramValue.c_str()); + SetSpacing(2, (spacing==0.)?1.:spacing); + } + else + { + paramName = section + std::string("_") + paramName; + itk::EncapsulateMetaData(this->GetMetaDataDictionary(), + paramName.c_str(), + paramValue); + } + } + + } + SetComponentType(itk::ImageIOBase::FLOAT); +} //// + +//-------------------------------------------------------------------- +// Read Image Information +bool rtk::XRadImageIO::CanReadFile(const char* FileNameToRead) +{ + std::string filename(FileNameToRead); + const std::string::size_type it = filename.find_last_of( "." ); + std::string fileExt( filename, it+1, filename.length() ); + + if (fileExt != std::string("header") ) return false; + return true; +} //// + +//-------------------------------------------------------------------- +// Read Image Content +void rtk::XRadImageIO::Read(void * buffer) +{ + // Adapted from itkRawImageIO + std::string rawFileName( m_FileName, 0, m_FileName.size()-6); + rawFileName += "img"; + + std::ifstream is(rawFileName.c_str(), std::ios::binary); + if(!is.is_open() ) + itkExceptionMacro(<<"Could not open file " << 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 rtk::XRadImageIO::WriteImageInformation(bool keepOfStream) +{ +} + +//-------------------------------------------------------------------- +// Write Image Information +bool rtk::XRadImageIO::CanWriteFile(const char* FileNameToWrite) +{ + return false; +} + +//-------------------------------------------------------------------- +// Write Image +void rtk::XRadImageIO::Write(const void * buffer) +{ +} //// diff --git a/common/rtkXRadImageIO.h b/common/rtkXRadImageIO.h new file mode 100644 index 0000000..8229455 --- /dev/null +++ b/common/rtkXRadImageIO.h @@ -0,0 +1,77 @@ +/*========================================================================= + * + * Copyright RTK Consortium + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ + +#ifndef __rtkXRadImageIO_h +#define __rtkXRadImageIO_h + +#include +#include +#include + +namespace rtk { + +/** \class XRadImageIO + * \brief Class for reading XRad image file format. XRad is the format of + * exported X-ray projection images on the small animal irradiator SMART. + * http://www.pxinc.com/products/small-animal-igrt-platform/x-rad-225cx/ + * + * \author Simon Rit + * + * \ingroup IOFilters + */ +class XRadImageIO : public itk::ImageIOBase +{ +public: + /** Standard class typedefs. */ + typedef XRadImageIO Self; + typedef itk::ImageIOBase Superclass; + typedef itk::SmartPointer Pointer; + + XRadImageIO(): Superclass() {} + + /** Method for creation through the object factory. */ + itkNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkTypeMacro(XRadImageIO, 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: + +}; // end class XRadImageIO + +} // end namespace + +#endif diff --git a/common/rtkXRadImageIOFactory.cxx b/common/rtkXRadImageIOFactory.cxx new file mode 100644 index 0000000..2b29897 --- /dev/null +++ b/common/rtkXRadImageIOFactory.cxx @@ -0,0 +1,29 @@ +/*========================================================================= + * + * Copyright RTK Consortium + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ + +#include "rtkXRadImageIOFactory.h" + +//==================================================================== +rtk::XRadImageIOFactory::XRadImageIOFactory() +{ + this->RegisterOverride("itkImageIOBase", + "XRadImageIO", + "XRad Image IO", + 1, + itk::CreateObjectFunction::New() ); +} diff --git a/common/rtkXRadImageIOFactory.h b/common/rtkXRadImageIOFactory.h new file mode 100644 index 0000000..1651918 --- /dev/null +++ b/common/rtkXRadImageIOFactory.h @@ -0,0 +1,77 @@ +/*========================================================================= + * + * Copyright RTK Consortium + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ + +#ifndef __rtkXRadImageIOFactory_h +#define __rtkXRadImageIOFactory_h + +#include "rtkXRadImageIO.h" +#include +#include +#include + +namespace rtk { + +/** \class XRadImageIOFactory + * \brief ITK factory for XRad file I/O. + * + * \author Simon Rit + */ +class XRadImageIOFactory : public itk::ObjectFactoryBase +{ +public: + /** Standard class typedefs. */ + typedef XRadImageIOFactory 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 "XRad ImageIO Factory, allows the loading of XRad images into insight"; + } + + /** Method for class instantiation. */ + itkFactorylessNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkTypeMacro(XRadImageIOFactory, ObjectFactoryBase); + + /** Register one factory of this type */ + static void RegisterOneFactory(void) { + ObjectFactoryBase::RegisterFactory( Self::New() ); + } + +protected: + XRadImageIOFactory(); + ~XRadImageIOFactory() {} + + typedef XRadImageIOFactory myProductType; + const myProductType* m_MyProduct; +private: + XRadImageIOFactory(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + +}; + +} // end namespace + +#endif -- 2.47.1