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

index f7a1d106928f6fd5d8cf0231025acda495676914..d6818392ee6566160afc72b6f71da87257fe5e43 100644 (file)
@@ -78,6 +78,8 @@ void ReadMHDPlane::ReadNormalMHD()
 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
 
        std::string inputfilename;
+       int slice;
+       int width;
 
        if (bbGetInputDirectionPlane()=="XY")
        {
@@ -110,6 +112,13 @@ void ReadMHDPlane::ReadNormalMHD()
                } // XZ
        } // if YZ || XZ
 
+       width = bbGetInputWidth();
+       if (width<=0 ) { width=1; }
+
+       slice = bbGetInputSlice();
+       if (slice<0 ) { slice=0; }
+
+
        vtkImageData *newImage;
        long long int newHeaderSize;
     std::string newFileName=inputfilename+"-OneSlice";
@@ -127,12 +136,26 @@ void ReadMHDPlane::ReadNormalMHD()
        {
            strcpy(mystring,"\n");
                        fgets(mystring,250,ffIn);
-               if (strncmp("NDims",mystring,5)==0) {strcpy(mystring,"NDims = 2\n");}
+               if (strncmp("NDims",mystring,5)==0) {
+                               if (width==1) 
+                               { 
+                                       strcpy(mystring,"NDims = 2\n"); 
+                               } else {
+                                       strcpy(mystring,"NDims = 3\n"); 
+                               }
+                       }
                if (strncmp("DimSize",mystring,6)==0) 
                        {
                                sscanf(mystring,"%s %s %d %d %d",strTmp, strTmp, &sx, &sy,&sz);
-                               sprintf(mystring,"DimSize = %d %d\n",sx,sy);
-                       newHeaderSize = (long long int)sx*(long long int)sy*(long long int)bbGetInputSlice();
+
+                               if (width==1) 
+                               { 
+                                       sprintf(mystring,"DimSize = %d %d\n",sx,sy);
+                               } else {
+                                       sprintf(mystring,"DimSize = %d %d %d\n",sx,sy,width);
+                               }
+
+                       newHeaderSize = (long long int)sx*(long long int)sy*(long long int)slice;
                                if (bbGetInputSlice()>=sz) {ok=false;}
                        } // if
                if (strncmp("ElementType = MET_CHAR",mystring,22)==0)   { newHeaderSize=newHeaderSize*1; }
@@ -170,11 +193,22 @@ void ReadMHDPlane::ReadNormalMHD()
        } // if ok
 
        bbSetOutputOut( newImage );
-
 }
 
+
+
+
+
 void ReadMHDPlane::Read64lseek()
 {
+       int             slice;
+       int             width;
+
+       width = bbGetInputWidth();
+       if (width<=0 ) { width=1; }
+
+       slice = bbGetInputSlice();
+       if (slice<0 ) { slice=0; }
 
        int             dimX=-1,dimY=-1,dimZ=-1;
        int             dim=-1;
@@ -208,18 +242,18 @@ void ReadMHDPlane::Read64lseek()
                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 = 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 = 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);                }
+               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);
@@ -227,15 +261,15 @@ void ReadMHDPlane::Read64lseek()
                newImage->Initialize();
 
 
-               int fd;
-               long long ret;
+               int             fd;
+               long long       ret;
                std::size_t found;
                std::string filename;
-               found                                   =       bbGetInputFileName().find_last_of("/\\");
+               found                                   = bbGetInputFileName().find_last_of("/\\");
                filename                                = bbGetInputFileName().substr(0,found+1) + elementdatafile ;
-               long long pos;
+               long long       pos;
 
-               long long lsize                 = dimX*dimY*1 *dataSize;
+               long long lsize                 = dimX*dimY*width *dataSize;
 
 
 #if defined(_WIN32)
@@ -256,13 +290,13 @@ void ReadMHDPlane::Read64lseek()
                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->SetDimensions(  dimX,dimY,width );
+                       newImage->SetWholeExtent(0,  dimX-1,0,dimY-1,0,width-1 );
+                       newImage->SetExtent(0,  dimX-1,0,dimY-1,0,width-1 );
                        newImage->SetNumberOfScalarComponents(1);
                        newImage->AllocateScalars();
                        newImage->Update();
-                       pos = dimX*dimY*(long long)bbGetInputSlice()*dataSize;
+                       pos = dimX*dimY*(long long)slice*dataSize;
 #if defined(_WIN32)
                        if (_lseeki64( fd, pos, SEEK_SET ) < 0)
 #else
@@ -283,77 +317,36 @@ void ReadMHDPlane::Read64lseek()
 
                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->SetDimensions(  dimX,dimZ,width );
+                       newImage->SetWholeExtent(0,  dimX-1,0,dimZ-1,0,width-1 );
+                       newImage->SetExtent(0,  dimX-1,0,dimZ-1,0,width-1 );
                        newImage->SetNumberOfScalarComponents(1);
                        newImage->AllocateScalars();
                        newImage->Update();
-                       long long sizeBytesPlane = dimX*dimY*dataSize;
-                       pos = dimX*(long long int)bbGetInputSlice()*dataSize;
-                       char *pImage = (char*)(newImage->GetScalarPointer());
-                       for (j=0;j<dimZ;j++)
+                       int iWidth;
+                       for (iWidth=0;iWidth<width;iWidth++)
                        {
-#if defined(_WIN32)
-                               if (_lseeki64( fd, pos + j*sizeBytesPlane , SEEK_SET ) < 0)
-#else
-                               if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0) 
-#endif // defined(_WIN32)
-                               {
-                                       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
+                               copy_XZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
+                       }
                }  // if PLANE XZ
 
 
                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->SetDimensions(  dimY,dimZ,width );
