]> Creatis software - clitk.git/blobdiff - common/clitkVfImageIO.cxx
Debug RTStruct conversion with empty struc
[clitk.git] / common / clitkVfImageIO.cxx
index f56b13e8cde9535621be2476a46e393fea01ac6a..04108159f4960cc8dbbdda5c103d5cf7c3ddd756 100644 (file)
@@ -1,23 +1,20 @@
 /*=========================================================================
-                                                                                
-  Program:   clitk
-  Module:    $RCSfile: clitkVfImageIO.cxx,v $
-  Language:  C++
-  Date:      $Date: 2010/01/06 13:32:01 $
-  Version:   $Revision: 1.1 $
-                                                                                
-  Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
-  l'Image). All rights reserved. See Doc/License.txt or
-  http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
-                                                                                
-     This software is distributed WITHOUT ANY WARRANTY; without even
-     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-     PURPOSE.  See the above copyright notices for more information.
-                                                                             
-=========================================================================*/
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
 
+  Authors belong to:
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
 
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
 
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+===========================================================================**/
 #ifndef CLITKVFIMAGEIO_CXX
 #define CLITKVFIMAGEIO_CXX
 
  * @file   clitkVfImageIO.cxx
  * @author Simon Rit <simon.rit@gmail.com>
  * @date   Mon Sep 18 10:14:53 2006
- * 
+ *
  * @brief  VectorField .vf I/O implementation
- * 
- * 
+ *
+ *
  */
 
 // clitk include
 
 //====================================================================
 // Read Image Information
-void clitk::VfImageIO::ReadImageInformation() 
+void clitk::VfImageIO::ReadImageInformation()
 {
   // open file
   std::ifstream is;
-  clitk::openFileForReading(is, m_FileName);  
+  clitk::openFileForReading(is, m_FileName);
   // read magic number
-  std::string mn; 
-  is >> mn; 
+  std::string mn;
+  is >> mn;
   //DD(mn);
   if (mn != "IAMA3DVECTORFIELD") {
-       itkExceptionMacro(<<"read magic number '" << mn << "' while expect IAMA3DVECTORFIELD");
-  }    
+    itkExceptionMacro(<<"read magic number '" << mn << "' while expect IAMA3DVECTORFIELD");
+  }
   // read vf file version
-  skipComment(is); 
-  is >> mn; 
+  skipComment(is);
+  is >> mn;
   //DD(mn);
   if (mn != "V2") {
-       itkExceptionMacro(<<"read old format '" << mn << "'. TODO");
-  }  
-       
+    itkExceptionMacro(<<"read old format '" << mn << "'. TODO");
+  }
+
   // read grid size/spacing
   itk::Vector<unsigned int,3> dim;
   itk::Vector<double,3> spacing;
   itk::Vector<double,3> origin;
   origin.Fill(0.0);
-  skipComment(is); 
-  is >> dim[0]; 
-  is >> dim[1]; 
+  skipComment(is);
+  is >> dim[0];
+  is >> dim[1];
   is >> dim[2];
   // DD(dim);
   is >> spacing[0];
   is >> spacing[1];
   is >> spacing[2];
   // DD(spacing);
-    
+
   // get header size
   m_HeaderSize = is.tellg();
   m_HeaderSize+=2;
@@ -81,21 +78,21 @@ void clitk::VfImageIO::ReadImageInformation()
   // set dimension values
   SetNumberOfDimensions(3);
   for(unsigned int i=0; i<3; i++) {
-       SetDimensions(i,dim[i]);
-       SetSpacing(i,spacing[i]);
-       SetOrigin(i,origin[i]);
+    SetDimensions(i,dim[i]);
+    SetSpacing(i,spacing[i]);
+    SetOrigin(i,origin[i]);
   }
 
   // set other information
   SetByteOrderToLittleEndian();
   SetPixelType(itk::ImageIOBase::VECTOR);
-  SetNumberOfComponents(3);  
+  SetNumberOfComponents(3);
   SetComponentType(itk::ImageIOBase::FLOAT);
 } ////
 
 //====================================================================
 // Read Image Information
