]> Creatis software - creaVtk.git/commitdiff
#2711 creaVtk Feature New Normal - new Box ReadMHDPlane
authorEduardo DAVILA <eduardo.davila@creatis.insa-lyon.fr>
Fri, 22 Apr 2016 07:56:54 +0000 (09:56 +0200)
committerEduardo DAVILA <eduardo.davila@creatis.insa-lyon.fr>
Fri, 22 Apr 2016 07:56:54 +0000 (09:56 +0200)
bbtk_creaVtk_PKG/src/bbcreaVtkReadMHDPlane.cxx
bbtk_creaVtk_PKG/src/bbcreaVtkReadMHDPlane.h

index ae2b8cc21791193c069a544c9487f70a6bacda4f..56e8590e137c918e16517345266511a10bb37e17 100644 (file)
@@ -6,6 +6,23 @@
 
 #include "stdio.h"
 
+#include <sys/types.h>
+#include <unistd.h> 
+
+
+
+
+#define _LARGEFILE64_SOURCE
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
 namespace bbcreaVtk
 {
 
@@ -14,9 +31,9 @@ BBTK_BLACK_BOX_IMPLEMENTATION(ReadMHDPlane,bbtk::AtomicBlackBox);
 //===== 
 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
 //===== 
-void ReadMHDPlane::Process()
-{
 
+void ReadMHDPlane::ReadNormalMHD()
+{
 // THE MAIN PROCESSING METHOD BODY
 //   Here we simply set the input 'In' value to the output 'Out'
 //   And print out the output value
@@ -66,7 +83,7 @@ void ReadMHDPlane::Process()
        } // if YZ || XZ
 
        vtkImageData *newImage;
-       long int newHeaderSize;
+       long long int newHeaderSize;
     std::string newFileName=inputfilename+"-OneSlice";
 
     int sx,sy,sz;
@@ -87,7 +104,7 @@ void ReadMHDPlane::Process()
                        {
                                sscanf(mystring,"%s %s %d %d %d",strTmp, strTmp, &sx, &sy,&sz);
                                sprintf(mystring,"DimSize = %d %d\n",sx,sy);
-                       newHeaderSize = sx*sy*bbGetInputSlice();
+                       newHeaderSize = (long long int)sx*(long long int)sy*(long long int)bbGetInputSlice();
                                if (bbGetInputSlice()>=sz) {ok=false;}
                        } // if
                if (strncmp("ElementType = MET_CHAR",mystring,22)==0)   { newHeaderSize=newHeaderSize*1; }
@@ -100,7 +117,7 @@ void ReadMHDPlane::Process()
                if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");}
                if (strncmp("ElementDataFile",mystring,15)==0) 
                        {
-                               fprintf(ffOut,"HeaderSize = %ld\n\n", newHeaderSize );
+                               fprintf(ffOut,"HeaderSize = %lld\n\n", newHeaderSize );
                        } // if
                fprintf(ffOut,mystring); 
        } // while
@@ -146,6 +163,198 @@ void ReadMHDPlane::Process()
 
        bbSetOutputOut( newImage );
 
+}
+
+void ReadMHDPlane::Read64lseek()
+{
+
+       int             dimX=-1,dimY=-1,dimZ=-1;
+       int             dim=-1;
+       std::string formattype("VOID");
+       std::string elementdatafile("VOID");
+       float           spcX=-1,spcY=-1,spcZ=-1;
+       float           ox=-1,oy=-1,oz=-1;
+       long int        headersize=0;
+
+       vtkImageData *newImage;
+
+       char mystring[250];
+       char strTmp[30];
+       char strTmp2[30];
+       FILE *ffIn      = fopen(bbGetInputFileName().c_str(),"r");
+       long long dataSize;
+
+
+
+       if (ffIn!=NULL)
+       {
+               newImage = vtkImageData::New();
+
+               while(!feof(ffIn))
+               {
+           strcpy(mystring,"\n");
+                       fgets(mystring,250,ffIn);
+               if (strncmp("NDims",mystring,5)==0)                     { sscanf(mystring,"%s %s %d"            ,strTmp, strTmp, &dim);                                 }
+               if (strncmp("DimSize",mystring,6)==0)                   { sscanf(mystring,"%s %s %d %d %d"      ,strTmp, strTmp, &dimX, &dimY,&dimZ);   } 
+                       if (strncmp("ElementType",mystring,11)==0)              { sscanf(mystring,"%s %s %s"            ,strTmp, strTmp, strTmp2); formattype=strTmp2;                  }
+                       if (strncmp("ElementSpacing",mystring,11)==0)   { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &spcX,&spcY,&spcZ);    }
+               if (strncmp("Offset",mystring,6)==0)                    { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &ox, &oy, &oz);                }
+               if (strncmp("HeaderSize",mystring,10)==0)               { sscanf(mystring,"%s %s %ld"           ,strTmp, strTmp, &headersize);                  }
+                       if (strncmp("ElementDataFile",mystring,15)==0)  { sscanf(mystring,"%s %s %s"            ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2;             }
+
+               if (strncmp("ElementType = MET_CHAR",mystring,22)==0)   { newImage->SetScalarTypeToChar();                      dataSize=sizeof(char);                  }
+               if (strncmp("ElementType = VTK_CHAR",mystring,22)==0)   { newImage->SetScalarTypeToChar();                      dataSize=sizeof(char);                  }
+               if (strncmp("ElementType = MET_UCHAR",mystring,23)==0)  { newImage->SetScalarTypeToUnsignedChar();      dataSize=sizeof(unsigned char); }
+               if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0)  { newImage->SetScalarTypeToUnsignedChar();      dataSize=sizeof(unsigned char); }
+               if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newImage->SetScalarTypeToUnsignedShort();     dataSize=sizeof(unsigned short);}
+               if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { newImage->SetScalarTypeToUnsignedShort();     dataSize=sizeof(unsigned short);}
+               if (strncmp("ElementType = MET_SHORT",mystring,23)==0)  { newImage->SetScalarTypeToShort();                     dataSize=sizeof(short);                 }
+               if (strncmp("ElementType = VTK_SHORT",mystring,23)==0)  { newImage->SetScalarTypeToShort();                     dataSize=sizeof(short);                 }
+               if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)  { newImage->SetScalarTypeToFloat();                     dataSize=sizeof(float);                 }
+               if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)  { newImage->SetScalarTypeToFloat();                     dataSize=sizeof(float);                 }
+               if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0) { newImage->SetScalarTypeToDouble();            dataSize=sizeof(double);                }
+               if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0) { newImage->SetScalarTypeToDouble();            dataSize=sizeof(double);                }
+
+       } // while
+               fclose(ffIn);
+
+               newImage->Initialize();
+
+
+               int fd;
+               long long ret;
+               std::size_t found;
+               std::string filename;
+               found                                   =       bbGetInputFileName().find_last_of("/\\");
+               filename                                = bbGetInputFileName().substr(0,found+1) + elementdatafile ;
+               long long pos;
+
+               long long lsize                 = dimX*dimY*1 *dataSize;
+               fd = open ( filename.c_str() ,  O_RDONLY|O_LARGEFILE );
+               if (fd < 0)
+               {
+                       printf("EED ReadMHDPlane::Read64lseek \n");
+                       fprintf(stderr, "%s\n", strerror(errno));
+                       exit(1);
+               }
+
+               if (bbGetInputDirectionPlane()=="XY") 
+               { 
+                       newImage->SetSpacing( spcX,spcY,spcZ );
+                       newImage->SetDimensions(  dimX,dimY,1 );
+                       newImage->SetWholeExtent(0,  dimX-1,0,dimY-1,0,0 );
+                       newImage->SetExtent(0,  dimX-1,0,dimY-1,0,0 );
+                       newImage->SetNumberOfScalarComponents(1);
+                       newImage->AllocateScalars();
+                       newImage->Update();
+                       pos = dimX*dimY*(long long)bbGetInputSlice()*dataSize;
+                       if (lseek64(fd, pos, SEEK_SET) < 0) 
+                       {
+                               printf("EED ReadMHDPlane::Read64lseek \n");
+                               fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
+                               exit(1);
+                       }
+
+                       if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0) 
+                       {
+                               fprintf(stderr, "Failed reading: %s\n", strerror(errno));
+                               exit(1);
+                       }
+               }  // if PLANE XY
+
+
+               if (bbGetInputDirectionPlane()=="XZ") 
+               { 
+                       long long int j;
+                       newImage->SetSpacing( spcX,spcZ,spcY );
+                       newImage->SetDimensions(  dimX,dimZ,1 );
+                       newImage->SetWholeExtent(0,  dimX-1,0,dimZ-1,0,0 );
+                       newImage->SetExtent(0,  dimX-1,0,dimZ-1,0,0 );
+                       newImage->SetNumberOfScalarComponents(1);
+                       newImage->AllocateScalars();
+                       newImage->Update();
+                       long long sizeBytesPlane = dimX*dimY*dataSize;
+                       pos = dimX*(long long int)bbGetInputSlice()*dataSize;
+                       void *pImage = newImage->GetScalarPointer();
+                       for (j=0;j<dimZ;j++)
+                       {
+printf("EED EED ReadMHDPlane::Read64lseek XZ j=%d \n", j);
+                               if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0) 
+                               {
+                                       printf("EED ReadMHDPlane::Read64lseek \n");
+                                       fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
+                                       exit(1);
+                               }
+
+                               if ((ret = read(fd, pImage , dimX*dataSize)) < 0) 
+                               {
+                                       fprintf(stderr, "Failed reading: %s\n", strerror(errno));
+                                       exit(1);
+                               }
+                               pImage=pImage+dimX*dataSize;
+                       } // for j
+               }  // if PLANE XY
+
+
+               if (bbGetInputDirectionPlane()=="YZ") 
+               { 
+                       long long int j;
+                       long long int i;
+                       newImage->SetSpacing( spcY,spcZ,spcX );
+                       newImage->SetDimensions(  dimY,dimZ,1 );
+                       newImage->SetWholeExtent(0,  dimY-1,0,dimZ-1,0,0 );
+                       newImage->SetExtent(0,  dimY-1,0,dimZ-1,0,0 );
+                       newImage->SetNumberOfScalarComponents(1);
+                       newImage->AllocateScalars();
+                       newImage->Update();
+                       long long sizeBytesPlane = dimX*dimY*dataSize;
+//                     pos = dimX*(long long int)bbGetInputSlice()*dataSize;
+                       void *pImage;
+                       for (j=0;j<dimZ;j++)
+                       {
+printf("EED EED ReadMHDPlane::Read64lseek YZ i=%d j=%d \n", i,j);
+                               for (i=0;i<dimY;i++)
+                               {
+                                       pos = ((long long int)bbGetInputSlice() + i*dimX + j*dimX*dimY)*dataSize ;
+                                       if (lseek64(fd, pos  , SEEK_SET) < 0) 
+                                       {
+                                               printf("EED ReadMHDPlane::Read64lseek \n");
+                                               fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
+                                               exit(1);
+                                       }
+
+                                       pImage=newImage->GetScalarPointer(i, j,0 );
+                                       if ((ret = read(fd, pImage , dataSize)) < 0) 
+                                       {
+                                               fprintf(stderr, "Failed reading: %s\n", strerror(errno));
+                                               exit(1);
+                                       }
+                               }
+                       } // for j
+               }  // if PLANE XY
+
+       
+               close (fd);
+
+               bbSetOutputOut( newImage );
+       } // if
+
+
+}
+
+
+void ReadMHDPlane::Process()
+{
+       if (bbGetInputType()==0)
+       {
+               ReadNormalMHD();
+       }       
+
+       if (bbGetInputType()==1)
+       {
+               Read64lseek();
+       }       
+
 }
 //===== 
 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
