]> Creatis software - clitk.git/blob - common/clitkVfImageIO.cxx
changes in license header
[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://www.centreleonberard.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   file.precision(40);
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;
165
166   // close file
167   if (!keepOfStream) file.close();
168 }
169
170 //====================================================================
171 // Write Image Information
172 bool clitk::VfImageIO::CanWriteFile(const char* FileNameToWrite)
173 {
174   std::string filename(FileNameToWrite);
175   std::string filenameext = GetExtension(filename);
176   if (filenameext != std::string("vf")) return false;
177   return true;
178 }
179
180 //====================================================================
181 // Write Image
182 void clitk::VfImageIO::Write(const void * buffer)
183 {
184   clitk::VfImageIO::WriteImageInformation(true);
185
186   typedef itk::ByteSwapper< float > InternalByteSwapperType;
187   std::cout << "GetImageSizeInBytes() " << GetImageSizeInBytes() << std::endl;
188   float* tempBuffer = new float[ GetImageSizeInPixels() ];
189
190
191   for(int i=0 ; i< 3 ; i++) {
192     float *pb = (float *)buffer;
193     pb+=i;
194     float *ptb = tempBuffer;
195     const float *pbe = (float *)buffer + GetImageSizeInComponents() + i;
196     while(pb != pbe) {
197       *ptb++ = (*pb)/GetSpacing(i);
198       pb+=3;
199     }
200     InternalByteSwapperType::SwapRangeFromSystemToLittleEndian(tempBuffer,GetImageSizeInPixels());
201     file.write((char*)tempBuffer, GetImageSizeInBytes()/3 );
202   }
203   delete [] tempBuffer;
204
205   file.close();
206 } ////
207
208 #endif /* end #define CLITKVFIMAGEIO_CXX */
209