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://oncora1.lyon.fnclcc.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 ======================================================================-====*/
18 #ifndef CLITKVFIMAGEIO_CXX
19 #define CLITKVFIMAGEIO_CXX
22 * @file clitkVfImageIO.cxx
23 * @author Simon Rit <simon.rit@gmail.com>
24 * @date Mon Sep 18 10:14:53 2006
26 * @brief VectorField .vf I/O implementation
32 #include "clitkVfImageIO.h"
34 // itk include (for itkReadRawBytesAfterSwappingMacro)
35 #include "itkRawImageIO.h"
37 //====================================================================
38 // Read Image Information
39 void clitk::VfImageIO::ReadImageInformation()
43 clitk::openFileForReading(is, m_FileName);
48 if (mn != "IAMA3DVECTORFIELD") {
49 itkExceptionMacro(<<"read magic number '" << mn << "' while expect IAMA3DVECTORFIELD");
51 // read vf file version
56 itkExceptionMacro(<<"read old format '" << mn << "'. TODO");
59 // read grid size/spacing
60 itk::Vector<unsigned int,3> dim;
61 itk::Vector<double,3> spacing;
62 itk::Vector<double,3> origin;
75 m_HeaderSize = is.tellg();
78 // set dimension values
79 SetNumberOfDimensions(3);
80 for(unsigned int i=0; i<3; i++) {
81 SetDimensions(i,dim[i]);
82 SetSpacing(i,spacing[i]);
83 SetOrigin(i,origin[i]);
86 // set other information
87 SetByteOrderToLittleEndian();
88 SetPixelType(itk::ImageIOBase::VECTOR);
89 SetNumberOfComponents(3);
90 SetComponentType(itk::ImageIOBase::FLOAT);
93 //====================================================================
94 // Read Image Information
95 bool clitk::VfImageIO::CanReadFile(const char* FileNameToRead)
97 std::string filename(FileNameToRead);
98 std::string filenameext = GetExtension(filename);
99 if (filenameext != std::string("vf")) return false;
103 //====================================================================
104 // Read Image Content
105 void clitk::VfImageIO::Read(void * buffer)
107 // Adapted from itkRawImageIO
110 openFileForReading(file, m_FileName);
113 unsigned long streamStart = m_HeaderSize;
114 file.seekg((long)streamStart, std::ios::beg);
116 itkExceptionMacro(<<"File seek failed (Vf Read)");
119 float * tmpBuff = new float[GetImageSizeInComponents()];
120 if(!this->ReadBufferAsBinary(file, tmpBuff, GetImageSizeInBytes())) {
121 itkExceptionMacro(<<"Read failed: Wanted "
122 << GetImageSizeInBytes()
123 << " bytes, but read "
124 << file.gcount() << " bytes.");
126 itkDebugMacro(<< "Reading Done");
128 float *pb = (float *)buffer;
130 float *py = tmpBuff + GetImageSizeInPixels();
131 float *pz = tmpBuff + 2 * GetImageSizeInPixels();
132 const float *pbe = (float *)buffer + GetImageSizeInComponents();
134 *pb++ = (*px++)*GetSpacing(0);
135 *pb++ = (*py++)*GetSpacing(1);
136 *pb++ = (*pz++)*GetSpacing(2);
140 typedef itk::ByteSwapper< float > InternalByteSwapperType;
141 InternalByteSwapperType::SwapRangeFromSystemToLittleEndian((float *)buffer, GetImageSizeInComponents());
144 //====================================================================
145 // Write Image Information
146 void clitk::VfImageIO::WriteImageInformation(bool keepOfStream)
149 if (GetNumberOfDimensions() != 3) {
150 itkExceptionMacro(<<"Write failed: only 3D image for Vf file format yet.");
154 clitk::openFileForWriting(file, m_FileName);
156 // write magic number
157 file << "IAMA3DVECTORFIELD V2 " << std::endl;
158 // write grid size/spacing
159 file << GetDimensions(0) << ' '
160 << GetDimensions(1) << ' '
161 << GetDimensions(2) << ' '
162 << GetSpacing(0) << ' '
163 << GetSpacing(1) << ' '
164 << GetSpacing(2) << ' ' << std::endl;
167 if (!keepOfStream) file.close();
170 //====================================================================
171 // Write Image Information
172 bool clitk::VfImageIO::CanWriteFile(const char* FileNameToWrite)
174 std::string filename(FileNameToWrite);
175 std::string filenameext = GetExtension(filename);
176 if (filenameext != std::string("vf")) return false;
180 //====================================================================
182 void clitk::VfImageIO::Write(const void * buffer)
184 clitk::VfImageIO::WriteImageInformation(true);
186 typedef itk::ByteSwapper< float > InternalByteSwapperType;
187 std::cout << "GetImageSizeInBytes() " << GetImageSizeInBytes() << std::endl;
188 float* tempBuffer = new float[ GetImageSizeInPixels() ];
191 for(int i=0 ; i< 3 ; i++) {
192 float *pb = (float *)buffer;
194 float *ptb = tempBuffer;
195 const float *pbe = (float *)buffer + GetImageSizeInComponents() + i;
197 *ptb++ = (*pb)/GetSpacing(i);
200 InternalByteSwapperType::SwapRangeFromSystemToLittleEndian(tempBuffer,GetImageSizeInPixels());
201 file.write((char*)tempBuffer, GetImageSizeInBytes()/3 );
203 delete [] tempBuffer;
208 #endif /* end #define CLITKVFIMAGEIO_CXX */