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->SetScalarTypeToUnsignedChar();
46 newImage->SetSpacing( 1,1,1 );
47 newImage->SetDimensions( sizeX,sizeY,sizeZ );
48 newImage->SetWholeExtent(0, sizeX-1,0,sizeY-1,0,sizeZ-1 );
49 newImage->SetExtent(0, sizeX-1,0,sizeY-1,0,sizeZ-1 );
50 newImage->SetNumberOfScalarComponents(1);
51 newImage->AllocateScalars();
53 memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
54 for (i=0; i<sizeX; i++)
56 newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
57 newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
63 void ReadMHDPlane::ReadNormalMHD()
65 // THE MAIN PROCESSING METHOD BODY
66 // Here we simply set the input 'In' value to the output 'Out'
67 // And print out the output value
68 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
69 // void bbSet{Input|Output}NAME(const TYPE&)
70 // const TYPE& bbGet{Input|Output}NAME() const
72 // * NAME is the name of the input/output
73 // (the one provided in the attribute 'name' of the tag 'input')
74 // * TYPE is the C++ type of the input/output
75 // (the one provided in the attribute 'type' of the tag 'input')
77 // bbSetOutputOut( bbGetInputIn() );
78 // std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
80 std::string inputfilename;
84 if (bbGetInputDirectionPlane()=="XY")
86 inputfilename=bbGetInputFileName();
90 if ((bbGetInputDirectionPlane()=="YZ") || (bbGetInputDirectionPlane()=="ZX"))
94 std::size_t found = bbGetInputFileName().find_last_of("/\\");
95 std::string path = bbGetInputFileName().substr(0,found+1);
96 std::string filename = bbGetInputFileName().substr(found+1);
104 if (bbGetInputDirectionPlane()=="YZ")
106 inputfilename = path+filename+"_YZ.mhd";
109 if (bbGetInputDirectionPlane()=="ZX")
111 inputfilename = path+filename+"_ZX.mhd";
115 width = bbGetInputWidth();
116 if (width<=0 ) { width=1; }
118 slice = bbGetInputSlice();
119 if (slice<0 ) { slice=0; }
122 vtkImageData *newImage;
123 long long int newHeaderSize;
124 std::string newFileName=inputfilename+"-OneSlice";
130 FILE *ffIn = fopen(inputfilename.c_str(),"r");
133 FILE *ffOut = fopen(newFileName.c_str(),"w");
137 strcpy(mystring,"\n");
138 fgets(mystring,250,ffIn);
139 if (strncmp("NDims",mystring,5)==0)
143 strcpy(mystring,"NDims = 2\n");
145 strcpy(mystring,"NDims = 3\n");
148 if (strncmp("DimSize",mystring,6)==0)
150 sscanf(mystring,"%s %s %d %d %d",strTmp, strTmp, &sx, &sy,&sz);
154 sprintf(mystring,"DimSize = %d %d\n",sx,sy);
156 sprintf(mystring,"DimSize = %d %d %d\n",sx,sy,width);
159 newHeaderSize = (long long int)sx*(long long int)sy*(long long int)slice;
160 if (bbGetInputSlice()>=sz) {ok=false;}
162 if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { newHeaderSize=newHeaderSize*1; }
163 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { newHeaderSize=newHeaderSize*1; }
164 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; }
165 if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { newHeaderSize=newHeaderSize*2; }
166 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { newHeaderSize=newHeaderSize*4; }
167 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { newHeaderSize=newHeaderSize*4; }
168 if (strncmp("Offset",mystring,6)==0) {strcpy(mystring,"Offset = 0 0 0\n");}
169 if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");}
170 if (strncmp("ElementDataFile",mystring,15)==0)
172 fprintf(ffOut,"HeaderSize = %lld\n\n", newHeaderSize );
174 fprintf(ffOut,mystring);
181 vtkMetaImageReader * reader = vtkMetaImageReader::New();
182 reader->SetFileName( newFileName.c_str() );
184 newImage = reader->GetOutput();
188 printf("EED ERROR: Problem openin:%s\n", inputfilename.c_str() );
193 newImage=CreateDefaultImage();
196 bbSetOutputOut( newImage );
203 void ReadMHDPlane::Read64lseek()
208 width = bbGetInputWidth();
209 if (width<=0 ) { width=1; }
211 slice = bbGetInputSlice();
212 if (slice<0 ) { slice=0; }
214 int dimX=-1,dimY=-1,dimZ=-1;
216 std::string formattype("VOID");
217 std::string elementdatafile("VOID");
218 float spcX=-1,spcY=-1,spcZ=-1;
219 float ox=-1,oy=-1,oz=-1;
220 long int headersize=0;
222 vtkImageData *newImage=NULL;
227 FILE *ffIn = fopen(bbGetInputFileName().c_str(),"r");
232 newImage = vtkImageData::New();
236 strcpy(mystring,"\n");
237 fgets(mystring,250,ffIn);
238 if (strncmp("NDims",mystring,5)==0) { sscanf(mystring,"%s %s %d" ,strTmp, strTmp, &dim); }
239 if (strncmp("DimSize",mystring,6)==0) { sscanf(mystring,"%s %s %d %d %d" ,strTmp, strTmp, &dimX, &dimY,&dimZ); }
240 if (strncmp("ElementType",mystring,11)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); formattype=strTmp2; }
241 if (strncmp("ElementSpacing",mystring,14)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); }
242 if (strncmp("ElementSize",mystring,11)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); }
243 if (strncmp("Offset",mystring,6)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &ox, &oy, &oz); }
244 if (strncmp("HeaderSize",mystring,10)==0) { sscanf(mystring,"%s %s %ld" ,strTmp, strTmp, &headersize); }
245 if (strncmp("ElementDataFile",mystring,15)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2; }
247 if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { newImage->SetScalarTypeToChar(); dataSize=sizeof(char); }
248 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0) { newImage->SetScalarTypeToChar(); dataSize=sizeof(char); }
249 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { newImage->SetScalarTypeToUnsignedChar(); dataSize=sizeof(unsigned char); }
250 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0) { newImage->SetScalarTypeToUnsignedChar(); dataSize=sizeof(unsigned char); }
251 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newImage->SetScalarTypeToUnsignedShort(); dataSize=sizeof(unsigned short);}
252 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { newImage->SetScalarTypeToUnsignedShort(); dataSize=sizeof(unsigned short);}
253 if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { newImage->SetScalarTypeToShort(); dataSize=sizeof(short); }
254 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0) { newImage->SetScalarTypeToShort(); dataSize=sizeof(short); }
255 if (strncmp("ElementType = MET_UINT",mystring,24)==0) { newImage->SetScalarTypeToUnsignedInt(); dataSize=sizeof(unsigned int); }
256 if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,32)==0) { newImage->SetScalarTypeToUnsignedInt(); dataSize=sizeof(unsigned int); }
257 if (strncmp("ElementType = MET_INT",mystring,24)==0) { newImage->SetScalarTypeToInt(); dataSize=sizeof(int); }
258 if (strncmp("ElementType = VTK_INT",mystring,32)==0) { newImage->SetScalarTypeToInt(); dataSize=sizeof(int); }
259 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { newImage->SetScalarTypeToFloat(); dataSize=sizeof(float); }
260 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { newImage->SetScalarTypeToFloat(); dataSize=sizeof(float); }
261 if (strncmp("ElementType = MET_LONG",mystring,23)==0) { newImage->SetScalarTypeToLong(); dataSize=sizeof(long); }
262 if (strncmp("ElementType = VTK_LONG",mystring,23)==0) { newImage->SetScalarTypeToLong(); dataSize=sizeof(long); }
263 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0) { newImage->SetScalarTypeToDouble(); dataSize=sizeof(double); }
264 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0) { newImage->SetScalarTypeToDouble(); dataSize=sizeof(double); }
269 newImage->Initialize();
275 std::string filename;
276 found = bbGetInputFileName().find_last_of("/\\");
277 filename = bbGetInputFileName().substr(0,found+1) + elementdatafile ;
280 long long lsize = dimX*dimY*width *dataSize;
284 _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 );
286 fd = open ( filename.c_str() , O_RDONLY|O_LARGEFILE );
287 #endif // defined(_WIN32)
292 printf("EED ReadMHDPlane::Read64lseek WARNNING! raw file not exist\n");
293 fprintf(stderr, "%s\n", strerror(errno));
294 newImage=CreateDefaultImage();
299 if ((bbGetInputDirectionPlane()=="XY") && (fd>=0))
301 newImage->SetSpacing( spcX,spcY,spcZ );
302 newImage->SetDimensions( dimX,dimY,width );
303 newImage->SetWholeExtent(0, dimX-1,0,dimY-1,0,width-1 );
304 newImage->SetExtent(0, dimX-1,0,dimY-1,0,width-1 );
305 newImage->SetNumberOfScalarComponents(1);
306 newImage->AllocateScalars();
308 pos = dimX*dimY*(long long)slice*dataSize;
310 if (_lseeki64( fd, pos, SEEK_SET ) < 0)
312 if (lseek64(fd, pos, SEEK_SET) < 0)
313 #endif // defined(_WIN32)
315 printf("EED ReadMHDPlane::Read64lseek \n");
316 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
320 if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0)
322 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
327 if ((bbGetInputDirectionPlane()=="XZ") && (fd>=0))
329 newImage->SetSpacing( spcX,spcZ,spcY );
330 newImage->SetDimensions( dimX,dimZ,width );
331 newImage->SetWholeExtent(0, dimX-1,0,dimZ-1,0,width-1 );
332 newImage->SetExtent(0, dimX-1,0,dimZ-1,0,width-1 );
333 newImage->SetNumberOfScalarComponents(1);
334 newImage->AllocateScalars();
337 for (iWidth=0;iWidth<width;iWidth++)
339 copy_XZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
343 if ((bbGetInputDirectionPlane()=="YZ") && (fd>=0))
345 newImage->SetSpacing( spcY,spcZ,spcX );
346 newImage->SetDimensions( dimY,dimZ,width );
347 newImage->SetWholeExtent(0, dimY-1,0,dimZ-1,0,width-1 );
348 newImage->SetExtent(0, dimY-1,0,dimZ-1,0,width-1 );
349 newImage->SetNumberOfScalarComponents(1);
350 newImage->AllocateScalars();
353 for (iWidth=0;iWidth<width;iWidth++)
355 copy_YZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
363 #endif // defined(_WIN32)
365 newImage=CreateDefaultImage();
368 bbSetOutputOut( newImage );
372 void ReadMHDPlane::copy_YZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
378 long long sizeBytesPlane = dimX*dimY*dataSize;
384 pos = ((long long int)slice+iWidth + i*dimX + j*dimX*dimY)*dataSize ;
386 if (_lseeki64( fd, pos , SEEK_SET ) < 0)
388 if (lseek64(fd, pos , SEEK_SET) < 0)
389 #endif // defined(_WIN32)
391 printf("EED ReadMHDPlane::Read64lseek \n");
392 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
396 pImage=(char*)(newImage->GetScalarPointer(i, dimZ-1-j,iWidth ));
397 if ((ret = read(fd, pImage , dataSize)) < 0)
399 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
407 void ReadMHDPlane::copy_XZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
412 pos = dimX*(long long int)slice*dataSize;
413 long long sizeBytesPlane = dimX*dimY*dataSize;
414 char *pImage = (char*)( newImage->GetScalarPointer(0,0,iWidth) );
415 for (j=dimZ-1;j>=0;j--)
418 if (_lseeki64( fd, pos + j*sizeBytesPlane , SEEK_SET ) < 0)
420 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0)
421 #endif // defined(_WIN32)
423 printf("EED ReadMHDPlane::Read64lseek \n");
424 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
428 if ((ret = read(fd, pImage , dimX*dataSize)) < 0)
430 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
433 pImage = pImage+dimX*dataSize;
439 void ReadMHDPlane::Process()
441 if (bbGetInputType()==0)
446 if (bbGetInputType()==1)
453 // 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)
455 void ReadMHDPlane::bbUserSetDefaultValues()
458 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
459 // Here we initialize the input 'In' to 0
460 bbSetInputFileName("");
464 bbSetInputDirectionPlane("XY");
468 // 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)
470 void ReadMHDPlane::bbUserInitializeProcessing()
473 // THE INITIALIZATION METHOD BODY :
475 // but this is where you should allocate the internal/output pointers
481 // 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)
483 void ReadMHDPlane::bbUserFinalizeProcessing()
486 // THE FINALIZATION METHOD BODY :
488 // but this is where you should desallocate the internal/output pointers
493 // EO namespace bbcreaVtk