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 #include "rtkXRadImageIO.h"
21 #include <itkRawImageIO.h>
22 #include <itkMetaDataObject.h>
24 //--------------------------------------------------------------------
25 // Read Image Information
26 void rtk::XRadImageIO::ReadImageInformation()
29 is.open(m_FileName.c_str());
32 itkExceptionMacro(<<"Could not open file " << m_FileName);
34 SetNumberOfDimensions(3);
35 std::string section="";
39 std::getline(is, line);
40 if(line.find('[') != std::string::npos)
42 unsigned int pos1 = line.find('[');
43 unsigned int pos2 = line.find(']');
44 section = line.substr(pos1+1, pos2-pos1-1);
46 if(line.find('=') != std::string::npos)
48 unsigned int pos = line.find('=');
49 std::string paramName = line.substr(0,pos);
50 std::string paramValue = line.substr(pos+1, line.length()-pos-1);
52 if(paramName == std::string("CBCT.DimensionalAttributes.IDim"))
53 SetDimensions(0, atoi(paramValue.c_str()));
54 else if(paramName == std::string("CBCT.DimensionalAttributes.JDim"))
55 SetDimensions(1, atoi(paramValue.c_str()));
56 else if(paramName == std::string("CBCT.DimensionalAttributes.KDim"))
57 SetDimensions(2, atoi(paramValue.c_str()));
58 else if(paramName == std::string("CBCT.DimensionalAttributes.DataSize"))
60 if(atoi(paramValue.c_str()) == 3)
61 SetComponentType(itk::ImageIOBase::FLOAT);
62 if(atoi(paramValue.c_str()) == 6)
63 SetComponentType(itk::ImageIOBase::USHORT);
65 else if(paramName == std::string("CBCT.DimensionalAttributes.PixelDimension_I_cm"))
67 double spacing = 10*atof(paramValue.c_str());
68 SetSpacing(0, (spacing==0.)?1.:spacing);
70 else if(paramName == std::string("CBCT.DimensionalAttributes.PixelDimension_J_cm"))
72 double spacing = 10*atof(paramValue.c_str());
73 SetSpacing(1, (spacing==0.)?1.:spacing);
75 else if(paramName == std::string("CBCT.DimensionalAttributes.PixelDimension_K_cm"))
77 double spacing = 10*atof(paramValue.c_str());
78 SetSpacing(2, (spacing==0.)?1.:spacing);
82 paramName = section + std::string("_") + paramName;
83 itk::EncapsulateMetaData<std::string>(this->GetMetaDataDictionary(),
92 //--------------------------------------------------------------------
93 // Read Image Information
94 bool rtk::XRadImageIO::CanReadFile(const char* FileNameToRead)
96 std::string filename(FileNameToRead);
97 const std::string::size_type it = filename.find_last_of( "." );
98 std::string fileExt( filename, it+1, filename.length() );
100 if (fileExt != std::string("header") ) return false;
104 //--------------------------------------------------------------------
105 // Read Image Content
106 void rtk::XRadImageIO::Read(void * buffer)
108 // Adapted from itkRawImageIO
109 std::string rawFileName( m_FileName, 0, m_FileName.size()-6);
110 rawFileName += "img";
112 std::ifstream is(rawFileName.c_str(), std::ios::binary);
114 itkExceptionMacro(<<"Could not open file " << rawFileName);
116 unsigned long numberOfBytesToBeRead = GetComponentSize();
117 for(unsigned int i=0; i<GetNumberOfDimensions(); i++) numberOfBytesToBeRead *= GetDimensions(i);
119 if(!this->ReadBufferAsBinary(is, buffer, numberOfBytesToBeRead) ) {
120 itkExceptionMacro(<<"Read failed: Wanted "
121 << numberOfBytesToBeRead
122 << " bytes, but read "
123 << is.gcount() << " bytes.");
125 itkDebugMacro(<< "Reading Done");
127 // Adapted from itkRawImageIO
130 // Swap bytes if necessary
131 if itkReadRawBytesAfterSwappingMacro( unsigned short, USHORT )
132 else if itkReadRawBytesAfterSwappingMacro( short, SHORT )
133 else if itkReadRawBytesAfterSwappingMacro( char, CHAR )
134 else if itkReadRawBytesAfterSwappingMacro( unsigned char, UCHAR )
135 else if itkReadRawBytesAfterSwappingMacro( unsigned int, UINT )
136 else if itkReadRawBytesAfterSwappingMacro( int, INT )
137 else if itkReadRawBytesAfterSwappingMacro( unsigned int, ULONG )
138 else if itkReadRawBytesAfterSwappingMacro( int, LONG )
139 else if itkReadRawBytesAfterSwappingMacro( float, FLOAT )
140 else if itkReadRawBytesAfterSwappingMacro( double, DOUBLE );
144 //--------------------------------------------------------------------
145 // Write Image Information
146 void rtk::XRadImageIO::WriteImageInformation(bool itkNotUsed(keepOfStream))
150 //--------------------------------------------------------------------
151 // Write Image Information
152 bool rtk::XRadImageIO::CanWriteFile(const char* itkNotUsed(FileNameToWrite))
157 //--------------------------------------------------------------------
159 void rtk::XRadImageIO::Write(const void * itkNotUsed(buffer))