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