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* itkNotUsed(FileNameToWrite) )
136 //--------------------------------------------------------------------
138 void rtk::HisImageIO::Write( const void* itkNotUsed(buffer) )