]> Creatis software - clitk.git/blob - common/clitkHndImageIO.cxx
Added Varian OBI file format
[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://oncora1.lyon.fnclcc.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 //--------------------------------------------------------------------
27 // Read Image Information
28 void clitk::HndImageIO::ReadImageInformation()
29 {
30     Hnd_header hnd;
31     FILE *fp;
32
33     fp = fopen (m_FileName.c_str(), "rb");
34     if (fp == NULL)
35         itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
36
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);
97     fclose (fp);
98
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);
104 }
105
106 //--------------------------------------------------------------------
107 bool clitk::HndImageIO::CanReadFile(const char* FileNameToRead)
108 {
109   std::string filename(FileNameToRead);
110   std::string filenameext = GetExtension(filename);
111   if (filenameext != std::string("hnd")) return false;
112   return true;
113 }
114
115 //--------------------------------------------------------------------
116 // Read Image Content
117 void clitk::HndImageIO::Read(void * buffer)
118 {
119     FILE *fp;
120
121     uint32_t* buf = (uint32_t*)buffer;
122     unsigned char *pt_lut;
123     uint32_t a;
124     float b;
125     unsigned char v;
126     int lut_idx, lut_off;
127     size_t num_read;
128     char dc;
129     short ds;
130     long dl, diff=0;
131     uint32_t i;
132
133     fp = fopen (m_FileName.c_str(), "rb");
134     if (fp == NULL)
135         itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
136
137     pt_lut = (unsigned char*) malloc (sizeof (unsigned char) * GetDimensions(0) * GetDimensions(1));
138
139     /* Read LUT */
140     fseek (fp, 1024, SEEK_SET);
141     fread (pt_lut, sizeof(unsigned char), (GetDimensions(1)-1)*GetDimensions(0) / 4, fp);
142
143     /* Read first row */
144     for (i = 0; i < GetDimensions(0); i++) {
145         fread (&a, sizeof(uint32_t), 1, fp);
146         buf[i] = a;
147         b = a;
148     }
149
150     /* Read first pixel of second row */
151     fread (&a, sizeof(uint32_t), 1, fp);
152     buf[i++] = a;
153     b = a;
154
155     /* Decompress the rest */
156     lut_idx = 0;
157     lut_off = 0;
158     while (i < GetDimensions(0) * GetDimensions(1)) {
159         uint32_t r11, r12, r21;
160
161         r11 = buf[i-GetDimensions(0)-1];
162         r12 = buf[i-GetDimensions(0)];
163         r21 = buf[i-1];
164         v = pt_lut[lut_idx];
165         switch (lut_off) {
166         case 0:
167             v = v & 0x03;
168             lut_off ++;
169             break;
170         case 1:
171             v = (v & 0x0C) >> 2;
172             lut_off ++;
173             break;
174         case 2:
175             v = (v & 0x30) >> 4;
176             lut_off ++;
177             break;
178         case 3:
179             v = (v & 0xC0) >> 6;
180             lut_off = 0;
181             lut_idx ++;
182             break;
183         }
184         switch (v) {
185         case 0:
186             num_read = fread (&dc, sizeof(unsigned char), 1, fp);
187             if (num_read != 1) goto read_error;
188             diff = dc;
189             break;
190         case 1:
191             num_read = fread (&ds, sizeof(unsigned short), 1, fp);
192             if (num_read != 1) goto read_error;
193             diff = ds;
194             break;
195         case 2:
196             num_read = fread (&dl, sizeof(uint32_t), 1, fp);
197             if (num_read != 1) goto read_error;
198             diff = dl;
199             break;
200         }
201
202         buf[i] = r21 + r12 + diff - r11;
203         b = buf[i];
204         i++;
205     }
206
207     /* Clean up */
208     free (pt_lut);
209     fclose (fp);
210     return;
211
212  read_error:
213
214     itkGenericExceptionMacro(<< "Error reading hnd file");
215     free (pt_lut);
216     fclose (fp);
217     return;
218 }
219
220 //--------------------------------------------------------------------
221 bool clitk::HndImageIO::CanWriteFile(const char* FileNameToWrite)
222 {
223   return false;
224 }
225
226 //--------------------------------------------------------------------
227 // Write Image
228 void clitk::HndImageIO::Write(const void* buffer)
229 {
230 }