X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=bbtk_creaVtk_PKG%2Fsrc%2FbbcreaVtkReadMHDPlane.cxx;h=020eb5f9080d0aecff4b85e807382e57172c7029;hb=ef9f90810fcc3800acac54a104f14ff505135fd3;hp=b842e8dca42f643bc2cdfdd9d4deae1fc212e86d;hpb=c63a1475156e6df7ba66ccd385f0924e7c3c032d;p=creaVtk.git diff --git a/bbtk_creaVtk_PKG/src/bbcreaVtkReadMHDPlane.cxx b/bbtk_creaVtk_PKG/src/bbcreaVtkReadMHDPlane.cxx index b842e8d..020eb5f 100644 --- a/bbtk_creaVtk_PKG/src/bbcreaVtkReadMHDPlane.cxx +++ b/bbtk_creaVtk_PKG/src/bbcreaVtkReadMHDPlane.cxx @@ -32,6 +32,41 @@ 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) //===== + +vtkImageData* ReadMHDPlane::CreateDefaultImage() +{ + int i; + int sizeX, sizeY, sizeZ; + sizeX = 200; + sizeY = sizeX; + sizeZ = 1; + vtkImageData *newImage = vtkImageData::New(); + newImage->Initialize(); + newImage->SetSpacing( 1,1,1 ); + newImage->SetDimensions( sizeX,sizeY,sizeZ ); + newImage->SetExtent(0, sizeX-1,0,sizeY-1,0,sizeZ-1 ); + +//EED 2017-01-01 Migration VTK7 +#if VTK_MAJOR_VERSION <= 5 + newImage->SetWholeExtent(0, sizeX-1,0,sizeY-1,0,sizeZ-1 ); + newImage->SetScalarTypeToUnsignedChar(); + newImage->SetNumberOfScalarComponents(1); + newImage->AllocateScalars(); + newImage->Update(); +#else + newImage->AllocateScalars(VTK_UNSIGNED_CHAR,1); +#endif + + memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 ); + for (i=0; iSetScalarComponentFromDouble(i,i,0, 0, 255 ); + newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 ); + } // for i + return newImage; +} + + void ReadMHDPlane::ReadNormalMHD() { // THE MAIN PROCESSING METHOD BODY @@ -50,6 +85,10 @@ void ReadMHDPlane::ReadNormalMHD() // std::cout << "Output value = " <=sz) {ok=false;} } // if - if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { newHeaderSize=newHeaderSize*1; } - if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { newHeaderSize=newHeaderSize*1; } - if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; } - if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { newHeaderSize=newHeaderSize*2; } - if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { newHeaderSize=newHeaderSize*4; } - if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { newHeaderSize=newHeaderSize*4; } + if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { dataSize=sizeof(char); } + if (strncmp("ElementType = VTK_CHAR",mystring,22)==0) { dataSize=sizeof(char); } + if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { dataSize=sizeof(unsigned char); } + if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0) { dataSize=sizeof(unsigned char); } + if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { dataSize=sizeof(unsigned short); } + if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { dataSize=sizeof(unsigned short); } + if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { dataSize=sizeof(short); } + if (strncmp("ElementType = VTK_SHORT",mystring,23)==0) { dataSize=sizeof(short); } + if (strncmp("ElementType = MET_UINT",mystring,22)==0) { dataSize=sizeof(unsigned int); } + if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,30)==0) { dataSize=sizeof(unsigned int); } + if (strncmp("ElementType = MET_INT",mystring,21)==0) { dataSize=sizeof(int); } + if (strncmp("ElementType = VTK_INT",mystring,21)==0) { dataSize=sizeof(int); } + if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { dataSize=sizeof(float); } + if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { dataSize=sizeof(float); } + if (strncmp("ElementType = MET_LONG",mystring,22)==0) { dataSize=sizeof(long); } + if (strncmp("ElementType = VTK_LONG",mystring,22)==0) { dataSize=sizeof(long); } + if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0) { dataSize=sizeof(double); } + if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0) { dataSize=sizeof(double); } + newHeaderSize=newHeaderSize*dataSize; + if (strncmp("Offset",mystring,6)==0) {strcpy(mystring,"Offset = 0 0 0\n");} if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");} if (strncmp("ElementDataFile",mystring,15)==0) @@ -138,35 +209,26 @@ void ReadMHDPlane::ReadNormalMHD() if (ok==false) { - int i; - int sizeX, sizeY, sizeZ; - sizeX = 200; - sizeY = sizeX; - sizeZ = 1; - newImage = vtkImageData::New(); - newImage->Initialize(); - newImage->SetScalarTypeToUnsignedChar(); - newImage->SetSpacing( 1,1,1 ); - newImage->SetDimensions( sizeX,sizeY,sizeZ ); - newImage->SetWholeExtent(0, sizeX-1,0,sizeY-1,0,sizeZ-1 ); - newImage->SetExtent(0, sizeX-1,0,sizeY-1,0,sizeZ-1 ); - newImage->SetNumberOfScalarComponents(1); - newImage->AllocateScalars(); - newImage->Update(); - memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 ); - for (i=0; iSetScalarComponentFromDouble(i,i,0, 0, 255 ); - newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 ); - } // for i + newImage=CreateDefaultImage(); } // 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; @@ -176,7 +238,8 @@ void ReadMHDPlane::Read64lseek() float ox=-1,oy=-1,oz=-1; long int headersize=0; - vtkImageData *newImage; + vtkImageData *newImage=NULL; + int imageType; char mystring[250]; char strTmp[30]; @@ -184,8 +247,6 @@ void ReadMHDPlane::Read64lseek() FILE *ffIn = fopen(bbGetInputFileName().c_str(),"r"); long long dataSize; - - if (ffIn!=NULL) { newImage = vtkImageData::New(); @@ -197,23 +258,32 @@ void ReadMHDPlane::Read64lseek() 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("ElementSpacing",mystring,14)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); } + if (strncmp("ElementSize",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); } + + + if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { imageType =VTK_CHAR; dataSize=sizeof(char); } + if (strncmp("ElementType = VTK_CHAR",mystring,22)==0) { imageType =VTK_CHAR; dataSize=sizeof(char); } + if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { imageType =VTK_UNSIGNED_CHAR; dataSize=sizeof(unsigned char); } + if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0) { imageType =VTK_UNSIGNED_CHAR; dataSize=sizeof(unsigned char); } + if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { imageType =VTK_UNSIGNED_SHORT; dataSize=sizeof(unsigned short);} + if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { imageType =VTK_UNSIGNED_SHORT; dataSize=sizeof(unsigned short);} + if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { imageType =VTK_SHORT; dataSize=sizeof(short); } + if (strncmp("ElementType = VTK_SHORT",mystring,23)==0) { imageType =VTK_SHORT; dataSize=sizeof(short); } + if (strncmp("ElementType = MET_UINT",mystring,22)==0) { imageType =VTK_UNSIGNED_INT; dataSize=sizeof(unsigned int); } + if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,30)==0) { imageType =VTK_UNSIGNED_INT; dataSize=sizeof(unsigned int); } + if (strncmp("ElementType = MET_INT",mystring,21)==0) { imageType =VTK_INT; dataSize=sizeof(int); } + if (strncmp("ElementType = VTK_INT",mystring,21)==0) { imageType =VTK_INT; dataSize=sizeof(int); } + if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { imageType =VTK_FLOAT; dataSize=sizeof(float); } + if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { imageType =VTK_FLOAT; dataSize=sizeof(float); } + if (strncmp("ElementType = MET_LONG",mystring,22)==0) { imageType =VTK_LONG; dataSize=sizeof(long); } + if (strncmp("ElementType = VTK_LONG",mystring,22)==0) { imageType =VTK_LONG; dataSize=sizeof(long); } + if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0) { imageType =VTK_DOUBLE; dataSize=sizeof(double); } + if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0) { imageType =VTK_DOUBLE; dataSize=sizeof(double); } } // while fclose(ffIn); @@ -221,15 +291,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) @@ -239,24 +309,34 @@ void ReadMHDPlane::Read64lseek() #endif // defined(_WIN32) - if (fd < 0) { - printf("EED ReadMHDPlane::Read64lseek \n"); + printf("EED ReadMHDPlane::Read64lseek WARNNING! raw file not exist\n"); fprintf(stderr, "%s\n", strerror(errno)); - exit(1); - } + newImage=CreateDefaultImage(); +// exit(1); + } + - if (bbGetInputDirectionPlane()=="XY") + if ((bbGetInputDirectionPlane()=="XY") && (fd>=0)) { 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->SetExtent(0, dimX-1,0,dimY-1,0,width-1 ); + +//EED 2017-01-01 Migration VTK7 +#if VTK_MAJOR_VERSION <= 5 + newImage->SetWholeExtent(0, dimX-1,0,dimY-1,0,width-1 ); newImage->SetNumberOfScalarComponents(1); + newImage->SetScalarType(imageType); newImage->AllocateScalars(); newImage->Update(); - pos = dimX*dimY*(long long)bbGetInputSlice()*dataSize; +#else + newImage->AllocateScalars( imageType,1 ); +#endif + + + pos = dimX*dimY*(long long)slice*dataSize; #if defined(_WIN32) if (_lseeki64( fd, pos, SEEK_SET ) < 0) #else @@ -275,95 +355,132 @@ void ReadMHDPlane::Read64lseek() } } // if PLANE XY - if (bbGetInputDirectionPlane()=="XZ") + if ((bbGetInputDirectionPlane()=="XZ") && (fd>=0)) { - 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->SetExtent(0, dimX-1,0,dimZ-1,0,width-1 ); +//EED 2017-01-01 Migration VTK7 +#if VTK_MAJOR_VERSION <= 5 + newImage->SetWholeExtent(0, dimX-1,0,dimZ-1,0,width-1 ); newImage->SetNumberOfScalarComponents(1); + newImage->SetScalarType(imageType); 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;jAllocateScalars( imageType, 1); +#endif - if ((ret = read(fd, pImage , dimX*dataSize)) < 0) - { - fprintf(stderr, "Failed reading: %s\n", strerror(errno)); - exit(1); - } - pImage = pImage+dimX*dataSize; - } // for j + int iWidth; + for (iWidth=0;iWidth=0)) { - 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->SetExtent(0, dimY-1,0,dimZ-1,0,width-1 ); +//EED 2017-01-01 Migration VTK7 +#if VTK_MAJOR_VERSION <= 5 + newImage->SetWholeExtent(0, dimY-1,0,dimZ-1,0,width-1 ); newImage->SetNumberOfScalarComponents(1); + newImage->SetScalarType(imageType); newImage->AllocateScalars(); newImage->Update(); - long long sizeBytesPlane = dimX*dimY*dataSize; - char *pImage; - for (j=0;jAllocateScalars( imageType ,1 ); +#endif + + int iWidth; + for (iWidth=0;iWidthGetScalarPointer(i, j,0 )); - if ((ret = read(fd, pImage , dataSize)) < 0) - { - fprintf(stderr, "Failed reading: %s\n", strerror(errno)); - exit(1); - } - } - } // for j - } // if PLANE YZ + } else { + newImage=CreateDefaultImage(); + } // 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;jGetScalarPointer(i, dimZ-1-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=dimZ-1;j>=0;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) @@ -388,6 +505,7 @@ void ReadMHDPlane::bbUserSetDefaultValues() bbSetInputFileName(""); bbSetInputSlice(0); bbSetInputType(1); + bbSetInputWidth(1); bbSetInputDirectionPlane("XY"); }