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>
19 #endif // defined(_WIN32)
30 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,ReadMHDPlane)
31 BBTK_BLACK_BOX_IMPLEMENTATION(ReadMHDPlane,bbtk::AtomicBlackBox);
33 // 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)
37 vtkImageData* ReadMHDPlane::CreateDefaultImage()
40 int sizeX, sizeY, sizeZ;
44 vtkImageData *newImage = vtkImageData::New();
45 newImage->Initialize();
46 newImage->SetSpacing( 1,1,1 );
47 newImage->SetDimensions( sizeX,sizeY,sizeZ );
48 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);
59 memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
60 for (i=0; i<sizeX; i++)
62 newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
63 newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
68 vtkImageData* ReadMHDPlane::ChangeOrientation(vtkImageData* imgOrg)
70 int width = bbGetInputWidth();
72 int sizeXOrg, sizeYOrg,sizeZOrg;
73 int sizeXDst, sizeYDst,sizeZDst;
74 //EED 2017-01-01 Migration VTK7
75 #if VTK_MAJOR_VERSION <= 5
76 imgOrg->GetWholeExtent(ext);
78 imgOrg->GetExtent(ext);
82 sizeXOrg = ext[1]-ext[0]+1;
83 sizeYOrg = ext[3]-ext[2]+1;
84 sizeZOrg = ext[5]-ext[4]+1;
86 if (bbGetInputDirectionPlane()=="XY")
88 sizeXDst = ext[1]-ext[0]+1;
89 sizeYDst = ext[3]-ext[2]+1;
92 if (bbGetInputDirectionPlane()=="YZ")
95 sizeYDst = ext[1]-ext[0]+1;
96 sizeZDst = ext[3]-ext[2]+1;
99 if (bbGetInputDirectionPlane()=="ZX")
101 sizeXDst = ext[1]-ext[0]+1;
103 sizeZDst = ext[3]-ext[2]+1;
106 vtkImageData *imgDst = vtkImageData::New();
107 imgDst->Initialize();
108 imgDst->SetSpacing( imgOrg->GetSpacing() );
109 imgDst->SetDimensions( sizeXDst,sizeYDst,sizeZDst );
110 imgDst->SetExtent(0,sizeXDst-1,0,sizeYDst-1,0,sizeZDst-1 );
112 //EED 2017-01-01 Migration VTK7
113 #if VTK_MAJOR_VERSION <= 5
114 imgDst->SetWholeExtent(0,sizeXDst-1,0,sizeYDst-1,0,sizeZDst-1 );
115 imgDst->SetScalarType( imgOrg->GetScalarType() );
116 imgDst->SetNumberOfScalarComponents(1);
117 imgDst->AllocateScalars();
120 imgDst->AllocateScalars(imgOrg->GetScalarType(),1);
123 char *ptrDst,*ptrOrg;
124 int sizeBytes = imgOrg->GetScalarSize();
125 int sizeLineBytes = sizeLine*imgOrg->GetScalarSize();
128 long int sizeXDstBytes = sizeXDst*sizeBytes;
129 long int sizeXYDstBytes = sizeXDst*sizeYDst*sizeBytes;
130 long int sizeXYDstBytes2 = sizeXDst*sizeYDst*sizeBytes*2;
131 long int sizeXYZDstBytes = sizeXDst*sizeYDst*sizeZDst*sizeBytes;
132 long int sizeXYZDstBytes1 = sizeXDst*sizeYDst*(sizeZDst-1)*sizeBytes;
134 ptrOrg = (char*)( imgOrg->GetScalarPointer() );
136 if (bbGetInputDirectionPlane()=="XY")
138 memcpy ( imgDst->GetScalarPointer(), ptrOrg , sizeXDst*sizeYDst*sizeZDst*(imgOrg->GetScalarSize()) );
141 if (bbGetInputDirectionPlane()=="ZX")
143 ptrDst = (char*)( imgDst->GetScalarPointer(0,0,sizeYOrg-00-1) );
144 for( zz=0 ; zz<sizeZOrg ; zz++)
146 // ptrDst=(char*)( imgDst->GetScalarPointer(0,zz,sizeYOrg-00-1) );
147 for( yy=0 ; yy<sizeYOrg ; yy++)
149 // ptrOrg=(char*)( imgOrg->GetScalarPointer(0,yy,zz) );
150 // ptrDst=(char*)( imgDst->GetScalarPointer(0,zz,sizeYOrg-yy-1) );
151 memcpy ( ptrDst, ptrOrg , sizeLineBytes );
152 ptrOrg = ptrOrg + sizeLineBytes;
153 ptrDst = ptrDst - sizeXYDstBytes;
155 ptrDst = ptrDst + sizeXDstBytes;
156 ptrDst = ptrDst + sizeXYZDstBytes;
160 if (bbGetInputDirectionPlane()=="YZ")
162 ptrDst = (char*)( imgDst->GetScalarPointer(0,0,sizeYOrg-0-1) );
163 for( zz=0 ; zz<sizeZOrg ; zz++)
165 ptrDst=(char*)( imgDst->GetScalarPointer(zz,0,sizeYOrg-0-1) );
166 for( yy=0 ; yy<sizeYOrg ; yy++)
168 // ptrDst=(char*)( imgDst->GetScalarPointer(zz,0,sizeYOrg-yy-1) );
169 for( xx=0 ; xx<sizeXOrg ; xx++)
171 // ptrOrg=(char*)( imgOrg->GetScalarPointer(xx,yy,zz) );
172 // ptrDst=(char*)( imgDst->GetScalarPointer(zz,xx,sizeYOrg-yy-1) );
173 memcpy ( ptrDst, ptrOrg , sizeBytes );
175 ptrDst+= sizeXDstBytes;
177 ptrDst = ptrDst - sizeXYDstBytes2;
179 ptrDst = ptrDst + sizeXYZDstBytes;
187 void ReadMHDPlane::Read64lseek(std::string fileNameIn, std::string plane)
192 width = bbGetInputWidth();
193 if (width<=0 ) { width=1; }
194 slice = bbGetInputSlice();
195 if (slice<0 ) { slice=0; }
196 int dimX=-1,dimY=-1,dimZ=-1;
198 std::string formattype("VOID");
199 std::string elementdatafile("VOID");
200 float spcX=-1,spcY=-1,spcZ=-1;
201 float ox=-1,oy=-1,oz=-1;
202 long int headersize=0;
203 vtkImageData *newImage=NULL;
207 FILE *ffIn = fopen( fileNameIn.c_str() , "r");
211 newImage = vtkImageData::New();
214 strcpy(mystring,"\n");
215 fgets(mystring,250,ffIn);
216 if (strncmp("NDims",mystring,5)==0) { sscanf(mystring,"%s %s %d" ,strTmp, strTmp, &dim); }
217 if (strncmp("DimSize",mystring,6)==0) { sscanf(mystring,"%s %s %d %d %d" ,strTmp, strTmp, &dimX, &dimY,&dimZ); }
218 if (strncmp("ElementType",mystring,11)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); formattype=strTmp2; }
219 if (strncmp("ElementSpacing",mystring,14)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); }
220 if (strncmp("ElementSize",mystring,11)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); }
221 if (strncmp("Offset",mystring,6)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &ox, &oy, &oz); }
222 if (strncmp("HeaderSize",mystring,10)==0) { sscanf(mystring,"%s %s %ld" ,strTmp, strTmp, &headersize); }
223 if (strncmp("ElementDataFile",mystring,15)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2; }
224 if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { imageType=VTK_CHAR; dataSize=sizeof(char); }
225 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0) { imageType=VTK_CHAR; dataSize=sizeof(char); }
226 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { imageType=VTK_UNSIGNED_CHAR; dataSize=sizeof(unsigned char); }
227 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0) { imageType=VTK_UNSIGNED_CHAR; dataSize=sizeof(unsigned char); }
228 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { imageType=VTK_UNSIGNED_SHORT; dataSize=sizeof(unsigned short);}
229 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { imageType=VTK_UNSIGNED_SHORT; dataSize=sizeof(unsigned short);}
230 if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { imageType=VTK_SHORT; dataSize=sizeof(short); }
231 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0) { imageType=VTK_SHORT; dataSize=sizeof(short); }
232 if (strncmp("ElementType = MET_UINT",mystring,22)==0) { imageType=VTK_UNSIGNED_INT; dataSize=sizeof(unsigned int); }
233 if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,30)==0) { imageType=VTK_UNSIGNED_INT; dataSize=sizeof(unsigned int); }
234 if (strncmp("ElementType = MET_INT",mystring,21)==0) { imageType=VTK_INT; dataSize=sizeof(int); }
235 if (strncmp("ElementType = VTK_INT",mystring,21)==0) { imageType=VTK_INT; dataSize=sizeof(int); }
236 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { imageType=VTK_FLOAT; dataSize=sizeof(float); }
237 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { imageType=VTK_FLOAT; dataSize=sizeof(float); }
238 if (strncmp("ElementType = MET_LONG",mystring,22)==0) { imageType=VTK_LONG; dataSize=sizeof(long); }
239 if (strncmp("ElementType = VTK_LONG",mystring,22)==0) { imageType=VTK_LONG; dataSize=sizeof(long); }
240 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0) { imageType=VTK_DOUBLE; dataSize=sizeof(double); }
241 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0) { imageType=VTK_DOUBLE; dataSize=sizeof(double); }
244 newImage->Initialize();
248 std::string filename;
249 found = fileNameIn.find_last_of("/\\");
250 filename = fileNameIn.substr(0,found+1) + elementdatafile ;
252 long long lsize = dimX*dimY*width *dataSize;
254 _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 );
258 fd = open ( filename.c_str() , O_RDONLY );
260 fd = open ( filename.c_str() , O_RDONLY|O_LARGEFILE );
262 #endif // defined(_WIN32)
265 printf("EED ReadMHDPlane::Read64lseek WARNNING! raw file not exist\n");
266 fprintf(stderr, "%s\n", strerror(errno));
267 newImage=CreateDefaultImage();
270 if ((plane=="XY") && (fd>=0))
272 newImage->SetSpacing( spcX,spcY,spcZ );
273 newImage->SetDimensions( dimX,dimY,width );
274 newImage->SetExtent(0, dimX-1,0,dimY-1,0,width-1 );
278 //EED 2017-01-01 Migration VTK7
279 #if VTK_MAJOR_VERSION <= 5
280 newImage->SetScalarType( imageType );
281 newImage->SetWholeExtent(0, dimX-1,0,dimY-1,0,width-1 );
282 newImage->SetNumberOfScalarComponents(1);
283 newImage->AllocateScalars();
286 newImage->AllocateScalars(imageType,1);
289 pos = dimX*dimY*(long long)slice*dataSize;
291 if (_lseeki64( fd, pos, SEEK_SET ) < 0)
295 if (lseek(fd, pos, SEEK_SET) < 0)
297 if (lseek64(fd, pos, SEEK_SET) < 0)
299 #endif // defined(_WIN32)
301 printf("EED ReadMHDPlane::Read64lseek \n");
302 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
305 if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0)
307 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
311 if ((plane=="ZX") && (fd>=0))
313 newImage->SetSpacing( spcX,spcZ,spcY );
314 newImage->SetDimensions( dimX,dimZ,width );
315 newImage->SetExtent(0, dimX-1,0,dimZ-1,0,width-1 );
318 //EED 2017-01-01 Migration VTK7
319 #if VTK_MAJOR_VERSION <= 5
320 newImage->SetScalarType( imageType );
321 newImage->SetWholeExtent(0, dimX-1,0,dimZ-1,0,width-1 );
322 newImage->SetNumberOfScalarComponents(1);
323 newImage->AllocateScalars();
326 newImage->AllocateScalars(imageType,1);
330 for (iWidth=0;iWidth<width;iWidth++)
332 copy_ZX_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
335 if ((plane=="YZ") && (fd>=0))
337 newImage->SetSpacing( spcY,spcZ,spcX );
338 newImage->SetDimensions( dimY,dimZ,width );
339 newImage->SetExtent(0, dimY-1,0,dimZ-1,0,width-1 );
341 //EED 2017-01-01 Migration VTK7
342 #if VTK_MAJOR_VERSION <= 5
343 newImage->SetScalarType( imageType );
344 newImage->SetWholeExtent(0, dimY-1,0,dimZ-1,0,width-1 );
345 newImage->SetNumberOfScalarComponents(1);
346 newImage->AllocateScalars();
349 newImage->AllocateScalars(imageType,1);
353 for (iWidth=0;iWidth<width;iWidth++)
355 copy_YZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
362 #endif // defined(_WIN32)
364 newImage=CreateDefaultImage();
366 bbSetOutputOut( newImage );
367 bbSetOutputOut2( ChangeOrientation(newImage) );
371 void ReadMHDPlane::copy_YZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
377 long long sizeBytesPlane = dimX*dimY*dataSize;
383 pos = ((long long int)slice+iWidth + i*dimX + j*dimX*dimY)*dataSize ;
385 if (_lseeki64( fd, pos , SEEK_SET ) < 0)
389 if (lseek(fd, pos , SEEK_SET) < 0)
391 if (lseek64(fd, pos , SEEK_SET) < 0)
393 #endif // defined(_WIN32)
395 printf("EED ReadMHDPlane::Read64lseek \n");
396 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
400 pImage=(char*)(newImage->GetScalarPointer(i, dimZ-1-j,iWidth ));
401 if ((ret = read(fd, pImage , dataSize)) < 0)
403 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
411 void ReadMHDPlane::copy_ZX_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
416 pos = dimX*(long long int)slice*dataSize;
417 long long sizeBytesPlane = dimX*dimY*dataSize;
418 char *pImage = (char*)( newImage->GetScalarPointer(0,0,iWidth) );
419 for (j=dimZ-1;j>=0;j--)
422 if (_lseeki64( fd, pos + j*sizeBytesPlane , SEEK_SET ) < 0)
426 if (lseek(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0)
428 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0)
430 #endif // defined(_WIN32)
432 printf("EED ReadMHDPlane::Read64lseek \n");
433 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
436 if ((ret = read(fd, pImage , dimX*dataSize)) < 0)
438 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
441 pImage = pImage+dimX*dataSize;
445 void ReadMHDPlane::Process()
447 if (bbGetInputActive()==true)
449 if (bbGetInputType()==0)
452 std::string inputfilename;
453 std::size_t found = bbGetInputFileName().find_last_of("/\\");
454 std::string path = bbGetInputFileName().substr(0,found+1);
455 std::string filename = bbGetInputFileName().substr(found+1);
461 if (bbGetInputDirectionPlane()=="XY") { inputfilename = bbGetInputFileName(); } // if XY
462 if (bbGetInputDirectionPlane()=="YZ") { inputfilename = path+filename+"_YZ.mhd"; } // if YZ
463 if (bbGetInputDirectionPlane()=="ZX") { inputfilename = path+filename+"_ZX.mhd"; } // if XZ
464 Read64lseek( inputfilename ,"XY");
466 if (bbGetInputType()==1)
468 Read64lseek( bbGetInputFileName(), bbGetInputDirectionPlane() );
474 // 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)
476 void ReadMHDPlane::bbUserSetDefaultValues()
478 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
479 // Here we initialize the input 'In' to 0
480 bbSetInputActive(true);
481 bbSetInputFileName("");
485 bbSetInputDirectionPlane("XY");
489 // 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)
491 void ReadMHDPlane::bbUserInitializeProcessing()
493 // THE INITIALIZATION METHOD BODY :
495 // but this is where you should allocate the internal/output pointers
500 // 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)
502 void ReadMHDPlane::bbUserFinalizeProcessing()
504 // THE FINALIZATION METHOD BODY :
506 // but this is where you should desallocate the internal/output pointers
510 } // EO namespace bbcreaVtk