1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
23 #include "clitkGateAsciiImageIO.h"
24 #include "clitkCommon.h"
27 #include <itkMetaDataObject.h>
29 //--------------------------------------------------------------------
30 // Read Image Information
31 void clitk::GateAsciiImageIO::ReadImageInformation()
33 itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
36 /* Convert hnd to ITK image information */
37 SetNumberOfDimensions(2);
38 //SetDimensions(0, hnd.SizeX);
39 //SetDimensions(1, hnd.SizeY);
40 //SetSpacing(0, hnd.dIDUResolutionX);
41 //SetSpacing(1, hnd.dIDUResolutionY);
42 //SetOrigin(0, -0.5*(hnd.SizeX-1)*hnd.dIDUResolutionX); //SR: assumed centered
43 //SetOrigin(1, -0.5*(hnd.SizeY-1)*hnd.dIDUResolutionY); //SR: assumed centered
44 //SetComponentType(itk::ImageIOBase::UINT);
46 /* Store important meta information in the meta data dictionary */
47 //itk::EncapsulateMetaData<double>(this->GetMetaDataDictionary(), "dCTProjectionAngle", hnd.dCTProjectionAngle);
48 //itk::ExposeMetaData<double>( this->GetMetaDataDictionary(), &(hnd.dCTProjectionAngle), "dCTProjectionAngle");
51 //--------------------------------------------------------------------
52 bool clitk::GateAsciiImageIO::CanReadFile(const char* FileNameToRead)
54 std::string filename(FileNameToRead);
55 std::string filenameext = GetExtension(filename);
56 if (filenameext != std::string("hnd")) return false;
60 //--------------------------------------------------------------------
62 void clitk::GateAsciiImageIO::Read(void * buffer)
66 uint32_t* buf = (uint32_t*)buffer;
67 unsigned char *pt_lut;
78 fp = fopen (m_FileName.c_str(), "rb");
80 itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
82 pt_lut = (unsigned char*) malloc (sizeof (unsigned char) * GetDimensions(0) * GetDimensions(1));
85 fseek (fp, 1024, SEEK_SET);
86 fread (pt_lut, sizeof(unsigned char), (GetDimensions(1)-1)*GetDimensions(0) / 4, fp);
89 for (i = 0; i < GetDimensions(0); i++) {
90 fread (&a, sizeof(uint32_t), 1, fp);
95 /* Read first pixel of second row */
96 fread (&a, sizeof(uint32_t), 1, fp);
100 /* Decompress the rest */
103 while (i < GetDimensions(0) * GetDimensions(1)) {
104 uint32_t r11, r12, r21;
106 r11 = buf[i-GetDimensions(0)-1];
107 r12 = buf[i-GetDimensions(0)];
131 num_read = fread (&dc, sizeof(unsigned char), 1, fp);
132 if (num_read != 1) goto read_error;
136 num_read = fread (&ds, sizeof(unsigned short), 1, fp);
137 if (num_read != 1) goto read_error;
141 num_read = fread (&dl, sizeof(uint32_t), 1, fp);
142 if (num_read != 1) goto read_error;
147 buf[i] = r21 + r12 + diff - r11;
159 itkGenericExceptionMacro(<< "Error reading gate ascii file");
165 //--------------------------------------------------------------------
166 bool clitk::GateAsciiImageIO::CanWriteFile(const char* FileNameToWrite)
171 //--------------------------------------------------------------------
173 void clitk::GateAsciiImageIO::Write(const void* buffer)