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