1 /*=========================================================================
4 Module: $RCSfile: clitkVfImageIO.cxx,v $
6 Date: $Date: 2010/01/06 13:32:01 $
7 Version: $Revision: 1.1 $
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.
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.
17 =========================================================================*/
21 #ifndef CLITKVFIMAGEIO_CXX
22 #define CLITKVFIMAGEIO_CXX
25 * @file clitkVfImageIO.cxx
26 * @author Simon Rit <simon.rit@gmail.com>
27 * @date Mon Sep 18 10:14:53 2006
29 * @brief VectorField .vf I/O implementation
35 #include "clitkVfImageIO.h"
37 // itk include (for itkReadRawBytesAfterSwappingMacro)
38 #include "itkRawImageIO.h"
40 //====================================================================
41 // Read Image Information
42 void clitk::VfImageIO::ReadImageInformation()
46 clitk::openFileForReading(is, m_FileName);
51 if (mn != "IAMA3DVECTORFIELD") {
52 itkExceptionMacro(<<"read magic number '" << mn << "' while expect IAMA3DVECTORFIELD");
54 // read vf file version
59 itkExceptionMacro(<<"read old format '" << mn << "'. TODO");
62 // read grid size/spacing
63 itk::Vector<unsigned int,3> dim;
64 itk::Vector<double,3> spacing;
65 itk::Vector<double,3> origin;
78 m_HeaderSize = is.tellg();
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]);
89 // set other information
90 SetByteOrderToLittleEndian();
91 SetPixelType(itk::ImageIOBase::VECTOR);
92 SetNumberOfComponents(3);
93 SetComponentType(itk::ImageIOBase::FLOAT);
96 //====================================================================
97 // Read Image Information
98 bool clitk::VfImageIO::CanReadFile(const char* FileNameToRead)
100 std::string filename(FileNameToRead);
101 std::string filenameext = GetExtension(filename);
102 if (filenameext != std::string("vf")) return false;
106 //====================================================================
107 // Read Image Content
108 void clitk::VfImageIO::Read(void * buffer)
110 // Adapted from itkRawImageIO
113 openFileForReading(file, m_FileName);
116 unsigned long streamStart = m_HeaderSize;
117 file.seekg((long)streamStart, std::ios::beg);
119 itkExceptionMacro(<<"File seek failed (Vf Read)");
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.");
129 itkDebugMacro(<< "Reading Done");
131 float *pb = (float *)buffer;
133 float *py = tmpBuff + GetImageSizeInPixels();
134 float *pz = tmpBuff + 2 * GetImageSizeInPixels();
135 const float *pbe = (float *)buffer + GetImageSizeInComponents();
137 *pb++ = (*px++)*GetSpacing(0);
138 *pb++ = (*py++)*GetSpacing(1);
139 *pb++ = (*pz++)*GetSpacing(2);
143 typedef itk::ByteSwapper< float > InternalByteSwapperType;
144 InternalByteSwapperType::SwapRangeFromSystemToLittleEndian((float *)buffer, GetImageSizeInComponents());
147 //====================================================================
148 // Write Image Information
149 void clitk::VfImageIO::WriteImageInformation(bool keepOfStream)
152 if (GetNumberOfDimensions() != 3) {
153 itkExceptionMacro(<<"Write failed: only 3D image for Vf file format yet.");
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;
169 if (!keepOfStream) file.close();
172 //====================================================================
173 // Write Image Information
174 bool clitk::VfImageIO::CanWriteFile(const char* FileNameToWrite)
176 std::string filename(FileNameToWrite);
177 std::string filenameext = GetExtension(filename);
178 if (filenameext != std::string("vf")) return false;
182 //====================================================================
184 void clitk::VfImageIO::Write(const void * buffer)
186 clitk::VfImageIO::WriteImageInformation(true);
188 typedef itk::ByteSwapper< float > InternalByteSwapperType;
189 std::cout << "GetImageSizeInBytes() " << GetImageSizeInBytes() << std::endl;
190 float* tempBuffer = new float[ GetImageSizeInPixels() ];
193 for(int i=0 ; i< 3 ; i++){
194 float *pb = (float *)buffer;
196 float *ptb = tempBuffer;
197 const float *pbe = (float *)buffer + GetImageSizeInComponents() + i;
199 *ptb++ = (*pb)/GetSpacing(i);
202 InternalByteSwapperType::SwapRangeFromSystemToLittleEndian(tempBuffer,GetImageSizeInPixels());
203 file.write((char*)tempBuffer, GetImageSizeInBytes()/3 );
205 delete [] tempBuffer;
210 #endif /* end #define CLITKVFIMAGEIO_CXX */