]> Creatis software - clitk.git/blob - common/rtkXRadImageIO.cxx
Merge branch 'master' into extentSimon
[clitk.git] / common / rtkXRadImageIO.cxx
1 /*=========================================================================
2  *
3  *  Copyright RTK Consortium
4  *
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
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
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.
16  *
17  *=========================================================================*/
18
19 #include "rtkXRadImageIO.h"
20
21 #include <itkRawImageIO.h>
22 #include <itkMetaDataObject.h>
23
24 //--------------------------------------------------------------------
25 // Read Image Information
26 void rtk::XRadImageIO::ReadImageInformation()
27 {
28   std::ifstream is;
29   is.open(m_FileName.c_str());
30
31   if(!is.is_open())
32     itkExceptionMacro(<<"Could not open file " << m_FileName);
33
34   SetNumberOfDimensions(3);
35   std::string section="";
36   while(!is.eof())
37     {
38     std::string line;
39     std::getline(is, line);
40     if(line.find('[') != std::string::npos)
41       {
42       unsigned int pos1 = line.find('[');
43       unsigned int pos2 = line.find(']');
44       section = line.substr(pos1+1, pos2-pos1-1);
45       }
46     if(line.find('=') != std::string::npos)
47       {
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);
51
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"))
59         {
60         if(atoi(paramValue.c_str()) == 3)
61           SetComponentType(itk::ImageIOBase::FLOAT);
62         if(atoi(paramValue.c_str()) == 6)
63           SetComponentType(itk::ImageIOBase::USHORT);
64         }
65       else if(paramName == std::string("CBCT.DimensionalAttributes.PixelDimension_I_cm"))
66         {
67         double spacing = 10*atof(paramValue.c_str());
68         SetSpacing(0, (spacing==0.)?1.:spacing);
69         }
70       else if(paramName == std::string("CBCT.DimensionalAttributes.PixelDimension_J_cm"))
71         {
72         double spacing = 10*atof(paramValue.c_str());
73         SetSpacing(1, (spacing==0.)?1.:spacing);
74         }
75       else if(paramName == std::string("CBCT.DimensionalAttributes.PixelDimension_K_cm"))
76         {
77         double spacing = 10*atof(paramValue.c_str());
78         SetSpacing(2, (spacing==0.)?1.:spacing);
79         }
80       else
81         {
82         paramName = section + std::string("_") + paramName;
83         itk::EncapsulateMetaData<std::string>(this->GetMetaDataDictionary(),
84                                               paramName.c_str(),
85                                               paramValue);
86         }
87       }
88
89     }
90 } ////
91
92 //--------------------------------------------------------------------
93 // Read Image Information
94 bool rtk::XRadImageIO::CanReadFile(const char* FileNameToRead)
95 {
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() );
99
100   if (fileExt != std::string("header") ) return false;
101   return true;
102 } ////
103
104 //--------------------------------------------------------------------
105 // Read Image Content
106 void rtk::XRadImageIO::Read(void * buffer)
107 {
108   // Adapted from itkRawImageIO
109   std::string rawFileName( m_FileName, 0, m_FileName.size()-6);
110   rawFileName += "img";
111
112   std::ifstream is(rawFileName.c_str(), std::ios::binary);
113   if(!is.is_open() )
114     itkExceptionMacro(<<"Could not open file " << rawFileName);
115
116   unsigned long numberOfBytesToBeRead = GetComponentSize();
117   for(unsigned int i=0; i<GetNumberOfDimensions(); i++) numberOfBytesToBeRead *= GetDimensions(i);
118
119   if(!this->ReadBufferAsBinary(is, buffer, numberOfBytesToBeRead) ) {
120     itkExceptionMacro(<<"Read failed: Wanted "
121                       << numberOfBytesToBeRead
122                       << " bytes, but read "
123                       << is.gcount() << " bytes.");
124     }
125   itkDebugMacro(<< "Reading Done");
126
127   // Adapted from itkRawImageIO
128     {
129     using namespace itk;
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 );
141     }
142 }
143
144 //--------------------------------------------------------------------
145 // Write Image Information
146 void rtk::XRadImageIO::WriteImageInformation(bool itkNotUsed(keepOfStream))
147 {
148 }
149
150 //--------------------------------------------------------------------
151 // Write Image Information
152 bool rtk::XRadImageIO::CanWriteFile(const char* itkNotUsed(FileNameToWrite))
153 {
154   return false;
155 }
156
157 //--------------------------------------------------------------------
158 // Write Image
159 void rtk::XRadImageIO::Write(const void * itkNotUsed(buffer))
160 {
161 } ////