]> Creatis software - clitk.git/blob - common/rtkImagXImageIO.cxx
Merge branch 'master' of tux.creatis.insa-lyon.fr:clitk
[clitk.git] / common / rtkImagXImageIO.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 "rtkImagXImageIO.h"
20 #include "rtkImagXXMLFileReader.h"
21
22 // itk include (for itkReadRawBytesAfterSwappingMacro)
23 #include <itkRawImageIO.h>
24 #include <itksys/SystemTools.hxx>
25 #include <itkMetaDataObject.h>
26 #include <itkMatrix.h>
27
28 //--------------------------------------------------------------------
29 // Read Image Information
30 void rtk::ImagXImageIO::ReadImageInformation()
31 {
32   rtk::ImagXXMLFileReader::Pointer xmlReader;
33
34   xmlReader = rtk::ImagXXMLFileReader::New();
35   xmlReader->SetFilename(m_FileName);
36   xmlReader->GenerateOutputInformation();
37
38   itk::MetaDataDictionary &dic = *(xmlReader->GetOutputObject() );
39
40   typedef itk::MetaDataObject< double >      MetaDataDoubleType;
41   typedef itk::MetaDataObject< std::string > MetaDataStringType;
42   typedef itk::MetaDataObject< int >         MetaDataIntType;
43
44   std::string pixelType = dynamic_cast<MetaDataStringType*>(dic["pixelFormat"].GetPointer() )->GetMetaDataObjectValue();
45   if(pixelType=="Type_uint8")
46     SetComponentType(itk::ImageIOBase::UCHAR);
47   if(pixelType=="Type_sint8")
48     SetComponentType(itk::ImageIOBase::CHAR);
49   if(pixelType=="Type_uint16")
50     SetComponentType(itk::ImageIOBase::USHORT);
51   if(pixelType=="Type_sint16")
52     SetComponentType(itk::ImageIOBase::SHORT);
53   if(pixelType=="Type_uint32")
54     SetComponentType(itk::ImageIOBase::UINT);
55   if(pixelType=="Type_sint32")
56     SetComponentType(itk::ImageIOBase::INT);
57   if(pixelType=="Type_float")
58     SetComponentType(itk::ImageIOBase::FLOAT);
59
60   if(dic["dimensions"].GetPointer()==NULL)
61     SetNumberOfDimensions(3);
62   else
63     SetNumberOfDimensions( ( dynamic_cast<MetaDataIntType *>(dic["dimensions"].GetPointer() )->GetMetaDataObjectValue() ) );
64
65   SetDimensions(0, dynamic_cast<MetaDataIntType *>(dic["x"].GetPointer() )->GetMetaDataObjectValue() );
66   SetSpacing(0, dynamic_cast<MetaDataDoubleType *>(dic["spacing_x"].GetPointer() )->GetMetaDataObjectValue() );
67   if(GetNumberOfDimensions()>1)
68     {
69     SetDimensions(1, dynamic_cast<MetaDataIntType *>(dic["y"].GetPointer() )->GetMetaDataObjectValue() );
70     SetSpacing(1, dynamic_cast<MetaDataDoubleType *>(dic["spacing_y"].GetPointer() )->GetMetaDataObjectValue() );
71     }
72   if(GetNumberOfDimensions()>2)
73     {
74     SetDimensions(2, dynamic_cast<MetaDataIntType *>(dic["z"].GetPointer() )->GetMetaDataObjectValue() );
75     SetSpacing(2, dynamic_cast<MetaDataDoubleType *>(dic["spacing_z"].GetPointer() )->GetMetaDataObjectValue() );
76     }
77
78   std::istringstream iss(
79     dynamic_cast<MetaDataStringType*>(dic["matrixTransform"].GetPointer() )->GetMetaDataObjectValue() );
80   itk::Matrix<double, 4, 4> matrix;
81   for(unsigned int j=0; j<4; j++)
82     for(unsigned int i=0; i<4; i++)
83       iss >> matrix[j][i];
84   matrix /= matrix[3][3];
85
86   std::vector<double> direction;
87   for(unsigned int i=0; i<GetNumberOfDimensions(); i++)
88     {
89     direction.clear();
90     for(unsigned int j=0; j<GetNumberOfDimensions(); j++)
91       direction.push_back(matrix[i][j]);
92     SetDirection(i, direction);
93     SetOrigin(i, matrix[i][3]);
94     }
95
96   if(std::string("LSB") == dynamic_cast<MetaDataStringType*>(dic["byteOrder"].GetPointer() )->GetMetaDataObjectValue() )
97     this->SetByteOrder(LittleEndian);
98   else
99     this->SetByteOrder(BigEndian);
100
101   // Prepare raw file name
102   m_RawFileName = itksys::SystemTools::GetFilenamePath(m_FileName);
103   if(m_RawFileName != "")
104     m_RawFileName += std::string("/");
105   m_RawFileName += dynamic_cast<MetaDataStringType*>(dic["rawFile"].GetPointer() )->GetMetaDataObjectValue();
106 } ////
107
108 //--------------------------------------------------------------------
109 // Read Image Information
110 bool rtk::ImagXImageIO::CanReadFile(const char* FileNameToRead)
111 {
112   std::string ext = itksys::SystemTools::GetFilenameLastExtension(FileNameToRead);
113
114   if( ext!=std::string(".xml") )
115     return false;
116
117   std::ifstream is(FileNameToRead);
118   if(!is.is_open() )
119     return false;
120
121   // If the XML file has "<image name=" at the beginning of the first or second
122   // line, we assume this is an ImagX file
123   std::string line;
124
125   std::getline(is, line);
126   if(line.substr(0, 12) == std::string("<image name=") )
127     return true;
128
129   std::getline(is, line);
130   if(line.substr(0, 12) == std::string("<image name=") )
131     return true;
132
133   return false;
134 } ////
135
136 //--------------------------------------------------------------------
137 // Read Image Content
138 void rtk::ImagXImageIO::Read(void * buffer)
139 {
140   // Adapted from itkRawImageIO
141   std::ifstream is(m_RawFileName.c_str(), std::ios::binary);
142
143   if(!is.is_open() )
144     itkExceptionMacro(<<"Could not open file " << m_RawFileName);
145
146   unsigned long numberOfBytesToBeRead = GetComponentSize();
147   for(unsigned int i=0; i<GetNumberOfDimensions(); i++) numberOfBytesToBeRead *= GetDimensions(i);
148
149   if(!this->ReadBufferAsBinary(is, buffer, numberOfBytesToBeRead) ) {
150     itkExceptionMacro(<<"Read failed: Wanted "
151                       << numberOfBytesToBeRead
152                       << " bytes, but read "
153                       << is.gcount() << " bytes.");
154     }
155   itkDebugMacro(<< "Reading Done");
156
157   // Adapted from itkRawImageIO
158     {
159     using namespace itk;
160     // Swap bytes if necessary
161     if itkReadRawBytesAfterSwappingMacro( unsigned short, USHORT )
162     else if itkReadRawBytesAfterSwappingMacro( short, SHORT )
163     else if itkReadRawBytesAfterSwappingMacro( char, CHAR )
164     else if itkReadRawBytesAfterSwappingMacro( unsigned char, UCHAR )
165     else if itkReadRawBytesAfterSwappingMacro( unsigned int, UINT )
166     else if itkReadRawBytesAfterSwappingMacro( int, INT )
167     else if itkReadRawBytesAfterSwappingMacro( unsigned int, ULONG )
168     else if itkReadRawBytesAfterSwappingMacro( int, LONG )
169     else if itkReadRawBytesAfterSwappingMacro( float, FLOAT )
170     else if itkReadRawBytesAfterSwappingMacro( double, DOUBLE );
171     }
172 }
173
174 //--------------------------------------------------------------------
175 // Write Image Information
176 void rtk::ImagXImageIO::WriteImageInformation(bool keepOfStream)
177 {
178 }
179
180 //--------------------------------------------------------------------
181 // Write Image Information
182 bool rtk::ImagXImageIO::CanWriteFile(const char* FileNameToWrite)
183 {
184   return false;
185 }
186
187 //--------------------------------------------------------------------
188 // Write Image
189 void rtk::ImagXImageIO::Write(const void * buffer)
190 {
191 } ////
192