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;
86 if (bbGetInputDirectionPlane()=="XY")
88 inputfilename=bbGetInputFileName();
92 if ((bbGetInputDirectionPlane()=="YZ") || (bbGetInputDirectionPlane()=="ZX"))
95 std::size_t found = bbGetInputFileName().find_last_of("/\\");
96 std::string path = bbGetInputFileName().substr(0,found+1);
97 std::string filename = bbGetInputFileName().substr(found+1);
103 if (bbGetInputDirectionPlane()=="YZ")
105 inputfilename = path+filename+"_YZ.mhd";
108 if (bbGetInputDirectionPlane()=="ZX")
110 inputfilename = path+filename+"_ZX.mhd";
114 width = bbGetInputWidth();
115 if (width<=0 ) { width=1; }
117 slice = bbGetInputSlice();
118 if (slice<0 ) { slice=0; }
120 vtkImageData *newImage;
121 long long newHeaderSize;
122 std::string newFileName=inputfilename+"-OneSlice";
128 FILE *ffIn = fopen(inputfilename.c_str(),"r");
131 FILE *ffOut = fopen(newFileName.c_str(),"w");
135 strcpy(mystring,"\n");
136 fgets(mystring,250,ffIn);
137 if (strncmp("NDims",mystring,5)==0)
141 strcpy(mystring,"NDims = 2\n");
143 strcpy(mystring,"NDims = 3\n");
146 if (strncmp("DimSize",mystring,6)==0)
148 sscanf(mystring,"%s %s %d %d %d",strTmp, strTmp, &sx, &sy,&sz);
152 sprintf(mystring,"DimSize = %d %d\n",sx,sy);
154 sprintf(mystring,"DimSize = %d %d %d\n",sx,sy,width);
157 newHeaderSize = (long long int)sx*(long long int)sy*(long long int)slice;
158 if (bbGetInputSlice()>=sz) {ok=false;}
160 if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { dataSize=sizeof(char); }
161 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0) { dataSize=sizeof(char); }
162 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { dataSize=sizeof(unsigned char); }
163 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0) { dataSize=sizeof(unsigned char); }
164 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { dataSize=sizeof(unsigned short); }
165 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { dataSize=sizeof(unsigned short); }
166 if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { dataSize=sizeof(short); }
167 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0) { dataSize=sizeof(short); }
168 if (strncmp("ElementType = MET_UINT",mystring,22)==0) { dataSize=sizeof(unsigned int); }
169 if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,30)==0) { dataSize=sizeof(unsigned int); }
170 if (strncmp("ElementType = MET_INT",mystring,21)==0) { dataSize=sizeof(int); }
171 if (strncmp("ElementType = VTK_INT",mystring,21)==0) { dataSize=sizeof(int); }
172 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { dataSize=sizeof(float); }
173 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { dataSize=sizeof(float); }
174 if (strncmp("ElementType = MET_LONG",mystring,22)==0) { dataSize=sizeof(long); }
175 if (strncmp("ElementType = VTK_LONG",mystring,22)==0) { dataSize=sizeof(long); }
176 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0) { dataSize=sizeof(double); }
177 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0) { dataSize=sizeof(double); }
178 newHeaderSize=newHeaderSize*dataSize;
180 if (strncmp("Offset",mystring,6)==0) {strcpy(mystring,"Offset = 0 0 0\n");}
181 if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");}
182 if (strncmp("ElementDataFile",mystring,15)==0)
184 fprintf(ffOut,"HeaderSize = %lld\n\n", newHeaderSize );
186 fprintf(ffOut,mystring);
193 vtkMetaImageReader * reader = vtkMetaImageReader::New();
194 reader->SetFileName( newFileName.c_str() );
196 newImage = reader->GetOutput();
200 printf("EED ERROR: Problem openin:%s\n", inputfilename.c_str() );
205 newImage=CreateDefaultImage();
208 bbSetOutputOut( newImage );
215 void ReadMHDPlane::Read64lseek()
220 width = bbGetInputWidth();
221 if (width<=0 ) { width=1; }
223 slice = bbGetInputSlice();
224 if (slice<0 ) { slice=0; }
226 int dimX=-1,dimY=-1,dimZ=-1;
228 std::string formattype("VOID");
229 std::string elementdatafile("VOID");
230 float spcX=-1,spcY=-1,spcZ=-1;
231 float ox=-1,oy=-1,oz=-1;
232 long int headersize=0;
234 vtkImageData *newImage=NULL;
239 FILE *ffIn = fopen(bbGetInputFileName().c_str(),"r");
244 newImage = vtkImageData::New();
248 strcpy(mystring,"\n");
249 fgets(mystring,250,ffIn);
250 if (strncmp("NDims",mystring,5)==0) { sscanf(mystring,"%s %s %d" ,strTmp, strTmp, &dim); }
251 if (strncmp("DimSize",mystring,6)==0) { sscanf(mystring,"%s %s %d %d %d" ,strTmp, strTmp, &dimX, &dimY,&dimZ); }
252 if (strncmp("ElementType",mystring,11)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); formattype=strTmp2; }
253 if (strncmp("ElementSpacing",mystring,14)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); }
254 if (strncmp("ElementSize",mystring,11)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); }
255 if (strncmp("Offset",mystring,6)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &ox, &oy, &oz); }
256 if (strncmp("HeaderSize",mystring,10)==0) { sscanf(mystring,"%s %s %ld" ,strTmp, strTmp, &headersize); }
257 if (strncmp("ElementDataFile",mystring,15)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2; }
259 if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { newImage->SetScalarTypeToChar(); dataSize=sizeof(char); }
260 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0) { newImage->SetScalarTypeToChar(); dataSize=sizeof(char); }
261 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { newImage->SetScalarTypeToUnsignedChar(); dataSize=sizeof(unsigned char); }
262 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0) { newImage->SetScalarTypeToUnsignedChar(); dataSize=sizeof(unsigned char); }
263 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newImage->SetScalarTypeToUnsignedShort(); dataSize=sizeof(unsigned short);}
264 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { newImage->SetScalarTypeToUnsignedShort(); dataSize=sizeof(unsigned short);}
265 if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { newImage->SetScalarTypeToShort(); dataSize=sizeof(short); }
266 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0) { newImage->SetScalarTypeToShort(); dataSize=sizeof(short); }
267 if (strncmp("ElementType = MET_UINT",mystring,22)==0) { newImage->SetScalarTypeToUnsignedInt(); dataSize=sizeof(unsigned int); }
268 if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,30)==0) { newImage->SetScalarTypeToUnsignedInt(); dataSize=sizeof(unsigned int); }
269 if (strncmp("ElementType = MET_INT",mystring,21)==0) { newImage->SetScalarTypeToInt(); dataSize=sizeof(int); }
270 if (strncmp("ElementType = VTK_INT",mystring,21)==0) { newImage->SetScalarTypeToInt(); dataSize=sizeof(int); }
271 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { newImage->SetScalarTypeToFloat(); dataSize=sizeof(float); }
272 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { newImage->SetScalarTypeToFloat(); dataSize=sizeof(float); }
273 if (strncmp("ElementType = MET_LONG",mystring,22)==0) { newImage->SetScalarTypeToLong(); dataSize=sizeof(long); }
274 if (strncmp("ElementType = VTK_LONG",mystring,22)==0) { newImage->SetScalarTypeToLong(); dataSize=sizeof(long); }
275 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0) { newImage->SetScalarTypeToDouble(); dataSize=sizeof(double); }
276 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0) { newImage->SetScalarTypeToDouble(); dataSize=sizeof(double); }
281 newImage->Initialize();
287 std::string filename;
288 found = bbGetInputFileName().find_last_of("/\\");
289 filename = bbGetInputFileName().substr(0,found+1) + elementdatafile ;
292 long long lsize = dimX*dimY*width *dataSize;
296 _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 );
298 fd = open ( filename.c_str() , O_RDONLY|O_LARGEFILE );
299 #endif // defined(_WIN32)
304 printf("EED ReadMHDPlane::Read64lseek WARNNING! raw file not exist\n");
305 fprintf(stderr, "%s\n", strerror(errno));
306 newImage=CreateDefaultImage();
311 if ((bbGetInputDirectionPlane()=="XY") && (fd>=0))
313 newImage->SetSpacing( spcX,spcY,spcZ );
314 newImage->SetDimensions( dimX,dimY,width );
315 newImage->SetWholeExtent(0, dimX-1,0,dimY-1,0,width-1 );
316 newImage->SetExtent(0, dimX-1,0,dimY-1,0,width-1 );
317 newImage->SetNumberOfScalarComponents(1);
318 newImage->AllocateScalars();
320 pos = dimX*dimY*(long long)slice*dataSize;
322 if (_lseeki64( fd, pos, SEEK_SET ) < 0)
324 if (lseek64(fd, pos, SEEK_SET) < 0)
325 #endif // defined(_WIN32)
327 printf("EED ReadMHDPlane::Read64lseek \n");
328 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
332 if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0)
334 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
339 if ((bbGetInputDirectionPlane()=="XZ") && (fd>=0))
341 newImage->SetSpacing( spcX,spcZ,spcY );
342 newImage->SetDimensions( dimX,dimZ,width );
343 newImage->SetWholeExtent(0, dimX-1,0,dimZ-1,0,width-1 );
344 newImage->SetExtent(0, dimX-1,0,dimZ-1,0,width-1 );
345 newImage->SetNumberOfScalarComponents(1);
346 newImage->AllocateScalars();
349 for (iWidth=0;iWidth<width;iWidth++)
351 copy_XZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
355 if ((bbGetInputDirectionPlane()=="YZ") && (fd>=0))
357 newImage->SetSpacing( spcY,spcZ,spcX );
358 newImage->SetDimensions( dimY,dimZ,width );
359 newImage->SetWholeExtent(0, dimY-1,0,dimZ-1,0,width-1 );
360 newImage->SetExtent(0, dimY-1,0,dimZ-1,0,width-1 );
361 newImage->SetNumberOfScalarComponents(1);
362 newImage->AllocateScalars();
365 for (iWidth=0;iWidth<width;iWidth++)
367 copy_YZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
375 #endif // defined(_WIN32)
377 newImage=CreateDefaultImage();
380 bbSetOutputOut( newImage );
384 void ReadMHDPlane::copy_YZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
390 long long sizeBytesPlane = dimX*dimY*dataSize;
396 pos = ((long long int)slice+iWidth + i*dimX + j*dimX*dimY)*dataSize ;
398 if (_lseeki64( fd, pos , SEEK_SET ) < 0)
400 if (lseek64(fd, pos , SEEK_SET) < 0)
401 #endif // defined(_WIN32)
403 printf("EED ReadMHDPlane::Read64lseek \n");
404 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
408 pImage=(char*)(newImage->GetScalarPointer(i, dimZ-1-j,iWidth ));
409 if ((ret = read(fd, pImage , dataSize)) < 0)
411 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
419 void ReadMHDPlane::copy_XZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
424 pos = dimX*(long long int)slice*dataSize;
425 long long sizeBytesPlane = dimX*dimY*dataSize;
426 char *pImage = (char*)( newImage->GetScalarPointer(0,0,iWidth) );
427 for (j=dimZ-1;j>=0;j--)
430 if (_lseeki64( fd, pos + j*sizeBytesPlane , SEEK_SET ) < 0)
432 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0)
433 #endif // defined(_WIN32)
435 printf("EED ReadMHDPlane::Read64lseek \n");
436 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
440 if ((ret = read(fd, pImage , dimX*dataSize)) < 0)
442 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
445 pImage = pImage+dimX*dataSize;
451 void ReadMHDPlane::Process()
453 if (bbGetInputType()==0)
458 if (bbGetInputType()==1)
465 // 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)
467 void ReadMHDPlane::bbUserSetDefaultValues()
470 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
471 // Here we initialize the input 'In' to 0
472 bbSetInputFileName("");
476 bbSetInputDirectionPlane("XY");
480 // 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)
482 void ReadMHDPlane::bbUserInitializeProcessing()
485 // THE INITIALIZATION METHOD BODY :
487 // but this is where you should allocate the internal/output pointers
493 // 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)
495 void ReadMHDPlane::bbUserFinalizeProcessing()
498 // THE FINALIZATION METHOD BODY :
500 // but this is where you should desallocate the internal/output pointers
505 // EO namespace bbcreaVtk