From 3451be2e38ff6603113fb95f7498e127707de370 Mon Sep 17 00:00:00 2001 From: srit Date: Tue, 13 Jul 2010 09:14:16 +0000 Subject: [PATCH] Added Varian OBI file format --- common/CMakeLists.txt | 2 + common/clitkHndImageIO.cxx | 230 ++++++++++++++++++++++++++++++ common/clitkHndImageIO.h | 135 ++++++++++++++++++ common/clitkHndImageIOFactory.cxx | 29 ++++ common/clitkHndImageIOFactory.h | 76 ++++++++++ common/clitkIO.cxx | 6 +- vv/vvImageReader.txx | 2 + 7 files changed, 476 insertions(+), 4 deletions(-) create mode 100644 common/clitkHndImageIO.cxx create mode 100644 common/clitkHndImageIO.h create mode 100644 common/clitkHndImageIOFactory.cxx create mode 100644 common/clitkHndImageIOFactory.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 19e6671..2dd0659 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -24,6 +24,8 @@ SET(clitkCommon_SRC clitkXdrImageIOReader.cxx clitkXdrImageIOWriter.cxx clitkXdrImageIOFactory.cxx + clitkHndImageIO.cxx + clitkHndImageIOFactory.cxx clitkOrientation.cxx vvImage.cxx clitkImageToImageGenericFilterBase.cxx diff --git a/common/clitkHndImageIO.cxx b/common/clitkHndImageIO.cxx new file mode 100644 index 0000000..85602bc --- /dev/null +++ b/common/clitkHndImageIO.cxx @@ -0,0 +1,230 @@ +/*========================================================================= + 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://oncora1.lyon.fnclcc.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" + +//-------------------------------------------------------------------- +// 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); + SetComponentType(itk::ImageIOBase::UINT); +} + +//-------------------------------------------------------------------- +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; + float b; + 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; + b = a; + } + + /* Read first pixel of second row */ + fread (&a, sizeof(uint32_t), 1, fp); + buf[i++] = a; + b = 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; + b = buf[i]; + 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 new file mode 100644 index 0000000..233d9ac --- /dev/null +++ b/common/clitkHndImageIO.h @@ -0,0 +1,135 @@ +/*========================================================================= + 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://oncora1.lyon.fnclcc.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) +#include "msinttypes/stdint.h" +#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 new file mode 100644 index 0000000..7d54df9 --- /dev/null +++ b/common/clitkHndImageIOFactory.cxx @@ -0,0 +1,29 @@ +/*========================================================================= + 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://oncora1.lyon.fnclcc.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 new file mode 100644 index 0000000..9bcce5b --- /dev/null +++ b/common/clitkHndImageIOFactory.h @@ -0,0 +1,76 @@ +/*========================================================================= + 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://oncora1.lyon.fnclcc.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 39b8514..2c8b9b5 100755 --- a/common/clitkIO.cxx +++ b/common/clitkIO.cxx @@ -36,14 +36,11 @@ // clitk include #include "clitkIO.h" #include "clitkImageCommon.h" -#include "clitkVoxImageIO.h" #include "clitkVoxImageIOFactory.h" -#include "clitkHisImageIO.h" #include "clitkHisImageIOFactory.h" -#include "clitkVfImageIO.h" #include "clitkVfImageIOFactory.h" -#include "clitkXdrImageIO.h" #include "clitkXdrImageIOFactory.h" +#include "clitkHndImageIOFactory.h" //-------------------------------------------------------------------- // Register factories @@ -54,5 +51,6 @@ void clitk::RegisterClitkFactories() clitk::VfImageIOFactory::RegisterOneFactory(); clitk::HisImageIOFactory::RegisterOneFactory(); clitk::XdrImageIOFactory::RegisterOneFactory(); + clitk::HndImageIOFactory::RegisterOneFactory(); } //// diff --git a/vv/vvImageReader.txx b/vv/vvImageReader.txx index de8da61..fd1c575 100644 --- a/vv/vvImageReader.txx +++ b/vv/vvImageReader.txx @@ -46,6 +46,8 @@ void vvImageReader::UpdateWithDim(std::string InputPixelType) UpdateWithDimAndInputPixelType(); else if (InputPixelType == "int") UpdateWithDimAndInputPixelType(); + else if (InputPixelType == "unsigned_int") + UpdateWithDimAndInputPixelType(); else if (InputPixelType == "double") UpdateWithDimAndInputPixelType(); else if (InputPixelType == "float") -- 2.45.1