]> Creatis software - clitk.git/blob - common/clitkEsrfHstImageIO.cxx
Merge branch 'master' into extentSimon
[clitk.git] / common / clitkEsrfHstImageIO.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18
19 #include "clitkEsrfHstImageIO.h"
20 #include "clitkEsrfHstXMLFileReader.h"
21 #include "clitkDD.h"
22
23 // itk include (for itkReadRawBytesAfterSwappingMacro)
24 #include <itkRawImageIO.h>
25 #include <itksys/SystemTools.hxx>
26 #include <itkMetaDataObject.h>
27
28 //--------------------------------------------------------------------
29 // Read Image Information
30 void clitk::EsrfHstImageIO::ReadImageInformation()
31 {
32   clitk::EsrfHstXMLFileReader::Pointer hstXmlReader;
33   hstXmlReader = clitk::EsrfHstXMLFileReader::New();
34   hstXmlReader->SetFilename(m_XmlFileName);
35   hstXmlReader->GenerateOutputInformation();
36   itk::MetaDataDictionary &dic = *(hstXmlReader->GetOutputObject() );
37
38   typedef itk::MetaDataObject< double > MetaDataDoubleType;
39   typedef itk::MetaDataObject< std::string > MetaDataStringType;
40   typedef itk::MetaDataObject< int > MetaDataIntType;
41   SetComponentType(itk::ImageIOBase::FLOAT);
42   SetNumberOfDimensions(3);
43   SetDimensions(0, dynamic_cast<MetaDataIntType *>(dic["SIZEX"].GetPointer() )->GetMetaDataObjectValue());
44   SetDimensions(1, dynamic_cast<MetaDataIntType *>(dic["SIZEY"].GetPointer() )->GetMetaDataObjectValue());
45   SetDimensions(2, dynamic_cast<MetaDataIntType *>(dic["SIZEZ"].GetPointer() )->GetMetaDataObjectValue());
46   SetSpacing(0, dynamic_cast<MetaDataDoubleType *>(dic["voxelsize"].GetPointer() )->GetMetaDataObjectValue());
47   SetSpacing(1, GetSpacing(0));
48   SetSpacing(2, GetSpacing(0));
49   SetOrigin(0, GetSpacing(0)*(dynamic_cast<MetaDataIntType *>(dic["ORIGINX"].GetPointer() )->GetMetaDataObjectValue()-1));
50   SetOrigin(1, GetSpacing(0)*(dynamic_cast<MetaDataIntType *>(dic["ORIGINY"].GetPointer() )->GetMetaDataObjectValue()-1));
51   SetOrigin(2, GetSpacing(0)*(dynamic_cast<MetaDataIntType *>(dic["ORIGINZ"].GetPointer() )->GetMetaDataObjectValue()-1));
52   if(std::string("LOWBYTEFIRST") == dynamic_cast<MetaDataStringType*>(dic["SUBVOLUME_NAME"].GetPointer() )->GetMetaDataObjectValue())
53     this->SetByteOrder(BigEndian);
54   else
55     this->SetByteOrder(LittleEndian);
56
57   // Prepare raw file name
58   m_RawFileName = itksys::SystemTools::GetFilenamePath(m_XmlFileName);
59   if(m_RawFileName != "")
60     m_RawFileName += std::string("/");
61   m_RawFileName += dynamic_cast<MetaDataStringType*>(dic["SUBVOLUME_NAME"].GetPointer() )->GetMetaDataObjectValue();
62 } ////
63
64 //--------------------------------------------------------------------
65 // Read Image Information
66 bool clitk::EsrfHstImageIO::CanReadFile(const char* FileNameToRead)
67 {
68   std::string ext = itksys::SystemTools::GetFilenameLastExtension(FileNameToRead);
69   if( ext!=std::string(".xml") && ext!=std::string(".vol") )
70     return false;
71
72   m_XmlFileName = std::string(FileNameToRead);
73   if( ext==std::string(".vol") )
74     m_XmlFileName += std::string(".xml");
75
76   std::ifstream is(m_XmlFileName.c_str());
77   if(! is.is_open() )
78     return false;
79
80   std::string firstLine;
81   std::getline(is, firstLine);
82   if(firstLine.find( std::string("<!-- PyHST VOLUME XML FILE -->") ) == std::string::npos)
83     return false;
84
85   return true;
86 } ////
87
88 //--------------------------------------------------------------------
89 // Read Image Content
90 void clitk::EsrfHstImageIO::Read(void * buffer)
91 {
92   // Adapted from itkRawImageIO
93   std::ifstream is(m_RawFileName.c_str(), std::ios::binary);
94   if(! is.is_open() )
95     itkExceptionMacro(<<"Could not open file " << m_RawFileName);
96
97   unsigned long numberOfBytesToBeRead = GetComponentSize();
98   for(unsigned int i=0; i<GetNumberOfDimensions(); i++) numberOfBytesToBeRead *= GetDimensions(i);
99
100   if(!this->ReadBufferAsBinary(is, buffer, numberOfBytesToBeRead)) {
101     itkExceptionMacro(<<"Read failed: Wanted "
102                       << numberOfBytesToBeRead
103                       << " bytes, but read "
104                       << is.gcount() << " bytes.");
105   }
106   itkDebugMacro(<< "Reading Done");
107
108   // Adapted from itkRawImageIO
109   {
110     using namespace itk;
111     // Swap bytes if necessary
112     if itkReadRawBytesAfterSwappingMacro( unsigned short, USHORT )
113       else if itkReadRawBytesAfterSwappingMacro( short, SHORT )
114         else if itkReadRawBytesAfterSwappingMacro( char, CHAR )
115           else if itkReadRawBytesAfterSwappingMacro( unsigned char, UCHAR )
116             else if itkReadRawBytesAfterSwappingMacro( unsigned int, UINT )
117               else if itkReadRawBytesAfterSwappingMacro( int, INT )
118                 else if itkReadRawBytesAfterSwappingMacro( unsigned int, ULONG )
119                   else if itkReadRawBytesAfterSwappingMacro( int, LONG )
120                     else if itkReadRawBytesAfterSwappingMacro( float, FLOAT )
121                       else if itkReadRawBytesAfterSwappingMacro( double, DOUBLE );
122   }
123 }
124
125 //--------------------------------------------------------------------
126 // Write Image Information
127 void clitk::EsrfHstImageIO::WriteImageInformation(bool keepOfStream)
128 {
129 }
130
131 //--------------------------------------------------------------------
132 // Write Image Information
133 bool clitk::EsrfHstImageIO::CanWriteFile(const char* FileNameToWrite)
134 {
135   return false;
136 }
137
138 //--------------------------------------------------------------------
139 // Write Image
140 void clitk::EsrfHstImageIO::Write(const void * buffer)
141 {
142 } ////