From 4c1f33fa1ab2a114f4dc94b24b0f2ef936ffafb8 Mon Sep 17 00:00:00 2001 From: Simon Rit Date: Tue, 20 Nov 2012 17:30:21 +0100 Subject: [PATCH] Added ImagX file format from RTK --- common/CMakeLists.txt | 3 + common/clitkIO.cxx | 2 + common/rtkImagXImageIO.cxx | 192 ++++++++++++++++++ common/rtkImagXImageIO.h | 72 +++++++ common/rtkImagXImageIOFactory.cxx | 30 +++ common/rtkImagXImageIOFactory.h | 78 +++++++ common/rtkImagXLookupTableImageFilter.h | 97 +++++++++ common/rtkImagXRawToAttenuationImageFilter.h | 94 +++++++++ .../rtkImagXRawToAttenuationImageFilter.txx | 85 ++++++++ common/rtkImagXXMLFileReader.cxx | 109 ++++++++++ common/rtkImagXXMLFileReader.h | 75 +++++++ 11 files changed, 837 insertions(+) create mode 100644 common/rtkImagXImageIO.cxx create mode 100644 common/rtkImagXImageIO.h create mode 100644 common/rtkImagXImageIOFactory.cxx create mode 100644 common/rtkImagXImageIOFactory.h create mode 100644 common/rtkImagXLookupTableImageFilter.h create mode 100644 common/rtkImagXRawToAttenuationImageFilter.h create mode 100644 common/rtkImagXRawToAttenuationImageFilter.txx create mode 100644 common/rtkImagXXMLFileReader.cxx create mode 100644 common/rtkImagXXMLFileReader.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index a6edaf4..3c7a580 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -24,6 +24,9 @@ SET(clitkCommon_SRC rtkHndImageIOFactory.cxx rtkEdfImageIO.cxx rtkEdfImageIOFactory.cxx + rtkImagXImageIO.cxx + rtkImagXImageIOFactory.cxx + rtkImagXXMLFileReader.cxx clitkEsrfHstImageIO.cxx clitkEsrfHstImageIOFactory.cxx clitkEsrfHstXMLFileReader.cxx diff --git a/common/clitkIO.cxx b/common/clitkIO.cxx index 74284ea..8b159e7 100644 --- a/common/clitkIO.cxx +++ b/common/clitkIO.cxx @@ -33,6 +33,7 @@ #include "rtkHisImageIOFactory.h" #include "rtkHndImageIOFactory.h" #include "rtkEdfImageIOFactory.h" +#include "rtkImagXImageIOFactory.h" #include "clitkEsrfHstImageIOFactory.h" #include "clitkGateAsciiImageIOFactory.h" #include "clitkConfiguration.h" @@ -62,6 +63,7 @@ void clitk::RegisterClitkFactories() rtk::HisImageIOFactory::RegisterOneFactory(); rtk::HndImageIOFactory::RegisterOneFactory(); rtk::EdfImageIOFactory::RegisterOneFactory(); + rtk::ImagXImageIOFactory::RegisterOneFactory(); clitk::EsrfHstImageIOFactory::RegisterOneFactory(); } //// diff --git a/common/rtkImagXImageIO.cxx b/common/rtkImagXImageIO.cxx new file mode 100644 index 0000000..d197ed7 --- /dev/null +++ b/common/rtkImagXImageIO.cxx @@ -0,0 +1,192 @@ +/*========================================================================= + * + * 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 "rtkImagXImageIO.h" +#include "rtkImagXXMLFileReader.h" + +// itk include (for itkReadRawBytesAfterSwappingMacro) +#include +#include +#include +#include + +//-------------------------------------------------------------------- +// Read Image Information +void rtk::ImagXImageIO::ReadImageInformation() +{ + rtk::ImagXXMLFileReader::Pointer xmlReader; + + xmlReader = rtk::ImagXXMLFileReader::New(); + xmlReader->SetFilename(m_FileName); + xmlReader->GenerateOutputInformation(); + + itk::MetaDataDictionary &dic = *(xmlReader->GetOutputObject() ); + + typedef itk::MetaDataObject< double > MetaDataDoubleType; + typedef itk::MetaDataObject< std::string > MetaDataStringType; + typedef itk::MetaDataObject< int > MetaDataIntType; + + std::string pixelType = dynamic_cast(dic["pixelFormat"].GetPointer() )->GetMetaDataObjectValue(); + if(pixelType=="Type_uint8") + SetComponentType(itk::ImageIOBase::UCHAR); + if(pixelType=="Type_sint8") + SetComponentType(itk::ImageIOBase::CHAR); + if(pixelType=="Type_uint16") + SetComponentType(itk::ImageIOBase::USHORT); + if(pixelType=="Type_sint16") + SetComponentType(itk::ImageIOBase::SHORT); + if(pixelType=="Type_uint32") + SetComponentType(itk::ImageIOBase::UINT); + if(pixelType=="Type_sint32") + SetComponentType(itk::ImageIOBase::INT); + if(pixelType=="Type_float") + SetComponentType(itk::ImageIOBase::FLOAT); + + if(dic["dimensions"].GetPointer()==NULL) + SetNumberOfDimensions(3); + else + SetNumberOfDimensions( ( dynamic_cast(dic["dimensions"].GetPointer() )->GetMetaDataObjectValue() ) ); + + SetDimensions(0, dynamic_cast(dic["x"].GetPointer() )->GetMetaDataObjectValue() ); + SetSpacing(0, dynamic_cast(dic["spacing_x"].GetPointer() )->GetMetaDataObjectValue() ); + if(GetNumberOfDimensions()>1) + { + SetDimensions(1, dynamic_cast(dic["y"].GetPointer() )->GetMetaDataObjectValue() ); + SetSpacing(1, dynamic_cast(dic["spacing_y"].GetPointer() )->GetMetaDataObjectValue() ); + } + if(GetNumberOfDimensions()>2) + { + SetDimensions(2, dynamic_cast(dic["z"].GetPointer() )->GetMetaDataObjectValue() ); + SetSpacing(2, dynamic_cast(dic["spacing_z"].GetPointer() )->GetMetaDataObjectValue() ); + } + + std::istringstream iss( + dynamic_cast(dic["matrixTransform"].GetPointer() )->GetMetaDataObjectValue() ); + itk::Matrix matrix; + for(unsigned int j=0; j<4; j++) + for(unsigned int i=0; i<4; i++) + iss >> matrix[j][i]; + matrix /= matrix[3][3]; + + std::vector direction; + for(unsigned int i=0; i(dic["byteOrder"].GetPointer() )->GetMetaDataObjectValue() ) + this->SetByteOrder(LittleEndian); + else + this->SetByteOrder(BigEndian); + + // Prepare raw file name + m_RawFileName = itksys::SystemTools::GetFilenamePath(m_FileName); + if(m_RawFileName != "") + m_RawFileName += std::string("/"); + m_RawFileName += dynamic_cast(dic["rawFile"].GetPointer() )->GetMetaDataObjectValue(); +} //// + +//-------------------------------------------------------------------- +// Read Image Information +bool rtk::ImagXImageIO::CanReadFile(const char* FileNameToRead) +{ + std::string ext = itksys::SystemTools::GetFilenameLastExtension(FileNameToRead); + + if( ext!=std::string(".xml") ) + return false; + + std::ifstream is(FileNameToRead); + if(!is.is_open() ) + return false; + + // If the XML file has "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 rtk::ImagXImageIO::WriteImageInformation(bool keepOfStream) +{ +} + +//-------------------------------------------------------------------- +// Write Image Information +bool rtk::ImagXImageIO::CanWriteFile(const char* FileNameToWrite) +{ + return false; +} + +//-------------------------------------------------------------------- +// Write Image +void rtk::ImagXImageIO::Write(const void * buffer) +{ +} //// + diff --git a/common/rtkImagXImageIO.h b/common/rtkImagXImageIO.h new file mode 100644 index 0000000..8188b04 --- /dev/null +++ b/common/rtkImagXImageIO.h @@ -0,0 +1,72 @@ +/*========================================================================= + * + * 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 __rtkImagXImageIO_h +#define __rtkImagXImageIO_h + +#include +#include +#include + +namespace rtk +{ + +/** \class ImagXImageIO + * + * TODO + * + */ +class ImagXImageIO : public itk::ImageIOBase +{ +public: + /** Standard class typedefs. */ + typedef ImagXImageIO Self; + typedef itk::ImageIOBase Superclass; + typedef itk::SmartPointer Pointer; + + ImagXImageIO() : Superclass() {} + + /** Method for creation through the object factory. */ + itkNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkTypeMacro(ImagXImageIO, 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_RawFileName; +}; + +} // end namespace + +#endif + diff --git a/common/rtkImagXImageIOFactory.cxx b/common/rtkImagXImageIOFactory.cxx new file mode 100644 index 0000000..47520a9 --- /dev/null +++ b/common/rtkImagXImageIOFactory.cxx @@ -0,0 +1,30 @@ +/*========================================================================= + * + * 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 "rtkImagXImageIOFactory.h" + +//==================================================================== +rtk::ImagXImageIOFactory::ImagXImageIOFactory() +{ + this->RegisterOverride("itkImageIOBase", + "ImagXImageIO", + "ImagX Image IO", + 1, + itk::CreateObjectFunction::New() ); +} + diff --git a/common/rtkImagXImageIOFactory.h b/common/rtkImagXImageIOFactory.h new file mode 100644 index 0000000..5170d45 --- /dev/null +++ b/common/rtkImagXImageIOFactory.h @@ -0,0 +1,78 @@ +/*========================================================================= + * + * 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 __rtkImagXImageIOFactory_h +#define __rtkImagXImageIOFactory_h + +#include "rtkImagXImageIO.h" +#include +#include +#include + +namespace rtk +{ + +/** \class ImagXImageIOFactory + * + * TODO + * + */ +class ImagXImageIOFactory : public itk::ObjectFactoryBase +{ +public: + /** Standard class typedefs. */ + typedef ImagXImageIOFactory 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 "ImagX ImageIO Factory, allows the loading of ImagX 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: + ImagXImageIOFactory(); + ~ImagXImageIOFactory() {} + typedef ImagXImageIOFactory myProductType; + const myProductType* m_MyProduct; +private: + ImagXImageIOFactory(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + +}; + +} // end namespace + +#endif + diff --git a/common/rtkImagXLookupTableImageFilter.h b/common/rtkImagXLookupTableImageFilter.h new file mode 100644 index 0000000..c6271cc --- /dev/null +++ b/common/rtkImagXLookupTableImageFilter.h @@ -0,0 +1,97 @@ +/*========================================================================= + * + * 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 __rtkImagXLookupTableImageFilter_h +#define __rtkImagXLookupTableImageFilter_h + +#include "rtkLookupTableImageFilter.h" +#include + +namespace rtk +{ + +/** \class ImagXLookupTableImageFilter + * \brief Lookup table for ImagX data. + * + * The lookup table converts the raw values measured by the panel to the + * logarithm of the value divided by the maximum numerical value. This could + * be improved with a calibration of the air value. + * + * \author Simon Rit + * + * \ingroup ImageToImageFilter + */ +template +class ITK_EXPORT ImagXLookupTableImageFilter : public LookupTableImageFilter +{ + +public: + /** Standard class typedefs. */ + typedef ImagXLookupTableImageFilter Self; + typedef LookupTableImageFilter Superclass; + typedef itk::SmartPointer Pointer; + typedef itk::SmartPointer ConstPointer; + + typedef typename TInputImage::PixelType InputImagePixelType; + typedef typename TOutputImage::PixelType OutputImagePixelType; + typedef typename Superclass::FunctorType::LookupTableType LookupTableType; + + /** Method for creation through the object factory. */ + itkNewMacro(Self); + + /** Runtime information support. */ + itkTypeMacro(ImagXLookupTableImageFilter, LookupTableImageFilter); +protected: + ImagXLookupTableImageFilter(); + virtual ~ImagXLookupTableImageFilter() { + } + +private: + ImagXLookupTableImageFilter(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + +}; + +} // end namespace rtk + +template +rtk::ImagXLookupTableImageFilter::ImagXLookupTableImageFilter() +{ + // Create the lut + typename LookupTableType::Pointer lut = LookupTableType::New(); + typename LookupTableType::SizeType size; + size[0] = itk::NumericTraits::max()-itk::NumericTraits::min()+1; + lut->SetRegions( size ); + lut->Allocate(); + + OutputImagePixelType logRef = log(OutputImagePixelType(size[0]) ); + + // Iterate and set lut + itk::ImageRegionIteratorWithIndex it( lut, lut->GetBufferedRegion() ); + it.GoToBegin(); + while( !it.IsAtEnd() ) + { + it.Set( logRef - log(it.GetIndex()[0]+1.) ); + ++it; + } + + // Set the lut to member and functor + this->SetLookupTable(lut); +} + +#endif diff --git a/common/rtkImagXRawToAttenuationImageFilter.h b/common/rtkImagXRawToAttenuationImageFilter.h new file mode 100644 index 0000000..6ee663a --- /dev/null +++ b/common/rtkImagXRawToAttenuationImageFilter.h @@ -0,0 +1,94 @@ +/*========================================================================= + * + * 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 __rtkImagXRawToAttenuationImageFilter_h +#define __rtkImagXRawToAttenuationImageFilter_h + +#include +#include + +#include "rtkImagXLookupTableImageFilter.h" +#include "rtkBoellaardScatterCorrectionImageFilter.h" + +/** \class ImagXRawToAttenuationImageFilter + * \brief Convert raw ImagX data to attenuation images + * + * TODO + * + * \author Simon Rit + * + * \ingroup ImageToImageFilter + */ +namespace rtk +{ + +template +class ITK_EXPORT ImagXRawToAttenuationImageFilter : + public itk::ImageToImageFilter +{ +public: + /** Standard class typedefs. */ + typedef ImagXRawToAttenuationImageFilter Self; + typedef itk::ImageToImageFilter Superclass; + typedef itk::SmartPointer Pointer; + typedef itk::SmartPointer ConstPointer; + + /** Some convenient typedefs. */ + typedef TInputImage InputImageType; + typedef TOutputImage OutputImageType; + + /** Standard New method. */ + itkNewMacro(Self); + + /** Runtime information support. */ + itkTypeMacro(ImagXRawToAttenuationImageFilter, itk::ImageToImageFilter); +protected: + ImagXRawToAttenuationImageFilter(); + ~ImagXRawToAttenuationImageFilter(){ + } + + /** Apply changes to the input image requested region. */ + virtual void GenerateInputRequestedRegion(); + + void GenerateOutputInformation(); + + /** Single-threaded version of GenerateData. This filter delegates + * to other filters. */ + void GenerateData(); + +private: + //purposely not implemented + ImagXRawToAttenuationImageFilter(const Self&); + void operator=(const Self&); + + typedef itk::CropImageFilter CropFilterType; + typedef rtk::BoellaardScatterCorrectionImageFilter ScatterFilterType; + typedef rtk::ImagXLookupTableImageFilter LookupTableFilterType; + + typename LookupTableFilterType::Pointer m_LookupTableFilter; + typename CropFilterType::Pointer m_CropFilter; + typename ScatterFilterType::Pointer m_ScatterFilter; +}; // end of class + +} // end namespace rtk + +#ifndef ITK_MANUAL_INSTANTIATION +#include "rtkImagXRawToAttenuationImageFilter.txx" +#endif + +#endif diff --git a/common/rtkImagXRawToAttenuationImageFilter.txx b/common/rtkImagXRawToAttenuationImageFilter.txx new file mode 100644 index 0000000..5e02cee --- /dev/null +++ b/common/rtkImagXRawToAttenuationImageFilter.txx @@ -0,0 +1,85 @@ +/*========================================================================= + * + * 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 __rtkImagXRawToAttenuationImageFilter_txx +#define __rtkImagXRawToAttenuationImageFilter_txx + +#include + +namespace rtk +{ + +template +void +ImagXRawToAttenuationImageFilter +::GenerateInputRequestedRegion() +{ + typename Superclass::InputImagePointer inputPtr = + const_cast< TInputImage * >( this->GetInput() ); + if ( !inputPtr ) + return; + + m_CropFilter->SetInput(inputPtr); //SR: this is most likely useless + m_LookupTableFilter->GetOutput()->SetRequestedRegion(this->GetOutput()->GetRequestedRegion() ); + m_LookupTableFilter->GetOutput()->PropagateRequestedRegion(); +} + +template +ImagXRawToAttenuationImageFilter +::ImagXRawToAttenuationImageFilter() +{ + m_CropFilter = CropFilterType::New(); + m_ScatterFilter = ScatterFilterType::New(); + m_LookupTableFilter = LookupTableFilterType::New(); + + //Permanent internal connections + m_ScatterFilter->SetInput( m_CropFilter->GetOutput() ); + m_LookupTableFilter->SetInput( m_ScatterFilter->GetOutput() ); + + //Default filter parameters + typename CropFilterType::SizeType border = m_CropFilter->GetLowerBoundaryCropSize(); + border[0] = 4; + border[1] = 4; + m_CropFilter->SetBoundaryCropSize(border); +} + +template +void +ImagXRawToAttenuationImageFilter +::GenerateOutputInformation() +{ + m_CropFilter->SetInput(this->GetInput() ); + m_LookupTableFilter->UpdateOutputInformation(); + this->GetOutput()->SetOrigin( m_LookupTableFilter->GetOutput()->GetOrigin() ); + this->GetOutput()->SetSpacing( m_LookupTableFilter->GetOutput()->GetSpacing() ); + this->GetOutput()->SetDirection( m_LookupTableFilter->GetOutput()->GetDirection() ); + this->GetOutput()->SetLargestPossibleRegion( m_LookupTableFilter->GetOutput()->GetLargestPossibleRegion() ); +} + +template +void +ImagXRawToAttenuationImageFilter +::GenerateData() +{ + m_CropFilter->SetInput(this->GetInput() ); + m_LookupTableFilter->Update(); + this->GraftOutput( m_LookupTableFilter->GetOutput() ); +} + +} // end namespace rtk +#endif diff --git a/common/rtkImagXXMLFileReader.cxx b/common/rtkImagXXMLFileReader.cxx new file mode 100644 index 0000000..f415f0d --- /dev/null +++ b/common/rtkImagXXMLFileReader.cxx @@ -0,0 +1,109 @@ +/*========================================================================= + * + * 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 "rtkImagXXMLFileReader.h" +#include "itkMacro.h" + +#include +#include + +namespace rtk +{ + +int +ImagXXMLFileReader:: +CanReadFile(const char *name) +{ + if(!itksys::SystemTools::FileExists(name) || + itksys::SystemTools::FileIsDirectory(name) || + itksys::SystemTools::FileLength(name) == 0) + return 0; + return 1; +} + +void +ImagXXMLFileReader:: +StartElement(const char * name, const char ** atts) +{ +#define ENCAPLULATE_META_DATA_INT(metaName) \ + if(itksys::SystemTools::Strucmp(atts[i], metaName) == 0) { \ + double d = atof(atts[i+1]); \ + itk::EncapsulateMetaData(m_Dictionary, metaName, d); \ + } + +#define ENCAPLULATE_META_DATA_STRING(metaName) \ + if(itksys::SystemTools::Strucmp(atts[i], metaName) == 0) { \ + itk::EncapsulateMetaData(m_Dictionary, metaName, atts[i+1]); \ + } + + if(std::string(name) == std::string("image") ) + { + for(int i=0; atts[i] != NULL; i+=2) + { + ENCAPLULATE_META_DATA_STRING("name"); + ENCAPLULATE_META_DATA_INT("bitDepth"); + ENCAPLULATE_META_DATA_STRING("pixelFormat"); + ENCAPLULATE_META_DATA_STRING("byteOrder"); + ENCAPLULATE_META_DATA_STRING("modality"); + ENCAPLULATE_META_DATA_STRING("matrixTransform"); + ENCAPLULATE_META_DATA_INT("dimensions"); + ENCAPLULATE_META_DATA_INT("sequence"); + ENCAPLULATE_META_DATA_STRING("rawFile"); + } + } + if(std::string(name) == std::string("size") ) + { + for(int i=0; atts[i] != NULL; i+=2) + { + ENCAPLULATE_META_DATA_INT("x"); + ENCAPLULATE_META_DATA_INT("y"); + ENCAPLULATE_META_DATA_INT("z"); + } + } + if(std::string(name) == std::string("spacing") ) + { +#define ENCAPLULATE_META_DATA_DOUBLE(metaName) \ + if(itksys::SystemTools::Strucmp(atts[i], metaName) == 0) { \ + double d = atof(atts[i+1]); \ + itk::EncapsulateMetaData(m_Dictionary, std::string("spacing_") + std::string(metaName), d); \ + } + for(int i=0; atts[i] != NULL; i+=2) + { + ENCAPLULATE_META_DATA_DOUBLE("x"); + ENCAPLULATE_META_DATA_DOUBLE("y"); + ENCAPLULATE_META_DATA_DOUBLE("z"); + } + } + m_CurCharacterData = ""; +} + +void +ImagXXMLFileReader:: +EndElement(const char *name) +{ +} + +void +ImagXXMLFileReader:: +CharacterDataHandler(const char *inData, int inLength) +{ + for(int i = 0; i < inLength; i++) + m_CurCharacterData = m_CurCharacterData + inData[i]; +} + +} diff --git a/common/rtkImagXXMLFileReader.h b/common/rtkImagXXMLFileReader.h new file mode 100644 index 0000000..1a948e2 --- /dev/null +++ b/common/rtkImagXXMLFileReader.h @@ -0,0 +1,75 @@ +/*========================================================================= + * + * 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 __rtkImagXXMLFileReader_h +#define __rtkImagXXMLFileReader_h + +#ifdef _MSC_VER +#pragma warning ( disable : 4786 ) +#endif + +#include +#include + +#include + +namespace rtk +{ + +/** \class ImagXXMLFileReader + * + * TODO + * + */ +class ImagXXMLFileReader : public itk::XMLReader +{ +public: + /** Standard typedefs */ + typedef ImagXXMLFileReader Self; + typedef itk::XMLReader Superclass; + typedef itk::SmartPointer Pointer; + + /** Run-time type information (and related methods). */ + itkTypeMacro(ImagXXMLFileReader, itk::XMLReader); + + /** Method for creation through the object factory. */ + itkNewMacro(Self); + + /** Determine if a file can be read */ + int CanReadFile(const char* name); + +protected: + ImagXXMLFileReader() {m_OutputObject = &m_Dictionary;} + virtual ~ImagXXMLFileReader() {} + + virtual void StartElement(const char * name,const char **atts); + + virtual void EndElement(const char *name); + + void CharacterDataHandler(const char *inData, int inLength); + +private: + ImagXXMLFileReader(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + + itk::MetaDataDictionary m_Dictionary; + std::string m_CurCharacterData; +}; + +} +#endif -- 2.45.1