]> Creatis software - clitk.git/blob - common/clitkHndImageIO.cxx
Remove warnings
[clitk.git] / common / clitkHndImageIO.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
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
8
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.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18
19 // std include
20 #include <stdio.h>
21
22 // clitk include
23 #include "clitkHndImageIO.h"
24 #include "clitkCommon.h"
25
26 // itk include
27 #include <itkMetaDataObject.h>
28
29 //--------------------------------------------------------------------
30 // Read Image Information
31 void clitk::HndImageIO::ReadImageInformation()
32 {
33   Hnd_header hnd;
34   FILE *fp;
35
36   fp = fopen (m_FileName.c_str(), "rb");
37   if (fp == NULL)
38     itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
39
40   fread ((void *) hnd.sFileType, sizeof(char), 32, fp);
41   fread ((void *) &hnd.FileLength, sizeof(uint32_t), 1, fp);
42   fread ((void *) hnd.sChecksumSpec, sizeof(char), 4, fp);
43   fread ((void *) &hnd.nCheckSum, sizeof(uint32_t), 1, fp);
44   fread ((void *) hnd.sCreationDate, sizeof(char), 8, fp);
45   fread ((void *) hnd.sCreationTime, sizeof(char), 8, fp);
46   fread ((void *) hnd.sPatientID, sizeof(char), 16, fp);
47   fread ((void *) &hnd.nPatientSer, sizeof(uint32_t), 1, fp);
48   fread ((void *) hnd.sSeriesID, sizeof(char), 16, fp);
49   fread ((void *) &hnd.nSeriesSer, sizeof(uint32_t), 1, fp);
50   fread ((void *) hnd.sSliceID, sizeof(char), 16, fp);
51   fread ((void *) &hnd.nSliceSer, sizeof(uint32_t), 1, fp);
52   fread ((void *) &hnd.SizeX, sizeof(uint32_t), 1, fp);
53   fread ((void *) &hnd.SizeY, sizeof(uint32_t), 1, fp);
54   fread ((void *) &hnd.dSliceZPos, sizeof(double), 1, fp);
55   fread ((void *) hnd.sModality, sizeof(char), 16, fp);
56   fread ((void *) &hnd.nWindow, sizeof(uint32_t), 1, fp);
57   fread ((void *) &hnd.nLevel, sizeof(uint32_t), 1, fp);
58   fread ((void *) &hnd.nPixelOffset, sizeof(uint32_t), 1, fp);
59   fread ((void *) hnd.sImageType, sizeof(char), 4, fp);
60   fread ((void *) &hnd.dGantryRtn, sizeof(double), 1, fp);
61   fread ((void *) &hnd.dSAD, sizeof(double), 1, fp);
62   fread ((void *) &hnd.dSFD, sizeof(double), 1, fp);
63   fread ((void *) &hnd.dCollX1, sizeof(double), 1, fp);
64   fread ((void *) &hnd.dCollX2, sizeof(double), 1, fp);
65   fread ((void *) &hnd.dCollY1, sizeof(double), 1, fp);
66   fread ((void *) &hnd.dCollY2, sizeof(double), 1, fp);
67   fread ((void *) &hnd.dCollRtn, sizeof(double), 1, fp);
68   fread ((void *) &hnd.dFieldX, sizeof(double), 1, fp);
69   fread ((void *) &hnd.dFieldY, sizeof(double), 1, fp);
70   fread ((void *) &hnd.dBladeX1, sizeof(double), 1, fp);
71   fread ((void *) &hnd.dBladeX2, sizeof(double), 1, fp);
72   fread ((void *) &hnd.dBladeY1, sizeof(double), 1, fp);
73   fread ((void *) &hnd.dBladeY2, sizeof(double), 1, fp);
74   fread ((void *) &hnd.dIDUPosLng, sizeof(double), 1, fp);
75   fread ((void *) &hnd.dIDUPosLat, sizeof(double), 1, fp);
76   fread ((void *) &hnd.dIDUPosVrt, sizeof(double), 1, fp);
77   fread ((void *) &hnd.dIDUPosRtn, sizeof(double), 1, fp);
78   fread ((void *) &hnd.dPatientSupportAngle, sizeof(double), 1, fp);
79   fread ((void *) &hnd.dTableTopEccentricAngle, sizeof(double), 1, fp);
80   fread ((void *) &hnd.dCouchVrt, sizeof(double), 1, fp);
81   fread ((void *) &hnd.dCouchLng, sizeof(double), 1, fp);
82   fread ((void *) &hnd.dCouchLat, sizeof(double), 1, fp);
83   fread ((void *) &hnd.dIDUResolutionX, sizeof(double), 1, fp);
84   fread ((void *) &hnd.dIDUResolutionY, sizeof(double), 1, fp);
85   fread ((void *) &hnd.dImageResolutionX, sizeof(double), 1, fp);
86   fread ((void *) &hnd.dImageResolutionY, sizeof(double), 1, fp);
87   fread ((void *) &hnd.dEnergy, sizeof(double), 1, fp);
88   fread ((void *) &hnd.dDoseRate, sizeof(double), 1, fp);
89   fread ((void *) &hnd.dXRayKV, sizeof(double), 1, fp);
90   fread ((void *) &hnd.dXRayMA, sizeof(double), 1, fp);
91   fread ((void *) &hnd.dMetersetExposure, sizeof(double), 1, fp);
92   fread ((void *) &hnd.dAcqAdjustment, sizeof(double), 1, fp);
93   fread ((void *) &hnd.dCTProjectionAngle, sizeof(double), 1, fp);
94   fread ((void *) &hnd.dCTNormChamber, sizeof(double), 1, fp);
95   fread ((void *) &hnd.dGatingTimeTag, sizeof(double), 1, fp);
96   fread ((void *) &hnd.dGating4DInfoX, sizeof(double), 1, fp);
97   fread ((void *) &hnd.dGating4DInfoY, sizeof(double), 1, fp);
98   fread ((void *) &hnd.dGating4DInfoZ, sizeof(double), 1, fp);
99   fread ((void *) &hnd.dGating4DInfoTime, sizeof(double), 1, fp);
100   fclose (fp);
101
102   /* Convert hnd to ITK image information */
103   SetNumberOfDimensions(2);
104   SetDimensions(0, hnd.SizeX);
105   SetDimensions(1, hnd.SizeY);
106   SetSpacing(0, hnd.dIDUResolutionX);
107   SetSpacing(1, hnd.dIDUResolutionY);
108   SetOrigin(0, -0.5*(hnd.SizeX-1)*hnd.dIDUResolutionX); //SR: assumed centered
109   SetOrigin(1, -0.5*(hnd.SizeY-1)*hnd.dIDUResolutionY); //SR: assumed centered
110   SetComponentType(itk::ImageIOBase::UINT);
111
112   /* Store important meta information in the meta data dictionary */
113   itk::EncapsulateMetaData<double>(this->GetMetaDataDictionary(), "dCTProjectionAngle", hnd.dCTProjectionAngle);
114   //itk::ExposeMetaData<double>( this->GetMetaDataDictionary(), &(hnd.dCTProjectionAngle), "dCTProjectionAngle");
115 }
116
117 //--------------------------------------------------------------------
118 bool clitk::HndImageIO::CanReadFile(const char* FileNameToRead)
119 {
120   std::string filename(FileNameToRead);
121   std::string filenameext = GetExtension(filename);
122   if (filenameext != std::string("hnd")) return false;
123   return true;
124 }
125
126 //--------------------------------------------------------------------
127 // Read Image Content
128 void clitk::HndImageIO::Read(void * buffer)
129 {
130   FILE *fp;
131
132   uint32_t* buf = (uint32_t*)buffer;
133   unsigned char *pt_lut;
134   uint32_t a;
135   unsigned char v;
136   int lut_idx, lut_off;
137   size_t num_read;
138   char dc;
139   short ds;
140   long dl, diff=0;
141   uint32_t i;
142
143   fp = fopen (m_FileName.c_str(), "rb");
144   if (fp == NULL)
145     itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
146
147   pt_lut = (unsigned char*) malloc (sizeof (unsigned char) * GetDimensions(0) * GetDimensions(1));
148
149   /* Read LUT */
150   fseek (fp, 1024, SEEK_SET);
151   fread (pt_lut, sizeof(unsigned char), (GetDimensions(1)-1)*GetDimensions(0) / 4, fp);
152
153   /* Read first row */
154   for (i = 0; i < GetDimensions(0); i++) {
155     fread (&a, sizeof(uint32_t), 1, fp);
156     buf[i] = a;
157   }
158
159   /* Read first pixel of second row */
160   fread (&a, sizeof(uint32_t), 1, fp);
161   buf[i++] = a;
162
163   /* Decompress the rest */
164   lut_idx = 0;
165   lut_off = 0;
166   while (i < GetDimensions(0) * GetDimensions(1)) {
167     uint32_t r11, r12, r21;
168
169     r11 = buf[i-GetDimensions(0)-1];
170     r12 = buf[i-GetDimensions(0)];
171     r21 = buf[i-1];
172     v = pt_lut[lut_idx];
173     switch (lut_off) {
174     case 0:
175       v = v & 0x03;
176       lut_off ++;
177       break;
178     case 1:
179       v = (v & 0x0C) >> 2;
180       lut_off ++;
181       break;
182     case 2:
183       v = (v & 0x30) >> 4;
184       lut_off ++;
185       break;
186     case 3:
187       v = (v & 0xC0) >> 6;
188       lut_off = 0;
189       lut_idx ++;
190       break;
191     }
192     switch (v) {
193     case 0:
194       num_read = fread (&dc, sizeof(unsigned char), 1, fp);
195       if (num_read != 1) goto read_error;
196       diff = dc;
197       break;
198     case 1:
199       num_read = fread (&ds, sizeof(unsigned short), 1, fp);
200       if (num_read != 1) goto read_error;
201       diff = ds;
202       break;
203     case 2:
204       num_read = fread (&dl, sizeof(uint32_t), 1, fp);
205       if (num_read != 1) goto read_error;
206       diff = dl;
207       break;
208     }
209
210     buf[i] = r21 + r12 + diff - r11;
211     i++;
212   }
213
214   /* Clean up */
215   free (pt_lut);
216   fclose (fp);
217   return;
218
219 read_error:
220
221   itkGenericExceptionMacro(<< "Error reading hnd file");
222   free (pt_lut);
223   fclose (fp);
224   return;
225 }
226
227 //--------------------------------------------------------------------
228 bool clitk::HndImageIO::CanWriteFile(const char* FileNameToWrite)
229 {
230   return false;
231 }
232
233 //--------------------------------------------------------------------
234 // Write Image
235 void clitk::HndImageIO::Write(const void* buffer)
236 {
237 }