From 138d9b4ae5336543a9afc3b391b30f8398be3421 Mon Sep 17 00:00:00 2001 From: Simon Rit Date: Tue, 13 Nov 2012 17:27:35 +0100 Subject: [PATCH] Copied Hnd processing from rtk and use rtk file names to emphasize that they are comming from rtk. --- common/CMakeLists.txt | 4 +- common/clitkHndImageIO.cxx | 237 ---------------------------- common/clitkHndImageIO.h | 137 ---------------- common/clitkHndImageIOFactory.cxx | 29 ---- common/clitkHndImageIOFactory.h | 76 --------- common/clitkIO.cxx | 4 +- common/rtkHndImageIO.cxx | 250 ++++++++++++++++++++++++++++++ common/rtkHndImageIO.h | 143 +++++++++++++++++ common/rtkHndImageIOFactory.cxx | 31 ++++ common/rtkHndImageIOFactory.h | 83 ++++++++++ 10 files changed, 511 insertions(+), 483 deletions(-) delete mode 100644 common/clitkHndImageIO.cxx delete mode 100644 common/clitkHndImageIO.h delete mode 100644 common/clitkHndImageIOFactory.cxx delete mode 100644 common/clitkHndImageIOFactory.h create mode 100644 common/rtkHndImageIO.cxx create mode 100644 common/rtkHndImageIO.h create mode 100644 common/rtkHndImageIOFactory.cxx create mode 100644 common/rtkHndImageIOFactory.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index d895560..a85fda6 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -20,8 +20,8 @@ SET(clitkCommon_SRC clitkXdrImageIOReader.cxx clitkXdrImageIOWriter.cxx clitkXdrImageIOFactory.cxx - clitkHndImageIO.cxx - clitkHndImageIOFactory.cxx + rtkHndImageIO.cxx + rtkHndImageIOFactory.cxx clitkDicomRTDoseIO.cxx clitkDicomRTDoseIOFactory.cxx clitkOrientation.cxx diff --git a/common/clitkHndImageIO.cxx b/common/clitkHndImageIO.cxx deleted file mode 100644 index 44486c2..0000000 --- a/common/clitkHndImageIO.cxx +++ /dev/null @@ -1,237 +0,0 @@ -/*========================================================================= - 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 -===========================================================================**/ - -// std include -#include - -// clitk include -#include "clitkHndImageIO.h" -#include "clitkCommon.h" - -// itk include -#include - -//-------------------------------------------------------------------- -// Read Image Information -void clitk::HndImageIO::ReadImageInformation() -{ - Hnd_header hnd; - FILE *fp; - - fp = fopen (m_FileName.c_str(), "rb"); - if (fp == NULL) - itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName); - - fread ((void *) hnd.sFileType, sizeof(char), 32, fp); - fread ((void *) &hnd.FileLength, sizeof(uint32_t), 1, fp); - fread ((void *) hnd.sChecksumSpec, sizeof(char), 4, fp); - fread ((void *) &hnd.nCheckSum, sizeof(uint32_t), 1, fp); - fread ((void *) hnd.sCreationDate, sizeof(char), 8, fp); - fread ((void *) hnd.sCreationTime, sizeof(char), 8, fp); - fread ((void *) hnd.sPatientID, sizeof(char), 16, fp); - fread ((void *) &hnd.nPatientSer, sizeof(uint32_t), 1, fp); - fread ((void *) hnd.sSeriesID, sizeof(char), 16, fp); - fread ((void *) &hnd.nSeriesSer, sizeof(uint32_t), 1, fp); - fread ((void *) hnd.sSliceID, sizeof(char), 16, fp); - fread ((void *) &hnd.nSliceSer, sizeof(uint32_t), 1, fp); - fread ((void *) &hnd.SizeX, sizeof(uint32_t), 1, fp); - fread ((void *) &hnd.SizeY, sizeof(uint32_t), 1, fp); - fread ((void *) &hnd.dSliceZPos, sizeof(double), 1, fp); - fread ((void *) hnd.sModality, sizeof(char), 16, fp); - fread ((void *) &hnd.nWindow, sizeof(uint32_t), 1, fp); - fread ((void *) &hnd.nLevel, sizeof(uint32_t), 1, fp); - fread ((void *) &hnd.nPixelOffset, sizeof(uint32_t), 1, fp); - fread ((void *) hnd.sImageType, sizeof(char), 4, fp); - fread ((void *) &hnd.dGantryRtn, sizeof(double), 1, fp); - fread ((void *) &hnd.dSAD, sizeof(double), 1, fp); - fread ((void *) &hnd.dSFD, sizeof(double), 1, fp); - fread ((void *) &hnd.dCollX1, sizeof(double), 1, fp); - fread ((void *) &hnd.dCollX2, sizeof(double), 1, fp); - fread ((void *) &hnd.dCollY1, sizeof(double), 1, fp); - fread ((void *) &hnd.dCollY2, sizeof(double), 1, fp); - fread ((void *) &hnd.dCollRtn, sizeof(double), 1, fp); - fread ((void *) &hnd.dFieldX, sizeof(double), 1, fp); - fread ((void *) &hnd.dFieldY, sizeof(double), 1, fp); - fread ((void *) &hnd.dBladeX1, sizeof(double), 1, fp); - fread ((void *) &hnd.dBladeX2, sizeof(double), 1, fp); - fread ((void *) &hnd.dBladeY1, sizeof(double), 1, fp); - fread ((void *) &hnd.dBladeY2, sizeof(double), 1, fp); - fread ((void *) &hnd.dIDUPosLng, sizeof(double), 1, fp); - fread ((void *) &hnd.dIDUPosLat, sizeof(double), 1, fp); - fread ((void *) &hnd.dIDUPosVrt, sizeof(double), 1, fp); - fread ((void *) &hnd.dIDUPosRtn, sizeof(double), 1, fp); - fread ((void *) &hnd.dPatientSupportAngle, sizeof(double), 1, fp); - fread ((void *) &hnd.dTableTopEccentricAngle, sizeof(double), 1, fp); - fread ((void *) &hnd.dCouchVrt, sizeof(double), 1, fp); - fread ((void *) &hnd.dCouchLng, sizeof(double), 1, fp); - fread ((void *) &hnd.dCouchLat, sizeof(double), 1, fp); - fread ((void *) &hnd.dIDUResolutionX, sizeof(double), 1, fp); - fread ((void *) &hnd.dIDUResolutionY, sizeof(double), 1, fp); - fread ((void *) &hnd.dImageResolutionX, sizeof(double), 1, fp); - fread ((void *) &hnd.dImageResolutionY, sizeof(double), 1, fp); - fread ((void *) &hnd.dEnergy, sizeof(double), 1, fp); - fread ((void *) &hnd.dDoseRate, sizeof(double), 1, fp); - fread ((void *) &hnd.dXRayKV, sizeof(double), 1, fp); - fread ((void *) &hnd.dXRayMA, sizeof(double), 1, fp); - fread ((void *) &hnd.dMetersetExposure, sizeof(double), 1, fp); - fread ((void *) &hnd.dAcqAdjustment, sizeof(double), 1, fp); - fread ((void *) &hnd.dCTProjectionAngle, sizeof(double), 1, fp); - fread ((void *) &hnd.dCTNormChamber, sizeof(double), 1, fp); - fread ((void *) &hnd.dGatingTimeTag, sizeof(double), 1, fp); - fread ((void *) &hnd.dGating4DInfoX, sizeof(double), 1, fp); - fread ((void *) &hnd.dGating4DInfoY, sizeof(double), 1, fp); - fread ((void *) &hnd.dGating4DInfoZ, sizeof(double), 1, fp); - fread ((void *) &hnd.dGating4DInfoTime, sizeof(double), 1, fp); - fclose (fp); - - /* Convert hnd to ITK image information */ - SetNumberOfDimensions(2); - SetDimensions(0, hnd.SizeX); - SetDimensions(1, hnd.SizeY); - SetSpacing(0, hnd.dIDUResolutionX); - SetSpacing(1, hnd.dIDUResolutionY); - SetOrigin(0, -0.5*(hnd.SizeX-1)*hnd.dIDUResolutionX); //SR: assumed centered - SetOrigin(1, -0.5*(hnd.SizeY-1)*hnd.dIDUResolutionY); //SR: assumed centered - SetComponentType(itk::ImageIOBase::UINT); - - /* Store important meta information in the meta data dictionary */ - itk::EncapsulateMetaData(this->GetMetaDataDictionary(), "dCTProjectionAngle", hnd.dCTProjectionAngle); - //itk::ExposeMetaData( this->GetMetaDataDictionary(), &(hnd.dCTProjectionAngle), "dCTProjectionAngle"); -} - -//-------------------------------------------------------------------- -bool clitk::HndImageIO::CanReadFile(const char* FileNameToRead) -{ - std::string filename(FileNameToRead); - std::string filenameext = GetExtension(filename); - if (filenameext != std::string("hnd")) return false; - return true; -} - -//-------------------------------------------------------------------- -// Read Image Content -void clitk::HndImageIO::Read(void * buffer) -{ - FILE *fp; - - uint32_t* buf = (uint32_t*)buffer; - unsigned char *pt_lut; - uint32_t a; - unsigned char v; - int lut_idx, lut_off; - size_t num_read; - char dc; - short ds; - long dl, diff=0; - uint32_t i; - - fp = fopen (m_FileName.c_str(), "rb"); - if (fp == NULL) - itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName); - - pt_lut = (unsigned char*) malloc (sizeof (unsigned char) * GetDimensions(0) * GetDimensions(1)); - - /* Read LUT */ - fseek (fp, 1024, SEEK_SET); - fread (pt_lut, sizeof(unsigned char), (GetDimensions(1)-1)*GetDimensions(0) / 4, fp); - - /* Read first row */ - for (i = 0; i < GetDimensions(0); i++) { - fread (&a, sizeof(uint32_t), 1, fp); - buf[i] = a; - } - - /* Read first pixel of second row */ - fread (&a, sizeof(uint32_t), 1, fp); - buf[i++] = a; - - /* Decompress the rest */ - lut_idx = 0; - lut_off = 0; - while (i < GetDimensions(0) * GetDimensions(1)) { - uint32_t r11, r12, r21; - - r11 = buf[i-GetDimensions(0)-1]; - r12 = buf[i-GetDimensions(0)]; - r21 = buf[i-1]; - v = pt_lut[lut_idx]; - switch (lut_off) { - case 0: - v = v & 0x03; - lut_off ++; - break; - case 1: - v = (v & 0x0C) >> 2; - lut_off ++; - break; - case 2: - v = (v & 0x30) >> 4; - lut_off ++; - break; - case 3: - v = (v & 0xC0) >> 6; - lut_off = 0; - lut_idx ++; - break; - } - switch (v) { - case 0: - num_read = fread (&dc, sizeof(unsigned char), 1, fp); - if (num_read != 1) goto read_error; - diff = dc; - break; - case 1: - num_read = fread (&ds, sizeof(unsigned short), 1, fp); - if (num_read != 1) goto read_error; - diff = ds; - break; - case 2: - num_read = fread (&dl, sizeof(uint32_t), 1, fp); - if (num_read != 1) goto read_error; - diff = dl; - break; - } - - buf[i] = r21 + r12 + diff - r11; - i++; - } - - /* Clean up */ - free (pt_lut); - fclose (fp); - return; - -read_error: - - itkGenericExceptionMacro(<< "Error reading hnd file"); - free (pt_lut); - fclose (fp); - return; -} - -//-------------------------------------------------------------------- -bool clitk::HndImageIO::CanWriteFile(const char* FileNameToWrite) -{ - return false; -} - -//-------------------------------------------------------------------- -// Write Image -void clitk::HndImageIO::Write(const void* buffer) -{ -} diff --git a/common/clitkHndImageIO.h b/common/clitkHndImageIO.h deleted file mode 100644 index 05fc1bd..0000000 --- a/common/clitkHndImageIO.h +++ /dev/null @@ -1,137 +0,0 @@ -/*========================================================================= - 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 CLITKHNDIMAGEIO_H -#define CLITKHNDIMAGEIO_H - -// itk include -#include "itkImageIOBase.h" - -#if defined (_MSC_VER) && (_MSC_VER < 1600) -//SR: taken from -//#include "msinttypes/stdint.h" -typedef unsigned int uint32_t; -#else -#include -#endif - -namespace clitk { - - //==================================================================== - // Class for reading Hnd Image file format - class HndImageIO: public itk::ImageIOBase - { - public: - /** Standard class typedefs. */ - typedef HndImageIO Self; - typedef itk::ImageIOBase Superclass; - typedef itk::SmartPointer Pointer; - typedef signed short int PixelType; - - typedef struct hnd_header { - char sFileType[32]; - uint32_t FileLength; - char sChecksumSpec[4]; - uint32_t nCheckSum; - char sCreationDate[8]; - char sCreationTime[8]; - char sPatientID[16]; - uint32_t nPatientSer; - char sSeriesID[16]; - uint32_t nSeriesSer; - char sSliceID[16]; - uint32_t nSliceSer; - uint32_t SizeX; - uint32_t SizeY; - double dSliceZPos; - char sModality[16]; - uint32_t nWindow; - uint32_t nLevel; - uint32_t nPixelOffset; - char sImageType[4]; - double dGantryRtn; - double dSAD; - double dSFD; - double dCollX1; - double dCollX2; - double dCollY1; - double dCollY2; - double dCollRtn; - double dFieldX; - double dFieldY; - double dBladeX1; - double dBladeX2; - double dBladeY1; - double dBladeY2; - double dIDUPosLng; - double dIDUPosLat; - double dIDUPosVrt; - double dIDUPosRtn; - double dPatientSupportAngle; - double dTableTopEccentricAngle; - double dCouchVrt; - double dCouchLng; - double dCouchLat; - double dIDUResolutionX; - double dIDUResolutionY; - double dImageResolutionX; - double dImageResolutionY; - double dEnergy; - double dDoseRate; - double dXRayKV; - double dXRayMA; - double dMetersetExposure; - double dAcqAdjustment; - double dCTProjectionAngle; - double dCTNormChamber; - double dGatingTimeTag; - double dGating4DInfoX; - double dGating4DInfoY; - double dGating4DInfoZ; - double dGating4DInfoTime; - } Hnd_header; - - HndImageIO():Superclass() {;} - - /** Method for creation through the object factory. */ - itkNewMacro(Self); - - /** Run-time type information (and related methods). */ - itkTypeMacro(HndImageIO, 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: - int m_HeaderSize; - - }; // end class HndImageIO -} // end namespace - -// explicit template instantiation -//template class itk::CreateObjectFunction; - -#endif /* end #define CLITKHNDIMAGEIO_H */ - diff --git a/common/clitkHndImageIOFactory.cxx b/common/clitkHndImageIOFactory.cxx deleted file mode 100644 index e21a4f1..0000000 --- a/common/clitkHndImageIOFactory.cxx +++ /dev/null @@ -1,29 +0,0 @@ -/*========================================================================= - 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 "clitkHndImageIOFactory.h" - -//==================================================================== -clitk::HndImageIOFactory::HndImageIOFactory() -{ - this->RegisterOverride("itkImageIOBase", - "HndImageIO", - "Hnd Image IO", - 1, - itk::CreateObjectFunction::New()); -} diff --git a/common/clitkHndImageIOFactory.h b/common/clitkHndImageIOFactory.h deleted file mode 100644 index 8b87dc7..0000000 --- a/common/clitkHndImageIOFactory.h +++ /dev/null @@ -1,76 +0,0 @@ -/*========================================================================= - 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 CLITKHNDIMAGEIOFACTORY_H -#define CLITKHNDIMAGEIOFACTORY_H - -// clitk include -#include "clitkHndImageIO.h" - -// itk include -#include "itkImageIOBase.h" -#include "itkObjectFactoryBase.h" -#include "itkVersion.h" - -namespace clitk { - - //==================================================================== - // Factory for reading Hnd Image file format - class HndImageIOFactory: public itk::ObjectFactoryBase - { - public: - /** Standard class typedefs. */ - typedef HndImageIOFactory 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 "Hnd ImageIO Factory, allows the loading of Hnd images into insight"; - } - - /** Method for class instantiation. */ - itkFactorylessNewMacro(Self); - - /** Run-time type information (and related methods). */ - itkTypeMacro(HndImageIOFactory, ObjectFactoryBase); - - /** Register one factory of this type */ - static void RegisterOneFactory(void) { - ObjectFactoryBase::RegisterFactory( Self::New() ); - } - - protected: - HndImageIOFactory(); - ~HndImageIOFactory() {}; - typedef HndImageIOFactory myProductType; - const myProductType* m_MyProduct; - - private: - HndImageIOFactory(const Self&); //purposely not implemented - void operator=(const Self&); //purposely not implemented - }; - -} // end namespace - -#endif /* end #define CLITKHNDIMAGEIOFACTORY_H */ - diff --git a/common/clitkIO.cxx b/common/clitkIO.cxx index 79aa958..806a812 100644 --- a/common/clitkIO.cxx +++ b/common/clitkIO.cxx @@ -31,7 +31,7 @@ #include "clitkHisImageIOFactory.h" #include "clitkVfImageIOFactory.h" #include "clitkXdrImageIOFactory.h" -#include "clitkHndImageIOFactory.h" +#include "rtkHndImageIOFactory.h" #include "clitkGateAsciiImageIOFactory.h" #include "clitkConfiguration.h" #if CLITK_PRIVATE_FEATURES @@ -58,6 +58,6 @@ void clitk::RegisterClitkFactories() clitk::VfImageIOFactory::RegisterOneFactory(); clitk::HisImageIOFactory::RegisterOneFactory(); clitk::XdrImageIOFactory::RegisterOneFactory(); - clitk::HndImageIOFactory::RegisterOneFactory(); + rtk::HndImageIOFactory::RegisterOneFactory(); } //// diff --git a/common/rtkHndImageIO.cxx b/common/rtkHndImageIO.cxx new file mode 100644 index 0000000..8d59982 --- /dev/null +++ b/common/rtkHndImageIO.cxx @@ -0,0 +1,250 @@ +/*========================================================================= + * + * 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. + * + *=========================================================================*/ + +// std include +#include + +#include "rtkHndImageIO.h" +#include + +//-------------------------------------------------------------------- +// Read Image Information +void rtk::HndImageIO::ReadImageInformation() +{ + Hnd_header hnd; + FILE * fp; + + fp = fopen (m_FileName.c_str(), "rb"); + if (fp == NULL) + itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName); + + size_t nelements = 0; + nelements += fread ( (void *) hnd.sFileType, sizeof(char), 32, fp); + nelements += fread ( (void *) &hnd.FileLength, sizeof(uint32_t), 1, fp); + nelements += fread ( (void *) hnd.sChecksumSpec, sizeof(char), 4, fp); + nelements += fread ( (void *) &hnd.nCheckSum, sizeof(uint32_t), 1, fp); + nelements += fread ( (void *) hnd.sCreationDate, sizeof(char), 8, fp); + nelements += fread ( (void *) hnd.sCreationTime, sizeof(char), 8, fp); + nelements += fread ( (void *) hnd.sPatientID, sizeof(char), 16, fp); + nelements += fread ( (void *) &hnd.nPatientSer, sizeof(uint32_t), 1, fp); + nelements += fread ( (void *) hnd.sSeriesID, sizeof(char), 16, fp); + nelements += fread ( (void *) &hnd.nSeriesSer, sizeof(uint32_t), 1, fp); + nelements += fread ( (void *) hnd.sSliceID, sizeof(char), 16, fp); + nelements += fread ( (void *) &hnd.nSliceSer, sizeof(uint32_t), 1, fp); + nelements += fread ( (void *) &hnd.SizeX, sizeof(uint32_t), 1, fp); + nelements += fread ( (void *) &hnd.SizeY, sizeof(uint32_t), 1, fp); + nelements += fread ( (void *) &hnd.dSliceZPos, sizeof(double), 1, fp); + nelements += fread ( (void *) hnd.sModality, sizeof(char), 16, fp); + nelements += fread ( (void *) &hnd.nWindow, sizeof(uint32_t), 1, fp); + nelements += fread ( (void *) &hnd.nLevel, sizeof(uint32_t), 1, fp); + nelements += fread ( (void *) &hnd.nPixelOffset, sizeof(uint32_t), 1, fp); + nelements += fread ( (void *) hnd.sImageType, sizeof(char), 4, fp); + nelements += fread ( (void *) &hnd.dGantryRtn, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dSAD, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dSFD, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dCollX1, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dCollX2, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dCollY1, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dCollY2, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dCollRtn, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dFieldX, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dFieldY, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dBladeX1, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dBladeX2, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dBladeY1, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dBladeY2, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dIDUPosLng, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dIDUPosLat, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dIDUPosVrt, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dIDUPosRtn, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dPatientSupportAngle, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dTableTopEccentricAngle, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dCouchVrt, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dCouchLng, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dCouchLat, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dIDUResolutionX, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dIDUResolutionY, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dImageResolutionX, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dImageResolutionY, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dEnergy, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dDoseRate, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dXRayKV, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dXRayMA, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dMetersetExposure, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dAcqAdjustment, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dCTProjectionAngle, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dCTNormChamber, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dGatingTimeTag, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dGating4DInfoX, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dGating4DInfoY, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dGating4DInfoZ, sizeof(double), 1, fp); + nelements += fread ( (void *) &hnd.dGating4DInfoTime, sizeof(double), 1, fp); + + if(nelements != /*char*/120 + /*uint32_t*/10 + /*double*/41) + itkGenericExceptionMacro(<< "Could not read header data in " << m_FileName); + + if(fclose (fp) != 0) + itkGenericExceptionMacro(<< "Could not close file: " << m_FileName); + + /* Convert hnd to ITK image information */ + SetNumberOfDimensions(2); + SetDimensions(0, hnd.SizeX); + SetDimensions(1, hnd.SizeY); + SetSpacing(0, hnd.dIDUResolutionX); + SetSpacing(1, hnd.dIDUResolutionY); + SetOrigin(0, -0.5*(hnd.SizeX-1)*hnd.dIDUResolutionX); //SR: assumed centered + SetOrigin(1, -0.5*(hnd.SizeY-1)*hnd.dIDUResolutionY); //SR: assumed centered + SetComponentType(itk::ImageIOBase::UINT); + + /* Store important meta information in the meta data dictionary */ + itk::EncapsulateMetaData(this->GetMetaDataDictionary(), "dCTProjectionAngle", hnd.dCTProjectionAngle); +} + +//-------------------------------------------------------------------- +bool rtk::HndImageIO::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("hnd") ) + return false; + return true; +} + +//-------------------------------------------------------------------- +// Read Image Content +void rtk::HndImageIO::Read(void * buffer) +{ + FILE *fp; + + uint32_t* buf = (uint32_t*)buffer; + unsigned char *pt_lut; + uint32_t a; + unsigned char v; + int lut_idx, lut_off; + size_t num_read; + char dc; + short ds; + long dl, diff=0; + uint32_t i; + + fp = fopen (m_FileName.c_str(), "rb"); + if (fp == NULL) + itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName); + + pt_lut = (unsigned char*) malloc (sizeof (unsigned char) * GetDimensions(0) * GetDimensions(1) ); + + /* Read LUT */ + if(fseek (fp, 1024, SEEK_SET) != 0) + itkGenericExceptionMacro(<< "Could not seek to image data in: " << m_FileName); + + size_t nbytes = (GetDimensions(1)-1)*GetDimensions(0) / 4; + if(nbytes != fread (pt_lut, sizeof(unsigned char), nbytes, fp)) + itkGenericExceptionMacro(<< "Could not read image LUT in: " << m_FileName); + + /* Read first row */ + for (i = 0; i < GetDimensions(0); i++) { + if(1 != fread (&a, sizeof(uint32_t), 1, fp)) + itkGenericExceptionMacro(<< "Could not read first row in: " << m_FileName); + buf[i] = a; + } + + /* Read first pixel of second row */ + if(1 != fread (&a, sizeof(uint32_t), 1, fp)) + itkGenericExceptionMacro(<< "Could not read first pixel of second row"); + buf[i++] = a; + + /* Decompress the rest */ + lut_idx = 0; + lut_off = 0; + while (i < GetDimensions(0) * GetDimensions(1) ) { + uint32_t r11, r12, r21; + + r11 = buf[i-GetDimensions(0)-1]; + r12 = buf[i-GetDimensions(0)]; + r21 = buf[i-1]; + v = pt_lut[lut_idx]; + switch (lut_off) { + case 0: + v = v & 0x03; + lut_off++; + break; + case 1: + v = (v & 0x0C) >> 2; + lut_off++; + break; + case 2: + v = (v & 0x30) >> 4; + lut_off++; + break; + case 3: + v = (v & 0xC0) >> 6; + lut_off = 0; + lut_idx++; + break; + } + switch (v) { + case 0: + num_read = fread (&dc, sizeof(unsigned char), 1, fp); + if (num_read != 1) goto read_error; + diff = dc; + break; + case 1: + num_read = fread (&ds, sizeof(unsigned short), 1, fp); + if (num_read != 1) goto read_error; + diff = ds; + break; + case 2: + num_read = fread (&dl, sizeof(uint32_t), 1, fp); + if (num_read != 1) goto read_error; + diff = dl; + break; + } + + buf[i] = r21 + r12 + diff - r11; + i++; + } + + /* Clean up */ + free (pt_lut); + if(fclose (fp) != 0) + itkGenericExceptionMacro(<< "Could not close file: " << m_FileName); + return; + +read_error: + + itkGenericExceptionMacro(<< "Error reading hnd file"); + free (pt_lut); + if(fclose (fp) != 0) + itkGenericExceptionMacro(<< "Could not close file: " << m_FileName); + return; +} + +//-------------------------------------------------------------------- +bool rtk::HndImageIO::CanWriteFile(const char* itkNotUsed(FileNameToWrite)) +{ + return false; +} + +//-------------------------------------------------------------------- +// Write Image +void rtk::HndImageIO::Write(const void* itkNotUsed(buffer)) +{ + //TODO? +} diff --git a/common/rtkHndImageIO.h b/common/rtkHndImageIO.h new file mode 100644 index 0000000..6f32ec9 --- /dev/null +++ b/common/rtkHndImageIO.h @@ -0,0 +1,143 @@ +/*========================================================================= + * + * 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 __rtkHndImageIO_h +#define __rtkHndImageIO_h + +// itk include +#include + +#if defined (_MSC_VER) && (_MSC_VER < 1600) +//SR: taken from +//#include "msinttypes/stdint.h" +#else +#include +#endif + +namespace rtk { + +/** \class HndImageIO + * \brief Class for reading Hnd Image file format + * + * Reads Hnd files (file format used by Varian for Obi raw data). + * + * \author Simon Rit + * + * \ingroup IOFilters + */ +class HndImageIO : public itk::ImageIOBase +{ +public: +/** Standard class typedefs. */ + typedef HndImageIO Self; + typedef itk::ImageIOBase Superclass; + typedef itk::SmartPointer Pointer; + typedef signed short int PixelType; + + typedef struct hnd_header { + char sFileType[32]; + unsigned int FileLength; + char sChecksumSpec[4]; + unsigned int nCheckSum; + char sCreationDate[8]; + char sCreationTime[8]; + char sPatientID[16]; + unsigned int nPatientSer; + char sSeriesID[16]; + unsigned int nSeriesSer; + char sSliceID[16]; + unsigned int nSliceSer; + unsigned int SizeX; + unsigned int SizeY; + double dSliceZPos; + char sModality[16]; + unsigned int nWindow; + unsigned int nLevel; + unsigned int nPixelOffset; + char sImageType[4]; + double dGantryRtn; + double dSAD; + double dSFD; + double dCollX1; + double dCollX2; + double dCollY1; + double dCollY2; + double dCollRtn; + double dFieldX; + double dFieldY; + double dBladeX1; + double dBladeX2; + double dBladeY1; + double dBladeY2; + double dIDUPosLng; + double dIDUPosLat; + double dIDUPosVrt; + double dIDUPosRtn; + double dPatientSupportAngle; + double dTableTopEccentricAngle; + double dCouchVrt; + double dCouchLng; + double dCouchLat; + double dIDUResolutionX; + double dIDUResolutionY; + double dImageResolutionX; + double dImageResolutionY; + double dEnergy; + double dDoseRate; + double dXRayKV; + double dXRayMA; + double dMetersetExposure; + double dAcqAdjustment; + double dCTProjectionAngle; + double dCTNormChamber; + double dGatingTimeTag; + double dGating4DInfoX; + double dGating4DInfoY; + double dGating4DInfoZ; + double dGating4DInfoTime; + } Hnd_header; + + HndImageIO() : Superclass() {} + + /** Method for creation through the object factory. */ + itkNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkTypeMacro(HndImageIO, itk::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); + +}; // end class HndImageIO + +} // end namespace + +#endif /* end #define __rtkHndImageIO_h */ diff --git a/common/rtkHndImageIOFactory.cxx b/common/rtkHndImageIOFactory.cxx new file mode 100644 index 0000000..8094adf --- /dev/null +++ b/common/rtkHndImageIOFactory.cxx @@ -0,0 +1,31 @@ +/*========================================================================= + * + * 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 "rtkHndImageIOFactory.h" + +#include + +//==================================================================== +rtk::HndImageIOFactory::HndImageIOFactory() +{ + this->RegisterOverride("itkImageIOBase", + "HndImageIO", + "Hnd Image IO", + 1, + itk::CreateObjectFunction::New() ); +} diff --git a/common/rtkHndImageIOFactory.h b/common/rtkHndImageIOFactory.h new file mode 100644 index 0000000..a410e62 --- /dev/null +++ b/common/rtkHndImageIOFactory.h @@ -0,0 +1,83 @@ +/*========================================================================= + * + * 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 ITKHNDIMAGEIOFACTORY_H +//#define ITKHNDIMAGEIOFACTORY_H + +#ifndef __rtkHndImageIOFactory_h +#define __rtkHndImageIOFactory_h + +#include "rtkHndImageIO.h" + +// itk include +#include +#include +#include + +namespace rtk +{ + +/** \class HndImageIOFactory + * \brief ITK factory for Hnd file I/O. + * + * \author Simon Rit + */ +class HndImageIOFactory : public itk::ObjectFactoryBase +{ +public: + /** Standard class typedefs. */ + typedef HndImageIOFactory 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 "His ImageIO Factory, allows the loading of His images into insight"; + } + + /** Method for class instantiation. */ + itkFactorylessNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkTypeMacro(HndImageIOFactory, itk::ObjectFactoryBase); + + /** Register one factory of this type */ + static void RegisterOneFactory(void) { + ObjectFactoryBase::RegisterFactory( Self::New() ); + } + +protected: + HndImageIOFactory(); + ~HndImageIOFactory() {}; + typedef HndImageIOFactory myProductType; + const myProductType* m_MyProduct; + +private: + HndImageIOFactory(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + +}; + +} // end namespace + +#endif // __rtkHndImageIOFactory_h -- 2.47.1