]> Creatis software - clitk.git/blob - common/clitkHisImageIO.cxx
Applied clitk indentation
[clitk.git] / common / clitkHisImageIO.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 #ifndef CLITKHISIMAGEIO_CXX
19 #define CLITKHISIMAGEIO_CXX
20 #define HEADER_INFO_SIZE 68
21 /**
22    -------------------------------------------------
23    * @file   clitkHisImageIO.cxx
24    * @author Simon Rit <simon.rit@gmail.com>
25    * @date   16 Feb 2010
26    *
27    * @brief
28    *
29    *
30    -------------------------------------------------*/
31
32 // Based on a true story by the Nederlands Kanker Instituut (AVS_HEIMANN.CPP from the 20090608)
33
34 // std include
35 #include <fstream>
36
37 // clitk include
38 #include "clitkHisImageIO.h"
39 #include "clitkCommon.h"
40
41 //--------------------------------------------------------------------
42 // Read Image Information
43 void clitk::HisImageIO::ReadImageInformation()
44 {
45   // open file
46   std::ifstream file(m_FileName.c_str(), std::ios::in | std::ios::binary);
47   if ( file.fail() )
48     itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
49
50   // read header
51   char header[HEADER_INFO_SIZE];
52   file.read(header, HEADER_INFO_SIZE);
53
54   if (header[0]!=0 || header[1]!=112 || header[2]!=68 || header[3]!=0) {
55     itkExceptionMacro(<< "clitk::HisImageIO::ReadImageInformation: file " << m_FileName << " not in Heimann HIS format version 100");
56     return;
57   }
58
59   int nrframes, type, ulx, uly, brx, bry;
60   m_HeaderSize  = header[10] + (header[11]<<8);
61   ulx      = header[12] + (header[13]<<8);
62   uly      = header[14] + (header[15]<<8);
63   brx      = header[16] + (header[17]<<8);
64   bry      = header[18] + (header[19]<<8);
65   nrframes = header[20] + (header[21]<<8);
66   type     = header[32] + (header[34]<<8);
67
68   switch(type) {
69   case  4:
70     SetComponentType(itk::ImageIOBase::USHORT);
71     break;
72 //    case  8: SetComponentType(itk::ImageIOBase::INT);   break;
73 //    case 16: SetComponentType(itk::ImageIOBase::FLOAT); break;
74 //    case 32: SetComponentType(itk::ImageIOBase::INT);   break;
75   default:
76     SetComponentType(itk::ImageIOBase::USHORT);
77     break;
78   }
79
80   switch(nrframes) {
81   case 1:
82     SetNumberOfDimensions(2);
83     break;
84   default:
85     SetNumberOfDimensions(3);
86     break;
87   }
88
89   SetDimensions(0, bry-uly+1);
90   SetDimensions(1, brx-ulx+1);
91   if (nrframes>1)
92     SetDimensions(2, nrframes);
93
94   SetSpacing(0, 409.6/GetDimensions(0));
95   SetSpacing(1, 409.6/GetDimensions(1));
96
97   SetOrigin(0, -0.5*(GetDimensions(0)-1)*GetSpacing(0));
98   SetOrigin(1, -0.5*(GetDimensions(1)-1)*GetSpacing(1));
99 } ////
100
101 //--------------------------------------------------------------------
102 // Read Image Information
103 bool clitk::HisImageIO::CanReadFile(const char* FileNameToRead)
104 {
105   std::string filename(FileNameToRead);
106   std::string filenameext = GetExtension(filename);
107   if (filenameext != std::string("his")) return false;
108   return true;
109 } ////
110
111 //--------------------------------------------------------------------
112 // Read Image Content
113 void clitk::HisImageIO::Read(void * buffer)
114 {
115   // open file
116   std::ifstream file(m_FileName.c_str(), std::ios::in | std::ios::binary);
117   if ( file.fail() )
118     itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
119
120
121   file.seekg(m_HeaderSize+HEADER_INFO_SIZE, std::ios::beg);
122   if ( file.fail() )
123     itkExceptionMacro(<<"File seek failed (His Read)");
124
125
126   file.read((char*)buffer, GetImageSizeInBytes());
127   if ( file.fail() )
128     itkExceptionMacro(<<"Read failed: Wanted "
129                       << GetImageSizeInBytes()
130                       << " bytes, but read "
131                       << file.gcount() << " bytes. The current state is: "
132                       << file.rdstate());
133 }
134
135 //--------------------------------------------------------------------
136 bool clitk::HisImageIO::CanWriteFile(const char* FileNameToWrite)
137 {
138   std::string filename(FileNameToWrite);
139   std::string filenameext = GetExtension(filename);
140   if (filenameext != std::string("his")) return false;
141   return true;
142 }
143
144 //--------------------------------------------------------------------
145 // Write Image
146 void clitk::HisImageIO::Write(const void* buffer)
147 {
148   std::ofstream file(m_FileName.c_str(), std::ios::out | std::ios::binary);
149   if ( file.fail() )
150     itkGenericExceptionMacro(<< "Could not open file (for writing): " << m_FileName);
151
152   m_HeaderSize = HEADER_INFO_SIZE + 32;
153   char szHeader[HEADER_INFO_SIZE + 32] = {
154     0x00, 0x70, 0x44, 0x00, 0x64, 0x00, 0x64, 0x00, 0x20, 0x00, 0x20, 0x00, 0x01, 0x00, 0x01, 0x00,
155     0x00, 0x04, 0x00, 0x04, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6A, 0x18, 0x41,
156     0x04, 0x00, 0x40, 0x5F, 0x48, 0x01, 0x40, 0x00, 0x86, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
157     0x00, 0x00, 0x00, 0x00, 0x08, 0x63, 0x13, 0x00, 0xE8, 0x51, 0x13, 0x00, 0x5C, 0xE7, 0x12, 0x00,
158     0xFE, 0x2A, 0x49, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
159     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
160     0x00, 0x00, 0x00, 0x00
161   };
162
163   /* Fill into the header the essentials
164      The 'iheader' in previous module is fixed to 0x20, and is included in szHeader.
165      The 'ulx' and 'uly' are fixed to 0x01, so that 'brx' and 'bry' reflect the dimensions of
166      the image.
167   */
168   const unsigned int ndim = GetNumberOfDimensions();
169   if ((ndim < 2) || (ndim > 3))
170     itkExceptionMacro( <<"Only 2D or 3D support");
171
172   szHeader[16] = (char)(GetDimensions(0) % 256);        // X-size       lsb
173   szHeader[17] = (char)(GetDimensions(0) / 256);        // X-size       msb
174   szHeader[18] = (char)(GetDimensions(1) % 256);        // Y-size       lsb
175   szHeader[19] = (char)(GetDimensions(1) / 256);        // Y-size       msb
176   if (ndim == 3) {
177     szHeader[20] = (char)(GetDimensions(0) % 256);      // NbFrames     lsb
178     szHeader[21] = (char)(GetDimensions(0) / 256);      // NbFrames     msb
179   }
180
181   switch (GetComponentType()) {
182   case itk::ImageIOBase::USHORT:
183     szHeader[32] = 4;
184     break;
185     //case AVS_TYPE_INTEGER:
186     //  szHeader[32] = 8;
187     //  break;
188     //case AVS_TYPE_REAL:
189     //  szHeader[32] = 16;
190     //  break;
191   default:
192     itkExceptionMacro(<< "Unsupported field type");
193     break;
194   }
195
196   file.write(szHeader, m_HeaderSize);
197   file.write((const char *)buffer, GetImageSizeInBytes());
198   file.close();
199 } ////
200
201 #endif /* end #define CLITKHISIMAGEIO_CXX */
202