1 /*=========================================================================
3 * Copyright RTK Consortium
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 *=========================================================================*/
19 #define HEADER_INFO_SIZE 68
21 // Based on a true story by the Nederlands Kanker Instituut (AVS_HEIMANN.CPP
26 #include "rtkHisImageIO.h"
28 //--------------------------------------------------------------------
29 // Read Image Information
30 void rtk::HisImageIO::ReadImageInformation()
33 std::ifstream file(m_FileName.c_str(), std::ios::in | std::ios::binary);
36 itkGenericExceptionMacro(<< "Could not open file (for reading): "
40 char header[HEADER_INFO_SIZE];
41 file.read(header, HEADER_INFO_SIZE);
43 if (header[0]!=0 || header[1]!=112 || header[2]!=68 || header[3]!=0) {
44 itkExceptionMacro(<< "rtk::HisImageIO::ReadImageInformation: file "
46 << " not in Heimann HIS format version 100");
50 int nrframes, type, ulx, uly, brx, bry;
51 m_HeaderSize = header[10] + (header[11]<<8);
52 ulx = header[12] + (header[13]<<8);
53 uly = header[14] + (header[15]<<8);
54 brx = header[16] + (header[17]<<8);
55 bry = header[18] + (header[19]<<8);
56 nrframes = header[20] + (header[21]<<8);
57 type = header[32] + (header[34]<<8);
62 SetComponentType(itk::ImageIOBase::USHORT);
64 // case 8: SetComponentType(itk::ImageIOBase::INT); break;
65 // case 16: SetComponentType(itk::ImageIOBase::FLOAT); break;
66 // case 32: SetComponentType(itk::ImageIOBase::INT); break;
68 SetComponentType(itk::ImageIOBase::USHORT);
75 SetNumberOfDimensions(2);
78 SetNumberOfDimensions(3);
82 SetDimensions(0, bry-uly+1);
83 SetDimensions(1, brx-ulx+1);
85 SetDimensions(2, nrframes);
87 SetSpacing(0, 409.6/GetDimensions(0) );
88 SetSpacing(1, 409.6/GetDimensions(1) );
90 SetOrigin(0, -0.5*(GetDimensions(0)-1)*GetSpacing(0) );
91 SetOrigin(1, -0.5*(GetDimensions(1)-1)*GetSpacing(1) );
94 //--------------------------------------------------------------------
95 // Read Image Information
96 bool rtk::HisImageIO::CanReadFile(const char* FileNameToRead)
98 std::string filename(FileNameToRead);
99 const std::string::size_type it = filename.find_last_of( "." );
100 std::string fileExt( filename, it+1, filename.length() );
102 if (fileExt != std::string("his") )
107 //--------------------------------------------------------------------
108 // Read Image Content
109 void rtk::HisImageIO::Read(void * buffer)
112 std::ifstream file(m_FileName.c_str(), std::ios::in | std::ios::binary);
115 itkGenericExceptionMacro(<< "Could not open file (for reading): " << m_FileName);
117 file.seekg(m_HeaderSize+HEADER_INFO_SIZE, std::ios::beg);
119 itkExceptionMacro(<<"File seek failed (His Read)");
121 file.read( (char*)buffer, GetImageSizeInBytes() );
123 itkExceptionMacro(<<"Read failed: Wanted "
124 << GetImageSizeInBytes()
125 << " bytes, but read "
126 << file.gcount() << " bytes. The current state is: "
130 //--------------------------------------------------------------------
131 bool rtk::HisImageIO::CanWriteFile(const char* FileNameToWrite)
133 return CanReadFile(FileNameToWrite);
136 //--------------------------------------------------------------------
138 void rtk::HisImageIO::Write(const void* buffer)
140 std::ofstream file(m_FileName.c_str(), std::ios::out | std::ios::binary);
143 itkGenericExceptionMacro(<< "Could not open file (for writing): " << m_FileName);
145 m_HeaderSize = HEADER_INFO_SIZE + 32;
146 char szHeader[HEADER_INFO_SIZE + 32] = {
147 0x00, 0x70, 0x44, 0x00, 0x64, 0x00, 0x64, 0x00, 0x20, 0x00, 0x20, 0x00, 0x01, 0x00, 0x01, 0x00,
148 0x00, 0x04, 0x00, 0x04, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6A, 0x18, 0x41,
149 0x04, 0x00, 0x40, 0x5F, 0x48, 0x01, 0x40, 0x00, 0x86, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
150 0x00, 0x00, 0x00, 0x00, 0x08, 0x63, 0x13, 0x00, 0xE8, 0x51, 0x13, 0x00, 0x5C, 0xE7, 0x12, 0x00,
151 0xFE, 0x2A, 0x49, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
152 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153 0x00, 0x00, 0x00, 0x00
156 /* Fill into the header the essentials
157 The 'iheader' in previous module is fixed to 0x20, and is included in szHeader.
158 The 'ulx' and 'uly' are fixed to 0x01, so that 'brx' and 'bry' reflect the dimensions of
161 const unsigned int ndim = GetNumberOfDimensions();
162 if ( (ndim < 2) || (ndim > 3) )
163 itkExceptionMacro( <<"Only 2D or 3D support");
165 szHeader[16] = (char)(GetDimensions(0) % 256); // X-size lsb
166 szHeader[17] = (char)(GetDimensions(0) / 256); // X-size msb
167 szHeader[18] = (char)(GetDimensions(1) % 256); // Y-size lsb
168 szHeader[19] = (char)(GetDimensions(1) / 256); // Y-size msb
170 szHeader[20] = (char)(GetDimensions(0) % 256); // NbFrames lsb
171 szHeader[21] = (char)(GetDimensions(0) / 256); // NbFrames msb
174 switch (GetComponentType())
176 case itk::ImageIOBase::USHORT:
179 //case AVS_TYPE_INTEGER:
182 //case AVS_TYPE_REAL:
183 // szHeader[32] = 16;
186 itkExceptionMacro(<< "Unsupported field type");
190 file.write(szHeader, m_HeaderSize);
191 file.write( (const char *)buffer, GetImageSizeInBytes() );