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://oncora1.lyon.fnclcc.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 "clitkHndImageIO.h"
24 #include "clitkCommon.h"
26 //--------------------------------------------------------------------
27 // Read Image Information
28 void clitk::HndImageIO::ReadImageInformation()
33 fp = fopen (m_FileName.c_str(), "rb");
35 itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
37 fread ((void *) hnd.sFileType, sizeof(char), 32, fp);
38 fread ((void *) &hnd.FileLength, sizeof(uint32_t), 1, fp);
39 fread ((void *) hnd.sChecksumSpec, sizeof(char), 4, fp);
40 fread ((void *) &hnd.nCheckSum, sizeof(uint32_t), 1, fp);
41 fread ((void *) hnd.sCreationDate, sizeof(char), 8, fp);
42 fread ((void *) hnd.sCreationTime, sizeof(char), 8, fp);
43 fread ((void *) hnd.sPatientID, sizeof(char), 16, fp);
44 fread ((void *) &hnd.nPatientSer, sizeof(uint32_t), 1, fp);
45 fread ((void *) hnd.sSeriesID, sizeof(char), 16, fp);
46 fread ((void *) &hnd.nSeriesSer, sizeof(uint32_t), 1, fp);
47 fread ((void *) hnd.sSliceID, sizeof(char), 16, fp);
48 fread ((void *) &hnd.nSliceSer, sizeof(uint32_t), 1, fp);
49 fread ((void *) &hnd.SizeX, sizeof(uint32_t), 1, fp);
50 fread ((void *) &hnd.SizeY, sizeof(uint32_t), 1, fp);
51 fread ((void *) &hnd.dSliceZPos, sizeof(double), 1, fp);
52 fread ((void *) hnd.sModality, sizeof(char), 16, fp);
53 fread ((void *) &hnd.nWindow, sizeof(uint32_t), 1, fp);
54 fread ((void *) &hnd.nLevel, sizeof(uint32_t), 1, fp);
55 fread ((void *) &hnd.nPixelOffset, sizeof(uint32_t), 1, fp);
56 fread ((void *) hnd.sImageType, sizeof(char), 4, fp);
57 fread ((void *) &hnd.dGantryRtn, sizeof(double), 1, fp);
58 fread ((void *) &hnd.dSAD, sizeof(double), 1, fp);
59 fread ((void *) &hnd.dSFD, sizeof(double), 1, fp);
60 fread ((void *) &hnd.dCollX1, sizeof(double), 1, fp);
61 fread ((void *) &hnd.dCollX2, sizeof(double), 1, fp);
62 fread ((void *) &hnd.dCollY1, sizeof(double), 1, fp);
63 fread ((void *) &hnd.dCollY2, sizeof(double), 1, fp);
64 fread ((void *) &hnd.dCollRtn, sizeof(double), 1, fp);
65 fread ((void *) &hnd.dFieldX, sizeof(double), 1, fp);
66 fread ((void *) &hnd.dFieldY, sizeof(double), 1, fp);
67 fread ((void *) &hnd.dBladeX1, sizeof(double), 1, fp);
68 fread ((void *) &hnd.dBladeX2, sizeof(double), 1, fp);
69 fread ((void *) &hnd.dBladeY1, sizeof(double), 1, fp);
70 fread ((void *) &hnd.dBladeY2, sizeof(double), 1, fp);
71 fread ((void *) &hnd.dIDUPosLng, sizeof(double), 1, fp);
72 fread ((void *) &hnd.dIDUPosLat, sizeof(double), 1, fp);
73 fread ((void *) &hnd.dIDUPosVrt, sizeof(double), 1, fp);
74 fread ((void *) &hnd.dIDUPosRtn, sizeof(double), 1, fp);
75 fread ((void *) &hnd.dPatientSupportAngle, sizeof(double), 1, fp);
76 fread ((void *) &hnd.dTableTopEccentricAngle, sizeof(double), 1, fp);
77 fread ((void *) &hnd.dCouchVrt, sizeof(double), 1, fp);
78 fread ((void *) &hnd.dCouchLng, sizeof(double), 1, fp);
79 fread ((void *) &hnd.dCouchLat, sizeof(double), 1, fp);
80 fread ((void *) &hnd.dIDUResolutionX, sizeof(double), 1, fp);
81 fread ((void *) &hnd.dIDUResolutionY, sizeof(double), 1, fp);
82 fread ((void *) &hnd.dImageResolutionX, sizeof(double), 1, fp);
83 fread ((void *) &hnd.dImageResolutionY, sizeof(double), 1, fp);
84 fread ((void *) &hnd.dEnergy, sizeof(double), 1, fp);
85 fread ((void *) &hnd.dDoseRate, sizeof(double), 1, fp);
86 fread ((void *) &hnd.dXRayKV, sizeof(double), 1, fp);
87 fread ((void *) &hnd.dXRayMA, sizeof(double), 1, fp);
88 fread ((void *) &hnd.dMetersetExposure, sizeof(double), 1, fp);
89 fread ((void *) &hnd.dAcqAdjustment, sizeof(double), 1, fp);
90 fread ((void *) &hnd.dCTProjectionAngle, sizeof(double), 1, fp);
91 fread ((void *) &hnd.dCTNormChamber, sizeof(double), 1, fp);
92 fread ((void *) &hnd.dGatingTimeTag, sizeof(double), 1, fp);
93 fread ((void *) &hnd.dGating4DInfoX, sizeof(double), 1, fp);
94 fread ((void *) &hnd.dGating4DInfoY, sizeof(double), 1, fp);
95 fread ((void *) &hnd.dGating4DInfoZ, sizeof(double), 1, fp);
96 fread ((void *) &hnd.dGating4DInfoTime, sizeof(double), 1, fp);
99 /* Convert hnd to ITK image information */
100 SetNumberOfDimensions(2);
101 SetDimensions(0, hnd.SizeX);
102 SetDimensions(1, hnd.SizeY);
103 SetComponentType(itk::ImageIOBase::UINT);
106 //--------------------------------------------------------------------
107 bool clitk::HndImageIO::CanReadFile(const char* FileNameToRead)
109 std::string filename(FileNameToRead);
110 std::string filenameext = GetExtension(filename);
111 if (filenameext != std::string("hnd")) return false;
115 //--------------------------------------------------------------------
116 // Read Image Content
117 void clitk::HndImageIO::Read(void * buffer)
121 uint32_t* buf = (uint32_t*)buffer;
122 unsigned char *pt_lut;
126 int lut_idx, lut_off;
133 fp = fopen (m_FileName.c_str(), "rb");
135 itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
137 pt_lut = (unsigned char*) malloc (sizeof (unsigned char) * GetDimensions(0) * GetDimensions(1));
140 fseek (fp, 1024, SEEK_SET);
141 fread (pt_lut, sizeof(unsigned char), (GetDimensions(1)-1)*GetDimensions(0) / 4, fp);
144 for (i = 0; i < GetDimensions(0); i++) {
145 fread (&a, sizeof(uint32_t), 1, fp);
150 /* Read first pixel of second row */
151 fread (&a, sizeof(uint32_t), 1, fp);
155 /* Decompress the rest */
158 while (i < GetDimensions(0) * GetDimensions(1)) {
159 uint32_t r11, r12, r21;
161 r11 = buf[i-GetDimensions(0)-1];
162 r12 = buf[i-GetDimensions(0)];
186 num_read = fread (&dc, sizeof(unsigned char), 1, fp);
187 if (num_read != 1) goto read_error;
191 num_read = fread (&ds, sizeof(unsigned short), 1, fp);
192 if (num_read != 1) goto read_error;
196 num_read = fread (&dl, sizeof(uint32_t), 1, fp);
197 if (num_read != 1) goto read_error;
202 buf[i] = r21 + r12 + diff - r11;
214 itkGenericExceptionMacro(<< "Error reading hnd file");
220 //--------------------------------------------------------------------
221 bool clitk::HndImageIO::CanWriteFile(const char* FileNameToWrite)
226 //--------------------------------------------------------------------
228 void clitk::HndImageIO::Write(const void* buffer)