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()
39 vtkImageData *newImage = NULL;
41 newImage = vtkImageData::New();
43 int sizeX, sizeY, sizeZ;
47 vtkImageData *newImage = vtkImageData::New();
48 newImage->Initialize();
49 newImage->SetSpacing( 1,1,1 );
50 newImage->SetDimensions( sizeX,sizeY,sizeZ );
51 newImage->SetExtent(0, sizeX-1,0,sizeY-1,0,sizeZ-1 );
52 //EED 2017-01-01 Migration VTK7
53 #if VTK_MAJOR_VERSION <= 5
54 newImage->SetWholeExtent(0, sizeX-1,0,sizeY-1,0,sizeZ-1 );
55 newImage->SetScalarTypeToUnsignedChar();
56 newImage->SetNumberOfScalarComponents(1);
57 newImage->AllocateScalars();
60 // newImage->AllocateScalars(VTK_UNSIGNED_CHAR,1);
61 newImage->AllocateScalars(VTK_SHORT,1);
63 // memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*sizeZ*sizeof(unsigned char) );
64 memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*sizeof(short) );
65 // for (i=0; i<sizeX; i++)
67 // newImage->SetScalarComponentFromDouble(i,i ,0, 0, 255 );
68 // newImage->SetScalarComponentFromDouble(i,sizeY-1-i ,0, 0, 255 );
75 vtkImageData* ReadMHDPlane::ChangeOrientation(vtkImageData* imgOrg)
77 vtkImageData *imgDst = NULL;
83 int sizeXOrg, sizeYOrg,sizeZOrg;
84 int sizeXDst, sizeYDst,sizeZDst;
85 //EED 2017-01-01 Migration VTK7
86 #if VTK_MAJOR_VERSION <= 5
87 imgOrg->GetWholeExtent(ext);
89 imgOrg->GetExtent(ext);
93 sizeXOrg = ext[1]-ext[0]+1;
94 sizeYOrg = ext[3]-ext[2]+1;
95 sizeZOrg = ext[5]-ext[4]+1;
98 if (bbGetInputDirectionPlane()=="XY")
100 sizeXDst = ext[1]-ext[0]+1;
101 sizeYDst = ext[3]-ext[2]+1;
104 if (bbGetInputDirectionPlane()=="YZ")
107 sizeYDst = ext[1]-ext[0]+1;
108 sizeZDst = ext[3]-ext[2]+1;
111 if (bbGetInputDirectionPlane()=="ZX")
113 sizeXDst = ext[1]-ext[0]+1;
115 sizeZDst = ext[3]-ext[2]+1;
118 imgDst = vtkImageData::New();
119 imgDst->Initialize();
120 imgDst->SetSpacing( imgOrg->GetSpacing() );
121 imgDst->SetDimensions( sizeXDst,sizeYDst,sizeZDst );
122 imgDst->SetExtent(0,sizeXDst-1,0,sizeYDst-1,0,sizeZDst-1 );
124 //EED 2017-01-01 Migration VTK7
125 #if VTK_MAJOR_VERSION <= 5
126 imgDst->SetWholeExtent(0,sizeXDst-1,0,sizeYDst-1,0,sizeZDst-1 );
127 imgDst->SetScalarType( imgOrg->GetScalarType() );
128 imgDst->SetNumberOfScalarComponents(1);
129 imgDst->AllocateScalars();
132 imgDst->AllocateScalars(imgOrg->GetScalarType(),1);
135 char *ptrDst,*ptrOrg;
136 int sizeBytes = imgOrg->GetScalarSize();
137 int sizeLineBytes = sizeLine*imgOrg->GetScalarSize();
140 long int sizeXDstBytes = sizeXDst*sizeBytes;
141 long int sizeXYDstBytes = sizeXDst*sizeYDst*sizeBytes;
142 long int sizeXYDstBytes2 = sizeXDst*sizeYDst*sizeBytes*2;
143 long int sizeXYZDstBytes = sizeXDst*sizeYDst*sizeZDst*sizeBytes;
144 long int sizeXYZDstBytes1 = sizeXDst*sizeYDst*(sizeZDst-1)*sizeBytes;
146 ptrOrg = (char*)( imgOrg->GetScalarPointer() );
148 if (bbGetInputDirectionPlane()=="XY")
150 memcpy ( imgDst->GetScalarPointer(), ptrOrg , sizeXDst*sizeYDst*sizeZDst*(imgOrg->GetScalarSize()) );
153 if (bbGetInputDirectionPlane()=="ZX")
155 ptrDst = (char*)( imgDst->GetScalarPointer(0,0,sizeYOrg-00-1) );
156 for( zz=0 ; zz<sizeZOrg ; zz++)
158 // ptrDst=(char*)( imgDst->GetScalarPointer(0,zz,sizeYOrg-00-1) );
159 for( yy=0 ; yy<sizeYOrg ; yy++)
161 // ptrOrg=(char*)( imgOrg->GetScalarPointer(0,yy,zz) );
162 // ptrDst=(char*)( imgDst->GetScalarPointer(0,zz,sizeYOrg-yy-1) );
163 memcpy ( ptrDst, ptrOrg , sizeLineBytes );
164 ptrOrg = ptrOrg + sizeLineBytes;
165 ptrDst = ptrDst - sizeXYDstBytes;
167 ptrDst = ptrDst + sizeXDstBytes;
168 ptrDst = ptrDst + sizeXYZDstBytes;
172 if (bbGetInputDirectionPlane()=="YZ")
174 ptrDst = (char*)( imgDst->GetScalarPointer(0,0,sizeYOrg-0-1) );
175 for( zz=0 ; zz<sizeZOrg ; zz++)
177 ptrDst=(char*)( imgDst->GetScalarPointer(zz,0,sizeYOrg-0-1) );
178 for( yy=0 ; yy<sizeYOrg ; yy++)
180 // ptrDst=(char*)( imgDst->GetScalarPointer(zz,0,sizeYOrg-yy-1) );
181 for( xx=0 ; xx<sizeXOrg ; xx++)
183 // ptrOrg=(char*)( imgOrg->GetScalarPointer(xx,yy,zz) );
184 // ptrDst=(char*)( imgDst->GetScalarPointer(zz,xx,sizeYOrg-yy-1) );
185 memcpy ( ptrDst, ptrOrg , sizeBytes );
187 ptrDst+= sizeXDstBytes;
189 ptrDst = ptrDst - sizeXYDstBytes2;
191 ptrDst = ptrDst + sizeXYZDstBytes;
199 void ReadMHDPlane::Read64lseek(std::string fileNameIn, std::string plane)
204 width = bbGetInputWidth();
205 if (width<=0 ) { width=1; }
206 slice = bbGetInputSlice();
207 if (slice<0 ) { slice=0; }
208 int dimX=-1,dimY=-1,dimZ=-1;
210 std::string formattype("VOID");
211 std::string elementdatafile("VOID");
212 float spcX=-1,spcY=-1,spcZ=-1;
213 float ox=-1,oy=-1,oz=-1;
214 long int headersize=0;
215 vtkImageData *newImage=NULL;
219 FILE *ffIn = fopen( fileNameIn.c_str() , "r");
223 newImage = vtkImageData::New();
226 strcpy(mystring,"\n");
227 fgets(mystring,250,ffIn);
228 if (strncmp("NDims",mystring,5)==0) { sscanf(mystring,"%s %s %d" ,strTmp, strTmp, &dim); }
229 if (strncmp("DimSize",mystring,6)==0) { sscanf(mystring,"%s %s %d %d %d" ,strTmp, strTmp, &dimX, &dimY,&dimZ); }
230 if (strncmp("ElementType",mystring,11)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); formattype=strTmp2; }
231 if (strncmp("ElementSpacing",mystring,14)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); }
232 if (strncmp("ElementSize",mystring,11)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); }
233 if (strncmp("Offset",mystring,6)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &ox, &oy, &oz); }
234 if (strncmp("HeaderSize",mystring,10)==0) { sscanf(mystring,"%s %s %ld" ,strTmp, strTmp, &headersize); }
235 if (strncmp("ElementDataFile",mystring,15)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2; }
236 if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { imageType=VTK_CHAR; dataSize=sizeof(char); }
237 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0) { imageType=VTK_CHAR; dataSize=sizeof(char); }
238 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { imageType=VTK_UNSIGNED_CHAR; dataSize=sizeof(unsigned char); }
239 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0) { imageType=VTK_UNSIGNED_CHAR; dataSize=sizeof(unsigned char); }
240 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { imageType=VTK_UNSIGNED_SHORT; dataSize=sizeof(unsigned short);}
241 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { imageType=VTK_UNSIGNED_SHORT; dataSize=sizeof(unsigned short);}
242 if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { imageType=VTK_SHORT; dataSize=sizeof(short); }
243 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0) { imageType=VTK_SHORT; dataSize=sizeof(short); }
244 if (strncmp("ElementType = MET_UINT",mystring,22)==0) { imageType=VTK_UNSIGNED_INT; dataSize=sizeof(unsigned int); }
245 if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,30)==0) { imageType=VTK_UNSIGNED_INT; dataSize=sizeof(unsigned int); }
246 if (strncmp("ElementType = MET_INT",mystring,21)==0) { imageType=VTK_INT; dataSize=sizeof(int); }
247 if (strncmp("ElementType = VTK_INT",mystring,21)==0) { imageType=VTK_INT; dataSize=sizeof(int); }
248 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { imageType=VTK_FLOAT; dataSize=sizeof(float); }
249 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { imageType=VTK_FLOAT; dataSize=sizeof(float); }
250 if (strncmp("ElementType = MET_LONG",mystring,22)==0) { imageType=VTK_LONG; dataSize=sizeof(long); }
251 if (strncmp("ElementType = VTK_LONG",mystring,22)==0) { imageType=VTK_LONG; dataSize=sizeof(long); }
252 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0) { imageType=VTK_DOUBLE; dataSize=sizeof(double); }
253 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0) { imageType=VTK_DOUBLE; dataSize=sizeof(double); }
256 newImage->Initialize();
260 std::string filename;
261 found = fileNameIn.find_last_of("/\\");
262 filename = fileNameIn.substr(0,found+1) + elementdatafile ;
264 long long lsize = dimX*dimY*width *dataSize;
266 _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 );
268 fd = open ( filename.c_str() , O_RDONLY|O_LARGEFILE );
269 #endif // defined(_WIN32)
272 printf("EED ReadMHDPlane::Read64lseek WARNNING! raw file not exist\n");
273 fprintf(stderr, "%s\n", strerror(errno));
274 newImage=CreateDefaultImage();
277 if ((plane=="XY") && (fd>=0))
279 newImage->SetSpacing( spcX,spcY,spcZ );
280 newImage->SetDimensions( dimX,dimY,width );
281 newImage->SetExtent(0, dimX-1,0,dimY-1,0,width-1 );
285 //EED 2017-01-01 Migration VTK7
286 #if VTK_MAJOR_VERSION <= 5
287 newImage->SetScalarType( imageType );
288 newImage->SetWholeExtent(0, dimX-1,0,dimY-1,0,width-1 );
289 newImage->SetNumberOfScalarComponents(1);
290 newImage->AllocateScalars();
293 newImage->AllocateScalars(imageType,1);
296 pos = dimX*dimY*(long long)slice*dataSize;
298 if (_lseeki64( fd, pos, SEEK_SET ) < 0)
300 if (lseek64(fd, pos, SEEK_SET) < 0)
301 #endif // defined(_WIN32)
303 printf("EED ReadMHDPlane::Read64lseek \n");
304 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
307 if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0)
309 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
313 if ((plane=="ZX") && (fd>=0))
315 newImage->SetSpacing( spcX,spcZ,spcY );
316 newImage->SetDimensions( dimX,dimZ,width );
317 newImage->SetExtent(0, dimX-1,0,dimZ-1,0,width-1 );
320 //EED 2017-01-01 Migration VTK7
321 #if VTK_MAJOR_VERSION <= 5
322 newImage->SetScalarType( imageType );
323 newImage->SetWholeExtent(0, dimX-1,0,dimZ-1,0,width-1 );
324 newImage->SetNumberOfScalarComponents(1);
325 newImage->AllocateScalars();
328 newImage->AllocateScalars(imageType,1);
332 for (iWidth=0;iWidth<width;iWidth++)
334 copy_ZX_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
337 if ((plane=="YZ") && (fd>=0))
339 newImage->SetSpacing( spcY,spcZ,spcX );
340 newImage->SetDimensions( dimY,dimZ,width );
341 newImage->SetExtent(0, dimY-1,0,dimZ-1,0,width-1 );
343 //EED 2017-01-01 Migration VTK7
344 #if VTK_MAJOR_VERSION <= 5
345 newImage->SetScalarType( imageType );
346 newImage->SetWholeExtent(0, dimY-1,0,dimZ-1,0,width-1 );
347 newImage->SetNumberOfScalarComponents(1);
348 newImage->AllocateScalars();
351 newImage->AllocateScalars(imageType,1);
355 for (iWidth=0;iWidth<width;iWidth++)
357 copy_YZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
364 #endif // defined(_WIN32)
366 newImage=CreateDefaultImage();
368 bbSetOutputOut( newImage );
369 bbSetOutputOut2( ChangeOrientation(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_ZX_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));
428 if ((ret = read(fd, pImage , dimX*dataSize)) < 0)
430 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
433 pImage = pImage+dimX*dataSize;
437 void ReadMHDPlane::Process()
439 if (bbGetInputActive()==true)
441 if (bbGetInputType()==0)
444 std::string inputfilename;
445 std::size_t found = bbGetInputFileName().find_last_of("/\\");
446 std::string path = bbGetInputFileName().substr(0,found+1);
447 std::string filename = bbGetInputFileName().substr(found+1);
453 if (bbGetInputDirectionPlane()=="XY") { inputfilename = bbGetInputFileName(); } // if XY
454 if (bbGetInputDirectionPlane()=="YZ") { inputfilename = path+filename+"_YZ.mhd"; } // if YZ
455 if (bbGetInputDirectionPlane()=="ZX") { inputfilename = path+filename+"_ZX.mhd"; } // if XZ
456 Read64lseek( inputfilename ,"XY");
458 if (bbGetInputType()==1)
460 Read64lseek( bbGetInputFileName(), bbGetInputDirectionPlane() );
466 // 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)
468 void ReadMHDPlane::bbUserSetDefaultValues()
470 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
471 // Here we initialize the input 'In' to 0
472 bbSetInputActive(true);
473 bbSetInputFileName("");
477 bbSetInputDirectionPlane("XY");
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::bbUserInitializeProcessing()
485 // THE INITIALIZATION METHOD BODY :
487 // but this is where you should allocate the internal/output pointers
492 // 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)
494 void ReadMHDPlane::bbUserFinalizeProcessing()
496 // THE FINALIZATION METHOD BODY :
498 // but this is where you should desallocate the internal/output pointers
502 } // EO namespace bbcreaVtk