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