]> Creatis software - clitk.git/blob - common/clitkGateAsciiImageIO.cxx
started io support for gate ascii file
[clitk.git] / common / clitkGateAsciiImageIO.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 "clitkGateAsciiImageIO.h"
24 #include "clitkCommon.h"
25
26 // itk include
27 #include <itkMetaDataObject.h>
28
29 //--------------------------------------------------------------------
30 // Read Image Information
31 void clitk::GateAsciiImageIO::ReadImageInformation()
32 {
33     itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
34
35
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);
45
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");
49 }
50
51 //--------------------------------------------------------------------
52 bool clitk::GateAsciiImageIO::CanReadFile(const char* FileNameToRead)
53 {
54   std::string filename(FileNameToRead);
55   std::string filenameext = GetExtension(filename);
56   if (filenameext != std::string("hnd")) return false;
57   return true;
58 }
59
60 //--------------------------------------------------------------------
61 // Read Image Content
62 void clitk::GateAsciiImageIO::Read(void * buffer)
63 {
64   FILE *fp;
65
66   uint32_t* buf = (uint32_t*)buffer;
67   unsigned char *pt_lut;
68   uint32_t a;
69   float b;
70   unsigned char v;
71   int lut_idx, lut_off;
72   size_t num_read;
73   char dc;
74   short ds;
75   long dl, diff=0;
76   uint32_t i;
77
78   fp = fopen (m_FileName.c_str(), "rb");
79   if (fp == NULL)
80     itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
81
82   pt_lut = (unsigned char*) malloc (sizeof (unsigned char) * GetDimensions(0) * GetDimensions(1));
83
84   /* Read LUT */
85   fseek (fp, 1024, SEEK_SET);
86   fread (pt_lut, sizeof(unsigned char), (GetDimensions(1)-1)*GetDimensions(0) / 4, fp);
87
88   /* Read first row */
89   for (i = 0; i < GetDimensions(0); i++) {
90     fread (&a, sizeof(uint32_t), 1, fp);
91     buf[i] = a;
92     b = a;
93   }
94
95   /* Read first pixel of second row */
96   fread (&a, sizeof(uint32_t), 1, fp);
97   buf[i++] = a;
98   b = a;
99
100   /* Decompress the rest */
101   lut_idx = 0;
102   lut_off = 0;
103   while (i < GetDimensions(0) * GetDimensions(1)) {
104     uint32_t r11, r12, r21;
105
106     r11 = buf[i-GetDimensions(0)-1];
107     r12 = buf[i-GetDimensions(0)];
108     r21 = buf[i-1];
109     v = pt_lut[lut_idx];
110     switch (lut_off) {
111     case 0:
112       v = v & 0x03;
113       lut_off ++;
114       break;
115     case 1:
116       v = (v & 0x0C) >> 2;
117       lut_off ++;
118       break;
119     case 2:
120       v = (v & 0x30) >> 4;
121       lut_off ++;
122       break;
123     case 3:
124       v = (v & 0xC0) >> 6;
125       lut_off = 0;
126       lut_idx ++;
127       break;
128     }
129     switch (v) {
130     case 0:
131       num_read = fread (&dc, sizeof(unsigned char), 1, fp);
132       if (num_read != 1) goto read_error;
133       diff = dc;
134       break;
135     case 1:
136       num_read = fread (&ds, sizeof(unsigned short), 1, fp);
137       if (num_read != 1) goto read_error;
138       diff = ds;
139       break;
140     case 2:
141       num_read = fread (&dl, sizeof(uint32_t), 1, fp);
142       if (num_read != 1) goto read_error;
143       diff = dl;
144       break;
145     }
146
147     buf[i] = r21 + r12 + diff - r11;
148     b = buf[i];
149     i++;
150   }
151
152   /* Clean up */
153   free (pt_lut);
154   fclose (fp);
155   return;
156
157 read_error:
158
159   itkGenericExceptionMacro(<< "Error reading gate ascii file");
160   free (pt_lut);
161   fclose (fp);
162   return;
163 }
164
165 //--------------------------------------------------------------------
166 bool clitk::GateAsciiImageIO::CanWriteFile(const char* FileNameToWrite)
167 {
168   return false;
169 }
170
171 //--------------------------------------------------------------------
172 // Write Image
173 void clitk::GateAsciiImageIO::Write(const void* buffer)
174 {
175 }