From afb8bc7fe27981af21b4dc69bdbda00c6c33a350 Mon Sep 17 00:00:00 2001 From: pierre gueth Date: Thu, 20 Oct 2011 16:32:39 +0200 Subject: [PATCH] started io support for gate ascii file --- common/CMakeLists.txt | 2 + common/clitkGateAsciiImageIO.cxx | 175 ++++++++++++++++++++++++ common/clitkGateAsciiImageIO.h | 81 +++++++++++ common/clitkGateAsciiImageIOFactory.cxx | 29 ++++ common/clitkGateAsciiImageIOFactory.h | 76 ++++++++++ common/clitkIO.cxx | 2 + 6 files changed, 365 insertions(+) create mode 100644 common/clitkGateAsciiImageIO.cxx create mode 100644 common/clitkGateAsciiImageIO.h create mode 100644 common/clitkGateAsciiImageIOFactory.cxx create mode 100644 common/clitkGateAsciiImageIOFactory.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 9f92d71..ec826a2 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -24,6 +24,8 @@ SET(clitkCommon_SRC clitkXdrImageIOFactory.cxx clitkHndImageIO.cxx clitkHndImageIOFactory.cxx + clitkGateAsciiImageIO.cxx + clitkGateAsciiImageIOFactory.cxx clitkDicomRTDoseIO.cxx clitkDicomRTDoseIOFactory.cxx clitkOrientation.cxx diff --git a/common/clitkGateAsciiImageIO.cxx b/common/clitkGateAsciiImageIO.cxx new file mode 100644 index 0000000..aa785f6 --- /dev/null +++ b/common/clitkGateAsciiImageIO.cxx @@ -0,0 +1,175 @@ +/*========================================================================= + 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 "clitkGateAsciiImageIO.h" +#include "clitkCommon.h" + +// itk include +#include + +//-------------------------------------------------------------------- +// Read Image Information +void clitk::GateAsciiImageIO::ReadImageInformation() +{ + itkGenericExceptionMacro(<< "Could not open file (for reading): " << 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); + //itk::ExposeMetaData( this->GetMetaDataDictionary(), &(hnd.dCTProjectionAngle), "dCTProjectionAngle"); +} + +//-------------------------------------------------------------------- +bool clitk::GateAsciiImageIO::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::GateAsciiImageIO::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 gate ascii file"); + free (pt_lut); + fclose (fp); + return; +} + +//-------------------------------------------------------------------- +bool clitk::GateAsciiImageIO::CanWriteFile(const char* FileNameToWrite) +{ + return false; +} + +//-------------------------------------------------------------------- +// Write Image +void clitk::GateAsciiImageIO::Write(const void* buffer) +{ +} diff --git a/common/clitkGateAsciiImageIO.h b/common/clitkGateAsciiImageIO.h new file mode 100644 index 0000000..b7fa18f --- /dev/null +++ b/common/clitkGateAsciiImageIO.h @@ -0,0 +1,81 @@ +/*========================================================================= + 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 CLITKGATEASCIIIMAGEIO_H +#define CLITKGATEASCIIIMAGEIO_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 gate ascii Image file format + class GateAsciiImageIO: public itk::ImageIOBase + { + public: + /** Standard class typedefs. */ + typedef GateAsciiImageIO Self; + typedef itk::ImageIOBase Superclass; + typedef itk::SmartPointer Pointer; + typedef signed short int PixelType; + + struct GateAsciiHeader { + double matrix_size[3]; + int resolution[3]; + double voxel_size[3]; + int nb_value; + }; + + GateAsciiImageIO():Superclass() {;} + + /** Method for creation through the object factory. */ + itkNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkTypeMacro(GateAsciiImageIO, 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 GateAsciiImageIO +} // end namespace + +// explicit template instantiation +//template class itk::CreateObjectFunction; + +#endif /* end #define CLITKGATEASCIIIMAGEIO_H */ + diff --git a/common/clitkGateAsciiImageIOFactory.cxx b/common/clitkGateAsciiImageIOFactory.cxx new file mode 100644 index 0000000..7c78f7d --- /dev/null +++ b/common/clitkGateAsciiImageIOFactory.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://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 "clitkGateAsciiImageIOFactory.h" + +//==================================================================== +clitk::GateAsciiImageIOFactory::GateAsciiImageIOFactory() +{ + this->RegisterOverride("itkImageIOBase", + "GateAsciiImageIO", + "GateAscii Image IO", + 1, + itk::CreateObjectFunction::New()); +} diff --git a/common/clitkGateAsciiImageIOFactory.h b/common/clitkGateAsciiImageIOFactory.h new file mode 100644 index 0000000..3a3b4fe --- /dev/null +++ b/common/clitkGateAsciiImageIOFactory.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://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 CLITKGATEASCIIIMAGEIOFACTORY_H +#define CLITKGATEASCIIIMAGEIOFACTORY_H + +// clitk include +#include "clitkGateAsciiImageIO.h" + +// itk include +#include "itkImageIOBase.h" +#include "itkObjectFactoryBase.h" +#include "itkVersion.h" + +namespace clitk { + + //==================================================================== + // Factory for reading GateAscii Image file format + class GateAsciiImageIOFactory: public itk::ObjectFactoryBase + { + public: + /** Standard class typedefs. */ + typedef GateAsciiImageIOFactory 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 "GateAscii ImageIO Factory, allows the loading of gate ascii images into insight"; + } + + /** Method for class instantiation. */ + itkFactorylessNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkTypeMacro(GateAsciiImageIOFactory, ObjectFactoryBase); + + /** Register one factory of this type */ + static void RegisterOneFactory(void) { + ObjectFactoryBase::RegisterFactory( Self::New() ); + } + + protected: + GateAsciiImageIOFactory(); + ~GateAsciiImageIOFactory() {}; + typedef GateAsciiImageIOFactory myProductType; + const myProductType* m_MyProduct; + + private: + GateAsciiImageIOFactory(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + }; + +} // end namespace + +#endif /* end #define CLITKGATEASCIIIMAGEIOFACTORY_H */ + diff --git a/common/clitkIO.cxx b/common/clitkIO.cxx index d2f7758..1acd352 100644 --- a/common/clitkIO.cxx +++ b/common/clitkIO.cxx @@ -32,11 +32,13 @@ #include "clitkVfImageIOFactory.h" #include "clitkXdrImageIOFactory.h" #include "clitkHndImageIOFactory.h" +#include "clitkGateAsciiImageIOFactory.h" //-------------------------------------------------------------------- // Register factories void clitk::RegisterClitkFactories() { + clitk::GateAsciiImageIOFactory::RegisterOneFactory(); clitk::DicomRTDoseIOFactory::RegisterOneFactory(); #if ITK_VERSION_MAJOR <= 3 itk::ImageIOFactory::RegisterBuiltInFactories(); -- 2.47.1