-bool clitk::VfImageIO::CanReadFile(const char* FileNameToRead) 
+bool clitk::VfImageIO::CanReadFile(const char* FileNameToRead)
 {
   std::string filename(FileNameToRead);
   std::string filenameext = GetExtension(filename);
@@ -105,7 +102,7 @@ bool clitk::VfImageIO::CanReadFile(const char* FileNameToRead)
 
 //====================================================================
 // Read Image Content
-void clitk::VfImageIO::Read(void * buffer) 
+void clitk::VfImageIO::Read(void * buffer)
 {
   // Adapted from itkRawImageIO
 
@@ -116,30 +113,30 @@ void clitk::VfImageIO::Read(void * buffer)
   unsigned long streamStart = m_HeaderSize;
   file.seekg((long)streamStart, std::ios::beg);
   if ( file.fail() ) {
-       itkExceptionMacro(<<"File seek failed (Vf Read)");
+    itkExceptionMacro(<<"File seek failed (Vf Read)");
   }
-       
+
   float * tmpBuff = new float[GetImageSizeInComponents()];
   if(!this->ReadBufferAsBinary(file, tmpBuff, GetImageSizeInBytes())) {
-       itkExceptionMacro(<<"Read failed: Wanted " 
-                                         << GetImageSizeInBytes()
-                                         << " bytes, but read " 
-                                         << file.gcount() << " bytes.");
+    itkExceptionMacro(<<"Read failed: Wanted "
+                      << GetImageSizeInBytes()
+                      << " bytes, but read "
+                      << file.gcount() << " bytes.");
   }
   itkDebugMacro(<< "Reading Done");
-  
+
   float *pb = (float *)buffer;
   float *px = tmpBuff;
   float *py = tmpBuff + GetImageSizeInPixels();
   float *pz = tmpBuff + 2 * GetImageSizeInPixels();
   const float *pbe = (float *)buffer + GetImageSizeInComponents();
-  while(pb != pbe){
+  while(pb != pbe) {
     *pb++ = (*px++)*GetSpacing(0);
     *pb++ = (*py++)*GetSpacing(1);
     *pb++ = (*pz++)*GetSpacing(2);
   }
   delete [] tmpBuff;
-  
+
   typedef itk::ByteSwapper< float > InternalByteSwapperType;
   InternalByteSwapperType::SwapRangeFromSystemToLittleEndian((float *)buffer, GetImageSizeInComponents());
 }
@@ -150,25 +147,26 @@ void clitk::VfImageIO::WriteImageInformation(bool keepOfStream)
 {
   // Check dimension
   if (GetNumberOfDimensions() != 3) {
-       itkExceptionMacro(<<"Write failed: only 3D image for Vf file format yet.");
+    itkExceptionMacro(<<"Write failed: only 3D image for Vf file format yet.");
   }
 
   // Open the file
   clitk::openFileForWriting(file, m_FileName);
+  file.precision(40);
   // write magic number
   file << "IAMA3DVECTORFIELD V2 " << std::endl;
   // write grid size/spacing
-  file << GetDimensions(0) << ' ' 
-          << GetDimensions(1) << ' ' 
-          << GetDimensions(2) << ' '
-          << GetSpacing(0) << ' ' 
-          << GetSpacing(1) << ' ' 
-          << GetSpacing(2) << ' ' << std::endl;
+  file << GetDimensions(0) << ' '
+       << GetDimensions(1) << ' '
+       << GetDimensions(2) << ' '
+       << GetSpacing(0) << ' '
+       << GetSpacing(1) << ' '
+       << GetSpacing(2) << ' ' << std::endl;
 
   // close file
-  if (!keepOfStream) file.close();     
+  if (!keepOfStream) file.close();
 }
-  
+
 //====================================================================
 // Write Image Information
 bool clitk::VfImageIO::CanWriteFile(const char* FileNameToWrite)
@@ -181,29 +179,29 @@ bool clitk::VfImageIO::CanWriteFile(const char* FileNameToWrite)
 
 //====================================================================
 // Write Image
-void clitk::VfImageIO::Write(const void * buffer) 
+void clitk::VfImageIO::Write(const void * buffer)
 {
   clitk::VfImageIO::WriteImageInformation(true);
-  
+
   typedef itk::ByteSwapper< float > InternalByteSwapperType;
   std::cout << "GetImageSizeInBytes() " << GetImageSizeInBytes() << std::endl;
   float* tempBuffer = new float[ GetImageSizeInPixels() ];
 
 
-  for(int i=0 ; i< 3 ; i++){
-       float *pb = (float *)buffer;
-       pb+=i;
-       float *ptb = tempBuffer;
-       const float *pbe = (float *)buffer + GetImageSizeInComponents() + i;
-       while(pb != pbe){
-         *ptb++ = (*pb)/GetSpacing(i);
-         pb+=3;
-       }
-       InternalByteSwapperType::SwapRangeFromSystemToLittleEndian(tempBuffer,GetImageSizeInPixels());
-       file.write((char*)tempBuffer, GetImageSizeInBytes()/3 );
+  for(int i=0 ; i< 3 ; i++) {
+    float *pb = (float *)buffer;
+    pb+=i;
+    float *ptb = tempBuffer;
+    const float *pbe = (float *)buffer + GetImageSizeInComponents() + i;
+    while(pb != pbe) {
+      *ptb++ = (*pb)/GetSpacing(i);
+      pb+=3;
+    }
+    InternalByteSwapperType::SwapRangeFromSystemToLittleEndian(tempBuffer,GetImageSizeInPixels());
+    file.write((char*)tempBuffer, GetImageSizeInBytes()/3 );
   }
-  delete [] tempBuffer;                                                
-  
+  delete [] tempBuffer;
+
   file.close();
 } ////