1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
19 #include "clitkEsrfHstImageIO.h"
20 #include "clitkEsrfHstXMLFileReader.h"
23 // itk include (for itkReadRawBytesAfterSwappingMacro)
24 #include <itkRawImageIO.h>
25 #include <itksys/SystemTools.hxx>
26 #include <itkMetaDataObject.h>
28 //--------------------------------------------------------------------
29 // Read Image Information
30 void clitk::EsrfHstImageIO::ReadImageInformation()
32 clitk::EsrfHstXMLFileReader::Pointer hstXmlReader;
33 hstXmlReader = clitk::EsrfHstXMLFileReader::New();
34 hstXmlReader->SetFilename(m_XmlFileName);
35 hstXmlReader->GenerateOutputInformation();
36 itk::MetaDataDictionary &dic = *(hstXmlReader->GetOutputObject() );
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);
55 this->SetByteOrder(LittleEndian);
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();
64 //--------------------------------------------------------------------
65 // Read Image Information
66 bool clitk::EsrfHstImageIO::CanReadFile(const char* FileNameToRead)
68 std::string ext = itksys::SystemTools::GetFilenameLastExtension(FileNameToRead);
69 if( ext!=std::string(".xml") && ext!=std::string(".vol") )
72 m_XmlFileName = std::string(FileNameToRead);
73 if( ext==std::string(".vol") )
74 m_XmlFileName += std::string(".xml");
76 std::ifstream is(m_XmlFileName.c_str());
80 std::string firstLine;
81 std::getline(is, firstLine);
82 if(firstLine.find( std::string("<!-- PyHST VOLUME XML FILE -->") ) == std::string::npos)
88 //--------------------------------------------------------------------
90 void clitk::EsrfHstImageIO::Read(void * buffer)
92 // Adapted from itkRawImageIO
93 std::ifstream is(m_RawFileName.c_str(), std::ios::binary);
95 itkExceptionMacro(<<"Could not open file " << m_RawFileName);
97 unsigned long numberOfBytesToBeRead = GetComponentSize();
98 for(unsigned int i=0; i<GetNumberOfDimensions(); i++) numberOfBytesToBeRead *= GetDimensions(i);
100 if(!this->ReadBufferAsBinary(is, buffer, numberOfBytesToBeRead)) {
101 itkExceptionMacro(<<"Read failed: Wanted "
102 << numberOfBytesToBeRead
103 << " bytes, but read "
104 << is.gcount() << " bytes.");
106 itkDebugMacro(<< "Reading Done");
108 // Adapted from itkRawImageIO
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 );
125 //--------------------------------------------------------------------
126 // Write Image Information
127 void clitk::EsrfHstImageIO::WriteImageInformation(bool keepOfStream)
131 //--------------------------------------------------------------------
132 // Write Image Information
133 bool clitk::EsrfHstImageIO::CanWriteFile(const char* FileNameToWrite)
138 //--------------------------------------------------------------------
140 void clitk::EsrfHstImageIO::Write(const void * buffer)