+                       newImage->SetWholeExtent(0,  dimY-1,0,dimZ-1,0,width-1 );
+                       newImage->SetExtent(0,  dimY-1,0,dimZ-1,0,width-1 );
                        newImage->SetNumberOfScalarComponents(1);
                        newImage->AllocateScalars();
                        newImage->Update();
-                       long long sizeBytesPlane = dimX*dimY*dataSize;
-                       char *pImage;
-                       for (j=0;j<dimZ;j++)
+                       int iWidth;
+                       for (iWidth=0;iWidth<width;iWidth++)
                        {
-                               for (i=0;i<dimY;i++)
-                               {
-                                       pos = ((long long int)bbGetInputSlice() + i*dimX + j*dimX*dimY)*dataSize ;
-#if defined(_WIN32)
-                                       if (_lseeki64( fd, pos , SEEK_SET ) < 0)
-#else
-                                       if (lseek64(fd, pos , SEEK_SET) < 0) 
-#endif // defined(_WIN32)
-                                       {
-                                               printf("EED ReadMHDPlane::Read64lseek \n");
-                                               fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
-                                               exit(1);
-                                       }
-
-                                       pImage=(char*)(newImage->GetScalarPointer(i, j,0 ));
-                                       if ((ret = read(fd, pImage , dataSize)) < 0) 
-                                       {
-                                               fprintf(stderr, "Failed reading: %s\n", strerror(errno));
-                                               exit(1);
-                                       }
-                               }
-                       } // for j
+                               copy_YZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
+                       }
+
                }  // if PLANE YZ       
 #if defined(_WIN32)
                _close (fd);
@@ -365,10 +358,76 @@ void ReadMHDPlane::Read64lseek()
        } // if  ffIn
 
        bbSetOutputOut( newImage );
+}
+
 
+void ReadMHDPlane::copy_YZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
+{
+       long long int j;
+       long long int i;
+       long long       ret;
+       long long       pos;
+       long long sizeBytesPlane = dimX*dimY*dataSize;
+       char *pImage;
+       for (j=0;j<dimZ;j++)
+       {
+               for (i=0;i<dimY;i++)
+               {
+                       pos = ((long long int)slice+iWidth + i*dimX + j*dimX*dimY)*dataSize ;
+#if defined(_WIN32)
+                       if (_lseeki64( fd, pos , SEEK_SET ) < 0)
+#else
+                       if (lseek64(fd, pos , SEEK_SET) < 0) 
+#endif // defined(_WIN32)
+                       {
+                               printf("EED ReadMHDPlane::Read64lseek \n");
+                               fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
+                               exit(1);
+                       }
+
+                       pImage=(char*)(newImage->GetScalarPointer(i, j,iWidth ));
+                       if ((ret = read(fd, pImage , dataSize)) < 0) 
+                       {
+                               fprintf(stderr, "Failed reading: %s\n", strerror(errno));
+                               exit(1);
+                       }
+                       } // for i
+               } // for j
 }
 
 
+void ReadMHDPlane::copy_XZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
+{
+       long long int j;
+       long long       ret;
+       long long       pos;
+       pos = dimX*(long long int)slice*dataSize;
+       long long sizeBytesPlane = dimX*dimY*dataSize;
+       char *pImage = (char*)( newImage->GetScalarPointer(0,0,iWidth) );
+       for (j=0;j<dimZ;j++)
+       {
+#if defined(_WIN32)
+               if (_lseeki64( fd, pos + j*sizeBytesPlane , SEEK_SET ) < 0)
+#else
+               if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0) 
+#endif // defined(_WIN32)
+               {
+                       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
+}
+
+
+
 void ReadMHDPlane::Process()
 {
        if (bbGetInputType()==0)
@@ -393,6 +452,7 @@ void ReadMHDPlane::bbUserSetDefaultValues()
    bbSetInputFileName("");
    bbSetInputSlice(0);
    bbSetInputType(1);
+   bbSetInputWidth(1);
    bbSetInputDirectionPlane("XY");
   
 }
index 33a96eb9540be0bbab6dde018b8b79b42ebc43cd..5b94b9554b7586236ab678af29d5c49b70c81815 100644 (file)
@@ -29,6 +29,7 @@ class bbcreaVtk_EXPORT ReadMHDPlane
 //===== 
   BBTK_DECLARE_INPUT(FileName,std::string);
   BBTK_DECLARE_INPUT(Slice,int);
+  BBTK_DECLARE_INPUT(Width,int);
   BBTK_DECLARE_INPUT(DirectionPlane,std::string);
   BBTK_DECLARE_INPUT(Type,int);
   BBTK_DECLARE_OUTPUT(Out,vtkImageData*); 
@@ -37,6 +38,9 @@ class bbcreaVtk_EXPORT ReadMHDPlane
   vtkImageData* CreateDefaultImage();
   void ReadNormalMHD();
   void Read64lseek();
+  void copy_XZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize);
+  void copy_YZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize);
+
 
   void Process();
 //===== 
@@ -51,8 +55,9 @@ BBTK_BEGIN_DESCRIBE_BLACK_BOX(ReadMHDPlane,bbtk::AtomicBlackBox);
  BBTK_CATEGORY("empty");
  BBTK_INPUT(ReadMHDPlane,FileName,"Image file name",std::string,"");
  BBTK_INPUT(ReadMHDPlane,Slice,"Slice number",int,"");
+ BBTK_INPUT(ReadMHDPlane,Width,"(1 default) Number of slices to be read",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_INPUT(ReadMHDPlane,Type,"(1 default) 0 = Normal mhd,  1 = lseek64",int,"");
  BBTK_OUTPUT(ReadMHDPlane,Out,"Image 2D",vtkImageData*,"");
 BBTK_END_DESCRIBE_BLACK_BOX(ReadMHDPlane);
 //=====