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 );
256 fd = open ( filename.c_str() , O_RDONLY|O_LARGEFILE );
257 #endif // defined(_WIN32)
260 printf("EED ReadMHDPlane::Read64lseek WARNNING! raw file not exist\n");
261 fprintf(stderr, "%s\n", strerror(errno));
262 newImage=CreateDefaultImage();
265 if ((plane=="XY") && (fd>=0))
267 newImage->SetSpacing( spcX,spcY,spcZ );
268 newImage->SetDimensions( dimX,dimY,width );
269 newImage->SetExtent(0, dimX-1,0,dimY-1,0,width-1 );
273 //EED 2017-01-01 Migration VTK7
274 #if VTK_MAJOR_VERSION <= 5
275 newImage->SetScalarType( imageType );
276 newImage->SetWholeExtent(0, dimX-1,0,dimY-1,0,width-1 );
277 newImage->SetNumberOfScalarComponents(1);
278 newImage->AllocateScalars();
281 newImage->AllocateScalars(imageType,1);
284 pos = dimX*dimY*(long long)slice*dataSize;
286 if (_lseeki64( fd, pos, SEEK_SET ) < 0)
288 if (lseek64(fd, pos, SEEK_SET) < 0)
289 #endif // defined(_WIN32)
291 printf("EED ReadMHDPlane::Read64lseek \n");
292 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
295 if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0)
297 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
301 if ((plane=="ZX") && (fd>=0))
303 newImage->SetSpacing( spcX,spcZ,spcY );
304 newImage->SetDimensions( dimX,dimZ,width );
305 newImage->SetExtent(0, dimX-1,0,dimZ-1,0,width-1 );
308 //EED 2017-01-01 Migration VTK7
309 #if VTK_MAJOR_VERSION <= 5
310 newImage->SetScalarType( imageType );
311 newImage->SetWholeExtent(0, dimX-1,0,dimZ-1,0,width-1 );
312 newImage->SetNumberOfScalarComponents(1);
313 newImage->AllocateScalars();
316 newImage->AllocateScalars(imageType,1);
320 for (iWidth=0;iWidth<width;iWidth++)
322 copy_ZX_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
325 if ((plane=="YZ") && (fd>=0))
327 newImage->SetSpacing( spcY,spcZ,spcX );
328 newImage->SetDimensions( dimY,dimZ,width );
329 newImage->SetExtent(0, dimY-1,0,dimZ-1,0,width-1 );
331 //EED 2017-01-01 Migration VTK7
332 #if VTK_MAJOR_VERSION <= 5
333 newImage->SetScalarType( imageType );
334 newImage->SetWholeExtent(0, dimY-1,0,dimZ-1,0,width-1 );
335 newImage->SetNumberOfScalarComponents(1);
336 newImage->AllocateScalars();
339 newImage->AllocateScalars(imageType,1);
343 for (iWidth=0;iWidth<width;iWidth++)
345 copy_YZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
352 #endif // defined(_WIN32)
354 newImage=CreateDefaultImage();
356 bbSetOutputOut( newImage );
357 bbSetOutputOut2( ChangeOrientation(newImage) );
361 void ReadMHDPlane::copy_YZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
367 long long sizeBytesPlane = dimX*dimY*dataSize;
373 pos = ((long long int)slice+iWidth + i*dimX + j*dimX*dimY)*dataSize ;
375 if (_lseeki64( fd, pos , SEEK_SET ) < 0)
377 if (lseek64(fd, pos , SEEK_SET) < 0)
378 #endif // defined(_WIN32)
380 printf("EED ReadMHDPlane::Read64lseek \n");
381 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
385 pImage=(char*)(newImage->GetScalarPointer(i, dimZ-1-j,iWidth ));
386 if ((ret = read(fd, pImage , dataSize)) < 0)
388 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
396 void ReadMHDPlane::copy_ZX_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
401 pos = dimX*(long long int)slice*dataSize;
402 long long sizeBytesPlane = dimX*dimY*dataSize;
403 char *pImage = (char*)( newImage->GetScalarPointer(0,0,iWidth) );
404 for (j=dimZ-1;j>=0;j--)
407 if (_lseeki64( fd, pos + j*sizeBytesPlane , SEEK_SET ) < 0)
409 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0)
410 #endif // defined(_WIN32)
412 printf("EED ReadMHDPlane::Read64lseek \n");
413 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
416 if ((ret = read(fd, pImage , dimX*dataSize)) < 0)
418 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
421 pImage = pImage+dimX*dataSize;
425 void ReadMHDPlane::Process()
427 if (bbGetInputActive()==true)
429 if (bbGetInputType()==0)
432 std::string inputfilename;
433 std::size_t found = bbGetInputFileName().find_last_of("/\\");
434 std::string path = bbGetInputFileName().substr(0,found+1);
435 std::string filename = bbGetInputFileName().substr(found+1);
441 if (bbGetInputDirectionPlane()=="XY") { inputfilename = bbGetInputFileName(); } // if XY
442 if (bbGetInputDirectionPlane()=="YZ") { inputfilename = path+filename+"_YZ.mhd"; } // if YZ
443 if (bbGetInputDirectionPlane()=="ZX") { inputfilename = path+filename+"_ZX.mhd"; } // if XZ
444 Read64lseek( inputfilename ,"XY");
446 if (bbGetInputType()==1)
448 Read64lseek( bbGetInputFileName(), bbGetInputDirectionPlane() );
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()
458 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
459 // Here we initialize the input 'In' to 0
460 bbSetInputActive(true);
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()
473 // THE INITIALIZATION METHOD BODY :
475 // but this is where you should allocate the internal/output pointers
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::bbUserFinalizeProcessing()
484 // THE FINALIZATION METHOD BODY :
486 // but this is where you should desallocate the internal/output pointers
490 } // EO namespace bbcreaVtk