[Insight-developers] Incorrect byte order when writing vector float images in VTK format

Gunnar Farneback gunnar at bwh.harvard.edu
Fri Aug 20 12:43:15 EDT 2004


I'm writing both scalar and vector float images in VTK format. To my
surprise these turn out to have different byte order. A little
debugging shows that when reaching the switch statement at line 479 of
itkVTKImageIO.cxx,

    int size = this->GetPixelSize();
    const unsigned long int numbytes=this->GetImageSizeInBytes();
    char * tempmemory=new char[numbytes];
    memcpy(tempmemory,buffer,numbytes);
    switch( size )
      {
      case 2:
      {
      ByteSwapper<short>::SwapRangeFromSystemToBigEndian(reinterpret_cast<short *>(tempmemory), this->GetImageSizeInComponents() );
      }
      break;
      case 4:
      {
      ByteSwapper<float>::SwapRangeFromSystemToBigEndian(reinterpret_cast<float *>(tempmemory), this->GetImageSizeInComponents() );
      }
      break;
      case 8:
      {
      ByteSwapper<double>::SwapRangeFromSystemToBigEndian(reinterpret_cast<double *>(tempmemory), this->GetImageSizeInComponents() );
      }
      break;
      }
    file.write(static_cast<const char*>(tempmemory), this->GetImageSizeInBytes());
    delete [] tempmemory;

in the case of an Image<float, 3>, size is 12. As far as I can tell
the problem is simply that size should be the component size rather
than the pixel size. Presumably the corresponding code for reading has
the same problem.

Unless there's something more to this, the attached patch should solve
the problem (both for reading and writing).

/Gunnar
-------------- next part --------------
Index: itkVTKImageIO.cxx
===================================================================
RCS file: /cvsroot/Insight/Insight/Code/IO/itkVTKImageIO.cxx,v
retrieving revision 1.30
diff -u -r1.30 itkVTKImageIO.cxx
--- a/itkVTKImageIO.cxx	14 May 2004 15:12:37 -0000	1.30
+++ b/itkVTKImageIO.cxx	20 Aug 2004 17:12:40 -0000
@@ -382,7 +382,7 @@
   else
     {
     file.read(static_cast<char*>(buffer), this->GetImageSizeInBytes());
-    int size = this->GetPixelSize();
+    int size = this->GetComponentSize();
     switch( size )
       {
       case 2:
@@ -472,7 +472,7 @@
     }
   else //binary
     {
-    int size = this->GetPixelSize();
+    int size = this->GetComponentSize();
     const unsigned long int numbytes=this->GetImageSizeInBytes();
     char * tempmemory=new char[numbytes];
     memcpy(tempmemory,buffer,numbytes);


More information about the Insight-developers mailing list