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