2 // 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)
4 #include "bbcreaVtkReadMHDPlane.h"
5 #include "bbcreaVtkPackage.h"
10 #define _LARGEFILE64_SOURCE
13 #include <sys/types.h>
18 #endif // defined(_WIN32)
29 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,ReadMHDPlane)
30 BBTK_BLACK_BOX_IMPLEMENTATION(ReadMHDPlane,bbtk::AtomicBlackBox);
32 // 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)
36 vtkImageData* ReadMHDPlane::CreateDefaultImage()
39 int sizeX, sizeY, sizeZ;
43 vtkImageData *newImage = vtkImageData::New();
44 newImage->Initialize();
45 newImage->SetSpacing( 1,1,1 );
46 newImage->SetDimensions( sizeX,sizeY,sizeZ );
47 newImage->SetExtent(0, sizeX-1,0,sizeY-1,0,sizeZ-1 );
49 //EED 2017-01-01 Migration VTK7
50 #if VTK_MAJOR_VERSION <= 5
51 newImage->SetWholeExtent(0, sizeX-1,0,sizeY-1,0,sizeZ-1 );
52 newImage->SetScalarTypeToUnsignedChar();
53 newImage->SetNumberOfScalarComponents(1);
54 newImage->AllocateScalars();
57 newImage->AllocateScalars(VTK_UNSIGNED_CHAR,1);
60 memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
61 for (i=0; i<sizeX; i++)
63 newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
64 newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
70 void ReadMHDPlane::ReadNormalMHD()
72 // THE MAIN PROCESSING METHOD BODY
73 // Here we simply set the input 'In' value to the output 'Out'
74 // And print out the output value
75 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
76 // void bbSet{Input|Output}NAME(const TYPE&)
77 // const TYPE& bbGet{Input|Output}NAME() const
79 // * NAME is the name of the input/output
80 // (the one provided in the attribute 'name' of the tag 'input')
81 // * TYPE is the C++ type of the input/output
82 // (the one provided in the attribute 'type' of the tag 'input')
84 // bbSetOutputOut( bbGetInputIn() );
85 // std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
87 std::string inputfilename;
93 if (bbGetInputDirectionPlane()=="XY")
95 inputfilename=bbGetInputFileName();
99 if ((bbGetInputDirectionPlane()=="YZ") || (bbGetInputDirectionPlane()=="ZX"))
102 std::size_t found = bbGetInputFileName().find_last_of("/\\");
103 std::string path = bbGetInputFileName().substr(0,found+1);
104 std::string filename = bbGetInputFileName().substr(found+1);
110 if (bbGetInputDirectionPlane()=="YZ")
112 inputfilename = path+filename+"_YZ.mhd";
115 if (bbGetInputDirectionPlane()=="ZX")
117 inputfilename = path+filename+"_ZX.mhd";
121 width = bbGetInputWidth();
122 if (width<=0 ) { width=1; }
124 slice = bbGetInputSlice();
125 if (slice<0 ) { slice=0; }
127 vtkImageData *newImage;
128 long long newHeaderSize;
129 std::string newFileName=inputfilename+"-OneSlice";
135 FILE *ffIn = fopen(inputfilename.c_str(),"r");
138 FILE *ffOut = fopen(newFileName.c_str(),"w");
142 strcpy(mystring,"\n");
143 fgets(mystring,250,ffIn);
144 if (strncmp("NDims",mystring,5)==0)
148 strcpy(mystring,"NDims = 2\n");
150 strcpy(mystring,"NDims = 3\n");
153 if (strncmp("DimSize",mystring,6)==0)
155 sscanf(mystring,"%s %s %d %d %d",strTmp, strTmp, &sx, &sy,&sz);
159 sprintf(mystring,"DimSize = %d %d\n",sx,sy);
161 sprintf(mystring,"DimSize = %d %d %d\n",sx,sy,width);
164 newHeaderSize = (long long int)sx*(long long int)sy*(long long int)slice;
165 if (bbGetInputSlice()>=sz) {ok=false;}
167 if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { dataSize=sizeof(char); }
168 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0) { dataSize=sizeof(char); }
169 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { dataSize=sizeof(unsigned char); }
170 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0) { dataSize=sizeof(unsigned char); }
171 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { dataSize=sizeof(unsigned short); }
172 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { dataSize=sizeof(unsigned short); }
173 if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { dataSize=sizeof(short); }
174 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0) { dataSize=sizeof(short); }
175 if (strncmp("ElementType = MET_UINT",mystring,22)==0) { dataSize=sizeof(unsigned int); }
176 if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,30)==0) { dataSize=sizeof(unsigned int); }
177 if (strncmp("ElementType = MET_INT",mystring,21)==0) { dataSize=sizeof(int); }
178 if (strncmp("ElementType = VTK_INT",mystring,21)==0) { dataSize=sizeof(int); }
179 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { dataSize=sizeof(float); }
180 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { dataSize=sizeof(float); }
181 if (strncmp("ElementType = MET_LONG",mystring,22)==0) { dataSize=sizeof(long); }
182 if (strncmp("ElementType = VTK_LONG",mystring,22)==0) { dataSize=sizeof(long); }
183 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0) { dataSize=sizeof(double); }
184 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0) { dataSize=sizeof(double); }
185 newHeaderSize=newHeaderSize*dataSize;
187 if (strncmp("Offset",mystring,6)==0) {strcpy(mystring,"Offset = 0 0 0\n");}
188 if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");}
189 if (strncmp("ElementDataFile",mystring,15)==0)
191 fprintf(ffOut,"HeaderSize = %lld\n\n", newHeaderSize );
193 fprintf(ffOut,mystring);
200 vtkMetaImageReader * reader = vtkMetaImageReader::New();
201 reader->SetFileName( newFileName.c_str() );
203 newImage = reader->GetOutput();
207 printf("EED ERROR: Problem openin:%s\n", inputfilename.c_str() );
212 newImage=CreateDefaultImage();
215 bbSetOutputOut( newImage );
222 void ReadMHDPlane::Read64lseek()
227 width = bbGetInputWidth();
228 if (width<=0 ) { width=1; }
230 slice = bbGetInputSlice();
231 if (slice<0 ) { slice=0; }
233 int dimX=-1,dimY=-1,dimZ=-1;
235 std::string formattype("VOID");
236 std::string elementdatafile("VOID");
237 float spcX=-1,spcY=-1,spcZ=-1;
238 float ox=-1,oy=-1,oz=-1;
239 long int headersize=0;
241 vtkImageData *newImage=NULL;
247 FILE *ffIn = fopen(bbGetInputFileName().c_str(),"r");
252 newImage = vtkImageData::New();
256 strcpy(mystring,"\n");
257 fgets(mystring,250,ffIn);
258 if (strncmp("NDims",mystring,5)==0) { sscanf(mystring,"%s %s %d" ,strTmp, strTmp, &dim); }
259 if (strncmp("DimSize",mystring,6)==0) { sscanf(mystring,"%s %s %d %d %d" ,strTmp, strTmp, &dimX, &dimY,&dimZ); }
260 if (strncmp("ElementType",mystring,11)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); formattype=strTmp2; }
261 if (strncmp("ElementSpacing",mystring,14)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); }
262 if (strncmp("ElementSize",mystring,11)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); }
263 if (strncmp("Offset",mystring,6)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &ox, &oy, &oz); }
264 if (strncmp("HeaderSize",mystring,10)==0) { sscanf(mystring,"%s %s %ld" ,strTmp, strTmp, &headersize); }
265 if (strncmp("ElementDataFile",mystring,15)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2; }
269 if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { imageType =VTK_CHAR; dataSize=sizeof(char); }
270 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0) { imageType =VTK_CHAR; dataSize=sizeof(char); }
271 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { imageType =VTK_UNSIGNED_CHAR; dataSize=sizeof(unsigned char); }
272 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0) { imageType =VTK_UNSIGNED_CHAR; dataSize=sizeof(unsigned char); }
273 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { imageType =VTK_UNSIGNED_SHORT; dataSize=sizeof(unsigned short);}
274 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { imageType =VTK_UNSIGNED_SHORT; dataSize=sizeof(unsigned short);}
275 if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { imageType =VTK_SHORT; dataSize=sizeof(short); }
276 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0) { imageType =VTK_SHORT; dataSize=sizeof(short); }
277 if (strncmp("ElementType = MET_UINT",mystring,22)==0) { imageType =VTK_UNSIGNED_INT; dataSize=sizeof(unsigned int); }
278 if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,30)==0) { imageType =VTK_UNSIGNED_INT; dataSize=sizeof(unsigned int); }
279 if (strncmp("ElementType = MET_INT",mystring,21)==0) { imageType =VTK_INT; dataSize=sizeof(int); }
280 if (strncmp("ElementType = VTK_INT",mystring,21)==0) { imageType =VTK_INT; dataSize=sizeof(int); }
281 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { imageType =VTK_FLOAT; dataSize=sizeof(float); }
282 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { imageType =VTK_FLOAT; dataSize=sizeof(float); }
283 if (strncmp("ElementType = MET_LONG",mystring,22)==0) { imageType =VTK_LONG; dataSize=sizeof(long); }
284 if (strncmp("ElementType = VTK_LONG",mystring,22)==0) { imageType =VTK_LONG; dataSize=sizeof(long); }
285 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0) { imageType =VTK_DOUBLE; dataSize=sizeof(double); }
286 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0) { imageType =VTK_DOUBLE; dataSize=sizeof(double); }
291 newImage->Initialize();
297 std::string filename;
298 found = bbGetInputFileName().find_last_of("/\\");
299 filename = bbGetInputFileName().substr(0,found+1) + elementdatafile ;
302 long long lsize = dimX*dimY*width *dataSize;
306 _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 );
308 fd = open ( filename.c_str() , O_RDONLY|O_LARGEFILE );
309 #endif // defined(_WIN32)
314 printf("EED ReadMHDPlane::Read64lseek WARNNING! raw file not exist\n");
315 fprintf(stderr, "%s\n", strerror(errno));
316 newImage=CreateDefaultImage();
321 if ((bbGetInputDirectionPlane()=="XY") && (fd>=0))
323 newImage->SetSpacing( spcX,spcY,spcZ );
324 newImage->SetDimensions( dimX,dimY,width );
325 newImage->SetExtent(0, dimX-1,0,dimY-1,0,width-1 );
327 //EED 2017-01-01 Migration VTK7
328 #if VTK_MAJOR_VERSION <= 5
329 newImage->SetWholeExtent(0, dimX-1,0,dimY-1,0,width-1 );
330 newImage->SetNumberOfScalarComponents(1);
331 newImage->SetScalarType(imageType);
332 newImage->AllocateScalars();
335 newImage->AllocateScalars( imageType,1 );
339 pos = dimX*dimY*(long long)slice*dataSize;
341 if (_lseeki64( fd, pos, SEEK_SET ) < 0)
343 if (lseek64(fd, pos, SEEK_SET) < 0)
344 #endif // defined(_WIN32)
346 printf("EED ReadMHDPlane::Read64lseek \n");
347 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
351 if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0)
353 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
358 if ((bbGetInputDirectionPlane()=="XZ") && (fd>=0))
360 newImage->SetSpacing( spcX,spcZ,spcY );
361 newImage->SetDimensions( dimX,dimZ,width );
362 newImage->SetExtent(0, dimX-1,0,dimZ-1,0,width-1 );
363 //EED 2017-01-01 Migration VTK7
364 #if VTK_MAJOR_VERSION <= 5
365 newImage->SetWholeExtent(0, dimX-1,0,dimZ-1,0,width-1 );
366 newImage->SetNumberOfScalarComponents(1);
367 newImage->SetScalarType(imageType);
368 newImage->AllocateScalars();
371 newImage->AllocateScalars( imageType, 1);
375 for (iWidth=0;iWidth<width;iWidth++)
377 copy_XZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
381 if ((bbGetInputDirectionPlane()=="YZ") && (fd>=0))
383 newImage->SetSpacing( spcY,spcZ,spcX );
384 newImage->SetDimensions( dimY,dimZ,width );
385 newImage->SetExtent(0, dimY-1,0,dimZ-1,0,width-1 );
386 //EED 2017-01-01 Migration VTK7
387 #if VTK_MAJOR_VERSION <= 5
388 newImage->SetWholeExtent(0, dimY-1,0,dimZ-1,0,width-1 );
389 newImage->SetNumberOfScalarComponents(1);
390 newImage->SetScalarType(imageType);
391 newImage->AllocateScalars();
394 newImage->AllocateScalars( imageType ,1 );
398 for (iWidth=0;iWidth<width;iWidth++)
400 copy_YZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
408 #endif // defined(_WIN32)
410 newImage=CreateDefaultImage();
413 bbSetOutputOut( newImage );
417 void ReadMHDPlane::copy_YZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
423 long long sizeBytesPlane = dimX*dimY*dataSize;
429 pos = ((long long int)slice+iWidth + i*dimX + j*dimX*dimY)*dataSize ;
431 if (_lseeki64( fd, pos , SEEK_SET ) < 0)
433 if (lseek64(fd, pos , SEEK_SET) < 0)
434 #endif // defined(_WIN32)
436 printf("EED ReadMHDPlane::Read64lseek \n");
437 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
441 pImage=(char*)(newImage->GetScalarPointer(i, dimZ-1-j,iWidth ));
442 if ((ret = read(fd, pImage , dataSize)) < 0)
444 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
452 void ReadMHDPlane::copy_XZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
457 pos = dimX*(long long int)slice*dataSize;
458 long long sizeBytesPlane = dimX*dimY*dataSize;
459 char *pImage = (char*)( newImage->GetScalarPointer(0,0,iWidth) );
460 for (j=dimZ-1;j>=0;j--)
463 if (_lseeki64( fd, pos + j*sizeBytesPlane , SEEK_SET ) < 0)
465 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0)
466 #endif // defined(_WIN32)
468 printf("EED ReadMHDPlane::Read64lseek \n");
469 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
473 if ((ret = read(fd, pImage , dimX*dataSize)) < 0)
475 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
478 pImage = pImage+dimX*dataSize;
484 void ReadMHDPlane::Process()
486 if (bbGetInputType()==0)
491 if (bbGetInputType()==1)
498 // 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)
500 void ReadMHDPlane::bbUserSetDefaultValues()
503 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
504 // Here we initialize the input 'In' to 0
505 bbSetInputFileName("");
509 bbSetInputDirectionPlane("XY");
513 // 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)
515 void ReadMHDPlane::bbUserInitializeProcessing()
518 // THE INITIALIZATION METHOD BODY :
520 // but this is where you should allocate the internal/output pointers
526 // 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)
528 void ReadMHDPlane::bbUserFinalizeProcessing()
531 // THE FINALIZATION METHOD BODY :
533 // but this is where you should desallocate the internal/output pointers
538 // EO namespace bbcreaVtk