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");
228 printf("EED ReadMHDPlane::Read64lseek filename %s %p\n", bbGetInputFileName().c_str(), ffIn );
233 newImage = vtkImageData::New();
237 strcpy(mystring,"\n");
238 fgets(mystring,250,ffIn);
239 if (strncmp("NDims",mystring,5)==0) { sscanf(mystring,"%s %s %d" ,strTmp, strTmp, &dim); }
240 if (strncmp("DimSize",mystring,6)==0) { sscanf(mystring,"%s %s %d %d %d" ,strTmp, strTmp, &dimX, &dimY,&dimZ); }
241 if (strncmp("ElementType",mystring,11)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); formattype=strTmp2; }
242 if (strncmp("ElementSpacing",mystring,14)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); }
243 if (strncmp("ElementSize",mystring,11)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); }
244 if (strncmp("Offset",mystring,6)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &ox, &oy, &oz); }
245 if (strncmp("HeaderSize",mystring,10)==0) { sscanf(mystring,"%s %s %ld" ,strTmp, strTmp, &headersize); }
246 if (strncmp("ElementDataFile",mystring,15)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2; }
248 if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { newImage->SetScalarTypeToChar(); dataSize=sizeof(char); }
249 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0) { newImage->SetScalarTypeToChar(); dataSize=sizeof(char); }
250 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { newImage->SetScalarTypeToUnsignedChar(); dataSize=sizeof(unsigned char); }
251 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0) { newImage->SetScalarTypeToUnsignedChar(); dataSize=sizeof(unsigned char); }
252 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newImage->SetScalarTypeToUnsignedShort(); dataSize=sizeof(unsigned short);}
253 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { newImage->SetScalarTypeToUnsignedShort(); dataSize=sizeof(unsigned short);}
254 if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { newImage->SetScalarTypeToShort(); dataSize=sizeof(short); }
255 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0) { newImage->SetScalarTypeToShort(); dataSize=sizeof(short); }
256 if (strncmp("ElementType = MET_UINT",mystring,24)==0) { newImage->SetScalarTypeToUnsignedInt(); dataSize=sizeof(unsigned int); }
257 if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,32)==0) { newImage->SetScalarTypeToUnsignedInt(); dataSize=sizeof(unsigned int); }
258 if (strncmp("ElementType = MET_INT",mystring,24)==0) { newImage->SetScalarTypeToInt(); dataSize=sizeof(int); }
259 if (strncmp("ElementType = VTK_INT",mystring,32)==0) { newImage->SetScalarTypeToInt(); dataSize=sizeof(int); }
260 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { newImage->SetScalarTypeToFloat(); dataSize=sizeof(float); }
261 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { newImage->SetScalarTypeToFloat(); dataSize=sizeof(float); }
262 if (strncmp("ElementType = MET_LONG",mystring,23)==0) { newImage->SetScalarTypeToLong(); dataSize=sizeof(long); }
263 if (strncmp("ElementType = VTK_LONG",mystring,23)==0) { newImage->SetScalarTypeToLong(); dataSize=sizeof(long); }
264 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0) { newImage->SetScalarTypeToDouble(); dataSize=sizeof(double); }
265 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0) { newImage->SetScalarTypeToDouble(); dataSize=sizeof(double); }
270 newImage->Initialize();
276 std::string filename;
277 found = bbGetInputFileName().find_last_of("/\\");
278 filename = bbGetInputFileName().substr(0,found+1) + elementdatafile ;
281 long long lsize = dimX*dimY*width *dataSize;
285 _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 );
287 fd = open ( filename.c_str() , O_RDONLY|O_LARGEFILE );
288 #endif // defined(_WIN32)
293 printf("EED ReadMHDPlane::Read64lseek WARNNING! raw file not exist\n");
294 fprintf(stderr, "%s\n", strerror(errno));
295 newImage=CreateDefaultImage();
300 if ((bbGetInputDirectionPlane()=="XY") && (fd>=0))
302 newImage->SetSpacing( spcX,spcY,spcZ );
303 newImage->SetDimensions( dimX,dimY,width );
304 newImage->SetWholeExtent(0, dimX-1,0,dimY-1,0,width-1 );
305 newImage->SetExtent(0, dimX-1,0,dimY-1,0,width-1 );
306 newImage->SetNumberOfScalarComponents(1);
307 newImage->AllocateScalars();
309 pos = dimX*dimY*(long long)slice*dataSize;
311 if (_lseeki64( fd, pos, SEEK_SET ) < 0)
313 if (lseek64(fd, pos, SEEK_SET) < 0)
314 #endif // defined(_WIN32)
316 printf("EED ReadMHDPlane::Read64lseek \n");
317 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
321 if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0)
323 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
328 if ((bbGetInputDirectionPlane()=="XZ") && (fd>=0))
330 newImage->SetSpacing( spcX,spcZ,spcY );
331 newImage->SetDimensions( dimX,dimZ,width );
332 newImage->SetWholeExtent(0, dimX-1,0,dimZ-1,0,width-1 );
333 newImage->SetExtent(0, dimX-1,0,dimZ-1,0,width-1 );
334 newImage->SetNumberOfScalarComponents(1);
335 newImage->AllocateScalars();
338 for (iWidth=0;iWidth<width;iWidth++)
340 copy_XZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
344 if ((bbGetInputDirectionPlane()=="YZ") && (fd>=0))
346 newImage->SetSpacing( spcY,spcZ,spcX );
347 newImage->SetDimensions( dimY,dimZ,width );
348 newImage->SetWholeExtent(0, dimY-1,0,dimZ-1,0,width-1 );
349 newImage->SetExtent(0, dimY-1,0,dimZ-1,0,width-1 );
350 newImage->SetNumberOfScalarComponents(1);
351 newImage->AllocateScalars();
354 for (iWidth=0;iWidth<width;iWidth++)
356 copy_YZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
364 #endif // defined(_WIN32)
366 newImage=CreateDefaultImage();
369 bbSetOutputOut( newImage );
373 void ReadMHDPlane::copy_YZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
379 long long sizeBytesPlane = dimX*dimY*dataSize;
385 pos = ((long long int)slice+iWidth + i*dimX + j*dimX*dimY)*dataSize ;
387 if (_lseeki64( fd, pos , SEEK_SET ) < 0)
389 if (lseek64(fd, pos , SEEK_SET) < 0)
390 #endif // defined(_WIN32)
392 printf("EED ReadMHDPlane::Read64lseek \n");
393 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
397 pImage=(char*)(newImage->GetScalarPointer(i, dimZ-1-j,iWidth ));
398 if ((ret = read(fd, pImage , dataSize)) < 0)
400 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
408 void ReadMHDPlane::copy_XZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
413 pos = dimX*(long long int)slice*dataSize;
414 long long sizeBytesPlane = dimX*dimY*dataSize;
415 char *pImage = (char*)( newImage->GetScalarPointer(0,0,iWidth) );
416 for (j=dimZ-1;j>=0;j--)
419 if (_lseeki64( fd, pos + j*sizeBytesPlane , SEEK_SET ) < 0)
421 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0)
422 #endif // defined(_WIN32)
424 printf("EED ReadMHDPlane::Read64lseek \n");
425 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
429 if ((ret = read(fd, pImage , dimX*dataSize)) < 0)
431 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
434 pImage = pImage+dimX*dataSize;
440 void ReadMHDPlane::Process()
442 if (bbGetInputType()==0)
447 if (bbGetInputType()==1)
454 // 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)
456 void ReadMHDPlane::bbUserSetDefaultValues()
459 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
460 // Here we initialize the input 'In' to 0
461 bbSetInputFileName("");
465 bbSetInputDirectionPlane("XY");
469 // 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)
471 void ReadMHDPlane::bbUserInitializeProcessing()
474 // THE INITIALIZATION METHOD BODY :
476 // but this is where you should allocate the internal/output pointers
482 // 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)
484 void ReadMHDPlane::bbUserFinalizeProcessing()
487 // THE FINALIZATION METHOD BODY :
489 // but this is where you should desallocate the internal/output pointers
494 // EO namespace bbcreaVtk