@@ -157,6 +366,7 @@ void ReadMHDPlane::bbUserSetDefaultValues()
 //    Here we initialize the input 'In' to 0
    bbSetInputFileName("");
    bbSetInputSlice(0);
+   bbSetInputType(0);
    bbSetInputDirectionPlane("XY");
   
 }
index 53cc07c0b4b9e33f559574040ec8ae7f61553277..8dfd228a0f42e86dcb0f7072b83bf54c009f1d00 100644 (file)
@@ -3,6 +3,12 @@
 //===== 
 #ifndef __bbcreaVtkReadMHDPlane_h_INCLUDED__
 #define __bbcreaVtkReadMHDPlane_h_INCLUDED__
+
+
+
+
+
+
 #include "bbcreaVtk_EXPORT.h"
 #include "bbtkAtomicBlackBox.h"
 #include "iostream"
@@ -24,8 +30,13 @@ class bbcreaVtk_EXPORT ReadMHDPlane
   BBTK_DECLARE_INPUT(FileName,std::string);
   BBTK_DECLARE_INPUT(Slice,int);
   BBTK_DECLARE_INPUT(DirectionPlane,std::string);
+  BBTK_DECLARE_INPUT(Type,int);
   BBTK_DECLARE_OUTPUT(Out,vtkImageData*); 
   BBTK_PROCESS(Process);
+       
+  void ReadNormalMHD();
+  void Read64lseek();
+
   void Process();
 //===== 
 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
@@ -40,6 +51,7 @@ BBTK_BEGIN_DESCRIBE_BLACK_BOX(ReadMHDPlane,bbtk::AtomicBlackBox);
  BBTK_INPUT(ReadMHDPlane,FileName,"Image file name",std::string,"");
  BBTK_INPUT(ReadMHDPlane,Slice,"Slice number",int,"");
  BBTK_INPUT(ReadMHDPlane,DirectionPlane,"Direction plane: XY (default), YZ , XZ",std::string,"");
+ BBTK_INPUT(ReadMHDPlane,Type,"(0 default) 0 = Normal mhd,  1 = lseek64",int,"");
  BBTK_OUTPUT(ReadMHDPlane,Out,"Image 2D",vtkImageData*,"");
 BBTK_END_DESCRIBE_BLACK_BOX(ReadMHDPlane);
 //=====