//===== // 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) //===== #include "bbcreaVtkReadMHDPlane.h" #include "bbcreaVtkPackage.h" #include "stdio.h" #define _LARGEFILE64_SOURCE #include #include #if defined(_WIN32) #else #include #endif // defined(_WIN32) #include #include #include #include #include namespace bbcreaVtk { BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,ReadMHDPlane) 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->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 return newImage; } /* EED Borrame 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 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM : // void bbSet{Input|Output}NAME(const TYPE&) // const TYPE& bbGet{Input|Output}NAME() const // Where : // * NAME is the name of the input/output // (the one provided in the attribute 'name' of the tag 'input') // * TYPE is the C++ type of the input/output // (the one provided in the attribute 'type' of the tag 'input') // bbSetOutputOut( bbGetInputIn() ); // std::cout << "Output value = " <=sz) {ok=false;} } // if 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) { fprintf(ffOut,"HeaderSize = %lld\n\n", newHeaderSize ); } // if fprintf(ffOut,mystring); } // while fclose(ffIn); fclose(ffOut); if (ok==true) { vtkMetaImageReader * reader = vtkMetaImageReader::New(); reader->SetFileName( newFileName.c_str() ); reader->Update(); newImage = reader->GetOutput(); } // if ok } else { ok=false; printf("EED ERROR: Problem openin:%s\n", inputfilename.c_str() ); }// if ffIn if (ok==false) { newImage=CreateDefaultImage(); } // if ok bbSetOutputOut( newImage ); bbSetOutputOut2( ChangeOrientation(newImage) ); } */ vtkImageData* ReadMHDPlane::ChangeOrientation(vtkImageData* img) { int ext[6]; int sizeX, sizeY,sizeZ; img->GetWholeExtent(ext); int sizeLine; if (bbGetInputDirectionPlane()=="XY") { sizeX = ext[1]-ext[0]+1; sizeY = ext[3]-ext[2]+1; sizeZ = 1; } // XY if (bbGetInputDirectionPlane()=="YZ") { sizeX = 1; sizeY = ext[1]-ext[0]+1; sizeZ = ext[3]-ext[2]+1; sizeLine = sizeY; } // YZ if (bbGetInputDirectionPlane()=="ZX") { sizeX = ext[1]-ext[0]+1; sizeY = 1; sizeZ = ext[3]-ext[2]+1; sizeLine = sizeX; } // ZX vtkImageData *newImageOrinted = vtkImageData::New(); newImageOrinted->Initialize(); newImageOrinted->SetScalarType( img->GetScalarType() ); newImageOrinted->SetSpacing( img->GetSpacing() ); newImageOrinted->SetDimensions( sizeX,sizeY,sizeZ ); newImageOrinted->SetWholeExtent(0,sizeX-1,0,sizeY-1,0,sizeZ-1 ); newImageOrinted->SetExtent(0,sizeX-1,0,sizeY-1,0,sizeZ-1 ); newImageOrinted->SetNumberOfScalarComponents(1); newImageOrinted->AllocateScalars(); newImageOrinted->Update(); if (bbGetInputDirectionPlane()=="XY") { memcpy ( newImageOrinted->GetScalarPointer(), img->GetScalarPointer(), sizeX*sizeY*sizeZ*img->GetScalarSize() ); } else { int yy; int dimY=ext[3]-ext[2]+1; int sizeByte=sizeLine*img->GetScalarSize(); char *ptrDst=(char*)(newImageOrinted->GetScalarPointer()) + sizeByte*dimY; char *ptrOrg=(char*)(img->GetScalarPointer()); for (yy=0 ; yySetScalarTypeToChar(); 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_UINT",mystring,22)==0) { newImage->SetScalarTypeToUnsignedInt(); dataSize=sizeof(unsigned int); } if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,30)==0) { newImage->SetScalarTypeToUnsignedInt(); dataSize=sizeof(unsigned int); } if (strncmp("ElementType = MET_INT",mystring,21)==0) { newImage->SetScalarTypeToInt(); dataSize=sizeof(int); } if (strncmp("ElementType = VTK_INT",mystring,21)==0) { newImage->SetScalarTypeToInt(); dataSize=sizeof(int); } 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_LONG",mystring,22)==0) { newImage->SetScalarTypeToLong(); dataSize=sizeof(long); } if (strncmp("ElementType = VTK_LONG",mystring,22)==0) { newImage->SetScalarTypeToLong(); dataSize=sizeof(long); } 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*width *dataSize; #if defined(_WIN32) _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 ); #else fd = open ( filename.c_str() , O_RDONLY|O_LARGEFILE ); #endif // defined(_WIN32) if (fd < 0) { printf("EED ReadMHDPlane::Read64lseek WARNNING! raw file not exist\n"); fprintf(stderr, "%s\n", strerror(errno)); newImage=CreateDefaultImage(); // exit(1); } if ((bbGetInputDirectionPlane()=="XY") && (fd>=0)) { newImage->SetSpacing( spcX,spcY,spcZ ); 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)slice*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); } if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0) { fprintf(stderr, "Failed reading: %s\n", strerror(errno)); exit(1); } } // if PLANE XY if ((bbGetInputDirectionPlane()=="ZX") && (fd>=0)) { newImage->SetSpacing( spcX,spcZ,spcY ); 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(); int iWidth; for (iWidth=0;iWidth=0)) { newImage->SetSpacing( spcY,spcZ,spcX ); 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(); int iWidth; for (iWidth=0;iWidthSetScalarTypeToChar(); 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_UINT",mystring,22)==0) { newImage->SetScalarTypeToUnsignedInt(); dataSize=sizeof(unsigned int); } if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,30)==0) { newImage->SetScalarTypeToUnsignedInt(); dataSize=sizeof(unsigned int); } if (strncmp("ElementType = MET_INT",mystring,21)==0) { newImage->SetScalarTypeToInt(); dataSize=sizeof(int); } if (strncmp("ElementType = VTK_INT",mystring,21)==0) { newImage->SetScalarTypeToInt(); dataSize=sizeof(int); } 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_LONG",mystring,22)==0) { newImage->SetScalarTypeToLong(); dataSize=sizeof(long); } if (strncmp("ElementType = VTK_LONG",mystring,22)==0) { newImage->SetScalarTypeToLong(); dataSize=sizeof(long); } 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 = fileNameIn.find_last_of("/\\"); filename = fileNameIn.substr(0,found+1) + elementdatafile ; long long pos; long long lsize = dimX*dimY*width *dataSize; #if defined(_WIN32) _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 ); #else fd = open ( filename.c_str() , O_RDONLY|O_LARGEFILE ); #endif // defined(_WIN32) if (fd < 0) { printf("EED ReadMHDPlane::Read64lseek WARNNING! raw file not exist\n"); fprintf(stderr, "%s\n", strerror(errno)); newImage=CreateDefaultImage(); // exit(1); } if ((plane=="XY") && (fd>=0)) { newImage->SetSpacing( spcX,spcY,spcZ ); 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)slice*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); } if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0) { fprintf(stderr, "Failed reading: %s\n", strerror(errno)); exit(1); } } // if PLANE XY if ((plane=="ZX") && (fd>=0)) { newImage->SetSpacing( spcX,spcZ,spcY ); 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(); int iWidth; for (iWidth=0;iWidth=0)) { newImage->SetSpacing( spcY,spcZ,spcX ); 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(); int iWidth; for (iWidth=0;iWidthGetScalarPointer(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_ZX_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) { //-- Split FileName std::string inputfilename; std::size_t found = bbGetInputFileName().find_last_of("/\\"); std::string path = bbGetInputFileName().substr(0,found+1); std::string filename = bbGetInputFileName().substr(found+1); #ifdef _WIN32 path=path+"YZ_ZX\\"; #else path=path+"YZ_ZX/"; #endif if (bbGetInputDirectionPlane()=="XY") { inputfilename = bbGetInputFileName(); } // if XY if (bbGetInputDirectionPlane()=="YZ") { inputfilename = path+filename+"_YZ.mhd"; } // if YZ if (bbGetInputDirectionPlane()=="ZX") { inputfilename = path+filename+"_ZX.mhd"; } // if XZ Read64lseek( inputfilename ,"XY"); } if (bbGetInputType()==1) { Read64lseek( bbGetInputFileName(), bbGetInputDirectionPlane() ); } } //===== // 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::bbUserSetDefaultValues() { // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX // Here we initialize the input 'In' to 0 bbSetInputFileName(""); bbSetInputSlice(0); bbSetInputType(1); bbSetInputWidth(1); bbSetInputDirectionPlane("XY"); } //===== // 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::bbUserInitializeProcessing() { // THE INITIALIZATION METHOD BODY : // Here does nothing // but this is where you should allocate the internal/output pointers // if any } //===== // 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::bbUserFinalizeProcessing() { // THE FINALIZATION METHOD BODY : // Here does nothing // but this is where you should desallocate the internal/output pointers // if any } } // EO namespace bbcreaVtk