]> Creatis software - clitk.git/blob - common/clitkVfImageIO.cxx
removed headers
[clitk.git] / common / clitkVfImageIO.cxx
1 #ifndef CLITKVFIMAGEIO_CXX
2 #define CLITKVFIMAGEIO_CXX
3
4 /**
5  * @file   clitkVfImageIO.cxx
6  * @author Simon Rit <simon.rit@gmail.com>
7  * @date   Mon Sep 18 10:14:53 2006
8  * 
9  * @brief  VectorField .vf I/O implementation
10  * 
11  * 
12  */
13
14 // clitk include
15 #include "clitkVfImageIO.h"
16
17 // itk include (for itkReadRawBytesAfterSwappingMacro)
18 #include "itkRawImageIO.h"
19
20 //====================================================================
21 // Read Image Information
22 void clitk::VfImageIO::ReadImageInformation() 
23 {
24   // open file
25   std::ifstream is;
26   clitk::openFileForReading(is, m_FileName);  
27   // read magic number
28   std::string mn; 
29   is >> mn; 
30   //DD(mn);
31   if (mn != "IAMA3DVECTORFIELD") {
32         itkExceptionMacro(<<"read magic number '" << mn << "' while expect IAMA3DVECTORFIELD");
33   }     
34   // read vf file version
35   skipComment(is); 
36   is >> mn; 
37   //DD(mn);
38   if (mn != "V2") {
39         itkExceptionMacro(<<"read old format '" << mn << "'. TODO");
40   }  
41         
42   // read grid size/spacing
43   itk::Vector<unsigned int,3> dim;
44   itk::Vector<double,3> spacing;
45   itk::Vector<double,3> origin;
46   origin.Fill(0.0);
47   skipComment(is); 
48   is >> dim[0]; 
49   is >> dim[1]; 
50   is >> dim[2];
51   // DD(dim);
52   is >> spacing[0];
53   is >> spacing[1];
54   is >> spacing[2];
55   // DD(spacing);
56     
57   // get header size
58   m_HeaderSize = is.tellg();
59   m_HeaderSize+=2;
60
61   // set dimension values
62   SetNumberOfDimensions(3);
63   for(unsigned int i=0; i<3; i++) {
64         SetDimensions(i,dim[i]);
65         SetSpacing(i,spacing[i]);
66         SetOrigin(i,origin[i]);
67   }
68
69   // set other information
70   SetByteOrderToLittleEndian();
71   SetPixelType(itk::ImageIOBase::VECTOR);
72   SetNumberOfComponents(3);  
73   SetComponentType(itk::ImageIOBase::FLOAT);
74 } ////
75
76 //====================================================================
77 // Read Image Information
78 bool clitk::VfImageIO::CanReadFile(const char* FileNameToRead) 
79 {
80   std::string filename(FileNameToRead);
81   std::string filenameext = GetExtension(filename);
82   if (filenameext != std::string("vf")) return false;
83   return true;
84 } ////
85
86 //====================================================================
87 // Read Image Content
88 void clitk::VfImageIO::Read(void * buffer) 
89 {
90   // Adapted from itkRawImageIO
91
92   std::ifstream file;
93   openFileForReading(file, m_FileName);
94
95   // Offset into file
96   unsigned long streamStart = m_HeaderSize;
97   file.seekg((long)streamStart, std::ios::beg);
98   if ( file.fail() ) {
99         itkExceptionMacro(<<"File seek failed (Vf Read)");
100   }
101         
102   float * tmpBuff = new float[GetImageSizeInComponents()];
103   if(!this->ReadBufferAsBinary(file, tmpBuff, GetImageSizeInBytes())) {
104         itkExceptionMacro(<<"Read failed: Wanted " 
105                                           << GetImageSizeInBytes()
106                                           << " bytes, but read " 
107                                           << file.gcount() << " bytes.");
108   }
109   itkDebugMacro(<< "Reading Done");
110   
111   float *pb = (float *)buffer;
112   float *px = tmpBuff;
113   float *py = tmpBuff + GetImageSizeInPixels();
114   float *pz = tmpBuff + 2 * GetImageSizeInPixels();
115   const float *pbe = (float *)buffer + GetImageSizeInComponents();
116   while(pb != pbe){
117     *pb++ = (*px++)*GetSpacing(0);
118     *pb++ = (*py++)*GetSpacing(1);
119     *pb++ = (*pz++)*GetSpacing(2);
120   }
121   delete [] tmpBuff;
122   
123   typedef itk::ByteSwapper< float > InternalByteSwapperType;
124   InternalByteSwapperType::SwapRangeFromSystemToLittleEndian((float *)buffer, GetImageSizeInComponents());
125 }
126
127 //====================================================================
128 // Write Image Information
129 void clitk::VfImageIO::WriteImageInformation(bool keepOfStream)
130 {
131   // Check dimension
132   if (GetNumberOfDimensions() != 3) {
133         itkExceptionMacro(<<"Write failed: only 3D image for Vf file format yet.");
134   }
135
136   // Open the file
137   clitk::openFileForWriting(file, m_FileName);
138   // write magic number
139   file << "IAMA3DVECTORFIELD V2 " << std::endl;
140   // write grid size/spacing
141   file << GetDimensions(0) << ' ' 
142            << GetDimensions(1) << ' ' 
143            << GetDimensions(2) << ' '
144            << GetSpacing(0) << ' ' 
145            << GetSpacing(1) << ' ' 
146            << GetSpacing(2) << ' ' << std::endl;
147
148   // close file
149   if (!keepOfStream) file.close();      
150 }
151   
152 //====================================================================
153 // Write Image Information
154 bool clitk::VfImageIO::CanWriteFile(const char* FileNameToWrite)
155 {
156   std::string filename(FileNameToWrite);
157   std::string filenameext = GetExtension(filename);
158   if (filenameext != std::string("vf")) return false;
159   return true;
160 }
161
162 //====================================================================
163 // Write Image
164 void clitk::VfImageIO::Write(const void * buffer) 
165 {
166   clitk::VfImageIO::WriteImageInformation(true);
167   
168   typedef itk::ByteSwapper< float > InternalByteSwapperType;
169   std::cout << "GetImageSizeInBytes() " << GetImageSizeInBytes() << std::endl;
170   float* tempBuffer = new float[ GetImageSizeInPixels() ];
171
172
173   for(int i=0 ; i< 3 ; i++){
174         float *pb = (float *)buffer;
175         pb+=i;
176         float *ptb = tempBuffer;
177         const float *pbe = (float *)buffer + GetImageSizeInComponents() + i;
178         while(pb != pbe){
179           *ptb++ = (*pb)/GetSpacing(i);
180           pb+=3;
181         }
182         InternalByteSwapperType::SwapRangeFromSystemToLittleEndian(tempBuffer,GetImageSizeInPixels());
183         file.write((char*)tempBuffer, GetImageSizeInBytes()/3 );
184   }
185   delete [] tempBuffer;                                         
186   
187   file.close();
188 } ////
189
190 #endif /* end #define CLITKVFIMAGEIO_CXX */
191