]> Creatis software - clitk.git/blob - common/clitkEsrfHstImageIO.cxx
With ITK 5, add itkReadRawBytesAfterSwappingMacro and itkWriteRawBytesAfterSwappingMacro
[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 #if ( ITK_VERSION_MAJOR < 5 )
111     using namespace itk;
112     // Swap bytes if necessary
113     if itkReadRawBytesAfterSwappingMacro( unsigned short, USHORT )
114       else if itkReadRawBytesAfterSwappingMacro( short, SHORT )
115         else if itkReadRawBytesAfterSwappingMacro( char, CHAR )
116           else if itkReadRawBytesAfterSwappingMacro( unsigned char, UCHAR )
117             else if itkReadRawBytesAfterSwappingMacro( unsigned int, UINT )
118               else if itkReadRawBytesAfterSwappingMacro( int, INT )
119                 else if itkReadRawBytesAfterSwappingMacro( float, FLOAT )
120                   else if itkReadRawBytesAfterSwappingMacro( double, DOUBLE );
121 #else
122   #define itkReadRawBytesAfterSwappingMacro(StrongType, WeakType)   \
123     ( this->GetComponentType() == WeakType )                        \
124       {                                                             \
125       using InternalByteSwapperType = itk::ByteSwapper<StrongType>; \
126       if ( m_ByteOrder == LittleEndian )                            \
127         {                                                           \
128         InternalByteSwapperType::SwapRangeFromSystemToLittleEndian( \
129           (StrongType *)buffer, this->GetImageSizeInComponents() ); \
130         }                                                           \
131       else if ( m_ByteOrder == BigEndian )                          \
132         {                                                           \
133         InternalByteSwapperType::SwapRangeFromSystemToBigEndian(    \
134           (StrongType *)buffer, this->GetImageSizeInComponents() ); \
135         }                                                           \
136       }
137
138     // Swap bytes if necessary
139     if itkReadRawBytesAfterSwappingMacro( unsigned short, USHORT )
140       else if itkReadRawBytesAfterSwappingMacro( short, SHORT )
141         else if itkReadRawBytesAfterSwappingMacro( char, CHAR )
142           else if itkReadRawBytesAfterSwappingMacro( unsigned char, UCHAR )
143             else if itkReadRawBytesAfterSwappingMacro( unsigned int, UINT )
144               else if itkReadRawBytesAfterSwappingMacro( int, INT )
145                 else if itkReadRawBytesAfterSwappingMacro( float, FLOAT )
146                   else if itkReadRawBytesAfterSwappingMacro( double, DOUBLE );
147 #endif
148   }
149 }
150
151 //--------------------------------------------------------------------
152 // Write Image Information
153 void clitk::EsrfHstImageIO::WriteImageInformation(bool keepOfStream)
154 {
155 }
156
157 //--------------------------------------------------------------------
158 // Write Image Information
159 bool clitk::EsrfHstImageIO::CanWriteFile(const char* FileNameToWrite)
160 {
161   return false;
162 }
163
164 //--------------------------------------------------------------------
165 // Write Image
166 void clitk::EsrfHstImageIO::Write(const void * buffer)
167 {
168 } ////